[llvm] [KnownBits] Add KnownBits::absdiff to compute the absolute difference of 2 unsigned values (PR #82354)

via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 1 09:17:35 PST 2024


================
@@ -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());
+}
----------------
goldsteinn wrote:

Does this test provide anything that the exhaustive test doing already cover?

https://github.com/llvm/llvm-project/pull/82354


More information about the llvm-commits mailing list