os.h 5.1 KB

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