llvm.org GIT mirror llvm / 1e5158d
[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 Craig Topper 3 years ago
3 changed file(s) with 40 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
841841 ///
842842 /// \returns *this
843843 APInt &operator*=(const APInt &RHS);
844 APInt &operator*=(uint64_t RHS);
844845
845846 /// \brief Addition assignment operator.
846847 ///
20422043 return b;
20432044 }
20442045
2046 inline APInt operator*(APInt a, uint64_t RHS) {
2047 a *= RHS;
2048 return a;
2049 }
2050
2051 inline APInt operator*(uint64_t LHS, APInt b) {
2052 b *= LHS;
2053 return b;
2054 }
2055
20452056
20462057 namespace APIntOps {
20472058
253253 assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
254254 *this = *this * RHS;
255255 return *this;
256 }
257
258 APInt& APInt::operator*=(uint64_t RHS) {
259 if (isSingleWord()) {
260 U.VAL *= RHS;
261 } else {
262 unsigned NumWords = getNumWords();
263 tcMultiplyPart(U.pVal, U.pVal, RHS, 0, NumWords, NumWords, false);
264 }
265 return clearUnusedBits();
256266 }
257267
258268 bool APInt::EqualSlowCase(const APInt& RHS) const {
21412141 EXPECT_EQ(63U, i32_neg1.countPopulation());
21422142 }
21432143
2144 TEST(APIntTest, multiply) {
2145 APInt i64(64, 1234);
2146
2147 EXPECT_EQ(7006652, i64 * 5678);
2148 EXPECT_EQ(7006652, 5678 * i64);
2149
2150 APInt i128 = APInt::getOneBitSet(128, 64);
2151 APInt i128_1234(128, 1234);
2152 i128_1234 <<= 64;
2153 EXPECT_EQ(i128_1234, i128 * 1234);
2154 EXPECT_EQ(i128_1234, 1234 * i128);
2155
2156 APInt i96 = APInt::getOneBitSet(96, 64);
2157 i96 *= ~0ULL;
2158 EXPECT_EQ(32U, i96.countLeadingOnes());
2159 EXPECT_EQ(32U, i96.countPopulation());
2160 EXPECT_EQ(64U, i96.countTrailingZeros());
2161 }
2162
21442163 } // end anonymous namespace