os.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #ifndef _OS_H
  2. #define _OS_H
  3. /********************************************************************
  4. * *
  5. * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
  6. * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
  7. * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
  8. * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
  9. * *
  10. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
  11. * by the XIPHOPHORUS Company http://www.xiph.org/ *
  12. * *
  13. ********************************************************************
  14. function: #ifdef jail to whip a few platforms into the UNIX ideal.
  15. last mod: $Id: os.h,v 1.33 2003/09/02 05:11:53 xiphmont Exp $
  16. ********************************************************************/
  17. #include <math.h>
  18. #include "../ogg/os_types.h"
  19. #include "misc.h"
  20. #ifndef _V_IFDEFJAIL_H_
  21. # define _V_IFDEFJAIL_H_
  22. # ifdef __GNUC__
  23. # define STIN static __inline__
  24. # elif _WIN32
  25. # define STIN static __inline
  26. # else
  27. # define STIN static
  28. # endif
  29. #ifdef DJGPP
  30. # define rint(x) (floor((x)+0.5f))
  31. #endif
  32. #ifndef M_PI
  33. # define M_PI (3.1415926536f)
  34. #endif
  35. #ifdef _WIN32
  36. # include <malloc.h>
  37. # define rint(x) (floor((x)+0.5f))
  38. # define NO_FLOAT_MATH_LIB
  39. # define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b))
  40. #endif
  41. #ifndef FAST_HYPOT
  42. # define FAST_HYPOT hypot
  43. #endif
  44. #endif
  45. #ifdef HAVE_ALLOCA_H
  46. # include <alloca.h>
  47. #endif
  48. #ifdef USE_MEMORY_H
  49. # include <memory.h>
  50. #endif
  51. #ifndef min
  52. # define min(x,y) ((x)>(y)?(y):(x))
  53. #endif
  54. #ifndef max
  55. # define max(x,y) ((x)<(y)?(y):(x))
  56. #endif
  57. #if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__)
  58. # define VORBIS_FPU_CONTROL
  59. /* both GCC and MSVC are kinda stupid about rounding/casting to int.
  60. Because of encapsulation constraints (GCC can't see inside the asm
  61. block and so we end up doing stupid things like a store/load that
  62. is collectively a noop), we do it this way */
  63. /* we must set up the fpu before this works!! */
  64. typedef ogg_int16_t vorbis_fpu_control;
  65. static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
  66. ogg_int16_t ret;
  67. ogg_int16_t temp;
  68. __asm__ __volatile__("fnstcw %0\n\t"
  69. "movw %0,%%dx\n\t"
  70. "orw $62463,%%dx\n\t"
  71. "movw %%dx,%1\n\t"
  72. "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
  73. *fpu=ret;
  74. }
  75. static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
  76. __asm__ __volatile__("fldcw %0":: "m"(fpu));
  77. }
  78. /* assumes the FPU is in round mode! */
  79. static inline int vorbis_ftoi(double f){ /* yes, double! Otherwise,
  80. we get extra fst/fld to
  81. truncate precision */
  82. int i;
  83. __asm__("fistl %0": "=m"(i) : "t"(f));
  84. return(i);
  85. }
  86. #endif
  87. #if defined(_WIN32) && !defined(__GNUC__) && !defined(__BORLANDC__)
  88. # define VORBIS_FPU_CONTROL
  89. typedef ogg_int16_t vorbis_fpu_control;
  90. static __inline int vorbis_ftoi(double f){
  91. int i;
  92. __asm{
  93. fld f
  94. fistp i
  95. }
  96. return i;
  97. }
  98. static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
  99. }
  100. static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
  101. }
  102. #endif
  103. #ifndef VORBIS_FPU_CONTROL
  104. typedef int vorbis_fpu_control;
  105. static int vorbis_ftoi(double f){
  106. return (int)(f+.5);
  107. }
  108. /* We don't have special code for this compiler/arch, so do it the slow way */
  109. # define vorbis_fpu_setround(vorbis_fpu_control) {}
  110. # define vorbis_fpu_restore(vorbis_fpu_control) {}
  111. #endif
  112. #endif /* _OS_H */