This source file includes following definitions.
- count_trailing_zeros_32
- count_trailing_zeros
- count_trailing_zeros_l
- count_trailing_zeros_ll
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #ifndef COUNT_TRAILING_ZEROS_H
20 #define COUNT_TRAILING_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_TRAILING_ZEROS_INLINE
30 # define COUNT_TRAILING_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_TRAILING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE) \
44 return x ? BUILTIN (x) : CHAR_BIT * sizeof x;
45 #elif _MSC_VER
46 # pragma intrinsic (_BitScanForward)
47 # if defined _M_X64
48 # pragma intrinsic (_BitScanForward64)
49 # endif
50 # define COUNT_TRAILING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE) \
51 do \
52 { \
53 unsigned long result; \
54 return MSC_BUILTIN (&result, x) ? result : CHAR_BIT * sizeof x; \
55 } \
56 while (0)
57 #else
58 # define COUNT_TRAILING_ZEROS(BUILTIN, MSC_BUILTIN, TYPE) \
59 do \
60 { \
61 int count = 0; \
62 if (! x) \
63 return CHAR_BIT * sizeof x; \
64 for (count = 0; \
65 (count < CHAR_BIT * sizeof x - 32 \
66 && ! (x & 0xffffffffU)); \
67 count += 32) \
68 x = x >> 31 >> 1; \
69 return count + count_trailing_zeros_32 (x); \
70 } \
71 while (0)
72
73
74
75 COUNT_TRAILING_ZEROS_INLINE int
76 count_trailing_zeros_32 (unsigned int x)
77 {
78
79
80 static const char de_Bruijn_lookup[32] = {
81 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
82 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
83 };
84 return de_Bruijn_lookup[(((x & -x) * 0x077cb531U) & 0xffffffffU) >> 27];
85 }
86 #endif
87
88
89 COUNT_TRAILING_ZEROS_INLINE int
90 count_trailing_zeros (unsigned int x)
91 {
92 COUNT_TRAILING_ZEROS (__builtin_ctz, _BitScanForward, unsigned int);
93 }
94
95
96 COUNT_TRAILING_ZEROS_INLINE int
97 count_trailing_zeros_l (unsigned long int x)
98 {
99 COUNT_TRAILING_ZEROS (__builtin_ctzl, _BitScanForward, unsigned long int);
100 }
101
102
103 COUNT_TRAILING_ZEROS_INLINE int
104 count_trailing_zeros_ll (unsigned long long int x)
105 {
106 #if (defined _MSC_VER && !defined __clang__) && !defined _M_X64
107
108 unsigned long result;
109 if (_BitScanForward (&result, (unsigned long) x))
110 return result;
111 if (_BitScanForward (&result, (unsigned long) (x >> 32)))
112 return result + 32;
113 return CHAR_BIT * sizeof x;
114 #else
115 COUNT_TRAILING_ZEROS (__builtin_ctzll, _BitScanForward64,
116 unsigned long long int);
117 #endif
118 }
119
120 #ifdef __cplusplus
121 }
122 #endif
123
124 _GL_INLINE_HEADER_END
125
126 #endif