Skip to content

Commit b1e40ed

Browse files
utilize __builtin_popcountg
1 parent 457a41d commit b1e40ed

File tree

1 file changed

+9
-1
lines changed
  • libc/src/__support/CPP

1 file changed

+9
-1
lines changed

libc/src/__support/CPP/bit.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@ LIBC_INLINE constexpr To bit_or_static_cast(const From &from) {
242242
/// Count number of 1's aka population count or Hamming weight.
243243
///
244244
/// Only unsigned integral types are allowed.
245+
// clang-19+, gcc-14+
246+
#if __has_builtin(__builtin_popcountg)
247+
template <typename T>
248+
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
249+
popcount(T value) {
250+
return __builtin_popcountg(value);
251+
}
252+
#else // !__has_builtin(__builtin_popcountg)
245253
template <typename T>
246254
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
247255
popcount(T value) {
@@ -261,7 +269,7 @@ ADD_SPECIALIZATION(unsigned short, __builtin_popcount)
261269
ADD_SPECIALIZATION(unsigned, __builtin_popcount)
262270
ADD_SPECIALIZATION(unsigned long, __builtin_popcountl)
263271
ADD_SPECIALIZATION(unsigned long long, __builtin_popcountll)
264-
// TODO: 128b specializations?
272+
#endif // __builtin_popcountg
265273
#undef ADD_SPECIALIZATION
266274

267275
} // namespace LIBC_NAMESPACE::cpp

0 commit comments

Comments
 (0)