1 /* count-leading-zeros.h -- counts the number of leading 0 bits in a word.
2 Copyright (C) 2012-2023 Free Software Foundation, Inc.
3
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8
9 This file 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 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 /* Written by Eric Blake. */
18
19 #ifndef COUNT_LEADING_ZEROS_H
20 #define COUNT_LEADING_ZEROS_H 1
21
22 #include <limits.h>
23 #include <stdlib.h>
24
25 #ifndef _GL_INLINE_HEADER_BEGIN
26 #error "Please include config.h first."
27 #endif
28 _GL_INLINE_HEADER_BEGIN
29 #ifndef COUNT_LEADING_ZEROS_INLINE
30 # define COUNT_LEADING_ZEROS_INLINE _GL_INLINE
31 #endif
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
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;
45 #elif _MSC_VER
46 # pragma intrinsic (_BitScanReverse)
47 # if defined _M_X64
48 # pragma intrinsic (_BitScanReverse64)
49 # endif
50 # define COUNT_LEADING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE) \
51 do \
52 { \
53 unsigned long result; \
54 if (MSC_BUILTIN (&result, x)) \
55 return CHAR_BIT * sizeof x - 1 - result; \
56 return CHAR_BIT * sizeof x; \
57 } \
58 while (0)
59 #else
60 # define COUNT_LEADING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE) \
61 do \
62 { \
63 int count; \
64 unsigned int leading_32; \
65 if (! x) \
66 return CHAR_BIT * sizeof x; \
67 for (count = 0; \
68 (leading_32 = ((x >> (sizeof (TYPE) * CHAR_BIT - 32)) \
69 & 0xffffffffU), \
70 count < CHAR_BIT * sizeof x - 32 && !leading_32); \
71 count += 32) \
72 x = x << 31 << 1; \
73 return count + count_leading_zeros_32 (leading_32); \
74 } \
75 while (0)
76
77 /* Compute and return the number of leading zeros in X,
78 where 0 < X < 2**32. */
79 COUNT_LEADING_ZEROS_INLINE int
80 count_leading_zeros_32 (unsigned int x)
81 {
82 /* <https://github.com/gibsjose/BitHacks>
83 <https://www.fit.vutbr.cz/~ibarina/pub/bithacks.pdf> */
84 static const char de_Bruijn_lookup[32] = {
85 31, 22, 30, 21, 18, 10, 29, 2, 20, 17, 15, 13, 9, 6, 28, 1,
86 23, 19, 11, 3, 16, 14, 7, 24, 12, 4, 8, 25, 5, 26, 27, 0
87 };
88
89 x |= x >> 1;
90 x |= x >> 2;
91 x |= x >> 4;
92 x |= x >> 8;
93 x |= x >> 16;
94 return de_Bruijn_lookup[((x * 0x07c4acddU) & 0xffffffffU) >> 27];
95 }
96 #endif
97
98 /* Compute and return the number of leading zeros in X. */
99 COUNT_LEADING_ZEROS_INLINE int
100 count_leading_zeros (unsigned int x)
101 {
102 COUNT_LEADING_ZEROS (__builtin_clz, _BitScanReverse, unsigned int);
103 }
104
105 /* Compute and return the number of leading zeros in X. */
106 COUNT_LEADING_ZEROS_INLINE int
107 count_leading_zeros_l (unsigned long int x)
108 {
109 COUNT_LEADING_ZEROS (__builtin_clzl, _BitScanReverse, unsigned long int);
110 }
111
112 /* Compute and return the number of leading zeros in X. */
113 COUNT_LEADING_ZEROS_INLINE int
114 count_leading_zeros_ll (unsigned long long int x)
115 {
116 #if (defined _MSC_VER && !defined __clang__) && !defined _M_X64
117 /* 32-bit MSVC does not have _BitScanReverse64, only _BitScanReverse. */
118 unsigned long result;
119 if (_BitScanReverse (&result, (unsigned long) (x >> 32)))
120 return CHAR_BIT * sizeof x - 1 - 32 - result;
121 if (_BitScanReverse (&result, (unsigned long) x))
122 return CHAR_BIT * sizeof x - 1 - result;
123 return CHAR_BIT * sizeof x;
124 #else
125 COUNT_LEADING_ZEROS (__builtin_clzll, _BitScanReverse64,
126 unsigned long long int);
127 #endif
128 }
129
130 #ifdef __cplusplus
131 }
132 #endif
133
134 _GL_INLINE_HEADER_END
135
136 #endif /* COUNT_LEADING_ZEROS_H */