1 /*
2  * Copyright (C) 2002     Manuel Novoa III
3  * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
4  *
5  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
6  */
7 
8 #include <limits.h>
9 #include <string.h>
10 
ffs(int i)11 int ffs(int i)
12 {
13 #if 1
14 	/* inlined binary search method */
15 	char n = 1;
16 #if UINT_MAX == 0xffffU
17 	/* nothing to do here -- just trying to avoiding possible problems */
18 #elif UINT_MAX == 0xffffffffU
19 	if (!(i & 0xffff)) {
20 		n += 16;
21 		i >>= 16;
22 	}
23 #else
24 #error ffs needs rewriting!
25 #endif
26 
27 	if (!(i & 0xff)) {
28 		n += 8;
29 		i >>= 8;
30 	}
31 	if (!(i & 0x0f)) {
32 		n += 4;
33 		i >>= 4;
34 	}
35 	if (!(i & 0x03)) {
36 		n += 2;
37 		i >>= 2;
38 	}
39 	return (i) ? (n + ((i+1) & 0x01)) : 0;
40 
41 #else
42 	/* linear search -- slow, but small */
43 	int n;
44 
45 	for (n = 0 ; i ; ++n) {
46 		i >>= 1;
47 	}
48 
49 	return n;
50 #endif
51 }
52 libc_hidden_def(ffs)
53 #if ULONG_MAX == UINT_MAX
54 strong_alias_untyped(ffs, ffsl)
55 #endif
56