OSDN Git Service

hidden_def/hidden_proto: convert all users (I hope) termios split, add some missing...
[uclinux-h8/uClibc.git] / libc / string / ffs.c
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 /* ffsl,ffsll */
9
10 #include "_string.h"
11
12 libc_hidden_proto(ffs)
13
14 int ffs(int i)
15 {
16 #if 1
17         /* inlined binary search method */
18         char n = 1;
19 #if UINT_MAX == 0xffffU
20         /* nothing to do here -- just trying to avoiding possible problems */
21 #elif UINT_MAX == 0xffffffffU
22         if (!(i & 0xffff)) {
23                 n += 16;
24                 i >>= 16;
25         }
26 #else
27 #error ffs needs rewriting!
28 #endif
29
30         if (!(i & 0xff)) {
31                 n += 8;
32                 i >>= 8;
33         }
34         if (!(i & 0x0f)) {
35                 n += 4;
36                 i >>= 4;
37         }
38         if (!(i & 0x03)) {
39                 n += 2;
40                 i >>= 2;
41         }
42         return (i) ? (n + ((i+1) & 0x01)) : 0;
43
44 #else
45         /* linear search -- slow, but small */
46         int n;
47
48         for (n = 0 ; i ; ++n) {
49                 i >>= 1;
50         }
51         
52         return n;
53 #endif
54 }
55 libc_hidden_def(ffs)