From: Eric Biggers Date: Mon, 20 Jul 2015 04:13:53 +0000 (-0500) Subject: Avoid branch in 'num_bits == 0' branch when peeking bits X-Git-Tag: v1.8.2~41 X-Git-Url: https://wimlib.net/git/?p=wimlib;a=commitdiff_plain;h=9bb97aee4a44801c1d755916aa7a132b2cdec80f Avoid branch in 'num_bits == 0' branch when peeking bits --- diff --git a/include/wimlib/decompress_common.h b/include/wimlib/decompress_common.h index 420f7f41..ce731dca 100644 --- a/include/wimlib/decompress_common.h +++ b/include/wimlib/decompress_common.h @@ -90,9 +90,7 @@ overflow: static inline u32 bitstream_peek_bits(const struct input_bitstream *is, const unsigned num_bits) { - if (unlikely(num_bits == 0)) - return 0; - return is->bitbuf >> (32 - num_bits); + return (is->bitbuf >> 1) >> (sizeof(is->bitbuf) * 8 - num_bits - 1); } /* Remove @num_bits from the bitstream. There must be at least @num_bits diff --git a/src/lzms_decompress.c b/src/lzms_decompress.c index 7f82803c..bd7e76e1 100644 --- a/src/lzms_decompress.c +++ b/src/lzms_decompress.c @@ -430,9 +430,7 @@ lzms_ensure_bits(struct lzms_input_bitstream *is, unsigned num_bits) static inline bitbuf_t lzms_peek_bits(struct lzms_input_bitstream *is, unsigned num_bits) { - if (unlikely(num_bits == 0)) - return 0; - return is->bitbuf >> (sizeof(is->bitbuf) * 8 - num_bits); + return (is->bitbuf >> 1) >> (sizeof(is->bitbuf) * 8 - num_bits - 1); } /* Remove @num_bits bits from the bitbuffer variable. */