Efficently scanning a bit array

Andrew Bell andrew.bell.ia@gmail.com
Tue Jul 1 13:18:00 GMT 2008


On Tue, Jul 1, 2008 at 3:40 AM, Chris Jefferson <chris@bubblescope.net> wrote:
> I want to implement the following function efficently:
>
> void check_bits(char* ptr, int length)
> {
>  for each bit in the array starting at ptr, of length 'length' which is true
>     f(position of bit);
> }
>
> Where the position of bit is it's position compared to the start of
> the array (so the first bit of *(ptr+2) is at position 16, assuming 8
> bits per byte!)

Why not first check the longest size that makes sense to see if any
bits are set at all if things are really sparse -- if (*(long *)
ptr))... and then check the bits if it is true, but perhaps
__builtin_ffs is just as fast.

-- 
Andrew Bell
andrew.bell.ia@gmail.com



More information about the Gcc-help mailing list