xref: /openbsd/sys/lib/libkern/ffs.c (revision 3cab2bb3)
1 /*	$OpenBSD: ffs.c,v 1.9 2014/06/10 04:16:57 deraadt Exp $	*/
2 
3 /*
4  * Public domain.
5  * Written by Dale Rahn.
6  */
7 
8 #include <lib/libkern/libkern.h>
9 
10 /*
11  * ffs -- vax ffs instruction
12  */
13 int
14 ffs(int mask)
15 {
16 	int bit;
17 	unsigned int r = mask;
18 	static const signed char t[16] = {
19 		-28, 1, 2, 1,
20 		  3, 1, 2, 1,
21 		  4, 1, 2, 1,
22 		  3, 1, 2, 1
23 	};
24 
25 	bit = 0;
26 	if (!(r & 0xffff)) {
27 		bit += 16;
28 		r >>= 16;
29 	}
30 	if (!(r & 0xff)) {
31 		bit += 8;
32 		r >>= 8;
33 	}
34 	if (!(r & 0xf)) {
35 		bit += 4;
36 		r >>= 4;
37 	}
38 
39 	return (bit + t[ r & 0xf ]);
40 }
41