Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fastmath.cpp File Reference
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

Go to the source code of this file.

Classes

union  FastSqrtUnion
 
struct  Vec3
 

Macros

#define FP_BITS(fp)   ( ( *(DWORD*) &(fp) ) )
 
#define FP_ABS_BITS(fp)   ( (FP_BITS( fp )&0x7FFFFFFF) )
 
#define FP_SIGN_BIT(fp)   ( (FP_BITS( fp )&0x80000000) )
 
#define FP_ONE_BITS   ( (0x3F800000) )
 
#define __forceinline   inline
 
#define FP_INV(r, p)
 
#define FP_INV2(r, p)
 
#define FP_EXP(e, p)
 
#define FP_NORM_TO_BYTE(i, p)
 

Typedefs

typedef int DWORD
 
typedef union FastSqrtUnion FastSqrtUnion
 

Functions

unsigned long FP_NORM_TO_BYTE2 (float p)
 
unsigned long FP_NORM_TO_BYTE3 (float p)
 
void build_sqrt_table ()
 
float fastsqrt (float n)
 
__forceinline void FloatToInt (int *int_pointer, float f)
 
int Stupodmain (int argc, char *argv[])
 
float CylTest_CapsFirst (const Vec3 &pt1, const Vec3 &pt2, float lengthsq, float radius_sq, const Vec3 &testpt)
 

Variables

float two = 2.0f
 
static unsigned int fast_sqrt_table [0x10000]
 

Macro Definition Documentation

#define __forceinline   inline

Definition at line 23 of file fastmath.cpp.

#define FP_ABS_BITS (   fp)    ( (FP_BITS( fp )&0x7FFFFFFF) )

Definition at line 20 of file fastmath.cpp.

#define FP_BITS (   fp)    ( ( *(DWORD*) &(fp) ) )

Definition at line 19 of file fastmath.cpp.

Referenced by fastsqrt().

#define FP_EXP (   e,
 
)
Value:
do { \
int _i; \
e = -1.44269504f*(float) 0x00800000*(p); \
_i = (int) e+0x3F800000; \
e = *(float*) &_i; \
} \
while (0)

Definition at line 59 of file fastmath.cpp.

#define FP_INV (   r,
 
)
Value:
do { \
int _i = 2*FP_ONE_BITS-*(int*) &(p); \
r = *(float*) &_i; \
r = r*(2.0f-(p)*r); \
} \
while (0)

Definition at line 27 of file fastmath.cpp.

Referenced by Stupodmain().

#define FP_INV2 (   r,
 
)
Value:
do { \
__asm {mov eax, 0x7F000000}; \
__asm {sub eax, dword ptr[p]}; \
__asm {mov dword ptr[r], eax}; \
__asm {fld dword ptr[p]}; \
__asm {fmul dword ptr[r]}; \
__asm {fsubr[two]}; \
__asm {fmul dword ptr[r]}; \
__asm {fstp dword ptr[r]}; \
} \
while (0)

Definition at line 44 of file fastmath.cpp.

#define FP_NORM_TO_BYTE (   i,
 
)
Value:
do { \
float _n = (p)+1.0f; \
i = *(int*) &_n; \
if (i >= 0x40000000) i = 0xFF; \
else if (i <= 0x3F800000) \
i = 0; \
else i = ( (i)>>15 )&0xFF; \
} \
while (0)

Definition at line 68 of file fastmath.cpp.

#define FP_ONE_BITS   ( (0x3F800000) )

Definition at line 22 of file fastmath.cpp.

#define FP_SIGN_BIT (   fp)    ( (FP_BITS( fp )&0x80000000) )

Definition at line 21 of file fastmath.cpp.

Typedef Documentation

typedef int DWORD

Definition at line 16 of file fastmath.cpp.

Function Documentation

void build_sqrt_table ( )

Definition at line 98 of file fastmath.cpp.

References FastSqrtUnion::f, fast_sqrt_table, float, FastSqrtUnion::i, i, and UniverseUtil::sqrt().

Referenced by Stupodmain().

99 {
100  unsigned int i;
101  FastSqrtUnion s;
102  for (i = 0; i <= 0x7FFF; i++) {
103  //Build a float with the bit pattern i as mantissa
104  //and an exponent of 0, stored as 127
105 
106  s.i = (i<<8)|(0x7F<<23);
107  s.f = (float) sqrt( s.f );
108 
109  //Take the square root then strip the first 7 bits of
110  //the mantissa into the table
111 
112  fast_sqrt_table[i+0x8000] = (s.i&0x7FFFFF);
113 
114  //Repeat the process, this time with an exponent of 1,
115  //stored as 128
116 
117  s.i = (i<<8)|(0x80<<23);
118  s.f = (float) sqrt( s.f );
119 
120  fast_sqrt_table[i] = (s.i&0x7FFFFF);
121  }
122 }
float CylTest_CapsFirst ( const Vec3 pt1,
const Vec3 pt2,
float  lengthsq,
float  radius_sq,
const Vec3 testpt 
)

