os.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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.27.6.1 2001/12/17 05:39:24 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_ACOSF
  47. # define acos acosf
  48. #endif
  49. #ifdef HAVE_ATANF
  50. # define atan atanf
  51. #endif
  52. #ifdef HAVE_FREXPF
  53. # define frexp frexpf
  54. #endif
  55. #ifdef HAVE_RINTF
  56. # define rint rintf
  57. #endif
  58. #ifndef FAST_HYPOT
  59. # define FAST_HYPOT hypot
  60. #endif
  61. #endif
  62. #ifdef HAVE_ALLOCA_H
  63. # include <alloca.h>
  64. #endif
  65. #ifdef USE_MEMORY_H
  66. # include <memory.h>
  67. #endif
  68. #ifndef min
  69. # define min(x,y) ((x)>(y)?(y):(x))
  70. #endif
  71. #ifndef max
  72. # define max(x,y) ((x)<(y)?(y):(x))
  73. #endif
  74. #if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__)
  75. # define VORBIS_FPU_CONTROL
  76. /* both GCC and MSVC are kinda stupid about rounding/casting to int.
  77. Because of encapsulation constraints (GCC can't see inside the asm
  78. block and so we end up doing stupid things like a store/load that
  79. is collectively a noop), we do it this way */
  80. /* we must set up the fpu before this works!! */
  81. typedef ogg_int16_t vorbis_fpu_control;
  82. static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
  83. ogg_int16_t ret;
  84. ogg_int16_t temp;
  85. __asm__ __volatile__("fnstcw %0\n\t"
  86. "movw %0,%%dx\n\t"
  87. "orw $62463,%%dx\n\t"
  88. "movw %%dx,%1\n\t"
  89. "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
  90. *fpu=ret;
  91. }
  92. static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
  93. __asm__ __volatile__("fldcw %0":: "m"(fpu));
  94. }
  95. /* assumes the FPU is in round mode! */
  96. static inline int vorbis_ftoi(double f){ /* yes, double! Otherwise,
  97. we get extra fst/fld to
  98. truncate precision */
  99. int i;
  100. __asm__("fistl %0": "=m"(i) : "t"(f));
  101. return(i);
  102. }
  103. #endif
  104. #if defined(_WIN32) && !defined(__GNUC__)
  105. # define VORBIS_FPU_CONTROL
  106. typedef ogg_int16_t vorbis_fpu_control;
  107. static __inline int vorbis_ftoi(double f){
  108. int i;
  109. __asm{
  110. fld f
  111. fistp i
  112. }
  113. return i;
  114. }
  115. static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
  116. }
  117. static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
  118. }
  119. #endif
  120. #ifndef VORBIS_FPU_CONTROL
  121. typedef int vorbis_fpu_control;
  122. static int vorbis_ftoi(double f){
  123. return (int)(f+.5);
  124. }
  125. /* We don't have special code for this compiler/arch, so do it the slow way */
  126. # define vorbis_fpu_setround(vorbis_fpu_control) {}
  127. # define vorbis_fpu_restore(vorbis_fpu_control) {}
  128. #endif
  129. #endif /* _OS_H */