summaryrefslogtreecommitdiff
path: root/lib/libc/string/ffs.c
blob: de4c205cd040692b6817e7d8e47ba2c657f7f4ee (plain)
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
/*	$OpenBSD: ffs.c,v 1.4 2000/07/02 01:00:01 mickey Exp $	*/

/*
 * Public domain.
 * Written by Dale Rahn.
 */

#if defined(LIBC_SCCS) && !defined(lint)
static char *rcsid = "$OpenBSD: ffs.c,v 1.4 2000/07/02 01:00:01 mickey Exp $";
#endif /* LIBC_SCCS and not lint */

#if !defined(_KERNEL) && !defined(_STANDALONE)
#include <string.h>
#else
#include <lib/libkern/libkern.h>
#endif

/*
 * ffs -- vax ffs instruction
 */
int
ffs(mask)
	register int mask;
{
	register int bit;
	register unsigned int r = mask;
	static const signed char t[16] = {
		-28, 1, 2, 1,
		  3, 1, 2, 1,
		  4, 1, 2, 1,
		  3, 1, 2, 1 };

	bit = 0;
	if (0 == (r & 0xffff)) {
		bit += 16;
		r >>= 16;
	}
	if (0 == (r & 0xff)) {
		bit += 8;
		r >>= 8;
	}
	if (0 == (r & 0xf)) {
		bit += 4;
		r >>= 4;
	}

	return (bit + t[ r & 0xf ]);
}