Files
wuffs/std/lzw/decode_lzw.wuffs
T
Nigel Tao cae1bfbc7d Remove LZW "cyclical prefix chain" internal error
name                             old speed     new speed     delta

wuffs_gif_decode_1k_bw/clang     289MB/s ± 1%  309MB/s ± 0%   +6.94%  (p=0.000 n=50+5)
wuffs_gif_decode_1k_color/clang  159MB/s ± 1%  160MB/s ± 0%     ~     (p=0.080 n=48+5)
wuffs_gif_decode_10k/clang       145MB/s ± 1%  145MB/s ± 0%     ~     (p=0.371 n=49+5)
wuffs_gif_decode_100k/clang      125MB/s ± 0%  131MB/s ± 1%   +4.84%  (p=0.000 n=46+5)
wuffs_gif_decode_1000k/clang     128MB/s ± 1%  134MB/s ± 0%   +5.22%  (p=0.000 n=47+5)

wuffs_gif_decode_1k_bw/gcc       341MB/s ± 1%  339MB/s ± 0%   -0.48%  (p=0.001 n=49+5)
wuffs_gif_decode_1k_color/gcc    184MB/s ± 1%  186MB/s ± 0%   +1.10%  (p=0.000 n=50+5)
wuffs_gif_decode_10k/gcc         162MB/s ± 0%  164MB/s ± 0%   +1.76%  (p=0.000 n=50+5)
wuffs_gif_decode_100k/gcc        139MB/s ± 0%  143MB/s ± 0%   +3.49%  (p=0.000 n=49+5)
wuffs_gif_decode_1000k/gcc       142MB/s ± 0%  146MB/s ± 0%   +3.08%  (p=0.000 n=49+5)

wuffs_lzw_decode_20k/clang       132MB/s ± 0%  142MB/s ± 0%   +7.91%  (p=0.000 n=46+5)
wuffs_lzw_decode_100k/clang      170MB/s ± 0%  186MB/s ± 1%   +9.18%  (p=0.000 n=46+5)

wuffs_lzw_decode_20k/gcc         156MB/s ± 0%  162MB/s ± 0%   +3.74%  (p=0.000 n=47+5)
wuffs_lzw_decode_100k/gcc        214MB/s ± 0%  235MB/s ± 1%  +10.03%  (p=0.000 n=47+5)
2018-10-14 11:06:38 +11:00

140 lines
3.7 KiB
Plaintext

// Copyright 2017 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.
pub status "?bad code"
pub struct decoder?(
literal_width base.u32[..8],
stack array[4096] base.u8,
suffixes array[4096] base.u8,
prefixes array[4096] base.u16[..4095],
)
pub func decoder.set_literal_width!(lw base.u32[2..8]) {
this.literal_width = args.lw
}
pub func decoder.decode!??(dst base.io_writer, src base.io_reader) {
// These variables don't change over the lifetime of this func.
var literal_width base.u32[2..8] = 8
if this.literal_width >= 2 {
literal_width = this.literal_width
}
var clear_code base.u32[4..256] = (1 as base.u32) << literal_width
var end_code base.u32[5..257] = clear_code + 1
// These variables do change.
//
// save_code is the code for which, after decoding a code, we save what the
// next back-reference expands to. The README.md file also calls this value
// `max`. 4096 means do not save.
var save_code base.u32[..4096] = end_code
var prev_code base.u32[..4095]
var width base.u32[..12] = literal_width + 1
// These variables yield src's bits in Least Significant Bits order.
var bits base.u32
var n_bits base.u32
while true,
pre n_bits < 8,
{
assert n_bits < (width + 8) via "a < (b + c): a < c; 0 <= b"()
while n_bits < width,
inv n_bits < (width + 8),
post n_bits >= width,
{
assert n_bits < 12 via "a < b: a < c; c <= b"(c:width)
bits |= (args.src.read_u8!??() as base.u32) << n_bits
n_bits += 8
}
var code base.u32[..4095] = bits.low_bits(n:width)
bits >>= width
n_bits -= width
if code < clear_code {
assert code < 256 via "a < b: a < c; c <= b"(c:clear_code)
args.dst.write_u8!??(x:code as base.u8)
if save_code <= 4095 {
this.suffixes[save_code] = code as base.u8
this.prefixes[save_code] = prev_code as base.u16
save_code += 1
if (save_code == ((1 as base.u32) << width)) and (width < 12) {
width += 1
}
prev_code = code
}
} else if code <= end_code {
if code == end_code {
return
}
save_code = end_code
prev_code = 0
width = literal_width + 1
} else if code <= save_code {
var s base.u32[..4095] = 4095
var c base.u32[..4095] = code
if code == save_code {
s -= 1
c = prev_code
}
while c >= clear_code,
inv n_bits < 8,
post c < 256 via "a < b: a < c; c <= b"(c:clear_code),
{
this.stack[s] = this.suffixes[c]
// The bitwise-and with 4095 is algorithmically a no-op, but is
// necessary for the overflow checker.
s = (s ~mod- 1) & 4095
c = this.prefixes[c] as base.u32
}
this.stack[s] = c as base.u8
if code == save_code {
this.stack[4095] = c as base.u8
}
while true,
inv n_bits < 8,
inv c < 256,
{
var expansion slice base.u8 = this.stack[s:]
var n_copied base.u64 = args.dst.copy_from_slice!(s:expansion)
if n_copied == expansion.length() {
break
}
s = (s + ((n_copied & 4095) as base.u32)) & 4095
yield status "$short write"
}
if save_code <= 4095 {
this.suffixes[save_code] = c as base.u8
this.prefixes[save_code] = prev_code as base.u16
save_code += 1
if (save_code == ((1 as base.u32) << width)) and (width < 12) {
width += 1
}
prev_code = code
}
} else {
return status "?bad code"
}
}
}