RigsofRods
Soft-body Physics Simulation
ApproxMath.h
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2009 Lefteris Stamatogiannakis
4 
5  For more information, see http://www.rigsofrods.org/
6 
7  Rigs of Rods is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License version 3, as
9  published by the Free Software Foundation.
10 
11  Rigs of Rods is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
23 
24 #pragma once
25 
26 #include "Application.h"
27 
28 static int mirand = 1;
29 
30 // Returns a random number in the range [0, 1]
31 inline float frand()
32 {
33  unsigned int a;
34 
35  mirand *= 16807;
36 
37  a = (mirand&0x007fffff) | 0x40000000;
38 
39  return( *((float*)&a) - 2.0f )*0.5f;
40 }
41 
42 // Returns a random number in the range [0, 2]
43 inline float frand_02()
44 {
45  unsigned int a;
46 
47  mirand *= 16807;
48 
49  a = (mirand&0x007fffff) | 0x40000000;
50 
51  return( *((float*)&a) - 2.0f );
52 }
53 
54 // Returns a random number in the range [-1, 1]
55 inline float frand_11()
56 {
57  unsigned int a;
58 
59  mirand *= 16807;
60 
61  a = (mirand&0x007fffff) | 0x40000000;
62 
63  return( *((float*)&a) - 3.0f );
64 }
65 
66 // Calculates approximate e^x.
67 // Use it in code not requiring precision
68 inline float approx_exp(const float x)
69 {
70  if (x < -15)
71  return 0.f ;
72  else if (x > 88)
73  return 1e38f ;
74  else {
75  int i=12102203*x+1064652319;
76  return *(float *)&i;
77  }
78 }
79 
80 // Calculates approximate 2^x
81 // Use it in code not requiring precision
82 inline float approx_pow2(const float x)
83 {
84  int i = 8388608*x+1065353216;
85 
86  return *(float *)&i;
87 }
88 
89 // Calculates approximate x^y
90 // Use it in code not requiring precision
91 inline float approx_pow(const float x, const float y)
92 {
93  float v = x;
94  int i = y * ( (*(int *)&v) - 1065353216) + 1065353216;
95 
96  return *(float *)&i;
97 }
98 
99 // Calculates approximate square_root(x)
100 // Use it in code not requiring precision
101 inline float approx_sqrt(const float y)
102 {
103  float f = y;
104  int i = (( (*(int *)&f) - 1065353216)>>1) + 1065353216;
105 
106  return *(float *)&i;
107 }
108 
109 // Calculates approximate 1/square_root(x)
110 // it is faster than fast_invSqrt BUT
111 // use it in code not requiring precision
112 inline float approx_invSqrt(const float y)
113 {
114  float f = y;
115  int i = 0x5f3759df - ( (*(int *)&f) >> 1);
116 
117  return *(float *)&i;
118 }
119 
120 // This function is a classic 1/square_root(x)code
121 // used by quake's game engine.
122 // It is very fast and has enough precision
123 // to drive a physics engine.
124 inline float fast_invSqrt(const float v)
125 {
126  float y = v;
127  int i = 0x5f3759df - ( (*(int *)&y) >>1);
128  y = *(float *)&i;
129 
130  y *= (1.5f - (0.5f * v * y * y));
131  return y;
132 }
133 
134 // It calculates a fast and accurate square_root(x)
135 inline float fast_sqrt(const float x)
136 {
137  return x * fast_invSqrt(x);
138 }
139 
140 inline float sign(const float x)
141 {
142  return (x > 0.0f) ? 1.0f : (x < 0.0f) ? -1.0f : 0.0f;
143 }
144 
145 // Ogre3 specific helpers
146 inline Ogre::Vector3 approx_normalise(Ogre::Vector3 v)
147 {
148  return v*approx_invSqrt(v.squaredLength());
149 }
150 
151 inline Ogre::Vector3 fast_normalise(Ogre::Vector3 v)
152 {
153  return v*fast_invSqrt(v.squaredLength());
154 }
155 
156 inline float approx_length(Ogre::Vector3 v)
157 {
158  return approx_sqrt(v.squaredLength());
159 }
160 
161 inline float fast_length(Ogre::Vector3 v)
162 {
163  return fast_sqrt(v.squaredLength());
164 }
165 
frand_02
float frand_02()
Definition: ApproxMath.h:43
y
float y
Definition: (ValueTypes) quaternion.h:6
sign
float sign(const float x)
Definition: ApproxMath.h:140
fast_invSqrt
float fast_invSqrt(const float v)
Definition: ApproxMath.h:124
approx_exp
float approx_exp(const float x)
Definition: ApproxMath.h:68
approx_invSqrt
float approx_invSqrt(const float y)
Definition: ApproxMath.h:112
fast_length
float fast_length(Ogre::Vector3 v)
Definition: ApproxMath.h:161
approx_pow
float approx_pow(const float x, const float y)
Definition: ApproxMath.h:91
approx_normalise
Ogre::Vector3 approx_normalise(Ogre::Vector3 v)
Definition: ApproxMath.h:146
approx_pow2
float approx_pow2(const float x)
Definition: ApproxMath.h:82
fast_normalise
Ogre::Vector3 fast_normalise(Ogre::Vector3 v)
Definition: ApproxMath.h:151
Application.h
Central state/object manager and communications hub.
approx_length
float approx_length(Ogre::Vector3 v)
Definition: ApproxMath.h:156
frand
float frand()
Definition: ApproxMath.h:31
mirand
static int mirand
Definition: ApproxMath.h:28
approx_sqrt
float approx_sqrt(const float y)
Definition: ApproxMath.h:101
fast_sqrt
float fast_sqrt(const float x)
Definition: ApproxMath.h:135
frand_11
float frand_11()
Definition: ApproxMath.h:55
x
float x
Definition: (ValueTypes) quaternion.h:5