Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
bkaradzic committed Nov 24, 2024
1 parent d588a8f commit 7014882
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
16 changes: 13 additions & 3 deletions include/bx/inline/math.inl
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ namespace bx
return -floor(-_a);
}

inline BX_CONSTEXPR_FUNC float round(float _f)
inline BX_CONSTEXPR_FUNC float round(float _a)
{
return floor(_f + 0.5f);
return floor(_a + 0.5f);
}

inline BX_CONSTEXPR_FUNC float lerp(float _a, float _b, float _t)
Expand Down Expand Up @@ -214,7 +214,17 @@ namespace bx

inline BX_CONST_FUNC float pow(float _a, float _b)
{
return exp(_b * log(_a) );
if (abs(_b) < kFloatSmallest)
{
return 1.0f;
}

if (abs(_a) < kFloatSmallest)
{
return 0.0f;
}

return copySign(exp(_b * log(abs(_a) ) ), _a);
}

inline BX_CONST_FUNC float exp2(float _a)
Expand Down
1 change: 1 addition & 0 deletions src/math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ namespace bx
{
int32_t exp;
float ff = frexp(_a, &exp);

if (ff < kSqrt2*0.5f)
{
ff *= 2.0f;
Expand Down
50 changes: 49 additions & 1 deletion tests/math_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -431,22 +431,48 @@ TEST_CASE("mod", "[math][libm]")
STATIC_REQUIRE( 1.0f == bx::mod(1389.0f, 2.0f) );
}

typedef float (*MathFloatFn)(float);

template<MathFloatFn BxT, MathFloatFn CrtT>
void testMathFunc1Float(float _value)
{
REQUIRE(CrtT(_value) == BxT(_value) );
}

TEST_CASE("floor", "[math][libm]")
{
STATIC_REQUIRE( 13.0f == bx::floor( 13.89f) );
STATIC_REQUIRE(-14.0f == bx::floor(-13.89f) );

testMathFunc1Float<bx::floor, ::floorf>( 13.89f);
testMathFunc1Float<bx::floor, ::floorf>(-13.89f);
}

TEST_CASE("ceil", "[math][libm]")
{
STATIC_REQUIRE( 14.0f == bx::ceil( 13.89f) );
STATIC_REQUIRE(-13.0f == bx::ceil( -13.89f) );

testMathFunc1Float<bx::ceil, ::ceilf>( 13.89f);
testMathFunc1Float<bx::ceil, ::ceilf>(-13.89f);
}

TEST_CASE("round", "[math][libm]")
{
STATIC_REQUIRE( 14.0f == bx::round( 13.89f) );
STATIC_REQUIRE(-14.0f == bx::round( -13.89f) );

testMathFunc1Float<bx::round, ::roundf>( 13.89f);
testMathFunc1Float<bx::round, ::roundf>(-13.89f);
}

TEST_CASE("trunc", "[math][libm]")
{
STATIC_REQUIRE( 13.0f == bx::trunc( 13.89f) );
STATIC_REQUIRE(-13.0f == bx::trunc(-13.89f) );

testMathFunc1Float<bx::trunc, ::truncf>( 13.89f);
testMathFunc1Float<bx::trunc, ::truncf>(-13.89f);
}

TEST_CASE("fract", "[math][libm]")
Expand All @@ -457,7 +483,13 @@ TEST_CASE("fract", "[math][libm]")

TEST_CASE("ldexp", "[math][libm]")
{
STATIC_REQUIRE(1389.0f == bx::ldexp(86.8125, 4) );
STATIC_REQUIRE( 1389.0f == bx::ldexp(86.8125, 4) );
STATIC_REQUIRE(0.437500f == bx::ldexp(7.0f, -4.0f) );
STATIC_REQUIRE(bx::isEqual(-0.0f, bx::ldexp(-0.0f, 10.0f), 0.000000001f) );

STATIC_REQUIRE(0x1p127f == bx::ldexp(1.0f, 127.0f) );
STATIC_REQUIRE(0x1p-126f == bx::ldexp(1.0f, -126.0f) );
STATIC_REQUIRE(0x1p24f == bx::ldexp(1.0f, 24.0f) );

bx::WriterI* writer = bx::getNullOut();
bx::Error err;
Expand Down Expand Up @@ -487,6 +519,22 @@ TEST_CASE("exp", "[math][libm]")

TEST_CASE("pow", "[math][libm]")
{
REQUIRE(1.0f == bx::pow(0.0f, 0.0f) );
REQUIRE(1.0f == bx::pow(1.0f, 0.0f) );
REQUIRE(1.0f == bx::pow(3.0f, 0.0f) );
REQUIRE(1.0f == bx::pow(8.0f, 0.0f) );
REQUIRE(1.0f == bx::pow(9.0f, 0.0f) );
REQUIRE(0.0f == bx::pow(0.0f, 2.0f) );

REQUIRE( 4.0f == bx::pow( 2.0f, 2.0f) );
REQUIRE( -4.0f == bx::pow(-2.0f, 2.0f) );
REQUIRE( 0.25f == bx::pow( 2.0f, -2.0f) );
REQUIRE( -0.25f == bx::pow(-2.0f, -2.0f) );
REQUIRE( 8.0f == bx::pow( 2.0f, 3.0f) );
REQUIRE( -8.0f == bx::pow(-2.0f, 3.0f) );
REQUIRE( 0.125f == bx::pow( 2.0f, -3.0f) );
REQUIRE(-0.125f == bx::pow(-2.0f, -3.0f) );

bx::WriterI* writer = bx::getNullOut();
bx::Error err;

Expand Down

0 comments on commit 7014882

Please sign in to comment.