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