add a few operations for signed operations that also

return an overflow flag.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@116452 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2010-10-13 23:46:33 +00:00
parent 95369599c6
commit f2ddc64c87
2 changed files with 69 additions and 11 deletions

View File

@ -788,8 +788,7 @@ public:
APInt &Quotient, APInt &Remainder);
static void sdivrem(const APInt &LHS, const APInt &RHS,
APInt &Quotient, APInt &Remainder)
{
APInt &Quotient, APInt &Remainder) {
if (LHS.isNegative()) {
if (RHS.isNegative())
APInt::udivrem(-LHS, -RHS, Quotient, Remainder);
@ -805,6 +804,16 @@ public:
}
}
// Operations that return overflow indicators.
// ssub_ov - Signed subtraction. Unsigned subtraction never overflows.
APInt sadd_ov(const APInt &RHS, bool &Overflow);
APInt ssub_ov(const APInt &RHS, bool &Overflow);
APInt sdiv_ov(const APInt &RHS, bool &Overflow);
APInt smul_ov(const APInt &RHS, bool &Overflow);
APInt sshl_ov(unsigned Amt, bool &Overflow);
/// @returns the bit value at bitPosition
/// @brief Array-indexing support.
bool operator[](unsigned bitPosition) const;
@ -988,6 +997,9 @@ public:
return sge(APInt(getBitWidth(), RHS));
}
/// This operation tests if there are any pairs of corresponding bits
/// between this APInt and RHS that are both set.
bool intersects(const APInt &RHS) const {

View File

@ -2046,6 +2046,52 @@ void APInt::udivrem(const APInt &LHS, const APInt &RHS,
divide(LHS, lhsWords, RHS, rhsWords, &Quotient, &Remainder);
}
APInt APInt::sadd_ov(const APInt &RHS, bool &Overflow) {
APInt Res = *this+RHS;
Overflow = isNonNegative() == RHS.isNonNegative() &&
Res.isNonNegative() != isNonNegative();
return Res;
}
APInt APInt::ssub_ov(const APInt &RHS, bool &Overflow) {
APInt Res = *this - RHS;
Overflow = isNonNegative() != RHS.isNonNegative() &&
Res.isNonNegative() != isNonNegative();
return Res;
}
APInt APInt::sdiv_ov(const APInt &RHS, bool &Overflow) {
// MININT/-1 --> overflow.
Overflow = isMinSignedValue() && RHS.isAllOnesValue();
return sdiv(RHS);
}
APInt APInt::smul_ov(const APInt &RHS, bool &Overflow) {
APInt Res = *this * RHS;
if (*this != 0 && RHS != 0)
Overflow = Res.sdiv(RHS) != *this || Res.sdiv(*this) != RHS;
else
Overflow = false;
return Res;
}
APInt APInt::sshl_ov(unsigned ShAmt, bool &Overflow) {
Overflow = ShAmt >= getBitWidth();
if (Overflow)
ShAmt = getBitWidth()-1;
if (isNonNegative()) // Don't allow sign change.
Overflow = ShAmt >= countLeadingZeros();
else
Overflow = ShAmt >= countLeadingOnes();
return *this << ShAmt;
}
void APInt::fromString(unsigned numbits, StringRef str, uint8_t radix) {
// Check our assumptions here
assert(!str.empty() && "Invalid string length");