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