1 /* Copyright (C) 2001 Erik de Castro Lopo <erikd AT mega-nerd DOT com> */
2 /*
3    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
17    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
18    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19    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.
25 */
26 
27 /* Version 1.1 */
28 
29 #ifndef FLOAT_CAST_H
30 #define FLOAT_CAST_H
31 
32 /*============================================================================
33 **	On Intel Pentium processors (especially PIII and probably P4), converting
34 **	from float to int is very slow. To meet the C specs, the code produced by
35 **	most C compilers targeting Pentium needs to change the FPU rounding mode
36 **	before the float to int conversion is performed.
37 **
38 **	Changing the FPU rounding mode causes the FPU pipeline to be flushed. It
39 **	is this flushing of the pipeline which is so slow.
40 **
41 **	Fortunately the ISO C99 specifications define the functions lrint, lrintf,
42 **	llrint and llrintf which fix this problem as a side effect.
43 **
44 **	On Unix-like systems, the configure process should have detected the
45 **	presence of these functions. If they weren't found we have to replace them
46 **	here with a standard C cast.
47 */
48 
49 /*
50 **	The C99 prototypes for lrint and lrintf are as follows:
51 **
52 **		long int lrintf (float x) ;
53 **		long int lrint  (double x) ;
54 */
55 
56 /*	The presence of the required functions are detected during the configure
57 **	process and the values HAVE_LRINT and HAVE_LRINTF are set accordingly in
58 **	the config.h file.
59 */
60 
61 #if (HAVE_LRINTF)
62 /*#if 0*/
63 
64 	/*	These defines enable functionality introduced with the 1999 ISO C
65 	**	standard. They must be defined before the inclusion of math.h to
66 	**	engage them. If optimisation is enabled, these functions will be
67 	**	inlined. With optimisation switched off, you have to link in the
68 	**	maths library using -lm.
69 	*/
70 
71 	#define	_ISOC9X_SOURCE	1
72 	#define _ISOC99_SOURCE	1
73 
74 	#define	__USE_ISOC9X	1
75 	#define	__USE_ISOC99	1
76 
77 	#include	<math.h>
78 	#define float2int(x) lrintf(x)
79 
80 #elif (defined(HAVE_LRINT))
81 
82 #define	_ISOC9X_SOURCE	1
83 #define _ISOC99_SOURCE	1
84 
85 #define	__USE_ISOC9X	1
86 #define	__USE_ISOC99	1
87 
88 #include	<math.h>
89 #define float2int(x) lrint(x)
90 
91 #elif (defined (WIN64) || defined (_WIN64))
92 	#include <xmmintrin.h>
93 
float2int(float value)94 	__inline long int float2int(float value)
95 	{
96 		return _mm_cvtss_si32(_mm_load_ss(&value));
97 	}
98 #elif (defined (WIN32) || defined (_WIN32))
99 
100 	#include	<math.h>
101 
102 	/*	Win32 doesn't seem to have these functions.
103 	**	Therefore implement inline versions of these functions here.
104 	*/
105 
106 	__inline long int
float2int(float flt)107 	float2int (float flt)
108 	{	int intgr;
109 
110 		_asm
111 		{	fld flt
112 			fistp intgr
113 			} ;
114 
115 		return intgr ;
116 	}
117 
118 #else
119 
120 #ifdef __GNUC__ /* supported by gcc, but not by all other compilers*/
121 	#warning "Don't have the functions lrint() and lrintf ()."
122 	#warning "Replacing these functions with a standard C cast."
123 #endif /* __GNUC__ */
124 
125 	#include	<math.h>
126 
127 	#define	float2int(flt)		((int)(floor(.5+flt)))
128 
129 #endif
130 
131 
132 #endif /* FLOAT_CAST_H */
133