This source file includes following definitions.
- count_leading_zeros_32
- count_leading_zeros
- count_leading_zeros_l
- count_leading_zeros_ll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
38
39
40
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
78
79 COUNT_LEADING_ZEROS_INLINE int
80 count_leading_zeros_32 (unsigned int x)
81 {
82
83
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
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
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
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
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