cannam@154
|
1 /* Copyright (C) 2001 Erik de Castro Lopo <erikd AT mega-nerd DOT com> */
|
cannam@154
|
2 /*
|
cannam@154
|
3 Redistribution and use in source and binary forms, with or without
|
cannam@154
|
4 modification, are permitted provided that the following conditions
|
cannam@154
|
5 are met:
|
cannam@154
|
6
|
cannam@154
|
7 - Redistributions of source code must retain the above copyright
|
cannam@154
|
8 notice, this list of conditions and the following disclaimer.
|
cannam@154
|
9
|
cannam@154
|
10 - Redistributions in binary form must reproduce the above copyright
|
cannam@154
|
11 notice, this list of conditions and the following disclaimer in the
|
cannam@154
|
12 documentation and/or other materials provided with the distribution.
|
cannam@154
|
13
|
cannam@154
|
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
cannam@154
|
15 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
cannam@154
|
16 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
cannam@154
|
17 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
cannam@154
|
18 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
cannam@154
|
19 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
cannam@154
|
20 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
cannam@154
|
21 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
cannam@154
|
22 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
cannam@154
|
23 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
cannam@154
|
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
cannam@154
|
25 */
|
cannam@154
|
26
|
cannam@154
|
27 /* Version 1.1 */
|
cannam@154
|
28
|
cannam@154
|
29 #ifndef FLOAT_CAST_H
|
cannam@154
|
30 #define FLOAT_CAST_H
|
cannam@154
|
31
|
cannam@154
|
32
|
cannam@154
|
33 #include "arch.h"
|
cannam@154
|
34
|
cannam@154
|
35 /*============================================================================
|
cannam@154
|
36 ** On Intel Pentium processors (especially PIII and probably P4), converting
|
cannam@154
|
37 ** from float to int is very slow. To meet the C specs, the code produced by
|
cannam@154
|
38 ** most C compilers targeting Pentium needs to change the FPU rounding mode
|
cannam@154
|
39 ** before the float to int conversion is performed.
|
cannam@154
|
40 **
|
cannam@154
|
41 ** Changing the FPU rounding mode causes the FPU pipeline to be flushed. It
|
cannam@154
|
42 ** is this flushing of the pipeline which is so slow.
|
cannam@154
|
43 **
|
cannam@154
|
44 ** Fortunately the ISO C99 specifications define the functions lrint, lrintf,
|
cannam@154
|
45 ** llrint and llrintf which fix this problem as a side effect.
|
cannam@154
|
46 **
|
cannam@154
|
47 ** On Unix-like systems, the configure process should have detected the
|
cannam@154
|
48 ** presence of these functions. If they weren't found we have to replace them
|
cannam@154
|
49 ** here with a standard C cast.
|
cannam@154
|
50 */
|
cannam@154
|
51
|
cannam@154
|
52 /*
|
cannam@154
|
53 ** The C99 prototypes for lrint and lrintf are as follows:
|
cannam@154
|
54 **
|
cannam@154
|
55 ** long int lrintf (float x) ;
|
cannam@154
|
56 ** long int lrint (double x) ;
|
cannam@154
|
57 */
|
cannam@154
|
58
|
cannam@154
|
59 /* The presence of the required functions are detected during the configure
|
cannam@154
|
60 ** process and the values HAVE_LRINT and HAVE_LRINTF are set accordingly in
|
cannam@154
|
61 ** the config.h file.
|
cannam@154
|
62 */
|
cannam@154
|
63
|
cannam@154
|
64 /* With GCC, when SSE is available, the fastest conversion is cvtss2si. */
|
cannam@154
|
65 #if defined(__GNUC__) && defined(__SSE__)
|
cannam@154
|
66
|
cannam@154
|
67 #include <xmmintrin.h>
|
cannam@154
|
68 static OPUS_INLINE opus_int32 float2int(float x) {return _mm_cvt_ss2si(_mm_set_ss(x));}
|
cannam@154
|
69
|
cannam@154
|
70 #elif defined(HAVE_LRINTF)
|
cannam@154
|
71
|
cannam@154
|
72 /* These defines enable functionality introduced with the 1999 ISO C
|
cannam@154
|
73 ** standard. They must be defined before the inclusion of math.h to
|
cannam@154
|
74 ** engage them. If optimisation is enabled, these functions will be
|
cannam@154
|
75 ** inlined. With optimisation switched off, you have to link in the
|
cannam@154
|
76 ** maths library using -lm.
|
cannam@154
|
77 */
|
cannam@154
|
78
|
cannam@154
|
79 #define _ISOC9X_SOURCE 1
|
cannam@154
|
80 #define _ISOC99_SOURCE 1
|
cannam@154
|
81
|
cannam@154
|
82 #define __USE_ISOC9X 1
|
cannam@154
|
83 #define __USE_ISOC99 1
|
cannam@154
|
84
|
cannam@154
|
85 #include <math.h>
|
cannam@154
|
86 #define float2int(x) lrintf(x)
|
cannam@154
|
87
|
cannam@154
|
88 #elif (defined(HAVE_LRINT))
|
cannam@154
|
89
|
cannam@154
|
90 #define _ISOC9X_SOURCE 1
|
cannam@154
|
91 #define _ISOC99_SOURCE 1
|
cannam@154
|
92
|
cannam@154
|
93 #define __USE_ISOC9X 1
|
cannam@154
|
94 #define __USE_ISOC99 1
|
cannam@154
|
95
|
cannam@154
|
96 #include <math.h>
|
cannam@154
|
97 #define float2int(x) lrint(x)
|
cannam@154
|
98
|
cannam@154
|
99 #elif (defined(_MSC_VER) && _MSC_VER >= 1400) && (defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 1))
|
cannam@154
|
100 #include <xmmintrin.h>
|
cannam@154
|
101
|
cannam@154
|
102 __inline long int float2int(float value)
|
cannam@154
|
103 {
|
cannam@154
|
104 return _mm_cvtss_si32(_mm_load_ss(&value));
|
cannam@154
|
105 }
|
cannam@154
|
106 #elif (defined(_MSC_VER) && _MSC_VER >= 1400) && defined (_M_IX86)
|
cannam@154
|
107 #include <math.h>
|
cannam@154
|
108
|
cannam@154
|
109 /* Win32 doesn't seem to have these functions.
|
cannam@154
|
110 ** Therefore implement OPUS_INLINE versions of these functions here.
|
cannam@154
|
111 */
|
cannam@154
|
112
|
cannam@154
|
113 __inline long int
|
cannam@154
|
114 float2int (float flt)
|
cannam@154
|
115 { int intgr;
|
cannam@154
|
116
|
cannam@154
|
117 _asm
|
cannam@154
|
118 { fld flt
|
cannam@154
|
119 fistp intgr
|
cannam@154
|
120 } ;
|
cannam@154
|
121
|
cannam@154
|
122 return intgr ;
|
cannam@154
|
123 }
|
cannam@154
|
124
|
cannam@154
|
125 #else
|
cannam@154
|
126
|
cannam@154
|
127 #if (defined(__GNUC__) && defined(__STDC__) && __STDC__ && __STDC_VERSION__ >= 199901L)
|
cannam@154
|
128 /* supported by gcc in C99 mode, but not by all other compilers */
|
cannam@154
|
129 #warning "Don't have the functions lrint() and lrintf ()."
|
cannam@154
|
130 #warning "Replacing these functions with a standard C cast."
|
cannam@154
|
131 #endif /* __STDC_VERSION__ >= 199901L */
|
cannam@154
|
132 #include <math.h>
|
cannam@154
|
133 #define float2int(flt) ((int)(floor(.5+flt)))
|
cannam@154
|
134 #endif
|
cannam@154
|
135
|
cannam@154
|
136 #ifndef DISABLE_FLOAT_API
|
cannam@154
|
137 static OPUS_INLINE opus_int16 FLOAT2INT16(float x)
|
cannam@154
|
138 {
|
cannam@154
|
139 x = x*CELT_SIG_SCALE;
|
cannam@154
|
140 x = MAX32(x, -32768);
|
cannam@154
|
141 x = MIN32(x, 32767);
|
cannam@154
|
142 return (opus_int16)float2int(x);
|
cannam@154
|
143 }
|
cannam@154
|
144 #endif /* DISABLE_FLOAT_API */
|
cannam@154
|
145
|
cannam@154
|
146 #endif /* FLOAT_CAST_H */
|