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