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