mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-05 09:06:08 +00:00
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:
parent
29112f7365
commit
d0d81e470e
Notes:
sideshowbarker
2024-07-18 01:43:16 +09:00
Author: https://github.com/tcl3
Commit: d0d81e470e
Pull-request: https://github.com/SerenityOS/serenity/pull/24318
Reviewed-by: https://github.com/Hendiadyoin1 ✅
2 changed files with 36 additions and 4 deletions
|
@ -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>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue