Added integer version of log2.

This commit is contained in:
Бранимир Караџић
2019-10-22 19:29:51 -07:00
parent a9e8a24b60
commit 4bcb2c4fb3
3 changed files with 37 additions and 1 deletions

View File

@@ -10,6 +10,7 @@
#endif // BX_MATH_H_HEADER_GUARD
#include <bx/simd_t.h>
#include <bx/uint32_t.h>
namespace bx
{
@@ -191,11 +192,18 @@ namespace bx
return pow(2.0f, _a);
}
template<>
inline BX_CONST_FUNC float log2(float _a)
{
return log(_a) * kInvLogNat2;
}
template<>
inline BX_CONST_FUNC int32_t log2(int32_t _a)
{
return 31 - uint32_cntlz(_a);
}
inline BX_CONST_FUNC float rsqrtRef(float _a)
{
return pow(_a, -0.5f);

View File

@@ -225,7 +225,8 @@ namespace bx
/// Returns the base 2 logarithm of _a.
///
BX_CONST_FUNC float log2(float _a);
template<typename Ty>
BX_CONST_FUNC Ty log2(Ty _a);
/// Returns the square root of _a.
///

View File

@@ -31,6 +31,33 @@ TEST_CASE("log2", "")
{
log2_test(0.0f);
log2_test(256.0f);
REQUIRE(0.0f == bx::log2(1.0f) );
REQUIRE(0 == bx::log2(1) );
REQUIRE(1.0f == bx::log2(2.0f) );
REQUIRE(1 == bx::log2(2) );
REQUIRE(2.0f == bx::log2(4.0f) );
REQUIRE(2 == bx::log2(4) );
REQUIRE(3.0f == bx::log2(8.0f) );
REQUIRE(3 == bx::log2(8) );
REQUIRE(4.0f == bx::log2(16.0f) );
REQUIRE(4 == bx::log2(16) );
REQUIRE(5.0f == bx::log2(32.0f) );
REQUIRE(5 == bx::log2(32) );
REQUIRE(6.0f == bx::log2(64.0f) );
REQUIRE(6 == bx::log2(64) );
REQUIRE(7.0f == bx::log2(128.0f) );
REQUIRE(7 == bx::log2(128) );
REQUIRE(8.0f == bx::log2(256.0f) );
REQUIRE(8 == bx::log2(256) );
}
TEST_CASE("libm", "")