Skip to content

Commit 768d6dd

Browse files
author
Serguei Katkov
committed
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
1 parent 63a328c commit 768d6dd

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

llvm/lib/Support/APFloat.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2546,12 +2546,12 @@ IEEEFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode) {
25462546
}
25472547

25482548
bool IEEEFloat::convertFromStringSpecials(StringRef str) {
2549-
if (str.equals("inf") || str.equals("INFINITY")) {
2549+
if (str.equals("inf") || str.equals("INFINITY") || str.equals("+Inf")) {
25502550
makeInf(false);
25512551
return true;
25522552
}
25532553

2554-
if (str.equals("-inf") || str.equals("-INFINITY")) {
2554+
if (str.equals("-inf") || str.equals("-INFINITY") || str.equals("-Inf")) {
25552555
makeInf(true);
25562556
return true;
25572557
}

llvm/lib/Support/StringRef.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ bool StringRef::getAsDouble(double &Result, bool AllowInexact) const {
586586
APFloat::opStatus Status =
587587
F.convertFromString(*this, APFloat::rmNearestTiesToEven);
588588
if (Status != APFloat::opOK) {
589-
if (!AllowInexact || Status != APFloat::opInexact)
589+
if (!AllowInexact || !(Status & APFloat::opInexact))
590590
return true;
591591
}
592592

llvm/unittests/ADT/APFloatTest.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,23 @@ TEST(APFloatTest, fromDecimalString) {
849849
EXPECT_EQ(2.71828, convertToDoubleFromString("2.71828"));
850850
}
851851

852+
TEST(APFloatTest, fromToStringSpecials) {
853+
auto expects = [] (const char *first, const char *second) {
854+
std::string roundtrip = convertToString(convertToDoubleFromString(second), 0, 3);
855+
EXPECT_STREQ(first, roundtrip.c_str());
856+
};
857+
expects("+Inf", "+Inf");
858+
expects("+Inf", "INFINITY");
859+
expects("+Inf", "inf");
860+
expects("-Inf", "-Inf");
861+
expects("-Inf", "-INFINITY");
862+
expects("-Inf", "-inf");
863+
expects("NaN", "NaN");
864+
expects("NaN", "nan");
865+
expects("NaN", "-NaN");
866+
expects("NaN", "-nan");
867+
}
868+
852869
TEST(APFloatTest, fromHexadecimalString) {
853870
EXPECT_EQ( 1.0, APFloat(APFloat::IEEEdouble(), "0x1p0").convertToDouble());
854871
EXPECT_EQ(+1.0, APFloat(APFloat::IEEEdouble(), "+0x1p0").convertToDouble());

llvm/unittests/ADT/StringRefTest.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,12 @@ struct GetDoubleStrings {
875875
{"0.0", false, false, 0.0},
876876
{"-0.0", false, false, -0.0},
877877
{"123.45", false, true, 123.45},
878-
{"123.45", true, false, 123.45}};
878+
{"123.45", true, false, 123.45},
879+
{"1.8e308", true, false, std::numeric_limits<double>::infinity()},
880+
{"1.8e308", false, true, std::numeric_limits<double>::infinity()},
881+
{"0x0.0000000000001P-1023", false, true, 0.0},
882+
{"0x0.0000000000001P-1023", true, false, 0.0},
883+
};
879884

880885
TEST(StringRefTest, getAsDouble) {
881886
for (const auto &Entry : DoubleStrings) {

0 commit comments

Comments
 (0)