Skip to content
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

[APInt] Add APIntOps::absdiff to compute the absolute difference of 2 unsigned values #82255

Merged
merged 1 commit into from
Feb 19, 2024

Conversation

RKSimon
Copy link
Collaborator

@RKSimon RKSimon commented Feb 19, 2024

Equivalent to "umax(A, B) - umin(A, B)"

First step towards adding knownbits support for absdiff patterns for #81765

@llvmbot
Copy link
Collaborator

llvmbot commented Feb 19, 2024

@llvm/pr-subscribers-llvm-adt

Author: Simon Pilgrim (RKSimon)

Changes

Equivalent to "umax(A, B) - umin(A, B)"

First step towards adding knownbits support for absdiff patterns for #81765


Full diff: https://github.com/llvm/llvm-project/pull/82255.diff

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/APInt.h (+5)
  • (modified) llvm/unittests/ADT/APIntTest.cpp (+34)
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h
index 6f2f25548cc84b..ef7e1878f303ba 100644
--- a/llvm/include/llvm/ADT/APInt.h
+++ b/llvm/include/llvm/ADT/APInt.h
@@ -2187,6 +2187,11 @@ inline const APInt &umax(const APInt &A, const APInt &B) {
   return A.ugt(B) ? A : B;
 }
 
+/// Determine the absolute difference of two APInts considered to be unsigned.
+inline const APInt absdiff(const APInt &A, const APInt &B) {
+  return umax(A, B) - umin(A, B);
+}
+
 /// Compute GCD of two unsigned APInt values.
 ///
 /// This function returns the greatest common divisor of the two APInt values
diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp
index 3b909f8f7d14e2..2fe59f05ca753a 100644
--- a/llvm/unittests/ADT/APIntTest.cpp
+++ b/llvm/unittests/ADT/APIntTest.cpp
@@ -2497,6 +2497,40 @@ TEST(APIntTest, clearLowBits) {
   EXPECT_EQ(16u, i32hi16.popcount());
 }
 
+TEST(APIntTest, AbsDiff) {
+  using APIntOps::absdiff;
+
+  APInt MaxU1(1, 1, false);
+  APInt MinU1(1, 0, false);
+  EXPECT_EQ(1u, absdiff(MaxU1, MinU1).getZExtValue());
+  EXPECT_EQ(1u, absdiff(MinU1, MaxU1).getZExtValue());
+
+  APInt MaxU4(4, 15, false);
+  APInt MinU4(4, 0, false);
+  EXPECT_EQ(15u, absdiff(MaxU4, MinU4).getZExtValue());
+  EXPECT_EQ(15u, absdiff(MinU4, MaxU4).getZExtValue());
+
+  APInt MaxS8(8, 127, true);
+  APInt MinS8(8, -128, true);
+  EXPECT_EQ(1u, absdiff(MaxS8, MinS8).getZExtValue());
+  EXPECT_EQ(1u, absdiff(MinS8, MaxS8).getZExtValue());
+
+  APInt MaxU16(16, 65535, false);
+  APInt MinU16(16, 0, false);
+  EXPECT_EQ(65535u, absdiff(MaxU16, MinU16).getZExtValue());
+  EXPECT_EQ(65535u, absdiff(MinU16, MaxU16).getZExtValue());
+
+  APInt MaxS16(16, 32767, true);
+  APInt MinS16(16, -32768, true);
+  APInt ZeroS16(16, 0, true);
+  EXPECT_EQ(1u, absdiff(MaxS16, MinS16).getZExtValue());
+  EXPECT_EQ(1u, absdiff(MinS16, MaxS16).getZExtValue());
+  EXPECT_EQ(32768u, absdiff(ZeroS16, MinS16));
+  EXPECT_EQ(32768u, absdiff(MinS16, ZeroS16));
+  EXPECT_EQ(32767u, absdiff(ZeroS16, MaxS16));
+  EXPECT_EQ(32767u, absdiff(MaxS16, ZeroS16));
+}
+
 TEST(APIntTest, GCD) {
   using APIntOps::GreatestCommonDivisor;
 

… unsigned values

Equivalent to "umax(A, B) - umin(A, B)"

First step towards adding knownbits support for absdiff patterns for llvm#81765
@RKSimon RKSimon merged commit 73f76cd into llvm:main Feb 19, 2024
4 checks passed
@RKSimon RKSimon deleted the apint-absdiff branch February 20, 2024 12:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants