libdl: first execute all destructors, then munmap library
[uclibc-ng.git] / libc / string / ffs.c
blobf39d304b7a7dcc5b7624bd22501229caf1617fbb
1 /*
2 * Copyright (C) 2002 Manuel Novoa III
3 * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
5 * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
6 */
8 #include <limits.h>
9 #include <string.h>
11 int ffs(int i)
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;
23 #else
24 #error ffs needs rewriting!
25 #endif
27 if (!(i & 0xff)) {
28 n += 8;
29 i >>= 8;
31 if (!(i & 0x0f)) {
32 n += 4;
33 i >>= 4;
35 if (!(i & 0x03)) {
36 n += 2;
37 i >>= 2;
39 return (i) ? (n + ((i+1) & 0x01)) : 0;
41 #else
42 /* linear search -- slow, but small */
43 int n;
45 for (n = 0 ; i ; ++n) {
46 i >>= 1;
49 return n;
50 #endif
52 libc_hidden_def(ffs)
53 #if ULONG_MAX == UINT_MAX
54 strong_alias_untyped(ffs, ffsl)
55 #endif