[PATCH] D38030: Fix APFloat from string conversion for Inf

Serguei Katkov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 18 20:28:38 PST 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL321054: Fix APFloat from string conversion for Inf (authored by skatkov, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D38030?vs=126926&id=127463#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D38030

Files:
  llvm/trunk/lib/Support/APFloat.cpp
  llvm/trunk/lib/Support/StringRef.cpp
  llvm/trunk/unittests/ADT/APFloatTest.cpp
  llvm/trunk/unittests/ADT/StringRefTest.cpp


Index: llvm/trunk/lib/Support/StringRef.cpp
===================================================================
--- llvm/trunk/lib/Support/StringRef.cpp
+++ llvm/trunk/lib/Support/StringRef.cpp
@@ -586,7 +586,7 @@
   APFloat::opStatus Status =
       F.convertFromString(*this, APFloat::rmNearestTiesToEven);
   if (Status != APFloat::opOK) {
-    if (!AllowInexact || Status != APFloat::opInexact)
+    if (!AllowInexact || !(Status & APFloat::opInexact))
       return true;
   }
 
Index: llvm/trunk/lib/Support/APFloat.cpp
===================================================================
--- llvm/trunk/lib/Support/APFloat.cpp
+++ llvm/trunk/lib/Support/APFloat.cpp
@@ -2546,12 +2546,12 @@
 }
 
 bool IEEEFloat::convertFromStringSpecials(StringRef str) {
-  if (str.equals("inf") || str.equals("INFINITY")) {
+  if (str.equals("inf") || str.equals("INFINITY") || str.equals("+Inf")) {
     makeInf(false);
     return true;
   }
 
-  if (str.equals("-inf") || str.equals("-INFINITY")) {
+  if (str.equals("-inf") || str.equals("-INFINITY") || str.equals("-Inf")) {
     makeInf(true);
     return true;
   }
Index: llvm/trunk/unittests/ADT/StringRefTest.cpp
===================================================================
--- llvm/trunk/unittests/ADT/StringRefTest.cpp
+++ llvm/trunk/unittests/ADT/StringRefTest.cpp
@@ -875,7 +875,12 @@
                      {"0.0", false, false, 0.0},
                      {"-0.0", false, false, -0.0},
                      {"123.45", false, true, 123.45},
-                     {"123.45", true, false, 123.45}};
+                     {"123.45", true, false, 123.45},
+                     {"1.8e308", true, false, std::numeric_limits<double>::infinity()},
+                     {"1.8e308", false, true, std::numeric_limits<double>::infinity()},
+                     {"0x0.0000000000001P-1023", false, true, 0.0},
+                     {"0x0.0000000000001P-1023", true, false, 0.0},
+                    };
 
 TEST(StringRefTest, getAsDouble) {
   for (const auto &Entry : DoubleStrings) {
Index: llvm/trunk/unittests/ADT/APFloatTest.cpp
===================================================================
--- llvm/trunk/unittests/ADT/APFloatTest.cpp
+++ llvm/trunk/unittests/ADT/APFloatTest.cpp
@@ -849,6 +849,23 @@
   EXPECT_EQ(2.71828, convertToDoubleFromString("2.71828"));
 }
 
+TEST(APFloatTest, fromToStringSpecials) {
+  auto expects = [] (const char *first, const char *second) {
+    std::string roundtrip = convertToString(convertToDoubleFromString(second), 0, 3);
+    EXPECT_STREQ(first, roundtrip.c_str());
+  };
+  expects("+Inf", "+Inf");
+  expects("+Inf", "INFINITY");
+  expects("+Inf", "inf");
+  expects("-Inf", "-Inf");
+  expects("-Inf", "-INFINITY");
+  expects("-Inf", "-inf");
+  expects("NaN", "NaN");
+  expects("NaN", "nan");
+  expects("NaN", "-NaN");
+  expects("NaN", "-nan");
+}
+
 TEST(APFloatTest, fromHexadecimalString) {
   EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble(),  "0x1p0").convertToDouble());
   EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble(), "+0x1p0").convertToDouble());


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38030.127463.patch
Type: text/x-patch
Size: 3066 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20171219/e43117aa/attachment.bin>


More information about the llvm-commits mailing list