]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 1991-2015 Free Software Foundation, Inc. |
ebbad4cc | 2 | This file is part of the GNU C Library. |
28f540f4 RM |
3 | Contributed by Torbjorn Granlund (tege@sics.se). |
4 | ||
ebbad4cc | 5 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
28f540f4 | 9 | |
ebbad4cc UD |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
28f540f4 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
28f540f4 | 18 | |
28f540f4 | 19 | #include <limits.h> |
bdd421cc | 20 | #define ffsl __something_else |
28f540f4 RM |
21 | #include <string.h> |
22 | ||
23 | #undef ffs | |
24 | ||
28f540f4 RM |
25 | /* Find the first bit set in I. */ |
26 | int | |
19212f87 | 27 | __ffs (i) |
ebbad4cc | 28 | int i; |
28f540f4 | 29 | { |
ebbad4cc | 30 | static const unsigned char table[] = |
28f540f4 RM |
31 | { |
32 | 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, | |
33 | 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, | |
34 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, | |
35 | 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, | |
36 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, | |
37 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, | |
38 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8, | |
39 | 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8 | |
40 | }; | |
b20e47cb RM |
41 | unsigned int a; |
42 | unsigned int x = i & -i; | |
28f540f4 RM |
43 | |
44 | a = x <= 0xffff ? (x <= 0xff ? 0 : 8) : (x <= 0xffffff ? 16 : 24); | |
45 | ||
46 | return table[x >> a] + a; | |
47 | } | |
19212f87 | 48 | weak_alias (__ffs, ffs) |
5bb43a43 | 49 | libc_hidden_def (__ffs) |
2dd18ce2 | 50 | libc_hidden_builtin_def (ffs) |
bdd421cc UD |
51 | |
52 | #if ULONG_MAX == UINT_MAX | |
53 | #undef ffsl | |
54 | weak_alias (__ffs, ffsl) | |
55 | #endif |