X-Git-Url: https://git.sesse.net/?p=fjl;a=blobdiff_plain;f=input.h;h=19315e54ab3c357ceaf1f16f96f310caace75b76;hp=34765c25b113b35efff8c77ccf4d3f3e09ae30f0;hb=618548d1f2e076a3da21368e708cf887dcbd20d2;hpb=71b4fae2b31acf415c3b07a019ae252f4d128952 diff --git a/input.h b/input.h index 34765c2..19315e5 100644 --- a/input.h +++ b/input.h @@ -17,9 +17,9 @@ static inline bitreservoir_fill_t read_bitreservoir_fill(uint8_t* source) return ntohl(*(bitreservoir_fill_t*)(source)); } -static const unsigned bitreservoir_size = 8 * sizeof(bitreservoir_t); -static const unsigned bitreservoir_fill_size = 8 * sizeof(bitreservoir_fill_t); -static const unsigned bytereservoir_size = 4096; +static const unsigned BITRESERVOIR_SIZE = 8 * sizeof(bitreservoir_t); +static const unsigned BITRESERVOIR_FILL_SIZE = 8 * sizeof(bitreservoir_fill_t); +static const unsigned BYTERESERVOIR_SIZE = 4096; // A function to read bytes from some input source. The bytes should be // already unstuffed (and thus without markers). @@ -55,7 +55,7 @@ void possibly_refill_slow_path(struct bit_source* source, unsigned num_bits); // refill A+B, read A, read B than refill A, read A, refill B, read B. static inline void possibly_refill(struct bit_source* source, unsigned num_bits) { - assert(num_bits <= bitreservoir_fill_size + 1); + assert(num_bits <= BITRESERVOIR_FILL_SIZE + 1); if (source->bits_available >= num_bits) { // Fast path (~90% of invocations?) @@ -63,13 +63,13 @@ static inline void possibly_refill(struct bit_source* source, unsigned num_bits) } // Slower path (~99% of remaining invocations?) - assert(source->bits_available + bitreservoir_fill_size < bitreservoir_size); + assert(source->bits_available + BITRESERVOIR_FILL_SIZE < BITRESERVOIR_SIZE); if (source->bytes_available >= sizeof(bitreservoir_fill_t)) { bitreservoir_fill_t fill = read_bitreservoir_fill(source->bytes); source->bytes += sizeof(bitreservoir_fill_t); source->bytes_available -= sizeof(bitreservoir_fill_t); - source->bits |= (bitreservoir_t)fill << (bitreservoir_size - bitreservoir_fill_size - source->bits_available); - source->bits_available += bitreservoir_fill_size; + source->bits |= (bitreservoir_t)fill << (BITRESERVOIR_SIZE - BITRESERVOIR_FILL_SIZE - source->bits_available); + source->bits_available += BITRESERVOIR_FILL_SIZE; return; } @@ -81,10 +81,16 @@ static inline void possibly_refill(struct bit_source* source, unsigned num_bits) static inline unsigned read_bits(struct bit_source* source, unsigned num_bits) { assert(source->bits_available >= num_bits); - unsigned ret = (source->bits >> (bitreservoir_size - num_bits)); + unsigned ret = (source->bits >> (BITRESERVOIR_SIZE - num_bits)); source->bits <<= num_bits; source->bits_available -= num_bits; return ret; } +static inline unsigned peek_bits(struct bit_source* source, unsigned num_bits) +{ + assert(source->bits_available >= num_bits); + return (source->bits >> (BITRESERVOIR_SIZE - num_bits)); +} + #endif /* !defined(_INPUT_H) */