[llvm] [APInt] Remove accumulator initialization from tcMultiply and tcFullMultiply. (PR #88202)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 9 14:53:48 PDT 2024


https://github.com/topperc created https://github.com/llvm/llvm-project/pull/88202

The tcMultiplyPart routine has a flag that says whether to add to the accumulator or overwrite it. By using the overwrite mode on the first iteration we don't need to initialize the accumulator to zero.

Note, the initialization in tcFullMultiply was only initializing the first rhsParts of dst. tcMultiplyPart always overwrites the rhsParts+1 part that just contains the last carry. The first write to each part of dst past rhsParts is a carry write so that's how the upper part of dst is initialized.

>From a246993c3f3c766c1e6055d062f1d2e721b630f2 Mon Sep 17 00:00:00 2001
From: Craig Topper <craig.topper at sifive.com>
Date: Tue, 9 Apr 2024 14:43:14 -0700
Subject: [PATCH] [APInt] Remove accumulator initialization from tcMultiply and
 tcFullMultiply.

The tcMultiplyPart routine has a flag that says whether to add to
the accumulator or overwrite it. By using the overwrite mode on
the first iteration we don't need to initialize the accumulator to
zero.

Note, the initialization in tcFullMultiply was only initializing
the first rhsParts of dst. tcMultiplyPart always overwrites the
rhsParts+1 part that just contains the last carry. The first
write to each part of dst past rhsParts is a carry write so that's
how the upper part of dst is initialized.
---
 llvm/lib/Support/APInt.cpp | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp
index 8825025ec32134..599c08a3c2b938 100644
--- a/llvm/lib/Support/APInt.cpp
+++ b/llvm/lib/Support/APInt.cpp
@@ -2585,11 +2585,13 @@ int APInt::tcMultiply(WordType *dst, const WordType *lhs,
   assert(dst != lhs && dst != rhs);
 
   int overflow = 0;
-  tcSet(dst, 0, parts);
 
-  for (unsigned i = 0; i < parts; i++)
+  for (unsigned i = 0; i < parts; i++) {
+    // Don't accumulate on the first iteration so we don't need to initalize
+    // dst to 0.
     overflow |= tcMultiplyPart(&dst[i], lhs, rhs[i], 0, parts,
-                               parts - i, true);
+                               parts - i, i != 0);
+  }
 
   return overflow;
 }
@@ -2605,10 +2607,11 @@ void APInt::tcFullMultiply(WordType *dst, const WordType *lhs,
 
   assert(dst != lhs && dst != rhs);
 
-  tcSet(dst, 0, rhsParts);
-
-  for (unsigned i = 0; i < lhsParts; i++)
-    tcMultiplyPart(&dst[i], rhs, lhs[i], 0, rhsParts, rhsParts + 1, true);
+  for (unsigned i = 0; i < lhsParts; i++) {
+    // Don't accumulate on the first iteration so we don't need to initalize
+    // dst to 0.
+    tcMultiplyPart(&dst[i], rhs, lhs[i], 0, rhsParts, rhsParts + 1, i != 0);
+  }
 }
 
 // If RHS is zero LHS and REMAINDER are left unchanged, return one.



More information about the llvm-commits mailing list