final beta 4 commit
[platform/upstream/libvorbis.git] / lib / os.h
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
15  function: #ifdef jail to whip a few platforms into the UNIX ideal.
16  last mod: $Id: os.h,v 1.24 2001/02/26 03:50:42 xiphmont Exp $
17
18  ********************************************************************/
19
20 #include <math.h>
21 #include <ogg/os_types.h>
22
23 #ifndef _V_IFDEFJAIL_H_
24 #  define _V_IFDEFJAIL_H_
25
26 #  ifdef __GNUC__
27 #    define STIN static inline
28 #  elif _WIN32
29 #    define STIN static __inline
30 #else
31 #  define STIN static
32 #endif
33
34 #ifndef M_PI
35 #  define M_PI (3.1415926536f)
36 #endif
37
38 #ifdef _WIN32
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
45 #ifndef __GNUC__
46 #  define NO_FLOAT_MATH_LIB
47 #endif
48
49 #ifdef DARWIN
50 #  define NO_FLOAT_MATH_LIB
51 #endif
52
53 #ifndef NO_FLOAT_MATH_LIB
54 #  define sqrt sqrtf
55 #  define log logf
56 #  define exp expf
57 #  define pow powf
58 #  define acos acosf
59 #  define atan atanf
60 #  define frexp frexpf
61 #  define rint rintf
62 #endif
63
64
65 #ifndef FAST_HYPOT
66 #  define FAST_HYPOT hypot
67 #endif
68
69 #endif
70
71 #ifdef HAVE_ALLOCA_H
72 #  include <alloca.h>
73 #endif
74
75 #ifdef USE_MEMORY_H
76 #  include <memory.h>
77 #endif
78
79 #ifndef min
80 #  define min(x,y)  ((x)>(y)?(y):(x))
81 #endif
82
83 #ifndef max
84 #  define max(x,y)  ((x)<(y)?(y):(x))
85 #endif
86
87 #if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__)
88 #  define VORBIS_FPU_CONTROL
89 /* both GCC and MSVC are kinda stupid about rounding/casting to int.
90    Because of encapsulation constraints (GCC can't see inside the asm
91    block and so we end up doing stupid things like a store/load that
92    is collectively a noop), we do it this way */
93
94 /* we must set up the fpu before this works!! */
95
96 typedef ogg_int16_t vorbis_fpu_control;
97
98 static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
99   ogg_int16_t ret;
100   ogg_int16_t temp;
101   __asm__ __volatile__("fnstcw %0\n\t"
102           "movw %0,%%dx\n\t"
103           "orw $62463,%%dx\n\t"
104           "movw %%dx,%1\n\t"
105           "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
106   *fpu=ret;
107 }
108
109 static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
110   __asm__ __volatile__("fldcw %0":: "m"(fpu));
111 }
112
113 /* assumes the FPU is in round mode! */
114 static inline int vorbis_ftoi(double f){  /* yes, double!  Otherwise,
115                                              we get extra fst/fld to
116                                              truncate precision */
117   int i;
118   __asm__("fistl %0": "=m"(i) : "t"(f));
119   return(i);
120 }
121 #endif
122
123
124 #if defined(_WIN32) && !defined(__GNUC__)
125 #  define VORBIS_FPU_CONTROL
126
127 typedef ogg_int16_t vorbis_fpu_control;
128
129 static __inline int vorbis_ftoi(double f){
130         int i;
131         __asm{
132                 fld f
133                 fistp i
134         }
135         return i;
136 }
137
138 static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
139 }
140
141 static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
142 }
143
144 #endif
145
146
147 #ifndef VORBIS_FPU_CONTROL
148
149 typedef int vorbis_fpu_control;
150
151 static int vorbis_ftoi(double f){
152   return (int)(f+.5);
153 }
154
155 /* We don't have special code for this compiler/arch, so do it the slow way */
156 #  define vorbis_fpu_setround(vorbis_fpu_control) {}
157 #  define vorbis_fpu_restore(vorbis_fpu_control) {}
158
159 #endif
160
161 #endif /* _OS_H */