Files
wuffs/std/jpeg/decode_mcu_progressive_ac_high_bits.wuffs
T
Nigel Tao dd6a5d5435 std/jpeg: memoize ac_huff_table_fast
name                                             old speed     new speed     delta

wuffs_jpeg_decode_19k_8bpp/clang11               215MB/s ± 0%  226MB/s ± 0%  +5.17%  (p=0.008 n=5+5)
wuffs_jpeg_decode_30k_24bpp_progressive/clang11  209MB/s ± 0%  210MB/s ± 0%  +0.50%  (p=0.008 n=5+5)
wuffs_jpeg_decode_30k_24bpp_sequential/clang11   368MB/s ± 0%  380MB/s ± 1%  +3.11%  (p=0.008 n=5+5)
wuffs_jpeg_decode_77k_24bpp/clang11              313MB/s ± 0%  327MB/s ± 0%  +4.27%  (p=0.008 n=5+5)
wuffs_jpeg_decode_552k_24bpp_420/clang11         326MB/s ± 0%  338MB/s ± 0%  +3.50%  (p=0.008 n=5+5)
wuffs_jpeg_decode_552k_24bpp_444/clang11         462MB/s ± 0%  468MB/s ± 0%  +1.14%  (p=0.008 n=5+5)
wuffs_jpeg_decode_4002k_24bpp/clang11            329MB/s ± 1%  346MB/s ± 0%  +5.39%  (p=0.008 n=5+5)

wuffs_jpeg_decode_19k_8bpp/gcc10                 174MB/s ± 0%  175MB/s ± 0%  +0.55%  (p=0.008 n=5+5)
wuffs_jpeg_decode_30k_24bpp_progressive/gcc10    228MB/s ± 0%  248MB/s ± 0%  +8.49%  (p=0.008 n=5+5)
wuffs_jpeg_decode_30k_24bpp_sequential/gcc10     472MB/s ± 0%  478MB/s ± 0%  +1.20%  (p=0.008 n=5+5)
wuffs_jpeg_decode_77k_24bpp/gcc10                327MB/s ± 0%  328MB/s ± 0%  +0.47%  (p=0.008 n=5+5)
wuffs_jpeg_decode_552k_24bpp_420/gcc10           344MB/s ± 0%  346MB/s ± 0%  +0.46%  (p=0.016 n=4+5)
wuffs_jpeg_decode_552k_24bpp_444/gcc10           422MB/s ± 0%  424MB/s ± 0%  +0.49%  (p=0.008 n=5+5)
wuffs_jpeg_decode_4002k_24bpp/gcc10              349MB/s ± 0%  348MB/s ± 0%  -0.24%  (p=0.008 n=5+5)
2023-06-13 22:38:29 +10:00

158 lines
6.1 KiB
Plaintext

