mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-20 00:47:28 +01:00
This means that they are not zero'ed by the initialize methods, which improves some benchmark numbers. There is a trade-off here, as bugs in the *.wuffs code could then mean that we are potentially reading uninitialized memory (bulk data fields). Nonetheless, on some micro-benchmarks, the relative performance gain on skipping this fixed cost is stark, up to +96% throughput, or equivalently, -49% in time per operation. Callers of the Wuffs libraries have the option to zero-initialize the entire state (and then pass WUFFS_INITIALIZE__ALREADY_ZEROED) or to zero-initialize private_impl state but not private_data state (i.e. skip the bulk data fields). The numbers below are in two sections: throughput (e.g. MB/s) and time per operation. Relatively large gains in throughput (e.g. +96%) can correspond to small absolute gains (e.g. it's the difference between an operation taking 2.02µs instead of 3.98µs). -------- name old speed new speed delta wuffs_adler32_10k/clang5 2.43GB/s ± 0% 2.44GB/s ± 0% +0.13% (p=0.000 n=47+48) wuffs_adler32_100k/clang5 2.44GB/s ± 0% 2.44GB/s ± 1% +0.10% (p=0.001 n=45+47) wuffs_adler32_10k/gcc7 3.24GB/s ± 0% 3.24GB/s ± 0% ~ (p=0.933 n=47+46) wuffs_adler32_100k/gcc7 3.25GB/s ± 0% 3.25GB/s ± 1% ~ (p=0.551 n=42+47) wuffs_crc32_ieee_10k/clang5 2.87GB/s ± 0% 2.87GB/s ± 0% ~ (p=0.519 n=45+46) wuffs_crc32_ieee_100k/clang5 2.89GB/s ± 0% 2.89GB/s ± 1% ~ (p=0.479 n=45+45) wuffs_crc32_ieee_10k/gcc7 3.35GB/s ± 0% 3.38GB/s ± 0% +0.95% (p=0.000 n=44+49) wuffs_crc32_ieee_100k/gcc7 3.39GB/s ± 1% 3.42GB/s ± 0% +0.96% (p=0.000 n=47+47) wuffs_deflate_decode_1k/clang5 139MB/s ± 1% 170MB/s ± 1% +22.72% (p=0.000 n=48+47) wuffs_deflate_decode_10k/clang5 232MB/s ± 1% 241MB/s ± 1% +4.13% (p=0.000 n=48+45) wuffs_deflate_decode_100k_just_one_read/clang5 274MB/s ± 0% 277MB/s ± 1% +1.14% (p=0.000 n=47+45) wuffs_deflate_decode_100k_many_big_reads/clang5 226MB/s ± 0% 226MB/s ± 1% ~ (p=0.057 n=48+47) wuffs_deflate_decode_1k/gcc7 157MB/s ± 1% 198MB/s ± 1% +25.61% (p=0.000 n=45+45) wuffs_deflate_decode_10k/gcc7 265MB/s ± 0% 272MB/s ± 1% +2.62% (p=0.000 n=47+47) wuffs_deflate_decode_100k_just_one_read/gcc7 322MB/s ± 0% 323MB/s ± 1% +0.40% (p=0.000 n=46+45) wuffs_deflate_decode_100k_many_big_reads/gcc7 254MB/s ± 0% 254MB/s ± 0% +0.23% (p=0.000 n=43+46) wuffs_gif_decode_1k_bw/clang5 246MB/s ± 1% 448MB/s ± 0% +82.21% (p=0.000 n=48+44) wuffs_gif_decode_1k_color/clang5 145MB/s ± 0% 194MB/s ± 1% +33.70% (p=0.000 n=48+48) wuffs_gif_decode_10k_bgra/clang5 740MB/s ± 1% 747MB/s ± 0% +0.97% (p=0.000 n=49+45) wuffs_gif_decode_10k_indexed/clang5 199MB/s ± 1% 201MB/s ± 0% +1.30% (p=0.000 n=46+45) wuffs_gif_decode_20k/clang5 251MB/s ± 0% 251MB/s ± 0% +0.23% (p=0.000 n=44+44) wuffs_gif_decode_100k_artificial/clang5 557MB/s ± 1% 557MB/s ± 1% ~ (p=0.653 n=45+47) wuffs_gif_decode_100k_realistic/clang5 230MB/s ± 0% 229MB/s ± 0% -0.45% (p=0.000 n=45+46) wuffs_gif_decode_1000k/clang5 236MB/s ± 0% 233MB/s ± 0% -0.91% (p=0.000 n=46+48) wuffs_gif_decode_anim_screencap/clang5 1.12GB/s ± 1% 1.11GB/s ± 0% -0.74% (p=0.000 n=47+45) wuffs_gif_decode_1k_bw/gcc7 257MB/s ± 1% 506MB/s ± 0% +96.52% (p=0.000 n=50+46) wuffs_gif_decode_1k_color/gcc7 146MB/s ± 1% 199MB/s ± 1% +36.35% (p=0.000 n=48+47) wuffs_gif_decode_10k_bgra/gcc7 632MB/s ± 1% 649MB/s ± 0% +2.80% (p=0.000 n=47+47) wuffs_gif_decode_10k_indexed/gcc7 198MB/s ± 1% 204MB/s ± 1% +3.32% (p=0.000 n=48+47) wuffs_gif_decode_20k/gcc7 242MB/s ± 0% 246MB/s ± 0% +1.59% (p=0.000 n=47+48) wuffs_gif_decode_100k_artificial/gcc7 517MB/s ± 1% 521MB/s ± 0% +0.71% (p=0.000 n=46+46) wuffs_gif_decode_100k_realistic/gcc7 214MB/s ± 0% 215MB/s ± 0% +0.34% (p=0.000 n=48+44) wuffs_gif_decode_1000k/gcc7 218MB/s ± 1% 218MB/s ± 0% ~ (p=0.238 n=44+49) wuffs_gif_decode_anim_screencap/gcc7 1.08GB/s ± 0% 1.07GB/s ± 1% -0.27% (p=0.000 n=45+45) wuffs_gzip_decode_10k/clang5 213MB/s ± 0% 222MB/s ± 0% +4.08% (p=0.000 n=43+46) wuffs_gzip_decode_100k/clang5 249MB/s ± 1% 253MB/s ± 0% +1.50% (p=0.000 n=47+46) wuffs_gzip_decode_10k/gcc7 246MB/s ± 1% 258MB/s ± 0% +4.76% (p=0.000 n=48+47) wuffs_gzip_decode_100k/gcc7 291MB/s ± 0% 291MB/s ± 0% ~ (p=0.679 n=48+48) wuffs_lzw_decode_20k/clang5 274MB/s ± 0% 280MB/s ± 0% +2.51% (p=0.000 n=46+47) wuffs_lzw_decode_100k/clang5 484MB/s ± 0% 487MB/s ± 0% +0.58% (p=0.000 n=46+49) wuffs_lzw_decode_20k/gcc7 267MB/s ± 0% 274MB/s ± 2% +2.31% (p=0.000 n=41+43) wuffs_lzw_decode_100k/gcc7 489MB/s ± 1% 486MB/s ± 2% -0.67% (p=0.000 n=47+42) wuffs_zlib_decode_10k/clang5 212MB/s ± 1% 219MB/s ± 0% +3.66% (p=0.000 n=47+47) wuffs_zlib_decode_100k/clang5 246MB/s ± 1% 248MB/s ± 0% +0.69% (p=0.000 n=47+46) wuffs_zlib_decode_10k/gcc7 251MB/s ± 0% 255MB/s ± 0% +1.83% (p=0.000 n=45+44) wuffs_zlib_decode_100k/gcc7 294MB/s ± 1% 290MB/s ± 0% -1.33% (p=0.000 n=43+42) -------- name old time/op new time/op delta wuffs_adler32_10k/clang5 4.54µs ± 1% 4.54µs ± 0% -0.15% (p=0.000 n=48+48) wuffs_adler32_100k/clang5 41.0µs ± 0% 41.0µs ± 1% -0.10% (p=0.001 n=45+47) wuffs_adler32_10k/gcc7 3.41µs ± 0% 3.41µs ± 0% ~ (p=0.890 n=47+46) wuffs_adler32_100k/gcc7 30.8µs ± 0% 30.8µs ± 1% ~ (p=0.557 n=42+47) wuffs_crc32_ieee_10k/clang5 3.86µs ± 0% 3.86µs ± 0% ~ (p=0.602 n=45+46) wuffs_crc32_ieee_100k/clang5 34.6µs ± 0% 34.6µs ± 1% ~ (p=0.475 n=45+45) wuffs_crc32_ieee_10k/gcc7 3.30µs ± 0% 3.27µs ± 0% -0.94% (p=0.000 n=45+49) wuffs_crc32_ieee_100k/gcc7 29.5µs ± 1% 29.3µs ± 0% -0.95% (p=0.000 n=47+47) wuffs_deflate_decode_1k/clang5 6.79µs ± 1% 5.54µs ± 1% -18.52% (p=0.000 n=48+47) wuffs_deflate_decode_10k/clang5 47.8µs ± 1% 45.9µs ± 1% -3.96% (p=0.000 n=48+45) wuffs_deflate_decode_100k_just_one_read/clang5 365µs ± 0% 361µs ± 1% -1.12% (p=0.000 n=47+45) wuffs_deflate_decode_100k_many_big_reads/clang5 442µs ± 0% 443µs ± 1% ~ (p=0.057 n=48+47) wuffs_deflate_decode_1k/gcc7 5.99µs ± 1% 4.77µs ± 1% -20.39% (p=0.000 n=45+45) wuffs_deflate_decode_10k/gcc7 41.7µs ± 0% 40.6µs ± 1% -2.56% (p=0.000 n=47+47) wuffs_deflate_decode_100k_just_one_read/gcc7 311µs ± 0% 310µs ± 1% -0.40% (p=0.000 n=46+45) wuffs_deflate_decode_100k_many_big_reads/gcc7 394µs ± 0% 393µs ± 0% -0.23% (p=0.000 n=43+46) wuffs_gif_decode_1k_bw/clang5 4.17µs ± 1% 2.29µs ± 0% -45.12% (p=0.000 n=46+44) wuffs_gif_decode_1k_color/clang5 6.95µs ± 0% 5.20µs ± 1% -25.21% (p=0.000 n=48+48) wuffs_gif_decode_10k_bgra/clang5 54.5µs ± 1% 54.0µs ± 0% -0.96% (p=0.000 n=49+45) wuffs_gif_decode_10k_indexed/clang5 50.7µs ± 1% 50.1µs ± 0% -1.28% (p=0.000 n=46+45) wuffs_gif_decode_20k/clang5 76.6µs ± 0% 76.4µs ± 0% -0.23% (p=0.000 n=44+44) wuffs_gif_decode_100k_artificial/clang5 248µs ± 1% 248µs ± 1% ~ (p=0.653 n=45+47) wuffs_gif_decode_100k_realistic/clang5 599µs ± 0% 602µs ± 0% +0.45% (p=0.000 n=45+46) wuffs_gif_decode_1000k/clang5 4.25ms ± 0% 4.29ms ± 0% +0.92% (p=0.000 n=46+48) wuffs_gif_decode_anim_screencap/clang5 4.17ms ± 1% 4.20ms ± 0% +0.74% (p=0.000 n=47+45) wuffs_gif_decode_1k_bw/gcc7 3.98µs ± 1% 2.02µs ± 0% -49.12% (p=0.000 n=50+46) wuffs_gif_decode_1k_color/gcc7 6.90µs ± 1% 5.06µs ± 0% -26.67% (p=0.000 n=48+46) wuffs_gif_decode_10k_bgra/gcc7 63.8µs ± 1% 62.1µs ± 0% -2.73% (p=0.000 n=47+47) wuffs_gif_decode_10k_indexed/gcc7 50.9µs ± 1% 49.3µs ± 1% -3.22% (p=0.000 n=48+47) wuffs_gif_decode_20k/gcc7 79.3µs ± 0% 78.1µs ± 0% -1.57% (p=0.000 n=47+48) wuffs_gif_decode_100k_artificial/gcc7 267µs ± 0% 265µs ± 0% -0.69% (p=0.000 n=45+46) wuffs_gif_decode_100k_realistic/gcc7 643µs ± 0% 641µs ± 0% -0.34% (p=0.000 n=48+44) wuffs_gif_decode_1000k/gcc7 4.59ms ± 1% 4.58ms ± 0% ~ (p=0.238 n=44+49) wuffs_gif_decode_anim_screencap/gcc7 4.32ms ± 0% 4.33ms ± 1% +0.27% (p=0.000 n=45+45) wuffs_gzip_decode_10k/clang5 51.9µs ± 0% 49.8µs ± 0% -3.92% (p=0.000 n=43+46) wuffs_gzip_decode_100k/clang5 401µs ± 1% 395µs ± 0% -1.48% (p=0.000 n=47+46) wuffs_gzip_decode_10k/gcc7 44.9µs ± 1% 42.9µs ± 0% -4.54% (p=0.000 n=48+47) wuffs_gzip_decode_100k/gcc7 343µs ± 0% 343µs ± 0% ~ (p=0.679 n=48+48) wuffs_lzw_decode_20k/clang5 70.2µs ± 0% 68.5µs ± 0% -2.45% (p=0.000 n=46+47) wuffs_lzw_decode_100k/clang5 207µs ± 0% 206µs ± 0% -0.58% (p=0.000 n=46+49) wuffs_lzw_decode_20k/gcc7 71.8µs ± 0% 70.2µs ± 2% -2.25% (p=0.000 n=41+43) wuffs_lzw_decode_100k/gcc7 204µs ± 1% 206µs ± 2% +0.67% (p=0.000 n=47+42) wuffs_zlib_decode_10k/clang5 52.3µs ± 1% 50.4µs ± 0% -3.53% (p=0.000 n=47+47) wuffs_zlib_decode_100k/clang5 406µs ± 1% 404µs ± 0% -0.68% (p=0.000 n=47+46) wuffs_zlib_decode_10k/gcc7 44.2µs ± 0% 43.4µs ± 0% -1.79% (p=0.000 n=45+44) wuffs_zlib_decode_100k/gcc7 340µs ± 1% 345µs ± 0% +1.35% (p=0.000 n=43+42) --------
361 lines
10 KiB
Plaintext
361 lines
10 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"
|
|
|
|
pri status "?internal error: inconsistent I/O"
|
|
|
|
// TODO: move bulk data buffers like decoder.suffixes or decoder.output into
|
|
// the workbuf? The first attempt at this was a performance regression for
|
|
// decoding all but the smallest GIFs. See these git commits for numbers:
|
|
// - 49627b4 Flatten the lzw.decoder.suffixes array
|
|
// - f877fb2 Use the workbuf instead of lzw.decoder.suffixes
|
|
// - 85be5b9 Delete the obsolete lzw.decoder.suffixes array
|
|
// and the roll back has combined numbers:
|
|
// - 3056a84 Roll back 3 recent lzw.decoder.suffixes commits
|
|
pub const decoder_workbuf_len_max_incl_worst_case base.u64 = 0
|
|
|
|
pub struct decoder?(
|
|
// set_literal_width_arg is the saved argument passed to set_literal_width.
|
|
// This field is copied to the literal_width field at the start of
|
|
// decode_io_writer. During that method, calling set_literal_width will
|
|
// change set_literal_width_arg but not literal_width.
|
|
set_literal_width_arg base.u32[..8],
|
|
|
|
// read_from state that does not change during a decode call.
|
|
literal_width base.u32[..8],
|
|
clear_code base.u32[..256],
|
|
end_code base.u32[..257],
|
|
|
|
// read_from state that does change during a decode call.
|
|
save_code base.u32[..4096],
|
|
prev_code base.u32[..4095],
|
|
width base.u32[..12],
|
|
bits base.u32,
|
|
n_bits base.u32[..31],
|
|
output_ri base.u32[..8191],
|
|
output_wi base.u32[..8191],
|
|
|
|
// read_from return value. The read_from method effectively returns a
|
|
// base.u32 to show how decode should continue after calling write_to. That
|
|
// value needs to be saved across write_to's possible suspension, so we
|
|
// might as well save it explicitly as a decoder field.
|
|
read_from_return_value base.u32,
|
|
|
|
util base.utility,
|
|
)(
|
|
// read_from per-code state.
|
|
suffixes array[4096] array[8] base.u8,
|
|
prefixes array[4096] base.u16[..4095],
|
|
// lm1s is the "length minus 1"s of the values for the implicit key-value
|
|
// table in this decoder. See std/lzw/README.md for more detail.
|
|
lm1s array[4096] base.u16[..4095],
|
|
|
|
// output[output_ri:output_wi] is the buffered output, connecting read_from
|
|
// with write_to and flush.
|
|
output array[8192 + 7] base.u8,
|
|
)
|
|
|
|
pub func decoder.set_literal_width!(lw base.u32[2..8]) {
|
|
this.set_literal_width_arg = args.lw
|
|
}
|
|
|
|
pub func decoder.workbuf_len() base.range_ii_u64 {
|
|
return this.util.make_range_ii_u64(min_incl:0, max_incl:0)
|
|
}
|
|
|
|
pub func decoder.decode_io_writer?(dst base.io_writer, src base.io_reader, workbuf slice base.u8) {
|
|
var i base.u32[..8191]
|
|
|
|
// Initialize read_from state.
|
|
this.literal_width = 8
|
|
if this.set_literal_width_arg >= 2 {
|
|
this.literal_width = this.set_literal_width_arg
|
|
}
|
|
this.clear_code = (1 as base.u32) << this.literal_width
|
|
this.end_code = this.clear_code + 1
|
|
this.save_code = this.end_code
|
|
this.prev_code = this.end_code
|
|
this.width = this.literal_width + 1
|
|
this.bits = 0
|
|
this.n_bits = 0
|
|
this.output_ri = 0
|
|
this.output_wi = 0
|
|
i = 0
|
|
while i < this.clear_code {
|
|
assert i < 256 via "a < b: a < c; c <= b"(c:this.clear_code)
|
|
this.lm1s[i] = 0
|
|
this.suffixes[i][0] = i as base.u8
|
|
i += 1
|
|
}
|
|
|
|
while true {
|
|
this.read_from!(src:args.src)
|
|
|
|
if this.output_wi > 0 {
|
|
this.write_to?(dst:args.dst)
|
|
}
|
|
|
|
if this.read_from_return_value == 0 {
|
|
break
|
|
} else if this.read_from_return_value == 1 {
|
|
continue
|
|
} else if this.read_from_return_value == 2 {
|
|
yield base."$short read"
|
|
} else if this.read_from_return_value == 3 {
|
|
return "?bad code"
|
|
} else {
|
|
return "?internal error: inconsistent I/O"
|
|
}
|
|
}
|
|
}
|
|
|
|
pri func decoder.read_from!(src base.io_reader) {
|
|
var clear_code base.u32[..256]
|
|
var end_code base.u32[..257]
|
|
|
|
var save_code base.u32[..4096]
|
|
var prev_code base.u32[..4095]
|
|
var width base.u32[..12]
|
|
var bits base.u32
|
|
var n_bits base.u32[..31]
|
|
var output_wi base.u32[..8191]
|
|
|
|
var code base.u32[..4095]
|
|
var c base.u32[..4095]
|
|
var o base.u32[..8191]
|
|
var steps base.u32
|
|
var first_byte base.u8
|
|
var lm1_b base.u16[..4095]
|
|
var lm1_a base.u16[..4095]
|
|
|
|
clear_code = this.clear_code
|
|
end_code = this.end_code
|
|
|
|
save_code = this.save_code
|
|
prev_code = this.prev_code
|
|
width = this.width
|
|
bits = this.bits
|
|
n_bits = this.n_bits
|
|
output_wi = this.output_wi
|
|
|
|
while true {
|
|
if n_bits < width {
|
|
assert n_bits < 12 via "a < b: a < c; c <= b"(c:width)
|
|
if args.src.available() >= 4 {
|
|
// Read 4 bytes, using the "Variant 4" technique of
|
|
// https://fgiesen.wordpress.com/2018/02/20/reading-bits-in-far-too-many-ways-part-2/
|
|
bits |= args.src.peek_u32le() ~mod<< n_bits
|
|
args.src.skip_fast!(actual:(31 - n_bits) >> 3, worst_case:3)
|
|
n_bits |= 24
|
|
assert width <= n_bits via "a <= b: a <= c; c <= b"(c:12)
|
|
assert n_bits >= width via "a >= b: b <= a"()
|
|
} else if args.src.available() <= 0 {
|
|
this.read_from_return_value = 2
|
|
break
|
|
} else {
|
|
bits |= args.src.peek_u8_as_u32() << n_bits
|
|
args.src.skip_fast!(actual:1, worst_case:1)
|
|
n_bits += 8
|
|
if n_bits >= width {
|
|
// No-op.
|
|
} else if args.src.available() <= 0 {
|
|
this.read_from_return_value = 2
|
|
break
|
|
} else {
|
|
bits |= args.src.peek_u8_as_u32() << n_bits
|
|
args.src.skip_fast!(actual:1, worst_case:1)
|
|
n_bits += 8
|
|
assert width <= n_bits via "a <= b: a <= c; c <= b"(c:12)
|
|
assert n_bits >= width via "a >= b: b <= a"()
|
|
|
|
// This if condition is always false, but for some unknown
|
|
// reason, removing it worsens the benchmarks slightly.
|
|
if n_bits < width {
|
|
this.read_from_return_value = 4
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
code = 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)
|
|
this.output[output_wi] = code as base.u8
|
|
output_wi = (output_wi + 1) & 8191
|
|
if save_code <= 4095 {
|
|
lm1_a = (this.lm1s[prev_code] + 1) & 4095
|
|
this.lm1s[save_code] = lm1_a
|
|
|
|
if (lm1_a % 8) != 0 {
|
|
this.prefixes[save_code] = this.prefixes[prev_code]
|
|
this.suffixes[save_code] = this.suffixes[prev_code]
|
|
this.suffixes[save_code][lm1_a % 8] = code as base.u8
|
|
} else {
|
|
this.prefixes[save_code] = prev_code as base.u16
|
|
this.suffixes[save_code][0] = code as base.u8
|
|
}
|
|
|
|
save_code += 1
|
|
if width < 12 {
|
|
width += 1 & (save_code >> width)
|
|
}
|
|
prev_code = code
|
|
}
|
|
|
|
} else if code <= end_code {
|
|
if code == end_code {
|
|
this.read_from_return_value = 0
|
|
break
|
|
}
|
|
save_code = end_code
|
|
prev_code = end_code
|
|
width = this.literal_width + 1
|
|
|
|
} else if code <= save_code {
|
|
c = code
|
|
if code == save_code {
|
|
c = prev_code
|
|
}
|
|
|
|
// Letting old_wi and new_wi denote the values of output_wi before
|
|
// and after these two lines of code, the decoded bytes will be
|
|
// written to output[old_wi:new_wi]. They will be written
|
|
// back-to-front, 8 bytes at a time, starting by writing
|
|
// output[o:o + 8], which will contain output[new_wi - 1].
|
|
//
|
|
// In the special case that code == save_code, the decoded bytes
|
|
// contain an extra copy (at the end) of the first byte, and will
|
|
// be written to output[old_wi:new_wi + 1].
|
|
o = (output_wi + ((this.lm1s[c] as base.u32) & 0xFFFFFFF8)) & 8191
|
|
output_wi = (output_wi + 1 + (this.lm1s[c] as base.u32)) & 8191
|
|
|
|
steps = (this.lm1s[c] as base.u32) >> 3
|
|
while true {
|
|
assert o <= (o + 8) via "a <= (a + b): 0 <= b"(b:8)
|
|
|
|
// The final "8" is redundant semantically, but helps the
|
|
// wuffs-c code generator recognize that both slices have the
|
|
// same constant length, and hence produce efficient C code.
|
|
this.output[o:o + 8].copy_from_slice!(s:this.suffixes[c][:8])
|
|
|
|
if steps <= 0 {
|
|
break
|
|
}
|
|
steps -= 1
|
|
|
|
// This line is essentially "o -= 8". The "& 8191" is a no-op
|
|
// in practice, but is necessary for the overflow checker.
|
|
o = (o ~mod- 8) & 8191
|
|
c = this.prefixes[c] as base.u32
|
|
}
|
|
first_byte = this.suffixes[c][0]
|
|
|
|
if code == save_code {
|
|
this.output[output_wi] = first_byte
|
|
output_wi = (output_wi + 1) & 8191
|
|
}
|
|
|
|
if save_code <= 4095 {
|
|
lm1_b = (this.lm1s[prev_code] + 1) & 4095
|
|
this.lm1s[save_code] = lm1_b
|
|
|
|
if (lm1_b % 8) != 0 {
|
|
this.prefixes[save_code] = this.prefixes[prev_code]
|
|
this.suffixes[save_code] = this.suffixes[prev_code]
|
|
this.suffixes[save_code][lm1_b % 8] = first_byte
|
|
} else {
|
|
this.prefixes[save_code] = prev_code as base.u16
|
|
this.suffixes[save_code][0] = first_byte as base.u8
|
|
}
|
|
|
|
save_code += 1
|
|
if width < 12 {
|
|
width += 1 & (save_code >> width)
|
|
}
|
|
prev_code = code
|
|
}
|
|
|
|
} else {
|
|
this.read_from_return_value = 3
|
|
break
|
|
}
|
|
|
|
// Flush the output if it could be too full to contain the entire
|
|
// decoding of the next code. The longest possible decoding is slightly
|
|
// less than 4096 and output's length is 8192, so a conservative
|
|
// threshold is ensuring that output_wi <= 4095.
|
|
if output_wi > 4095 {
|
|
this.read_from_return_value = 1
|
|
break
|
|
}
|
|
}
|
|
|
|
// Rewind args.src, if we're not in "$short read" and we've read too many
|
|
// bits.
|
|
if this.read_from_return_value != 2 {
|
|
while n_bits >= 8 {
|
|
n_bits -= 8
|
|
if args.src.can_undo_byte() {
|
|
args.src.undo_byte!()
|
|
} else {
|
|
this.read_from_return_value = 4
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
this.save_code = save_code
|
|
this.prev_code = prev_code
|
|
this.width = width
|
|
this.bits = bits
|
|
this.n_bits = n_bits
|
|
this.output_wi = output_wi
|
|
}
|
|
|
|
pri func decoder.write_to?(dst base.io_writer) {
|
|
var s slice base.u8
|
|
var n base.u64
|
|
|
|
while this.output_wi > 0 {
|
|
if this.output_ri > this.output_wi {
|
|
return "?internal error: inconsistent I/O"
|
|
}
|
|
s = this.output[this.output_ri:this.output_wi]
|
|
n = args.dst.copy_from_slice!(s:s)
|
|
if n == s.length() {
|
|
this.output_ri = 0
|
|
this.output_wi = 0
|
|
return ok
|
|
}
|
|
this.output_ri = (this.output_ri ~mod+ ((n & 0xFFFFFFFF) as base.u32)) & 8191
|
|
yield base."$short write"
|
|
}
|
|
}
|
|
|
|
pub func decoder.flush!() slice base.u8 {
|
|
var s slice base.u8
|
|
|
|
if this.output_ri <= this.output_wi {
|
|
s = this.output[this.output_ri:this.output_wi]
|
|
}
|
|
this.output_ri = 0
|
|
this.output_wi = 0
|
|
return s
|
|
}
|