os.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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-2001 *
  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.26 2001/06/04 05:50:10 xiphmont Exp $
  16. ********************************************************************/
  17. #include <math.h>
  18. #include <ogg/os_types.h>
  19. #ifndef _V_IFDEFJAIL_H_
  20. # define _V_IFDEFJAIL_H_
  21. # ifdef __GNUC__
  22. # define STIN static inline
  23. # elif _WIN32
  24. # define STIN static __inline
  25. #else
  26. # define STIN static
  27. #endif
  28. #ifndef M_PI
  29. # define M_PI (3.1415926536f)
  30. #endif
  31. #ifdef _WIN32
  32. # include <malloc.h>
  33. # define rint(x) (floor((x)+0.5f))
  34. # define NO_FLOAT_MATH_LIB
  35. # define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b))
  36. #endif
  37. #ifdef HAVE_SQRTF
  38. # define sqrt sqrtf
  39. #endif
  40. #ifdef HAVE_LOGF
  41. # define log logf
  42. #endif
  43. #ifdef HAVE_EXPF
  44. # define exp expf
  45. #endif
  46. #ifdef HAVE_POWF
  47. # define pow powf
  48. #endif
  49. #ifdef HAVE_ACOSF
  50. # define acos acosf
  51. #endif
  52. #ifdef HAVE_ATANF
  53. # define atan atanf
  54. #endif
  55. #ifdef HAVE_FREXPF
  56. # define frexp frexpf
  57. #endif
  58. #ifdef HAVE_RINTF
  59. # define rint rintf
  60. #endif
  61. #ifndef FAST_HYPOT
  62. # define FAST_HYPOT hypot
  63. #endif
  64. #endif
  65. #ifdef HAVE_ALLOCA_H
  66. # include <alloca.h>
  67. #endif
  68. #ifdef USE_MEMORY_H
  69. # include <memory.h>
  70. #endif
  71. #ifndef min
  72. # define min(x,y) ((x)>(y)?(y):(x))
  73. #endif
  74. #ifndef max
  75. # define max(x,y) ((x)<(y)?(y):(x))
  76. #endif
  77. #if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__)
  78. # define VORBIS_FPU_CONTROL
  79. /* both GCC and MSVC are kinda stupid about rounding/casting to int.
  80. Because of encapsulation constraints (GCC can't see inside the asm
  81. block and so we end up doing stupid things like a store/load that
  82. is collectively a noop), we do it this way */
  83. /* we must set up the fpu before this works!! */
  84. typedef ogg_int16_t vorbis_fpu_control;
  85. static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
  86. ogg_int16_t ret;
  87. ogg_int16_t temp;
  88. __asm__ __volatile__("fnstcw %0\n\t"
  89. "movw %0,%%dx\n\t"
  90. "orw $62463,%%dx\n\t"
  91. "movw %%dx,%1\n\t"
  92. "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
  93. *fpu=ret;
  94. }
  95. static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
  96. __asm__ __volatile__("fldcw %0":: "m"(fpu));
  97. }
  98. /* assumes the FPU is in round mode! */
  99. static inline int vorbis_ftoi(double f){ /* yes, double! Otherwise,
  100. we get extra fst/fld to
  101. truncate precision */
  102. int i;
  103. __asm__("fistl %0": "=m"(i) : "t"(f));
  104. return(i);
  105. }
  106. #endif
  107. #if defined(_WIN32) && !defined(__GNUC__)
  108. # define VORBIS_FPU_CONTROL
  109. typedef ogg_int16_t vorbis_fpu_control;
  110. static __inline int vorbis_ftoi(double f){
  111. int i;
  112. __asm{
  113. fld f
  114. fistp i
  115. }
  116. return i;
  117. }
  118. static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
  119. }
  120. static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
  121. }
  122. #endif
  123. #ifndef VORBIS_FPU_CONTROL
  124. typedef int vorbis_fpu_control;
  125. static int vorbis_ftoi(double f){
  126. return (int)(f+.5);
  127. }
  128. /* We don't have special code for this compiler/arch, so do it the slow way */
  129. # define vorbis_fpu_setround(vorbis_fpu_control) {}
  130. # define vorbis_fpu_restore(vorbis_fpu_control) {}
  131. #endif
  132. #endif /* _OS_H */