Skip to content

Commit

Permalink
Fix APFloat from string conversion for Inf
Browse files Browse the repository at this point in the history
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

llvm-svn: 316156
  • Loading branch information
Serguei Katkov committed Oct 19, 2017
1 parent 7bf7100 commit 4f7d5ef
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions llvm/lib/Support/APFloat.cpp
Expand Up @@ -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;
}
Expand Down
16 changes: 16 additions & 0 deletions llvm/unittests/ADT/APFloatTest.cpp
Expand Up @@ -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());
Expand Down

0 comments on commit 4f7d5ef

Please sign in to comment.