Definition at line 197 of file fastmath.cpp.

References Vec3::x, Vec3::y, and Vec3::z.

198 {
199  float dx, dy, dz; //vector d from line segment point 1 to point 2
200  float pdx, pdy, pdz; //vector pd from point 1 to test point
201  float dot, dsq;
202  dx = pt2.x-pt1.x; //translate so pt1 is origin. Make vector from
203  dy = pt2.y-pt1.y; //pt1 to pt2. Need for this is easily eliminated
204  dz = pt2.z-pt1.z;
205  pdx = testpt.x-pt1.x; //vector from pt1 to test point.
206  pdy = testpt.y-pt1.y;
207  pdz = testpt.z-pt1.z;
208  //Dot the d and pd vectors to see if point lies behind the
209  //cylinder cap at pt1.x, pt1.y, pt1.z
210  dot = pdx*dx+pdy*dy+pdz*dz;
211  //If dot is less than zero the point is behind the pt1 cap.
212  //If greater than the cylinder axis line segment length squared
213  //then the point is outside the other end cap at pt2.
214  if (dot < 0.0f || dot > lengthsq) {
215  return -1.0f;
216  } else {
217  //Point lies within the parallel caps, so find
218  //distance squared from point to line, using the fact that sin^2 + cos^2 = 1
219  //the dot = cos() * |d||pd|, and cross*cross = sin^2 * |d|^2 * |pd|^2
220  //Carefull: '*' means mult for scalars and dotproduct for vectors
221  //In short, where dist is pt distance to cyl axis:
222  //dist = sin( pd to d ) * |pd|
223  //distsq = dsq = (1 - cos^2( pd to d)) * |pd|^2
224  //dsq = ( 1 - (pd * d)^2 / (|pd|^2 * |d|^2) ) * |pd|^2
225  //dsq = pd * pd - dot * dot / lengthsq
226  //where lengthsq is d*d or |d|^2 that is passed into this function
227  //distance squared to the cylinder axis:
228  dsq = (pdx*pdx+pdy*pdy+pdz*pdz)-dot*dot/lengthsq;
229  if (dsq > radius_sq)
230  return -1.0f;
231  else
232  return dsq; //return distance squared to axis
233  }
234 }
float fastsqrt ( float  n)
inline

Definition at line 124 of file fastmath.cpp.

References fast_sqrt_table, and FP_BITS.

Referenced by Stupodmain().

125 {
126  if (FP_BITS( n ) == 0)
127  return 0.0; //check for square root of 0
128 
129  FP_BITS( n ) = fast_sqrt_table[(FP_BITS( n )>>8)&0xFFFF]|( ( ( (FP_BITS( n )-0x3F800000)>>1 )+0x3F800000 )&0x7F800000 );
130 
131  return n;
132 }
__forceinline void FloatToInt ( int int_pointer,
float  f 
)

Definition at line 136 of file fastmath.cpp.

Referenced by Stupodmain().

137 {
138  *int_pointer = f;
139 }
unsigned long FP_NORM_TO_BYTE2 ( float  p)
inline

Definition at line 79 of file fastmath.cpp.

80 {
81  float fpTmp = p+1.0f;
82  return ( (*(unsigned*) &fpTmp)>>15 )&0xFF;
83 }
unsigned long FP_NORM_TO_BYTE3 ( float  p)
inline

Definition at line 85 of file fastmath.cpp.

86 {
87  float ftmp = p+12582912.0f;
88  return (*(unsigned long*) &ftmp)&0xFF;
89 }
int Stupodmain ( int  argc,
char *  argv[] 
)

Definition at line 141 of file fastmath.cpp.

References build_sqrt_table(), fastsqrt(), FloatToInt(), FP_INV, i, and UniverseUtil::sqrt().

142 {
143  float t, it, test_sqrt;
144  int i = 0;
146  t = 1234.121234f;
147 
148  test_sqrt = fastsqrt( t );
149  printf( "sqrt expected %20.10f approx %20.10f\n", sqrt( t ), test_sqrt );
150  FP_INV( it, t );
151  printf( "inv expected %20.10f approx %20.10f\n", 1/t, it );
152  i = 0xdeafbabe;
153  FloatToInt( &i, t );
154  printf( "ftol expected %d actual %d %08X\n", (int) t, i, i );
155  return 0;
156 }

Variable Documentation

unsigned int fast_sqrt_table[0x10000]
static

Definition at line 91 of file fastmath.cpp.

Referenced by build_sqrt_table(), and fastsqrt().

float two = 2.0f

Definition at line 42 of file fastmath.cpp.

Referenced by Unit::LoadRow().