From d588a8fb0e027c4aef8d39c7f3758cbe00bcc80e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Fri, 22 Nov 2024 19:26:20 -0800 Subject: [PATCH] Cleanup. --- include/bx/inline/math.inl | 14 ++++++++++++++ include/bx/math.h | 4 ++-- src/math.cpp | 16 +--------------- tests/math_test.cpp | 2 ++ 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/include/bx/inline/math.inl b/include/bx/inline/math.inl index a869943ea..bd2f57aec 100644 --- a/include/bx/inline/math.inl +++ b/include/bx/inline/math.inl @@ -227,6 +227,20 @@ namespace bx return log(_a) * kInvLogNat2; } + inline BX_CONSTEXPR_FUNC float ldexp(float _a, int32_t _b) + { + const uint32_t ftob = floatToBits(_a); + const uint32_t masked = uint32_and(ftob, kFloatSignMask | kFloatExponentMask); + const uint32_t expsign0 = uint32_sra(masked, kFloatExponentBitShift); + const uint32_t tmp = uint32_iadd(expsign0, _b); + const uint32_t expsign1 = uint32_sll(tmp, kFloatExponentBitShift); + const uint32_t mantissa = uint32_and(ftob, kFloatMantissaMask); + const uint32_t bits = uint32_or(mantissa, expsign1); + const float result = bitsToFloat(bits); + + return result; + } + template<> inline BX_CONSTEXPR_FUNC uint8_t countBits(uint32_t _val) { diff --git a/include/bx/math.h b/include/bx/math.h index 1763569de..280688ab8 100644 --- a/include/bx/math.h +++ b/include/bx/math.h @@ -254,9 +254,9 @@ namespace bx /// BX_CONST_FUNC float pow(float _a, float _b); - /// Returns the result of multiplying _a by 2 raised to the power of the exponent. + /// Returns the result of multiplying _a by 2 raised to the power of the exponent `_a * (2^_b)`. /// - BX_CONST_FUNC float ldexp(float _a, int32_t _b); + BX_CONSTEXPR_FUNC float ldexp(float _a, int32_t _b); /// Returns decomposed given floating point value _a into a normalized fraction and /// an integral power of two. diff --git a/src/math.cpp b/src/math.cpp index 52cafd9dd..0216fa82b 100644 --- a/src/math.cpp +++ b/src/math.cpp @@ -130,25 +130,11 @@ namespace bx return result; } - BX_CONST_FUNC float ldexp(float _a, int32_t _b) - { - const uint32_t ftob = floatToBits(_a); - const uint32_t masked = uint32_and(ftob, kFloatSignMask | kFloatExponentMask); - const uint32_t expsign0 = uint32_sra(masked, 23); - const uint32_t tmp = uint32_iadd(expsign0, _b); - const uint32_t expsign1 = uint32_sll(tmp, 23); - const uint32_t mantissa = uint32_and(ftob, kFloatMantissaMask); - const uint32_t bits = uint32_or(mantissa, expsign1); - const float result = bitsToFloat(bits); - - return result; - } - 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, 23); + 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); diff --git a/tests/math_test.cpp b/tests/math_test.cpp index 1eafb6491..2ad207da4 100644 --- a/tests/math_test.cpp +++ b/tests/math_test.cpp @@ -457,6 +457,8 @@ TEST_CASE("fract", "[math][libm]") TEST_CASE("ldexp", "[math][libm]") { + STATIC_REQUIRE(1389.0f == bx::ldexp(86.8125, 4) ); + bx::WriterI* writer = bx::getNullOut(); bx::Error err;