diff options
author | MacDue <macdue@dueutil.tech> | 2022-06-29 18:56:39 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-06-30 11:16:22 +0200 |
commit | 072a78b958de7510cadf174b73eaa1b1584f5c00 (patch) | |
tree | f64eab29f23bc9aacd4379a9f97c27f01ba2d2e2 /AK/IntegralMath.h | |
parent | 1e3622432107dc5b47242da88479e0c189e7a488 (diff) | |
download | serenity-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.h | 11 |
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) { |