media/libopus/celt/float_cast.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial