Remove dependency of version.c on gc/gc.h
[ruby.git] / missing / ffs.c
blobbad99cf116d341b982d8171a30a5c90a3d1de9c5
1 /* ffs.c - find first set bit */
2 /* ffs() is defined by Single Unix Specification. */
4 #include "ruby.h"
6 int ffs(int arg)
8 unsigned int x = (unsigned int)arg;
9 int r;
11 if (x == 0)
12 return 0;
14 r = 1;
16 #if 32 < SIZEOF_INT * CHAR_BIT
17 if ((x & 0xffffffff) == 0) {
18 x >>= 32;
19 r += 32;
21 #endif
23 if ((x & 0xffff) == 0) {
24 x >>= 16;
25 r += 16;
28 if ((x & 0xff) == 0) {
29 x >>= 8;
30 r += 8;
33 if ((x & 0xf) == 0) {
34 x >>= 4;
35 r += 4;
38 if ((x & 0x3) == 0) {
39 x >>= 2;
40 r += 2;
43 if ((x & 0x1) == 0) {
44 x >>= 1;
45 r += 1;
48 return r;