]> git.ipfire.org Git - thirdparty/glibc.git/blame - string/ffs.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / string / ffs.c
CommitLineData
04277e02 1/* Copyright (C) 1991-2019 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. */
26int
9d46370c 27__ffs (int i)
28f540f4 28{
ebbad4cc 29 static const unsigned char table[] =
28f540f4
RM
30 {
31 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,
32 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,
33 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,
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 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,
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 };
b20e47cb
RM
40 unsigned int a;
41 unsigned int x = i & -i;
28f540f4
RM
42
43 a = x <= 0xffff ? (x <= 0xff ? 0 : 8) : (x <= 0xffffff ? 16 : 24);
44
45 return table[x >> a] + a;
46}
19212f87 47weak_alias (__ffs, ffs)
5bb43a43 48libc_hidden_def (__ffs)
2dd18ce2 49libc_hidden_builtin_def (ffs)
bdd421cc
UD
50
51#if ULONG_MAX == UINT_MAX
52#undef ffsl
53weak_alias (__ffs, ffsl)
54#endif