llvm.org GIT mirror llvm / c82cc58
Fix another roundToIntegral bug where very large values could become infinity. Problem and solution identified by Steve Canon. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@161969 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 8 years ago
2 changed file(s) with 20 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
17681768 APFloat::opStatus APFloat::roundToIntegral(roundingMode rounding_mode) {
17691769 opStatus fs;
17701770 assertArithmeticOK(*semantics);
1771
1772 // If the exponent is large enough, we know that this value is already
1773 // integral, and the arithmetic below would potentially cause it to saturate
1774 // to +/-Inf. Bail out early instead.
1775 if (exponent+1 >= (int)semanticsPrecision(*semantics))
1776 return opOK;
17711777
17721778 // The algorithm here is quite simple: we add 2^(p-1), where p is the
17731779 // precision of our format, and then subtract it back off again. The choice
648648 }
649649
650650 TEST(APFloatTest, roundToIntegral) {
651 APFloat T(-0.5), S(3.14), P(0.0);
651 APFloat T(-0.5), S(3.14), R(APFloat::getLargest(APFloat::IEEEdouble)), P(0.0);
652652
653653 P = T;
654654 P.roundToIntegral(APFloat::rmTowardZero);
675675 P = S;
676676 P.roundToIntegral(APFloat::rmNearestTiesToEven);
677677 EXPECT_EQ(3.0, P.convertToDouble());
678
679 P = R;
680 P.roundToIntegral(APFloat::rmTowardZero);
681 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
682 P = R;
683 P.roundToIntegral(APFloat::rmTowardNegative);
684 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
685 P = R;
686 P.roundToIntegral(APFloat::rmTowardPositive);
687 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
688 P = R;
689 P.roundToIntegral(APFloat::rmNearestTiesToEven);
690 EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
678691 }
679692
680693 TEST(APFloatTest, getLargest) {