Added fast path for bitreader

This commit is contained in:
Dan Hirsch 2012-05-01 01:45:45 +01:00
parent bc51198354
commit ddcfa5b1b2

View file

@ -12,6 +12,18 @@ long long read_bits(input_stream_t* state, int count, char signed_p) {
long long out = 0; long long out = 0;
int offset = 0; int offset = 0;
long long msb = (!!signed_p) << (count - 1); // 0 if unsigned, else 1 << (nbits - 1) long long msb = (!!signed_p) << (count - 1); // 0 if unsigned, else 1 << (nbits - 1)
if ((state->bit_offset & 0x7) == 0 && (count & 0x7) == 0) {
// fast path
if (state->endianness & BYTE_BIG_ENDIAN) {
while (count > 0)
out = (out << 8) | state->input[state->index++];
} else {
while (count > 0) {
count -= 8;
out |= state->input[state->index++] << count;
}
}
} else {
while (count) { while (count) {
int segment, segment_len; int segment, segment_len;
// Read a segment... // Read a segment...
@ -48,6 +60,7 @@ long long read_bits(input_stream_t* state, int count, char signed_p) {
} }
count -= segment_len; count -= segment_len;
} }
}
return (out ^ msb) - msb; // perform sign extension return (out ^ msb) - msb; // perform sign extension
} }