From a1271222a449bb292f105d0b85d44ffec1ceccef Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 6 Oct 2017 14:53:45 +1100 Subject: [PATCH] Call copy_history32 in a trival 1-iteration loop. Follow-up commits will re-try copy_history32's across I/O boundaries. --- gen/c/std/flate.c | 76 +++++++++++++++++++----------------- std/flate/decode_flate.puffs | 7 +++- 2 files changed, 46 insertions(+), 37 deletions(-) diff --git a/gen/c/std/flate.c b/gen/c/std/flate.c index d7eb1337..241b53eb 100644 --- a/gen/c/std/flate.c +++ b/gen/c/std/flate.c @@ -1314,46 +1314,52 @@ puffs_flate_status puffs_flate_decoder_decode_huffman( v_distance += ((v_bits) & ((1 << (v_table_entry_n_bits)) - 1)); v_bits >>= v_table_entry_n_bits; v_n_bits -= v_table_entry_n_bits; - if (((uint64_t)(v_distance)) > ((uint64_t)(b_wptr_dst - b_wstart_dst))) { - } - PUFFS_COROUTINE_SUSPENSION_POINT(8); - { - self->private_impl.c_decode_huffman[0].scratch = - ((uint64_t)(v_distance) << 32) | (uint64_t)(v_length); - PUFFS_COROUTINE_SUSPENSION_POINT(9); - size_t t_6 = - (size_t)(self->private_impl.c_decode_huffman[0].scratch >> 32); - if (PUFFS_UNLIKELY((t_6 == 0) || (t_6 > (b_wptr_dst - b_wstart_dst)))) { - status = PUFFS_FLATE_ERROR_BAD_ARGUMENT; - goto exit; + while (true) { + if (((uint64_t)(v_distance)) > + ((uint64_t)(b_wptr_dst - b_wstart_dst))) { } - uint8_t* t_7 = b_wptr_dst - t_6; - uint32_t t_8 = - (uint32_t)(self->private_impl.c_decode_huffman[0].scratch); - if (PUFFS_LIKELY((size_t)(t_8) <= (b_wend_dst - b_wptr_dst))) { - for (; t_8 >= 8; t_8 -= 8) { - *b_wptr_dst++ = *t_7++; - *b_wptr_dst++ = *t_7++; - *b_wptr_dst++ = *t_7++; - *b_wptr_dst++ = *t_7++; - *b_wptr_dst++ = *t_7++; - *b_wptr_dst++ = *t_7++; - *b_wptr_dst++ = *t_7++; - *b_wptr_dst++ = *t_7++; + PUFFS_COROUTINE_SUSPENSION_POINT(8); + { + self->private_impl.c_decode_huffman[0].scratch = + ((uint64_t)(v_distance) << 32) | (uint64_t)(v_length); + PUFFS_COROUTINE_SUSPENSION_POINT(9); + size_t t_6 = + (size_t)(self->private_impl.c_decode_huffman[0].scratch >> 32); + if (PUFFS_UNLIKELY((t_6 == 0) || + (t_6 > (b_wptr_dst - b_wstart_dst)))) { + status = PUFFS_FLATE_ERROR_BAD_ARGUMENT; + goto exit; } - for (; t_8; t_8--) { - *b_wptr_dst++ = *t_7++; + uint8_t* t_7 = b_wptr_dst - t_6; + uint32_t t_8 = + (uint32_t)(self->private_impl.c_decode_huffman[0].scratch); + if (PUFFS_LIKELY((size_t)(t_8) <= (b_wend_dst - b_wptr_dst))) { + for (; t_8 >= 8; t_8 -= 8) { + *b_wptr_dst++ = *t_7++; + *b_wptr_dst++ = *t_7++; + *b_wptr_dst++ = *t_7++; + *b_wptr_dst++ = *t_7++; + *b_wptr_dst++ = *t_7++; + *b_wptr_dst++ = *t_7++; + *b_wptr_dst++ = *t_7++; + *b_wptr_dst++ = *t_7++; + } + for (; t_8; t_8--) { + *b_wptr_dst++ = *t_7++; + } + } else { + t_8 = (uint32_t)(b_wend_dst - b_wptr_dst); + self->private_impl.c_decode_huffman[0].scratch -= (uint64_t)(t_8); + for (; t_8; t_8--) { + *b_wptr_dst++ = *t_7++; + } + status = PUFFS_FLATE_SUSPENSION_SHORT_WRITE; + goto suspend; } - } else { - t_8 = (uint32_t)(b_wend_dst - b_wptr_dst); - self->private_impl.c_decode_huffman[0].scratch -= (uint64_t)(t_8); - for (; t_8; t_8--) { - *b_wptr_dst++ = *t_7++; - } - status = PUFFS_FLATE_SUSPENSION_SHORT_WRITE; - goto suspend; } + goto label_5_break; } + label_5_break:; } label_0_break:; self->private_impl.f_bits = v_bits; diff --git a/std/flate/decode_flate.puffs b/std/flate/decode_flate.puffs index afb3f050..a3169158 100644 --- a/std/flate/decode_flate.puffs +++ b/std/flate/decode_flate.puffs @@ -341,9 +341,12 @@ pri func decoder.decode_huffman?(dst writer1, src reader1)() { // TODO: copy_history32 should also take some notion of how much of dst // is valid. When copying from a distance greater than that, we also // need to keep a sliding window of previous history. - if (distance as u64) > in.dst.length() { + while true { + if (distance as u64) > in.dst.length() { + } + in.dst.copy_history32?(distance:distance, length:length) + break } - in.dst.copy_history32?(distance:distance, length:length) } this.bits = bits