diff options
author | Roman Lebedev <lebedev.ri@gmail.com> | 2019-10-25 18:17:48 +0300 |
---|---|---|
committer | Roman Lebedev <lebedev.ri@gmail.com> | 2019-10-25 18:20:00 +0300 |
commit | 1cc8e1e1d7d78fc3f2185c5ba207cd21f227fa1c (patch) | |
tree | 23ed419b7338aceb2b0b098502c476e27b1a061d | |
parent | b2c184458e990c8faeffd5047e7086e4f7ff07a6 (diff) | |
download | llvm-1cc8e1e1d7d78fc3f2185c5ba207cd21f227fa1c.zip llvm-1cc8e1e1d7d78fc3f2185c5ba207cd21f227fa1c.tar.gz llvm-1cc8e1e1d7d78fc3f2185c5ba207cd21f227fa1c.tar.bz2 |
[APInt] Add saturating left-shift ops
Summary:
There are `*_ov()` functions already, so at least for consistency it may be good to also have saturating variants.
These may or may not be needed for `ConstantRange`'s `shlWithNoWrap()`
Reviewers: spatel, nikic
Reviewed By: nikic
Subscribers: hiraditya, dexonsmith, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D69398
-rw-r--r-- | llvm/include/llvm/ADT/APInt.h | 2 | ||||
-rw-r--r-- | llvm/lib/Support/APInt.cpp | 19 | ||||
-rw-r--r-- | llvm/unittests/ADT/APIntTest.cpp | 17 |
3 files changed, 38 insertions, 0 deletions
diff --git a/llvm/include/llvm/ADT/APInt.h b/llvm/include/llvm/ADT/APInt.h index ddf0e19..6a08e6e 100644 --- a/llvm/include/llvm/ADT/APInt.h +++ b/llvm/include/llvm/ADT/APInt.h @@ -1111,6 +1111,8 @@ public: APInt usub_sat(const APInt &RHS) const; APInt smul_sat(const APInt &RHS) const; APInt umul_sat(const APInt &RHS) const; + APInt sshl_sat(const APInt &RHS) const; + APInt ushl_sat(const APInt &RHS) const; /// Array-indexing support. /// diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp index df2e619..08e9757 100644 --- a/llvm/lib/Support/APInt.cpp +++ b/llvm/lib/Support/APInt.cpp @@ -2070,6 +2070,25 @@ APInt APInt::umul_sat(const APInt &RHS) const { return APInt::getMaxValue(BitWidth); } +APInt APInt::sshl_sat(const APInt &RHS) const { + bool Overflow; + APInt Res = sshl_ov(RHS, Overflow); + if (!Overflow) + return Res; + + return isNegative() ? APInt::getSignedMinValue(BitWidth) + : APInt::getSignedMaxValue(BitWidth); +} + +APInt APInt::ushl_sat(const APInt &RHS) const { + bool Overflow; + APInt Res = ushl_ov(RHS, Overflow); + if (!Overflow) + return Res; + + return APInt::getMaxValue(BitWidth); +} + void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) { // Check our assumptions here assert(!str.empty() && "Invalid string length"); diff --git a/llvm/unittests/ADT/APIntTest.cpp b/llvm/unittests/ADT/APIntTest.cpp index f45a4e6..5130b94 100644 --- a/llvm/unittests/ADT/APIntTest.cpp +++ b/llvm/unittests/ADT/APIntTest.cpp @@ -1215,6 +1215,23 @@ TEST(APIntTest, SaturatingMath) { EXPECT_EQ(APInt(8, -128), APInt(8, 25).smul_sat(APInt(8, -6))); EXPECT_EQ(APInt(8, 127), APInt(8, -25).smul_sat(APInt(8, -6))); EXPECT_EQ(APInt(8, 127), APInt(8, 25).smul_sat(APInt(8, 6))); + + EXPECT_EQ(APInt(8, 128), APInt(8, 4).ushl_sat(APInt(8, 5))); + EXPECT_EQ(APInt(8, 255), APInt(8, 4).ushl_sat(APInt(8, 6))); + EXPECT_EQ(APInt(8, 128), APInt(8, 1).ushl_sat(APInt(8, 7))); + EXPECT_EQ(APInt(8, 255), APInt(8, 1).ushl_sat(APInt(8, 8))); + EXPECT_EQ(APInt(8, 255), APInt(8, -128).ushl_sat(APInt(8, 2))); + EXPECT_EQ(APInt(8, 255), APInt(8, 64).ushl_sat(APInt(8, 2))); + EXPECT_EQ(APInt(8, 255), APInt(8, 64).ushl_sat(APInt(8, -2))); + + EXPECT_EQ(APInt(8, 64), APInt(8, 4).sshl_sat(APInt(8, 4))); + EXPECT_EQ(APInt(8, 127), APInt(8, 4).sshl_sat(APInt(8, 5))); + EXPECT_EQ(APInt(8, 127), APInt(8, 1).sshl_sat(APInt(8, 8))); + EXPECT_EQ(APInt(8, -64), APInt(8, -4).sshl_sat(APInt(8, 4))); + EXPECT_EQ(APInt(8, -128), APInt(8, -4).sshl_sat(APInt(8, 5))); + EXPECT_EQ(APInt(8, -128), APInt(8, -4).sshl_sat(APInt(8, 6))); + EXPECT_EQ(APInt(8, -128), APInt(8, -1).sshl_sat(APInt(8, 7))); + EXPECT_EQ(APInt(8, -128), APInt(8, -1).sshl_sat(APInt(8, 8))); } TEST(APIntTest, FromArray) { |