From 0f74e123b8b512b39f1eb959a5c8e27c0db2d642 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Thu, 7 Dec 2023 23:50:04 +0200 Subject: [PATCH] BAN: Implement ilog2 for unsigned integers --- BAN/include/BAN/Math.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/BAN/include/BAN/Math.h b/BAN/include/BAN/Math.h index 91a7a558..e8701287 100644 --- a/BAN/include/BAN/Math.h +++ b/BAN/include/BAN/Math.h @@ -59,6 +59,17 @@ namespace BAN::Math return (value & (value - 1)) == 0; } + template + requires is_same_v || is_same_v || is_same_v + inline constexpr T ilog2(T value) + { + if constexpr(is_same_v) + return sizeof(T) * 8 - __builtin_clz(value) - 1; + if constexpr(is_same_v) + return sizeof(T) * 8 - __builtin_clzl(value) - 1; + return sizeof(T) * 8 - __builtin_clzll(value) - 1; + } + template inline constexpr T log2(T value) {