Browse Source

Math: function for base-2 logarithm.

Verbatim copy from Corrade, might be better to have it here too
(possibly some SIMD optimizations?).
pull/7/head
Vladimír Vondruš 14 years ago
parent
commit
52300cf402
  1. 7
      src/Math/Math.cpp
  2. 9
      src/Math/Math.h
  3. 8
      src/Math/Test/MathTest.cpp

7
src/Math/Math.cpp

@ -17,6 +17,13 @@
namespace Magnum { namespace Math {
std::uint32_t log2(std::uint32_t number) {
std::uint32_t log = 0;
while(number >>= 1)
++log;
return log;
}
std::uint32_t log(std::uint32_t base, std::uint32_t number) {
std::uint32_t log = 0;
while(number /= base)

9
src/Math/Math.h

@ -58,10 +58,19 @@ template<std::uint32_t exponent, class T> inline constexpr T pow(T base) {
return Implementation::Pow<exponent>()(base);
}
/**
* @brief Base-2 integral logarithm
*
* Returns integral logarithm of given number with base `2`.
* @see log()
*/
std::uint32_t MAGNUM_EXPORT log2(std::uint32_t number);
/**
* @brief Integral logarithm
*
* Returns integral logarithm of given number with given base.
* @see log2()
*/
std::uint32_t MAGNUM_EXPORT log(std::uint32_t base, std::uint32_t number);

8
src/Math/Test/MathTest.cpp

@ -28,6 +28,7 @@ class MathTest: public Corrade::TestSuite::Tester {
void clamp();
void pow();
void log();
void log2();
};
MathTest::MathTest() {
@ -35,7 +36,8 @@ MathTest::MathTest() {
&MathTest::denormalize,
&MathTest::clamp,
&MathTest::pow,
&MathTest::log);
&MathTest::log,
&MathTest::log2);
}
void MathTest::normalize() {
@ -105,6 +107,10 @@ void MathTest::log() {
CORRADE_COMPARE(Math::log(256, 2), 0ul);
}
void MathTest::log2() {
CORRADE_COMPARE(Math::log2(2153), 11);
}
}}}
CORRADE_TEST_MAIN(Magnum::Math::Test::MathTest)

Loading…
Cancel
Save