Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 9 additions & 19 deletions llvm/include/llvm/ADT/bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,12 @@ template <typename T> [[nodiscard]] T bit_ceil(T Value) {
return T(1) << llvm::bit_width<T>(Value - 1u);
}

namespace detail {
template <typename T, std::size_t SizeOfT> struct PopulationCounter {
static int count(T Value) {
// Generic version, forward to 32 bits.
static_assert(SizeOfT <= 4, "Not implemented!");
/// Count the number of set bits in a value.
/// Ex. popcount(0xF000F000) = 8
/// Returns 0 if the word is zero.
template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
[[nodiscard]] inline int popcount(T Value) noexcept {
if constexpr (sizeof(T) <= 4) {
#if defined(__GNUC__)
return (int)__builtin_popcount(Value);
#else
Expand All @@ -313,11 +314,7 @@ template <typename T, std::size_t SizeOfT> struct PopulationCounter {
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
return int(((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24);
#endif
}
};

template <typename T> struct PopulationCounter<T, 8> {
static int count(T Value) {
} else if constexpr (sizeof(T) <= 8) {
#if defined(__GNUC__)
return (int)__builtin_popcountll(Value);
#else
Expand All @@ -327,16 +324,9 @@ template <typename T> struct PopulationCounter<T, 8> {
v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
return int((uint64_t)(v * 0x0101010101010101ULL) >> 56);
#endif
} else {
static_assert(sizeof(T) == 0, "T must be 8 bytes or less");
}
};
} // namespace detail

/// Count the number of set bits in a value.
/// Ex. popcount(0xF000F000) = 8
/// Returns 0 if the word is zero.
template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
[[nodiscard]] inline int popcount(T Value) noexcept {
return detail::PopulationCounter<T, sizeof(T)>::count(Value);
}

// Forward-declare rotr so that rotl can use it.
Expand Down
Loading