Skip to content

Commit baa392e

Browse files
committedApr 20, 2017
[APInt] Implement APInt::intersects without creating a temporary APInt in the multiword case
Summary: This is a simple question we should be able to answer without creating a temporary to hold the AND result. We can also get an early out as soon as we find a word that intersects. Reviewers: RKSimon, hans, spatel, davide Reviewed By: hans, davide Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D32253 llvm-svn: 300812
1 parent e49252c commit baa392e

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed
 

‎llvm/include/llvm/ADT/APInt.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ class LLVM_NODISCARD APInt {
209209
/// out-of-line slow case for countPopulation
210210
unsigned countPopulationSlowCase() const LLVM_READONLY;
211211

212+
/// out-of-line slow case for intersects.
213+
bool intersectsSlowCase(const APInt &RHS) const LLVM_READONLY;
214+
212215
/// out-of-line slow case for setBits.
213216
void setBitsSlowCase(unsigned loBit, unsigned hiBit);
214217

@@ -1206,9 +1209,10 @@ class LLVM_NODISCARD APInt {
12061209
/// This operation tests if there are any pairs of corresponding bits
12071210
/// between this APInt and RHS that are both set.
12081211
bool intersects(const APInt &RHS) const {
1209-
APInt temp(*this);
1210-
temp &= RHS;
1211-
return temp != 0;
1212+
assert(BitWidth == RHS.BitWidth && "Bit widths must be the same");
1213+
if (isSingleWord())
1214+
return (VAL & RHS.VAL) != 0;
1215+
return intersectsSlowCase(RHS);
12121216
}
12131217

12141218
/// @}

‎llvm/lib/Support/APInt.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,14 @@ unsigned APInt::countPopulationSlowCase() const {
722722
return Count;
723723
}
724724

725+
bool APInt::intersectsSlowCase(const APInt &RHS) const {
726+
for (unsigned i = 0, e = getNumWords(); i != e; ++i)
727+
if ((pVal[i] & RHS.pVal[i]) != 0)
728+
return true;
729+
730+
return false;
731+
}
732+
725733
APInt APInt::byteSwap() const {
726734
assert(BitWidth >= 16 && BitWidth % 16 == 0 && "Cannot byteswap!");
727735
if (BitWidth == 16)

0 commit comments

Comments
 (0)
Please sign in to comment.