Browse Source

Added floorLog2 function (#314)

Jamil Halabi 2 years ago
parent
commit
3e5d611007
2 changed files with 12 additions and 0 deletions
  1. 7 0
      include/bx/inline/math.inl
  2. 5 0
      include/bx/math.h

+ 7 - 0
include/bx/inline/math.inl

@@ -376,6 +376,13 @@ namespace bx
 		return Ty(_a) < Ty(1) ? Ty(0) : sizeof(Ty)*8 - countLeadingZeros<Ty>(_a - 1);
 	}
 
+	template<typename Ty>
+	inline BX_CONSTEXPR_FUNC uint8_t floorLog2(Ty _a)
+	{
+		BX_STATIC_ASSERT(isInteger<Ty>(), "Type Ty must be of integer type!");
+		return Ty(_a) < Ty(1) ? Ty(0) : sizeof(Ty)*8 - 1 - countLeadingZeros<Ty>(_a);
+	}
+
 	template<typename Ty>
 	inline BX_CONSTEXPR_FUNC Ty nextPow2(Ty _a)
 	{

+ 5 - 0
include/bx/math.h

@@ -297,6 +297,11 @@ namespace bx
 	template<typename Ty>
 	BX_CONSTEXPR_FUNC uint8_t ceilLog2(Ty _a);
 
+	/// Returns the next biggest integer base 2 logarithm of _a.
+	///
+	template<typename Ty>
+	BX_CONSTEXPR_FUNC uint8_t floorLog2(Ty _a);
+
 	/// Returns the next smallest power of two value.
 	///
 	template<typename Ty>