maths: add log2

This commit is contained in:
Danny Robson 2014-09-11 15:34:59 +10:00
parent 59348c0589
commit 755e0d92cb
2 changed files with 25 additions and 0 deletions

View File

@ -40,6 +40,26 @@ template bool is_pow2 (uint32_t);
template bool is_pow2 (uint64_t); template bool is_pow2 (uint64_t);
//-----------------------------------------------------------------------------
template <typename T>
T
log2 (T v) {
static_assert (std::is_integral<T>::value,
"log2 is only implemented for integers");
T l = 0;
while (v) {
v >>= 1;
l += 1;
}
return l;
}
template uint32_t log2 (uint32_t);
template uint64_t log2 (uint64_t);
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
double double

View File

@ -36,6 +36,11 @@ bool
is_pow2 (T value) pure; is_pow2 (T value) pure;
template <typename T>
T
log2 (T val) pure;
template <typename T> template <typename T>
double double
rootsquare (T a, T b) pure; rootsquare (T a, T b) pure;