/media/libvorbis/lib/os.h

http://github.com/zpao/v8monkey · C Header · 190 lines · 111 code · 44 blank · 35 comment · 9 complexity · 930c98bc30f1b2507f98c22cf923f153 MD5 · raw file

  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-2009 *
  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 16227 2009-07-08 06:58:46Z xiphmont $
  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. #ifdef SOLARIS
  24. #define HAVE_ALLOCA_H
  25. #endif
  26. #ifndef _V_IFDEFJAIL_H_
  27. # define _V_IFDEFJAIL_H_
  28. # ifdef __GNUC__
  29. # define STIN static __inline__
  30. # elif _WIN32
  31. # define STIN static __inline
  32. # else
  33. # define STIN static
  34. # endif
  35. #ifdef DJGPP
  36. # define rint(x) (floor((x)+0.5f))
  37. #endif
  38. #ifndef M_PI
  39. # define M_PI (3.1415926536f)
  40. #endif
  41. #if defined(_WIN32) && !defined(__SYMBIAN32__)
  42. # include <malloc.h>
  43. # define rint(x) (floor((x)+0.5f))
  44. # define NO_FLOAT_MATH_LIB
  45. # define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b))
  46. #endif
  47. #if defined(__SYMBIAN32__) && defined(__WINS__)
  48. void *_alloca(size_t size);
  49. # define alloca _alloca
  50. #endif
  51. #ifndef FAST_HYPOT
  52. # define FAST_HYPOT hypot
  53. #endif
  54. #endif
  55. #ifdef HAVE_ALLOCA_H
  56. # include <alloca.h>
  57. #endif
  58. #ifdef USE_MEMORY_H
  59. # include <memory.h>
  60. #endif
  61. #ifndef min
  62. # define min(x,y) ((x)>(y)?(y):(x))
  63. #endif
  64. #ifndef max
  65. # define max(x,y) ((x)<(y)?(y):(x))
  66. #endif
  67. /* Special i386 GCC implementation */
  68. #if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__)
  69. # define VORBIS_FPU_CONTROL
  70. /* both GCC and MSVC are kinda stupid about rounding/casting to int.
  71. Because of encapsulation constraints (GCC can't see inside the asm
  72. block and so we end up doing stupid things like a store/load that
  73. is collectively a noop), we do it this way */
  74. /* we must set up the fpu before this works!! */
  75. typedef ogg_int16_t vorbis_fpu_control;
  76. static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
  77. ogg_int16_t ret;
  78. ogg_int16_t temp;
  79. __asm__ __volatile__("fnstcw %0\n\t"
  80. "movw %0,%%dx\n\t"
  81. "andw $62463,%%dx\n\t"
  82. "movw %%dx,%1\n\t"
  83. "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
  84. *fpu=ret;
  85. }
  86. static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
  87. __asm__ __volatile__("fldcw %0":: "m"(fpu));
  88. }
  89. /* assumes the FPU is in round mode! */
  90. static inline int vorbis_ftoi(double f){ /* yes, double! Otherwise,
  91. we get extra fst/fld to
  92. truncate precision */
  93. int i;
  94. __asm__("fistl %0": "=m"(i) : "t"(f));
  95. return(i);
  96. }
  97. #endif /* Special i386 GCC implementation */
  98. /* MSVC inline assembly. 32 bit only; inline ASM isn't implemented in the
  99. * 64 bit compiler */
  100. #if defined(_MSC_VER) && !defined(_WIN64) && !defined(_WIN32_WCE)
  101. # define VORBIS_FPU_CONTROL
  102. typedef ogg_int16_t vorbis_fpu_control;
  103. static __inline int vorbis_ftoi(double f){
  104. int i;
  105. __asm{
  106. fld f
  107. fistp i
  108. }
  109. return i;
  110. }
  111. static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
  112. }
  113. static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
  114. }
  115. #endif /* Special MSVC 32 bit implementation */
  116. /* Optimized code path for x86_64 builds. Uses SSE2 intrinsics. This can be
  117. done safely because all x86_64 CPUs supports SSE2. */
  118. #if (defined(_MSC_VER) && defined(_WIN64)) || (defined(__GNUC__) && defined (__x86_64__))
  119. # define VORBIS_FPU_CONTROL
  120. typedef ogg_int16_t vorbis_fpu_control;
  121. #include <emmintrin.h>
  122. static __inline int vorbis_ftoi(double f){
  123. return _mm_cvtsd_si32(_mm_load_sd(&f));
  124. }
  125. static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
  126. }
  127. static __inline void vorbis_fpu_restore(vorbis_fpu_control 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. static 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 */