From 347992e9d93ed0f91cde7e2d580cfdc8d5eb95f4 Mon Sep 17 00:00:00 2001 From: Dave Chapman Date: Fri, 3 Feb 2006 08:27:53 +0000 Subject: Optimised C version of count_leading_zeros() taken from alac-0.1.1. This makes ALAC very close to realtime on the ipod (just the very occasional skip during disk reading - it is realtime when the disk is sleeping). git-svn-id: svn://svn.rockbox.org/rockbox/trunk@8545 a1c6a512-1295-4272-9138-f99709370657 --- apps/codecs/libalac/alac.c | 48 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/apps/codecs/libalac/alac.c b/apps/codecs/libalac/alac.c index 638ca93c16..0082b6dd42 100644 --- a/apps/codecs/libalac/alac.c +++ b/apps/codecs/libalac/alac.c @@ -184,18 +184,48 @@ static inline void unreadbits(alac_file *alac, int bits) alac->input_buffer_bitaccumulator *= -1; } -/* hideously inefficient. could use a bitmask search, - * alternatively bsr on x86, - */ -static inline int count_leading_zeros(int32_t input) +static inline int count_leading_zeros(int input) { - int i = 0; - while (!(0x80000000 & input) && i < 32) + int output = 0; + int curbyte = 0; + + curbyte = input >> 24; + if (curbyte) goto found; + output += 8; + + curbyte = input >> 16; + if (curbyte & 0xff) goto found; + output += 8; + + curbyte = input >> 8; + if (curbyte & 0xff) goto found; + output += 8; + + curbyte = input; + if (curbyte & 0xff) goto found; + output += 8; + + return output; + +found: + if (!(curbyte & 0xf0)) { - i++; - input = input << 1; + output += 4; } - return i; + else + curbyte >>= 4; + + if (curbyte & 0x8) + return output; + if (curbyte & 0x4) + return output + 1; + if (curbyte & 0x2) + return output + 2; + if (curbyte & 0x1) + return output + 3; + + /* shouldn't get here: */ + return output + 4; } void basterdised_rice_decompress(alac_file *alac, -- cgit v1.2.3