From 831a7b7037cb3e3232e6a171a052828967f1bd81 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Sat, 19 Sep 2020 09:05:23 +1000 Subject: [PATCH] maths: add integer consteval logarithm --- maths.hpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) 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