From 105ea1c0f896a4a2718f1356cae0dce8c01859cc Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Wed, 23 May 2018 23:24:50 +0000 Subject: Avoid repeated calculations in CFX_BitStream::GetBits(). Change-Id: Icfb7c6933625436b9fcf6a9fdfd0e5f655108eca Reviewed-on: https://pdfium-review.googlesource.com/32851 Reviewed-by: dsinclair Commit-Queue: Lei Zhang --- core/fxcrt/cfx_bitstream.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'core/fxcrt') diff --git a/core/fxcrt/cfx_bitstream.cpp b/core/fxcrt/cfx_bitstream.cpp index 114b5a9d2a..3d3040d96a 100644 --- a/core/fxcrt/cfx_bitstream.cpp +++ b/core/fxcrt/cfx_bitstream.cpp @@ -22,29 +22,34 @@ void CFX_BitStream::ByteAlign() { } uint32_t CFX_BitStream::GetBits(uint32_t nBits) { - if (nBits > m_BitSize || m_BitPos + nBits > m_BitSize) + ASSERT(nBits > 0); + ASSERT(nBits <= 32); + if (nBits > m_BitSize || m_BitPos > m_BitSize - nBits) return 0; + const uint32_t bit_pos = m_BitPos % 8; + uint32_t byte_pos = m_BitPos / 8; const uint8_t* data = m_pData.Get(); + uint8_t current_byte = data[byte_pos]; if (nBits == 1) { - int bit = (data[m_BitPos / 8] & (1 << (7 - m_BitPos % 8))) ? 1 : 0; + int bit = (current_byte & (1 << (7 - bit_pos))) ? 1 : 0; m_BitPos++; return bit; } - uint32_t byte_pos = m_BitPos / 8; - uint32_t bit_pos = m_BitPos % 8; uint32_t bit_left = nBits; uint32_t result = 0; if (bit_pos) { - if (8 - bit_pos >= bit_left) { - result = (data[byte_pos] & (0xff >> bit_pos)) >> (8 - bit_pos - bit_left); + uint32_t bits_readable = 8 - bit_pos; + if (bits_readable >= bit_left) { + result = (current_byte & (0xff >> bit_pos)) >> (bits_readable - bit_left); m_BitPos += bit_left; return result; } - bit_left -= 8 - bit_pos; - result = (data[byte_pos++] & ((1 << (8 - bit_pos)) - 1)) << bit_left; + bit_left -= bits_readable; + result = (current_byte & ((1 << bits_readable) - 1)) << bit_left; + ++byte_pos; } while (bit_left >= 8) { bit_left -= 8; -- cgit v1.2.3