diff options
author | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-16 18:49:26 +0900 |
---|---|---|
committer | Jari Vetoniemi <jari.vetoniemi@indooratlas.com> | 2020-03-30 00:39:06 +0900 |
commit | fcbf63e62c627deae76c1b8cb8c0876c536ed811 (patch) | |
tree | 64cb17de3f41a2b6fef2368028fbd00349946994 /jni/ruby/missing/ffs.c |
Fresh start
Diffstat (limited to 'jni/ruby/missing/ffs.c')
-rw-r--r-- | jni/ruby/missing/ffs.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/jni/ruby/missing/ffs.c b/jni/ruby/missing/ffs.c new file mode 100644 index 0000000..bad99cf --- /dev/null +++ b/jni/ruby/missing/ffs.c @@ -0,0 +1,49 @@ +/* ffs.c - find first set bit */ +/* ffs() is defined by Single Unix Specification. */ + +#include "ruby.h" + +int ffs(int arg) +{ + unsigned int x = (unsigned int)arg; + int r; + + if (x == 0) + return 0; + + r = 1; + +#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; +} |