summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support/APFloat.cpp
diff options
context:
space:
mode:
authorOwen Anderson <resistor@mac.com>2012-08-15 05:39:46 +0000
committerOwen Anderson <resistor@mac.com>2012-08-15 05:39:46 +0000
commit1ff74b0d2d4ea976a0191f391e506cd1f8fd338b (patch)
tree9fa1171172e27381f469ae952c03104773eab309 /llvm/lib/Support/APFloat.cpp
parent1049611070b64538c3c3dd771a24b95944c64d8c (diff)
downloadbcm5719-llvm-1ff74b0d2d4ea976a0191f391e506cd1f8fd338b.tar.gz
bcm5719-llvm-1ff74b0d2d4ea976a0191f391e506cd1f8fd338b.zip
Fix a problem with APFloat::roundToIntegral where it would return incorrect results for negative inputs to trunc. Add unit tests to verify this behavior.
llvm-svn: 161929
Diffstat (limited to 'llvm/lib/Support/APFloat.cpp')
-rw-r--r--llvm/lib/Support/APFloat.cpp12
1 files changed, 12 insertions, 0 deletions
diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index 5ea75a621ad..84021de0bf4 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -1774,19 +1774,31 @@ APFloat::opStatus APFloat::roundToIntegral(roundingMode rounding_mode) {
// precision of our format, and then subtract it back off again. The choice
// of rounding modes for the addition/subtraction determines the rounding mode
// for our integral rounding as well.
+ // NOTE: When the input value is negative, we do subtractation followed by
+ // addition instead.
APInt IntegerConstant(NextPowerOf2(semanticsPrecision(*semantics)), 1);
IntegerConstant <<= semanticsPrecision(*semantics)-1;
APFloat MagicConstant(*semantics);
fs = MagicConstant.convertFromAPInt(IntegerConstant, false,
rmNearestTiesToEven);
+ MagicConstant.copySign(*this);
+
if (fs != opOK)
return fs;
+ // Preserve the input sign so that we can handle 0.0/-0.0 cases correctly.
+ bool inputSign = isNegative();
+
fs = add(MagicConstant, rounding_mode);
if (fs != opOK && fs != opInexact)
return fs;
fs = subtract(MagicConstant, rounding_mode);
+
+ // Restore the input sign.
+ if (inputSign != isNegative())
+ changeSign();
+
return fs;
}
OpenPOWER on IntegriCloud