LibGfx: Use count_leading_zeroes to calculate nearest power of 2

This removes the possibility of an infinite loop.
This commit is contained in:
Tim Ledbetter 2023-10-25 21:52:26 +01:00 committed by Andreas Kling
parent 6e4c97a328
commit 7096ea82f9
Notes: sideshowbarker 2024-07-17 16:23:55 +09:00
2 changed files with 6 additions and 8 deletions

View file

@ -63,10 +63,9 @@ static constexpr u32 WOFF_SIGNATURE = 0x774F4646;
static u16 pow_2_less_than_or_equal(u16 x)
{
u16 result = 1;
while (result < x)
result <<= 1;
return result;
VERIFY(x > 0);
VERIFY(x < 32769);
return 1 << (sizeof(u16) * 8 - count_leading_zeroes<u16>(x - 1));
}
ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_file(DeprecatedString path, unsigned int index)

View file

@ -111,10 +111,9 @@ static i16 be_i16(u8 const* ptr)
static u16 pow_2_less_than_or_equal(u16 x)
{
u16 result = 1;
while (result < x)
result <<= 1;
return result;
VERIFY(x > 0);
VERIFY(x < 32769);
return 1 << (sizeof(u16) * 8 - count_leading_zeroes<u16>(x - 1));
}
enum class TransformationVersion {