File: ffs.c

package info (click to toggle)
ruby2.1 2.1.5-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 59,972 kB
  • sloc: ruby: 625,579; ansic: 295,220; xml: 25,445; yacc: 9,155; lisp: 2,433; tcl: 949; makefile: 535; sh: 402; perl: 62; python: 47; awk: 36; asm: 35; sed: 31
file content (49 lines) | stat: -rw-r--r-- 689 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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;
}