diff --git a/maths.hpp b/maths.hpp index 67748baf..93eda14f 100644 --- a/maths.hpp +++ b/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 + 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