-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[KnownBits] Add KnownBits::absdiff to compute the absolute difference of 2 unsigned values #82354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -244,6 +244,32 @@ TEST(KnownBitsTest, SubBorrowExhaustive) { | |
}); | ||
} | ||
|
||
TEST(KnownBitsTest, AbsDiffSpecialCase) { | ||
// There are 2 implementation of absdiff - both are currently needed to cover | ||
// extra cases. | ||
KnownBits LHS, RHS, Res; | ||
|
||
// absdiff(LHS,RHS) = sub(umax(LHS,RHS), umin(LHS,RHS)). | ||
// Actual: false (Inputs = 1011, 101?, Computed = 000?, Exact = 000?) | ||
LHS.One = APInt(4, 0b1011); | ||
RHS.One = APInt(4, 0b1010); | ||
LHS.Zero = APInt(4, 0b0100); | ||
RHS.Zero = APInt(4, 0b0100); | ||
Res = KnownBits::absdiff(LHS, RHS); | ||
EXPECT_EQ(0b0000, Res.One.getZExtValue()); | ||
EXPECT_EQ(0b1110, Res.Zero.getZExtValue()); | ||
|
||
// find the common bits between sub(LHS,RHS) and sub(RHS,LHS). | ||
// Actual: false (Inputs = ???1, 1000, Computed = ???1, Exact = 0??1) | ||
LHS.One = APInt(4, 0b0001); | ||
RHS.One = APInt(4, 0b1000); | ||
LHS.Zero = APInt(4, 0b0000); | ||
RHS.Zero = APInt(4, 0b0111); | ||
Res = KnownBits::absdiff(LHS, RHS); | ||
EXPECT_EQ(0b0001, Res.One.getZExtValue()); | ||
EXPECT_EQ(0b0000, Res.Zero.getZExtValue()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this test provide anything that the exhaustive test doing already cover? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was requested by @nikic - if you disable either of the computations then one of the tests will fail |
||
|
||
TEST(KnownBitsTest, BinaryExhaustive) { | ||
testBinaryOpExhaustive( | ||
[](const KnownBits &Known1, const KnownBits &Known2) { | ||
|
@@ -281,7 +307,14 @@ TEST(KnownBitsTest, BinaryExhaustive) { | |
return KnownBits::smin(Known1, Known2); | ||
}, | ||
[](const APInt &N1, const APInt &N2) { return APIntOps::smin(N1, N2); }); | ||
|
||
testBinaryOpExhaustive( | ||
[](const KnownBits &Known1, const KnownBits &Known2) { | ||
return KnownBits::absdiff(Known1, Known2); | ||
}, | ||
[](const APInt &N1, const APInt &N2) { | ||
return APIntOps::absdiff(N1, N2); | ||
}, | ||
checkCorrectnessOnlyBinary); | ||
RKSimon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
testBinaryOpExhaustive( | ||
[](const KnownBits &Known1, const KnownBits &Known2) { | ||
return KnownBits::udiv(Known1, Known2); | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think you can get away with special casing high bits. Basically all high bits
min(LHS.numSignBits(), RHS.numSignBits())
will always be zero.edit: thats not right! The 1s/0s need to match. So a bit more complicate but like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to work on "checkOptimalityBinary" cases in a future patch - the handling of leading bits still needs a bit of work - I think adding NUW handling to KnownBits::computeForAddSub might be the way to go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, please leave it as a todo then. I agree it makes sense to add
nuw
flag incomputeForAddSub
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Im working on a patch to support NUW for add/sub
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See: PR #83382