Skip to content

Commit e2fa6fe

Browse files
committed
Simplify
1 parent 7643c1d commit e2fa6fe

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

libcxx/include/__bit/countl.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,25 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_clz(__uint128_t __x)
6868
}
6969
#endif // _LIBCPP_HAS_NO_INT128
7070

71+
#if __has_builtin(__builtin_clzg)
72+
7173
template <class _Tp>
7274
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __countl_zero(_Tp __t) _NOEXCEPT {
7375
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
7476
if (__t == 0)
7577
return numeric_limits<_Tp>::digits;
7678

77-
#if __has_builtin(__builtin_clzg)
7879
return __builtin_clzg(__t) - (numeric_limits<unsigned>::digits - numeric_limits<_Tp>::digits);
79-
#else
80+
}
81+
82+
#else // __has_builtin(__builtin_clzg)
83+
84+
template <class _Tp>
85+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __countl_zero(_Tp __t) _NOEXCEPT {
86+
static_assert(__libcpp_is_unsigned_integer<_Tp>::value, "__countl_zero requires an unsigned integer type");
87+
if (__t == 0)
88+
return numeric_limits<_Tp>::digits;
89+
8090
if (sizeof(_Tp) <= sizeof(unsigned int))
8191
return std::__libcpp_clz(static_cast<unsigned int>(__t)) -
8292
(numeric_limits<unsigned int>::digits - numeric_limits<_Tp>::digits);
@@ -98,9 +108,10 @@ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __countl_zero(_Tp __t) _
98108
}
99109
return __ret + __iter;
100110
}
101-
#endif // __has_builtin(__builtin_clzg)
102111
}
103112

113+
#endif // __has_builtin(__builtin_clzg)
114+
104115
#if _LIBCPP_STD_VER >= 20
105116

106117
template <__libcpp_unsigned_integer _Tp>

libcxx/include/__bit/countr.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,23 @@ _LIBCPP_NODISCARD inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_ct
4747
#endif
4848
}
4949

50+
#if __has_builtin(__builtin_ctzg)
51+
5052
template <class _Tp>
5153
_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __countr_zero(_Tp __t) _NOEXCEPT {
5254
if (__t == 0)
5355
return numeric_limits<_Tp>::digits;
5456

55-
#if __has_builtin(__builtin_ctz)
5657
return __builtin_ctz(__t);
57-
#else
58+
}
59+
60+
#else // __has_builtin(__builtin_ctzg)
61+
62+
template <class _Tp>
63+
_LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __countr_zero(_Tp __t) _NOEXCEPT {
64+
if (__t == 0)
65+
return numeric_limits<_Tp>::digits;
66+
5867
if (sizeof(_Tp) <= sizeof(unsigned int))
5968
return std::__libcpp_ctz(static_cast<unsigned int>(__t));
6069
else if (sizeof(_Tp) <= sizeof(unsigned long))
@@ -70,9 +79,10 @@ _LIBCPP_NODISCARD _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 int __coun
7079
}
7180
return __ret + std::__libcpp_ctz(static_cast<unsigned long long>(__t));
7281
}
73-
#endif // __has_builtin(__builtin_ctz)
7482
}
7583

84+
#endif // __has_builtin(__builtin_ctzg)
85+
7686
#if _LIBCPP_STD_VER >= 20
7787

7888
template <__libcpp_unsigned_integer _Tp>

libcxx/include/__bit/popcount.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,17 @@ inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __libcpp_popcount(unsigned lo
4949

5050
#if _LIBCPP_STD_VER >= 20
5151

52+
# if __has_builtin(__builtin_popcountg)
53+
54+
template <__libcpp_unsigned_integer _Tp>
55+
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noexcept {
56+
return __builtin_popcountg(__t);
57+
}
58+
59+
# else // __has_builtin(__builtin_popcountg)
60+
5261
template <__libcpp_unsigned_integer _Tp>
5362
_LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noexcept {
54-
# if __has_builtin(__builtin_popcount)
55-
return __builtin_popcount(__t);
56-
# else
5763
if (sizeof(_Tp) <= sizeof(unsigned int))
5864
return std::__libcpp_popcount(static_cast<unsigned int>(__t));
5965
else if (sizeof(_Tp) <= sizeof(unsigned long))
@@ -68,9 +74,10 @@ _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr int popcount(_Tp __t) noex
6874
}
6975
return __ret;
7076
}
71-
# endif // __has_builtin(__builtin_popcount)
7277
}
7378

79+
# endif // __has_builtin(__builtin_popcountg)
80+
7481
#endif // _LIBCPP_STD_VER >= 20
7582

7683
_LIBCPP_END_NAMESPACE_STD

0 commit comments

Comments
 (0)