diff --git a/libs/util/include/psemek/util/bits.hpp b/libs/util/include/psemek/util/bits.hpp index 9f17bf9d..38d7343b 100644 --- a/libs/util/include/psemek/util/bits.hpp +++ b/libs/util/include/psemek/util/bits.hpp @@ -5,6 +5,20 @@ namespace psemek::util { + // log2 rounded down + + template + T log2(T x) + { + T result = 0; + while (x > 1) + { + ++result; + x /= 2; + } + return result; + } + // round up to power of 2 template T round_pow2(T x)