summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOwen Anderson <resistor@mac.com>2012-08-15 18:28:45 +0000
committerOwen Anderson <resistor@mac.com>2012-08-15 18:28:45 +0000
commit352dfff4476b076e2c032d59ea0def3519a7a5c9 (patch)
treec986b285aee567d25a8fc9179bd050cf50586e04
parent73e325de558520985f3250adf8c413e96273561f (diff)
downloadbcm5719-llvm-352dfff4476b076e2c032d59ea0def3519a7a5c9.tar.gz
bcm5719-llvm-352dfff4476b076e2c032d59ea0def3519a7a5c9.zip
Fix another roundToIntegral bug where very large values could become infinity. Problem and solution identified by Steve Canon.
llvm-svn: 161969
-rw-r--r--llvm/lib/Support/APFloat.cpp6
-rw-r--r--llvm/unittests/ADT/APFloatTest.cpp15
2 files changed, 20 insertions, 1 deletions
diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index b42a168bae7..ed261a4194c 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -1770,6 +1770,12 @@ APFloat::opStatus APFloat::roundToIntegral(roundingMode rounding_mode) {
opStatus fs;
assertArithmeticOK(*semantics);
+ // If the exponent is large enough, we know that this value is already
+ // integral, and the arithmetic below would potentially cause it to saturate
+ // to +/-Inf. Bail out early instead.
+ if (exponent+1 >= (int)semanticsPrecision(*semantics))
+ return opOK;
+
// The algorithm here is quite simple: we add 2^(p-1), where p is the
// precision of our format, and then subtract it back off again. The choice
// of rounding modes for the addition/subtraction determines the rounding mode
diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp
index caa288afaf5..00b62feaeb1 100644
--- a/llvm/unittests/ADT/APFloatTest.cpp
+++ b/llvm/unittests/ADT/APFloatTest.cpp
@@ -649,7 +649,7 @@ TEST(APFloatTest, exactInverse) {
}
TEST(APFloatTest, roundToIntegral) {
- APFloat T(-0.5), S(3.14), P(0.0);
+ APFloat T(-0.5), S(3.14), R(APFloat::getLargest(APFloat::IEEEdouble)), P(0.0);
P = T;
P.roundToIntegral(APFloat::rmTowardZero);
@@ -676,6 +676,19 @@ TEST(APFloatTest, roundToIntegral) {
P = S;
P.roundToIntegral(APFloat::rmNearestTiesToEven);
EXPECT_EQ(3.0, P.convertToDouble());
+
+ P = R;
+ P.roundToIntegral(APFloat::rmTowardZero);
+ EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
+ P = R;
+ P.roundToIntegral(APFloat::rmTowardNegative);
+ EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
+ P = R;
+ P.roundToIntegral(APFloat::rmTowardPositive);
+ EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
+ P = R;
+ P.roundToIntegral(APFloat::rmNearestTiesToEven);
+ EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
}
TEST(APFloatTest, getLargest) {
OpenPOWER on IntegriCloud