summaryrefslogtreecommitdiff
path: root/AK/IntegralMath.h
diff options
context:
space:
mode:
authorMacDue <macdue@dueutil.tech>2022-06-29 18:56:39 +0100
committerAndreas Kling <kling@serenityos.org>2022-06-30 11:16:22 +0200
commit072a78b958de7510cadf174b73eaa1b1584f5c00 (patch)
treef64eab29f23bc9aacd4379a9f97c27f01ba2d2e2 /AK/IntegralMath.h
parent1e3622432107dc5b47242da88479e0c189e7a488 (diff)
downloadserenity-072a78b958de7510cadf174b73eaa1b1584f5c00.zip
AK: Add AK::ceil(float) and AK::ceil_log2(integer)
Co-authored-by: Leon Albrecht <leon2002.la@gmail.com>
Diffstat (limited to 'AK/IntegralMath.h')
-rw-r--r--AK/IntegralMath.h11
1 files changed, 11 insertions, 0 deletions
diff --git a/AK/IntegralMath.h b/AK/IntegralMath.h
index 916d9b70cc..415e485bf9 100644
--- a/AK/IntegralMath.h
+++ b/AK/IntegralMath.h
@@ -24,6 +24,17 @@ constexpr T log2(T x)
return x ? (8 * sizeof(T) - 1) - count_leading_zeroes(static_cast<MakeUnsigned<T>>(x)) : 0;
}
+template<Integral T>
+constexpr T ceil_log2(T x)
+{
+ if (!x)
+ return 0;
+
+ T log = AK::log2(x);
+ log += (x & ((1 << (log - 1)) - 1)) != 0;
+ return log;
+}
+
template<Integral I>
constexpr I pow(I base, I exponent)
{