maths: document log2 and log2up

This commit is contained in:
Danny Robson 2020-03-16 13:51:45 +11:00
parent fb13c0fb0f
commit 67a3e016e1
2 changed files with 16 additions and 16 deletions

View File

@ -11,18 +11,6 @@
#include <type_traits>
///////////////////////////////////////////////////////////////////////////////
template <typename T>
T
cruft::log2up (T v)
{
return log2 ((v << 1) - 1);
}
template uint32_t cruft::log2up (uint32_t);
template uint64_t cruft::log2up (uint64_t);
///////////////////////////////////////////////////////////////////////////////
// Simple instantiations. Some functions aren't used internally to the library
// so it's easier to instantiate early and check for broken code at library

View File

@ -183,12 +183,18 @@ namespace cruft {
}
//-----------------------------------------------------------------------------
// Logarithms
///////////////////////////////////////////////////////////////////////////////
/// Calculate the base-2 logarithm of an integer, truncating to the next
/// lowest integer.
///
/// `val` must be strictly greater than zero, otherwise the results are
/// undefined.
template <concepts::integral T>
constexpr T
log2 (T val)
{
assert (val > 0);
T tally = 0;
while (val >>= 1)
++tally;
@ -196,10 +202,16 @@ namespace cruft {
}
//-------------------------------------------------------------------------
///------------------------------------------------------------------------
/// Calculates the base-2 logarithm of an integer, rounding up to the next
/// highest integer.
template <typename T>
T
log2up (T val);
log2up (T val)
{
return log2 ((val << 1) - 1);
}
///////////////////////////////////////////////////////////////////////////////