Chris@69: /* Copyright (C) 2001 Erik de Castro Lopo */ Chris@69: /* Chris@69: Redistribution and use in source and binary forms, with or without Chris@69: modification, are permitted provided that the following conditions Chris@69: are met: Chris@69: Chris@69: - Redistributions of source code must retain the above copyright Chris@69: notice, this list of conditions and the following disclaimer. Chris@69: Chris@69: - Redistributions in binary form must reproduce the above copyright Chris@69: notice, this list of conditions and the following disclaimer in the Chris@69: documentation and/or other materials provided with the distribution. Chris@69: Chris@69: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS Chris@69: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT Chris@69: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR Chris@69: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER Chris@69: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, Chris@69: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, Chris@69: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR Chris@69: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF Chris@69: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING Chris@69: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS Chris@69: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@69: */ Chris@69: Chris@69: /* Version 1.1 */ Chris@69: Chris@69: #ifndef FLOAT_CAST_H Chris@69: #define FLOAT_CAST_H Chris@69: Chris@69: Chris@69: #include "arch.h" Chris@69: Chris@69: /*============================================================================ Chris@69: ** On Intel Pentium processors (especially PIII and probably P4), converting Chris@69: ** from float to int is very slow. To meet the C specs, the code produced by Chris@69: ** most C compilers targeting Pentium needs to change the FPU rounding mode Chris@69: ** before the float to int conversion is performed. Chris@69: ** Chris@69: ** Changing the FPU rounding mode causes the FPU pipeline to be flushed. It Chris@69: ** is this flushing of the pipeline which is so slow. Chris@69: ** Chris@69: ** Fortunately the ISO C99 specifications define the functions lrint, lrintf, Chris@69: ** llrint and llrintf which fix this problem as a side effect. Chris@69: ** Chris@69: ** On Unix-like systems, the configure process should have detected the Chris@69: ** presence of these functions. If they weren't found we have to replace them Chris@69: ** here with a standard C cast. Chris@69: */ Chris@69: Chris@69: /* Chris@69: ** The C99 prototypes for lrint and lrintf are as follows: Chris@69: ** Chris@69: ** long int lrintf (float x) ; Chris@69: ** long int lrint (double x) ; Chris@69: */ Chris@69: Chris@69: /* The presence of the required functions are detected during the configure Chris@69: ** process and the values HAVE_LRINT and HAVE_LRINTF are set accordingly in Chris@69: ** the config.h file. Chris@69: */ Chris@69: Chris@69: /* With GCC, when SSE is available, the fastest conversion is cvtss2si. */ Chris@69: #if defined(__GNUC__) && defined(__SSE__) Chris@69: Chris@69: #include Chris@69: static OPUS_INLINE opus_int32 float2int(float x) {return _mm_cvt_ss2si(_mm_set_ss(x));} Chris@69: Chris@69: #elif defined(HAVE_LRINTF) Chris@69: Chris@69: /* These defines enable functionality introduced with the 1999 ISO C Chris@69: ** standard. They must be defined before the inclusion of math.h to Chris@69: ** engage them. If optimisation is enabled, these functions will be Chris@69: ** inlined. With optimisation switched off, you have to link in the Chris@69: ** maths library using -lm. Chris@69: */ Chris@69: Chris@69: #define _ISOC9X_SOURCE 1 Chris@69: #define _ISOC99_SOURCE 1 Chris@69: Chris@69: #define __USE_ISOC9X 1 Chris@69: #define __USE_ISOC99 1 Chris@69: Chris@69: #include Chris@69: #define float2int(x) lrintf(x) Chris@69: Chris@69: #elif (defined(HAVE_LRINT)) Chris@69: Chris@69: #define _ISOC9X_SOURCE 1 Chris@69: #define _ISOC99_SOURCE 1 Chris@69: Chris@69: #define __USE_ISOC9X 1 Chris@69: #define __USE_ISOC99 1 Chris@69: Chris@69: #include Chris@69: #define float2int(x) lrint(x) Chris@69: Chris@69: #elif (defined(_MSC_VER) && _MSC_VER >= 1400) && (defined(_M_X64) || (defined(_M_IX86_FP) && _M_IX86_FP >= 1)) Chris@69: #include Chris@69: Chris@69: __inline long int float2int(float value) Chris@69: { Chris@69: return _mm_cvtss_si32(_mm_load_ss(&value)); Chris@69: } Chris@69: #elif (defined(_MSC_VER) && _MSC_VER >= 1400) && defined (_M_IX86) Chris@69: #include Chris@69: Chris@69: /* Win32 doesn't seem to have these functions. Chris@69: ** Therefore implement OPUS_INLINE versions of these functions here. Chris@69: */ Chris@69: Chris@69: __inline long int Chris@69: float2int (float flt) Chris@69: { int intgr; Chris@69: Chris@69: _asm Chris@69: { fld flt Chris@69: fistp intgr Chris@69: } ; Chris@69: Chris@69: return intgr ; Chris@69: } Chris@69: Chris@69: #else Chris@69: Chris@69: #if (defined(__GNUC__) && defined(__STDC__) && __STDC__ && __STDC_VERSION__ >= 199901L) Chris@69: /* supported by gcc in C99 mode, but not by all other compilers */ Chris@69: #warning "Don't have the functions lrint() and lrintf ()." Chris@69: #warning "Replacing these functions with a standard C cast." Chris@69: #endif /* __STDC_VERSION__ >= 199901L */ Chris@69: #include Chris@69: #define float2int(flt) ((int)(floor(.5+flt))) Chris@69: #endif Chris@69: Chris@69: #ifndef DISABLE_FLOAT_API Chris@69: static OPUS_INLINE opus_int16 FLOAT2INT16(float x) Chris@69: { Chris@69: x = x*CELT_SIG_SCALE; Chris@69: x = MAX32(x, -32768); Chris@69: x = MIN32(x, 32767); Chris@69: return (opus_int16)float2int(x); Chris@69: } Chris@69: #endif /* DISABLE_FLOAT_API */ Chris@69: Chris@69: #endif /* FLOAT_CAST_H */