29 #ifndef __MATH_MATH_H__
30 #define __MATH_MATH_H__
35 #include <ppc_intrinsics.h>
56 #define DEG2RAD(a) ( (a) * idMath::M_DEG2RAD )
57 #define RAD2DEG(a) ( (a) * idMath::M_RAD2DEG )
59 #define SEC2MS(t) ( idMath::FtoiFast( (t) * idMath::M_SEC2MS ) )
60 #define MS2SEC(t) ( (t) * idMath::M_MS2SEC )
62 #define ANGLE2SHORT(x) ( idMath::FtoiFast( (x) * 65536.0f / 360.0f ) & 65535 )
63 #define SHORT2ANGLE(x) ( (x) * ( 360.0f / 65536.0f ) )
65 #define ANGLE2BYTE(x) ( idMath::FtoiFast( (x) * 256.0f / 360.0f ) & 255 )
66 #define BYTE2ANGLE(x) ( (x) * ( 360.0f / 256.0f ) )
68 #define FLOATSIGNBITSET(f) ((*(const unsigned long *)&(f)) >> 31)
69 #define FLOATSIGNBITNOTSET(f) ((~(*(const unsigned long *)&(f))) >> 31)
70 #define FLOATNOTZERO(f) ((*(const unsigned long *)&(f)) & ~(1<<31) )
71 #define INTSIGNBITSET(i) (((const unsigned long)(i)) >> 31)
72 #define INTSIGNBITNOTSET(i) ((~((const unsigned long)(i))) >> 31)
74 #define FLOAT_IS_NAN(x) (((*(const unsigned long *)&x) & 0x7f800000) == 0x7f800000)
75 #define FLOAT_IS_INF(x) (((*(const unsigned long *)&x) & 0x7fffffff) == 0x7f800000)
76 #define FLOAT_IS_IND(x) ((*(const unsigned long *)&x) == 0xffc00000)
77 #define FLOAT_IS_DENORMAL(x) (((*(const unsigned long *)&x) & 0x7f800000) == 0x00000000 && \
78 ((*(const unsigned long *)&x) & 0x007fffff) != 0x00000000 )
80 #define IEEE_FLT_MANTISSA_BITS 23
81 #define IEEE_FLT_EXPONENT_BITS 8
82 #define IEEE_FLT_EXPONENT_BIAS 127
83 #define IEEE_FLT_SIGN_BIT 31
85 #define IEEE_DBL_MANTISSA_BITS 52
86 #define IEEE_DBL_EXPONENT_BITS 11
87 #define IEEE_DBL_EXPONENT_BIAS 1023
88 #define IEEE_DBL_SIGN_BIT 63
90 #define IEEE_DBLE_MANTISSA_BITS 63
91 #define IEEE_DBLE_EXPONENT_BITS 15
92 #define IEEE_DBLE_EXPONENT_BIAS 0
93 #define IEEE_DBLE_SIGN_BIT 79
95 template<
class T> ID_INLINE
int MaxIndex(
T x,
T y ) {
return ( x > y ) ? 0 : 1; }
96 template<
class T> ID_INLINE
int MinIndex(
T x,
T y ) {
return ( x < y ) ? 0 : 1; }
98 template<
class T> ID_INLINE
T Max3(
T x,
T y,
T z ) {
return ( x > y ) ? ( ( x >
z ) ? x : z ) : ( ( y >
z ) ? y : z ); }
99 template<
class T> ID_INLINE
T Min3(
T x,
T y,
T z ) {
return ( x < y ) ? ( ( x <
z ) ? x : z ) : ( ( y <
z ) ? y : z ); }
100 template<
class T> ID_INLINE
int Max3Index(
T x,
T y,
T z ) {
return ( x > y ) ? ( ( x >
z ) ? 0 : 2 ) : ( ( y >
z ) ? 1 : 2 ); }
101 template<
class T> ID_INLINE
int Min3Index(
T x,
T y,
T z ) {
return ( x < y ) ? ( ( x <
z ) ? 0 : 2 ) : ( ( y <
z ) ? 1 : 2 ); }
103 template<
class T> ID_INLINE
T Sign(
T f ) {
return ( f > 0 ) ? 1 : ( ( f < 0 ) ? -1 : 0 ); }
104 template<
class T> ID_INLINE
T Square(
T x ) {
return x *
x; }
105 template<
class T> ID_INLINE
T Cube(
T x ) {
return x * x *
x; }
111 static void Init(
void );
113 static float RSqrt(
float x );
115 static float InvSqrt(
float x );
119 static float Sqrt(
float x );
120 static float Sqrt16(
float x );
121 static double Sqrt64(
float x );
123 static float Sin(
float a );
124 static float Sin16(
float a );
125 static double Sin64(
float a );
127 static float Cos(
float a );
128 static float Cos16(
float a );
129 static double Cos64(
float a );
131 static void SinCos(
float a,
float &
s,
float &
c );
132 static void SinCos16(
float a,
float &s,
float &c );
133 static void SinCos64(
float a,
double &s,
double &c );
135 static float Tan(
float a );
136 static float Tan16(
float a );
137 static double Tan64(
float a );
139 static float ASin(
float a );
140 static float ASin16(
float a );
141 static double ASin64(
float a );
143 static float ACos(
float a );
144 static float ACos16(
float a );
145 static double ACos64(
float a );
147 static float ATan(
float a );
148 static float ATan16(
float a );
149 static double ATan64(
float a );
151 static float ATan(
float y,
float x );
152 static float ATan16(
float y,
float x );
153 static double ATan64(
float y,
float x );
155 static float Pow(
float x,
float y );
156 static float Pow16(
float x,
float y );
157 static double Pow64(
float x,
float y );
159 static float Exp(
float f );
160 static float Exp16(
float f );
161 static double Exp64(
float f );
163 static float Log(
float f );
164 static float Log16(
float f );
165 static double Log64(
float f );
167 static int IPow(
int x,
int y );
168 static int ILog2(
float f );
169 static int ILog2(
int i );
181 static int Abs(
int x );
182 static float Fabs(
float f );
183 static float Floor(
float f );
184 static float Ceil(
float f );
185 static float Rint(
float f );
186 static int Ftoi(
float f );
188 static unsigned long Ftol(
float f );
189 static unsigned long FtolFast(
float );
194 static float ClampFloat(
float min,
float max,
float value );
198 static float AngleDelta(
float angle1,
float angle2 );
200 static int FloatToBits(
float f,
int exponentBits,
int mantissaBits );
201 static float BitsToFloat(
int i,
int exponentBits,
int mantissaBits );
203 static int FloatHash(
const float *array,
const int numFloats );
205 static const float PI;
209 static const float E;
247 i = *
reinterpret_cast<long *
>( &
x );
248 i = 0x5f3759df - ( i >> 1 );
249 r = *
reinterpret_cast<float *
>( &
i );
250 r = r * ( 1.5f - r * r *
y );
264 r = r * ( 1.5f - r * r *
y );
278 r = r * ( 1.5f - r * r *
y );
279 r = r * ( 1.5f - r * r *
y );
292 r = r * ( 1.5f - r * r *
y );
293 r = r * ( 1.5f - r * r *
y );
294 r = r * ( 1.5f - r * r *
y );
317 if ( ( a < 0.0f ) || ( a >=
TWO_PI ) ) {
335 a = ( ( a < 0.0f ) ? -
PI :
PI ) -
a;
339 return a * ( ( ( ( ( -2.39e-08f * s + 2.7526e-06
f ) * s - 1.98409e-04f ) * s + 8.3333315e-03
f ) * s - 1.666666664e-01f ) * s + 1.0f );
353 if ( ( a < 0.0f ) || ( a >=
TWO_PI ) ) {
376 a = ( ( a < 0.0f ) ? -
PI :
PI ) -
a;
383 return d * ( ( ( ( ( -2.605e-07f * s + 2.47609e-05
f ) * s - 1.3888397e-03f ) * s + 4.16666418e-02
f ) * s - 4.999999963e-01f ) * s + 1.0f );
432 a = ( ( a < 0.0f ) ? -
PI :
PI ) -
a;
439 s = a * ( ( ( ( ( -2.39e-08f * t + 2.7526e-06
f ) * t - 1.98409e-04f ) * t + 8.3333315e-03
f ) * t - 1.666666664e-01f ) * t + 1.0f );
440 c = d * ( ( ( ( ( -2.605e-07f * t + 2.47609e-05
f ) * t - 1.3888397e-03f ) * t + 4.16666418e-02
f ) * t - 4.999999963e-01f ) * t + 1.0f );
467 if ( ( a < 0.0f ) || ( a >=
PI ) ) {
468 a -= floorf( a /
PI ) *
PI;
497 s = a * ( ( ( ( ( ( 9.5168091e-03f * s + 2.900525e-03
f ) * s + 2.45650893e-02f ) * s + 5.33740603e-02
f ) * s + 1.333923995e-01f ) * s + 3.333314036e-01
f ) * s + 1.0f );
525 return ( ( ( -0.0187293f * a + 0.0742610f ) * a - 0.2121144f ) * a + 1.5707288f ) * sqrt( 1.0f - a ) -
HALF_PI;
530 return HALF_PI - ( ( ( -0.0187293f * a + 0.0742610f ) * a - 0.2121144f ) * a + 1.5707288f ) * sqrt( 1.0f - a );
560 return PI - ( ( ( -0.0187293f * a + 0.0742610f ) * a - 0.2121144f ) * a + 1.5707288f ) * sqrt( 1.0f - a );
565 return ( ( ( -0.0187293f * a + 0.0742610f ) * a - 0.2121144f ) * a + 1.5707288f ) * sqrt( 1.0f - a );
586 if ( fabs( a ) > 1.0f ) {
589 s = - ( ( ( ( ( ( ( ( ( 0.0028662257f * s - 0.0161657367f ) * s + 0.0429096138f ) * s - 0.0752896400f )
590 * s + 0.1065626393f ) * s - 0.1420889944f ) * s + 0.1999355085f ) * s - 0.3333314528f ) * s ) + 1.0f ) * a;
598 return ( ( ( ( ( ( ( ( ( 0.0028662257f * s - 0.0161657367f ) * s + 0.0429096138f ) * s - 0.0752896400f )
599 * s + 0.1065626393f ) * s - 0.1420889944f ) * s + 0.1999355085f ) * s - 0.3333314528f ) *
s ) + 1.0f ) *
a;
608 return atan2f( y, x );
614 if ( fabs( y ) > fabs( x ) ) {
617 s = - ( ( ( ( ( ( ( ( ( 0.0028662257f * s - 0.0161657367f ) * s + 0.0429096138f ) * s - 0.0752896400f )
618 * s + 0.1065626393f ) * s - 0.1420889944f ) * s + 0.1999355085f ) * s - 0.3333314528f ) * s ) + 1.0f ) * a;
627 return ( ( ( ( ( ( ( ( ( 0.0028662257f * s - 0.0161657367f ) * s + 0.0429096138f ) * s - 0.0752896400f )
628 * s + 0.1065626393f ) * s - 0.1420889944f ) * s + 0.1999355085f ) * s - 0.3333314528f ) *
s ) + 1.0f ) *
a;
633 return atan2( y, x );
653 int i,
s, e, m, exponent;
656 x = f * 1.44269504088896340f;
658 i = *
reinterpret_cast<int *
>(&
x);
670 y = *
reinterpret_cast<float *
>(&exponent);
674 y *= 1.4142135623730950488f;
677 p = x * ( 7.2152891511493f + x2 * 0.0576900723731f );
678 q = 20.8189237930062f +
x2;
679 x = y * ( q +
p ) / ( q - p );
695 i = *
reinterpret_cast<int *
>(&
f);
698 y = *
reinterpret_cast<float *
>(&
i);
699 y *= 1.4142135623730950488f;
700 y = ( y - 1.0f ) / ( y + 1.0f );
702 y = y * ( 2.000000000046727f + y2 * ( 0.666666635059382f + y2 * ( 0.4000059794795f + y2 * ( 0.28525381498f + y2 * 0.2376245609f ) ) ) );
703 y += 0.693147180559945f * ( (
float)exponent + 0.5f );
712 int r;
for( r = x; y > 1; y-- ) { r *=
x; }
return r;
720 return ILog2( (
float)i );
724 return ILog2( f ) + 1;
728 return ILog2( (
float)i ) + 1;
732 return ( (*reinterpret_cast<int *>(&f)) >> 31 );
755 return ( x & ( x - 1 ) ) == 0 && x > 0;
759 x -= ( ( x >> 1 ) & 0x55555555 );
760 x = ( ( ( x >> 2 ) & 0x33333333 ) + ( x & 0x33333333 ) );
761 x = ( ( ( x >> 4 ) + x ) & 0x0f0f0f0f );
763 return ( ( x + ( x >> 16 ) ) & 0x0000003f );
767 x = ( ( ( x >> 1 ) & 0x55555555 ) | ( ( x & 0x55555555 ) << 1 ) );
768 x = ( ( ( x >> 2 ) & 0x33333333 ) | ( ( x & 0x33333333 ) << 2 ) );
769 x = ( ( ( x >> 4 ) & 0x0f0f0f0f ) | ( ( x & 0x0f0f0f0f ) << 4 ) );
770 x = ( ( ( x >> 8 ) & 0x00ff00ff ) | ( ( x & 0x00ff00ff ) << 8 ) );
771 return ( ( x >> 16 ) | ( x << 16 ) );
776 return ( ( x ^ y ) - y );
780 int tmp = *
reinterpret_cast<int *
>( &
f );
782 return *
reinterpret_cast<float *
>( &tmp );
794 return floorf( f + 0.5f );
807 #elif 0 // round chop (C/C++ standard)
808 int i,
s, e, m, shift;
809 i = *
reinterpret_cast<int *
>(&
f);
814 return ( ( ( ( m >> -shift ) | ( m << shift ) ) & ~( e >> 31 ) ) ^
s ) - s;
818 __asm__ __volatile__ (
830 return (
unsigned long)
f;
840 #elif 0 // round chop (C/C++ standard)
841 int i,
s, e, m, shift;
842 i = *
reinterpret_cast<int *
>(&
f);
847 return ( ( ( ( m >> -shift ) | ( m << shift ) ) & ~( e >> 31 ) ) ^
s ) - s;
852 __asm__ __volatile__ (
859 return (
unsigned long)
f;
904 if ( ( angle >= 360.0f ) || ( angle < 0.0f ) ) {
905 angle -= floor( angle / 360.0f ) * 360.0f;
912 if ( angle > 180.0f ) {
926 ptr =
reinterpret_cast<const int *
>( array );
927 for ( i = 0; i < numFloats; i++ ) {
GLdouble GLdouble GLdouble GLdouble q
static float ATan16(float a)
static const float INFINITY
static float Log16(float f)
GLsizei const GLfloat * value
static double Sqrt64(float x)
static int BitReverse(int x)
static const float SQRT_TWO
assert(prefInfo.fullscreenBtn)
static float Log(float f)
static double Tan64(float a)
static void SinCos64(float a, double &s, double &c)
#define IEEE_FLT_EXPONENT_BIAS
static float Exp16(float f)
static double Exp64(float f)
static float Tan16(float a)
static float ACos16(float a)
static const float SQRT_THREE
static const float ONEFOURTH_PI
static const float FLT_EPSILON
static double ASin64(float a)
static int FloatHash(const float *array, const int numFloats)
static float ClampFloat(float min, float max, float value)
static float AngleDelta(float angle1, float angle2)
#define IEEE_FLT_SIGN_BIT
static float Sqrt(float x)
static const float M_DEG2RAD
static float InvSqrt16(float x)
static int FloorPowerOfTwo(int x)
static const float HALF_PI
static float Sqrt16(float x)
ID_INLINE int MinIndex(T x, T y)
static int ClampInt(int min, int max, int value)
static float Rint(float f)
static void SinCos(float a, float &s, float &c)
static double ATan64(float a)
static const float M_SEC2MS
static float BitsToFloat(int i, int exponentBits, int mantissaBits)
static float ASin(float a)
static float Cos16(float a)
static float Sin16(float a)
static double Pow64(float x, float y)
static const float M_RAD2DEG
static float Pow16(float x, float y)
static int BitCount(int x)
static double Log64(float f)
static int MaskForIntegerSign(int i)
#define FLOATSIGNBITSET(f)
static float ASin16(float a)
static int FtoiFast(float f)
static float ATan(float a)
static float Sin(float a)
static double ACos64(float a)
static float Fabs(float f)
static float Floor(float f)
ID_INLINE T Max3(T x, T y, T z)
static const float SQRT_1OVER2
static float AngleNormalize360(float angle)
static int CeilPowerOfTwo(int x)
static int MaskForFloatSign(float f)
static double Cos64(float a)
static float InvSqrt(float x)
static float Tan(float a)
GLubyte GLubyte GLubyte a
static float Exp(float f)
static void SinCos16(float a, float &s, float &c)
ID_INLINE T Min3(T x, T y, T z)
static int BitsForInteger(int i)
GLdouble GLdouble GLdouble y2
static float Ceil(float f)
static bool IsPowerOfTwo(int x)
static float Pow(float x, float y)
static const float TWO_PI
ID_INLINE int Min3Index(T x, T y, T z)
ID_INLINE int Max3Index(T x, T y, T z)
static double InvSqrt64(float x)
ID_INLINE int MaxIndex(T x, T y)
#define IEEE_FLT_EXPONENT_BITS
GLdouble GLdouble GLdouble r
static signed char ClampChar(int i)
static double Sin64(float a)
static unsigned long Ftol(float f)
#define IEEE_FLT_MANTISSA_BITS
static int IPow(int x, int y)
static float AngleNormalize180(float angle)
static int ILog2(float f)
static dword iSqrt[SQRT_TABLE_SIZE]
static unsigned long FtolFast(float)
static float ACos(float a)
static const float SQRT_1OVER3
static const float M_MS2SEC
static float RSqrt(float x)
static int FloatToBits(float f, int exponentBits, int mantissaBits)
static int BitsForFloat(float f)
static signed short ClampShort(int i)
static float Cos(float a)