[PATCH] D69772: [APFloat] Fix subtraction of subnormal numbers

Ehud Katz via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 3 12:39:10 PST 2019


ekatz created this revision.
ekatz added reviewers: chandlerc, lattner.
ekatz added a project: LLVM.
Herald added subscribers: llvm-commits, dexonsmith, hiraditya.

Fix incorrect determination of the bigger number out of the two subtracted, while subnormal numbers are involved.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D69772

Files:
  llvm/lib/Support/APFloat.cpp
  llvm/unittests/ADT/APFloatTest.cpp


Index: llvm/unittests/ADT/APFloatTest.cpp
===================================================================
--- llvm/unittests/ADT/APFloatTest.cpp
+++ llvm/unittests/ADT/APFloatTest.cpp
@@ -538,6 +538,16 @@
     EXPECT_FALSE(losesInfo);
     EXPECT_EQ(4.0f, M1.convertToFloat());
   }
+
+  // Test addition of a subnormal number with a negative normal.
+  // This used to trigger an assert failure.
+  {
+    APFloat f1(123.456);
+    APFloat f2(APFloat::IEEEdouble(), "-0x1p-1074");
+    APFloat f3(APFloat::IEEEdouble(), "+0x1p-1074");
+
+    f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
+  }
 }
 
 TEST(APFloatTest, MinNum) {
Index: llvm/lib/Support/APFloat.cpp
===================================================================
--- llvm/lib/Support/APFloat.cpp
+++ llvm/lib/Support/APFloat.cpp
@@ -1545,22 +1545,18 @@
   /* Subtraction is more subtle than one might naively expect.  */
   if (subtract) {
     IEEEFloat temp_rhs(rhs);
-    bool reverse;
 
-    if (bits == 0) {
-      reverse = compareAbsoluteValue(temp_rhs) == cmpLessThan;
-      lost_fraction = lfExactlyZero;
-    } else if (bits > 0) {
+    if (bits > 0) {
       lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
       shiftSignificandLeft(1);
-      reverse = false;
-    } else {
+    } else if (bits < 0) {
       lost_fraction = shiftSignificandRight(-bits - 1);
       temp_rhs.shiftSignificandLeft(1);
-      reverse = true;
-    }
+    } else
+      lost_fraction = lfExactlyZero;
 
-    if (reverse) {
+    // Should we reverse the subtraction.
+    if (compareAbsoluteValue(temp_rhs) == cmpLessThan) {
       carry = temp_rhs.subtractSignificand
         (*this, lost_fraction != lfExactlyZero);
       copySignificand(temp_rhs);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69772.227627.patch
Type: text/x-patch
Size: 1742 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191103/572a4bde/attachment.bin>


More information about the llvm-commits mailing list