Branimir Karadžić 8 jaren geleden
bovenliggende
commit
1ab2179a83
4 gewijzigde bestanden met toevoegingen van 25 en 11 verwijderingen
  1. 1 0
      include/bx/fpumath.h
  2. 5 0
      include/bx/inline/fpumath.inl
  3. 5 9
      src/fpumath.cpp
  4. 14 2
      tests/fpumath_test.cpp

+ 1 - 0
include/bx/fpumath.h

@@ -16,6 +16,7 @@ namespace bx
 	extern const float invPi;
 	extern const float piHalf;
 	extern const float sqrt2;
+	extern const float invLogNat2;
 	extern const float huge;
 
 	///

+ 5 - 0
include/bx/inline/fpumath.inl

@@ -136,6 +136,11 @@ namespace bx
 		return fpow(2.0f, _a);
 	}
 
+	inline float flog2(float _a)
+	{
+		return flog(_a) * invLogNat2;
+	}
+
 	inline float frsqrt(float _a)
 	{
 		return 1.0f/fsqrt(_a);

+ 5 - 9
src/fpumath.cpp

@@ -8,10 +8,11 @@
 
 namespace bx
 {
-	const float pi     = 3.14159265358979323846f;
-	const float invPi  = 1.0f/3.14159265358979323846f;
-	const float piHalf = 1.57079632679489661923f;
-	const float sqrt2  = 1.41421356237309504880f;
+	const float pi         = 3.1415926535897932384626433832795f;
+	const float invPi      = 1.0f/pi;
+	const float piHalf     = 1.5707963267948966192313216916398f;
+	const float sqrt2      = 1.4142135623730950488016887242097f;
+	const float invLogNat2 = 1.4426950408889634073599246810019f;
 #if BX_COMPILER_MSVC
 	const float huge   = float(HUGE_VAL);
 #else
@@ -63,11 +64,6 @@ namespace bx
 		return ::logf(_a);
 	}
 
-	float flog2(float _a)
-	{
-		return ::logf(_a) * (1 / ::logf(2));
-	}
-
 	float fsqrt(float _a)
 	{
 		return ::sqrtf(_a);

+ 14 - 2
tests/fpumath_test.cpp

@@ -13,13 +13,25 @@ TEST_CASE("isFinite, isInfinite, isNan", "")
 	for (uint64_t ii = 0; ii < UINT32_MAX; ii += rand()%(1<<13)+1)
 	{
 		union { uint32_t ui; float f; } u = { uint32_t(ii) };
-		REQUIRE(std::isnan(u.f) == bx::isNan(u.f) );
+		REQUIRE(std::isnan(u.f)    == bx::isNan(u.f) );
 		REQUIRE(std::isfinite(u.f) == bx::isFinite(u.f) );
-		REQUIRE(std::isinf(u.f) == bx::isInfinite(u.f) );
+		REQUIRE(std::isinf(u.f)    == bx::isInfinite(u.f) );
 	}
 }
 #endif // !BX_COMPILER_MSVC || BX_COMPILER_MSVC >= 1800
 
+
+bool flog2_test(float _a)
+{
+	return bx::flog2(_a) == bx::flog(_a) * (1.0f / bx::flog(2.0f) );
+}
+
+TEST_CASE("flog2", "")
+{
+	flog2_test(0.0f);
+	flog2_test(256.0f);
+}
+
 TEST_CASE("ToBits", "")
 {
 	REQUIRE(UINT32_C(0x12345678)         == bx::floatToBits( bx::bitsToFloat( UINT32_C(0x12345678) ) ) );