From 3e70383c6c6753d72e461b5371bd942979bdfdaf Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 1 Dec 2018 10:18:15 +1100 Subject: [PATCH] Remove some if's from deflate.decode_huffman_fast MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old speed new speed delta wuffs_deflate_decode_1k/clang 145MB/s ± 0% 151MB/s ± 1% +4.54% (p=0.008 n=5+5) wuffs_deflate_decode_10k/clang 234MB/s ± 0% 230MB/s ± 0% -1.75% (p=0.008 n=5+5) wuffs_deflate_decode_100k/clang 263MB/s ± 0% 276MB/s ± 1% +5.17% (p=0.008 n=5+5) wuffs_deflate_decode_1k/gcc 157MB/s ± 1% 157MB/s ± 0% ~ (p=0.421 n=5+5) wuffs_deflate_decode_10k/gcc 265MB/s ± 0% 259MB/s ± 0% -2.16% (p=0.008 n=5+5) wuffs_deflate_decode_100k/gcc 313MB/s ± 0% 315MB/s ± 0% +0.39% (p=0.016 n=5+5) wuffs_gzip_decode_10k/clang 216MB/s ± 0% 212MB/s ± 0% -1.69% (p=0.016 n=5+4) wuffs_gzip_decode_100k/clang 241MB/s ± 0% 252MB/s ± 0% +4.67% (p=0.016 n=5+4) wuffs_gzip_decode_10k/gcc 251MB/s ± 1% 248MB/s ± 1% -1.05% (p=0.008 n=5+5) wuffs_gzip_decode_100k/gcc 285MB/s ± 1% 291MB/s ± 0% +2.16% (p=0.008 n=5+5) wuffs_zlib_decode_10k/clang 213MB/s ± 0% 210MB/s ± 0% -1.18% (p=0.008 n=5+5) wuffs_zlib_decode_100k/clang 239MB/s ± 0% 247MB/s ± 0% +3.08% (p=0.008 n=5+5) wuffs_zlib_decode_10k/gcc 244MB/s ± 0% 247MB/s ± 0% +1.15% (p=0.008 n=5+5) wuffs_zlib_decode_100k/gcc 282MB/s ± 0% 291MB/s ± 0% +2.90% (p=0.008 n=5+5) --- release/c/wuffs-unsupported-snapshot.h | 40 ++++++++----------- script/print-deflate-magic-numbers.go | 14 +++++-- script/wuffs-deflate-decoder-decode-huffman.c | 8 ++-- std/deflate/decode_deflate.wuffs | 8 ++-- std/deflate/decode_huffman_fast.wuffs | 24 +++++------ std/deflate/decode_huffman_slow.wuffs | 13 ++++-- 6 files changed, 52 insertions(+), 55 deletions(-) diff --git a/release/c/wuffs-unsupported-snapshot.h b/release/c/wuffs-unsupported-snapshot.h index cc0ade72..1d9e390a 100644 --- a/release/c/wuffs-unsupported-snapshot.h +++ b/release/c/wuffs-unsupported-snapshot.h @@ -4885,11 +4885,11 @@ static const uint8_t wuffs_deflate__reverse8[256] = { }; static const uint32_t wuffs_deflate__lcode_magic_numbers[32] = { - 1073742592, 1073742848, 1073743104, 1073743360, 1073743616, 1073743872, - 1073744128, 1073744384, 1073744656, 1073745168, 1073745680, 1073746192, - 1073746720, 1073747744, 1073748768, 1073749792, 1073750832, 1073752880, - 1073754928, 1073756976, 1073759040, 1073763136, 1073767232, 1073771328, - 1073775440, 1073783632, 1073791824, 1073800016, 1073807872, 134217728, + 1073741824, 1073742080, 1073742336, 1073742592, 1073742848, 1073743104, + 1073743360, 1073743616, 1073743888, 1073744400, 1073744912, 1073745424, + 1073745952, 1073746976, 1073748000, 1073749024, 1073750064, 1073752112, + 1073754160, 1073756208, 1073758272, 1073762368, 1073766464, 1073770560, + 1073774672, 1073782864, 1073791056, 1073799248, 1073807104, 134217728, 134217728, 134217728, }; @@ -6147,7 +6147,7 @@ label_0_continue:; wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; goto exit; } - v_length = ((v_table_entry >> 8) & 32767); + v_length = (((v_table_entry >> 8) & 255) + 3); v_table_entry_n_bits = ((v_table_entry >> 4) & 15); if (v_table_entry_n_bits > 0) { if (v_n_bits < 15) { @@ -6160,18 +6160,14 @@ label_0_continue:; } else { } v_length = - ((v_length + - ((v_bits)&WUFFS_BASE__LOW_BITS_MASK__U32(v_table_entry_n_bits))) & - 32767); + (((v_length + 253 + + ((v_bits)&WUFFS_BASE__LOW_BITS_MASK__U32(v_table_entry_n_bits))) & + 255) + + 3); v_bits >>= v_table_entry_n_bits; v_n_bits -= v_table_entry_n_bits; } else { } - if (v_length > 258) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; - } if (v_n_bits < 15) { v_bits |= (((uint32_t)(wuffs_base__load_u8be(iop_a_src))) << v_n_bits); (iop_a_src += 1, wuffs_base__return_empty_struct()); @@ -6254,11 +6250,6 @@ label_0_continue:; if (v_length > v_hdist) { v_length -= v_hdist; v_hlen = v_hdist; - if (v_length > 258) { - status = - wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; - goto exit; - } } else { v_hlen = v_length; v_length = 0; @@ -6543,7 +6534,7 @@ wuffs_deflate__decoder__decode_huffman_slow(wuffs_deflate__decoder* self, wuffs_deflate__error__internal_error_inconsistent_huffman_decoder_state; goto exit; } - v_length = ((v_table_entry >> 8) & 32767); + v_length = (((v_table_entry >> 8) & 255) + 3); v_table_entry_n_bits = ((v_table_entry >> 4) & 15); if (v_table_entry_n_bits > 0) { while (v_n_bits < v_table_entry_n_bits) { @@ -6559,10 +6550,11 @@ wuffs_deflate__decoder__decode_huffman_slow(wuffs_deflate__decoder* self, v_bits |= (v_b2 << v_n_bits); v_n_bits += 8; } - v_length = - ((v_length + - ((v_bits)&WUFFS_BASE__LOW_BITS_MASK__U32(v_table_entry_n_bits))) & - 32767); + v_length = (((v_length + 253 + + ((v_bits)&WUFFS_BASE__LOW_BITS_MASK__U32( + v_table_entry_n_bits))) & + 255) + + 3); v_bits >>= v_table_entry_n_bits; v_n_bits -= v_table_entry_n_bits; } diff --git a/script/print-deflate-magic-numbers.go b/script/print-deflate-magic-numbers.go index 3e8f2765..042fd4fc 100644 --- a/script/print-deflate-magic-numbers.go +++ b/script/print-deflate-magic-numbers.go @@ -19,8 +19,13 @@ package main // print-deflate-magic-numbers.go prints the std/deflate lcode_magic_numbers // and dcode_magic_numbers values based on the tables in RFC 1951 secion 3.2.5. // +// The lcode base numbers are biased by -3 so that (base_number_minus_3 + +// extra_bits) fits in the range [0, 255]. This makes a bitwise-and with 0xFF a +// no-op, in terms of computed value, but proves to the compiler that the +// result is within a certain range. +// // The dcode base numbers are biased by -1 so that (base_number_minus_1 + -// extra_bits) fits in the range [0, 32767]. This makes a bitwise and with +// extra_bits) fits in the range [0, 32767]. This makes a bitwise-and with // 0x7FFF a no-op, in terms of computed value, but proves to the compiler that // the result is within a certain range. Furthermore, proving that (d + 1) > 0 // is trivial, for d of type u32[..something], compared to proving that d > 0, @@ -41,16 +46,17 @@ func main() { } func main1() error { + // See the "base numbers" comment above. + biases := [2]uint32{3, 1} + for i := 0; i < 2; i++ { - bias := uint32(0) if i != 0 { fmt.Println() - bias = 1 // See the "dcode base numbers" comment above. } for j := 0; j < 32; j++ { x := uint32(0x08000000) if bn := baseNumbers[i][j]; bn != bad { - x = 0x40000000 | ((bn - bias) << 8) | (extraBits[i][j] << 4) + x = 0x40000000 | ((bn - biases[i]) << 8) | (extraBits[i][j] << 4) } fmt.Printf("0x%08X,", x) if j&7 == 7 { diff --git a/script/wuffs-deflate-decoder-decode-huffman.c b/script/wuffs-deflate-decoder-decode-huffman.c index 5644e30d..9ef9cfb7 100644 --- a/script/wuffs-deflate-decoder-decode-huffman.c +++ b/script/wuffs-deflate-decoder-decode-huffman.c @@ -244,8 +244,8 @@ outer_loop: table_entry = self->private_impl.f_huffs[0][top + (bits & mask)]; } - // length = base_number + extra_bits. - uint32_t length = (table_entry >> 8) & 0xFFFF; + // length = base_number_minus_3 + 3 + extra_bits. + uint32_t length = ((table_entry >> 8) & 0xFF) + 3; { uint32_t n = (table_entry >> 4) & 0x0F; if (n) { @@ -290,8 +290,8 @@ outer_loop: table_entry = self->private_impl.f_huffs[1][top + (bits & mask)]; } - // dist_minus_1 = base_number_minus_1 + extra_bits + 1. - // distance = dist_minus_1 + 1. + // dist_minus_1 = base_number_minus_1 + extra_bits. + // distance = dist_minus_1 + 1. uint32_t dist_minus_1 = (table_entry >> 8) & 0xFFFF; { uint32_t n = (table_entry >> 4) & 0x0F; diff --git a/std/deflate/decode_deflate.wuffs b/std/deflate/decode_deflate.wuffs index 64e03db3..6e6ac1f3 100644 --- a/std/deflate/decode_deflate.wuffs +++ b/std/deflate/decode_deflate.wuffs @@ -41,10 +41,10 @@ pri status "?internal error: inconsistent n_bits" // Some trailing elements are 0x08000000. Bit 27 indicates an invalid value. pri const lcode_magic_numbers array[32] base.u32 = [ - 0x40000300, 0x40000400, 0x40000500, 0x40000600, 0x40000700, 0x40000800, 0x40000900, 0x40000A00, - 0x40000B10, 0x40000D10, 0x40000F10, 0x40001110, 0x40001320, 0x40001720, 0x40001B20, 0x40001F20, - 0x40002330, 0x40002B30, 0x40003330, 0x40003B30, 0x40004340, 0x40005340, 0x40006340, 0x40007340, - 0x40008350, 0x4000A350, 0x4000C350, 0x4000E350, 0x40010200, 0x08000000, 0x08000000, 0x08000000, + 0x40000000, 0x40000100, 0x40000200, 0x40000300, 0x40000400, 0x40000500, 0x40000600, 0x40000700, + 0x40000810, 0x40000A10, 0x40000C10, 0x40000E10, 0x40001020, 0x40001420, 0x40001820, 0x40001C20, + 0x40002030, 0x40002830, 0x40003030, 0x40003830, 0x40004040, 0x40005040, 0x40006040, 0x40007040, + 0x40008050, 0x4000A050, 0x4000C050, 0x4000E050, 0x4000FF00, 0x08000000, 0x08000000, 0x08000000, ] pri const dcode_magic_numbers array[32] base.u32 = [ diff --git a/std/deflate/decode_huffman_fast.wuffs b/std/deflate/decode_huffman_fast.wuffs index 221e2683..935243b8 100644 --- a/std/deflate/decode_huffman_fast.wuffs +++ b/std/deflate/decode_huffman_fast.wuffs @@ -153,8 +153,11 @@ pri func decoder.decode_huffman_fast!?(dst base.io_writer, src base.io_reader) { return "?internal error: inconsistent Huffman decoder state" } - // length = base_number + extra_bits. - var length base.u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF + // length = base_number_minus_3 + 3 + extra_bits. + // + // The -3 is from the bias in script/print-deflate-magic-numbers.go. + // That bias makes the "& 0xFF" 1 and 15-ish lines below correct. + var length base.u32[..258] = ((table_entry >> 8) & 0xFF) + 3 table_entry_n_bits = (table_entry >> 4) & 0x0F if table_entry_n_bits > 0 { // Ensure that we have at least 15 bits of input. @@ -174,16 +177,15 @@ pri func decoder.decode_huffman_fast!?(dst base.io_writer, src base.io_reader) { assert args.src.available() >= 6 assert n_bits >= 15 - length = (length + bits.low_bits(n:table_entry_n_bits)) & 0x7FFF + // The "+ 253" is the same as "- 3", after the "& 0xFF", but the + // plus form won't require an underflow check. + length = ((length + 253 + bits.low_bits(n:table_entry_n_bits)) & 0xFF) + 3 bits >>= table_entry_n_bits n_bits -= table_entry_n_bits } else { assert args.src.available() >= 6 } - if length > 258 { - return "?internal error: inconsistent Huffman decoder state" - } // Ensure that we have at least 15 bits of input. if n_bits < 15 { @@ -252,7 +254,7 @@ pri func decoder.decode_huffman_fast!?(dst base.io_writer, src base.io_reader) { // distance = dist_minus_1 + 1. // // The -1 is from the bias in script/print-deflate-magic-numbers.go. - // That bias makes the "& 0x7FFF" 15-ish lines below correct and + // That bias makes the "& 0x7FFF" 2 and 15-ish lines below correct and // undoing that bias makes proving (dist_minus_1 + 1) > 0 trivial. var dist_minus_1 base.u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF table_entry_n_bits = (table_entry >> 4) & 0x0F @@ -281,7 +283,6 @@ pri func decoder.decode_huffman_fast!?(dst base.io_writer, src base.io_reader) { // its presence minimizes the diff between decode_huffman_fast and // decode_huffman_slow. while true, - pre length <= 258, pre args.dst.available() >= 258, { // Copy from this.history. @@ -297,14 +298,9 @@ pri func decoder.decode_huffman_fast!?(dst base.io_writer, src base.io_reader) { assert hdist < 0x8000 via "a < b: a < c; c <= b"(c:length) length -= hdist hlen = hdist - // TODO: this if check should be redundant. - if length > 258 { - return "?internal error: inconsistent Huffman decoder state" - } } else { hlen = length length = 0 - assert length <= 258 } if this.history_index < hdist { return "?bad distance" @@ -324,7 +320,6 @@ pri func decoder.decode_huffman_fast!?(dst base.io_writer, src base.io_reader) { // "etc", but its presence minimizes the diff between // decode_huffman_fast and decode_huffman_slow. while true, - inv length <= 258, inv args.dst.available() >= 258, { // TODO: copy_from_slice32 should probably update the @@ -350,7 +345,6 @@ pri func decoder.decode_huffman_fast!?(dst base.io_writer, src base.io_reader) { } } // Once again, redundant but explicit assertions. - assert length <= 258 assert ((dist_minus_1 + 1) as base.u64) <= args.dst.since_mark().length() assert args.dst.available() >= 258 diff --git a/std/deflate/decode_huffman_slow.wuffs b/std/deflate/decode_huffman_slow.wuffs index d254862d..3e2c3312 100644 --- a/std/deflate/decode_huffman_slow.wuffs +++ b/std/deflate/decode_huffman_slow.wuffs @@ -101,8 +101,11 @@ pri func decoder.decode_huffman_slow!??(dst base.io_writer, src base.io_reader) return "?internal error: inconsistent Huffman decoder state" } - // length = base_number + extra_bits. - var length base.u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF + // length = base_number_minus_3 + 3 + extra_bits. + // + // The -3 is from the bias in script/print-deflate-magic-numbers.go. + // That bias makes the "& 0xFF" 1 and 15-ish lines below correct. + var length base.u32[..258] = ((table_entry >> 8) & 0xFF) + 3 table_entry_n_bits = (table_entry >> 4) & 0x0F if table_entry_n_bits > 0 { while n_bits < table_entry_n_bits, @@ -113,7 +116,9 @@ pri func decoder.decode_huffman_slow!??(dst base.io_writer, src base.io_reader) bits |= b2 << n_bits n_bits += 8 } - length = (length + bits.low_bits(n:table_entry_n_bits)) & 0x7FFF + // The "+ 253" is the same as "- 3", after the "& 0xFF", but the + // plus form won't require an underflow check. + length = ((length + 253 + bits.low_bits(n:table_entry_n_bits)) & 0xFF) + 3 bits >>= table_entry_n_bits n_bits -= table_entry_n_bits } @@ -166,7 +171,7 @@ pri func decoder.decode_huffman_slow!??(dst base.io_writer, src base.io_reader) // distance = dist_minus_1 + 1. // // The -1 is from the bias in script/print-deflate-magic-numbers.go. - // That bias makes the "& 0x7FFF" 15-ish lines below correct and + // That bias makes the "& 0x7FFF" 2 and 15-ish lines below correct and // undoing that bias makes proving (dist_minus_1 + 1) > 0 trivial. var dist_minus_1 base.u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF table_entry_n_bits = (table_entry >> 4) & 0x0F