blob: 9095bb6627ee143b831fbc010dd3b5143b3acfb2 [file] [log] [blame]
Jean-Marc Valin3806c1d2011-02-09 22:37:41 -05001/* Copyright (C) 2001 Erik de Castro Lopo <erikd AT mega-nerd DOT com> */
Jean-Marc Valind9b95652008-08-31 23:34:47 -04002/*
Jean-Marc Valin3806c1d2011-02-09 22:37:41 -05003 Redistribution and use in source and binary forms, with or without
4 modification, are permitted provided that the following conditions
5 are met:
6
7 - Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9
10 - Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Jean-Marc Valincb05e7c2012-04-20 16:40:24 -040017 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
18 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Jean-Marc Valin3806c1d2011-02-09 22:37:41 -050019 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Jean-Marc Valind9b95652008-08-31 23:34:47 -040025*/
26
27/* Version 1.1 */
28
29#ifndef FLOAT_CAST_H
30#define FLOAT_CAST_H
31
Jean-Marc Valin66820f32012-01-31 02:03:39 -050032
33#include "arch.h"
34
Gregory Maxwell71d39ad2011-07-30 00:00:29 -040035/*============================================================================
Ralph Gilesda025d52011-10-26 20:24:49 -070036** On Intel Pentium processors (especially PIII and probably P4), converting
37** from float to int is very slow. To meet the C specs, the code produced by
38** most C compilers targeting Pentium needs to change the FPU rounding mode
39** before the float to int conversion is performed.
Jean-Marc Valind9b95652008-08-31 23:34:47 -040040**
Ralph Gilesda025d52011-10-26 20:24:49 -070041** Changing the FPU rounding mode causes the FPU pipeline to be flushed. It
42** is this flushing of the pipeline which is so slow.
Jean-Marc Valind9b95652008-08-31 23:34:47 -040043**
Ralph Gilesda025d52011-10-26 20:24:49 -070044** Fortunately the ISO C99 specifications define the functions lrint, lrintf,
45** llrint and llrintf which fix this problem as a side effect.
Jean-Marc Valind9b95652008-08-31 23:34:47 -040046**
Ralph Gilesda025d52011-10-26 20:24:49 -070047** On Unix-like systems, the configure process should have detected the
48** presence of these functions. If they weren't found we have to replace them
49** here with a standard C cast.
Jean-Marc Valind9b95652008-08-31 23:34:47 -040050*/
51
Ralph Gilesda025d52011-10-26 20:24:49 -070052/*
53** The C99 prototypes for lrint and lrintf are as follows:
54**
55** long int lrintf (float x) ;
56** long int lrint (double x) ;
Jean-Marc Valind9b95652008-08-31 23:34:47 -040057*/
58
Ralph Gilesda025d52011-10-26 20:24:49 -070059/* The presence of the required functions are detected during the configure
60** process and the values HAVE_LRINT and HAVE_LRINTF are set accordingly in
61** the config.h file.
Jean-Marc Valind9b95652008-08-31 23:34:47 -040062*/
63
64#if (HAVE_LRINTF)
Jean-Marc Valind9b95652008-08-31 23:34:47 -040065
Ralph Gilesda025d52011-10-26 20:24:49 -070066/* These defines enable functionality introduced with the 1999 ISO C
67** standard. They must be defined before the inclusion of math.h to
68** engage them. If optimisation is enabled, these functions will be
69** inlined. With optimisation switched off, you have to link in the
70** maths library using -lm.
Christian Hoene59a93ab2011-07-25 09:04:51 -040071*/
Jean-Marc Valind9b95652008-08-31 23:34:47 -040072
Ralph Gilesda025d52011-10-26 20:24:49 -070073#define _ISOC9X_SOURCE 1
74#define _ISOC99_SOURCE 1
Jean-Marc Valind9b95652008-08-31 23:34:47 -040075
Ralph Gilesda025d52011-10-26 20:24:49 -070076#define __USE_ISOC9X 1
77#define __USE_ISOC99 1
Jean-Marc Valind9b95652008-08-31 23:34:47 -040078
Ralph Gilesda025d52011-10-26 20:24:49 -070079#include <math.h>
Christian Hoene59a93ab2011-07-25 09:04:51 -040080#define float2int(x) lrintf(x)
Jean-Marc Valind9b95652008-08-31 23:34:47 -040081
82#elif (defined(HAVE_LRINT))
83
Ralph Gilesda025d52011-10-26 20:24:49 -070084#define _ISOC9X_SOURCE 1
85#define _ISOC99_SOURCE 1
Jean-Marc Valind9b95652008-08-31 23:34:47 -040086
Ralph Gilesda025d52011-10-26 20:24:49 -070087#define __USE_ISOC9X 1
88#define __USE_ISOC99 1
Jean-Marc Valind9b95652008-08-31 23:34:47 -040089
Ralph Gilesda025d52011-10-26 20:24:49 -070090#include <math.h>
Jean-Marc Valind9b95652008-08-31 23:34:47 -040091#define float2int(x) lrint(x)
92
John Ridgese0ae9802010-10-16 18:00:54 -040093#elif (defined (WIN64) || defined (_WIN64))
Ralph Gilesda025d52011-10-26 20:24:49 -070094 #include <xmmintrin.h>
John Ridgese0ae9802010-10-16 18:00:54 -040095
Ralph Gilesda025d52011-10-26 20:24:49 -070096 __inline long int float2int(float value)
97 {
98 return _mm_cvtss_si32(_mm_load_ss(&value));
99 }
Jean-Marc Valind9b95652008-08-31 23:34:47 -0400100#elif (defined (WIN32) || defined (_WIN32))
Ralph Gilesda025d52011-10-26 20:24:49 -0700101 #include <math.h>
Jean-Marc Valind9b95652008-08-31 23:34:47 -0400102
Ralph Gilesda025d52011-10-26 20:24:49 -0700103 /* Win32 doesn't seem to have these functions.
104 ** Therefore implement inline versions of these functions here.
105 */
Jean-Marc Valind9b95652008-08-31 23:34:47 -0400106
Ralph Gilesda025d52011-10-26 20:24:49 -0700107 __inline long int
108 float2int (float flt)
109 { int intgr;
110
111 _asm
112 { fld flt
113 fistp intgr
114 } ;
115
116 return intgr ;
117 }
Jean-Marc Valind9b95652008-08-31 23:34:47 -0400118
119#else
120
Gregory Maxwellf7bf43b2011-09-19 15:49:43 -0400121#if (defined(__GNUC__) && defined(__STDC__) && __STDC__ && __STDC_VERSION__ >= 199901L)
122 /* supported by gcc in C99 mode, but not by all other compilers */
Ralph Gilesda025d52011-10-26 20:24:49 -0700123 #warning "Don't have the functions lrint() and lrintf ()."
124 #warning "Replacing these functions with a standard C cast."
Gregory Maxwellf7bf43b2011-09-19 15:49:43 -0400125#endif /* __STDC_VERSION__ >= 199901L */
Ralph Gilesda025d52011-10-26 20:24:49 -0700126 #include <math.h>
127 #define float2int(flt) ((int)(floor(.5+flt)))
Jean-Marc Valind9b95652008-08-31 23:34:47 -0400128#endif
129
Jean-Marc Valin222494f2011-08-17 15:53:37 -0400130static inline opus_int16 FLOAT2INT16(float x)
131{
132 x = x*CELT_SIG_SCALE;
133 x = MAX32(x, -32768);
134 x = MIN32(x, 32767);
135 return (opus_int16)float2int(x);
136}
137
Jean-Marc Valind9b95652008-08-31 23:34:47 -0400138#endif /* FLOAT_CAST_H */