static inline define hack for GCC where it really helps the mdct
[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.18 2000/12/17 08:47:46 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.14159265358979323846)
29 #endif
30
31 #ifndef __GNUC__
32 #ifdef _WIN32
33 #  include <malloc.h>
34 #  define rint(x)   (floor((x)+0.5)) 
35 #endif
36
37 #define STIN static
38 #else
39 #define STIN static inline
40 #endif
41
42
43 #ifdef _WIN32
44 #  define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b))
45 #else /* if not _WIN32 */
46 #  define FAST_HYPOT hypot
47 #endif
48
49 #endif
50
51 #ifdef HAVE_ALLOCA_H
52 #include <alloca.h>
53 #endif
54
55 #ifdef USE_MEMORY_H
56 #include <memory.h>
57 #endif
58
59 #ifndef min
60 #  define min(x,y)  ((x)>(y)?(y):(x))
61 #endif
62
63 #ifndef max
64 #  define max(x,y)  ((x)<(y)?(y):(x))
65 #endif
66
67
68 #if defined(__i386__) && defined(__GNUC__)
69
70 #ifndef __BEOS__
71
72 /* both GCC and MSVC are kinda stupid about rounding/casting to int.
73    Because of encapsulation constraints (GCC can't see inside the asm
74    block and so we end up doing stupid things like a store/load that
75    is collectively a noop), we do it this way */
76
77 /* we must set up the fpu before this works!! */
78
79 typedef ogg_int16_t vorbis_fpu_control;
80
81 static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
82   ogg_int16_t ret;
83   ogg_int16_t temp;
84   __asm__ __volatile__("fnstcw %0\n\t"
85           "movw %0,%%dx\n\t"
86           "orw $62463,%%dx\n\t"
87           "movw %%dx,%1\n\t"
88           "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
89   *fpu=ret;
90 }
91
92 static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
93   __asm__ __volatile__("fldcw %0":: "m"(fpu));
94 }
95
96 /* assumes the FPU is in round mode! */
97 static inline int vorbis_ftoi(double f){  /* yes, double!  Otherwise,
98                                              we get extra fst/fld to
99                                              truncate precision */
100   int i;
101   __asm__("fistl %0": "=m"(i) : "t"(f));
102   return(i);
103 }
104
105 #else
106 /* this is for beos */
107
108 typedef int vorbis_fpu_control;
109 static int vorbis_ftoi(double f){
110   return (int)(f+.5);
111 }
112
113 /* We don't have special code for this compiler/arch, so do it the slow way */
114 #define vorbis_fpu_setround(vorbis_fpu_control) {}
115 #define vorbis_fpu_restore(vorbis_fpu_control) {}
116
117 #endif
118
119 #else
120
121
122 typedef int vorbis_fpu_control;
123
124 #ifdef _WIN32
125
126 static __inline int vorbis_ftoi(double f){
127         int i;
128         __asm{
129                 fld f
130                 fistp i
131         }
132         return i;
133 }
134
135 static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
136 }
137
138 static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
139 }
140 #else 
141
142 static int vorbis_ftoi(double f){
143   return (int)(f+.5);
144 }
145
146 /* We don't have special code for this compiler/arch, so do it the slow way */
147 #define vorbis_fpu_setround(vorbis_fpu_control) {}
148 #define vorbis_fpu_restore(vorbis_fpu_control) {}
149
150 #endif
151
152 #endif
153
154 #endif /* _OS_H */