Efficently scanning a bit array

Andrew Haley aph@redhat.com
Tue Jul 1 12:40:00 GMT 2008


Chris Jefferson 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!)
> 
> I'm currently doing shifting, but this performs poorly for long,
> sparse arrays. Is there a more efficient method of implementing this,
> possibly using built-ins?

__builtin_ffs

Andrew.



More information about the Gcc-help mailing list