From fddf8c330351edc4721a8b1dfeb015f8e73691b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Thu, 4 Jan 2018 08:59:17 -0800 Subject: [PATCH] Removing reliance on fmodf and fabsf CRT functions. --- include/bx/inline/math.inl | 10 ++++++++++ src/math.cpp | 10 ---------- tests/math_test.cpp | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/include/bx/inline/math.inl b/include/bx/inline/math.inl index 670a6d2..e1e93d4 100644 --- a/include/bx/inline/math.inl +++ b/include/bx/inline/math.inl @@ -102,6 +102,11 @@ namespace bx return _a + (_b - _a) * _t; } + inline float fabs(float _a) + { + return _a < 0.0f ? -_a : _a; + } + inline float fsign(float _a) { return _a < 0.0f ? -1.0f : 1.0f; @@ -132,6 +137,11 @@ namespace bx return _a - ffloor(_a); } + inline float fmod(float _a, float _b) + { + return _a - _b * ffloor(_a / _b); + } + inline bool fequal(float _a, float _b, float _epsilon) { // http://realtimecollisiondetection.net/blog/?p=89 diff --git a/src/math.cpp b/src/math.cpp index d9edce1..b5c89c9 100644 --- a/src/math.cpp +++ b/src/math.cpp @@ -22,11 +22,6 @@ namespace bx const float kHuge = HUGE_VALF; #endif // BX_COMPILER_MSVC - float fabs(float _a) - { - return ::fabsf(_a); - } - float fsin(float _a) { return ::sinf(_a); @@ -82,11 +77,6 @@ namespace bx return ::ceilf(_f); } - float fmod(float _a, float _b) - { - return ::fmodf(_a, _b); - } - void mtxLookAtImpl(float* _result, const float* _eye, const float* _view, const float* _up) { float up[3] = { 0.0f, 1.0f, 0.0f }; diff --git a/tests/math_test.cpp b/tests/math_test.cpp index 4c1163f..6c0cb53 100644 --- a/tests/math_test.cpp +++ b/tests/math_test.cpp @@ -32,6 +32,20 @@ TEST_CASE("flog2", "") flog2_test(256.0f); } +TEST_CASE("fmod", "") +{ + REQUIRE(389.0f == bx::fmod(1389.0f, 1000.0f) ); + REQUIRE(bx::isNan(bx::fmod(0.0f, 0.0f) ) ); +} + +TEST_CASE("fabs", "") +{ + REQUIRE(1389.0f == bx::fabs(-1389.0f) ); + REQUIRE(1389.0f == bx::fabs( 1389.0f) ); + REQUIRE( 0.0f == bx::fabs(-0.0f) ); + REQUIRE( 0.0f == bx::fabs( 0.0f) ); +} + TEST_CASE("ToBits", "") { REQUIRE(UINT32_C(0x12345678) == bx::floatToBits( bx::bitsToFloat( UINT32_C(0x12345678) ) ) );