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 53 #ifdef PI // make sure we don't collide with a define 56 const f32 PI = 3.14159265359f;
65 #ifdef PI64 // make sure we don't collide with a define 68 const f64 PI64 = 3.1415926535897932384626433832795028841971693993751;
124 inline const T&
min_(
const T&
a,
const T&
b)
126 return a <
b ?
a :
b;
131 inline const T&
min_(
const T&
a,
const T&
b,
const T&
c)
138 inline const T&
max_(
const T&
a,
const T&
b)
140 return a <
b ?
b :
a;
145 inline const T&
max_(
const T&
a,
const T&
b,
const T&
c)
154 return a < (T)0 ? -
a :
a;
162 return (T)(
a*(1.f-
t)) + (
b*
t);
167 inline const T
clamp (
const T&
value,
const T& low,
const T& high)
177 template <
class T1,
class T2>
246 inline bool equals(
const T
a,
const T
b,
const T tolerance = roundingError<T>())
248 return (
a + tolerance >=
b) && (
a - tolerance <=
b);
255 inline bool equalsRelative(
const T
a,
const T
b,
const T factor = relativeErrorFactor<T>())
259 const T maxi =
max_(
a,
b);
260 const T mini =
min_(
a,
b);
261 const T maxMagnitude =
max_( maxi, -mini);
263 return (maxMagnitude*factor + maxi) == (maxMagnitude*factor + mini);
270 bool sign()
const {
return (
i >> 31) != 0; }
299 const int ulpsDiff =
abs_(fa.
i- fb.
i);
300 if (ulpsDiff <= maxUlpDiff)
309 return fabs(
a) <= tolerance;
315 return fabsf(
a) <= tolerance;
321 return fabsf(
a) > tolerance;
327 return (
a & 0x7ffffff ) <= tolerance;
333 return a <= tolerance;
340 return abs_(
a) <= tolerance;
376 #define F32_AS_S32(f) (*((s32 *) &(f))) 377 #define F32_AS_U32(f) (*((u32 *) &(f))) 378 #define F32_AS_U32_POINTER(f) ( ((u32 *) &(f))) 380 #define F32_VALUE_0 0x00000000 381 #define F32_VALUE_1 0x3f800000 382 #define F32_SIGN_BIT 0x80000000U 383 #define F32_EXPON_MANTISSA 0x7FFFFFFFU 387 #ifdef IRRLICHT_FAST_MATH 388 #define IR(x) ((u32&)(x)) 394 #define AIR(x) (IR(x)&0x7fffffff) 397 #ifdef IRRLICHT_FAST_MATH 398 #define FR(x) ((f32&)(x)) 405 #define IEEE_1_0 0x3f800000 406 #define IEEE_255_0 0x437f0000 409 #ifdef IRRLICHT_FAST_MATH 410 #define F32_LOWER_0(f) (F32_AS_U32(f) > F32_SIGN_BIT) 411 #define F32_LOWER_EQUAL_0(f) (F32_AS_S32(f) <= F32_VALUE_0) 412 #define F32_GREATER_0(f) (F32_AS_S32(f) > F32_VALUE_0) 413 #define F32_GREATER_EQUAL_0(f) (F32_AS_U32(f) <= F32_SIGN_BIT) 414 #define F32_EQUAL_1(f) (F32_AS_U32(f) == F32_VALUE_1) 415 #define F32_EQUAL_0(f) ( (F32_AS_U32(f) & F32_EXPON_MANTISSA ) == F32_VALUE_0) 418 #define F32_A_GREATER_B(a,b) (F32_AS_S32((a)) > F32_AS_S32((b))) 422 #define F32_LOWER_0(n) ((n) < 0.0f) 423 #define F32_LOWER_EQUAL_0(n) ((n) <= 0.0f) 424 #define F32_GREATER_0(n) ((n) > 0.0f) 425 #define F32_GREATER_EQUAL_0(n) ((n) >= 0.0f) 426 #define F32_EQUAL_1(n) ((n) == 1.0f) 427 #define F32_EQUAL_0(n) ((n) == 0.0f) 428 #define F32_A_GREATER_B(a,b) ((a) > (b)) 434 #define REALINLINE __forceinline 436 #define REALINLINE inline 440 #if defined(__BORLANDC__) || defined (__BCPLUSPLUS__) 491 return floorf(
x + 0.5
f );
509 return static_cast<s32>(
squareroot(static_cast<f32>(
f)));
516 return static_cast<s64>(
squareroot(static_cast<f64>(
f)));
523 return 1.0 / sqrt(
x);
529 #if defined ( IRRLICHT_FAST_MATH ) 533 #if defined(_MSC_VER) && !defined(_WIN64) 537 __asm rsqrtss xmm0,
f 538 __asm movss recsqrt, xmm0
548 return 1.f / sqrtf(
f);
550 #else // no fast math 551 return 1.f / sqrtf(
f);
564 #if defined (IRRLICHT_FAST_MATH) 571 #if defined(_MSC_VER) && !defined(_WIN64) 575 __asm mulss xmm1, xmm0
576 __asm mulss xmm1, xmm0
577 __asm addss xmm0, xmm0
578 __asm subss xmm0, xmm1
580 __asm movss rec, xmm0
582 #else // no support yet for other compilers 592 #else // no fast math 607 #if defined( IRRLICHT_FAST_MATH) 613 #if defined(_MSC_VER) && !defined(_WIN64) 617 __asm mulss xmm1, xmm0
618 __asm mulss xmm1, xmm0
619 __asm addss xmm0, xmm0
620 __asm subss xmm0, xmm1
622 __asm movss rec, xmm0
624 #else // no support yet for other compilers 640 #else // no fast math 648 return (
s32) floorf (
x );
653 return (
s32) ceilf (
x );
664 return a >
b ? (
a >
c ?
a :
c) : (
b >
c ?
b :
c);
669 return a <
b ? (
a <
c ?
a :
c) : (
b <
c ?
b :
c);
674 return x - floorf (
x );
680 #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.
unsigned long long u64
64 bit unsigned variable.
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.
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)
bool equalsRelative(const T a, const T b, const T factor=relativeErrorFactor< T >())
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
bool equals(const T a, const T b, const T tolerance=roundingError< T >())
returns if a equals b, taking possible 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)
GLsizei const GLfloat * value
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)
GLboolean GLboolean GLboolean b
GLint GLint GLint GLint GLint x
FloatIntUnion32(float f1=0.0f)
f32 radToDeg(f32 radians)
Utility function to convert a radian value to degrees.
GLboolean GLboolean GLboolean GLboolean a
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.
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)