Do you actually have a system that doesn't have HAVE_FFS set?
On my machine,
int main(int argc, char **argv) { return ffs(argc); }
compiles to
pushl %ebp movl %esp, %ebp movl $-1, %edx bsfl 8(%ebp), %eax cmove %edx, %eax addl $1, %eax popl %ebp ret
which is probably pretty fast.
Large lookup tables can dirty the L1 cache and slow other things down.
See also http://stackoverflow.com/questions/757059/position-of-least-significant-bit-...
On Fri, Mar 11, 2011 at 05:51:27AM -0800, Dan Kegel wrote:
Large lookup tables can dirty the L1 cache and slow other things down.
And even small tables can also be a slow data cache miss in the critical path. Of course, when benchmarking, the table is all in the cache! In real life it is likely to be absent (unless you are doing a lot of lookups together).
David