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 52 #ifdef PI // make sure we don't collide with a define 55 const f32 PI = 3.14159265359f;
64 #ifdef PI64 // make sure we don't collide with a define 67 const f64 PI64 = 3.1415926535897932384626433832795028841971693993751;
91 return RADTODEG * radians;
100 return RADTODEG64 * radians;
109 return DEGTORAD * degrees;
118 return DEGTORAD64 * degrees;
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);
200 bool sign()
const {
return (
i >> 31) != 0; }
229 int ulpsDiff =
abs_(fa.
i- fb.
i);
230 if (ulpsDiff <= maxUlpDiff)
249 inline bool equals(
const s32 a,
const s32 b,
const s32 tolerance = ROUNDING_ERROR_S32)
252 return (a + tolerance >= b) && (a - tolerance <= b);
256 inline bool equals(
const u32 a,
const u32 b,
const s32 tolerance = ROUNDING_ERROR_S32)
258 return (a + tolerance >= b) && (a - tolerance <= b);
262 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;
300 inline bool iszero(
const s64 a,
const s64 tolerance = 0)
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 369 #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.5
f );
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);
523 #else // no fast math 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
562 #else // no fast math 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
607 #else // no fast math 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 );
640 #else // no fast math 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 );
673 #else // not fast math 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. 703 #else // no fast math 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 const f64 RADTODEG64
64bit constant for converting from radians to degrees
signed short s16
16 bit signed variable.
const f32 PI
Constant for PI.
const f64 PI64
Constant for 64bit PI.
REALINLINE s32 round32(f32 x)
bool iszero(const f64 a, const f64 tolerance=ROUNDING_ERROR_f64)
returns if a equals zero, taking rounding errors into account
f32 degToRad(f32 degrees)
Utility function to convert a degrees value to radians.
s32 s32_clamp(s32 value, s32 low, s32 high)
f32 f32_max3(const f32 a, const f32 b, const f32 c)
float f32
32 bit floating point variable.
REALINLINE void clearFPUException()
const f32 RADTODEG
32bit constant for converting from radians to degrees (formally known as GRAD_PI) ...
char c8
8 bit character variable.
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...
REALINLINE f32 squareroot(const f32 f)
const f32 HALF_PI
Constant for half of PI.
s32 s32_min(s32 a, s32 b)
s32 s32_max(s32 a, s32 b)
Everything in the Irrlicht Engine can be found in this namespace.
REALINLINE s32 ceil32(f32 x)
REALINLINE f32 reciprocal_approxim(const f32 f)
T lerp(const T &a, const T &b, const f32 t)
double f64
64 bit floating point variable.
long long s64
64 bit signed variable.
unsigned short u16
16 bit unsigned variable.
const f64 DEGTORAD64
64bit constant for converting from degrees to radians (formally known as GRAD_PI2) ...
const f32 ROUNDING_ERROR_f32
const f32 RECIPROCAL_PI
Constant for reciprocal of PI.
bool isnotzero(const f32 a, const f32 tolerance=ROUNDING_ERROR_f32)
returns if a equals not zero, taking rounding errors into account
const f64 ROUNDING_ERROR_f64
signed int s32
32 bit signed variable.
REALINLINE void setbit_cond(u32 &state, s32 condition, u32 mask)
REALINLINE f32 reciprocal(const f32 f)
unsigned int u32
32 bit unsigned variable.
const s64 ROUNDING_ERROR_S64
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 u32 if_c_a_else_b(const s32 condition, const u32 a, const u32 b)
conditional set based on mask and arithmetic shift
const f32 DEGTORAD
32bit Constant for converting from degrees to radians
f32 FR(u32 x)
Floating-point representation of an integer value.
void swap(T1 &a, T2 &b)
swaps the content of the passed parameters
const T & max_(const T &a, const T &b)
returns maximum of two values. Own implementation to get rid of the STL (VS6 problems) ...
FloatIntUnion32(float f1=0.0f)
f32 radToDeg(f32 radians)
Utility function to convert a radian value to degrees.
REALINLINE u32 if_c_a_else_0(const s32 condition, const u32 a)
conditional set based on mask and arithmetic shift
const f64 RECIPROCAL_PI64
Constant for 64bit reciprocal of PI.
REALINLINE s32 floor32(f32 x)
const s32 ROUNDING_ERROR_S32
Rounding error constant often used when comparing f32 values.
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
f32 f32_min3(const f32 a, const f32 b, const f32 c)
REALINLINE f64 reciprocal_squareroot(const f64 x)
const T clamp(const T &value, const T &low, const T &high)
clamps a value between low and high
T abs_(const T &a)
returns abs of two values. Own implementation to get rid of STL (VS6 problems)