[llvm] [APInt] Add `bitwiseParity`, `compressBits`, and `expandBits` operations (PR #200114)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Thu May 28 02:35:05 PDT 2026


================
@@ -3259,3 +3259,61 @@ APInt llvm::APIntOps::clmulh(const APInt &LHS, const APInt &RHS) {
   assert(LHS.getBitWidth() == RHS.getBitWidth());
   return clmulr(LHS, RHS).lshr(1);
 }
+
+APInt llvm::APIntOps::bitwiseParity(const APInt &V) {
+  // This computes `clmul(V, ~0)`,
+  // but we don't use `clmul` because this special case has lower complexity
+  // (logarithmic amount of loop iterations instead of linear in BW).
+  unsigned BW = V.getBitWidth();
+  APInt Result = V;
+  for (unsigned I = 1; I < BW; I <<= 1)
+    Result ^= Result.shl(I);
+  return Result;
----------------
RKSimon wrote:

can we implement this with APIntOps::clmul instead? or even avoid adding a bitwiseParity function at all and use the clmul calls directly?

https://github.com/llvm/llvm-project/pull/200114


More information about the llvm-commits mailing list