maths: add integer consteval logarithm
This commit is contained in:
parent
c23d2e38f3
commit
831a7b7037
17
maths.hpp
17
maths.hpp
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user