APFloat::roundToIntegral: Special values don't keep the exponent value up to date...
authorBenjamin Kramer <benny.kra@googlemail.com>
Wed, 26 Sep 2012 14:06:58 +0000 (14:06 +0000)
committerBenjamin Kramer <benny.kra@googlemail.com>
Wed, 26 Sep 2012 14:06:58 +0000 (14:06 +0000)
Add a couple of unit tests for special floats. Fixes 13929, found by MemorySanitizer.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@164698 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/APFloat.cpp
unittests/ADT/APFloatTest.cpp

index f143e6d0adeb353e157fbe6084d716709c4c1916..d07a3c9e7f35b8c7eaa2e937fc8474b7c5d437d4 100644 (file)
@@ -1775,7 +1775,7 @@ APFloat::opStatus APFloat::roundToIntegral(roundingMode rounding_mode) {
   // 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))
+  if (category == fcNormal && exponent+1 >= (int)semanticsPrecision(*semantics))
     return opOK;
 
   // The algorithm here is quite simple: we add 2^(p-1), where p is the
index 00b62feaeb1560d3737e62e1a8e0dcc0e25edbe0..c8d7177d866bf39a679cc9ef417abd4258c55b65 100644 (file)
@@ -689,6 +689,23 @@ TEST(APFloatTest, roundToIntegral) {
   P = R;
   P.roundToIntegral(APFloat::rmNearestTiesToEven);
   EXPECT_EQ(R.convertToDouble(), P.convertToDouble());
+
+  P = APFloat::getZero(APFloat::IEEEdouble);
+  P.roundToIntegral(APFloat::rmTowardZero);
+  EXPECT_EQ(0.0, P.convertToDouble());
+  P = APFloat::getZero(APFloat::IEEEdouble, true);
+  P.roundToIntegral(APFloat::rmTowardZero);
+  EXPECT_EQ(-0.0, P.convertToDouble());
+  P = APFloat::getNaN(APFloat::IEEEdouble);
+  P.roundToIntegral(APFloat::rmTowardZero);
+  EXPECT_TRUE(IsNAN(P.convertToDouble()));
+  P = APFloat::getInf(APFloat::IEEEdouble);
+  P.roundToIntegral(APFloat::rmTowardZero);
+  EXPECT_TRUE(IsInf(P.convertToDouble()) && P.convertToDouble() > 0.0);
+  P = APFloat::getInf(APFloat::IEEEdouble, true);
+  P.roundToIntegral(APFloat::rmTowardZero);
+  EXPECT_TRUE(IsInf(P.convertToDouble()) && P.convertToDouble() < 0.0);
+
 }
 
 TEST(APFloatTest, getLargest) {