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

Ehud Katz via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 22 09:40:14 PST 2019


ekatz updated this revision to Diff 230673.
ekatz added a comment.

Reordered the conditions, to make a smaller diff.

Also replaced the previous test case with the one from PR44010, as it is practically the same, but also tests the result.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D69772/new/

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
@@ -530,6 +530,15 @@
     EXPECT_FALSE(losesInfo);
     EXPECT_EQ(4.0f, M1.convertToFloat());
   }
+
+  // Regression test that failed an assertion.
+  {
+    APFloat f1(-8.85242279E-41f);
+    APFloat f2(2.0f);
+    APFloat f3(8.85242279E-41f);
+    f1.fusedMultiplyAdd(f2, f3, APFloat::rmNearestTiesToEven);
+    EXPECT_EQ(-8.85242279E-41f, f1.convertToFloat());
+  }
 }
 
 TEST(APFloatTest, MinNum) {
Index: llvm/lib/Support/APFloat.cpp
===================================================================
--- llvm/lib/Support/APFloat.cpp
+++ llvm/lib/Support/APFloat.cpp
@@ -1484,22 +1484,19 @@
   /* 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;
+    if (bits == 0)
       lost_fraction = lfExactlyZero;
-    } else if (bits > 0) {
+    else if (bits > 0) {
       lost_fraction = temp_rhs.shiftSignificandRight(bits - 1);
       shiftSignificandLeft(1);
-      reverse = false;
     } else {
       lost_fraction = shiftSignificandRight(-bits - 1);
       temp_rhs.shiftSignificandLeft(1);
-      reverse = true;
     }
 
-    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.230673.patch
Type: text/x-patch
Size: 1636 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191122/900c2aca/attachment.bin>


More information about the llvm-commits mailing list