[llvm] r316156 - Fix APFloat from string conversion for Inf
Serguei Katkov via llvm-commits
llvm-commits at lists.llvm.org
Thu Oct 19 04:16:03 PDT 2017
Author: skatkov
Date: Thu Oct 19 04:16:03 2017
New Revision: 316156
URL: http://llvm.org/viewvc/llvm-project?rev=316156&view=rev
Log:
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().
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
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=316156&r1=316155&r2=316156&view=diff
==============================================================================
--- llvm/trunk/lib/Support/APFloat.cpp (original)
+++ llvm/trunk/lib/Support/APFloat.cpp Thu Oct 19 04:16:03 2017
@@ -2543,12 +2543,12 @@ IEEEFloat::convertFromDecimalString(Stri
}
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;
}
Modified: llvm/trunk/unittests/ADT/APFloatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APFloatTest.cpp?rev=316156&r1=316155&r2=316156&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/APFloatTest.cpp (original)
+++ llvm/trunk/unittests/ADT/APFloatTest.cpp Thu Oct 19 04:16:03 2017
@@ -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());
More information about the llvm-commits
mailing list