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