diff options
author | Owen Anderson <resistor@mac.com> | 2012-08-15 05:39:46 +0000 |
---|---|---|
committer | Owen Anderson <resistor@mac.com> | 2012-08-15 05:39:46 +0000 |
commit | f7a5dfcb3b68b782543d25ba3151893111ff008f (patch) | |
tree | b90574a5d05d8eb8d2dc60ee2ee8f0421d3be6a5 /lib/Support | |
parent | 9441ad0b6e02ee9558168b37a9620ef1afe79388 (diff) | |
download | external_llvm-f7a5dfcb3b68b782543d25ba3151893111ff008f.zip external_llvm-f7a5dfcb3b68b782543d25ba3151893111ff008f.tar.gz external_llvm-f7a5dfcb3b68b782543d25ba3151893111ff008f.tar.bz2 |
Fix a problem with APFloat::roundToIntegral where it would return incorrect results for negative inputs to trunc. Add unit tests to verify this behavior.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@161929 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support')
-rw-r--r-- | lib/Support/APFloat.cpp | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/lib/Support/APFloat.cpp b/lib/Support/APFloat.cpp index 5ea75a6..84021de 100644 --- a/lib/Support/APFloat.cpp +++ b/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; } |