diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp --- a/llvm/lib/Support/APFloat.cpp +++ b/llvm/lib/Support/APFloat.cpp @@ -1977,7 +1977,7 @@ return fs; } -/* Rounding-mode corrrect round to integral value. */ +/* Rounding-mode correct round to integral value. */ IEEEFloat::opStatus IEEEFloat::roundToIntegral(roundingMode rounding_mode) { opStatus fs; @@ -2007,7 +2007,8 @@ bool inputSign = isNegative(); fs = add(MagicConstant, rounding_mode); - if (fs != opOK && fs != opInexact) + opStatus IsInexact = static_cast(fs & opInexact); + if (fs != opOK && !IsInexact) return fs; fs = subtract(MagicConstant, rounding_mode); @@ -2016,7 +2017,7 @@ if (inputSign != isNegative()) changeSign(); - return fs; + return fs == opOK ? IsInexact : fs; } diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp --- a/llvm/unittests/ADT/APFloatTest.cpp +++ b/llvm/unittests/ADT/APFloatTest.cpp @@ -1525,6 +1525,62 @@ P = APFloat::getInf(APFloat::IEEEdouble(), true); P.roundToIntegral(APFloat::rmTowardZero); EXPECT_TRUE(std::isinf(P.convertToDouble()) && P.convertToDouble() < 0.0); + + APFloat::opStatus St; + + P = APFloat(10.0); + St = P.roundToIntegral(APFloat::rmTowardZero); + EXPECT_EQ(10.0, P.convertToDouble()); + EXPECT_EQ(APFloat::opOK, St); + + P = APFloat::getNaN(APFloat::IEEEdouble()); + St = P.roundToIntegral(APFloat::rmTowardZero); + EXPECT_TRUE(P.isNaN()); + EXPECT_FALSE(P.isNegative()); + EXPECT_EQ(APFloat::opOK, St); + + P = APFloat::getNaN(APFloat::IEEEdouble(), true); + St = P.roundToIntegral(APFloat::rmTowardZero); + EXPECT_TRUE(P.isNaN()); + EXPECT_TRUE(P.isNegative()); + EXPECT_EQ(APFloat::opOK, St); + + P = APFloat::getInf(APFloat::IEEEdouble()); + St = P.roundToIntegral(APFloat::rmTowardZero); + EXPECT_TRUE(P.isInfinity()); + EXPECT_FALSE(P.isNegative()); + EXPECT_EQ(APFloat::opOK, St); + + P = APFloat::getInf(APFloat::IEEEdouble(), true); + St = P.roundToIntegral(APFloat::rmTowardZero); + EXPECT_TRUE(P.isInfinity()); + EXPECT_TRUE(P.isNegative()); + EXPECT_EQ(APFloat::opOK, St); + + P = APFloat(10.5); + St = P.roundToIntegral(APFloat::rmTowardZero); + EXPECT_EQ(10.0, P.convertToDouble()); + EXPECT_EQ(APFloat::opInexact, St); + + P = APFloat(10.5); + St = P.roundToIntegral(APFloat::rmTowardPositive); + EXPECT_EQ(11.0, P.convertToDouble()); + EXPECT_EQ(APFloat::opInexact, St); + + P = APFloat(10.5); + St = P.roundToIntegral(APFloat::rmTowardNegative); + EXPECT_EQ(10.0, P.convertToDouble()); + EXPECT_EQ(APFloat::opInexact, St); + + P = APFloat(10.5); + St = P.roundToIntegral(APFloat::rmNearestTiesToAway); + EXPECT_EQ(11.0, P.convertToDouble()); + EXPECT_EQ(APFloat::opInexact, St); + + P = APFloat(10.5); + St = P.roundToIntegral(APFloat::rmNearestTiesToEven); + EXPECT_EQ(10.0, P.convertToDouble()); + EXPECT_EQ(APFloat::opInexact, St); } TEST(APFloatTest, isInteger) {