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:
parent
3ffc75961d
commit
71b0aaa461
1 changed files with 27 additions and 27 deletions
|
|
@ -60,10 +60,26 @@ public:
|
|||
|
||||
size_t nread = 0;
|
||||
while (nread < count) {
|
||||
if (m_current_byte.has_value()) {
|
||||
if constexpr (!IsSame<bool, T> && !IsSame<u8, T>) {
|
||||
// read as many bytes as possible directly
|
||||
if (((count - nread) >= 8) && is_aligned_to_byte_boundary()) {
|
||||
if (!m_current_byte.has_value()) {
|
||||
m_current_byte = TRY(m_stream->read_value<u8>());
|
||||
m_bit_offset = 0;
|
||||
}
|
||||
|
||||
if constexpr (IsOneOf<T, bool, u8>) {
|
||||
// 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;
|
||||
if constexpr (IsSame<bool, T>) {
|
||||
result = bit;
|
||||
} else {
|
||||
result <<= 1;
|
||||
result |= bit;
|
||||
}
|
||||
++nread;
|
||||
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();
|
||||
|
|
@ -77,22 +93,6 @@ public:
|
|||
if (m_bit_offset++ == 7)
|
||||
m_current_byte.clear();
|
||||
}
|
||||
} 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;
|
||||
if constexpr (IsSame<bool, T>)
|
||||
result = bit;
|
||||
else {
|
||||
result <<= 1;
|
||||
result |= bit;
|
||||
}
|
||||
++nread;
|
||||
if (m_bit_offset++ == 7)
|
||||
m_current_byte.clear();
|
||||
}
|
||||
} else {
|
||||
m_current_byte = TRY(m_stream->read_value<u8>());
|
||||
m_bit_offset = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue