diff --git a/maths.cpp b/maths.cpp index 4e382600..4b5cbdd0 100644 --- a/maths.cpp +++ b/maths.cpp @@ -40,6 +40,18 @@ template bool is_pow2 (uint32_t); template bool is_pow2 (uint64_t); +//----------------------------------------------------------------------------- +template +T +log2up (T v) +{ + return log2 ((v << 1) - 1); +} + +template uint32_t log2up (uint32_t); +template uint64_t log2up (uint64_t); + + //----------------------------------------------------------------------------- template T diff --git a/maths.hpp b/maths.hpp index 19c44d75..0b9ad9d2 100644 --- a/maths.hpp +++ b/maths.hpp @@ -41,6 +41,11 @@ T log2 (T val) pure; +template +T +log2up (T val) pure; + + template double rootsquare (T a, T b) pure; diff --git a/test/maths/maths.cpp b/test/maths/maths.cpp index c61026c0..4d70735f 100644 --- a/test/maths/maths.cpp +++ b/test/maths/maths.cpp @@ -45,5 +45,10 @@ main (int, char **) { CHECK_EQ (log2 (8u), 3); CHECK_EQ (log2 (1u), 0); + CHECK_EQ (log2 (9u), 3); + CHECK_EQ (log2up (9u), 4); + CHECK_EQ (log2up (8u), 3); + CHECK_EQ (log2up (1u), 0); + return EXIT_SUCCESS; }