]> git.mxchange.org Git - simgear.git/blob - simgear/math/fastmath.hxx
Frederic Bouvier:
[simgear.git] / simgear / math / fastmath.hxx
1 /*
2  * \file fastmath.hxx
3  * fast mathematics routines.
4  *
5  * Refferences:
6  *
7  * A Fast, Compact Approximation of the Exponential Function
8  * Nicol N. Schraudolph
9  * IDSIA, Lugano, Switzerland
10  * http://www.inf.ethz.ch/~schraudo/pubs/exp.pdf
11  *
12  * Fast log() Function, by Laurent de Soras:
13  * http://www.flipcode.com/cgi-bin/msg.cgi?showThread=Tip-Fastlogfunction&forum=totd&id=-1
14  *
15  */
16
17 /*
18  * $Id$
19  */
20
21 #ifndef _SG_FMATH_HXX
22 #define _SG_FMATH_HXX 1
23
24 #ifndef __cplusplus
25 # error This library requires C++
26 #endif
27
28 #ifdef _MSC_VER
29 #define  _USE_MATH_DEFINES
30 #endif
31 #include <math.h>
32
33
34 double fast_exp(double val);
35 double fast_exp2(const double val);
36
37 float fast_pow(const float val1, const float val2);
38 float fast_log2(const float cal);
39 float fast_root(const float f, const int n);
40
41 float _fast_pow2(const float cal);
42 float _fast_log2(const float val);
43
44 float fast_sin(const float val);
45 float fast_cos(const float val);
46 float fast_tan(const float val);
47 float fast_asin(const float val);
48 float fast_acos(const float val);
49 float fast_atan(const float val);
50
51 void fast_BSL(float &x, register unsigned long shiftAmount);
52 void fast_BSR(float &x, register unsigned long shiftAmount);
53
54
55 inline float fast_log2 (float val)
56 {
57     int * const  exp_ptr = reinterpret_cast <int *> (&val);
58     int          x = *exp_ptr;
59     const int    log_2 = ((x >> 23) & 255) - 128;
60     x &= ~(255 << 23);
61     x += 127 << 23;
62     *exp_ptr = x;
63
64     val = ((-1.0f/3) * val + 2) * val - 2.0f/3;   // (1)
65
66     return (val + log_2);
67 }
68
69
70 /**
71  * This function is about 3 times faster than the system log() function
72  * and has an error of about 0.01%
73  */
74 inline float fast_log (const float &val)
75 {
76    return (fast_log2 (val) * 0.69314718f);
77 }
78
79 inline float fast_log10 (const float &val)
80 {
81    return (fast_log2(val) / 3.321928095f);
82 }
83
84
85 /**
86  * This function is about twice as fast as the system pow(x,y) function
87  */
88 inline float fast_pow(const float val1, const float val2)
89 {
90    return _fast_pow2(val2 * _fast_log2(val1));
91 }
92
93
94 /*
95  * Haven't seen this elsewhere, probably because it is too obvious?
96  * Anyway, these functions are intended for 32-bit floating point numbers
97  * only and should work a bit faster than the regular ones.
98  */
99 inline float fast_abs(float f)
100 {
101     int i=((*(int*)&f)&0x7fffffff);
102     return (*(float*)&i);
103 }
104
105 inline float fast_neg(float f)
106 {
107     int i=((*(int*)&f)^0x80000000);
108     return (*(float*)&i);
109 }
110
111 inline int fast_sgn(float f)
112 {
113     return 1+(((*(int*)&f)>>31)<<1);
114 }
115
116 #endif // !_SG_FMATH_HXX
117