diff options
author | Eric Wong <normalperson@yhbt.net> | 2006-08-28 18:43:01 -0700 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2006-09-09 18:57:39 -0700 |
commit | f345a3435dde249874fde205ef4b793184e1e7c4 (patch) | |
tree | 13f42921669ef3bd2ba008a92c62be9ea2d4498d | |
parent | 2e9a1a6d10979ff89bc7238053ca4fb2ec7041ee (diff) | |
download | flac-arm-1.1.3-f345a3435dde249874fde205ef4b793184e1e7c4.tar.gz |
Branch optimizations to bitbuffer_read_rice_signed_block
I've reordered the blocks to reduce jumps to optimize for the more likely cases. For extremely likely branches, I take advantage of __builtin_expect(x,y) if we're using gcc 3 or later.
-rw-r--r-- | src/libFLAC/bitbuffer.c | 93 |
1 files changed, 53 insertions, 40 deletions
diff --git a/src/libFLAC/bitbuffer.c b/src/libFLAC/bitbuffer.c index f6b1b74f..24e38452 100644 --- a/src/libFLAC/bitbuffer.c +++ b/src/libFLAC/bitbuffer.c @@ -2205,6 +2205,12 @@ FLAC__bool FLAC__bitbuffer_read_rice_signed_block(FLAC__BitBuffer *bb, int vals[ return true; } #else +# if defined(__GNUC__) && (__GNUC__ >= 3) +# define likely(x) __builtin_expect(!!(x),1) +# else +# warning not using __builtin_expect(x,y) +# define likely(x) (x) +# endif { const FLAC__blurb *buffer = bb->buffer; @@ -2226,31 +2232,7 @@ FLAC__bool FLAC__bitbuffer_read_rice_signed_block(FLAC__BitBuffer *bb, int vals[ for( ; i < bb->blurbs; i++) { blurb = (save_blurb = buffer[i]) << cbits; while(1) { - if(state == 0) { - if(blurb) { - j = FLAC__ALIGNED_BLURB_UNARY(blurb); - msbs += j; - j++; - cbits += j; - - uval = 0; - lsbs_left = parameter; - state++; - if(cbits == FLAC__BITS_PER_BLURB) { - cbits = 0; - CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16); - break; - } - blurb <<= j; - } - else { - msbs += FLAC__BITS_PER_BLURB - cbits; - cbits = 0; - CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16); - break; - } - } - else { + if(likely(state != 0)) { const unsigned available_bits = FLAC__BITS_PER_BLURB - cbits; if(lsbs_left >= available_bits) { uval <<= available_bits; @@ -2258,23 +2240,26 @@ FLAC__bool FLAC__bitbuffer_read_rice_signed_block(FLAC__BitBuffer *bb, int vals[ cbits = 0; CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16); - if(lsbs_left == available_bits) { + if(likely(lsbs_left != available_bits)) { + lsbs_left -= available_bits; + break; + } else { /* compose the value */ uval |= (msbs << parameter); *vals = (int)(uval >> 1 ^ -(int)(uval & 1)); --val_i; - if(val_i == 0) { - i++; - goto break2; - } - ++vals; + if(val_i != 0) { + ++vals; - msbs = 0; - state = 0; - } + msbs = 0; + state = 0; - lsbs_left -= available_bits; - break; + lsbs_left -= available_bits; + break; + } + i++; + goto break2; + } } else { cbits += lsbs_left; @@ -2286,12 +2271,39 @@ FLAC__bool FLAC__bitbuffer_read_rice_signed_block(FLAC__BitBuffer *bb, int vals[ uval |= (msbs << parameter); *vals = (int)(uval >> 1 ^ -(int)(uval & 1)); --val_i; - if(val_i == 0) + if(val_i != 0) { + ++vals; + msbs = 0; + state = 0; + } else { goto break2; - ++vals; + } + } + } + else { + if(likely(blurb)) { + j = FLAC__ALIGNED_BLURB_UNARY(blurb); + msbs += j; + j++; + cbits += j; - msbs = 0; - state = 0; + uval = 0; + lsbs_left = parameter; + state++; + + if (likely(cbits != FLAC__BITS_PER_BLURB)) + blurb <<= j; + else { + cbits = 0; + CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16); + break; + } + } + else { + msbs += FLAC__BITS_PER_BLURB - cbits; + cbits = 0; + CRC16_UPDATE_BLURB(bb, save_blurb, bb->read_crc16); + break; } } } @@ -2312,6 +2324,7 @@ break2: return true; } +#undef likely #endif #if 0 /* UNUSED */ |