os.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.31 2002/07/01 06:43:33 msmith Exp $
  16. ********************************************************************/
  17. #include <math.h>
  18. #include <ogg/os_types.h>
  19. #include "misc.h"
  20. #ifndef _V_IFDEFJAIL_H_
  21. # define _V_IFDEFJAIL_H_
  22. # ifdef __GNUC__
  23. # define STIN static __inline__
  24. # elif _WIN32
  25. # define STIN static __inline
  26. #else
  27. # define STIN static
  28. #endif
  29. #ifndef M_PI
  30. # define M_PI (3.1415926536f)
  31. #endif
  32. #ifdef _WIN32
  33. # include <malloc.h>
  34. # define rint(x) (floor((x)+0.5f))
  35. # define NO_FLOAT_MATH_LIB
  36. # define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b))
  37. #endif
  38. #ifndef FAST_HYPOT
  39. # define FAST_HYPOT hypot
  40. #endif
  41. #endif
  42. #ifdef HAVE_ALLOCA_H
  43. # include <alloca.h>
  44. #endif
  45. #ifdef USE_MEMORY_H
  46. # include <memory.h>
  47. #endif
  48. #ifndef min
  49. # define min(x,y) ((x)>(y)?(y):(x))
  50. #endif
  51. #ifndef max
  52. # define max(x,y) ((x)<(y)?(y):(x))
  53. #endif
  54. #if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__)
  55. # define VORBIS_FPU_CONTROL
  56. /* both GCC and MSVC are kinda stupid about rounding/casting to int.
  57. Because of encapsulation constraints (GCC can't see inside the asm
  58. block and so we end up doing stupid things like a store/load that
  59. is collectively a noop), we do it this way */
  60. /* we must set up the fpu before this works!! */
  61. typedef ogg_int16_t vorbis_fpu_control;
  62. static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
  63. ogg_int16_t ret;
  64. ogg_int16_t temp;
  65. __asm__ __volatile__("fnstcw %0\n\t"
  66. "movw %0,%%dx\n\t"
  67. "orw $62463,%%dx\n\t"
  68. "movw %%dx,%1\n\t"
  69. "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
  70. *fpu=ret;
  71. }
  72. static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
  73. __asm__ __volatile__("fldcw %0":: "m"(fpu));
  74. }
  75. /* assumes the FPU is in round mode! */
  76. static inline int vorbis_ftoi(double f){ /* yes, double! Otherwise,
  77. we get extra fst/fld to
  78. truncate precision */
  79. int i;
  80. __asm__("fistl %0": "=m"(i) : "t"(f));
  81. return(i);
  82. }
  83. #endif
  84. #if defined(_WIN32) && !defined(__GNUC__) && !defined(__BORLANDC__)
  85. # define VORBIS_FPU_CONTROL
  86. typedef ogg_int16_t vorbis_fpu_control;
  87. static __inline int vorbis_ftoi(double f){
  88. int i;
  89. __asm{
  90. fld f
  91. fistp i
  92. }
  93. return i;
  94. }
  95. static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
  96. }
  97. static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
  98. }
  99. #endif
  100. #ifndef VORBIS_FPU_CONTROL
  101. typedef int vorbis_fpu_control;
  102. static int vorbis_ftoi(double f){
  103. return (int)(f+.5);
  104. }
  105. /* We don't have special code for this compiler/arch, so do it the slow way */
  106. # define vorbis_fpu_setround(vorbis_fpu_control) {}
  107. # define vorbis_fpu_restore(vorbis_fpu_control) {}
  108. #endif
  109. #endif /* _OS_H */