From: Craig Topper Date: Mon, 8 May 2017 04:55:09 +0000 (+0000) Subject: [APInt] Add support for multiplying by a uint64_t. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1e5158d3c88d567e909fff00bf6cf8d6421dca41;p=llvm [APInt] Add support for multiplying by a uint64_t. This makes multiply similar to add, sub, xor, and, and or. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@302402 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/ADT/APInt.h b/include/llvm/ADT/APInt.h index 63c92c1a7fc..c3822e35906 100644 --- a/include/llvm/ADT/APInt.h +++ b/include/llvm/ADT/APInt.h @@ -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, APInt b) { 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 { diff --git a/lib/Support/APInt.cpp b/lib/Support/APInt.cpp index ee5c9f9a577..e2cfb90d9e2 100644 --- a/lib/Support/APInt.cpp +++ b/lib/Support/APInt.cpp @@ -256,6 +256,16 @@ APInt& APInt::operator*=(const APInt& RHS) { 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); } diff --git a/unittests/ADT/APIntTest.cpp b/unittests/ADT/APIntTest.cpp index bb6cf35fe9e..5594955e7ba 100644 --- a/unittests/ADT/APIntTest.cpp +++ b/unittests/ADT/APIntTest.cpp @@ -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