From 3e5d6110070c74b543a0044bfe49cf92dfd06c0b Mon Sep 17 00:00:00 2001 From: Jamil Halabi Date: Mon, 11 Dec 2023 18:38:56 +0300 Subject: [PATCH] Added floorLog2 function (#314) --- include/bx/inline/math.inl | 7 +++++++ include/bx/math.h | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/include/bx/inline/math.inl b/include/bx/inline/math.inl index 90b3236..b5b3a28 100644 --- a/include/bx/inline/math.inl +++ b/include/bx/inline/math.inl @@ -376,6 +376,13 @@ namespace bx return Ty(_a) < Ty(1) ? Ty(0) : sizeof(Ty)*8 - countLeadingZeros(_a - 1); } + template + inline BX_CONSTEXPR_FUNC uint8_t floorLog2(Ty _a) + { + BX_STATIC_ASSERT(isInteger(), "Type Ty must be of integer type!"); + return Ty(_a) < Ty(1) ? Ty(0) : sizeof(Ty)*8 - 1 - countLeadingZeros(_a); + } + template inline BX_CONSTEXPR_FUNC Ty nextPow2(Ty _a) { diff --git a/include/bx/math.h b/include/bx/math.h index d040806..1dee710 100644 --- a/include/bx/math.h +++ b/include/bx/math.h @@ -297,6 +297,11 @@ namespace bx template BX_CONSTEXPR_FUNC uint8_t ceilLog2(Ty _a); + /// Returns the next biggest integer base 2 logarithm of _a. + /// + template + BX_CONSTEXPR_FUNC uint8_t floorLog2(Ty _a); + /// Returns the next smallest power of two value. /// template