Browse Source

Add integer log2 to math

Michael Ragazzon 3 years ago
parent
commit
26dc55417a
2 changed files with 16 additions and 0 deletions
  1. 4 0
      Include/RmlUi/Core/Math.h
  2. 12 0
      Source/Core/Math.cpp

+ 4 - 0
Include/RmlUi/Core/Math.h

@@ -156,6 +156,10 @@ RMLUICORE_API float ATan2(float y, float x);
 /// @param[in] value The value
 /// @return e^(value)
 RMLUICORE_API float Exp(float value);
+/// Evaluates the base-2 logarithm of an integer.
+/// @param[in] value The value
+/// @return log2(value)
+RMLUICORE_API int Log2(int value);
 
 /// Converts an angle from radians to degrees.
 /// @param[in] The angle, in radians.

+ 12 - 0
Source/Core/Math.cpp

@@ -111,6 +111,18 @@ RMLUICORE_API float Exp(float value)
 	return expf(value);
 }
 
+// Evaluates the base-2 logarithm of an integer.
+RMLUICORE_API int Log2(int value)
+{
+	int result = 0;
+	while (value > 1)
+	{
+		value >>= 1;
+		result++;
+	}
+	return result;
+}
+
 // Converts an angle from radians to degrees.
 RMLUICORE_API float RadiansToDegrees(float angle)
 {