5 #ifndef __IRR_MATH_H_INCLUDED__
6 #define __IRR_MATH_H_INCLUDED__
15 #if defined(_IRR_SOLARIS_PLATFORM_) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__) || defined (_WIN32_WCE)
16 #define sqrtf(X) (irr::f32)sqrt((irr::f64)(X))
17 #define sinf(X) (irr::f32)sin((irr::f64)(X))
18 #define cosf(X) (irr::f32)cos((irr::f64)(X))
19 #define asinf(X) (irr::f32)asin((irr::f64)(X))
20 #define acosf(X) (irr::f32)acos((irr::f64)(X))
21 #define atan2f(X,Y) (irr::f32)atan2((irr::f64)(X),(irr::f64)(Y))
22 #define ceilf(X) (irr::f32)ceil((irr::f64)(X))
23 #define floorf(X) (irr::f32)floor((irr::f64)(X))
24 #define powf(X,Y) (irr::f32)pow((irr::f64)(X),(irr::f64)(Y))
25 #define fmodf(X,Y) (irr::f32)fmod((irr::f64)(X),(irr::f64)(Y))
26 #define fabsf(X) (irr::f32)fabs((irr::f64)(X))
27 #define logf(X) (irr::f32)log((irr::f64)(X))
31 #define FLT_MAX 3.402823466E+38F
35 #define FLT_MIN 1.17549435e-38F
68 const f64 PI64 = 3.1415926535897932384626433832795028841971693993751;
123 inline const T&
min_(
const T& a,
const T& b)
125 return a < b ? a : b;
130 inline const T&
min_(
const T& a,
const T& b,
const T& c)
132 return a < b ?
min_(a, c) :
min_(b, c);
137 inline const T&
max_(
const T& a,
const T& b)
139 return a < b ? b : a;
144 inline const T&
max_(
const T& a,
const T& b,
const T& c)
146 return a < b ?
max_(b, c) :
max_(a, c);
153 return a < (T)0 ? -a : a;
159 inline T
lerp(
const T& a,
const T& b,
const f32 t)
161 return (T)(a*(1.f-t)) + (b*t);
166 inline const T
clamp (
const T& value,
const T& low,
const T& high)
168 return min_ (
max_(value,low), high);
176 template <
class T1,
class T2>
187 return (a + tolerance >= b) && (a - tolerance <= b);
193 return (a + tolerance >= b) && (a - tolerance <= b);
200 bool sign()
const {
return (
i >> 31) != 0; }
229 int ulpsDiff =
abs_(fa.
i- fb.
i);
230 if (ulpsDiff <= maxUlpDiff)
252 return (a + tolerance >= b) && (a - tolerance <= b);
258 return (a + tolerance >= b) && (a - tolerance <= b);
265 return (a + tolerance >= b) && (a - tolerance <= b);
272 return fabs(a) <= tolerance;
278 return fabsf(a) <= tolerance;
284 return fabsf(a) > tolerance;
290 return ( a & 0x7ffffff ) <= tolerance;
296 return a <= tolerance;
303 return abs_(a) <= tolerance;
309 const s32 mask = (a - b) >> 31;
310 return (a & mask) | (b & ~mask);
315 const s32 mask = (a - b) >> 31;
316 return (b & mask) | (a & ~mask);
339 #define F32_AS_S32(f) (*((s32 *) &(f)))
340 #define F32_AS_U32(f) (*((u32 *) &(f)))
341 #define F32_AS_U32_POINTER(f) ( ((u32 *) &(f)))
343 #define F32_VALUE_0 0x00000000
344 #define F32_VALUE_1 0x3f800000
345 #define F32_SIGN_BIT 0x80000000U
346 #define F32_EXPON_MANTISSA 0x7FFFFFFFU
350 #ifdef IRRLICHT_FAST_MATH
351 #define IR(x) ((u32&)(x))
357 #define AIR(x) (IR(x)&0x7fffffff)
360 #ifdef IRRLICHT_FAST_MATH
361 #define FR(x) ((f32&)(x))
368 #define IEEE_1_0 0x3f800000
370 #define IEEE_255_0 0x437f0000
372 #ifdef IRRLICHT_FAST_MATH
373 #define F32_LOWER_0(f) (F32_AS_U32(f) > F32_SIGN_BIT)
374 #define F32_LOWER_EQUAL_0(f) (F32_AS_S32(f) <= F32_VALUE_0)
375 #define F32_GREATER_0(f) (F32_AS_S32(f) > F32_VALUE_0)
376 #define F32_GREATER_EQUAL_0(f) (F32_AS_U32(f) <= F32_SIGN_BIT)
377 #define F32_EQUAL_1(f) (F32_AS_U32(f) == F32_VALUE_1)
378 #define F32_EQUAL_0(f) ( (F32_AS_U32(f) & F32_EXPON_MANTISSA ) == F32_VALUE_0)
381 #define F32_A_GREATER_B(a,b) (F32_AS_S32((a)) > F32_AS_S32((b)))
385 #define F32_LOWER_0(n) ((n) < 0.0f)
386 #define F32_LOWER_EQUAL_0(n) ((n) <= 0.0f)
387 #define F32_GREATER_0(n) ((n) > 0.0f)
388 #define F32_GREATER_EQUAL_0(n) ((n) >= 0.0f)
389 #define F32_EQUAL_1(n) ((n) == 1.0f)
390 #define F32_EQUAL_0(n) ((n) == 0.0f)
391 #define F32_A_GREATER_B(a,b) ((a) > (b))
397 #define REALINLINE __forceinline
399 #define REALINLINE inline
403 #if defined(__BORLANDC__) || defined (__BCPLUSPLUS__)
410 return ( ( -condition >> 7 ) & ( a ^ b ) ) ^ b;
416 return ( -condition >> 31 ) & a;
423 return ( ( -condition >> 31 ) & ( a ^ b ) ) ^ b;
429 return ( ( -condition >> 15 ) & ( a ^ b ) ) ^ b;
435 return ( -condition >> 31 ) & a;
446 state ^= ( ( -condition >> 31 ) ^ state ) & mask;
451 return floorf( x + 0.5f );
456 #ifdef IRRLICHT_FAST_MATH
459 feclearexcept(FE_ALL_EXCEPT);
460 #elif defined(_MSC_VER)
462 #elif defined(__GNUC__) && defined(__x86__)
463 __asm__ __volatile__ (
"fclex \n\t");
465 # warn clearFPUException not supported.
499 return 1.0 / sqrt(x);
505 #if defined ( IRRLICHT_FAST_MATH )
506 #if defined(_MSC_VER)
510 __asm rsqrtss xmm0, f
511 __asm movss recsqrt, xmm0
521 return 1.f / sqrtf(f);
524 return 1.f / sqrtf(f);
537 #if defined (IRRLICHT_FAST_MATH)
546 __asm mulss xmm1, xmm0
547 __asm mulss xmm1, xmm0
548 __asm addss xmm0, xmm0
549 __asm subss xmm0, xmm1
551 __asm movss rec, xmm0
577 #if defined( IRRLICHT_FAST_MATH)
586 __asm mulss xmm1, xmm0
587 __asm mulss xmm1, xmm0
588 __asm addss xmm0, xmm0
589 __asm subss xmm0, xmm1
591 __asm movss rec, xmm0
615 #ifdef IRRLICHT_FAST_MATH
620 #if defined(_MSC_VER)
627 #elif defined(__GNUC__)
628 __asm__ __volatile__ (
636 # warn IRRLICHT_FAST_MATH not supported.
637 return (
s32) floorf ( x );
641 return (
s32) floorf ( x );
648 #ifdef IRRLICHT_FAST_MATH
653 #if defined(_MSC_VER)
660 #elif defined(__GNUC__)
661 __asm__ __volatile__ (
669 # warn IRRLICHT_FAST_MATH not supported.
670 return (
s32) ceilf ( x );
674 return (
s32) ceilf ( x );
682 #if defined(IRRLICHT_FAST_MATH)
685 #if defined(_MSC_VER)
691 #elif defined(__GNUC__)
692 __asm__ __volatile__ (
699 # warn IRRLICHT_FAST_MATH not supported.
710 return a > b ? (a > c ? a : c) : (b > c ? b : c);
715 return a < b ? (a < c ? a : c) : (b < c ? b : c);
720 return x - floorf ( x );
726 #ifndef IRRLICHT_FAST_MATH
REALINLINE s32 ceil32(f32 x)
const f64 PI64
Constant for 64bit PI.
const f32 PI
Constant for PI.
T abs_(const T &a)
returns abs of two values. Own implementation to get rid of STL (VS6 problems)
s32 s32_max(s32 a, s32 b)
const f32 DEGTORAD
32bit Constant for converting from degrees to radians
const T clamp(const T &value, const T &low, const T &high)
clamps a value between low and high
REALINLINE void setbit_cond(u32 &state, s32 condition, u32 mask)
bool isnotzero(const f32 a, const f32 tolerance=ROUNDING_ERROR_f32)
returns if a equals not zero, taking rounding errors into account
bool equalsByUlp(f32 a, f32 b, int maxUlpDiff)
We compare the difference in ULP's (spacing between floating-point numbers, aka ULP=1 means there exi...
const f64 ROUNDING_ERROR_f64
f32 f32_min3(const f32 a, const f32 b, const f32 c)
f32 radToDeg(f32 radians)
Utility function to convert a radian value to degrees.
const f64 DEGTORAD64
64bit constant for converting from degrees to radians (formally known as GRAD_PI2)
f32 f32_max3(const f32 a, const f32 b, const f32 c)
const f32 ROUNDING_ERROR_f32
const T & min_(const T &a, const T &b)
returns minimum of two values. Own implementation to get rid of the STL (VS6 problems)
REALINLINE f32 reciprocal(const f32 f)
s32 s32_min(s32 a, s32 b)
s32 s32_clamp(s32 value, s32 low, s32 high)
const s32 ROUNDING_ERROR_S32
Rounding error constant often used when comparing f32 values.
REALINLINE void clearFPUException()
void swap(T1 &a, T2 &b)
swaps the content of the passed parameters
REALINLINE f32 reciprocal_approxim(const f32 f)
const f32 HALF_PI
Constant for half of PI.
bool equals(const f64 a, const f64 b, const f64 tolerance=ROUNDING_ERROR_f64)
returns if a equals b, taking possible rounding errors into account
REALINLINE s32 floor32(f32 x)
const f32 RECIPROCAL_PI
Constant for reciprocal of PI.
const s64 ROUNDING_ERROR_S64
const f64 RECIPROCAL_PI64
Constant for 64bit reciprocal of PI.
f32 degToRad(f32 degrees)
Utility function to convert a degrees value to radians.
REALINLINE u32 if_c_a_else_b(const s32 condition, const u32 a, const u32 b)
conditional set based on mask and arithmetic shift
REALINLINE f64 reciprocal_squareroot(const f64 x)
f32 FR(u32 x)
Floating-point representation of an integer value.
T lerp(const T &a, const T &b, const f32 t)
const f32 RADTODEG
32bit constant for converting from radians to degrees (formally known as GRAD_PI)
REALINLINE s32 round32(f32 x)
const f64 RADTODEG64
64bit constant for converting from radians to degrees
REALINLINE u32 if_c_a_else_0(const s32 condition, const u32 a)
conditional set based on mask and arithmetic shift
REALINLINE f32 squareroot(const f32 f)
bool iszero(const f64 a, const f64 tolerance=ROUNDING_ERROR_f64)
returns if a equals zero, taking rounding errors into account
const T & max_(const T &a, const T &b)
returns maximum of two values. Own implementation to get rid of the STL (VS6 problems)
Everything in the Irrlicht Engine can be found in this namespace.
float f32
32 bit floating point variable.
unsigned int u32
32 bit unsigned variable.
double f64
64 bit floating point variable.
signed short s16
16 bit signed variable.
char c8
8 bit character variable.
long long s64
64 bit signed variable.
signed int s32
32 bit signed variable.
unsigned short u16
16 bit unsigned variable.
FloatIntUnion32(float f1=0.0f)