[llvm-commits] [llvm] r161969 - in /llvm/trunk: lib/Support/APFloat.cpp unittests/ADT/APFloatTest.cpp

Owen Anderson resistor at mac.com
Wed Aug 15 11:28:45 PDT 2012


Author: resistor
Date: Wed Aug 15 13:28:45 2012
New Revision: 161969

URL: http://llvm.org/viewvc/llvm-project?rev=161969&view=rev
Log:
Fix another roundToIntegral bug where very large values could become infinity.  Problem and solution identified by Steve Canon.

Modified:
    llvm/trunk/lib/Support/APFloat.cpp
    llvm/trunk/unittests/ADT/APFloatTest.cpp

Modified: llvm/trunk/lib/Support/APFloat.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/APFloat.cpp?rev=161969&r1=161968&r2=161969&view=diff
==============================================================================
--- llvm/trunk/lib/Support/APFloat.cpp (original)
+++ llvm/trunk/lib/Support/APFloat.cpp Wed Aug 15 13:28:45 2012
@@ -1770,6 +1770,12 @@
   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

Modified: llvm/trunk/unittests/ADT/APFloatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APFloatTest.cpp?rev=161969&r1=161968&r2=161969&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/APFloatTest.cpp (original)
+++ llvm/trunk/unittests/ADT/APFloatTest.cpp Wed Aug 15 13:28:45 2012
@@ -649,7 +649,7 @@
 }
 
 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 @@
   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) {





More information about the llvm-commits mailing list