lib/deflate_decompress: optimize codeword incrementing

This commit is contained in:
Eric Biggers 2018-12-25 21:29:13 -06:00
parent a25f3b86d7
commit 1a3f34eab9

View File

@ -729,22 +729,26 @@ build_decode_table(u32 decode_table[],
i += increment; i += increment;
} while (i < end); } while (i < end);
/* Advance to the next codeword by incrementing it. But since /* Advance to the next symbol and codeword */
* our codewords are bit-reversed, we must manipulate the bits
* ourselves rather than simply adding 1. */ if (++sym_idx == num_syms)
bit = 1U << (codeword_len - 1); return true;
while (codeword_reversed & bit) /*
bit >>= 1; * 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 - 1;
codeword_reversed |= bit; codeword_reversed |= bit;
/* Advance to the next symbol. This will either increase the /*
* codeword length, or keep the same codeword length but * If there are no more codewords of this length, proceed to the
* increase the symbol value. Note: since we are using * next lowest used length. Increasing the length logically
* bit-reversed codewords, we don't need to explicitly append * appends 0's to the codeword, but this is a no-op due to the
* zeroes to the codeword when the codeword length increases. */ * codeword being represented in bit-reversed form.
if (++sym_idx == num_syms) */
return true;
len_counts[codeword_len]--; len_counts[codeword_len]--;
while (len_counts[codeword_len] == 0) while (len_counts[codeword_len] == 0)
codeword_len++; codeword_len++;