Skip to content

Commit a23d6d4

Browse files
[libc][support][bit] use new type generic builtins
These are new in clang-19+, gcc-14+.
1 parent bdb60e6 commit a23d6d4

File tree

1 file changed

+18
-0
lines changed
  • libc/src/__support/CPP

1 file changed

+18
-0
lines changed

libc/src/__support/CPP/bit.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ has_single_bit(T value) {
7272
/// Only unsigned integral types are allowed.
7373
///
7474
/// Returns cpp::numeric_limits<T>::digits on an input of 0.
75+
// clang-19+, gcc-14+
76+
#if __has_builtin(__builtin_ctzg)
77+
template <typename T>
78+
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
79+
countr_zero(T value) {
80+
return __builtin_ctzg(value, cpp::numeric_limits<T>::digits);
81+
}
82+
#else
7583
template <typename T>
7684
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
7785
countr_zero(T value) {
@@ -99,16 +107,25 @@ ADD_SPECIALIZATION(countr_zero, unsigned short, __builtin_ctzs)
99107
ADD_SPECIALIZATION(countr_zero, unsigned int, __builtin_ctz)
100108
ADD_SPECIALIZATION(countr_zero, unsigned long, __builtin_ctzl)
101109
ADD_SPECIALIZATION(countr_zero, unsigned long long, __builtin_ctzll)
110+
#endif // __has_builtin(__builtin_ctzg)
102111

103112
/// Count number of 0's from the most significant bit to the least
104113
/// stopping at the first 1.
105114
///
106115
/// Only unsigned integral types are allowed.
107116
///
108117
/// Returns cpp::numeric_limits<T>::digits on an input of 0.
118+
// clang-19+, gcc-14+
119+
#if __has_builtin(__builtin_clzg)
109120
template <typename T>
110121
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
111122
countl_zero(T value) {
123+
return __builtin_clzg(value, cpp::numeric_limits<T>::digits);
124+
}
125+
#else
126+
template <typename T [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<
127+
cpp::is_unsigned_v<T>, int>
128+
countl_zero(T value) {
112129
if (!value)
113130
return cpp::numeric_limits<T>::digits;
114131
// Bisection method.
@@ -129,6 +146,7 @@ ADD_SPECIALIZATION(countl_zero, unsigned short, __builtin_clzs)
129146
ADD_SPECIALIZATION(countl_zero, unsigned int, __builtin_clz)
130147
ADD_SPECIALIZATION(countl_zero, unsigned long, __builtin_clzl)
131148
ADD_SPECIALIZATION(countl_zero, unsigned long long, __builtin_clzll)
149+
#endif // __has_builtin(__builtin_clzg)
132150

133151
#undef ADD_SPECIALIZATION
134152

0 commit comments

Comments
 (0)