LibCompress: Move two shared LZMA magic numbers into a common place

This commit is contained in:
Tim Schumacher 2023-05-19 14:13:13 +02:00 committed by Jelle Raaijmakers
parent e2ec8f6584
commit d4b0e64825
Notes: sideshowbarker 2024-07-17 02:23:25 +09:00
2 changed files with 7 additions and 11 deletions

View file

@ -231,10 +231,7 @@ ErrorOr<void> LzmaDecompressor::append_input_stream(MaybeOwned<Stream> stream, O
ErrorOr<void> LzmaDecompressor::normalize_range_decoder()
{
// "The value of the "Range" variable before each bit decoding can not be smaller
// than ((UInt32)1 << 24). The Normalize() function keeps the "Range" value in
// described range."
constexpr u32 minimum_range_value = 1 << 24;
// "The Normalize() function keeps the "Range" value in described range."
if (m_range_decoder_range >= minimum_range_value)
return {};
@ -290,8 +287,6 @@ ErrorOr<void> LzmaCompressor::normalize_range_encoder()
// Logically, we should only ever build up an overflow that is smaller than or equal to 0x01.
VERIFY((maximum_range_value >> 32) <= 0x01);
constexpr u32 minimum_range_value = 1 << 24;
if (m_range_encoder_range >= minimum_range_value)
return {};
@ -341,9 +336,6 @@ ErrorOr<u8> LzmaDecompressor::decode_bit_with_probability(Probability& probabili
// information about estimated probability for symbol 0 and the Range Decoder
// updates that CProb variable after decoding."
// The significance of the shift width is not explained and appears to be a magic constant.
constexpr size_t probability_shift_width = 5;
u32 bound = (m_range_decoder_range >> probability_bit_count) * probability;
dbgln_if(LZMA_DEBUG, "Decoding bit {} with probability = {:#x}, bound = {:#x}, code = {:#x}, range = {:#x}", m_range_decoder_code < bound ? 0 : 1, probability, bound, m_range_decoder_code, m_range_decoder_range);
@ -364,8 +356,6 @@ ErrorOr<u8> LzmaDecompressor::decode_bit_with_probability(Probability& probabili
ErrorOr<void> LzmaCompressor::encode_bit_with_probability(Probability& probability, u8 value)
{
constexpr size_t probability_shift_width = 5;
u32 bound = (m_range_encoder_range >> probability_bit_count) * probability;
dbgln_if(LZMA_DEBUG, "Encoding bit {} with probability = {:#x}, bound = {:#x}, code = {:#x}, range = {:#x}", value, probability, bound, m_range_encoder_code, m_range_encoder_range);

View file

@ -70,6 +70,12 @@ protected:
static constexpr Probability default_probability = (1 << probability_bit_count) / 2;
static void initialize_to_default_probability(Span<Probability>);
// The significance of the shift width is not explained and appears to be a magic constant.
static constexpr size_t probability_shift_width = 5;
// "The value of the "Range" variable before each bit decoding can not be smaller than ((UInt32)1 << 24)."
static constexpr u32 minimum_range_value = 1 << 24;
LzmaState(FixedArray<Probability> literal_probabilities);
u64 m_total_processed_bytes { 0 };