Skip to content

Commit 4a83160

Browse files
committed
Use std::countl_zero()
1 parent 7530960 commit 4a83160

1 file changed

Lines changed: 7 additions & 18 deletions

File tree

libdivide.h

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626

2727
#if defined(_MSC_VER) && (defined(__cplusplus) && (__cplusplus >= 202002L)) || \
2828
(defined(_MSVC_LANG) && (_MSVC_LANG >= 202002L))
29-
#include <limits.h>
30-
#include <type_traits>
29+
#if __has_include(<bit>)
30+
#include <bit>
3131
#define LIBDIVIDE_VC_CXX20
3232
#endif
33+
#endif
3334

3435
#if defined(LIBDIVIDE_SSE2)
3536
#include <emmintrin.h>
@@ -160,14 +161,8 @@ namespace libdivide {
160161

161162
static LIBDIVIDE_CONSTEXPR_INLINE int __builtin_clz(unsigned x) {
162163
#if defined(LIBDIVIDE_VC_CXX20)
163-
if (std::is_constant_evaluated()) {
164-
for (int i = 0; i < sizeof(x) * CHAR_BIT; ++i) {
165-
if (x >> (sizeof(x) * CHAR_BIT - 1 - i)) return i;
166-
}
167-
return sizeof(x) * CHAR_BIT;
168-
}
169-
#endif
170-
#if defined(_M_ARM) || defined(_M_ARM64) || defined(_M_HYBRID_X86_ARM64) || defined(_M_ARM64EC)
164+
return std::countl_zero(x);
165+
#elif defined(_M_ARM) || defined(_M_ARM64) || defined(_M_HYBRID_X86_ARM64) || defined(_M_ARM64EC)
171166
return (int)_CountLeadingZeros(x);
172167
#elif defined(__AVX2__) || defined(__LZCNT__)
173168
return (int)_lzcnt_u32(x);
@@ -180,14 +175,8 @@ static LIBDIVIDE_CONSTEXPR_INLINE int __builtin_clz(unsigned x) {
180175

181176
static LIBDIVIDE_CONSTEXPR_INLINE int __builtin_clzll(unsigned long long x) {
182177
#if defined(LIBDIVIDE_VC_CXX20)
183-
if (std::is_constant_evaluated()) {
184-
for (int i = 0; i < sizeof(x) * CHAR_BIT; ++i) {
185-
if (x >> (sizeof(x) * CHAR_BIT - 1 - i)) return i;
186-
}
187-
return sizeof(x) * CHAR_BIT;
188-
}
189-
#endif
190-
#if defined(_M_ARM) || defined(_M_ARM64) || defined(_M_HYBRID_X86_ARM64) || defined(_M_ARM64EC)
178+
return std::countl_zero(x);
179+
#elif defined(_M_ARM) || defined(_M_ARM64) || defined(_M_HYBRID_X86_ARM64) || defined(_M_ARM64EC)
191180
return (int)_CountLeadingZeros64(x);
192181
#elif defined(_WIN64)
193182
#if defined(__AVX2__) || defined(__LZCNT__)

0 commit comments

Comments
 (0)