os.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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-2007 *
  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$
  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. # include <malloc.h>
  40. # define rint(x) (floor((x)+0.5f))
  41. # define NO_FLOAT_MATH_LIB
  42. # define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b))
  43. #endif
  44. #if defined(__SYMBIAN32__) && defined(__WINS__)
  45. void *_alloca(size_t size);
  46. # define alloca _alloca
  47. #endif
  48. #ifndef FAST_HYPOT
  49. # define FAST_HYPOT hypot
  50. #endif
  51. #endif
  52. #ifdef HAVE_ALLOCA_H
  53. # include <alloca.h>
  54. #endif
  55. #ifdef USE_MEMORY_H
  56. # include <memory.h>
  57. #endif
  58. #ifndef min
  59. # define min(x,y) ((x)>(y)?(y):(x))
  60. #endif
  61. #ifndef max
  62. # define max(x,y) ((x)<(y)?(y):(x))
  63. #endif
  64. #if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__)
  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. "orw $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
  94. #if defined(_WIN32) && !defined(__GNUC__) && !defined(__BORLANDC__)
  95. # define VORBIS_FPU_CONTROL
  96. typedef ogg_int16_t vorbis_fpu_control;
  97. static __inline int vorbis_ftoi(double f){
  98. int i;
  99. __asm{
  100. fld f
  101. fistp i
  102. }
  103. return i;
  104. }
  105. static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
  106. }
  107. static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
  108. }
  109. #endif
  110. #ifndef VORBIS_FPU_CONTROL
  111. typedef int vorbis_fpu_control;
  112. static int vorbis_ftoi(double f){
  113. return (int)(f+.5);
  114. }
  115. /* We don't have special code for this compiler/arch, so do it the slow way */
  116. # define vorbis_fpu_setround(vorbis_fpu_control) {}
  117. # define vorbis_fpu_restore(vorbis_fpu_control) {}
  118. #endif
  119. #endif /* _OS_H */