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