]> granicus.if.org Git - llvm/commitdiff
Fix APFloat from string conversion for Inf
authorSerguei Katkov <serguei.katkov@azul.com>
Thu, 19 Oct 2017 11:16:03 +0000 (11:16 +0000)
committerSerguei Katkov <serguei.katkov@azul.com>
Thu, 19 Oct 2017 11:16:03 +0000 (11:16 +0000)
The method IEEEFloat::convertFromStringSpecials() does not recognize
the "+Inf" and "-Inf" strings but these strings are printed for
the double Infinities by the IEEEFloat::toString().

This patch adds the "+Inf" and "-Inf" strings to the list of recognized
patterns in IEEEFloat::convertFromStringSpecials().

Reviewers: sberg, bogner, majnemer, timshen, rnk, skatkov, gottesmm, bkramer, scanon
Reviewed By: skatkov
Subscribers: apilipenko, reames, llvm-commits
Differential Revision: https://reviews.llvm.org/D38030

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

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

index f835bd1fbd23048206d105b9708019b8b93c642a..cc77672633ef69afbb6989633b8134d06c48ca85 100644 (file)
@@ -2543,12 +2543,12 @@ IEEEFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode) {
 }
 
 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 280a0862ccc65cb6ebc9096bb458fbc3606f8faa..a0d3e97c3ec7d3af49ade3e8f9d99b7fae4f21a3 100644 (file)
@@ -849,6 +849,22 @@ TEST(APFloatTest, fromDecimalString) {
   EXPECT_EQ(2.71828, convertToDoubleFromString("2.71828"));
 }
 
+TEST(APFloatTest, fromToStringSpecials) {
+  auto roundTrip = [] (const char* str) {
+    return convertToString(convertToDoubleFromString(str), 0, 3).c_str();
+  };
+  EXPECT_STREQ("+Inf", roundTrip("+Inf"));
+  EXPECT_STREQ("+Inf", roundTrip("INFINITY"));
+  EXPECT_STREQ("+Inf", roundTrip("inf"));
+  EXPECT_STREQ("-Inf", roundTrip("-Inf"));
+  EXPECT_STREQ("-Inf", roundTrip("-INFINITY"));
+  EXPECT_STREQ("-Inf", roundTrip("-inf"));
+  EXPECT_STREQ("NaN", roundTrip("NaN"));
+  EXPECT_STREQ("NaN", roundTrip("nan"));
+  EXPECT_STREQ("NaN", roundTrip("-NaN"));
+  EXPECT_STREQ("NaN", roundTrip("-nan"));
+}
+
 TEST(APFloatTest, fromHexadecimalString) {
   EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble(),  "0x1p0").convertToDouble());
   EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble(), "+0x1p0").convertToDouble());