Procházet zdrojové kódy

Fixed issue with frexp.

Бранимир Караџић před 1 rokem
rodič
revize
e88e4bbad8

+ 9 - 16
include/bx/inline/math.inl

@@ -386,20 +386,6 @@ namespace bx
 		return result;
 	}
 
-	inline BX_CONSTEXPR_FUNC float frexp(float _a, int32_t* _outExp)
-	{
-		const uint32_t ftob     = floatToBits(_a);
-		const uint32_t masked0  = uint32_and(ftob, kFloatExponentMask);
-		const uint32_t exp0     = uint32_srl(masked0, kFloatExponentBitShift);
-		const uint32_t masked1  = uint32_and(ftob,   kFloatSignMask | kFloatMantissaMask);
-		const uint32_t bits     = uint32_or(masked1, UINT32_C(0x3f000000) );
-		const float    result   = bitsToFloat(bits);
-
-		*_outExp = int32_t(exp0 - 0x7e);
-
-		return result;
-	}
-
 	inline BX_CONSTEXPR_FUNC float ldexp(float _a, int32_t _b)
 	{
 		const uint32_t ftob     = floatToBits(_a);
@@ -416,8 +402,15 @@ namespace bx
 
 	inline BX_CONSTEXPR_FUNC float log(float _a)
 	{
-		int32_t exp = 0;
-		float ff = frexp(_a, &exp);
+		const uint32_t ftob     = floatToBits(_a);
+
+		const uint32_t masked0  = uint32_and(ftob, kFloatExponentMask);
+		const uint32_t exp0     = uint32_srl(masked0, kFloatExponentBitShift);
+		int32_t exp = int32_t(exp0 - 0x7e);
+
+		const uint32_t masked1  = uint32_and(ftob,   kFloatSignMask | kFloatMantissaMask);
+		const uint32_t bits     = uint32_or(masked1, UINT32_C(0x3f000000) );
+		float ff = bitsToFloat(bits);
 
 		if (ff < kSqrt2*0.5f)
 		{

+ 1 - 1
include/bx/math.h

@@ -261,7 +261,7 @@ namespace bx
 	/// Returns decomposed given floating point value _a into a normalized fraction and
 	/// an integral power of two.
 	///
-	BX_CONSTEXPR_FUNC float frexp(float _a, int32_t* _outExp);
+	float frexp(float _a, int32_t* _outExp);
 
 	/// Returns e (2.71828...) raised to the _a power.
 	///

+ 5 - 17
scripts/genie.lua

@@ -53,10 +53,6 @@ project "bx.test"
 		BX_THIRD_PARTY_DIR,
 	}
 
-	defines {
-		"CATCH_AMALGAMATED_CUSTOM_MAIN",
-	}
-
 	files {
 		path.join(BX_DIR, "3rdparty/catch/catch_amalgamated.cpp"),
 		path.join(BX_DIR, "tests/*_test.cpp"),
@@ -66,6 +62,10 @@ project "bx.test"
 
 	using_bx()
 
+	defines {
+		"CATCH_AMALGAMATED_CUSTOM_MAIN",
+	}
+
 	configuration { "vs* or mingw*" }
 		links {
 			"psapi",
@@ -116,9 +116,7 @@ project "bx.bench"
 		path.join(BX_DIR, "tests/dbg.*"),
 	}
 
-	links {
-		"bx",
-	}
+	using_bx()
 
 	configuration { "vs* or mingw*" }
 		links {
@@ -141,16 +139,6 @@ project "bx.bench"
 			"Cocoa.framework",
 		}
 
-	configuration { "Debug" }
-		defines {
-			"BX_CONFIG_DEBUG=1",
-		}
-
-	configuration { "Release" }
-		defines {
-			"BX_CONFIG_DEBUG=0",
-		}
-
 	configuration {}
 
 	strip()

+ 1 - 1
scripts/toolchain.lua

@@ -509,6 +509,7 @@ function toolchain(_buildDir, _libDir)
 		}
 
 	configuration { "Release" }
+		targetsuffix "Release"
 		flags {
 			"NoBufferSecurityCheck",
 			"OptimizeSpeed",
@@ -516,7 +517,6 @@ function toolchain(_buildDir, _libDir)
 		defines {
 			"NDEBUG",
 		}
-		targetsuffix "Release"
 
 	configuration { "*-clang" }
 		buildoptions {

+ 1 - 1
src/dtoa.cpp

@@ -428,7 +428,7 @@ namespace bx
 
 	int32_t toString(char* _dst, int32_t _max, double _value)
 	{
-		int32_t sign = 0 != (doubleToBits(_value) & (UINT64_C(1)<<63) ) ? 1 : 0;
+		int32_t sign = 0 != (doubleToBits(_value) & kDoubleSignMask) ? 1 : 0;
 		if (1 == sign)
 		{
 			*_dst++ = '-';

+ 15 - 0
src/math.cpp

@@ -10,6 +10,21 @@
 
 namespace bx
 {
+	float frexp(float _a, int32_t* _outExp)
+	{
+		const uint32_t ftob     = floatToBits(_a);
+		const uint32_t masked0  = uint32_and(ftob, kFloatExponentMask);
+		const uint32_t exp0     = uint32_srl(masked0, kFloatExponentBitShift);
+
+		const uint32_t masked1  = uint32_and(ftob,   kFloatSignMask | kFloatMantissaMask);
+		const uint32_t bits     = uint32_or(masked1, UINT32_C(0x3f000000) );
+		const float    result   = bitsToFloat(bits);
+
+		*_outExp = int32_t(exp0 - 0x7e);
+
+		return result;
+	}
+
 	void mtxLookAt(float* _result, const Vec3& _eye, const Vec3& _at, const Vec3& _up, Handedness::Enum _handedness)
 	{
 		const Vec3 view = normalize(

+ 8 - 2
tests/math_test.cpp

@@ -13,6 +13,10 @@
 
 TEST_CASE("isFinite, isInfinite, isNan", "[math]")
 {
+#if defined(__FAST_MATH__) && __FAST_MATH__
+	SKIP("This unit test fails with fast math is enabled.");
+#endif // !defined(__FAST_MATH__) || !__FAST_MATH__
+
 	for (uint64_t ii = 0; ii < UINT32_MAX; ii += rand()%(1<<13)+1)
 	{
 		union { uint32_t ui; float f; } u = { uint32_t(ii) };
@@ -313,8 +317,6 @@ TEST_CASE("rcp", "[math][libm]")
 {
 	STATIC_REQUIRE(1.0f == bx::rcp(1.0f) );
 	STATIC_REQUIRE(2.0f == bx::rcp(0.5f) );
-	REQUIRE(bx::isInfinite(bx::rcp( 0.0f) ) );
-	REQUIRE(bx::isInfinite(bx::rcp(-0.0f) ) );
 }
 
 TEST_CASE("rcpSafe", "[math][libm]")
@@ -341,7 +343,9 @@ TEST_CASE("rsqrt", "[math][libm]")
 	}
 
 	// rsqrtSimd
+#if !defined(__FAST_MATH__) || !__FAST_MATH__
 	REQUIRE(bx::isInfinite(bx::rsqrtSimd(0.0f) ) );
+#endif // !defined(__FAST_MATH__) || !__FAST_MATH__
 
 	for (float xx = bx::kNearZero; xx < 100.0f; xx += 0.1f)
 	{
@@ -351,8 +355,10 @@ TEST_CASE("rsqrt", "[math][libm]")
 	}
 
 	// rsqrt
+#if !defined(__FAST_MATH__) || !__FAST_MATH__
 	REQUIRE(bx::isInfinite(1.0f / ::sqrtf(0.0f) ) );
 	REQUIRE(bx::isInfinite(bx::rsqrt(0.0f) ) );
+#endif // !defined(__FAST_MATH__) || !__FAST_MATH__
 
 	for (float xx = bx::kNearZero; xx < 100.0f; xx += 0.1f)
 	{

+ 10 - 6
tests/vsnprintf_test.cpp

@@ -130,6 +130,8 @@ static bool testNotStdCompliant(const char* _expected, const char* _format, ...)
 
 TEST_CASE("Format %f", "[string][printf]")
 {
+	constexpr double kDoubleNan = bx::bitsToDouble(bx::kDoubleExponentMask | bx::kDoubleMantissaMask);
+
 	REQUIRE(test("1.337",    "%0.3f", 1.337) );
 	REQUIRE(test("  13.370", "%8.3f", 13.37) );
 	REQUIRE(test("  13.370", "%*.*f", 8, 3, 13.37) );
@@ -139,13 +141,15 @@ TEST_CASE("Format %f", "[string][printf]")
 	REQUIRE(test("          13.370", "% 16.3f",  13.37) );
 	REQUIRE(test("         -13.370", "% 16.3f", -13.37) );
 
-	REQUIRE(test("nan     ", "%-8f",  std::numeric_limits<double>::quiet_NaN() ) );
-	REQUIRE(test("     nan", "%8f",   std::numeric_limits<double>::quiet_NaN() ) );
-	REQUIRE(test("-NAN    ", "%-8F", -std::numeric_limits<double>::quiet_NaN() ) );
+	REQUIRE(test("nan     ", "%-8f",  kDoubleNan) );
+	REQUIRE(test("     nan", "%8f",   kDoubleNan) );
+	REQUIRE(test("-NAN    ", "%-8F", -kDoubleNan) );
 
-	REQUIRE(test("     inf", "%8f",   std::numeric_limits<double>::infinity() ) );
-	REQUIRE(test("inf     ", "%-8f",  std::numeric_limits<double>::infinity() ) );
-	REQUIRE(test("    -INF", "%8F",  -std::numeric_limits<double>::infinity() ) );
+#if !defined(__FAST_MATH__) || !__FAST_MATH__
+	REQUIRE(test("     inf", "%8f",   bx::kDoubleInfinity) );
+	REQUIRE(test("inf     ", "%-8f",  bx::kDoubleInfinity) );
+	REQUIRE(test("    -INF", "%8F",  -bx::kDoubleInfinity) );
+#endif // !defined(__FAST_MATH__) || !__FAST_MATH__
 
 	REQUIRE(test(" 1.0",     "%4.1f",    1.0) );
 	REQUIRE(test(" 1.500",   "%6.3f",    1.5) );