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