[cairo] Fast conversion of float to int
Bill Spitzak
spitzak at d2.com
Tue Jun 28 14:07:45 PDT 2005
The following code may be useful for speeding Cairo up. Tests have shown
that the int-cast instruction in the Intel FP is way too slow, in
addition for graphics you never want round-toward-zero. These work as
long as you know the input is within a certain range and are as much as
10 times faster, mostly because they dont interrupt the fp chips
pipeline. Assembler hackers may be able to do the same thing better than
gcc does:
#if __BIG_ENDIAN_
#define iman_ 1
#else
#define iman_ 0
#endif
typedef long int32;
/* Fast version of (int)rint()
Works for -2147483648.5 .. 2147483647.49975574019
Requires IEEE floating point.
*/
inline int32 fast_rint(double val) {
val = val + 68719476736.0*65536.0*1.5;
return ((int32*)&val)[iman_];
}
/* Fast version of (int)floor()
Requires IEEE floating point.
Rounds numbers greater than n.9999923668 to n+1 rather than n,
this could be fixed by changing the FP rounding mode and using
the fast_rint() code.
Works for -32728 to 32727.99999236688
The alternative that uses long-long works for -2147483648 to
2147483647.999923688
*/
inline int32 fast_floor(double val) {
val = val + (68719476736.0*1.5);
#if 0
return (int32)((*(long long *)&val)>>16);
#else
return (((int32*)&val)[iman_]>>16);
#endif
}
More information about the cairo
mailing list