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-2015             *
11  * by the Xiph.Org Foundation http://www.xiph.org/                  *
12  *                                                                  *
13  ********************************************************************
14 
15  function: #ifdef jail to whip a few platforms into the UNIX ideal.
16 
17  ********************************************************************/
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include <math.h>
24 #include <ogg/os_types.h>
25 
26 #include "misc.h"
27 
28 #ifndef _V_IFDEFJAIL_H_
29 #  define _V_IFDEFJAIL_H_
30 
31 #  ifdef __GNUC__
32 #    define STIN static __inline__
33 #  elif defined(_WIN32)
34 #    define STIN static __inline
35 #  else
36 #    define STIN static
37 #  endif
38 
39 #ifdef DJGPP
40 #  define rint(x)   (floor((x)+0.5f))
41 #endif
42 
43 #ifndef M_PI
44 #  define M_PI (3.1415926536f)
45 #endif
46 
47 #if defined(_WIN32) && !defined(__SYMBIAN32__)
48 #  include <malloc.h>
49 #  define rint(x)   (floor((x)+0.5f))
50 #  define NO_FLOAT_MATH_LIB
51 #  define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b))
52 #endif
53 
54 #if defined(__SYMBIAN32__) && defined(__WINS__)
55 void *_alloca(size_t size);
56 #  define alloca _alloca
57 #endif
58 
59 #ifndef FAST_HYPOT
60 #  define FAST_HYPOT hypot
61 #endif
62 
63 #endif
64 
65 #ifdef HAVE_ALLOCA_H
66 #  include <alloca.h>
67 #endif
68 
69 #ifdef USE_MEMORY_H
70 #  include <memory.h>
71 #endif
72 
73 #ifndef min
74 #  define min(x,y)  ((x)>(y)?(y):(x))
75 #endif
76 
77 #ifndef max
78 #  define max(x,y)  ((x)<(y)?(y):(x))
79 #endif
80 
81 
82 /* Special i386 GCC implementation */
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 
vorbis_fpu_setround(vorbis_fpu_control * fpu)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           "andw $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 
vorbis_fpu_restore(vorbis_fpu_control fpu)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! */
vorbis_ftoi(double f)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 /* Special i386 GCC implementation */
118 
119 
120 /* MSVC inline assembly. 32 bit only; inline ASM isn't implemented in the
121  * 64 bit compiler and doesn't work on arm. */
122 #if defined(_MSC_VER) && !defined(_WIN64) && \
123       !defined(_WIN32_WCE) && !defined(_M_ARM)
124 #  define VORBIS_FPU_CONTROL
125 
126 typedef ogg_int16_t vorbis_fpu_control;
127 
vorbis_ftoi(double f)128 static __inline int vorbis_ftoi(double f){
129         int i;
130         __asm{
131                 fld f
132                 fistp i
133         }
134         return i;
135 }
136 
vorbis_fpu_setround(vorbis_fpu_control * fpu)137 static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
138   (void)fpu;
139 }
140 
vorbis_fpu_restore(vorbis_fpu_control fpu)141 static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
142   (void)fpu;
143 }
144 
145 #endif /* Special MSVC 32 bit implementation */
146 
147 
148 /* Optimized code path for x86_64 builds. Uses SSE2 intrinsics. This can be
149    done safely because all x86_64 CPUs supports SSE2. */
150 #if (defined(_MSC_VER) && defined(_WIN64)) || (defined(__GNUC__) && defined (__x86_64__))
151 #  define VORBIS_FPU_CONTROL
152 
153 typedef ogg_int16_t vorbis_fpu_control;
154 
155 #include <emmintrin.h>
vorbis_ftoi(double f)156 static __inline int vorbis_ftoi(double f){
157         return _mm_cvtsd_si32(_mm_load_sd(&f));
158 }
159 
vorbis_fpu_setround(vorbis_fpu_control * fpu)160 static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
161   (void)fpu;
162 }
163 
vorbis_fpu_restore(vorbis_fpu_control fpu)164 static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
165   (void)fpu;
166 }
167 
168 #endif /* Special MSVC x64 implementation */
169 
170 
171 /* If no special implementation was found for the current compiler / platform,
172    use the default implementation here: */
173 #ifndef VORBIS_FPU_CONTROL
174 
175 typedef int vorbis_fpu_control;
176 
vorbis_ftoi(double f)177 static int vorbis_ftoi(double f){
178         /* Note: MSVC and GCC (at least on some systems) round towards zero, thus,
179            the floor() call is required to ensure correct roudning of
180            negative numbers */
181         return (int)floor(f+.5);
182 }
183 
184 /* We don't have special code for this compiler/arch, so do it the slow way */
185 #  define vorbis_fpu_setround(vorbis_fpu_control) {}
186 #  define vorbis_fpu_restore(vorbis_fpu_control) {}
187 
188 #endif /* default implementation */
189 
190 #endif /* _OS_H */
191