os.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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-2015 *
  11. * by the Xiph.Org Foundation https://xiph.org/ *
  12. * *
  13. ********************************************************************
  14. function: #ifdef jail to whip a few platforms into the UNIX ideal.
  15. ********************************************************************/
  16. #ifdef HAVE_CONFIG_H
  17. #include "config.h"
  18. #endif
  19. #include <math.h>
  20. #include <ogg/os_types.h>
  21. #include "misc.h"
  22. #ifndef _V_IFDEFJAIL_H_
  23. # define _V_IFDEFJAIL_H_
  24. # ifdef __GNUC__
  25. # define STIN static __inline__
  26. # elif defined(_WIN32)
  27. # define STIN static __inline
  28. # else
  29. # define STIN static
  30. # endif
  31. #ifdef DJGPP
  32. # define rint(x) (floor((x)+0.5f))
  33. #endif
  34. #ifndef M_PI
  35. # define M_PI (3.1415926536f)
  36. #endif
  37. #if defined(_WIN32) && !defined(__SYMBIAN32__)
  38. # include <malloc.h>
  39. # define rint(x) (floor((x)+0.5f))
  40. # define NO_FLOAT_MATH_LIB
  41. # define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b))
  42. #endif
  43. #if defined(__SYMBIAN32__) && defined(__WINS__)
  44. void *_alloca(size_t size);
  45. # define alloca _alloca
  46. #endif
  47. #ifndef FAST_HYPOT
  48. # define FAST_HYPOT hypot
  49. #endif
  50. #endif /* _V_IFDEFJAIL_H_ */
  51. #ifdef HAVE_ALLOCA_H
  52. # include <alloca.h>
  53. #endif
  54. #ifdef USE_MEMORY_H
  55. # include <memory.h>
  56. #endif
  57. #ifndef min
  58. # define min(x,y) ((x)>(y)?(y):(x))
  59. #endif
  60. #ifndef max
  61. # define max(x,y) ((x)<(y)?(y):(x))
  62. #endif
  63. /* Special i386 GCC implementation */
  64. #if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__) && !defined(__SSE2_MATH__)
  65. # define VORBIS_FPU_CONTROL
  66. /* both GCC and MSVC are kinda stupid about rounding/casting to int.
  67. Because of encapsulation constraints (GCC can't see inside the asm
  68. block and so we end up doing stupid things like a store/load that
  69. is collectively a noop), we do it this way */
  70. /* we must set up the fpu before this works!! */
  71. typedef ogg_int16_t vorbis_fpu_control;
  72. static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
  73. ogg_int16_t ret;
  74. ogg_int16_t temp;
  75. __asm__ __volatile__("fnstcw %0\n\t"
  76. "movw %0,%%dx\n\t"
  77. "andw $62463,%%dx\n\t"
  78. "movw %%dx,%1\n\t"
  79. "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
  80. *fpu=ret;
  81. }
  82. static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
  83. __asm__ __volatile__("fldcw %0":: "m"(fpu));
  84. }
  85. /* assumes the FPU is in round mode! */
  86. static inline int vorbis_ftoi(double f){ /* yes, double! Otherwise,
  87. we get extra fst/fld to
  88. truncate precision */
  89. int i;
  90. __asm__("fistl %0": "=m"(i) : "t"(f));
  91. return(i);
  92. }
  93. #endif /* Special i386 GCC implementation */
  94. /* MSVC inline assembly. 32 bit only; inline ASM isn't implemented in the
  95. * 64 bit compiler and doesn't work on arm. */
  96. #if defined(_MSC_VER) && defined(_M_IX86) && !defined(_WIN32_WCE)
  97. # define VORBIS_FPU_CONTROL
  98. typedef ogg_int16_t vorbis_fpu_control;
  99. static __inline int vorbis_ftoi(double f){
  100. int i;
  101. __asm{
  102. fld f
  103. fistp i
  104. }
  105. return i;
  106. }
  107. static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
  108. (void)fpu;
  109. }
  110. static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
  111. (void)fpu;
  112. }
  113. #endif /* Special MSVC 32 bit implementation */
  114. /* Optimized code path for x86_64 builds. Uses SSE2 intrinsics. This can be
  115. done safely because all x86_64 CPUs supports SSE2. */
  116. #if (defined(_MSC_VER) && defined(_M_X64)) || (defined(__GNUC__) && defined (__SSE2_MATH__))
  117. # define VORBIS_FPU_CONTROL
  118. typedef ogg_int16_t vorbis_fpu_control;
  119. #include <emmintrin.h>
  120. static __inline int vorbis_ftoi(double f){
  121. return _mm_cvtsd_si32(_mm_load_sd(&f));
  122. }
  123. static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
  124. (void)fpu;
  125. }
  126. static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
  127. (void)fpu;
  128. }
  129. #endif /* Special MSVC x64 implementation */
  130. /* If no special implementation was found for the current compiler / platform,
  131. use the default implementation here: */
  132. #ifndef VORBIS_FPU_CONTROL
  133. typedef int vorbis_fpu_control;
  134. STIN int vorbis_ftoi(double f){
  135. /* Note: MSVC and GCC (at least on some systems) round towards zero, thus,
  136. the floor() call is required to ensure correct roudning of
  137. negative numbers */
  138. return (int)floor(f+.5);
  139. }
  140. /* We don't have special code for this compiler/arch, so do it the slow way */
  141. # define vorbis_fpu_setround(vorbis_fpu_control) {}
  142. # define vorbis_fpu_restore(vorbis_fpu_control) {}
  143. #endif /* default implementation */
  144. #endif /* _OS_H */