[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
Wed Jul 6 17:39:21 PDT 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rG061df07389f3: [flang][runtime] Fix directed UP/DOWN rounding edge case for Fw.d output (authored by klausler).

Repository:
  rG LLVM Github Monorepo

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

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.442734.patch
Type: text/x-patch
Size: 1501 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20220707/40b5c794/attachment.bin>


More information about the flang-commits mailing list