1 /* count-leading-zeros.h -- counts the number of leading 0 bits in a word.
2 Copyright (C) 2012-2020 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 Eric Blake. */
19 #ifndef COUNT_LEADING_ZEROS_H
20 #define COUNT_LEADING_ZEROS_H 1
25 #ifndef _GL_INLINE_HEADER_BEGIN
26 #error "Please include config.h first."
28 _GL_INLINE_HEADER_BEGIN
29 #ifndef COUNT_LEADING_ZEROS_INLINE
30 # define COUNT_LEADING_ZEROS_INLINE _GL_INLINE
37 /* Assuming the GCC builtin is BUILTIN and the MSC builtin is MSC_BUILTIN,
38 expand to code that computes the number of leading zeros of the local
39 variable 'x' of type TYPE (an unsigned integer type) and return it
40 from the current function. */
41 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) \
42 || (__clang_major__ >= 4)
43 # define COUNT_LEADING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE) \
44 return x ? BUILTIN (x) : CHAR_BIT * sizeof x;
46 # pragma intrinsic _BitScanReverse
47 # pragma intrinsic _BitScanReverse64
48 # define COUNT_LEADING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE) \
51 unsigned long result; \
52 return MSC_BUILTIN (&result, x) ? result : CHAR_BIT * sizeof x; \
56 # define COUNT_LEADING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE) \
60 unsigned int leading_32; \
62 return CHAR_BIT * sizeof x; \
64 (leading_32 = ((x >> (sizeof (TYPE) * CHAR_BIT - 32)) \
66 count < CHAR_BIT * sizeof x - 32 && !leading_32); \
69 return count + count_leading_zeros_32 (leading_32); \
73 /* Compute and return the number of leading zeros in X,
74 where 0 < X < 2**32. */
75 COUNT_LEADING_ZEROS_INLINE
int
76 count_leading_zeros_32 (unsigned int x
)
78 /* <https://github.com/gibsjose/BitHacks>
79 <https://www.fit.vutbr.cz/~ibarina/pub/bithacks.pdf> */
80 static const char de_Bruijn_lookup
[32] = {
81 31, 22, 30, 21, 18, 10, 29, 2, 20, 17, 15, 13, 9, 6, 28, 1,
82 23, 19, 11, 3, 16, 14, 7, 24, 12, 4, 8, 25, 5, 26, 27, 0
90 return de_Bruijn_lookup
[((x
* 0x07c4acddU
) & 0xffffffffU
) >> 27];
94 /* Compute and return the number of leading zeros in X. */
95 COUNT_LEADING_ZEROS_INLINE
int
96 count_leading_zeros (unsigned int x
)
98 COUNT_LEADING_ZEROS (__builtin_clz
, _BitScanReverse
, unsigned int);
101 /* Compute and return the number of leading zeros in X. */
102 COUNT_LEADING_ZEROS_INLINE
int
103 count_leading_zeros_l (unsigned long int x
)
105 COUNT_LEADING_ZEROS (__builtin_clzl
, _BitScanReverse
, unsigned long int);
108 /* Compute and return the number of leading zeros in X. */
109 COUNT_LEADING_ZEROS_INLINE
int
110 count_leading_zeros_ll (unsigned long long int x
)
112 COUNT_LEADING_ZEROS (__builtin_clzll
, _BitScanReverse64
,
113 unsigned long long int);
120 _GL_INLINE_HEADER_END
122 #endif /* COUNT_LEADING_ZEROS_H */