~ubuntu-branches/debian/sid/ember/sid

« back to all changes in this revision

Viewing changes to src/framework/float_cast.h

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2009-07-23 07:46:40 UTC
  • Revision ID: james.westby@ubuntu.com-20090723074640-wh0ukzis0kda36qv
Tags: upstream-0.5.6
ImportĀ upstreamĀ versionĀ 0.5.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#ifndef FLOAT_CAST_H
 
39
#define FLOAT_CAST_H
 
40
 
 
41
#ifdef HAVE_CONFIG_H
 
42
#include "config.h"
 
43
#endif  
 
44
 
 
45
/*      The presence of the required functions are detected during the configure
 
46
**      process and the values HAVE_LRINT and HAVE_LRINTF are set accordingly in
 
47
**      the config.h file.
 
48
*/
 
49
 
 
50
#if (HAVE_MATH_H)
 
51
 
 
52
        /*      These defines enable functionality introduced with the 1999 ISO C
 
53
        **      standard. They must be defined before the inclusion of math.h to
 
54
        **      engage them. If optimisation is enabled, these functions will be 
 
55
        **      inlined. With optimisation switched off, you have to link in the
 
56
        **      maths library using -lm.
 
57
        */
 
58
 
 
59
        #define _ISOC9X_SOURCE  1
 
60
        #define _ISOC99_SOURCE  1
 
61
 
 
62
        #define __USE_ISOC9X    1
 
63
        #define __USE_ISOC99    1
 
64
 
 
65
        #include        <math.h>
 
66
        
 
67
#elif (defined (WIN32) || defined (_WIN32))
 
68
 
 
69
        #include        <math.h>
 
70
 
 
71
        /*      Win32 doesn't seem to have these functions. 
 
72
        **      Therefore implement inline versions of these functions here.
 
73
        */
 
74
        
 
75
        __inline long int 
 
76
        lrint (double flt) 
 
77
        {       int intgr;
 
78
 
 
79
                _asm
 
80
                {       fld flt
 
81
                        fistp intgr
 
82
                        } ;
 
83
                        
 
84
                return intgr ;
 
85
        } 
 
86
        
 
87
        __inline long int 
 
88
        lrintf (float flt)
 
89
        {       int intgr;
 
90
 
 
91
                _asm
 
92
                {       fld flt
 
93
                        fistp intgr
 
94
                        } ;
 
95
                        
 
96
                return intgr ;
 
97
        }
 
98
#define HAVE_LRINTF 1
 
99
#define HAVE_LRINT 1
 
100
#else
 
101
 
 
102
        #warning "Don't have the functions lrint() and lrintf ()."
 
103
        #warning "Replacing these functions with a standard C cast."
 
104
 
 
105
        #include        <math.h>
 
106
 
 
107
        #define lrint(dbl)              ((int)(dbl))
 
108
        #define lrintf(flt)             ((int)(flt))
 
109
 
 
110
#endif
 
111
 
 
112
#endif //FLOAT_CAST_H
 
113