[PATCH] D132407: [ADT] Add llvm::ctpop to <bit> helper wrapper

David Blaikie via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 22 16:46:31 PDT 2022


dblaikie added a comment.

In D132407#3740805 <https://reviews.llvm.org/D132407#3740805>, @kazu wrote:

>> Looks like at least here clang uses the same code in either case (but doesn't use a CPU intrinsic, whereas GCC does (but GCC doesn't optimize the non-intrinsic code into the intrinsic...)): https://godbolt.org/z/G5YM4bcM6 ?
>
> I get `popcnt` with `-march=native`.

Ah, great.

Could we remove the intrinsic special case then, and just keep the explicit implementation - if it boils down to the same thing anyway, it saves us maintaining conditional code, etc?



================
Comment at: llvm/include/llvm/ADT/bit.h:42-49
+#if defined(__GNUC__)
+    return (int)__builtin_popcount(Value);
+#else
+    uint32_t v = Value;
+    v = v - ((v >> 1) & 0x55555555);
+    v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
+    return int(((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24);
----------------



================
Comment at: llvm/include/llvm/ADT/bit.h:55-63
+#if defined(__GNUC__)
+    return (int)__builtin_popcountll(Value);
+#else
+    uint64_t v = Value;
+    v = v - ((v >> 1) & 0x5555555555555555ULL);
+    v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
+    v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
----------------



Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D132407/new/

https://reviews.llvm.org/D132407



More information about the llvm-commits mailing list