1 Index: src/libFLAC/bitreader.c
2 ===================================================================
3 RCS file: /cvsroot/flac/flac/src/libFLAC/bitreader.c,v
4 retrieving revision 1.15
5 diff -u -r1.15 bitreader.c
6 --- src/libFLAC/bitreader.c 28 Feb 2008 05:34:26 -0000 1.15
7 +++ src/libFLAC/bitreader.c 14 Mar 2008 11:07:07 -0000
10 /* counts the # of zero MSBs in a word */
11 #define COUNT_ZERO_MSBS(word) ( \
12 - (word) <= 0xffff ? \
13 - ( (word) <= 0xff? byte_to_unary_table[word] + 24 : byte_to_unary_table[(word) >> 8] + 16 ) : \
14 - ( (word) <= 0xffffff? byte_to_unary_table[word >> 16] + 8 : byte_to_unary_table[(word) >> 24] ) \
15 + word > 0xffffff ? byte_to_unary_table[(word) >> 24] : \
17 + word > 0xffff ? byte_to_unary_table[word >> 16] + 8 : \
18 + word > 0xff ? byte_to_unary_table[(word) >> 8] + 16 : \
19 + byte_to_unary_table[word] + 24 \
21 -/* this alternate might be slightly faster on some systems/compilers: */
22 -#define COUNT_ZERO_MSBS2(word) ( (word) <= 0xff ? byte_to_unary_table[word] + 24 : ((word) <= 0xffff ? byte_to_unary_table[(word) >> 8] + 16 : ((word) <= 0xffffff ? byte_to_unary_table[(word) >> 16] + 8 : byte_to_unary_table[(word) >> 24])) )
26 * This should be at least twice as large as the largest number of words
27 Index: src/libFLAC/bitreader.c
28 ===================================================================
29 RCS file: /cvsroot/flac/flac/src/libFLAC/bitreader.c,v
30 retrieving revision 1.15
31 diff -u -r1.15 bitreader.c
32 --- src/libFLAC/bitreader.c 28 Feb 2008 05:34:26 -0000 1.15
33 +++ src/libFLAC/bitreader.c 14 Mar 2008 13:19:46 -0000
35 FLAC__CPUInfo cpu_info;
38 +#if FLAC__BYTES_PER_WORD == 4 && FLAC__CPU_IA32
40 /* OPT: an MSVC built-in would be better */
41 static _inline FLAC__uint32 local_swap32_(FLAC__uint32 x)
47 +static void local_swap32_block_(FLAC__uint32 *start, FLAC__uint32 len)
51 + for(end = start + len; start < end; start++)
52 + asm ("bswap %0" : "=r"(*start) : "0"(*start));
57 static FLaC__INLINE void crc16_update_word_(FLAC__BitReader *br, brword word)
61 end = (br->words*FLAC__BYTES_PER_WORD + br->bytes + bytes + (FLAC__BYTES_PER_WORD-1)) / FLAC__BYTES_PER_WORD;
62 -# if defined(_MSC_VER) && (FLAC__BYTES_PER_WORD == 4)
63 +# if FLAC__CPU_IA32 && (__GNUC__ || defined(_MSC_VER)) && FLAC__BYTES_PER_WORD == 4
64 if(br->cpu_info.type == FLAC__CPUINFO_TYPE_IA32 && br->cpu_info.data.ia32.bswap) {
66 local_swap32_block_(br->buffer + start, end - start);
67 Index: src/libFLAC/bitreader.c
68 ===================================================================
69 RCS file: /cvsroot/flac/flac/src/libFLAC/bitreader.c,v
70 retrieving revision 1.15
71 diff -u -r1.15 bitreader.c
72 --- src/libFLAC/bitreader.c 28 Feb 2008 05:34:26 -0000 1.15
73 +++ src/libFLAC/bitreader.c 17 Mar 2008 15:42:57 -0000
74 @@ -803,379 +812,144 @@
77 /* this is by far the most heavily used reader call. it ain't pretty but it's fast */
78 -/* a lot of the logic is copied, then adapted, from FLAC__bitreader_read_unary_unsigned() and FLAC__bitreader_read_raw_uint32() */
79 FLAC__bool FLAC__bitreader_read_rice_signed_block(FLAC__BitReader *br, int vals[], unsigned nvals, unsigned parameter)
80 -/* OPT: possibly faster version for use with MSVC */
85 - unsigned bits; /* the # of binary LSBs left to read to finish a rice codeword */
87 /* try and get br->consumed_words and br->consumed_bits into register;
88 * must remember to flush them back to *br before calling other
89 - * bitwriter functions that use them, and before returning */
90 - register unsigned cwords;
91 - register unsigned cbits;
92 + * bitreader functions that use them, and before returning */
93 + unsigned cwords, words, lsbs, msbs, x, y;
94 + unsigned ucbits; /* keep track of the number of unconsumed bits in word */
98 FLAC__ASSERT(0 != br);
99 FLAC__ASSERT(0 != br->buffer);
100 /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
101 FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
102 FLAC__ASSERT(parameter < 32);
103 - /* the above two asserts also guarantee that the binary part never straddles more that 2 words, so we don't have to loop to read it */
108 - cbits = br->consumed_bits;
109 - cwords = br->consumed_words;
110 + /* the above two asserts also guarantee that the binary part never straddles more than 2 words, so we don't have to loop to read it */
114 + end = vals + nvals;
116 - /* read unary part */
118 - while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
119 - brword b = br->buffer[cwords] << cbits;
121 -#if 0 /* slower, probably due to bad register allocation... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32
129 - i = COUNT_ZERO_MSBS(b);
135 - if(cbits == FLAC__BITS_PER_WORD) {
136 - crc16_update_word_(br, br->buffer[cwords]);
143 - uval += FLAC__BITS_PER_WORD - cbits;
144 - crc16_update_word_(br, br->buffer[cwords]);
147 - /* didn't find stop bit yet, have to keep going... */
150 - /* at this point we've eaten up all the whole words; have to try
151 - * reading through any tail bytes before calling the read callback.
152 - * this is a repeat of the above logic adjusted for the fact we
153 - * don't have a whole word. note though if the client is feeding
154 - * us data a byte at a time (unlikely), br->consumed_bits may not
158 - const unsigned end = br->bytes * 8;
159 - brword b = (br->buffer[cwords] & (FLAC__WORD_ALL_ONES << (FLAC__BITS_PER_WORD-end))) << cbits;
161 - i = COUNT_ZERO_MSBS(b);
166 - FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
170 - uval += end - cbits;
172 - FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
173 - /* didn't find stop bit yet, have to keep going... */
176 - /* flush registers and read; bitreader_read_from_client_() does
177 - * not touch br->consumed_bits at all but we still need to set
178 - * it in case it fails and we have to return false.
180 - br->consumed_bits = cbits;
181 - br->consumed_words = cwords;
182 - if(!bitreader_read_from_client_(br))
183 + if(parameter == 0) {
185 + /* read the unary MSBs and end bit */
186 + if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
188 - cwords = br->consumed_words;
191 - /* read binary part */
192 - FLAC__ASSERT(cwords <= br->words);
195 - while((br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits < bits) {
196 - /* flush registers and read; bitreader_read_from_client_() does
197 - * not touch br->consumed_bits at all but we still need to set
198 - * it in case it fails and we have to return false.
200 - br->consumed_bits = cbits;
201 - br->consumed_words = cwords;
202 - if(!bitreader_read_from_client_(br))
204 - cwords = br->consumed_words;
206 - if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
208 - /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
209 - const unsigned n = FLAC__BITS_PER_WORD - cbits;
210 - const brword word = br->buffer[cwords];
213 - uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-bits);
218 - uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
220 - crc16_update_word_(br, word);
223 - if(bits) { /* if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
225 - uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits));
231 - FLAC__ASSERT(bits < FLAC__BITS_PER_WORD);
233 - uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
239 - /* in this case we're starting our read at a partial tail word;
240 - * the reader has guaranteed that we have at least 'bits' bits
241 - * available to read, which makes this case simpler.
245 - /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
246 - FLAC__ASSERT(cbits + bits <= br->bytes*8);
247 - uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-bits);
252 - uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-bits);
259 - /* compose the value */
260 - *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
265 - br->consumed_bits = cbits;
266 - br->consumed_words = cwords;
268 + *val++ = (int)(msbs >> 1) ^ -(int)(msbs & 1);
282 - /* try and get br->consumed_words and br->consumed_bits into register;
283 - * must remember to flush them back to *br before calling other
284 - * bitwriter functions that use them, and before returning */
285 - register unsigned cwords;
286 - register unsigned cbits;
287 - unsigned ucbits; /* keep track of the number of unconsumed bits in the buffer */
288 + FLAC__ASSERT(parameter > 0);
290 - FLAC__ASSERT(0 != br);
291 - FLAC__ASSERT(0 != br->buffer);
292 - /* WATCHOUT: code does not work with <32bit words; we can make things much faster with this assertion */
293 - FLAC__ASSERT(FLAC__BITS_PER_WORD >= 32);
294 - FLAC__ASSERT(parameter < 32);
295 - /* the above two asserts also guarantee that the binary part never straddles more than 2 words, so we don't have to loop to read it */
296 + cwords = br->consumed_words;
301 + /* if we've not consumed up to a partial tail word... */
302 + if(cwords >= words) {
307 + ucbits = FLAC__BITS_PER_WORD - br->consumed_bits;
308 + b = br->buffer[cwords] << br->consumed_bits; /* keep unconsumed bits aligned to left */
311 + /* read the unary MSBs and end bit */
312 + x = y = COUNT_ZERO_MSBS(b);
313 + if(x == FLAC__BITS_PER_WORD) {
316 + /* didn't find stop bit yet, have to keep going... */
317 + crc16_update_word_(br, br->buffer[cwords++]);
318 + if (cwords >= words)
319 + goto incomplete_msbs;
320 + b = br->buffer[cwords];
321 + y = COUNT_ZERO_MSBS(b);
323 + } while(y == FLAC__BITS_PER_WORD);
326 + b <<= 1; /* account for stop bit */
327 + ucbits = (ucbits - x - 1) % FLAC__BITS_PER_WORD;
330 + /* read the binary LSBs */
331 + x = b >> (FLAC__BITS_PER_WORD - parameter);
332 + if(parameter <= ucbits) {
333 + ucbits -= parameter;
336 + /* there are still bits left to read, they will all be in the next word */
337 + crc16_update_word_(br, br->buffer[cwords++]);
338 + if (cwords >= words)
339 + goto incomplete_lsbs;
340 + b = br->buffer[cwords];
341 + ucbits += FLAC__BITS_PER_WORD - parameter;
343 + b <<= FLAC__BITS_PER_WORD - ucbits;
347 - cbits = br->consumed_bits;
348 - cwords = br->consumed_words;
349 - ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
350 + /* compose the value */
351 + x = (msbs << parameter) | lsbs;
352 + *val++ = (int)(x >> 1) ^ -(int)(x & 1);
357 - /* read unary part */
359 - while(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
360 - brword b = br->buffer[cwords] << cbits;
362 -#if 0 /* is not discernably faster... */ && defined FLAC__CPU_IA32 && !defined FLAC__NO_ASM && FLAC__BITS_PER_WORD == 32 && defined __GNUC__
371 - i = COUNT_ZERO_MSBS(b);
375 - cbits++; /* skip over stop bit */
376 - if(cbits >= FLAC__BITS_PER_WORD) { /* faster way of testing if(cbits == FLAC__BITS_PER_WORD) */
377 - crc16_update_word_(br, br->buffer[cwords]);
384 - uval += FLAC__BITS_PER_WORD - cbits;
385 - crc16_update_word_(br, br->buffer[cwords]);
388 - /* didn't find stop bit yet, have to keep going... */
391 - /* at this point we've eaten up all the whole words; have to try
392 - * reading through any tail bytes before calling the read callback.
393 - * this is a repeat of the above logic adjusted for the fact we
394 - * don't have a whole word. note though if the client is feeding
395 - * us data a byte at a time (unlikely), br->consumed_bits may not
399 - const unsigned end = br->bytes * 8;
400 - brword b = (br->buffer[cwords] & ~(FLAC__WORD_ALL_ONES >> end)) << cbits;
402 - i = COUNT_ZERO_MSBS(b);
405 - cbits++; /* skip over stop bit */
406 - FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
410 - uval += end - cbits;
412 - FLAC__ASSERT(cbits < FLAC__BITS_PER_WORD);
413 - /* didn't find stop bit yet, have to keep going... */
415 + /* at this point we've eaten up all the whole words */
420 + br->consumed_bits = 0;
421 + br->consumed_words = cwords;
423 - /* flush registers and read; bitreader_read_from_client_() does
424 - * not touch br->consumed_bits at all but we still need to set
425 - * it in case it fails and we have to return false.
427 - br->consumed_bits = cbits;
428 - br->consumed_words = cwords;
429 - if(!bitreader_read_from_client_(br))
431 + /* read the unary MSBs and end bit */
432 + if(!FLAC__bitreader_read_unary_unsigned(br, &msbs))
434 - cwords = br->consumed_words;
435 - ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits + uval;
436 - /* + uval to offset our count by the # of unary bits already
437 - * consumed before the read, because we will add these back
438 - * in all at once at break1
443 - ucbits--; /* account for stop bit */
445 - /* read binary part */
446 - FLAC__ASSERT(cwords <= br->words);
449 - while(ucbits < parameter) {
450 - /* flush registers and read; bitreader_read_from_client_() does
451 - * not touch br->consumed_bits at all but we still need to set
452 - * it in case it fails and we have to return false.
454 - br->consumed_bits = cbits;
460 + br->consumed_bits = 0;
461 br->consumed_words = cwords;
462 - if(!bitreader_read_from_client_(br))
464 - cwords = br->consumed_words;
465 - ucbits = (br->words-cwords)*FLAC__BITS_PER_WORD + br->bytes*8 - cbits;
467 - if(cwords < br->words) { /* if we've not consumed up to a partial tail word... */
469 - /* this also works when consumed_bits==0, it's just slower than necessary for that case */
470 - const unsigned n = FLAC__BITS_PER_WORD - cbits;
471 - const brword word = br->buffer[cwords];
472 - if(parameter < n) {
473 - uval <<= parameter;
474 - uval |= (word & (FLAC__WORD_ALL_ONES >> cbits)) >> (n-parameter);
475 - cbits += parameter;
479 - uval |= word & (FLAC__WORD_ALL_ONES >> cbits);
480 - crc16_update_word_(br, word);
482 - cbits = parameter - n;
483 - if(cbits) { /* parameter > n, i.e. if there are still bits left to read, there have to be less than 32 so they will all be in the next word */
485 - uval |= (br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits));
491 - uval <<= parameter;
492 - uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
496 - /* in this case we're starting our read at a partial tail word;
497 - * the reader has guaranteed that we have at least 'parameter'
498 - * bits available to read, which makes this case simpler.
500 - uval <<= parameter;
502 - /* this also works when consumed_bits==0, it's just a little slower than necessary for that case */
503 - FLAC__ASSERT(cbits + parameter <= br->bytes*8);
504 - uval |= (br->buffer[cwords] & (FLAC__WORD_ALL_ONES >> cbits)) >> (FLAC__BITS_PER_WORD-cbits-parameter);
505 - cbits += parameter;
509 - uval |= br->buffer[cwords] >> (FLAC__BITS_PER_WORD-cbits);
514 - ucbits -= parameter;
516 - /* compose the value */
517 - *vals = (int)(uval >> 1 ^ -(int)(uval & 1));
518 + /* read the binary LSBs */
519 + if(!FLAC__bitreader_read_raw_uint32(br, &lsbs, parameter - ucbits))
526 - br->consumed_bits = cbits;
527 - br->consumed_words = cwords;
530 + /* compose the value */
531 + x = (msbs << parameter) | lsbs;
532 + *val++ = (int)(x >> 1) ^ -(int)(x & 1);
537 + cwords = br->consumed_words;
539 + ucbits = FLAC__BITS_PER_WORD - br->consumed_bits;
540 + b = br->buffer[cwords] << br->consumed_bits;
541 + } while(cwords >= words && val < end);
544 + if(ucbits == 0 && cwords < words) {
545 + /* don't leave the head word with no unconsumed bits */
546 + crc16_update_word_(br, br->buffer[cwords++]);
547 + ucbits = FLAC__BITS_PER_WORD;
550 + br->consumed_bits = FLAC__BITS_PER_WORD - ucbits;
551 + br->consumed_words = cwords;
558 FLAC__bool FLAC__bitreader_read_golomb_signed(FLAC__BitReader *br, int *val, unsigned parameter)