]> git.mxchange.org Git - simgear.git/blob - simgear/math/fastmath.cxx
Adjust the fog punch through effect for oursun
[simgear.git] / simgear / math / fastmath.cxx
1 /*
2  * \file fastmath.cxx
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
22
23 #include "fastmath.hxx"
24
25 /**
26  * This function is on avarage 9 times faster than the system exp() function
27  * and has an error of about 1.5%
28  */
29 static union {
30     double d;
31     struct {
32 #if BYTE_ORDER == BIG_ENDIAN
33         int i, j;
34 #else
35         int j, i;
36 #endif
37     } n;
38 } _eco;
39
40 double fast_exp(double val) {
41     const double a = 1048576/M_LN2;
42     const double b_c = 1072632447; /* 1072693248 - 60801 */
43
44     _eco.n.i = a*val + b_c;
45
46     return _eco.d;
47 }
48
49
50 /**
51  * While we're on the subject, someone might have use for these as well?
52  * Float Shift Left and Float Shift Right. Do what you want with this.
53  */
54 void fast_BSL(float &x, register unsigned long shiftAmount) {
55
56         *(unsigned long*)&x+=shiftAmount<<23;
57
58 }
59
60 void fast_BSR(float &x, register unsigned long shiftAmount) {
61
62         *(unsigned long*)&x-=shiftAmount<<23;
63
64 }
65