diff options
Diffstat (limited to 'src/lib/libc/string/ffs.c')
-rw-r--r-- | src/lib/libc/string/ffs.c | 44 |
1 files changed, 44 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..7dec1613a8 --- /dev/null +++ b/src/lib/libc/string/ffs.c | |||
@@ -0,0 +1,44 @@ | |||
1 | /* $OpenBSD: ffs.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Public domain. | ||
5 | * Written by Dale Rahn. | ||
6 | */ | ||
7 | |||
8 | #if !defined(_KERNEL) && !defined(_STANDALONE) | ||
9 | #include <string.h> | ||
10 | #else | ||
11 | #include <lib/libkern/libkern.h> | ||
12 | #endif | ||
13 | |||
14 | /* | ||
15 | * ffs -- vax ffs instruction | ||
16 | */ | ||
17 | int | ||
18 | ffs(int mask) | ||
19 | { | ||
20 | int bit; | ||
21 | unsigned int r = mask; | ||
22 | static const signed char t[16] = { | ||
23 | -28, 1, 2, 1, | ||
24 | 3, 1, 2, 1, | ||
25 | 4, 1, 2, 1, | ||
26 | 3, 1, 2, 1 | ||
27 | }; | ||
28 | |||
29 | bit = 0; | ||
30 | if (!(r & 0xffff)) { | ||
31 | bit += 16; | ||
32 | r >>= 16; | ||
33 | } | ||
34 | if (!(r & 0xff)) { | ||
35 | bit += 8; | ||
36 | r >>= 8; | ||
37 | } | ||
38 | if (!(r & 0xf)) { | ||
39 | bit += 4; | ||
40 | r >>= 4; | ||
41 | } | ||
42 | |||
43 | return (bit + t[ r & 0xf ]); | ||
44 | } | ||