]> git.lizzy.rs Git - irrlicht.git/blobdiff - include/irrMath.h
Fix COSOperator::getSystemMemory
[irrlicht.git] / include / irrMath.h
index 94eda1747c4781a1299b89234e8cfa06dc92c9b1..994f8cd200b1ece5d74113a4a9cd452006e374d8 100644 (file)
 #include <stdlib.h> // for abs() etc.\r
 #include <limits.h> // For INT_MAX / UINT_MAX\r
 \r
-#if defined(_IRR_SOLARIS_PLATFORM_) || defined(__BORLANDC__) || defined (__BCPLUSPLUS__) || defined (_WIN32_WCE)\r
-       #define sqrtf(X) (irr::f32)sqrt((irr::f64)(X))\r
-       #define sinf(X) (irr::f32)sin((irr::f64)(X))\r
-       #define cosf(X) (irr::f32)cos((irr::f64)(X))\r
-       #define asinf(X) (irr::f32)asin((irr::f64)(X))\r
-       #define acosf(X) (irr::f32)acos((irr::f64)(X))\r
-       #define atan2f(X,Y) (irr::f32)atan2((irr::f64)(X),(irr::f64)(Y))\r
-       #define ceilf(X) (irr::f32)ceil((irr::f64)(X))\r
-       #define floorf(X) (irr::f32)floor((irr::f64)(X))\r
-       #define powf(X,Y) (irr::f32)pow((irr::f64)(X),(irr::f64)(Y))\r
-       #define fmodf(X,Y) (irr::f32)fmod((irr::f64)(X),(irr::f64)(Y))\r
-       #define fabsf(X) (irr::f32)fabs((irr::f64)(X))\r
-       #define logf(X) (irr::f32)log((irr::f64)(X))\r
-#endif\r
-\r
-#ifndef FLT_MAX\r
-#define FLT_MAX 3.402823466E+38F\r
-#endif\r
-\r
-#ifndef FLT_MIN\r
-#define FLT_MIN 1.17549435e-38F\r
-#endif\r
-\r
 namespace irr\r
 {\r
 namespace core\r
@@ -343,19 +320,17 @@ namespace core
 \r
        inline s32 s32_min(s32 a, s32 b)\r
        {\r
-               const s32 mask = (a - b) >> 31;\r
-               return (a & mask) | (b & ~mask);\r
+               return min_(a, b);\r
        }\r
 \r
        inline s32 s32_max(s32 a, s32 b)\r
        {\r
-               const s32 mask = (a - b) >> 31;\r
-               return (b & mask) | (a & ~mask);\r
+               return max_(a, b);\r
        }\r
 \r
        inline s32 s32_clamp (s32 value, s32 low, s32 high)\r
        {\r
-               return s32_min(s32_max(value,low), high);\r
+               return clamp(value, low, high);\r
        }\r
 \r
        /*\r
@@ -379,45 +354,14 @@ namespace core
 \r
        #define F32_VALUE_0             0x00000000\r
        #define F32_VALUE_1             0x3f800000\r
-       #define F32_SIGN_BIT            0x80000000U\r
-       #define F32_EXPON_MANTISSA      0x7FFFFFFFU\r
 \r
        //! code is taken from IceFPU\r
        //! Integer representation of a floating-point value.\r
-#ifdef IRRLICHT_FAST_MATH\r
-       #define IR(x)                   ((u32&)(x))\r
-#else\r
        inline u32 IR(f32 x) {inttofloat tmp; tmp.f=x; return tmp.u;}\r
-#endif\r
-\r
-       //! Absolute integer representation of a floating-point value\r
-       #define AIR(x)                  (IR(x)&0x7fffffff)\r
 \r
        //! Floating-point representation of an integer value.\r
-#ifdef IRRLICHT_FAST_MATH\r
-       #define FR(x)                   ((f32&)(x))\r
-#else\r
        inline f32 FR(u32 x) {inttofloat tmp; tmp.u=x; return tmp.f;}\r
        inline f32 FR(s32 x) {inttofloat tmp; tmp.s=x; return tmp.f;}\r
-#endif\r
-\r
-       //! integer representation of 1.0\r
-       #define IEEE_1_0                0x3f800000\r
-       //! integer representation of 255.0\r
-       #define IEEE_255_0              0x437f0000\r
-\r
-#ifdef IRRLICHT_FAST_MATH\r
-       #define F32_LOWER_0(f)          (F32_AS_U32(f) >  F32_SIGN_BIT)\r
-       #define F32_LOWER_EQUAL_0(f)    (F32_AS_S32(f) <= F32_VALUE_0)\r
-       #define F32_GREATER_0(f)        (F32_AS_S32(f) >  F32_VALUE_0)\r
-       #define F32_GREATER_EQUAL_0(f)  (F32_AS_U32(f) <= F32_SIGN_BIT)\r
-       #define F32_EQUAL_1(f)          (F32_AS_U32(f) == F32_VALUE_1)\r
-       #define F32_EQUAL_0(f)          ( (F32_AS_U32(f) & F32_EXPON_MANTISSA ) == F32_VALUE_0)\r
-\r
-       // only same sign\r
-       #define F32_A_GREATER_B(a,b)    (F32_AS_S32((a)) > F32_AS_S32((b)))\r
-\r
-#else\r
 \r
        #define F32_LOWER_0(n)          ((n) <  0.0f)\r
        #define F32_LOWER_EQUAL_0(n)    ((n) <= 0.0f)\r
@@ -426,8 +370,6 @@ namespace core
        #define F32_EQUAL_1(n)          ((n) == 1.0f)\r
        #define F32_EQUAL_0(n)          ((n) == 0.0f)\r
        #define F32_A_GREATER_B(a,b)    ((a) > (b))\r
-#endif\r
-\r
 \r
 #ifndef REALINLINE\r
        #ifdef _MSC_VER\r
@@ -437,51 +379,6 @@ namespace core
        #endif\r
 #endif\r
 \r
-#if defined(__BORLANDC__) || defined (__BCPLUSPLUS__)\r
-\r
-       // 8-bit bools in Borland builder\r
-\r
-       //! conditional set based on mask and arithmetic shift\r
-       REALINLINE u32 if_c_a_else_b ( const c8 condition, const u32 a, const u32 b )\r
-       {\r
-               return ( ( -condition >> 7 ) & ( a ^ b ) ) ^ b;\r
-       }\r
-\r
-       //! conditional set based on mask and arithmetic shift\r
-       REALINLINE u32 if_c_a_else_0 ( const c8 condition, const u32 a )\r
-       {\r
-               return ( -condition >> 31 ) & a;\r
-       }\r
-#else\r
-\r
-       //! conditional set based on mask and arithmetic shift\r
-       REALINLINE u32 if_c_a_else_b ( const s32 condition, const u32 a, const u32 b )\r
-       {\r
-               return ( ( -condition >> 31 ) & ( a ^ b ) ) ^ b;\r
-       }\r
-\r
-       //! conditional set based on mask and arithmetic shift\r
-       REALINLINE u16 if_c_a_else_b ( const s16 condition, const u16 a, const u16 b )\r
-       {\r
-               return ( ( -condition >> 15 ) & ( a ^ b ) ) ^ b;\r
-       }\r
-\r
-       //! conditional set based on mask and arithmetic shift\r
-       REALINLINE u32 if_c_a_else_0 ( const s32 condition, const u32 a )\r
-       {\r
-               return ( -condition >> 31 ) & a;\r
-       }\r
-#endif\r
-\r
-       /*\r
-               if (condition) state |= m; else state &= ~m;\r
-       */\r
-       REALINLINE void setbit_cond ( u32 &state, s32 condition, u32 mask )\r
-       {\r
-               // 0, or any positive to mask\r
-               //s32 conmask = -condition >> 31;\r
-               state ^= ( ( -condition >> 31 ) ^ state ) & mask;\r
-       }\r
 \r
        // NOTE: This is not as exact as the c99/c++11 round function, especially at high numbers starting with 8388609\r
        //       (only low number which seems to go wrong is 0.49999997 which is rounded to 1)\r
@@ -526,30 +423,7 @@ namespace core
        // calculate: 1 / sqrtf ( x )\r
        REALINLINE f32 reciprocal_squareroot(const f32 f)\r
        {\r
-#if defined ( IRRLICHT_FAST_MATH )\r
-               // NOTE: Unlike comment below says I found inaccuracies already at 4'th significant bit.\r
-               // p.E: Input 1, expected 1, got 0.999755859\r
-\r
-       #if defined(_MSC_VER) && !defined(_WIN64)\r
-               // SSE reciprocal square root estimate, accurate to 12 significant\r
-               // bits of the mantissa\r
-               f32 recsqrt;\r
-               __asm rsqrtss xmm0, f           // xmm0 = rsqrtss(f)\r
-               __asm movss recsqrt, xmm0       // return xmm0\r
-               return recsqrt;\r
-\r
-/*\r
-               // comes from Nvidia\r
-               u32 tmp = (u32(IEEE_1_0 << 1) + IEEE_1_0 - *(u32*)&x) >> 1;\r
-               f32 y = *(f32*)&tmp;\r
-               return y * (1.47f - 0.47f * x * y * y);\r
-*/\r
-       #else\r
                return 1.f / sqrtf(f);\r
-       #endif\r
-#else // no fast math\r
-               return 1.f / sqrtf(f);\r
-#endif\r
        }\r
 \r
        // calculate: 1 / sqrtf( x )\r
@@ -561,37 +435,7 @@ namespace core
        // calculate: 1 / x\r
        REALINLINE f32 reciprocal( const f32 f )\r
        {\r
-#if defined (IRRLICHT_FAST_MATH)\r
-               // NOTE: Unlike with 1.f / f the values very close to 0 return -nan instead of inf\r
-\r
-               // SSE Newton-Raphson reciprocal estimate, accurate to 23 significant\r
-               // bi ts of the mantissa\r
-               // One Newton-Raphson Iteration:\r
-               // f(i+1) = 2 * rcpss(f) - f * rcpss(f) * rcpss(f)\r
-#if defined(_MSC_VER) && !defined(_WIN64)\r
-               f32 rec;\r
-               __asm rcpss xmm0, f               // xmm0 = rcpss(f)\r
-               __asm movss xmm1, f               // xmm1 = f\r
-               __asm mulss xmm1, xmm0            // xmm1 = f * rcpss(f)\r
-               __asm mulss xmm1, xmm0            // xmm2 = f * rcpss(f) * rcpss(f)\r
-               __asm addss xmm0, xmm0            // xmm0 = 2 * rcpss(f)\r
-               __asm subss xmm0, xmm1            // xmm0 = 2 * rcpss(f)\r
-                                                                                 //        - f * rcpss(f) * rcpss(f)\r
-               __asm movss rec, xmm0             // return xmm0\r
-               return rec;\r
-#else // no support yet for other compilers\r
                return 1.f / f;\r
-#endif\r
-               //! i do not divide through 0.. (fpu expection)\r
-               // instead set f to a high value to get a return value near zero..\r
-               // -1000000000000.f.. is use minus to stay negative..\r
-               // must test's here (plane.normal dot anything ) checks on <= 0.f\r
-               //u32 x = (-(AIR(f) != 0 ) >> 31 ) & ( IR(f) ^ 0xd368d4a5 ) ^ 0xd368d4a5;\r
-               //return 1.f / FR ( x );\r
-\r
-#else // no fast math\r
-               return 1.f / f;\r
-#endif\r
        }\r
 \r
        // calculate: 1 / x\r
@@ -604,45 +448,9 @@ namespace core
        // calculate: 1 / x, low precision allowed\r
        REALINLINE f32 reciprocal_approxim ( const f32 f )\r
        {\r
-#if defined( IRRLICHT_FAST_MATH)\r
-\r
-               // SSE Newton-Raphson reciprocal estimate, accurate to 23 significant\r
-               // bi ts of the mantissa\r
-               // One Newton-Raphson Iteration:\r
-               // f(i+1) = 2 * rcpss(f) - f * rcpss(f) * rcpss(f)\r
-#if defined(_MSC_VER) && !defined(_WIN64)\r
-               f32 rec;\r
-               __asm rcpss xmm0, f               // xmm0 = rcpss(f)\r
-               __asm movss xmm1, f               // xmm1 = f\r
-               __asm mulss xmm1, xmm0            // xmm1 = f * rcpss(f)\r
-               __asm mulss xmm1, xmm0            // xmm2 = f * rcpss(f) * rcpss(f)\r
-               __asm addss xmm0, xmm0            // xmm0 = 2 * rcpss(f)\r
-               __asm subss xmm0, xmm1            // xmm0 = 2 * rcpss(f)\r
-                                                                                 //        - f * rcpss(f) * rcpss(f)\r
-               __asm movss rec, xmm0             // return xmm0\r
-               return rec;\r
-#else // no support yet for other compilers\r
                return 1.f / f;\r
-#endif\r
-\r
-/*\r
-               // SSE reciprocal estimate, accurate to 12 significant bits of\r
-               f32 rec;\r
-               __asm rcpss xmm0, f             // xmm0 = rcpss(f)\r
-               __asm movss rec , xmm0          // return xmm0\r
-               return rec;\r
-*/\r
-/*\r
-               u32 x = 0x7F000000 - IR ( p );\r
-               const f32 r = FR ( x );\r
-               return r * (2.0f - p * r);\r
-*/\r
-#else // no fast math\r
-               return 1.f / f;\r
-#endif\r
        }\r
 \r
-\r
        REALINLINE s32 floor32(f32 x)\r
        {\r
                return (s32) floorf ( x );\r
@@ -677,9 +485,7 @@ namespace core
 } // end namespace core\r
 } // end namespace irr\r
 \r
-#ifndef IRRLICHT_FAST_MATH\r
-       using irr::core::IR;\r
-       using irr::core::FR;\r
-#endif\r
+using irr::core::IR;\r
+using irr::core::FR;\r
 \r
 #endif\r