|
17 | 17 |
|
18 | 18 | #pragma once |
19 | 19 |
|
20 | | -#if defined(_MSC_VER) |
21 | | -# if defined(_M_AMD64) || defined(_M_X64) |
22 | | -# include <intrin.h> // IWYU pragma: keep |
23 | | -# endif |
24 | | - |
25 | | -# pragma intrinsic(_BitScanReverse) |
26 | | -# pragma intrinsic(_BitScanForward) |
27 | | -# define ARROW_POPCOUNT64 __popcnt64 |
28 | | -# define ARROW_POPCOUNT32 __popcnt |
29 | | -#else |
30 | | -# define ARROW_POPCOUNT64 __builtin_popcountll |
31 | | -# define ARROW_POPCOUNT32 __builtin_popcount |
32 | | -#endif |
33 | | - |
| 20 | +#include <bit> |
34 | 21 | #include <cstdint> |
35 | 22 | #include <type_traits> |
36 | 23 |
|
@@ -66,8 +53,8 @@ static constexpr uint8_t kBytePopcount[] = { |
66 | 53 | 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, |
67 | 54 | 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8}; |
68 | 55 |
|
69 | | -static inline uint64_t PopCount(uint64_t bitmap) { return ARROW_POPCOUNT64(bitmap); } |
70 | | -static inline uint32_t PopCount(uint32_t bitmap) { return ARROW_POPCOUNT32(bitmap); } |
| 56 | +static inline uint64_t PopCount(uint64_t bitmap) { return static_cast<uint64_t>(std::popcount(bitmap)); } |
| 57 | +static inline uint32_t PopCount(uint32_t bitmap) { return static_cast<uint32_t>(std::popcount(bitmap)); } |
71 | 58 |
|
72 | 59 | // |
73 | 60 | // Bit-related computations on integer values |
@@ -181,95 +168,19 @@ static inline uint64_t TrailingBits(uint64_t v, int num_bits) { |
181 | 168 |
|
182 | 169 | /// \brief Count the number of leading zeros in an unsigned integer. |
183 | 170 | static inline int CountLeadingZeros(uint32_t value) { |
184 | | -#if defined(__clang__) || defined(__GNUC__) |
185 | | - if (value == 0) return 32; |
186 | | - return static_cast<int>(__builtin_clz(value)); |
187 | | -#elif defined(_MSC_VER) |
188 | | - unsigned long index; // NOLINT |
189 | | - if (_BitScanReverse(&index, static_cast<unsigned long>(value))) { // NOLINT |
190 | | - return 31 - static_cast<int>(index); |
191 | | - } else { |
192 | | - return 32; |
193 | | - } |
194 | | -#else |
195 | | - int bitpos = 0; |
196 | | - while (value != 0) { |
197 | | - value >>= 1; |
198 | | - ++bitpos; |
199 | | - } |
200 | | - return 32 - bitpos; |
201 | | -#endif |
| 171 | + return std::countl_zero(value); |
202 | 172 | } |
203 | 173 |
|
204 | 174 | static inline int CountLeadingZeros(uint64_t value) { |
205 | | -#if defined(__clang__) || defined(__GNUC__) |
206 | | - if (value == 0) return 64; |
207 | | - return static_cast<int>(__builtin_clzll(value)); |
208 | | -#elif defined(_MSC_VER) |
209 | | - unsigned long index; // NOLINT |
210 | | - if (_BitScanReverse64(&index, value)) { // NOLINT |
211 | | - return 63 - static_cast<int>(index); |
212 | | - } else { |
213 | | - return 64; |
214 | | - } |
215 | | -#else |
216 | | - int bitpos = 0; |
217 | | - while (value != 0) { |
218 | | - value >>= 1; |
219 | | - ++bitpos; |
220 | | - } |
221 | | - return 64 - bitpos; |
222 | | -#endif |
| 175 | + return std::countl_zero(value); |
223 | 176 | } |
224 | 177 |
|
225 | 178 | static inline int CountTrailingZeros(uint32_t value) { |
226 | | -#if defined(__clang__) || defined(__GNUC__) |
227 | | - if (value == 0) return 32; |
228 | | - return static_cast<int>(__builtin_ctzl(value)); |
229 | | -#elif defined(_MSC_VER) |
230 | | - unsigned long index; // NOLINT |
231 | | - if (_BitScanForward(&index, value)) { |
232 | | - return static_cast<int>(index); |
233 | | - } else { |
234 | | - return 32; |
235 | | - } |
236 | | -#else |
237 | | - int bitpos = 0; |
238 | | - if (value) { |
239 | | - while (value & 1 == 0) { |
240 | | - value >>= 1; |
241 | | - ++bitpos; |
242 | | - } |
243 | | - } else { |
244 | | - bitpos = 32; |
245 | | - } |
246 | | - return bitpos; |
247 | | -#endif |
| 179 | + return std::countr_zero(value); |
248 | 180 | } |
249 | 181 |
|
250 | 182 | static inline int CountTrailingZeros(uint64_t value) { |
251 | | -#if defined(__clang__) || defined(__GNUC__) |
252 | | - if (value == 0) return 64; |
253 | | - return static_cast<int>(__builtin_ctzll(value)); |
254 | | -#elif defined(_MSC_VER) |
255 | | - unsigned long index; // NOLINT |
256 | | - if (_BitScanForward64(&index, value)) { |
257 | | - return static_cast<int>(index); |
258 | | - } else { |
259 | | - return 64; |
260 | | - } |
261 | | -#else |
262 | | - int bitpos = 0; |
263 | | - if (value) { |
264 | | - while (value & 1 == 0) { |
265 | | - value >>= 1; |
266 | | - ++bitpos; |
267 | | - } |
268 | | - } else { |
269 | | - bitpos = 64; |
270 | | - } |
271 | | - return bitpos; |
272 | | -#endif |
| 183 | + return std::countr_zero(value); |
273 | 184 | } |
274 | 185 |
|
275 | 186 | // Returns the minimum number of bits needed to represent an unsigned value |
|
0 commit comments