From 3264dcee441e99679723d806b3a5e43b602dbbeb Mon Sep 17 00:00:00 2001 From: Bananymous Date: Tue, 21 Apr 2026 00:14:01 +0300 Subject: [PATCH] BAN: Add Math::round_up_to_power_of_two --- BAN/include/BAN/Math.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/BAN/include/BAN/Math.h b/BAN/include/BAN/Math.h index 3fd56b1f..1a054927 100644 --- a/BAN/include/BAN/Math.h +++ b/BAN/include/BAN/Math.h @@ -65,6 +65,22 @@ namespace BAN::Math return (x & (x - 1)) == 0; } + template requires(sizeof(T) <= 8) + inline constexpr T round_up_to_power_of_two(T x) + { + x--; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + if constexpr(sizeof(T) >= 2) + x |= x >> 8; + if constexpr(sizeof(T) >= 4) + x |= x >> 16; + if constexpr(sizeof(T) >= 8) + x |= x >> 32; + return x + 1; + } + template __attribute__((always_inline)) inline constexpr bool will_multiplication_overflow(T a, T b)