Skip to content

Commit

Permalink
[APInt] Remove calls to clearUnusedBits from XorSlowCase and operator^=
Browse files Browse the repository at this point in the history
Summary:
There's a comment in XorSlowCase that says "0^0==1" which isn't true. 0 xored with 0 is still 0. So I don't think we need to clear any unused bits here.

Now there is no difference between XorSlowCase and AndSlowCase/OrSlowCase other than the operation being performed

Reviewers: majnemer, MatzeB, chandlerc, bkramer

Reviewed By: MatzeB

Subscribers: chfast, llvm-commits

Differential Revision: https://reviews.llvm.org/D28986

llvm-svn: 292873
  • Loading branch information
topperc committed Jan 24, 2017
1 parent ea2dc02 commit 9028f05
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
8 changes: 2 additions & 6 deletions llvm/lib/Support/APInt.cpp
Expand Up @@ -440,13 +440,12 @@ APInt& APInt::operator^=(const APInt& RHS) {
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
if (isSingleWord()) {
VAL ^= RHS.VAL;
this->clearUnusedBits();
return *this;
}
unsigned numWords = getNumWords();
for (unsigned i = 0; i < numWords; ++i)
pVal[i] ^= RHS.pVal[i];
return clearUnusedBits();
return *this;
}

APInt APInt::AndSlowCase(const APInt& RHS) const {
Expand All @@ -471,10 +470,7 @@ APInt APInt::XorSlowCase(const APInt& RHS) const {
for (unsigned i = 0; i < numWords; ++i)
val[i] = pVal[i] ^ RHS.pVal[i];

APInt Result(val, getBitWidth());
// 0^0==1 so clear the high bits in case they got set.
Result.clearUnusedBits();
return Result;
return APInt(val, getBitWidth());
}

APInt APInt::operator*(const APInt& RHS) const {
Expand Down
30 changes: 30 additions & 0 deletions llvm/unittests/ADT/APIntTest.cpp
Expand Up @@ -158,6 +158,36 @@ TEST(APIntTest, i1) {
EXPECT_EQ(two, one - neg_one);
EXPECT_EQ(zero, one - one);

// And
EXPECT_EQ(zero, zero & zero);
EXPECT_EQ(zero, one & zero);
EXPECT_EQ(zero, zero & one);
EXPECT_EQ(one, one & one);
EXPECT_EQ(zero, zero & zero);
EXPECT_EQ(zero, neg_one & zero);
EXPECT_EQ(zero, zero & neg_one);
EXPECT_EQ(neg_one, neg_one & neg_one);

// Or
EXPECT_EQ(zero, zero | zero);
EXPECT_EQ(one, one | zero);
EXPECT_EQ(one, zero | one);
EXPECT_EQ(one, one | one);
EXPECT_EQ(zero, zero | zero);
EXPECT_EQ(neg_one, neg_one | zero);
EXPECT_EQ(neg_one, zero | neg_one);
EXPECT_EQ(neg_one, neg_one | neg_one);

// Xor
EXPECT_EQ(zero, zero ^ zero);
EXPECT_EQ(one, one ^ zero);
EXPECT_EQ(one, zero ^ one);
EXPECT_EQ(zero, one ^ one);
EXPECT_EQ(zero, zero ^ zero);
EXPECT_EQ(neg_one, neg_one ^ zero);
EXPECT_EQ(neg_one, zero ^ neg_one);
EXPECT_EQ(zero, neg_one ^ neg_one);

// Shifts.
EXPECT_EQ(zero, one << one);
EXPECT_EQ(one, one << zero);
Expand Down

0 comments on commit 9028f05

Please sign in to comment.