* lisp/emacs-lisp/ert.el (ert-run-tests-batch): Print selector.
[emacs.git] / lib / count-trailing-zeros.h
bloba4c78485c848d334fce898c1c9bf847da6318166
1 /* count-trailing-zeros.h -- counts the number of trailing 0 bits in a word.
2 Copyright 2013-2018 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Paul Eggert. */
19 #ifndef COUNT_TRAILING_ZEROS_H
20 #define COUNT_TRAILING_ZEROS_H 1
22 #include <limits.h>
23 #include <stdlib.h>
25 #ifndef _GL_INLINE_HEADER_BEGIN
26 #error "Please include config.h first."
27 #endif
28 _GL_INLINE_HEADER_BEGIN
29 #ifndef COUNT_TRAILING_ZEROS_INLINE
30 # define COUNT_TRAILING_ZEROS_INLINE _GL_INLINE
31 #endif
33 /* Assuming the GCC builtin is BUILTIN and the MSC builtin is MSC_BUILTIN,
34 expand to code that computes the number of trailing zeros of the local
35 variable 'x' of type TYPE (an unsigned integer type) and return it
36 from the current function. */
37 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
38 # define COUNT_TRAILING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE) \
39 return x ? BUILTIN (x) : CHAR_BIT * sizeof x;
40 #elif _MSC_VER
41 # pragma intrinsic _BitScanForward
42 # pragma intrinsic _BitScanForward64
43 # define COUNT_TRAILING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE) \
44 do \
45 { \
46 unsigned long result; \
47 return MSC_BUILTIN (&result, x) ? result : CHAR_BIT * sizeof x; \
48 } \
49 while (0)
50 #else
51 # define COUNT_TRAILING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE) \
52 do \
53 { \
54 int count = 0; \
55 if (! x) \
56 return CHAR_BIT * sizeof x; \
57 for (count = 0; \
58 (count < CHAR_BIT * sizeof x - 32 \
59 && ! (x & 0xffffffffU)); \
60 count += 32) \
61 x = x >> 31 >> 1; \
62 return count + count_trailing_zeros_32 (x); \
63 } \
64 while (0)
66 /* Compute and return the number of trailing zeros in the least
67 significant 32 bits of X. One of these bits must be nonzero. */
68 COUNT_TRAILING_ZEROS_INLINE int
69 count_trailing_zeros_32 (unsigned int x)
71 /* <https://github.com/gibsjose/BitHacks>
72 <http://www.fit.vutbr.cz/~ibarina/pub/bithacks.pdf> */
73 static const char de_Bruijn_lookup[32] = {
74 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
75 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
77 return de_Bruijn_lookup[(((x & -x) * 0x077cb531U) & 0xffffffffU) >> 27];
79 #endif
81 /* Compute and return the number of trailing zeros in X. */
82 COUNT_TRAILING_ZEROS_INLINE int
83 count_trailing_zeros (unsigned int x)
85 COUNT_TRAILING_ZEROS (__builtin_ctz, _BitScanForward, unsigned int);
88 /* Compute and return the number of trailing zeros in X. */
89 COUNT_TRAILING_ZEROS_INLINE int
90 count_trailing_zeros_l (unsigned long int x)
92 COUNT_TRAILING_ZEROS (__builtin_ctzl, _BitScanForward, unsigned long int);
95 #if HAVE_UNSIGNED_LONG_LONG_INT
96 /* Compute and return the number of trailing zeros in X. */
97 COUNT_TRAILING_ZEROS_INLINE int
98 count_trailing_zeros_ll (unsigned long long int x)
100 COUNT_TRAILING_ZEROS (__builtin_ctzll, _BitScanForward64,
101 unsigned long long int);
103 #endif
105 _GL_INLINE_HEADER_END
107 #endif