AK: Simplify BigEndianInputBitStream::read_bits()

No need to perform extra loop iterations every time we need to read a
new byte. No functional changes.
This commit is contained in:
Jelle Raaijmakers 2026-04-29 09:51:30 +02:00 committed by Jelle Raaijmakers
parent 3ffc75961d
commit 71b0aaa461

View file

@ -60,39 +60,39 @@ public:
size_t nread = 0; size_t nread = 0;
while (nread < count) { while (nread < count) {
if (m_current_byte.has_value()) { if (!m_current_byte.has_value()) {
if constexpr (!IsSame<bool, T> && !IsSame<u8, T>) { m_current_byte = TRY(m_stream->read_value<u8>());
// read as many bytes as possible directly m_bit_offset = 0;
if (((count - nread) >= 8) && is_aligned_to_byte_boundary()) { }
// shift existing data over
result <<= 8; if constexpr (IsOneOf<T, bool, u8>) {
result |= m_current_byte.value(); // Always take this branch for booleans or u8: there's no purpose in reading more than a single bit.
nread += 8; auto const bit = (m_current_byte.value() >> (7 - m_bit_offset)) & 1;
m_current_byte.clear(); if constexpr (IsSame<bool, T>) {
} else { result = bit;
auto const bit = (m_current_byte.value() >> (7 - m_bit_offset)) & 1; } else {
result <<= 1; result <<= 1;
result |= bit; result |= bit;
++nread; }
if (m_bit_offset++ == 7) ++nread;
m_current_byte.clear(); if (m_bit_offset++ == 7)
} m_current_byte.clear();
} else {
// Read as many bits as possible directly.
if ((count - nread >= 8) && is_aligned_to_byte_boundary()) {
// shift existing data over
result <<= 8;
result |= m_current_byte.value();
nread += 8;
m_current_byte.clear();
} else { } else {
// Always take this branch for booleans or u8: there's no purpose in reading more than a single bit
auto const bit = (m_current_byte.value() >> (7 - m_bit_offset)) & 1; auto const bit = (m_current_byte.value() >> (7 - m_bit_offset)) & 1;
if constexpr (IsSame<bool, T>) result <<= 1;
result = bit; result |= bit;
else {
result <<= 1;
result |= bit;
}
++nread; ++nread;
if (m_bit_offset++ == 7) if (m_bit_offset++ == 7)
m_current_byte.clear(); m_current_byte.clear();
} }
} else {
m_current_byte = TRY(m_stream->read_value<u8>());
m_bit_offset = 0;
} }
} }