Update header copyright dates, update copyright assignemnt
[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 SOURCE IS GOVERNED BY *
7  * THE GNU LESSER/LIBRARY PUBLIC LICENSE, WHICH IS INCLUDED WITH    *
8  * THIS SOURCE. 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.22 2001/02/02 03:51:57 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 #ifdef DARWIN
46 #  define NO_FLOAT_MATH_LIB
47 #endif
48
49 #ifndef NO_FLOAT_MATH_LIB
50 #  define sqrt sqrtf
51 #  define log logf
52 #  define exp expf
53 #  define pow powf
54 #  define acos acosf
55 #  define atan atanf
56 #  define frexp frexpf
57 #  define rint rintf
58 #endif
59
60
61 #ifndef FAST_HYPOT
62 #  define FAST_HYPOT hypot
63 #endif
64
65 #endif
66
67 #ifdef HAVE_ALLOCA_H
68 #  include <alloca.h>
69 #endif
70
71 #ifdef USE_MEMORY_H
72 #  include <memory.h>
73 #endif
74
75 #ifndef min
76 #  define min(x,y)  ((x)>(y)?(y):(x))
77 #endif
78
79 #ifndef max
80 #  define max(x,y)  ((x)<(y)?(y):(x))
81 #endif
82
83 #if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__)
84 #  define VORBIS_FPU_CONTROL
85 /* both GCC and MSVC are kinda stupid about rounding/casting to int.
86    Because of encapsulation constraints (GCC can't see inside the asm
87    block and so we end up doing stupid things like a store/load that
88    is collectively a noop), we do it this way */
89
90 /* we must set up the fpu before this works!! */
91
92 typedef ogg_int16_t vorbis_fpu_control;
93
94 static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
95   ogg_int16_t ret;
96   ogg_int16_t temp;
97   __asm__ __volatile__("fnstcw %0\n\t"
98           "movw %0,%%dx\n\t"
99           "orw $62463,%%dx\n\t"
100           "movw %%dx,%1\n\t"
101           "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
102   *fpu=ret;
103 }
104
105 static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
106   __asm__ __volatile__("fldcw %0":: "m"(fpu));
107 }
108
109 /* assumes the FPU is in round mode! */
110 static inline int vorbis_ftoi(double f){  /* yes, double!  Otherwise,
111                                              we get extra fst/fld to
112                                              truncate precision */
113   int i;
114   __asm__("fistl %0": "=m"(i) : "t"(f));
115   return(i);
116 }
117 #endif
118
119
120 #if defined(_WIN32) && !defined(__GNUC__)
121 #  define VORBIS_FPU_CONTROL
122
123 typedef ogg_int16_t vorbis_fpu_control;
124
125 static __inline int vorbis_ftoi(double f){
126         int i;
127         __asm{
128                 fld f
129                 fistp i
130         }
131         return i;
132 }
133
134 static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
135 }
136
137 static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
138 }
139
140 #endif
141
142
143 #ifndef VORBIS_FPU_CONTROL
144
145 typedef int vorbis_fpu_control;
146
147 static int vorbis_ftoi(double f){
148   return (int)(f+.5);
149 }
150
151 /* We don't have special code for this compiler/arch, so do it the slow way */
152 #  define vorbis_fpu_setround(vorbis_fpu_control) {}
153 #  define vorbis_fpu_restore(vorbis_fpu_control) {}
154
155 #endif
156
157 #endif /* _OS_H */