From 1a3f34eab9e81906e78c5121df0de075f44f5f8f Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Tue, 25 Dec 2018 21:29:13 -0600 Subject: [PATCH] lib/deflate_decompress: optimize codeword incrementing --- lib/deflate_decompress.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/lib/deflate_decompress.c b/lib/deflate_decompress.c index ad7ce32..bf129f4 100644 --- a/lib/deflate_decompress.c +++ b/lib/deflate_decompress.c @@ -729,22 +729,26 @@ build_decode_table(u32 decode_table[], i += increment; } while (i < end); - /* Advance to the next codeword by incrementing it. But since - * our codewords are bit-reversed, we must manipulate the bits - * ourselves rather than simply adding 1. */ - bit = 1U << (codeword_len - 1); - while (codeword_reversed & bit) - bit >>= 1; + /* Advance to the next symbol and codeword */ + + if (++sym_idx == num_syms) + return true; + /* + * Increment the codeword, bit-reversed: find the last (highest + * order) 0 bit in the codeword, set it, and clear any later + * (higher order) bits. + */ + bit = 1U << bsr32(~codeword_reversed & + ((1U << codeword_len) - 1)); codeword_reversed &= bit - 1; codeword_reversed |= bit; - /* Advance to the next symbol. This will either increase the - * codeword length, or keep the same codeword length but - * increase the symbol value. Note: since we are using - * bit-reversed codewords, we don't need to explicitly append - * zeroes to the codeword when the codeword length increases. */ - if (++sym_idx == num_syms) - return true; + /* + * If there are no more codewords of this length, proceed to the + * next lowest used length. Increasing the length logically + * appends 0's to the codeword, but this is a no-op due to the + * codeword being represented in bit-reversed form. + */ len_counts[codeword_len]--; while (len_counts[codeword_len] == 0) codeword_len++;