summaryrefslogtreecommitdiffstats
path: root/llvm/lib
diff options
context:
space:
mode:
authorDale Johannesen <dalej@apple.com>2007-09-25 23:07:07 +0000
committerDale Johannesen <dalej@apple.com>2007-09-25 23:07:07 +0000
commite7439eff5a1be7d5da85787180055d8ef8e24c1b (patch)
treeb92577ba06dba0f4ba2329910e13e77c1ec2f2a2 /llvm/lib
parenta88f7bcea3bf6328f86f55cd77a0e8eeba658904 (diff)
downloadbcm5719-llvm-e7439eff5a1be7d5da85787180055d8ef8e24c1b.tar.gz
bcm5719-llvm-e7439eff5a1be7d5da85787180055d8ef8e24c1b.zip
Make APFloat->int conversions deterministic even in
cases with undefined behavior. llvm-svn: 42328
Diffstat (limited to 'llvm/lib')
-rw-r--r--llvm/lib/Support/APFloat.cpp38
-rw-r--r--llvm/lib/VMCore/ConstantFold.cpp2
2 files changed, 33 insertions, 7 deletions
diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index b3705f5058f..348b8ab1cd9 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/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;
}
diff --git a/llvm/lib/VMCore/ConstantFold.cpp b/llvm/lib/VMCore/ConstantFold.cpp
index d8b8566193a..7154afb1833 100644
--- a/llvm/lib/VMCore/ConstantFold.cpp
+++ b/llvm/lib/VMCore/ConstantFold.cpp
@@ -197,8 +197,6 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, const Constant *V,
APFloat::opStatus status = V.convertToInteger(x, DestBitWidth,
opc==Instruction::FPToSI,
APFloat::rmTowardZero);
- if (status!=APFloat::opOK && status!=APFloat::opInexact)
- return 0; // give up
APInt Val(DestBitWidth, 2, x);
return ConstantInt::get(Val);
}
OpenPOWER on IntegriCloud