@@ -250,6 +250,7 @@ impl<W: Write> BitWriter<W> {
250
250
self . write_bits ( code, size)
251
251
}
252
252
253
+ #[ cfg( feature = "benchmarks" ) ]
253
254
fn write_block_old (
254
255
& mut self ,
255
256
block : & [ i32 ; 64 ] ,
@@ -260,7 +261,7 @@ impl<W: Write> BitWriter<W> {
260
261
// Differential DC encoding
261
262
let dcval = block[ 0 ] ;
262
263
let diff = dcval - prevdc;
263
- let ( size, value) = encode_coefficient ( diff) ;
264
+ let ( size, value) = encode_coefficient_old ( diff) ;
264
265
265
266
self . huffman_encode ( size, dctable) ?;
266
267
self . write_bits ( value, size) ?;
@@ -277,7 +278,7 @@ impl<W: Write> BitWriter<W> {
277
278
zero_run -= 16 ;
278
279
}
279
280
280
- let ( size, value) = encode_coefficient ( block[ k as usize ] ) ;
281
+ let ( size, value) = encode_coefficient_old ( block[ k as usize ] ) ;
281
282
let symbol = ( zero_run << 4 ) | size;
282
283
283
284
self . huffman_encode ( symbol, actable) ?;
@@ -855,6 +856,27 @@ fn build_quantization_segment(m: &mut Vec<u8>, precision: u8, identifier: u8, qt
855
856
}
856
857
}
857
858
859
+ #[ cfg( feature = "benchmarks" ) ]
860
+ fn encode_coefficient_old ( coefficient : i32 ) -> ( u8 , u16 ) {
861
+ let mut magnitude = coefficient. unsigned_abs ( ) as u16 ;
862
+ let mut num_bits = 0u8 ;
863
+
864
+ while magnitude > 0 {
865
+ magnitude >>= 1 ;
866
+ num_bits += 1 ;
867
+ }
868
+
869
+ let mask = ( 1 << num_bits as usize ) - 1 ;
870
+
871
+ let val = if coefficient < 0 {
872
+ ( coefficient - 1 ) as u16 & mask
873
+ } else {
874
+ coefficient as u16 & mask
875
+ } ;
876
+
877
+ ( num_bits, val)
878
+ }
879
+
858
880
#[ inline]
859
881
fn encode_coefficient ( coefficient : i32 ) -> ( u8 , u16 ) {
860
882
// 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
0 commit comments