5#ifndef NIRT_MATH_HPP_INCLUDED
6#define NIRT_MATH_HPP_INCLUDED
8#include <nirtcpp/core/engine/NirtCompileConfig.hpp>
9#include <nirtcpp/core/engine/nirt_types.hpp>
12#if defined(_NIRT_SOLARIS_PLATFORM_) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__) || defined (_WIN32_WCE)
13 #define sqrtf(X) (nirt::f32)sqrt((nirt::f64)(X))
14 #define sinf(X) (nirt::f32)sin((nirt::f64)(X))
15 #define cosf(X) (nirt::f32)cos((nirt::f64)(X))
16 #define asinf(X) (nirt::f32)asin((nirt::f64)(X))
17 #define acosf(X) (nirt::f32)acos((nirt::f64)(X))
18 #define atan2f(X,Y) (nirt::f32)atan2((nirt::f64)(X),(nirt::f64)(Y))
19 #define ceilf(X) (nirt::f32)ceil((nirt::f64)(X))
20 #define floorf(X) (nirt::f32)floor((nirt::f64)(X))
21 #define powf(X,Y) (nirt::f32)pow((nirt::f64)(X),(nirt::f64)(Y))
22 #define fmodf(X,Y) (nirt::f32)fmod((nirt::f64)(X),(nirt::f64)(Y))
23 #define fabsf(X) (nirt::f32)fabs((nirt::f64)(X))
24 #define logf(X) (nirt::f32)log((nirt::f64)(X))
28#define FLT_MAX 3.402823466E+38F
32#define FLT_MIN 1.17549435e-38F
47 const f32 ROUNDING_ERROR_f32 = 0.000001f;
48 const f64 ROUNDING_ERROR_f64 = 0.00000001;
66 const f64 PI64 = 3.1415926535897932384626433832795028841971693993751;
123 return a < b ? a : b;
128 inline const T&
min_(
const T& a,
const T& b,
const T& c)
130 return a < b ?
min_(a, c) :
min_(b, c);
137 return a < b ? b : a;
142 inline const T&
max_(
const T& a,
const T& b,
const T& c)
144 return a < b ?
max_(b, c) :
max_(a, c);
151 return a < (
T)0 ? -a : a;
159 return (
T)(a*(1.f-
t)) + (b*
t);
174 template <
class T1,
class T2>
183 inline T roundingError();
186 inline f32 roundingError()
188 return ROUNDING_ERROR_f32;
192 inline f64 roundingError()
194 return ROUNDING_ERROR_f64;
198 inline s32 roundingError()
204 inline u32 roundingError()
211 inline s64 roundingError()
213 return ROUNDING_ERROR_S64;
217 inline u64 roundingError()
219 return ROUNDING_ERROR_S64;
224 inline T relativeErrorFactor()
230 inline f32 relativeErrorFactor()
236 inline f64 relativeErrorFactor()
267 bool sign()
const {
return (i >> 31) != 0; }
287 if (
fa.sign() !=
fb.sign() )
335 inline bool iszero(
const s64 a,
const s64 tolerance = 0)
337 return abs_(a) <= tolerance;
343 const s32 mask = (a - b) >> 31;
344 return (a & mask) | (b & ~mask);
349 const s32 mask = (a - b) >> 31;
350 return (b & mask) | (a & ~mask);
355 return s32_min(s32_max(value,low), high);
371 using inttofloat =
union {
u32 u;
s32 s;
f32 f; };
373 #define F32_AS_S32(f) (*((s32 *) &(f)))
374 #define F32_AS_U32(f) (*((u32 *) &(f)))
375 #define F32_AS_U32_POINTER(f) ( ((u32 *) &(f)))
377 #define F32_VALUE_0 0x00000000
378 #define F32_VALUE_1 0x3f800000
379 #define F32_SIGN_BIT 0x80000000U
380 #define F32_EXPON_MANTISSA 0x7FFFFFFFU
384#ifdef NIRTCPP_FAST_MATH
385 #define IR(x) ((u32&)(x))
391 #define AIR(x) (IR(x)&0x7fffffff)
394#ifdef NIRTCPP_FAST_MATH
395 #define FR(x) ((f32&)(x))
398 inline f32 FR(
s32 x) {inttofloat tmp; tmp.s=x;
return tmp.f;}
402 #define IEEE_1_0 0x3f800000
404 #define IEEE_255_0 0x437f0000
406#ifdef NIRTCPP_FAST_MATH
407 #define F32_LOWER_0(f) (F32_AS_U32(f) > F32_SIGN_BIT)
408 #define F32_LOWER_EQUAL_0(f) (F32_AS_S32(f) <= F32_VALUE_0)
409 #define F32_GREATER_0(f) (F32_AS_S32(f) > F32_VALUE_0)
410 #define F32_GREATER_EQUAL_0(f) (F32_AS_U32(f) <= F32_SIGN_BIT)
411 #define F32_EQUAL_1(f) (F32_AS_U32(f) == F32_VALUE_1)
412 #define F32_EQUAL_0(f) ( (F32_AS_U32(f) & F32_EXPON_MANTISSA ) == F32_VALUE_0)
415 #define F32_A_GREATER_B(a,b) (F32_AS_S32((a)) > F32_AS_S32((b)))
419 #define F32_LOWER_0(n) ((n) < 0.0f)
420 #define F32_LOWER_EQUAL_0(n) ((n) <= 0.0f)
421 #define F32_GREATER_0(n) ((n) > 0.0f)
422 #define F32_GREATER_EQUAL_0(n) ((n) >= 0.0f)
423 #define F32_EQUAL_1(n) ((n) == 1.0f)
424 #define F32_EQUAL_0(n) ((n) == 0.0f)
425 #define F32_A_GREATER_B(a,b) ((a) > (b))
431 #define REALINLINE __forceinline
433 #define REALINLINE inline
437#if defined(__BORLANDC__) || defined (__BCPLUSPLUS__)
444 return ( ( -condition >> 7 ) & ( a ^ b ) ) ^ b;
450 return ( -condition >> 31 ) & a;
457 return ( ( -
condition >> 31 ) & ( a ^ b ) ) ^ b;
463 return ( ( -
condition >> 15 ) & ( a ^ b ) ) ^ b;
476 REALINLINE
void setbit_cond (
u32 &state,
s32 condition,
u32 mask )
480 state ^= ( ( -condition >> 31 ) ^ state ) & mask;
486 inline f32 round_(
f32 x )
488 return floorf( x + 0.5f );
492 REALINLINE
f32 squareroot(
const f32 f)
498 REALINLINE
f64 squareroot(
const f64 f)
504 REALINLINE
s32 squareroot(
const s32 f)
506 return static_cast<s32>(squareroot(
static_cast<f32>(f)));
511 REALINLINE s64 squareroot(
const s64 f)
513 return static_cast<s64
>(squareroot(
static_cast<f64>(f)));
518 REALINLINE
f64 reciprocal_squareroot(
const f64 x)
520 return 1.0 / sqrt(x);
524 REALINLINE
f32 reciprocal_squareroot(
const f32 f)
526#if defined ( NIRTCPP_FAST_MATH )
530 #if defined(_MSC_VER) && !defined(_WIN64)
534 __asm rsqrtss xmm0, f
535 __asm movss recsqrt, xmm0
545 return 1.f / sqrtf(f);
548 return 1.f / sqrtf(f);
553 REALINLINE
s32 reciprocal_squareroot(
const s32 x)
555 return static_cast<s32>(reciprocal_squareroot(
static_cast<f32>(x)));
559 REALINLINE
f32 reciprocal(
const f32 f )
561#if defined (NIRTCPP_FAST_MATH)
568#if defined(_MSC_VER) && !defined(_WIN64)
572 __asm mulss xmm1, xmm0
573 __asm mulss xmm1, xmm0
574 __asm addss xmm0, xmm0
575 __asm subss xmm0, xmm1
577 __asm movss rec, xmm0
595 REALINLINE
f64 reciprocal (
const f64 f )
602 REALINLINE
f32 reciprocal_approxim (
const f32 f )
604#if defined( NIRTCPP_FAST_MATH)
610#if defined(_MSC_VER) && !defined(_WIN64)
614 __asm mulss xmm1, xmm0
615 __asm mulss xmm1, xmm0
616 __asm addss xmm0, xmm0
617 __asm subss xmm0, xmm1
619 __asm movss rec, xmm0
643 REALINLINE
s32 floor32(
f32 x)
645 return (
s32) floorf ( x );
648 REALINLINE
s32 ceil32 (
f32 x )
650 return (
s32) ceilf ( x );
654 REALINLINE
s32 round32(
f32 x)
656 return (
s32) round_(x);
659 inline f32 f32_max3(
const f32 a,
const f32 b,
const f32 c)
661 return a > b ? (a > c ? a : c) : (b > c ? b : c);
664 inline f32 f32_min3(
const f32 a,
const f32 b,
const f32 c)
666 return a < b ? (a < c ? a : c) : (b < c ? b : c);
669 inline f32 fract (
f32 x )
671 return x - floorf ( x );
677#ifndef NIRTCPP_FAST_MATH
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.hpp:22
const f32 HALF_PI
Constant for half of PI.
Definition irrMath.hpp:60
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.hpp:135
const f64 RECIPROCAL_PI64
Constant for 64bit reciprocal of PI.
Definition irrMath.hpp:69
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.hpp:275
f32 degToRad(f32 degrees)
Utility function to convert a degrees value to radians.
Definition irrMath.hpp:105
f32 FR(u32 x)
Floating-point representation of an integer value.
Definition irrMath.hpp:397
const f32 RECIPROCAL_PI
Constant for reciprocal of PI.
Definition irrMath.hpp:57
REALINLINE u32 if_c_a_else_0(const s32 condition, const u32 a)
conditional set based on mask and arithmetic shift
Definition irrMath.hpp:467
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.hpp:455
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.hpp:121
const f32 PI
Constant for PI.
Definition irrMath.hpp:54
bool equalsRelative(const T a, const T b, const T factor=relativeErrorFactor< T >())
Definition irrMath.hpp:252
bool isnotzero(const f32 a, const f32 tolerance=ROUNDING_ERROR_f32)
returns if a equals not zero, taking rounding errors into account
Definition irrMath.hpp:316
f32 radToDeg(f32 radians)
Utility function to convert a radian value to degrees.
Definition irrMath.hpp:87
const f64 RADTODEG64
64bit constant for converting from radians to degrees
Definition irrMath.hpp:81
void swap(T1 &a, T2 &b)
swaps the content of the passed parameters
Definition irrMath.hpp:175
const s32 ROUNDING_ERROR_S32
Rounding error constant often used when comparing f32 values.
Definition irrMath.hpp:42
bool iszero(const f64 a, const f64 tolerance=ROUNDING_ERROR_f64)
returns if a equals zero, taking rounding errors into account
Definition irrMath.hpp:304
const f32 DEGTORAD
32bit Constant for converting from degrees to radians
Definition irrMath.hpp:72
T abs_(const T &a)
returns abs of two values. Own implementation to get rid of STL (VS6 problems)
Definition irrMath.hpp:149
const T clamp(const T &value, const T &low, const T &high)
clamps a value between low and high
Definition irrMath.hpp:164
T lerp(const T &a, const T &b, const f32 t)
Definition irrMath.hpp:157
u32 IR(f32 x)
Definition irrMath.hpp:387
const f64 DEGTORAD64
64bit constant for converting from degrees to radians (formally known as GRAD_PI2)
Definition irrMath.hpp:78
const f32 RADTODEG
32bit constant for converting from radians to degrees (formally known as GRAD_PI)
Definition irrMath.hpp:75
bool equals(const T a, const T b, const T tolerance=roundingError< T >())
returns if a equals b, taking possible rounding errors into account
Definition irrMath.hpp:243
const f64 PI64
Constant for 64bit PI.
Definition irrMath.hpp:66
As of Nirtcpp 1.6, position2d is a synonym for vector2d.
Definition vector3d.hpp:11
unsigned short u16
16 bit unsigned variable.
Definition irrTypes.hpp:46
signed int s32
32 bit signed variable.
Definition irrTypes.hpp:72
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.hpp:64
double f64
64 bit floating point variable.
Definition irrTypes.hpp:114
char c8
8 bit character variable.
Definition irrTypes.hpp:37
float f32
32 bit floating point variable.
Definition irrTypes.hpp:110
signed short s16
16 bit signed variable.
Definition irrTypes.hpp:54
Definition irrMath.hpp:264