maths: add log2up

This commit is contained in:
Danny Robson 2014-09-17 16:41:38 +10:00
parent b3e72051d6
commit bb3cbb584c
3 changed files with 22 additions and 0 deletions

View File

@ -40,6 +40,18 @@ template bool is_pow2 (uint32_t);
template bool is_pow2 (uint64_t); template bool is_pow2 (uint64_t);
//-----------------------------------------------------------------------------
template <typename T>
T
log2up (T v)
{
return log2 ((v << 1) - 1);
}
template uint32_t log2up (uint32_t);
template uint64_t log2up (uint64_t);
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
T T

View File

@ -41,6 +41,11 @@ T
log2 (T val) pure; log2 (T val) pure;
template <typename T>
T
log2up (T val) pure;
template <typename T> template <typename T>
double double
rootsquare (T a, T b) pure; rootsquare (T a, T b) pure;

View File

@ -45,5 +45,10 @@ main (int, char **) {
CHECK_EQ (log2 (8u), 3); CHECK_EQ (log2 (8u), 3);
CHECK_EQ (log2 (1u), 0); 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; return EXIT_SUCCESS;
} }