xref: /openbsd-src/sys/lib/libkern/ffs.c (revision ce7e0fc6a9d74d25b78fb6ad846387717f5172b6)
1 /*	$OpenBSD: ffs.c,v 1.5 2000/07/02 03:09:44 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:09:44 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 }
50