diff options
author | Roman Lebedev <lebedev.ri@gmail.com> | 2019-10-25 18:17:38 +0300 |
---|---|---|
committer | Roman Lebedev <lebedev.ri@gmail.com> | 2019-10-25 18:19:54 +0300 |
commit | b2c184458e990c8faeffd5047e7086e4f7ff07a6 (patch) | |
tree | 1b2be1eeb77db6e5857631ac17d9eec37cbc7d6a /llvm/lib/Support/APInt.cpp | |
parent | 59a51d84b3a4f96bcc9669ee9c2b2041175a2ccd (diff) | |
download | bcm5719-llvm-b2c184458e990c8faeffd5047e7086e4f7ff07a6.tar.gz bcm5719-llvm-b2c184458e990c8faeffd5047e7086e4f7ff07a6.zip |
[APInt] Add saturating multiply 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 `mulWithNoWrap()`
Reviewers: spatel, nikic
Reviewed By: nikic
Subscribers: hiraditya, dexonsmith, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D69397
Diffstat (limited to 'llvm/lib/Support/APInt.cpp')
-rw-r--r-- | llvm/lib/Support/APInt.cpp | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/llvm/lib/Support/APInt.cpp b/llvm/lib/Support/APInt.cpp index 758fe8b4f86..df2e6197b2a 100644 --- a/llvm/lib/Support/APInt.cpp +++ b/llvm/lib/Support/APInt.cpp @@ -2048,6 +2048,27 @@ APInt APInt::usub_sat(const APInt &RHS) const { return APInt(BitWidth, 0); } +APInt APInt::smul_sat(const APInt &RHS) const { + bool Overflow; + APInt Res = smul_ov(RHS, Overflow); + if (!Overflow) + return Res; + + // The result is negative if one and only one of inputs is negative. + bool ResIsNegative = isNegative() ^ RHS.isNegative(); + + return ResIsNegative ? APInt::getSignedMinValue(BitWidth) + : APInt::getSignedMaxValue(BitWidth); +} + +APInt APInt::umul_sat(const APInt &RHS) const { + bool Overflow; + APInt Res = umul_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 |