diff options
Diffstat (limited to 'src/lib/libc/string/ffs.c')
-rw-r--r-- | src/lib/libc/string/ffs.c | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/src/lib/libc/string/ffs.c b/src/lib/libc/string/ffs.c new file mode 100644 index 0000000000..887ce437d4 --- /dev/null +++ b/src/lib/libc/string/ffs.c | |||
@@ -0,0 +1,49 @@ | |||
1 | /* $OpenBSD: ffs.c,v 1.5 2000/07/02 03:10:38 mickey Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Public domain. | ||
5 | * Written by Dale Rahn. | ||
6 | */ | ||
7 | |||
8 | #if defined(LIBC_SCCS) && !defined(lint) | ||
9 | static char *rcsid = "$OpenBSD: ffs.c,v 1.5 2000/07/02 03:10:38 mickey Exp $"; | ||
10 | #endif /* LIBC_SCCS and not lint */ | ||
11 | |||
12 | #if !defined(_KERNEL) && !defined(_STANDALONE) | ||
13 | #include <string.h> | ||
14 | #else | ||
15 | #include <lib/libkern/libkern.h> | ||
16 | #endif | ||
17 | |||
18 | /* | ||
19 | * ffs -- vax ffs instruction | ||
20 | */ | ||
21 | int | ||
22 | ffs(mask) | ||
23 | register int mask; | ||
24 | { | ||
25 | register int bit; | ||
26 | register unsigned int r = mask; | ||
27 | static const signed char t[16] = { | ||
28 | -28, 1, 2, 1, | ||
29 | 3, 1, 2, 1, | ||
30 | 4, 1, 2, 1, | ||
31 | 3, 1, 2, 1 | ||
32 | }; | ||
33 | |||
34 | bit = 0; | ||
35 | if (!(r & 0xffff)) { | ||
36 | bit += 16; | ||
37 | r >>= 16; | ||
38 | } | ||
39 | if (!(r & 0xff)) { | ||
40 | bit += 8; | ||
41 | r >>= 8; | ||
42 | } | ||
43 | if (!(r & 0xf)) { | ||
44 | bit += 4; | ||
45 | r >>= 4; | ||
46 | } | ||
47 | |||
48 | return (bit + t[ r & 0xf ]); | ||
49 | } | ||