* Peter Stuge peter@stuge.se [110225 21:48]:
Patrick Georgi wrote:
+int ffs(int i);
Wording in my ffs(3) page suggests that int can be 64 bit. We don't care?
So far the only compiler I know where this could happen is MSVC. Since we have an endless amount of GNUisms in all of our code that basically limits us to gcc/llvm/sun compiler/intel compiler/... it will never be a problem for us.
We might want to consider a slightly bigger but non-looping and faster implementation
int ffs(int i) { int count = 1;
if (i == 0) return 0;
if (!(i & 0xffff)) { i >>= 16; count += 16; }
if (!(i & 0xff)) { i >>= 8; count += 8; }
if (!(i & 0xf)) { i >>= 4; count += 4; }
if (!(i & 3)) { i >>= 2; count += 2; }
if (!(i & 1)) { i >>= 1; count += 1; }
return count; }
Stefan