1 // most of the code below was taken from libvorbis/vorbis/lib/os.h
2 // under the conditions below
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-2009             *
11  * by the Xiph.Org Foundation http://www.xiph.org/                  *
12  *                                                                  *
13  ********************************************************************
14 
15 contents of the libvorbis COPYING:
16 
17 Copyright (c) 2002-2008 Xiph.org Foundation
18 
19 Redistribution and use in source and binary forms, with or without
20 modification, are permitted provided that the following conditions
21 are met:
22 
23 - Redistributions of source code must retain the above copyright
24 notice, this list of conditions and the following disclaimer.
25 
26 - Redistributions in binary form must reproduce the above copyright
27 notice, this list of conditions and the following disclaimer in the
28 documentation and/or other materials provided with the distribution.
29 
30 - Neither the name of the Xiph.org Foundation nor the names of its
31 contributors may be used to endorse or promote products derived from
32 this software without specific prior written permission.
33 
34 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
35 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
36 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
37 A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION
38 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
42 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
44 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  */
46 #ifndef __OPTMATH_H
47 #define __OPTMATH_H
48 
49 #include <math.h>
50 
51 #ifdef __SSE2__ // that comes from -msse2
52 #define __FORCE_SSE2__
53 #endif
54 
55 /* Special i386 GCC implementation */
56 #if defined(__i386__) && defined(__GNUC__) && !defined(__BEOS__) && !defined(__FORCE_SSE2__)
57 #  define FPU_CONTROL
58 /* both GCC and MSVC are kinda stupid about rounding/casting to int.
59    Because of encapsulation constraints (GCC can't see inside the asm
60    block and so we end up doing stupid things like a store/load that
61    is collectively a noop), we do it this way */
62 
63 /* we must set up the fpu before this works!! */
64 
65 typedef int16_t fpu_control;
66 
fpu_setround(fpu_control * fpu)67 static inline void fpu_setround(fpu_control *fpu){
68   int16_t ret;
69   int16_t temp;
70   __asm__ __volatile__("fnstcw %0\n\t"
71 	  "movw %0,%%dx\n\t"
72 	  "orw $62463,%%dx\n\t"
73 	  "movw %%dx,%1\n\t"
74 	  "fldcw %1\n\t":"=m"(ret):"m"(temp): "dx");
75   *fpu=ret;
76 }
77 
fpu_restore(fpu_control fpu)78 static inline void fpu_restore(fpu_control fpu){
79   __asm__ __volatile__("fldcw %0":: "m"(fpu));
80 }
81 
82 /* assumes the FPU is in round mode! */
ftoi(double f)83 static inline int ftoi(double f){  /* yes, double!  Otherwise,
84                                              we get extra fst/fld to
85                                              truncate precision */
86   int i;
87   __asm__("fistl %0": "=m"(i) : "t"(f));
88   return(i);
89 }
90 #endif /* Special i386 GCC implementation */
91 
92 
93 /* MSVC inline assembly. 32 bit only; inline ASM isn't implemented in the
94  * 64 bit compiler */
95 #if defined(_MSC_VER) && !defined(_WIN64)
96 #  define FPU_CONTROL
97 
98 typedef int16_t fpu_control;
99 
ftoi(double f)100 static __inline int ftoi(double f){
101 	int i;
102 	__asm{
103 		fld f
104 		fistp i
105 	}
106 	return i;
107 }
108 
fpu_setround(fpu_control * fpu)109 static __inline void fpu_setround(fpu_control *fpu){
110 }
111 
fpu_restore(fpu_control fpu)112 static __inline void fpu_restore(fpu_control fpu){
113 }
114 
115 #endif /* Special MSVC 32 bit implementation */
116 
117 
118 /* Optimized code path for x86_64 builds. Uses SSE2 intrinsics. This can be
119    done safely because all x86_64 CPUs supports SSE2. */
120 #if (defined(__FORCE_SSE2__)) || (defined(_MSC_VER) && defined(_WIN64)) || (defined(__GNUC__) && defined (__x86_64__))
121 #pragma warning "using sse2 for ftoi"
122 #  define FPU_CONTROL
123 
124 typedef int16_t fpu_control;
125 
126 #include <emmintrin.h>
ftoi(double f)127 static __inline int ftoi(double f){
128         return _mm_cvtsd_si32(_mm_load_sd(&f));
129 }
130 
fpu_setround(fpu_control * fpu)131 static __inline void fpu_setround(fpu_control *fpu){
132 }
133 
fpu_restore(fpu_control fpu)134 static __inline void fpu_restore(fpu_control fpu){
135 }
136 
137 #endif /* Special MSVC x64 implementation */
138 
139 
140 /* If no special implementation was found for the current compiler / platform,
141    use the default implementation here: */
142 #ifndef FPU_CONTROL
143 
144 typedef int fpu_control;
145 
ftoi(double f)146 static int ftoi(double f){
147         /* Note: MSVC and GCC (at least on some systems) round towards zero, thus,
148            the floor() call is required to ensure correct roudning of
149            negative numbers */
150         return (int)floor(f+.5);
151 }
152 
153 /* We don't have special code for this compiler/arch, so do it the slow way */
154 #  define fpu_setround(fpu_control) {}
155 #  define fpu_restore(fpu_control) {}
156 
157 #endif /* default implementation */
158 
159 #endif // __OPTMATH_H
160