maths: move log2 into header for constexpr

This commit is contained in:
Danny Robson 2017-12-22 12:33:29 +11:00
parent d0d5ae549e
commit cfe1185621
2 changed files with 9 additions and 23 deletions

View File

@ -31,27 +31,6 @@ template uint32_t util::log2up (uint32_t);
template uint64_t util::log2up (uint64_t);
///////////////////////////////////////////////////////////////////////////////
template <typename T>
T
util::log2 (T v)
{
static_assert (std::is_integral<T>::value,
"log2 is only implemented for integers");
T l = 0;
while (v >>= 1)
++l;
return l;
}
template uint8_t util::log2 (uint8_t);
template uint16_t util::log2 (uint16_t);
template uint32_t util::log2 (uint32_t);
template uint64_t util::log2 (uint64_t);
///////////////////////////////////////////////////////////////////////////////
template const float util::PI<float>;
template const double util::PI<double>;

View File

@ -242,8 +242,15 @@ namespace util {
//-----------------------------------------------------------------------------
// Logarithms
template <typename T>
T
log2 (T val);
constexpr
std::enable_if_t<std::is_integral_v<T>, T>
log2 (T val)
{
T tally = 0;
while (val >>= 1)
++tally;
return tally;
}
//-------------------------------------------------------------------------