diff options
author | Dale Johannesen <dalej@apple.com> | 2007-09-25 23:07:07 +0000 |
---|---|---|
committer | Dale Johannesen <dalej@apple.com> | 2007-09-25 23:07:07 +0000 |
commit | 0edc47ad7b3fb5ad5f9dcd4139f3074dd11ff457 (patch) | |
tree | d70f31f8a5ced87d8222fbce79c437ce0e1e5f20 /lib/Support | |
parent | 7086ce72684c302d9898ce0324f691a6a0787669 (diff) | |
download | external_llvm-0edc47ad7b3fb5ad5f9dcd4139f3074dd11ff457.zip external_llvm-0edc47ad7b3fb5ad5f9dcd4139f3074dd11ff457.tar.gz external_llvm-0edc47ad7b3fb5ad5f9dcd4139f3074dd11ff457.tar.bz2 |
Make APFloat->int conversions deterministic even in
cases with undefined behavior.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@42328 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support')
-rw-r--r-- | lib/Support/APFloat.cpp | 38 |
1 files changed, 33 insertions, 5 deletions
diff --git a/lib/Support/APFloat.cpp b/lib/Support/APFloat.cpp index b3705f5..348b8ab 100644 --- a/lib/Support/APFloat.cpp +++ b/lib/Support/APFloat.cpp @@ -1399,13 +1399,28 @@ APFloat::convertToInteger(integerPart *parts, unsigned int width, unsigned int msb, partsCount; int bits; - /* Handle the three special cases first. */ - if(category == fcInfinity || category == fcNaN) - return opInvalidOp; - partsCount = partCountForBits(width); - if(category == fcZero) { + /* Handle the three special cases first. We produce + a deterministic result even for the Invalid cases. */ + if (category == fcNaN) { + // Neither sign nor isSigned affects this. + APInt::tcSet(parts, 0, partsCount); + return opInvalidOp; + } + if (category == fcInfinity) { + if (!sign && isSigned) + APInt::tcSetLeastSignificantBits(parts, partsCount, width-1); + else if (!sign && !isSigned) + APInt::tcSetLeastSignificantBits(parts, partsCount, width); + else if (sign && isSigned) { + APInt::tcSetLeastSignificantBits(parts, partsCount, 1); + APInt::tcShiftLeft(parts, partsCount, width-1); + } else // sign && !isSigned + APInt::tcSet(parts, 0, partsCount); + return opInvalidOp; + } + if (category == fcZero) { APInt::tcSet(parts, 0, partsCount); return opOK; } @@ -1418,6 +1433,19 @@ APFloat::convertToInteger(integerPart *parts, unsigned int width, if(bits > 0) { lost_fraction = tmp.shiftSignificandRight(bits); } else { + if (-bits >= semantics->precision) { + // Unrepresentably large. + if (!sign && isSigned) + APInt::tcSetLeastSignificantBits(parts, partsCount, width-1); + else if (!sign && !isSigned) + APInt::tcSetLeastSignificantBits(parts, partsCount, width); + else if (sign && isSigned) { + APInt::tcSetLeastSignificantBits(parts, partsCount, 1); + APInt::tcShiftLeft(parts, partsCount, width-1); + } else // sign && !isSigned + APInt::tcSet(parts, 0, partsCount); + return (opStatus)(opOverflow | opInexact); + } tmp.shiftSignificandLeft(-bits); lost_fraction = lfExactlyZero; } |