From 26445adcdfad86dcc22e61ce6152ddb5e2b70ed7 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 23 Mar 2018 12:54:46 +1100 Subject: [PATCH] Rename deflate's "distance minus 1" variable --- gen/c/std/deflate.c | 40 +++++++++++++-------------- gen/c/std/zlib.c | 2 +- gen/h/std/deflate.h | 2 +- gen/h/std/zlib.h | 2 +- std/deflate/decode_huffman_fast.wuffs | 23 ++++++++------- std/deflate/decode_huffman_slow.wuffs | 17 ++++++------ 6 files changed, 42 insertions(+), 44 deletions(-) diff --git a/gen/c/std/deflate.c b/gen/c/std/deflate.c index c78b02f4..298e1142 100644 --- a/gen/c/std/deflate.c +++ b/gen/c/std/deflate.c @@ -318,7 +318,7 @@ typedef struct { uint32_t v_redir_top; uint32_t v_redir_mask; uint32_t v_length; - uint32_t v_distance; + uint32_t v_dist_minus_1; uint32_t v_n_copied; uint32_t v_hlen; uint32_t v_hdist; @@ -1997,7 +1997,7 @@ static wuffs_deflate__status wuffs_deflate__decoder__decode_huffman_fast( uint32_t v_redir_top; uint32_t v_redir_mask; uint32_t v_length; - uint32_t v_distance; + uint32_t v_dist_minus_1; uint32_t v_n_copied; uint32_t v_hlen; uint32_t v_hdist; @@ -2213,7 +2213,7 @@ label_0_continue:; WUFFS_DEFLATE__ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE; goto exit; } - v_distance = ((v_table_entry >> 8) & 32767); + v_dist_minus_1 = ((v_table_entry >> 8) & 32767); v_table_entry_n_bits = ((v_table_entry >> 4) & 15); if (v_table_entry_n_bits > 0) { if (v_n_bits < 15) { @@ -2228,15 +2228,15 @@ label_0_continue:; } v_n_bits += 8; } - v_distance = - ((v_distance + ((v_bits) & ((1 << (v_table_entry_n_bits)) - 1))) & + v_dist_minus_1 = + ((v_dist_minus_1 + ((v_bits) & ((1 << (v_table_entry_n_bits)) - 1))) & 32767); v_bits >>= v_table_entry_n_bits; v_n_bits -= v_table_entry_n_bits; } v_n_copied = 0; while (true) { - if (((uint64_t)((v_distance + 1))) > + if (((uint64_t)((v_dist_minus_1 + 1))) > ((uint64_t)( ((wuffs_base__slice_u8){ .ptr = a_dst.private_impl.mark, @@ -2247,7 +2247,7 @@ label_0_continue:; .len))) { v_hlen = 0; v_hdist = ((uint32_t)(( - ((uint64_t)((v_distance + 1))) - + ((uint64_t)((v_dist_minus_1 + 1))) - ((uint64_t)( ((wuffs_base__slice_u8){ .ptr = a_dst.private_impl.mark, @@ -2296,7 +2296,7 @@ label_0_continue:; if (v_length == 0) { goto label_0_continue; } - if (((uint64_t)((v_distance + 1))) > + if (((uint64_t)((v_dist_minus_1 + 1))) > ((uint64_t)( ((wuffs_base__slice_u8){ .ptr = a_dst.private_impl.mark, @@ -2310,7 +2310,7 @@ label_0_continue:; } } wuffs_base__writer1__copy_from_history32__bco( - &b_wptr_dst, a_dst.private_impl.mark, b_wend_dst, v_distance + 1, + &b_wptr_dst, a_dst.private_impl.mark, b_wend_dst, v_dist_minus_1 + 1, v_length); goto label_2_break; } @@ -2376,7 +2376,7 @@ static wuffs_deflate__status wuffs_deflate__decoder__decode_huffman_slow( uint32_t v_redir_top; uint32_t v_redir_mask; uint32_t v_length; - uint32_t v_distance; + uint32_t v_dist_minus_1; uint32_t v_n_copied; uint32_t v_hlen; uint32_t v_hdist; @@ -2428,7 +2428,7 @@ static wuffs_deflate__status wuffs_deflate__decoder__decode_huffman_slow( v_redir_top = self->private_impl.c_decode_huffman_slow[0].v_redir_top; v_redir_mask = self->private_impl.c_decode_huffman_slow[0].v_redir_mask; v_length = self->private_impl.c_decode_huffman_slow[0].v_length; - v_distance = self->private_impl.c_decode_huffman_slow[0].v_distance; + v_dist_minus_1 = self->private_impl.c_decode_huffman_slow[0].v_dist_minus_1; v_n_copied = self->private_impl.c_decode_huffman_slow[0].v_n_copied; v_hlen = self->private_impl.c_decode_huffman_slow[0].v_hlen; v_hdist = self->private_impl.c_decode_huffman_slow[0].v_hdist; @@ -2620,7 +2620,7 @@ static wuffs_deflate__status wuffs_deflate__decoder__decode_huffman_slow( WUFFS_DEFLATE__ERROR_INTERNAL_ERROR_INCONSISTENT_HUFFMAN_DECODER_STATE; goto exit; } - v_distance = ((v_table_entry >> 8) & 32767); + v_dist_minus_1 = ((v_table_entry >> 8) & 32767); 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) { @@ -2634,15 +2634,15 @@ static wuffs_deflate__status wuffs_deflate__decoder__decode_huffman_slow( } v_n_bits += 8; } - v_distance = - ((v_distance + ((v_bits) & ((1 << (v_table_entry_n_bits)) - 1))) & - 32767); + v_dist_minus_1 = ((v_dist_minus_1 + + ((v_bits) & ((1 << (v_table_entry_n_bits)) - 1))) & + 32767); v_bits >>= v_table_entry_n_bits; v_n_bits -= v_table_entry_n_bits; } v_n_copied = 0; while (true) { - if (((uint64_t)((v_distance + 1))) > + if (((uint64_t)((v_dist_minus_1 + 1))) > ((uint64_t)( ((wuffs_base__slice_u8){ .ptr = a_dst.private_impl.mark, @@ -2653,7 +2653,7 @@ static wuffs_deflate__status wuffs_deflate__decoder__decode_huffman_slow( .len))) { v_hlen = 0; v_hdist = ((uint32_t)( - (((uint64_t)((v_distance + 1))) - + (((uint64_t)((v_dist_minus_1 + 1))) - ((uint64_t)(((wuffs_base__slice_u8){ .ptr = a_dst.private_impl.mark, .len = a_dst.private_impl.mark @@ -2722,8 +2722,8 @@ static wuffs_deflate__status wuffs_deflate__decoder__decode_huffman_slow( } } v_n_copied = wuffs_base__writer1__copy_from_history32( - &b_wptr_dst, a_dst.private_impl.mark, b_wend_dst, v_distance + 1, - v_length); + &b_wptr_dst, a_dst.private_impl.mark, b_wend_dst, + v_dist_minus_1 + 1, v_length); if (v_length <= v_n_copied) { v_length = 0; goto label_7_break; @@ -2762,7 +2762,7 @@ suspend: self->private_impl.c_decode_huffman_slow[0].v_redir_top = v_redir_top; self->private_impl.c_decode_huffman_slow[0].v_redir_mask = v_redir_mask; self->private_impl.c_decode_huffman_slow[0].v_length = v_length; - self->private_impl.c_decode_huffman_slow[0].v_distance = v_distance; + self->private_impl.c_decode_huffman_slow[0].v_dist_minus_1 = v_dist_minus_1; self->private_impl.c_decode_huffman_slow[0].v_n_copied = v_n_copied; self->private_impl.c_decode_huffman_slow[0].v_hlen = v_hlen; self->private_impl.c_decode_huffman_slow[0].v_hdist = v_hdist; diff --git a/gen/c/std/zlib.c b/gen/c/std/zlib.c index 9214c0c5..abfcf86a 100644 --- a/gen/c/std/zlib.c +++ b/gen/c/std/zlib.c @@ -327,7 +327,7 @@ typedef struct { uint32_t v_redir_top; uint32_t v_redir_mask; uint32_t v_length; - uint32_t v_distance; + uint32_t v_dist_minus_1; uint32_t v_n_copied; uint32_t v_hlen; uint32_t v_hdist; diff --git a/gen/h/std/deflate.h b/gen/h/std/deflate.h index 33fd98bd..bd05270a 100644 --- a/gen/h/std/deflate.h +++ b/gen/h/std/deflate.h @@ -318,7 +318,7 @@ typedef struct { uint32_t v_redir_top; uint32_t v_redir_mask; uint32_t v_length; - uint32_t v_distance; + uint32_t v_dist_minus_1; uint32_t v_n_copied; uint32_t v_hlen; uint32_t v_hdist; diff --git a/gen/h/std/zlib.h b/gen/h/std/zlib.h index 377c53b3..39e2a88e 100644 --- a/gen/h/std/zlib.h +++ b/gen/h/std/zlib.h @@ -327,7 +327,7 @@ typedef struct { uint32_t v_redir_top; uint32_t v_redir_mask; uint32_t v_length; - uint32_t v_distance; + uint32_t v_dist_minus_1; uint32_t v_n_copied; uint32_t v_hlen; uint32_t v_hdist; diff --git a/std/deflate/decode_huffman_fast.wuffs b/std/deflate/decode_huffman_fast.wuffs index 69bfa8bc..58a9b316 100644 --- a/std/deflate/decode_huffman_fast.wuffs +++ b/std/deflate/decode_huffman_fast.wuffs @@ -245,14 +245,13 @@ pri func decoder.decode_huffman_fast?(dst writer1, src reader1)() { return error "internal error: inconsistent Huffman decoder state" } - // distance = base_number_minus_1 + extra_bits. - // - // TODO: rename distance to dist_minus_1. + // dist_minus_1 = base_number_minus_1 + extra_bits. + // 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 - // undoing that bias makes proving (distance + 1) > 0 trivial. - var distance u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF + // undoing that bias makes proving (dist_minus_1 + 1) > 0 trivial. + var dist_minus_1 u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF table_entry_n_bits = (table_entry >> 4) & 0x0F if table_entry_n_bits > 0 { // Ensure that we have at least 15 bits of input. @@ -267,7 +266,7 @@ pri func decoder.decode_huffman_fast?(dst writer1, src reader1)() { // Once again, redundant but explicit assertions. assert n_bits >= 15 - distance = (distance + bits.low_bits(n:table_entry_n_bits)) & 0x7FFF + dist_minus_1 = (dist_minus_1 + bits.low_bits(n:table_entry_n_bits)) & 0x7FFF bits >>= table_entry_n_bits n_bits -= table_entry_n_bits } @@ -282,13 +281,13 @@ pri func decoder.decode_huffman_fast?(dst writer1, src reader1)() { inv in.dst.is_marked(), { // Copy from this.history. - if ((distance + 1) as u64) > in.dst.since_mark().length() { + if ((dist_minus_1 + 1) as u64) > in.dst.since_mark().length() { // Set (hlen, hdist) to be the length-distance pair to copy // from this.history, and (length, distance) to be the // remaining length-distance pair to copy from in.dst. var hlen u32[..0x7FFF] var hdist u32 = - (((distance + 1) as u64) - in.dst.since_mark().length()) as u32 + (((dist_minus_1 + 1) as u64) - in.dst.since_mark().length()) as u32 if length > hdist { assert hdist < length via "a < b: b > a"() assert hdist < 0x8000 via "a < b: a < c; c <= b"(c:length) @@ -343,23 +342,23 @@ pri func decoder.decode_huffman_fast?(dst writer1, src reader1)() { continue:loop } - if ((distance + 1) as u64) > in.dst.since_mark().length() { + if ((dist_minus_1 + 1) as u64) > in.dst.since_mark().length() { return error "internal error: inconsistent distance" } } // Once again, redundant but explicit assertions. assert length <= 258 - assert ((distance + 1) as u64) <= in.dst.since_mark().length() + assert ((dist_minus_1 + 1) as u64) <= in.dst.since_mark().length() assert in.dst.available() >= 258 assert in.dst.is_marked() // We can therefore prove: - assert (distance + 1) > 0 + assert (dist_minus_1 + 1) > 0 assert (length as u64) <= 258 assert (length as u64) <= in.dst.available() via "a <= b: a <= c; c <= b"(c:258) // Copy from in.dst. - in.dst.copy_from_history32(distance:(distance + 1), length:length) + in.dst.copy_from_history32(distance:(dist_minus_1 + 1), length:length) break } } diff --git a/std/deflate/decode_huffman_slow.wuffs b/std/deflate/decode_huffman_slow.wuffs index 876adabe..e158e631 100644 --- a/std/deflate/decode_huffman_slow.wuffs +++ b/std/deflate/decode_huffman_slow.wuffs @@ -157,14 +157,13 @@ pri func decoder.decode_huffman_slow?(dst writer1, src reader1)() { return error "internal error: inconsistent Huffman decoder state" } - // distance = base_number_minus_1 + extra_bits. - // - // TODO: rename distance to dist_minus_1. + // dist_minus_1 = base_number_minus_1 + extra_bits. + // 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 - // undoing that bias makes proving (distance + 1) > 0 trivial. - var distance u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF + // undoing that bias makes proving (dist_minus_1 + 1) > 0 trivial. + var dist_minus_1 u32[..0x7FFF] = (table_entry >> 8) & 0x7FFF table_entry_n_bits = (table_entry >> 4) & 0x0F if table_entry_n_bits > 0 { while n_bits < table_entry_n_bits, @@ -174,7 +173,7 @@ pri func decoder.decode_huffman_slow?(dst writer1, src reader1)() { bits |= (in.src.read_u8?() as u32) << n_bits n_bits += 8 } - distance = (distance + bits.low_bits(n:table_entry_n_bits)) & 0x7FFF + dist_minus_1 = (dist_minus_1 + bits.low_bits(n:table_entry_n_bits)) & 0x7FFF bits >>= table_entry_n_bits n_bits -= table_entry_n_bits } @@ -182,13 +181,13 @@ pri func decoder.decode_huffman_slow?(dst writer1, src reader1)() { var n_copied u32 while true { // Copy from this.history. - if ((distance + 1) as u64) > in.dst.since_mark().length() { + if ((dist_minus_1 + 1) as u64) > in.dst.since_mark().length() { // Set (hlen, hdist) to be the length-distance pair to copy // from this.history, and (length, distance) to be the // remaining length-distance pair to copy from in.dst. var hlen u32[..0x7FFF] var hdist u32 = - (((distance + 1) as u64) - in.dst.since_mark().length()) as u32 + (((dist_minus_1 + 1) as u64) - in.dst.since_mark().length()) as u32 if length > hdist { assert hdist < length via "a < b: b > a"() assert hdist < 0x8000 via "a < b: a < c; c <= b"(c:length) @@ -247,7 +246,7 @@ pri func decoder.decode_huffman_slow?(dst writer1, src reader1)() { } // Copy from in.dst. - n_copied = in.dst.copy_from_history32(distance:(distance + 1), length:length) + n_copied = in.dst.copy_from_history32(distance:(dist_minus_1 + 1), length:length) if length <= n_copied { length = 0 break