Mercurial > gnulib
annotate lib/ffsl.h @ 40186:8964917f9574
autoupdate
author | Karl Berry <karl@freefriends.org> |
---|---|
date | Mon, 18 Feb 2019 08:02:49 -0800 |
parents | b06060465f09 |
children |
rev | line source |
---|---|
15428 | 1 /* ffsl.h -- find the first set bit in a word. |
40057
b06060465f09
maint: Run 'make update-copyright'
Paul Eggert <eggert@cs.ucla.edu>
parents:
19484
diff
changeset
|
2 Copyright (C) 2011-2019 Free Software Foundation, Inc. |
15428 | 3 |
4 This program is free software: you can redistribute it and/or modify | |
5 it under the terms of the GNU General Public License as published by | |
6 the Free Software Foundation; either version 3 of the License, or | |
7 (at your option) any later version. | |
8 | |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
19190 | 15 along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
15428 | 16 |
17 /* Written by Eric Blake. */ | |
18 | |
19 /* This file is meant to be included by ffsl.c and ffsll.c, after | |
20 they have defined FUNC and TYPE. */ | |
21 | |
16007
d057d14ac3bb
ffsl, ffsll: Avoid compilation error due to 'restrict'.
Bruno Haible <bruno@clisp.org>
parents:
15949
diff
changeset
|
22 #include <config.h> |
d057d14ac3bb
ffsl, ffsll: Avoid compilation error due to 'restrict'.
Bruno Haible <bruno@clisp.org>
parents:
15949
diff
changeset
|
23 |
d057d14ac3bb
ffsl, ffsll: Avoid compilation error due to 'restrict'.
Bruno Haible <bruno@clisp.org>
parents:
15949
diff
changeset
|
24 /* Specification. */ |
15428 | 25 #include <string.h> |
26 | |
27 #include <limits.h> | |
28 #include <strings.h> | |
29 | |
30 #if !defined FUNC || !defined TYPE | |
31 # error | |
32 #endif | |
33 | |
34 int | |
35 FUNC (TYPE i) | |
36 { | |
15946
d0eb709725f5
ffsl, ffsll: Optimize for GCC.
Bruno Haible <bruno@clisp.org>
parents:
15430
diff
changeset
|
37 #if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)) && defined GCC_BUILTIN |
d0eb709725f5
ffsl, ffsll: Optimize for GCC.
Bruno Haible <bruno@clisp.org>
parents:
15430
diff
changeset
|
38 return GCC_BUILTIN (i); |
d0eb709725f5
ffsl, ffsll: Optimize for GCC.
Bruno Haible <bruno@clisp.org>
parents:
15430
diff
changeset
|
39 #else |
15949
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
40 unsigned TYPE j = i; |
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
41 /* Split j into chunks, and look at one chunk after the other. */ |
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
42 enum { chunk_bits = CHAR_BIT * sizeof (unsigned int) }; |
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
43 /* The number of chunks is ceil (sizeof (TYPE) / sizeof (unsigned int)) |
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
44 = (sizeof (TYPE) - 1) / sizeof (unsigned int) + 1. */ |
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
45 enum { chunk_count = (sizeof (TYPE) - 1) / sizeof (unsigned int) + 1 }; |
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
46 |
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
47 if (chunk_count > 1) |
15947
d1ff4390df13
ffsl: Optimize on 32-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15946
diff
changeset
|
48 { |
15949
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
49 size_t k; |
15428 | 50 |
15947
d1ff4390df13
ffsl: Optimize on 32-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15946
diff
changeset
|
51 /* It is tempting to write if (!j) here, but if we do this, |
d1ff4390df13
ffsl: Optimize on 32-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15946
diff
changeset
|
52 Solaris 10/x86 "cc -O" miscompiles the code. */ |
d1ff4390df13
ffsl: Optimize on 32-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15946
diff
changeset
|
53 if (!i) |
d1ff4390df13
ffsl: Optimize on 32-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15946
diff
changeset
|
54 return 0; |
15949
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
55 /* Unroll the first loop round. k = 0. */ |
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
56 if ((unsigned int) j) |
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
57 return ffs ((unsigned int) j); |
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
58 /* Generic loop. */ |
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
59 for (k = 1; k < chunk_count - 1; k++) |
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
60 if ((unsigned int) (j >> (k * chunk_bits)) != 0) |
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
61 return k * chunk_bits + ffs ((unsigned int) (j >> (k * chunk_bits))); |
15428 | 62 } |
15949
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
63 /* Last loop round. k = chunk_count - 1. */ |
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
64 return (chunk_count - 1) * chunk_bits |
0319356f65c3
ffsl: Optimize on 64-bit platforms.
Bruno Haible <bruno@clisp.org>
parents:
15947
diff
changeset
|
65 + ffs ((unsigned int) (j >> ((chunk_count - 1) * chunk_bits))); |
15946
d0eb709725f5
ffsl, ffsll: Optimize for GCC.
Bruno Haible <bruno@clisp.org>
parents:
15430
diff
changeset
|
66 #endif |
15428 | 67 } |