3 /********************************************************************
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. *
10 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2009 *
11 * by the Xiph.Org Foundation http://www.xiph.org/ *
13 ********************************************************************
15 function: #ifdef jail to whip a few platforms into the UNIX ideal.
16 last mod: $Id: os.h 16227 2009-07-08 06:58:46Z xiphmont $
18 ********************************************************************/
25 #include <ext/libogg/os_types.h>
29 #ifndef _V_IFDEFJAIL_H_
30 # define _V_IFDEFJAIL_H_
33 # define STIN static __inline__
35 # define STIN static __inline
40 #if defined(DJGPP) || defined(__WATCOMC__)
41 # define rint(x) (floor((x)+0.5f))
45 # define M_PI (3.1415926536f)
48 #if defined(_WIN32) && !defined(__SYMBIAN32__)
50 # define rint(x) (floor((x)+0.5f))
51 # define NO_FLOAT_MATH_LIB
52 # define FAST_HYPOT(a, b) sqrt((a)*(a) + (b)*(b))
55 #if defined(__SYMBIAN32__) && defined(__WINS__)
56 void *_alloca(size_t size);
57 # define alloca _alloca
61 # define FAST_HYPOT hypot
75 # define min(x,y) ((x)>(y)?(y):(x))
79 # define max(x,y) ((x)<(y)?(y):(x))
83 /* Special i386 GCC implementation */
84 #if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__)
85 # define VORBIS_FPU_CONTROL
86 /* both GCC and MSVC are kinda stupid about rounding/casting to int.
87 Because of encapsulation constraints (GCC can't see inside the asm
88 block and so we end up doing stupid things like a store/load that
89 is collectively a noop), we do it this way */
91 /* we must set up the fpu before this works!! */
93 typedef ogg_int16_t vorbis_fpu_control;
95 static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
98 __asm__ __volatile__("fnstcw %0\n\t"
100 "andw $62463,%%dx\n\t"
102 "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
106 static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
107 __asm__ __volatile__("fldcw %0":: "m"(fpu));
110 /* assumes the FPU is in round mode! */
111 static inline int vorbis_ftoi(double f){ /* yes, double! Otherwise,
112 we get extra fst/fld to
113 truncate precision */
115 __asm__("fistl %0": "=m"(i) : "t"(f));
118 #endif /* Special i386 GCC implementation */
121 /* MSVC inline assembly. 32 bit only; inline ASM isn't implemented in the
123 #if defined(_MSC_VER) && !defined(_WIN64) && !defined(_WIN32_WCE)
124 # define VORBIS_FPU_CONTROL
126 typedef ogg_int16_t vorbis_fpu_control;
128 static __inline int vorbis_ftoi(double f){
137 static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
140 static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
143 #endif /* Special MSVC 32 bit implementation */
146 /* Optimized code path for x86_64 builds. Uses SSE2 intrinsics. This can be
147 done safely because all x86_64 CPUs supports SSE2. */
148 #if (defined(_MSC_VER) && defined(_WIN64)) || (defined(__GNUC__) && defined (__x86_64__))
149 # define VORBIS_FPU_CONTROL
151 typedef ogg_int16_t vorbis_fpu_control;
153 #include <emmintrin.h>
154 static __inline int vorbis_ftoi(double f){
155 return _mm_cvtsd_si32(_mm_load_sd(&f));
158 static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
161 static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
164 #endif /* Special MSVC x64 implementation */
167 /* If no special implementation was found for the current compiler / platform,
168 use the default implementation here: */
169 #ifndef VORBIS_FPU_CONTROL
171 typedef int vorbis_fpu_control;
173 static int vorbis_ftoi(double f){
174 /* Note: MSVC and GCC (at least on some systems) round towards zero, thus,
175 the floor() call is required to ensure correct roudning of
177 return (int)floor(f+.5);
180 /* We don't have special code for this compiler/arch, so do it the slow way */
181 # define vorbis_fpu_setround(vorbis_fpu_control) {}
182 # define vorbis_fpu_restore(vorbis_fpu_control) {}
184 #endif /* default implementation */