maths: add integer consteval logarithm

This commit is contained in:
Danny Robson 2020-09-19 09:05:23 +10:00
parent c23d2e38f3
commit 831a7b7037

View File

@ -214,6 +214,23 @@ namespace cruft {
}
/// Naively calculates the integer log of `val` in `base`, rounded down.
///
/// We deliberately restrict this to consteval to limit unexpected issues
/// with runtime performance given the simplistic construction.
///
/// It's useful for sizing temporary arrays.
template <concepts::integral T>
consteval T
ilog (T val, T base)
{
T tally = 0;
while (val /= base)
++tally;
return tally;
}
///////////////////////////////////////////////////////////////////////////////
/// round T up to the nearest multiple of U
template <concepts::integral T, concepts::integral U>