cannam@95
|
1 #ifndef ERIKD_FLOATCAST_H
|
cannam@95
|
2 #define ERIKD_FLOATCAST_H
|
cannam@95
|
3
|
cannam@95
|
4 /*
|
cannam@95
|
5 ** Copyright (C) 2001 Erik de Castro Lopo <erikd AT mega-nerd DOT com>
|
cannam@95
|
6 **
|
cannam@95
|
7 ** Permission to use, copy, modify, distribute, and sell this file for any
|
cannam@95
|
8 ** purpose is hereby granted without fee, provided that the above copyright
|
cannam@95
|
9 ** and this permission notice appear in all copies. No representations are
|
cannam@95
|
10 ** made about the suitability of this software for any purpose. It is
|
cannam@95
|
11 ** provided "as is" without express or implied warranty.
|
cannam@95
|
12 */
|
cannam@95
|
13
|
cannam@95
|
14 /* Version 1.1 */
|
cannam@95
|
15
|
cannam@95
|
16
|
cannam@95
|
17 /*============================================================================
|
cannam@95
|
18 ** On Intel Pentium processors (especially PIII and probably P4), converting
|
cannam@95
|
19 ** from float to int is very slow. To meet the C specs, the code produced by
|
cannam@95
|
20 ** most C compilers targeting Pentium needs to change the FPU rounding mode
|
cannam@95
|
21 ** before the float to int conversion is performed.
|
cannam@95
|
22 **
|
cannam@95
|
23 ** Changing the FPU rounding mode causes the FPU pipeline to be flushed. It
|
cannam@95
|
24 ** is this flushing of the pipeline which is so slow.
|
cannam@95
|
25 **
|
cannam@95
|
26 ** Fortunately the ISO C99 specifications define the functions lrint, lrintf,
|
cannam@95
|
27 ** llrint and llrintf which fix this problem as a side effect.
|
cannam@95
|
28 **
|
cannam@95
|
29 ** On Unix-like systems, the configure process should have detected the
|
cannam@95
|
30 ** presence of these functions. If they weren't found we have to replace them
|
cannam@95
|
31 ** here with a standard C cast.
|
cannam@95
|
32 */
|
cannam@95
|
33
|
cannam@95
|
34 /*
|
cannam@95
|
35 ** The C99 prototypes for lrint and lrintf are as follows:
|
cannam@95
|
36 **
|
cannam@95
|
37 ** long int lrintf (float x) ;
|
cannam@95
|
38 ** long int lrint (double x) ;
|
cannam@95
|
39 */
|
cannam@95
|
40
|
cannam@95
|
41 #if (defined (WIN32) || defined (_WIN32))
|
cannam@95
|
42
|
cannam@95
|
43 #include <math.h>
|
cannam@95
|
44
|
cannam@95
|
45 /* Win32 doesn't seem to have these functions.
|
cannam@95
|
46 ** Therefore implement inline versions of these functions here.
|
cannam@95
|
47 */
|
cannam@95
|
48
|
cannam@95
|
49 __inline long int
|
cannam@95
|
50 lrint (double flt)
|
cannam@95
|
51 { int intgr;
|
cannam@95
|
52
|
cannam@95
|
53 _asm
|
cannam@95
|
54 { fld flt
|
cannam@95
|
55 fistp intgr
|
cannam@95
|
56 } ;
|
cannam@95
|
57
|
cannam@95
|
58 return intgr ;
|
cannam@95
|
59 }
|
cannam@95
|
60
|
cannam@95
|
61 __inline long int
|
cannam@95
|
62 lrintf (float flt)
|
cannam@95
|
63 { int intgr;
|
cannam@95
|
64
|
cannam@95
|
65 _asm
|
cannam@95
|
66 { fld flt
|
cannam@95
|
67 fistp intgr
|
cannam@95
|
68 } ;
|
cannam@95
|
69
|
cannam@95
|
70 return intgr ;
|
cannam@95
|
71 }
|
cannam@95
|
72
|
cannam@95
|
73 #endif
|
cannam@95
|
74
|
cannam@95
|
75 #endif
|