@@ -72,6 +72,14 @@ has_single_bit(T value) {
72
72
// / Only unsigned integral types are allowed.
73
73
// /
74
74
// / 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
75
83
template <typename T>
76
84
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp::is_unsigned_v<T>, int >
77
85
countr_zero (T value) {
@@ -99,16 +107,25 @@ ADD_SPECIALIZATION(countr_zero, unsigned short, __builtin_ctzs)
99
107
ADD_SPECIALIZATION (countr_zero, unsigned int , __builtin_ctz)
100
108
ADD_SPECIALIZATION(countr_zero, unsigned long , __builtin_ctzl)
101
109
ADD_SPECIALIZATION(countr_zero, unsigned long long , __builtin_ctzll)
110
+ #endif // __has_builtin(__builtin_ctzg)
102
111
103
112
// / Count number of 0's from the most significant bit to the least
104
113
// / stopping at the first 1.
105
114
// /
106
115
// / Only unsigned integral types are allowed.
107
116
// /
108
117
// / Returns cpp::numeric_limits<T>::digits on an input of 0.
118
+ // clang-19+, gcc-14+
119
+ #if __has_builtin(__builtin_clzg)
109
120
template <typename T>
110
121
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t <cpp::is_unsigned_v<T>, int >
111
122
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) {
112
129
if (!value)
113
130
return cpp::numeric_limits<T>::digits;
114
131
// Bisection method.
@@ -129,6 +146,7 @@ ADD_SPECIALIZATION(countl_zero, unsigned short, __builtin_clzs)
129
146
ADD_SPECIALIZATION (countl_zero, unsigned int , __builtin_clz)
130
147
ADD_SPECIALIZATION(countl_zero, unsigned long , __builtin_clzl)
131
148
ADD_SPECIALIZATION(countl_zero, unsigned long long , __builtin_clzll)
149
+ #endif // __has_builtin(__builtin_clzg)
132
150
133
151
#undef ADD_SPECIALIZATION
134
152
0 commit comments