// Copyright 2023 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
pri func decoder.decode_mcu_progressive_ac_high_bits!(workbuf: slice base.u8, mx: base.u32[..= 0x1FFF], my: base.u32[..= 0x1FFF]) base.u32 {
var ret : base.u32
var bits : base.u64
var n_bits : base.u32
var r : base.io_reader
var pos : base.u32
var ac_huff_table_fast : nptr roarray[256] base.u16
var ac_h : base.u8[..= 7]
var ac_symbol : base.u32[..= 0xFF]
var ac_ht_fast : base.u32
var ac_bl : base.u32
var ac_code : base.u32
var ac_blm1 : base.u32[..= 15]
var ac_ht_slow : base.u32
var ac_value : base.u32
var ac_rrrr : base.u32[..= 15]
var ac_ssss : base.u32[..= 15]
var z : base.u32[..= 78] // 78 = 63 + 15.
if this.eob_run > 0 {
this.eob_run -= 1
return 0
}
bits = this.bitstream_bits
n_bits = this.bitstream_n_bits
if this.bitstream_ri > this.bitstream_wi {
return 2 // Internal error.
}
io_bind (io: r, data: this.bitstream_buffer[this.bitstream_ri .. this.bitstream_wi], history_position: this.bitstream_ri as base.u64) {
while.goto_done true {{
while.block true {
ac_h = this.mcu_blocks_ac_hselector[0]
ac_huff_table_fast = this.huff_tables_fast[ac_h][..] as ptr array[256] base.u16
while.ac_components true,
inv ac_huff_table_fast <> nullptr,
{
// Load at least 31 bits, by keeping 56 .. 64 loaded.
if r.length() < 8 {
ret = 1 // Request another fill_bitstream call.
break.goto_done
}
bits |= r.peek_u64be() >> (n_bits & 63)
r.skip_u32_fast!(actual: (63 - (n_bits & 63)) >> 3, worst_case: 8)
n_bits |= 56
// Read the Huffman-encoded ac_symbol, up to 16 bits long.
ac_ht_fast = ac_huff_table_fast[bits >> 56] as base.u32
ac_bl = ac_ht_fast >> 8
if n_bits >= ac_bl {
ac_symbol = 0xFF & ac_ht_fast
bits ~mod<<= (ac_bl & 63)
n_bits -= ac_bl
} else {
ac_code = (bits >> 55) as base.u32
ac_blm1 = 8
bits ~mod<<= 9
n_bits ~mod-= 9
while true,
inv ac_huff_table_fast <> nullptr,
{
ac_ht_slow = this.huff_tables_slow[ac_h][ac_blm1]
if ac_code < (ac_ht_slow >> 8) {
ac_symbol = this.huff_tables_symbols[ac_h][0xFF & (ac_code ~mod+ ac_ht_slow)] as base.u32
break
}
ac_code = (ac_code ~mod<< 1) | ((bits >> 63) as base.u32)
bits ~mod<<= 1
n_bits ~mod-= 1
ac_blm1 = (ac_blm1 + 1) & 15
if ac_blm1 == 0 {
ac_symbol = 0
break
}
} endwhile
}
// Split the 8-bit ac_symbol into two 4-bit halves, per section
// F.2.2.2 "Decoding procedure for AC coefficients".
ac_rrrr = ac_symbol >> 4
ac_ssss = ac_symbol & 15
// Process the ac_value in the next ac_ssss (up to 15) bits or
// the eob_run in the next ac_rrrr (up to 15) bits.
if ac_ssss > 0 {
ac_value = ((bits >> (64 - ac_ssss)) & 0xFFFF_FFFF) as base.u32
if (bits >> 63) == 0 { // EXTEND per section F.2.2.1.
ac_value ~mod+= 1 ~mod+ ((0xFFFF_FFFF as base.u32) ~mod<< ac_ssss)
}
bits ~mod<<= ac_ssss
n_bits ~mod-= ac_ssss
z = this.mcu_zig_index + ac_rrrr
this.mcu_blocks[0][UNZIG[z]] =
((ac_value ~mod<< this.scan_al) & 0xFFFF) as base.u16
if (z + 1) > (this.scan_se as base.u32) {
break.ac_components
}
assert (z + 1) <= 63 via "a <= b: a <= c; c <= b"(c: (this.scan_se as base.u32))
this.mcu_zig_index = z + 1
} else if ac_rrrr < 15 {
this.eob_run = (((1 as base.u16) << ac_rrrr) - 1) as base.u16
if ac_rrrr > 0 {
this.eob_run ~mod+= ((bits >> (64 - ac_rrrr)) & 0xFFFF) as base.u16
bits ~mod<<= ac_rrrr
n_bits ~mod-= ac_rrrr
}
break.ac_components
} else if (this.mcu_zig_index + 16) > (this.scan_se as base.u32) {
break.ac_components
} else {
assert (this.mcu_zig_index + 16) <= 63 via "a <= b: a <= c; c <= b"(c: (this.scan_se as base.u32))
this.mcu_zig_index = this.mcu_zig_index + 16
}
} endwhile.ac_components
this.mcu_zig_index = 0
break.block
} endwhile.block
break.goto_done
}} endwhile.goto_done
pos = (r.position() & 0xFFFF_FFFF) as base.u32
if pos > this.bitstream_wi {
ret = 2 // Internal error.
} else {
assert pos <= 0x800 via "a <= b: a <= c; c <= b"(c: this.bitstream_wi)
this.bitstream_ri = pos
}
}
this.bitstream_bits = bits
this.bitstream_n_bits = n_bits
return ret
}