decoder_example non-stereo decode fix
[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.25 2001/05/27 20:33:19 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 #ifdef HAVE_SQRTF
54 #  define sqrt sqrtf
55 #endif
56 #ifdef HAVE_LOGF
57 #  define log logf
58 #endif
59 #ifdef HAVE_EXPF
60 #  define exp expf
61 #endif
62 #ifdef HAVE_POWF
63 #  define pow powf
64 #endif
65 #ifdef HAVE_ACOSF
66 #  define acos acosf
67 #endif
68 #ifdef HAVE_ATANF
69 #  define atan atanf
70 #endif
71 #ifdef HAVE_FREXPF
72 #  define frexp frexpf
73 #endif
74 #ifdef HAVE_RINTF
75 #  define rint rintf
76 #endif
77
78 #ifndef FAST_HYPOT
79 #  define FAST_HYPOT hypot
80 #endif
81
82 #endif
83
84 #ifdef HAVE_ALLOCA_H
85 #  include <alloca.h>
86 #endif
87
88 #ifdef USE_MEMORY_H
89 #  include <memory.h>
90 #endif
91
92 #ifndef min
93 #  define min(x,y)  ((x)>(y)?(y):(x))
94 #endif
95
96 #ifndef max
97 #  define max(x,y)  ((x)<(y)?(y):(x))
98 #endif
99
100 #if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__)
101 #  define VORBIS_FPU_CONTROL
102 /* both GCC and MSVC are kinda stupid about rounding/casting to int.
103    Because of encapsulation constraints (GCC can't see inside the asm
104    block and so we end up doing stupid things like a store/load that
105    is collectively a noop), we do it this way */
106
107 /* we must set up the fpu before this works!! */
108
109 typedef ogg_int16_t vorbis_fpu_control;
110
111 static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
112   ogg_int16_t ret;
113   ogg_int16_t temp;
114   __asm__ __volatile__("fnstcw %0\n\t"
115           "movw %0,%%dx\n\t"
116           "orw $62463,%%dx\n\t"
117           "movw %%dx,%1\n\t"
118           "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
119   *fpu=ret;
120 }
121
122 static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
123   __asm__ __volatile__("fldcw %0":: "m"(fpu));
124 }
125
126 /* assumes the FPU is in round mode! */
127 static inline int vorbis_ftoi(double f){  /* yes, double!  Otherwise,
128                                              we get extra fst/fld to
129                                              truncate precision */
130   int i;
131   __asm__("fistl %0": "=m"(i) : "t"(f));
132   return(i);
133 }
134 #endif
135
136
137 #if defined(_WIN32) && !defined(__GNUC__)
138 #  define VORBIS_FPU_CONTROL
139
140 typedef ogg_int16_t vorbis_fpu_control;
141
142 static __inline int vorbis_ftoi(double f){
143         int i;
144         __asm{
145                 fld f
146                 fistp i
147         }
148         return i;
149 }
150
151 static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
152 }
153
154 static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
155 }
156
157 #endif
158
159
160 #ifndef VORBIS_FPU_CONTROL
161
162 typedef int vorbis_fpu_control;
163
164 static int vorbis_ftoi(double f){
165   return (int)(f+.5);
166 }
167
168 /* We don't have special code for this compiler/arch, so do it the slow way */
169 #  define vorbis_fpu_setround(vorbis_fpu_control) {}
170 #  define vorbis_fpu_restore(vorbis_fpu_control) {}
171
172 #endif
173
174 #endif /* _OS_H */