3 * fast mathematics routines.
7 * A Fast, Compact Approximation of the Exponential Function
9 * IDSIA, Lugano, Switzerland
10 * http://www.inf.ethz.ch/~schraudo/pubs/exp.pdf
12 * Fast log() Function, by Laurent de Soras:
13 * http://www.flipcode.com/cgi-bin/msg.cgi?showThread=Tip-Fastlogfunction&forum=totd&id=-1
22 #define _SG_FMATH_HXX 1
25 # error This library requires C++
31 double fast_exp(double val);
32 double fast_exp2(const double val);
34 float fast_pow(const float val1, const float val2);
35 float fast_log2(const float cal);
36 float fast_root(const float f, const int n);
38 float _fast_pow2(const float cal);
39 float _fast_log2(const float val);
41 float fast_sin(const float val);
42 float fast_cos(const float val);
43 float fast_tan(const float val);
44 float fast_asin(const float val);
45 float fast_acos(const float val);
46 float fast_atan(const float val);
48 void fast_BSL(float &x, register unsigned long shiftAmount);
49 void fast_BSR(float &x, register unsigned long shiftAmount);
52 inline float fast_log2 (float val)
54 int * const exp_ptr = reinterpret_cast <int *> (&val);
56 const int log_2 = ((x >> 23) & 255) - 128;
61 val = ((-1.0f/3) * val + 2) * val - 2.0f/3; // (1)
68 * This function is about 3 times faster than the system log() function
69 * and has an error of about 0.01%
71 inline float fast_log (const float &val)
73 return (fast_log2 (val) * 0.69314718f);
76 inline float fast_log10 (const float &val)
78 return (fast_log2(val) / 3.321928095f);
83 * This function is about twice as fast as the system pow(x,y) function
85 inline float fast_pow(const float val1, const float val2)
87 return _fast_pow2(val2 * _fast_log2(val1));
92 * Haven't seen this elsewhere, probably because it is too obvious?
93 * Anyway, these functions are intended for 32-bit floating point numbers
94 * only and should work a bit faster than the regular ones.
96 inline float fast_abs(float f)
98 int i=((*(int*)&f)&0x7fffffff);
102 inline float fast_neg(float f)
104 int i=((*(int*)&f)^0x80000000);
105 return (*(float*)&i);
108 inline int fast_sgn(float f)
110 return 1+(((*(int*)&f)>>31)<<1);
113 #endif // !_SG_FMATH_HXX