From 8693351f2b1f849261b30df38f35f9e488ff53d4 Mon Sep 17 00:00:00 2001 From: Kristof Date: Thu, 29 Feb 2024 10:25:53 +0100 Subject: [PATCH] fix merge --- src/codecs/jpeg/encoder.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/codecs/jpeg/encoder.rs b/src/codecs/jpeg/encoder.rs index db317459f1..2847f88fbf 100644 --- a/src/codecs/jpeg/encoder.rs +++ b/src/codecs/jpeg/encoder.rs @@ -250,6 +250,7 @@ impl BitWriter { self.write_bits(code, size) } + #[cfg(feature = "benchmarks")] fn write_block_old( &mut self, block: &[i32; 64], @@ -260,7 +261,7 @@ impl BitWriter { // Differential DC encoding let dcval = block[0]; let diff = dcval - prevdc; - let (size, value) = encode_coefficient(diff); + let (size, value) = encode_coefficient_old(diff); self.huffman_encode(size, dctable)?; self.write_bits(value, size)?; @@ -277,7 +278,7 @@ impl BitWriter { zero_run -= 16; } - let (size, value) = encode_coefficient(block[k as usize]); + let (size, value) = encode_coefficient_old(block[k as usize]); let symbol = (zero_run << 4) | size; self.huffman_encode(symbol, actable)?; @@ -855,6 +856,27 @@ fn build_quantization_segment(m: &mut Vec, precision: u8, identifier: u8, qt } } +#[cfg(feature = "benchmarks")] +fn encode_coefficient_old(coefficient: i32) -> (u8, u16) { + let mut magnitude = coefficient.unsigned_abs() as u16; + let mut num_bits = 0u8; + + while magnitude > 0 { + magnitude >>= 1; + num_bits += 1; + } + + let mask = (1 << num_bits as usize) - 1; + + let val = if coefficient < 0 { + (coefficient - 1) as u16 & mask + } else { + coefficient as u16 & mask + }; + + (num_bits, val) +} + #[inline] fn encode_coefficient(coefficient: i32) -> (u8, u16) { // since this is inlined, in the main AC case the compiler figures out that coefficient cannot be zero, so BSR on x86 doesn't need a branch