[llvm] [ADT] Add signed and unsigned mulHi and mulLo to APInt (PR #84719)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 10 22:42:03 PDT 2024


================
@@ -3094,3 +3094,31 @@ void llvm::LoadIntFromMemory(APInt &IntVal, const uint8_t *Src,
     memcpy(Dst + sizeof(uint64_t) - LoadBytes, Src, LoadBytes);
   }
 }
+
+APInt APIntOps::mulHiS(const APInt &C1, const APInt &C2) {
+  unsigned FullWidth = C1.getBitWidth() * 2;
+  APInt C1Ext = C1.sext(FullWidth);
+  APInt C2Ext = C2.sext(FullWidth);
+  return (C1Ext * C2Ext).extractBits(C1.getBitWidth(), C1.getBitWidth());
+}
+
+APInt APIntOps::mulHiU(const APInt &C1, const APInt &C2) {
+  unsigned FullWidth = C1.getBitWidth() * 2;
+  APInt C1Ext = C1.zext(FullWidth);
+  APInt C2Ext = C2.zext(FullWidth);
+  return (C1Ext * C2Ext).extractBits(C1.getBitWidth(), C1.getBitWidth());
+}
+
+APInt APIntOps::mulLoS(const APInt &C1, const APInt &C2) {
----------------
topperc wrote:

Isn't this equivalent to mul? The lower bits of the result don't care about the extension at all.

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


More information about the llvm-commits mailing list