llvm.org GIT mirror
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).
 1768 1768 APFloat::opStatus APFloat::roundToIntegral(roundingMode rounding_mode) { 1769 1769 opStatus fs; 1770 1770 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; 1771 1777 1772 1778 // The algorithm here is quite simple: we add 2^(p-1), where p is the 1773 1779 // precision of our format, and then subtract it back off again. The choice
 648 648 } 649 649 650 650 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);⏎ 652 652 653 653 P = T; 654 654 P.roundToIntegral(APFloat::rmTowardZero); 675 675 P = S; 676 676 P.roundToIntegral(APFloat::rmNearestTiesToEven); 677 677 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()); 678 691 } 679 692 680 693 TEST(APFloatTest, getLargest) {