diff options
author | Leonard Chan <leonardchan@google.com> | 2019-01-18 21:04:25 +0000 |
---|---|---|
committer | Leonard Chan <leonardchan@google.com> | 2019-01-18 21:04:25 +0000 |
commit | 87155c67f399837e1875acd5658a8ff76c3f3164 (patch) | |
tree | 412d26346c383f1b694dd8c610088174811ff48c /lib/Basic/FixedPoint.cpp | |
parent | 6b916c6814c961fd1d5070667b29102b1a928e80 (diff) |
[Fixed Point Arithmetic] Fixed Point Addition Constant Expression Evaluation
This patch includes logic for constant expression evaluation of fixed point additions.
Differential Revision: https://reviews.llvm.org/D55868
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@351593 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Basic/FixedPoint.cpp')
-rw-r--r-- | lib/Basic/FixedPoint.cpp | 73 |
1 files changed, 64 insertions, 9 deletions
diff --git a/lib/Basic/FixedPoint.cpp b/lib/Basic/FixedPoint.cpp index e3cffe628f..33caeb92e8 100644 --- a/lib/Basic/FixedPoint.cpp +++ b/lib/Basic/FixedPoint.cpp @@ -16,11 +16,14 @@ namespace clang { -APFixedPoint APFixedPoint::convert(const FixedPointSemantics &DstSema) const { +APFixedPoint APFixedPoint::convert(const FixedPointSemantics &DstSema, + bool *Overflow) const { llvm::APSInt NewVal = Val; unsigned DstWidth = DstSema.getWidth(); unsigned DstScale = DstSema.getScale(); bool Upscaling = DstScale > getScale(); + if (Overflow) + *Overflow = false; if (Upscaling) { NewVal = NewVal.extend(NewVal.getBitWidth() + DstScale - getScale()); @@ -29,18 +32,28 @@ APFixedPoint APFixedPoint::convert(const FixedPointSemantics &DstSema) const { NewVal >>= (getScale() - DstScale); } - if (DstSema.isSaturated()) { - auto Mask = llvm::APInt::getBitsSetFrom( - NewVal.getBitWidth(), - std::min(DstScale + DstSema.getIntegralBits(), NewVal.getBitWidth())); - llvm::APInt Masked(NewVal & Mask); + auto Mask = llvm::APInt::getBitsSetFrom( + NewVal.getBitWidth(), + std::min(DstScale + DstSema.getIntegralBits(), NewVal.getBitWidth())); + llvm::APInt Masked(NewVal & Mask); - // Change in the bits above the sign - if (!(Masked == Mask || Masked == 0)) + // Change in the bits above the sign + if (!(Masked == Mask || Masked == 0)) { + // Found overflow in the bits above the sign + if (DstSema.isSaturated()) NewVal = NewVal.isNegative() ? Mask : ~Mask; + else if (Overflow) + *Overflow = true; + } - if (!DstSema.isSigned() && NewVal.isNegative()) + // If the dst semantics are unsigned, but our value is signed and negative, we + // clamp to zero. + if (!DstSema.isSigned() && NewVal.isSigned() && NewVal.isNegative()) { + // Found negative overflow for unsigned result + if (DstSema.isSaturated()) NewVal = 0; + else if (Overflow) + *Overflow = true; } NewVal = NewVal.extOrTrunc(DstWidth); @@ -137,6 +150,30 @@ FixedPointSemantics FixedPointSemantics::getCommonSemantics( ResultIsSaturated, ResultHasUnsignedPadding); } +APFixedPoint APFixedPoint::add(const APFixedPoint &Other, + bool *Overflow) const { + auto CommonFXSema = Sema.getCommonSemantics(Other.getSemantics()); + APFixedPoint ConvertedThis = convert(CommonFXSema); + APFixedPoint ConvertedOther = Other.convert(CommonFXSema); + llvm::APSInt ThisVal = ConvertedThis.getValue(); + llvm::APSInt OtherVal = ConvertedOther.getValue(); + bool Overflowed = false; + + llvm::APSInt Result; + if (CommonFXSema.isSaturated()) { + Result = CommonFXSema.isSigned() ? ThisVal.sadd_sat(OtherVal) + : ThisVal.uadd_sat(OtherVal); + } else { + Result = ThisVal.isSigned() ? ThisVal.sadd_ov(OtherVal, Overflowed) + : ThisVal.uadd_ov(OtherVal, Overflowed); + } + + if (Overflow) + *Overflow = Overflowed; + + return APFixedPoint(Result, CommonFXSema); +} + void APFixedPoint::toString(llvm::SmallVectorImpl<char> &Str) const { llvm::APSInt Val = getValue(); unsigned Scale = getScale(); @@ -164,4 +201,22 @@ void APFixedPoint::toString(llvm::SmallVectorImpl<char> &Str) const { } while (FractPart != 0); } +APFixedPoint APFixedPoint::negate(bool *Overflow) const { + if (!isSaturated()) { + if (Overflow) + *Overflow = + (!isSigned() && Val != 0) || (isSigned() && Val.isMinSignedValue()); + return APFixedPoint(-Val, Sema); + } + + // We never overflow for saturation + if (Overflow) + *Overflow = false; + + if (isSigned()) + return Val.isMinSignedValue() ? getMax(Sema) : APFixedPoint(-Val, Sema); + else + return APFixedPoint(Sema); +} + } // namespace clang |