maths: move log2 into header for constexpr
This commit is contained in:
parent
d0d5ae549e
commit
cfe1185621
21
maths.cpp
21
maths.cpp
@ -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>;
|
||||
|
11
maths.hpp
11
maths.hpp
@ -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;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
|
Loading…
Reference in New Issue
Block a user