diff options
Diffstat (limited to 'missing/ffs.c')
-rw-r--r-- | missing/ffs.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/missing/ffs.c b/missing/ffs.c new file mode 100644 index 0000000000..9bbb707959 --- /dev/null +++ b/missing/ffs.c @@ -0,0 +1,47 @@ +/* ffs.c - find first set bit */ +/* ffs() is defined by POSIX. */ + +#include "ruby.h" + +int ffs(int arg) +{ + unsigned int x = (unsigned int)arg; + int r = 0; + + if (x == 0) + return 0; + +#if 32 < SIZEOF_INT * CHAR_BIT + if ((x & 0xffffffff) == 0) { + x >>= 32; + r += 32; + } +#endif + + if ((x & 0xffff) == 0) { + x >>= 16; + r += 16; + } + + if ((x & 0xff) == 0) { + x >>= 8; + r += 8; + } + + if ((x & 0xf) == 0) { + x >>= 4; + r += 4; + } + + if ((x & 0x3) == 0) { + x >>= 2; + r += 2; + } + + if ((x & 0x1) == 0) { + x >>= 1; + r += 1; + } + + return r; +} |