AK: Fix off by one error in integral ceil_log2()

Previously, certain values of `ceil_log2(x)` would be 1 smaller than
`ceil(log2(x))`.
This commit is contained in:
Tim Ledbetter 2024-05-13 18:57:38 +01:00 committed by Andreas Kling
commit d0d81e470e
Notes: sideshowbarker 2024-07-18 01:43:16 +09:00
2 changed files with 36 additions and 4 deletions

View file

@ -27,12 +27,10 @@ constexpr T log2(T x)
template<Integral T>
constexpr T ceil_log2(T x)
{
if (!x)
if (x <= 1)
return 0;
T log = AK::log2(x);
log += (x & ((((T)1) << (log - 1)) - 1)) != 0;
return log;
return AK::log2(x - 1) + 1;
}
template<Integral I>