Skip to content

Commit 3c0af41

Browse files
committed
Use standard functions in bit_util.h
1 parent 6a481e5 commit 3c0af41

File tree

1 file changed

+7
-96
lines changed

1 file changed

+7
-96
lines changed

cpp/src/arrow/util/bit_util.h

Lines changed: 7 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,7 @@
1717

1818
#pragma once
1919

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>
3421
#include <cstdint>
3522
#include <type_traits>
3623

@@ -66,8 +53,8 @@ static constexpr uint8_t kBytePopcount[] = {
6653
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,
6754
4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};
6855

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)); }
7158

7259
//
7360
// Bit-related computations on integer values
@@ -181,95 +168,19 @@ static inline uint64_t TrailingBits(uint64_t v, int num_bits) {
181168

182169
/// \brief Count the number of leading zeros in an unsigned integer.
183170
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);
202172
}
203173

204174
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);
223176
}
224177

225178
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);
248180
}
249181

250182
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);
273184
}
274185

275186
// Returns the minimum number of bits needed to represent an unsigned value

0 commit comments

Comments
 (0)