5#ifndef __IRR_MATH_H_INCLUDED__
6#define __IRR_MATH_H_INCLUDED__
8#include "IrrCompileConfig.h"
15#if defined(_IRR_SOLARIS_PLATFORM_) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__)
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
47 const s64 ROUNDING_ERROR_S64 = 0;
49 const f32 ROUNDING_ERROR_f32 = 0.000001f;
50 const f64 ROUNDING_ERROR_f64 = 0.00000001;
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>
185 inline bool equals(
const f64 a,
const f64 b,
const f64 tolerance = ROUNDING_ERROR_f64)
187 return (a + tolerance >= b) && (a - tolerance <= b);
191 inline bool equals(
const f32 a,
const f32 b,
const f32 tolerance = ROUNDING_ERROR_f32)
193 return (a + tolerance >= b) && (a - tolerance <= b);
196 union FloatIntUnion32
198 FloatIntUnion32(
float f1 = 0.0f) : f(f1) {}
200 bool sign()
const {
return (i >> 31) != 0; }
220 if ( fa.sign() != fb.sign() )
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);
263 inline bool equals(
const s64 a,
const s64 b,
const s64 tolerance = ROUNDING_ERROR_S64)
265 return (a + tolerance >= b) && (a - tolerance <= b);
270 inline bool iszero(
const f64 a,
const f64 tolerance = ROUNDING_ERROR_f64)
272 return fabs(a) <= tolerance;
276 inline bool iszero(
const f32 a,
const f32 tolerance = ROUNDING_ERROR_f32)
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);
321 return s32_min(s32_max(value,low), high);
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))
364 inline f32 FR(
s32 x) {inttofloat tmp; tmp.s=x;
return tmp.f;}
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;
442 REALINLINE
void setbit_cond (
u32 &state,
s32 condition,
u32 mask )
446 state ^= ( ( -condition >> 31 ) ^ state ) & mask;
449 inline f32 round_(
f32 x )
451 return floorf( x + 0.5f );
454 REALINLINE
void clearFPUException ()
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");
469 REALINLINE
f32 squareroot(
const f32 f)
475 REALINLINE
f64 squareroot(
const f64 f)
481 REALINLINE
s32 squareroot(
const s32 f)
483 return static_cast<s32>(squareroot(
static_cast<f32>(f)));
488 REALINLINE
s64 squareroot(
const s64 f)
490 return static_cast<s64>(squareroot(
static_cast<f64>(f)));
495 REALINLINE
f64 reciprocal_squareroot(
const f64 x)
497 return 1.0 / sqrt(x);
501 REALINLINE
f32 reciprocal_squareroot(
const f32 f)
503#if defined ( IRRLICHT_FAST_MATH )
504 #if defined(_MSC_VER)
508 __asm rsqrtss xmm0, f
509 __asm movss recsqrt, xmm0
519 return 1.f / sqrtf(f);
522 return 1.f / sqrtf(f);
527 REALINLINE
s32 reciprocal_squareroot(
const s32 x)
529 return static_cast<s32>(reciprocal_squareroot(
static_cast<f32>(x)));
533 REALINLINE
f32 reciprocal(
const f32 f )
535#if defined (IRRLICHT_FAST_MATH)
545 __asm mulss xmm1, xmm0
546 __asm mulss xmm1, xmm0
547 __asm addss xmm0, xmm0
548 __asm subss xmm0, xmm1
550 __asm movss rec, xmm0
568 REALINLINE
f64 reciprocal (
const f64 f )
575 REALINLINE
f32 reciprocal_approxim (
const f32 f )
577#if defined( IRRLICHT_FAST_MATH)
587 __asm mulss xmm1, xmm0
588 __asm mulss xmm1, xmm0
589 __asm addss xmm0, xmm0
590 __asm subss xmm0, xmm1
592 __asm movss rec, xmm0
616 REALINLINE
s32 floor32(
f32 x)
618#ifdef IRRLICHT_FAST_MATH
630#elif defined(__GNUC__)
631 __asm__ __volatile__ (
639 return (
s32) floorf ( x );
643 return (
s32) floorf ( x );
648 REALINLINE
s32 ceil32 (
f32 x )
650#ifdef IRRLICHT_FAST_MATH
662#elif defined(__GNUC__)
663 __asm__ __volatile__ (
671 return (
s32) ceilf ( x );
675 return (
s32) ceilf ( x );
681 REALINLINE
s32 round32(
f32 x)
683#if defined(IRRLICHT_FAST_MATH)
692#elif defined(__GNUC__)
693 __asm__ __volatile__ (
700 return (
s32) round_(x);
704 return (
s32) round_(x);
708 inline f32 f32_max3(
const f32 a,
const f32 b,
const f32 c)
710 return a > b ? (a > c ? a : c) : (b > c ? b : c);
713 inline f32 f32_min3(
const f32 a,
const f32 b,
const f32 c)
715 return a < b ? (a < c ? a : c) : (b < c ? b : c);
718 inline f32 fract (
f32 x )
720 return x - floorf ( x );
726#ifndef IRRLICHT_FAST_MATH
Basic classes such as vectors, planes, arrays, lists, and so on can be found in this namespace.
Definition aabbox3d.h:15
const f64 PI64
Constant for 64bit PI.
Definition irrMath.h:68
const f32 PI
Constant for PI.
Definition irrMath.h:56
T abs_(const T &a)
returns abs of two values. Own implementation to get rid of STL (VS6 problems)
Definition irrMath.h:151
const T & max_(const T &a, const T &b)
returns maximum of two values. Own implementation to get rid of the STL (VS6 problems)
Definition irrMath.h:137
const f32 DEGTORAD
32bit Constant for converting from degrees to radians
Definition irrMath.h:74
const T clamp(const T &value, const T &low, const T &high)
clamps a value between low and high
Definition irrMath.h:166
const T & min_(const T &a, const T &b)
returns minimum of two values. Own implementation to get rid of the STL (VS6 problems)
Definition irrMath.h:123
bool isnotzero(const f32 a, const f32 tolerance=ROUNDING_ERROR_f32)
returns if a equals not zero, taking rounding errors into account
Definition irrMath.h:282
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...
Definition irrMath.h:208
f32 radToDeg(f32 radians)
Utility function to convert a radian value to degrees.
Definition irrMath.h:89
const f64 DEGTORAD64
64bit constant for converting from degrees to radians (formally known as GRAD_PI2)
Definition irrMath.h:80
const s32 ROUNDING_ERROR_S32
Rounding error constant often used when comparing f32 values.
Definition irrMath.h:45
void swap(T1 &a, T2 &b)
swaps the content of the passed parameters
Definition irrMath.h:177
const f32 HALF_PI
Constant for half of PI.
Definition irrMath.h:62
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
Definition irrMath.h:185
const f32 RECIPROCAL_PI
Constant for reciprocal of PI.
Definition irrMath.h:59
const f64 RECIPROCAL_PI64
Constant for 64bit reciprocal of PI.
Definition irrMath.h:71
f32 degToRad(f32 degrees)
Utility function to convert a degrees value to radians.
Definition irrMath.h:107
REALINLINE u32 if_c_a_else_b(const s32 condition, const u32 a, const u32 b)
conditional set based on mask and arithmetic shift
Definition irrMath.h:421
f32 FR(u32 x)
Floating-point representation of an integer value.
Definition irrMath.h:363
T lerp(const T &a, const T &b, const f32 t)
Definition irrMath.h:159
const f32 RADTODEG
32bit constant for converting from radians to degrees (formally known as GRAD_PI)
Definition irrMath.h:77
const f64 RADTODEG64
64bit constant for converting from radians to degrees
Definition irrMath.h:83
REALINLINE u32 if_c_a_else_0(const s32 condition, const u32 a)
conditional set based on mask and arithmetic shift
Definition irrMath.h:433
bool iszero(const f64 a, const f64 tolerance=ROUNDING_ERROR_f64)
returns if a equals zero, taking rounding errors into account
Definition irrMath.h:270
u32 IR(f32 x)
Definition irrMath.h:353
Everything in the Irrlicht Engine can be found in this namespace.
Definition Skylicht.h:33
float f32
32 bit floating point variable.
Definition irrTypes.h:104
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.h:58
double f64
64 bit floating point variable.
Definition irrTypes.h:108
signed short s16
16 bit signed variable.
Definition irrTypes.h:48
char c8
8 bit character variable.
Definition irrTypes.h:31
long long s64
64 bit signed variable.
Definition irrTypes.h:96
signed int s32
32 bit signed variable.
Definition irrTypes.h:66
unsigned short u16
16 bit unsigned variable.
Definition irrTypes.h:40