[llvm] r302402 - [APInt] Add support for multiplying by a uint64_t.

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Sun May 7 21:55:10 PDT 2017


Author: ctopper
Date: Sun May  7 23:55:09 2017
New Revision: 302402

URL: http://llvm.org/viewvc/llvm-project?rev=302402&view=rev
Log:
[APInt] Add support for multiplying by a uint64_t.

This makes multiply similar to add, sub, xor, and, and or.

Modified:
    llvm/trunk/include/llvm/ADT/APInt.h
    llvm/trunk/lib/Support/APInt.cpp
    llvm/trunk/unittests/ADT/APIntTest.cpp

Modified: llvm/trunk/include/llvm/ADT/APInt.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/APInt.h?rev=302402&r1=302401&r2=302402&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/APInt.h (original)
+++ llvm/trunk/include/llvm/ADT/APInt.h Sun May  7 23:55:09 2017
@@ -842,6 +842,7 @@ public:
   ///
   /// \returns *this
   APInt &operator*=(const APInt &RHS);
+  APInt &operator*=(uint64_t RHS);
 
   /// \brief Addition assignment operator.
   ///
@@ -2043,6 +2044,16 @@ inline APInt operator-(uint64_t LHS, API
   return b;
 }
 
+inline APInt operator*(APInt a, uint64_t RHS) {
+  a *= RHS;
+  return a;
+}
+
+inline APInt operator*(uint64_t LHS, APInt b) {
+  b *= LHS;
+  return b;
+}
+
 
 namespace APIntOps {
 

Modified: llvm/trunk/lib/Support/APInt.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/APInt.cpp?rev=302402&r1=302401&r2=302402&view=diff
==============================================================================
--- llvm/trunk/lib/Support/APInt.cpp (original)
+++ llvm/trunk/lib/Support/APInt.cpp Sun May  7 23:55:09 2017
@@ -256,6 +256,16 @@ APInt& APInt::operator*=(const APInt& RH
   return *this;
 }
 
+APInt& APInt::operator*=(uint64_t RHS) {
+  if (isSingleWord()) {
+    U.VAL *= RHS;
+  } else {
+    unsigned NumWords = getNumWords();
+    tcMultiplyPart(U.pVal, U.pVal, RHS, 0, NumWords, NumWords, false);
+  }
+  return clearUnusedBits();
+}
+
 bool APInt::EqualSlowCase(const APInt& RHS) const {
   return std::equal(U.pVal, U.pVal + getNumWords(), RHS.U.pVal);
 }

Modified: llvm/trunk/unittests/ADT/APIntTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APIntTest.cpp?rev=302402&r1=302401&r2=302402&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/APIntTest.cpp (original)
+++ llvm/trunk/unittests/ADT/APIntTest.cpp Sun May  7 23:55:09 2017
@@ -2142,4 +2142,23 @@ TEST(APIntTest, sext) {
   EXPECT_EQ(63U, i32_neg1.countPopulation());
 }
 
+TEST(APIntTest, multiply) {
+  APInt i64(64, 1234);
+
+  EXPECT_EQ(7006652, i64 * 5678);
+  EXPECT_EQ(7006652, 5678 * i64);
+
+  APInt i128 = APInt::getOneBitSet(128, 64);
+  APInt i128_1234(128, 1234);
+  i128_1234 <<= 64;
+  EXPECT_EQ(i128_1234, i128 * 1234);
+  EXPECT_EQ(i128_1234, 1234 * i128);
+
+  APInt i96 = APInt::getOneBitSet(96, 64);
+  i96 *= ~0ULL;
+  EXPECT_EQ(32U, i96.countLeadingOnes());
+  EXPECT_EQ(32U, i96.countPopulation());
+  EXPECT_EQ(64U, i96.countTrailingZeros());
+}
+
 } // end anonymous namespace




More information about the llvm-commits mailing list