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-2015 *
11 * by the Xiph.Org Foundation https://xiph.org/ *
13 ********************************************************************
15 function: #ifdef jail to whip a few platforms into the UNIX ideal.
17 ********************************************************************/
24 #include <ogg/os_types.h>
28 #ifndef _V_IFDEFJAIL_H_
29 # define _V_IFDEFJAIL_H_
32 # define STIN static __inline__
33 # elif defined(_WIN32)
34 # define STIN static __inline
40 # define rint(x) (floor((x)+0.5f))
44 # define M_PI (3.1415926536f)
47 #if defined(_WIN32) && !defined(__SYMBIAN32__)
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))
54 #if defined(__SYMBIAN32__) && defined(__WINS__)
55 void *_alloca(size_t size);
56 # define alloca _alloca
60 # define FAST_HYPOT hypot
63 #endif /* _V_IFDEFJAIL_H_ */
74 # define min(x,y) ((x)>(y)?(y):(x))
78 # define max(x,y) ((x)<(y)?(y):(x))
82 /* Special i386 GCC implementation */
83 #if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__) && !defined(__SSE2_MATH__)
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 */
90 /* we must set up the fpu before this works!! */
92 typedef ogg_int16_t vorbis_fpu_control;
94 static inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
97 __asm__ __volatile__("fnstcw %0\n\t"
99 "andw $62463,%%dx\n\t"
101 "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
105 static inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
106 __asm__ __volatile__("fldcw %0":: "m"(fpu));
109 /* assumes the FPU is in round mode! */
110 static inline int vorbis_ftoi(double f){ /* yes, double! Otherwise,
111 we get extra fst/fld to
112 truncate precision */
114 __asm__("fistl %0": "=m"(i) : "t"(f));
117 #endif /* Special i386 GCC implementation */
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(_M_IX86) && !defined(_WIN32_WCE)
123 # define VORBIS_FPU_CONTROL
125 typedef ogg_int16_t vorbis_fpu_control;
127 static __inline int vorbis_ftoi(double f){
136 static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
140 static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
144 #endif /* Special MSVC 32 bit implementation */
147 /* Optimized code path for x86_64 builds. Uses SSE2 intrinsics. This can be
148 done safely because all x86_64 CPUs supports SSE2. */
149 #if (defined(_MSC_VER) && defined(_M_X64)) || (defined(__GNUC__) && defined (__SSE2_MATH__))
150 # define VORBIS_FPU_CONTROL
152 typedef ogg_int16_t vorbis_fpu_control;
154 #include <emmintrin.h>
155 static __inline int vorbis_ftoi(double f){
156 return _mm_cvtsd_si32(_mm_load_sd(&f));
159 static __inline void vorbis_fpu_setround(vorbis_fpu_control *fpu){
163 static __inline void vorbis_fpu_restore(vorbis_fpu_control fpu){
167 #endif /* Special MSVC x64 implementation */
170 /* If no special implementation was found for the current compiler / platform,
171 use the default implementation here: */
172 #ifndef VORBIS_FPU_CONTROL
174 typedef int vorbis_fpu_control;
176 STIN int vorbis_ftoi(double f){
177 /* Note: MSVC and GCC (at least on some systems) round towards zero, thus,
178 the floor() call is required to ensure correct roudning of
180 return (int)floor(f+.5);
183 /* We don't have special code for this compiler/arch, so do it the slow way */
184 # define vorbis_fpu_setround(vorbis_fpu_control) {}
185 # define vorbis_fpu_restore(vorbis_fpu_control) {}
187 #endif /* default implementation */