[flang-commits] [PATCH] D129021: [flang][runtime] Fix directed UP/DOWN rounding edge case for Fw.d output

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Fri Jul 1 15:17:38 PDT 2022


klausler created this revision.
klausler added a reviewer: vdonaldson.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a project: All.
klausler requested review of this revision.

When Fw.d output editing takes place with directed rounding, make sure that
nonzero values that would normally be converted to zero round up (or down,
depending on the sign) to a scaled 1.


https://reviews.llvm.org/D129021

Files:
  flang/runtime/edit-output.cpp


Index: flang/runtime/edit-output.cpp
===================================================================
--- flang/runtime/edit-output.cpp
+++ flang/runtime/edit-output.cpp
@@ -419,15 +419,22 @@
       canIncrease = false; // only once
       continue;
     } else if (expo == -fracDigits && convertedDigits > 0) {
-      if (rounding != decimal::FortranRounding::RoundToZero) {
-        // Convert again without rounding so that we can round here
-        rounding = decimal::FortranRounding::RoundToZero;
-        continue;
-      } else if (converted.str[signLength] >= '5') {
-        // Value rounds up to a scaled 1 (e.g., 0.06 for F5.1 -> 0.1)
+      if ((rounding == decimal::FortranRounding::RoundUp &&
+              *converted.str != '-') ||
+          (rounding == decimal::FortranRounding::RoundDown &&
+              *converted.str == '-') ||
+          (rounding == decimal::FortranRounding::RoundToZero &&
+              rounding != edit.modes.round && // it changed below
+              converted.str[signLength] >= '5')) {
+        // Round up/down to a scaled 1
         ++expo;
         convertedDigits = 0;
         trailingOnes = 1;
+      } else if (rounding != decimal::FortranRounding::RoundToZero) {
+        // Convert again with truncation so first digit can be checked
+        // on the next iteration by the code above
+        rounding = decimal::FortranRounding::RoundToZero;
+        continue;
       } else {
         // Value rounds down to zero
         expo = 0;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129021.441796.patch
Type: text/x-patch
Size: 1501 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220701/7f6f5c01/attachment-0001.bin>


More information about the flang-commits mailing list