Skip to content

Commit e62555c

Browse files
committed
[APFloat] Fix subtraction of subnormal numbers
Fix incorrect determination of the bigger number out of the two subtracted, while subnormal numbers are involved. Fixes PR44010. Differential Revision: https://reviews.llvm.org/D69772
1 parent 718d68e commit e62555c

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

llvm/lib/Support/APFloat.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -1484,22 +1484,19 @@ lostFraction IEEEFloat::addOrSubtractSignificand(const IEEEFloat &rhs,
14841484
/* Subtraction is more subtle than one might naively expect. */
14851485
if (subtract) {
14861486
IEEEFloat temp_rhs(rhs);
1487-
bool reverse;
14881487

1489-
if (bits == 0) {
1490-
reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
1488+
if (bits == 0)
14911489
lost_fraction = lfExactlyZero;
1492-
} else if (bits > 0) {
1490+
else if (bits > 0) {
14931491
lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
14941492
shiftSignificandLeft(1);
1495-
reverse = false;
14961493
} else {
14971494
lost_fraction = shiftSignificandRight(-bits - 1);
14981495
temp_rhs.shiftSignificandLeft(1);
1499-
reverse = true;
15001496
}
15011497

1502-
if (reverse) {
1498+
// Should we reverse the subtraction.
1499+
if (compareAbsoluteValue(temp_rhs) == cmpLessThan) {
15031500
carry = temp_rhs.subtractSignificand
15041501
(*this, lost_fraction != lfExactlyZero);
15051502
copySignificand(temp_rhs);

llvm/unittests/ADT/APFloatTest.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,15 @@ TEST(APFloatTest, FMA) {
530530
EXPECT_FALSE(losesInfo);
531531
EXPECT_EQ(4.0f, M1.convertToFloat());
532532
}
533+
534+
// Regression test that failed an assertion.
535+
{
536+
APFloat f1(-8.85242279E-41f);
537+
APFloat f2(2.0f);
538+
APFloat f3(8.85242279E-41f);
539+
f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
540+
EXPECT_EQ(-8.85242279E-41f, f1.convertToFloat());
541+
}
533542
}
534543

535544
TEST(APFloatTest, MinNum) {

0 commit comments

Comments
 (0)