Audacity  2.2.0
float_cast.h
Go to the documentation of this file.
1 /*
2 ** Copyright (C) 2001 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
3 **
4 ** Permission to use, copy, modify, distribute, and sell this file for any
5 ** purpose is hereby granted without fee, provided that the above copyright
6 ** and this permission notice appear in all copies. No representations are
7 ** made about the suitability of this software for any purpose. It is
8 ** provided "as is" without express or implied warranty.
9 */
10 
11 /* Version 1.1 */
12 
13 
14 /*============================================================================
15 ** On Intel Pentium processors (especially PIII and probably P4), converting
16 ** from float to int is very slow. To meet the C specs, the code produced by
17 ** most C compilers targeting Pentium needs to change the FPU rounding mode
18 ** before the float to int conversion is performed.
19 **
20 ** Changing the FPU rounding mode causes the FPU pipeline to be flushed. It
21 ** is this flushing of the pipeline which is so slow.
22 **
23 ** Fortunately the ISO C99 specifications define the functions lrint, lrintf,
24 ** llrint and llrintf which fix this problem as a side effect.
25 **
26 ** On Unix-like systems, the configure process should have detected the
27 ** presence of these functions. If they weren't found we have to replace them
28 ** here with a standard C cast.
29 */
30 
31 /*
32 ** The C99 prototypes for lrint and lrintf are as follows:
33 **
34 ** long int lrintf (float x) ;
35 ** long int lrint (double x) ;
36 */
37 
38 #include "Audacity.h"
39 
40 /* The presence of the required functions are detected during the configure
41 ** process and the values HAVE_LRINT and HAVE_LRINTF are set accordingly in
42 ** the config.h file.
43 */
44 
45 #if (HAVE_LRINT && HAVE_LRINTF)
46 
47  /* These defines enable functionality introduced with the 1999 ISO C
48  ** standard. They must be defined before the inclusion of math.h to
49  ** engage them. If optimisation is enabled, these functions will be
50  ** inlined. With optimisation switched off, you have to link in the
51  ** maths library using -lm.
52  */
53 
54  #define _ISOC9X_SOURCE 1
55  #define _ISOC99_SOURCE 1
56 
57  #define __USE_ISOC9X 1
58  #define __USE_ISOC99 1
59 
60  #include <math.h>
61 
62 #elif (defined (WIN32) || defined (_WIN32))
63 
64  // Including math.h allows us to use the inline assembler versions without
65  // producing errors in newer Visual Studio versions.
66  // Without the include, we get different linkage error messages.
67  // Without the inline assembler versions, these functions are VERY slow.
68  // I also see that the include was part of the original source for this file:
69  // http://www.mega-nerd.com/FPcast/
70 
71  #include <math.h>
72 
73  /* Win32 doesn't seem to have these functions.
74  ** Therefore implement inline versions of these functions here.
75  */
76  __inline long int
77  lrint (double flt)
78  { int intgr;
79 
80  _asm
81  { fld flt
82  fistp intgr
83  } ;
84 
85  return intgr ;
86  }
87 
88  __inline long int
89  lrintf (float flt)
90  { int intgr;
91 
92  _asm
93  { fld flt
94  fistp intgr
95  } ;
96 
97  return intgr ;
98  }
99 
100  __inline long long int
101  llrint (double flt)
102  { long long int intgr;
103 
104  _asm
105  { fld flt
106  fistp intgr
107  } ;
108 
109  return intgr ;
110  }
111 
112  __inline long long int
113  llrintf (float flt)
114  { long long int intgr;
115 
116  _asm
117  { fld flt
118  fistp intgr
119  } ;
120 
121  return intgr ;
122  }
123 
124 #else
125 
126  /* dmazzoni: modified these to do a proper rounding, even though
127  * it's slower. Correctness and consistency is more important
128  * than speed, especially since lrint/lrintf are certainly not
129  * available everywhere.
130  *
131  * MM: Now uses internal math.h rint() function
132  */
133 
134  #include <math.h>
135 
136  #define lrint(dbl) ((int)rint(dbl))
137  #define lrintf(flt) ((int)rint(flt))
138 
139 #endif
#define lrint(dbl)
Definition: float_cast.h:136
#define lrintf(flt)
Definition: float_cast.h:137