Fix APFloat from string conversion for Inf
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().
Re-landing after fix.
Reviewers: sberg, bogner, majnemer, timshen, rnk, skatkov, gottesmm, bkramer, scanon, anna
Reviewed By: anna
Subscribers: mkazantsev, FlameTop, llvm-commits, reames, apilipenko
Differential Revision: https://reviews.llvm.org/D38030
llvm-svn: 321054
diff --git a/llvm/unittests/ADT/APFloatTest.cpp b/llvm/unittests/ADT/APFloatTest.cpp
index 84fb6fa..8b88c123b 100644
--- a/llvm/unittests/ADT/APFloatTest.cpp
+++ b/llvm/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());