Skip to content

Commit

Permalink
Fixed issue with frexp.
Browse files Browse the repository at this point in the history
  • Loading branch information
bkaradzic committed Dec 6, 2024
1 parent b97e679 commit e88e4bb
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 44 deletions.
25 changes: 9 additions & 16 deletions include/bx/inline/math.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion include/bx/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
22 changes: 5 additions & 17 deletions scripts/genie.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -66,6 +62,10 @@ project "bx.test"

using_bx()

defines {
"CATCH_AMALGAMATED_CUSTOM_MAIN",
}

configuration { "vs* or mingw*" }
links {
"psapi",
Expand Down Expand Up @@ -116,9 +116,7 @@ project "bx.bench"
path.join(BX_DIR, "tests/dbg.*"),
}

links {
"bx",
}
using_bx()

configuration { "vs* or mingw*" }
links {
Expand All @@ -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()
2 changes: 1 addition & 1 deletion scripts/toolchain.lua
Original file line number Diff line number Diff line change
Expand Up @@ -509,14 +509,14 @@ function toolchain(_buildDir, _libDir)
}

configuration { "Release" }
targetsuffix "Release"
flags {
"NoBufferSecurityCheck",
"OptimizeSpeed",
}
defines {
"NDEBUG",
}
targetsuffix "Release"

configuration { "*-clang" }
buildoptions {
Expand Down
2 changes: 1 addition & 1 deletion src/dtoa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++ = '-';
Expand Down
15 changes: 15 additions & 0 deletions src/math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
10 changes: 8 additions & 2 deletions tests/math_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) };
Expand Down Expand Up @@ -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]")
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down
16 changes: 10 additions & 6 deletions tests/vsnprintf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) );
Expand All @@ -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) );
Expand Down

0 comments on commit e88e4bb

Please sign in to comment.