diff options
author | Ryan Beltran <rp.beltran@yahoo.com> | 2024-06-14 19:56:15 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-14 19:56:15 -0700 |
commit | 6f5dfbd73a2867019d52be546a89d4fb4224b83d (patch) | |
tree | 1b230f48902c2b07233327821fea2d3905620fcd /libc/src | |
parent | 8f74725731bf431fb97929e1dd962e9a0db20865 (diff) | |
download | llvm-6f5dfbd73a2867019d52be546a89d4fb4224b83d.zip llvm-6f5dfbd73a2867019d52be546a89d4fb4224b83d.tar.gz llvm-6f5dfbd73a2867019d52be546a89d4fb4224b83d.tar.bz2 |
[libc][__support][bit] Switch popcount to Brian Kernighan’s Algorithm (#95625)
Diffstat (limited to 'libc/src')
-rw-r--r-- | libc/src/__support/CPP/bit.h | 7 |
1 files changed, 4 insertions, 3 deletions
diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h index 8a8951a..4aea066 100644 --- a/libc/src/__support/CPP/bit.h +++ b/libc/src/__support/CPP/bit.h @@ -271,9 +271,10 @@ template <typename T> [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int> popcount(T value) { int count = 0; - for (int i = 0; i != cpp::numeric_limits<T>::digits; ++i) - if ((value >> i) & 0x1) - ++count; + while (value) { + value &= value - 1; + ++count; + } return count; } #define ADD_SPECIALIZATION(TYPE, BUILTIN) \ |