1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libopus/celt/float_cast.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,140 @@ 1.4 +/* Copyright (C) 2001 Erik de Castro Lopo <erikd AT mega-nerd DOT com> */ 1.5 +/* 1.6 + Redistribution and use in source and binary forms, with or without 1.7 + modification, are permitted provided that the following conditions 1.8 + are met: 1.9 + 1.10 + - Redistributions of source code must retain the above copyright 1.11 + notice, this list of conditions and the following disclaimer. 1.12 + 1.13 + - Redistributions in binary form must reproduce the above copyright 1.14 + notice, this list of conditions and the following disclaimer in the 1.15 + documentation and/or other materials provided with the distribution. 1.16 + 1.17 + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.18 + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.19 + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.20 + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 1.21 + OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.22 + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.23 + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.24 + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.25 + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.26 + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.27 + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.28 +*/ 1.29 + 1.30 +/* Version 1.1 */ 1.31 + 1.32 +#ifndef FLOAT_CAST_H 1.33 +#define FLOAT_CAST_H 1.34 + 1.35 + 1.36 +#include "arch.h" 1.37 + 1.38 +/*============================================================================ 1.39 +** On Intel Pentium processors (especially PIII and probably P4), converting 1.40 +** from float to int is very slow. To meet the C specs, the code produced by 1.41 +** most C compilers targeting Pentium needs to change the FPU rounding mode 1.42 +** before the float to int conversion is performed. 1.43 +** 1.44 +** Changing the FPU rounding mode causes the FPU pipeline to be flushed. It 1.45 +** is this flushing of the pipeline which is so slow. 1.46 +** 1.47 +** Fortunately the ISO C99 specifications define the functions lrint, lrintf, 1.48 +** llrint and llrintf which fix this problem as a side effect. 1.49 +** 1.50 +** On Unix-like systems, the configure process should have detected the 1.51 +** presence of these functions. If they weren't found we have to replace them 1.52 +** here with a standard C cast. 1.53 +*/ 1.54 + 1.55 +/* 1.56 +** The C99 prototypes for lrint and lrintf are as follows: 1.57 +** 1.58 +** long int lrintf (float x) ; 1.59 +** long int lrint (double x) ; 1.60 +*/ 1.61 + 1.62 +/* The presence of the required functions are detected during the configure 1.63 +** process and the values HAVE_LRINT and HAVE_LRINTF are set accordingly in 1.64 +** the config.h file. 1.65 +*/ 1.66 + 1.67 +#if (HAVE_LRINTF) 1.68 + 1.69 +/* These defines enable functionality introduced with the 1999 ISO C 1.70 +** standard. They must be defined before the inclusion of math.h to 1.71 +** engage them. If optimisation is enabled, these functions will be 1.72 +** inlined. With optimisation switched off, you have to link in the 1.73 +** maths library using -lm. 1.74 +*/ 1.75 + 1.76 +#define _ISOC9X_SOURCE 1 1.77 +#define _ISOC99_SOURCE 1 1.78 + 1.79 +#define __USE_ISOC9X 1 1.80 +#define __USE_ISOC99 1 1.81 + 1.82 +#include <math.h> 1.83 +#define float2int(x) lrintf(x) 1.84 + 1.85 +#elif (defined(HAVE_LRINT)) 1.86 + 1.87 +#define _ISOC9X_SOURCE 1 1.88 +#define _ISOC99_SOURCE 1 1.89 + 1.90 +#define __USE_ISOC9X 1 1.91 +#define __USE_ISOC99 1 1.92 + 1.93 +#include <math.h> 1.94 +#define float2int(x) lrint(x) 1.95 + 1.96 +#elif (defined(_MSC_VER) && _MSC_VER >= 1400) && (defined (WIN64) || defined (_WIN64)) 1.97 + #include <xmmintrin.h> 1.98 + 1.99 + __inline long int float2int(float value) 1.100 + { 1.101 + return _mm_cvtss_si32(_mm_load_ss(&value)); 1.102 + } 1.103 +#elif (defined(_MSC_VER) && _MSC_VER >= 1400) && (defined (WIN32) || defined (_WIN32)) 1.104 + #include <math.h> 1.105 + 1.106 + /* Win32 doesn't seem to have these functions. 1.107 + ** Therefore implement OPUS_INLINE versions of these functions here. 1.108 + */ 1.109 + 1.110 + __inline long int 1.111 + float2int (float flt) 1.112 + { int intgr; 1.113 + 1.114 + _asm 1.115 + { fld flt 1.116 + fistp intgr 1.117 + } ; 1.118 + 1.119 + return intgr ; 1.120 + } 1.121 + 1.122 +#else 1.123 + 1.124 +#if (defined(__GNUC__) && defined(__STDC__) && __STDC__ && __STDC_VERSION__ >= 199901L) 1.125 + /* supported by gcc in C99 mode, but not by all other compilers */ 1.126 + #warning "Don't have the functions lrint() and lrintf ()." 1.127 + #warning "Replacing these functions with a standard C cast." 1.128 +#endif /* __STDC_VERSION__ >= 199901L */ 1.129 + #include <math.h> 1.130 + #define float2int(flt) ((int)(floor(.5+flt))) 1.131 +#endif 1.132 + 1.133 +#ifndef DISABLE_FLOAT_API 1.134 +static OPUS_INLINE opus_int16 FLOAT2INT16(float x) 1.135 +{ 1.136 + x = x*CELT_SIG_SCALE; 1.137 + x = MAX32(x, -32768); 1.138 + x = MIN32(x, 32767); 1.139 + return (opus_int16)float2int(x); 1.140 +} 1.141 +#endif /* DISABLE_FLOAT_API */ 1.142 + 1.143 +#endif /* FLOAT_CAST_H */