5#ifndef DCPP_MATH_HPP_INCLUDED
6#define DCPP_MATH_HPP_INCLUDED
8#include <duckcpp/core/engine/DcppCompileConfig.hpp>
9#include <duckcpp/core/engine/dcpp_types.hpp>
12#if defined(_DCPP_SOLARIS_PLATFORM_) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__) || defined (_WIN32_WCE)
13 #define sqrtf(X) (dcpp::float32_kt)sqrt((dcpp::float64_kt)(X))
14 #define sinf(X) (dcpp::float32_kt)sin((dcpp::float64_kt)(X))
15 #define cosf(X) (dcpp::float32_kt)cos((dcpp::float64_kt)(X))
16 #define asinf(X) (dcpp::float32_kt)asin((dcpp::float64_kt)(X))
17 #define acosf(X) (dcpp::float32_kt)acos((dcpp::float64_kt)(X))
18 #define atan2f(X,Y) (dcpp::float32_kt)atan2((dcpp::float64_kt)(X),(dcpp::float64_kt)(Y))
19 #define ceilf(X) (dcpp::float32_kt)ceil((dcpp::float64_kt)(X))
20 #define floorf(X) (dcpp::float32_kt)floor((dcpp::float64_kt)(X))
21 #define powf(X,Y) (dcpp::float32_kt)pow((dcpp::float64_kt)(X),(dcpp::float64_kt)(Y))
22 #define fmodf(X,Y) (dcpp::float32_kt)fmod((dcpp::float64_kt)(X),(dcpp::float64_kt)(Y))
23 #define fabsf(X) (dcpp::float32_kt)fabs((dcpp::float64_kt)(X))
24 #define logf(X) (dcpp::float32_kt)log((dcpp::float64_kt)(X))
28#define FLT_MAX 3.402823466E+38F
32#define FLT_MIN 1.17549435e-38F
44#ifdef __DCPP_HAS_INT64
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();
188 return ROUNDING_ERROR_Float32;
194 return ROUNDING_ERROR_Float64;
209#ifdef __DCPP_HAS_INT64
211 inline dcpp::int64_kt roundingError()
213 return ROUNDING_ERROR_INT64;
217 inline dcpp::uint64_kt roundingError()
219 return ROUNDING_ERROR_INT64;
224 inline T relativeErrorFactor()
267 bool sign()
const {
return (i >> 31) != 0; }
287 if (
fa.sign() !=
fb.sign() )
333#ifdef __DCPP_HAS_INT64
335 inline bool iszero(
const dcpp::int64_kt a,
const dcpp::int64_kt tolerance = 0)
337 return abs_(a) <= tolerance;
344 return (a & mask) | (b & ~mask);
350 return (b & mask) | (a & ~mask);
355 return int32_min(int32_max(value,low), high);
373 #define FLOAT32_AS_INT32(f) (*((dcpp::int32_kt *) &(f)))
374 #define FLOAT32_AS_UINT32(f) (*((dcpp::uint32_kt *) &(f)))
375 #define FLOAT32_AS_UINT32_POINTER(f) ( ((dcpp::uint32_kt *) &(f)))
377 #define FLOAT32_VALUE_0 0x00000000
378 #define FLOAT32_VALUE_1 0x3f800000
379 #define FLOAT32_SIGN_BIT 0x80000000U
380 #define FLOAT32_EXPON_MANTISSA 0x7FFFFFFFU
384#ifdef DUCKCPP_FAST_MATH
385 #define IR(x) ((dcpp::uint32_kt&)(x))
391 #define AIR(x) (IR(x)&0x7fffffff)
394#ifdef DUCKCPP_FAST_MATH
395 #define FR(x) ((dcpp::float32_kt&)(x))
402 #define IEEE_1_0 0x3f800000
404 #define IEEE_255_0 0x437f0000
406#ifdef DUCKCPP_FAST_MATH
407 #define FLOAT32_LOWER_0(f) (FLOAT32_AS_UINT32(f) > FLOAT32_SIGN_BIT)
408 #define FLOAT32_LOWER_EQUAL_0(f) (FLOAT32_AS_INT32(f) <= FLOAT32_VALUE_0)
409 #define FLOAT32_GREATER_0(f) (FLOAT32_AS_INT32(f) > FLOAT32_VALUE_0)
410 #define FLOAT32_GREATER_EQUAL_0(f) (FLOAT32_AS_UINT32(f) <= FLOAT32_SIGN_BIT)
411 #define FLOAT32_EQUAL_1(f) (FLOAT32_AS_UINT32(f) == FLOAT32_VALUE_1)
412 #define FLOAT32_EQUAL_0(f) ( (FLOAT32_AS_UINT32(f) & FLOAT32_EXPON_MANTISSA ) == FLOAT32_VALUE_0)
415 #define FLOAT32_A_GREATER_B(a,b) (FLOAT32_AS_INT32((a)) > FLOAT32_AS_INT32((b)))
419 #define FLOAT32_LOWER_0(n) ((n) < 0.0f)
420 #define FLOAT32_LOWER_EQUAL_0(n) ((n) <= 0.0f)
421 #define FLOAT32_GREATER_0(n) ((n) > 0.0f)
422 #define FLOAT32_GREATER_EQUAL_0(n) ((n) >= 0.0f)
423 #define FLOAT32_EQUAL_1(n) ((n) == 1.0f)
424 #define FLOAT32_EQUAL_0(n) ((n) == 0.0f)
425 #define FLOAT32_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;
480 state ^= ( ( -condition >> 31 ) ^ state ) & mask;
488 return floorf( x + 0.5f );
509#ifdef __DCPP_HAS_INT64
511 REALINLINE dcpp::int64_kt squareroot(
const dcpp::int64_kt f)
513 return static_cast<dcpp::int64_kt
>(squareroot(
static_cast<dcpp::float64_kt>(f)));
520 return 1.0 / sqrt(x);
526#if defined ( DUCKCPP_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);
561#if defined (DUCKCPP_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
604#if defined( DUCKCPP_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
661 return a > b ? (a > c ? a : c) : (b > c ? b : c);
666 return a < b ? (a < c ? a : c) : (b < c ? b : c);
671 return x - floorf ( x );
677#ifndef DUCKCPP_FAST_MATH
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.hpp:22
const dcpp::float32_kt PI
Constant for PI.
Definition irrMath.hpp:54
const dcpp::float32_kt RECIPROCAL_PI
Constant for reciprocal of PI.
Definition irrMath.hpp:57
bool equalsRelative(const T a, const T b, const T factor=relativeErrorFactor< T >())
Definition irrMath.hpp:252
const dcpp::float32_kt HALF_PI
Constant for half of PI.
Definition irrMath.hpp:60
dcpp::uint32_kt IR(dcpp::float32_kt x)
Definition irrMath.hpp:387
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
T abs_(const T &a)
returns abs of two values. Own implementation to get rid of STL (VS6 problems)
Definition irrMath.hpp:149
const dcpp::float32_kt RADTODEG
32bit constant for converting from radians to degrees (formally known as GRAD_PI)
Definition irrMath.hpp:75
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
dcpp::float32_kt FR(dcpp::uint32_kt x)
Floating-point representation of an integer value.
Definition irrMath.hpp:397
dcpp::float32_kt radToDeg(dcpp::float32_kt radians)
Utility function to convert a radian value to degrees.
Definition irrMath.hpp:87
const dcpp::float64_kt RECIPROCAL_PI64
Constant for 64bit reciprocal of PI.
Definition irrMath.hpp:69
bool isnotzero(const dcpp::float32_kt a, const dcpp::float32_kt tolerance=ROUNDING_ERROR_Float32)
returns if a equals not zero, taking rounding errors into account
Definition irrMath.hpp:316
REALINLINE dcpp::uint32_kt if_c_a_else_b(const dcpp::int32_kt condition, const dcpp::uint32_kt a, const dcpp::uint32_kt b)
conditional set based on mask and arithmetic shift
Definition irrMath.hpp:455
const dcpp::float64_kt DEGTORAD64
64bit constant for converting from degrees to radians (formally known as GRAD_PI2)
Definition irrMath.hpp:78
void swap(T1 &a, T2 &b)
swaps the content of the passed parameters
Definition irrMath.hpp:175
dcpp::float32_kt degToRad(dcpp::float32_kt degrees)
Utility function to convert a degrees value to radians.
Definition irrMath.hpp:105
const dcpp::float32_kt DEGTORAD
32bit Constant for converting from degrees to radians
Definition irrMath.hpp:72
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 dcpp::int32_kt ROUNDING_ERROR_INT32
Rounding error constant often used when comparing dcpp::float32_kt values.
Definition irrMath.hpp:42
const T clamp(const T &value, const T &low, const T &high)
clamps a value between low and high
Definition irrMath.hpp:164
const dcpp::float64_kt PI64
Constant for 64bit PI.
Definition irrMath.hpp:66
const dcpp::float64_kt RADTODEG64
64bit constant for converting from radians to degrees
Definition irrMath.hpp:81
T lerp(const T &a, const T &b, const dcpp::float32_kt t)
Definition irrMath.hpp:157
bool iszero(const dcpp::float64_kt a, const dcpp::float64_kt tolerance=ROUNDING_ERROR_Float64)
returns if a equals zero, taking rounding errors into account
Definition irrMath.hpp:304
REALINLINE dcpp::uint32_kt if_c_a_else_0(const dcpp::int32_kt condition, const dcpp::uint32_kt a)
conditional set based on mask and arithmetic shift
Definition irrMath.hpp:467
bool equalsByUlp(dcpp::float32_kt a, dcpp::float32_kt 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
As of Duckcpp 1.6, position2d is a synonym for vector2d.
Definition shared_device.hpp:34
char char_kt
8 bit character variable.
Definition irrTypes.hpp:37
double float64_kt
64 bit floating point variable.
Definition irrTypes.hpp:112
unsigned int uint32_kt
32 bit unsigned variable.
Definition irrTypes.hpp:64
float float32_kt
32 bit floating point variable.
Definition irrTypes.hpp:108
unsigned short uint16_kt
16 bit unsigned variable.
Definition irrTypes.hpp:46
signed int int32_kt
32 bit signed variable.
Definition irrTypes.hpp:72
signed short int16_kt
16 bit signed variable.
Definition irrTypes.hpp:54
Definition irrMath.hpp:264