doom3-gpl
Doom 3 GPL source release
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Math.cpp
Go to the documentation of this file.
1 /*
2 ===========================================================================
3 
4 Doom 3 GPL Source Code
5 Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
6 
7 This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
8 
9 Doom 3 Source Code is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13 
14 Doom 3 Source Code is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
21 
22 In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
23 
24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
25 
26 ===========================================================================
27 */
28 
29 #include "../precompiled.h"
30 #pragma hdrstop
31 
32 
33 const float idMath::PI = 3.14159265358979323846f;
34 const float idMath::TWO_PI = 2.0f * PI;
35 const float idMath::HALF_PI = 0.5f * PI;
36 const float idMath::ONEFOURTH_PI = 0.25f * PI;
37 const float idMath::E = 2.71828182845904523536f;
38 const float idMath::SQRT_TWO = 1.41421356237309504880f;
39 const float idMath::SQRT_THREE = 1.73205080756887729352f;
40 const float idMath::SQRT_1OVER2 = 0.70710678118654752440f;
41 const float idMath::SQRT_1OVER3 = 0.57735026918962576450f;
42 const float idMath::M_DEG2RAD = PI / 180.0f;
43 const float idMath::M_RAD2DEG = 180.0f / PI;
44 const float idMath::M_SEC2MS = 1000.0f;
45 const float idMath::M_MS2SEC = 0.001f;
46 const float idMath::INFINITY = 1e30f;
47 const float idMath::FLT_EPSILON = 1.192092896e-07f;
48 
49 bool idMath::initialized = false;
50 dword idMath::iSqrt[SQRT_TABLE_SIZE]; // inverse square root lookup table
51 
52 /*
53 ===============
54 idMath::Init
55 ===============
56 */
57 void idMath::Init( void ) {
58  union _flint fi, fo;
59 
60  for ( int i = 0; i < SQRT_TABLE_SIZE; i++ ) {
61  fi.i = ((EXP_BIAS-1) << EXP_POS) | (i << LOOKUP_POS);
62  fo.f = (float)( 1.0 / sqrt( fi.f ) );
63  iSqrt[i] = ((dword)(((fo.i + (1<<(SEED_POS-2))) >> SEED_POS) & 0xFF))<<SEED_POS;
64  }
65 
66  iSqrt[SQRT_TABLE_SIZE / 2] = ((dword)(0xFF))<<(SEED_POS);
67 
68  initialized = true;
69 }
70 
71 /*
72 ================
73 idMath::FloatToBits
74 ================
75 */
76 int idMath::FloatToBits( float f, int exponentBits, int mantissaBits ) {
77  int i, sign, exponent, mantissa, value;
78 
79  assert( exponentBits >= 2 && exponentBits <= 8 );
80  assert( mantissaBits >= 2 && mantissaBits <= 23 );
81 
82  int maxBits = ( ( ( 1 << ( exponentBits - 1 ) ) - 1 ) << mantissaBits ) | ( ( 1 << mantissaBits ) - 1 );
83  int minBits = ( ( ( 1 << exponentBits ) - 2 ) << mantissaBits ) | 1;
84 
85  float max = BitsToFloat( maxBits, exponentBits, mantissaBits );
86  float min = BitsToFloat( minBits, exponentBits, mantissaBits );
87 
88  if ( f >= 0.0f ) {
89  if ( f >= max ) {
90  return maxBits;
91  } else if ( f <= min ) {
92  return minBits;
93  }
94  } else {
95  if ( f <= -max ) {
96  return ( maxBits | ( 1 << ( exponentBits + mantissaBits ) ) );
97  } else if ( f >= -min ) {
98  return ( minBits | ( 1 << ( exponentBits + mantissaBits ) ) );
99  }
100  }
101 
102  exponentBits--;
103  i = *reinterpret_cast<int *>(&f);
104  sign = ( i >> IEEE_FLT_SIGN_BIT ) & 1;
105  exponent = ( ( i >> IEEE_FLT_MANTISSA_BITS ) & ( ( 1 << IEEE_FLT_EXPONENT_BITS ) - 1 ) ) - IEEE_FLT_EXPONENT_BIAS;
106  mantissa = i & ( ( 1 << IEEE_FLT_MANTISSA_BITS ) - 1 );
107  value = sign << ( 1 + exponentBits + mantissaBits );
108  value |= ( ( INTSIGNBITSET( exponent ) << exponentBits ) | ( abs( exponent ) & ( ( 1 << exponentBits ) - 1 ) ) ) << mantissaBits;
109  value |= mantissa >> ( IEEE_FLT_MANTISSA_BITS - mantissaBits );
110  return value;
111 }
112 
113 /*
114 ================
115 idMath::BitsToFloat
116 ================
117 */
118 float idMath::BitsToFloat( int i, int exponentBits, int mantissaBits ) {
119  static int exponentSign[2] = { 1, -1 };
120  int sign, exponent, mantissa, value;
121 
122  assert( exponentBits >= 2 && exponentBits <= 8 );
123  assert( mantissaBits >= 2 && mantissaBits <= 23 );
124 
125  exponentBits--;
126  sign = i >> ( 1 + exponentBits + mantissaBits );
127  exponent = ( ( i >> mantissaBits ) & ( ( 1 << exponentBits ) - 1 ) ) * exponentSign[( i >> ( exponentBits + mantissaBits ) ) & 1];
128  mantissa = ( i & ( ( 1 << mantissaBits ) - 1 ) ) << ( IEEE_FLT_MANTISSA_BITS - mantissaBits );
129  value = sign << IEEE_FLT_SIGN_BIT | ( exponent + IEEE_FLT_EXPONENT_BIAS ) << IEEE_FLT_MANTISSA_BITS | mantissa;
130  return *reinterpret_cast<float *>(&value);
131 }
unsigned int dword
Definition: Lib.h:77
static const float INFINITY
Definition: Math.h:218
#define min(a, b)
GLsizei const GLfloat * value
Definition: glext.h:3614
static const float SQRT_TWO
Definition: Math.h:210
assert(prefInfo.fullscreenBtn)
#define IEEE_FLT_EXPONENT_BIAS
Definition: Math.h:82
static const float SQRT_THREE
Definition: Math.h:211
static const float ONEFOURTH_PI
Definition: Math.h:208
static const float PI
Definition: Math.h:205
static const float FLT_EPSILON
Definition: Math.h:219
#define IEEE_FLT_SIGN_BIT
Definition: Math.h:83
case const float
Definition: Callbacks.cpp:62
static bool initialized
Definition: Math.h:238
static const float M_DEG2RAD
Definition: Math.h:214
static const float HALF_PI
Definition: Math.h:207
int i
Definition: process.py:33
static const float M_SEC2MS
Definition: Math.h:216
static float BitsToFloat(int i, int exponentBits, int mantissaBits)
Definition: Math.cpp:118
#define PI
static const float M_RAD2DEG
Definition: Math.h:215
dword i
Definition: Math.h:233
static const float SQRT_1OVER2
Definition: Math.h:212
static const float E
Definition: Math.h:209
static const float TWO_PI
Definition: Math.h:206
#define IEEE_FLT_EXPONENT_BITS
Definition: Math.h:81
#define INTSIGNBITSET(i)
Definition: Math.h:71
tuple f
Definition: idal.py:89
#define IEEE_FLT_MANTISSA_BITS
Definition: Math.h:80
static dword iSqrt[SQRT_TABLE_SIZE]
Definition: Math.h:237
#define max(x, y)
Definition: os.h:70
float f
Definition: Math.h:234
static const float SQRT_1OVER3
Definition: Math.h:213
static const float M_MS2SEC
Definition: Math.h:217
static void Init(void)
Definition: Math.cpp:57
static int FloatToBits(float f, int exponentBits, int mantissaBits)
Definition: Math.cpp:76