Browse Source

Removing reliance on fmodf and fabsf CRT functions.

Branimir Karadžić 8 years ago
parent
commit
fddf8c3303
3 changed files with 24 additions and 10 deletions
  1. 10 0
      include/bx/inline/math.inl
  2. 0 10
      src/math.cpp
  3. 14 0
      tests/math_test.cpp

+ 10 - 0
include/bx/inline/math.inl

@@ -102,6 +102,11 @@ namespace bx
 		return _a + (_b - _a) * _t;
 		return _a + (_b - _a) * _t;
 	}
 	}
 
 
+	inline float fabs(float _a)
+	{
+		return _a < 0.0f ? -_a : _a;
+	}
+
 	inline float fsign(float _a)
 	inline float fsign(float _a)
 	{
 	{
 		return _a < 0.0f ? -1.0f : 1.0f;
 		return _a < 0.0f ? -1.0f : 1.0f;
@@ -132,6 +137,11 @@ namespace bx
 		return _a - ffloor(_a);
 		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)
 	inline bool fequal(float _a, float _b, float _epsilon)
 	{
 	{
 		// http://realtimecollisiondetection.net/blog/?p=89
 		// http://realtimecollisiondetection.net/blog/?p=89

+ 0 - 10
src/math.cpp

@@ -22,11 +22,6 @@ namespace bx
 	const float kHuge = HUGE_VALF;
 	const float kHuge = HUGE_VALF;
 #endif // BX_COMPILER_MSVC
 #endif // BX_COMPILER_MSVC
 
 
-	float fabs(float _a)
-	{
-		return ::fabsf(_a);
-	}
-
 	float fsin(float _a)
 	float fsin(float _a)
 	{
 	{
 		return ::sinf(_a);
 		return ::sinf(_a);
@@ -82,11 +77,6 @@ namespace bx
 		return ::ceilf(_f);
 		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)
 	void mtxLookAtImpl(float* _result, const float* _eye, const float* _view, const float* _up)
 	{
 	{
 		float up[3] = { 0.0f, 1.0f, 0.0f };
 		float up[3] = { 0.0f, 1.0f, 0.0f };

+ 14 - 0
tests/math_test.cpp

@@ -32,6 +32,20 @@ TEST_CASE("flog2", "")
 	flog2_test(256.0f);
 	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", "")
 TEST_CASE("ToBits", "")
 {
 {
 	REQUIRE(UINT32_C(0x12345678)         == bx::floatToBits( bx::bitsToFloat( UINT32_C(0x12345678) ) ) );
 	REQUIRE(UINT32_C(0x12345678)         == bx::floatToBits( bx::bitsToFloat( UINT32_C(0x12345678) ) ) );