Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang][runtime] Fix trailing blanks for Gw.dEe output editing #75263

Merged
merged 1 commit into from
Dec 26, 2023

Conversation

klausler
Copy link
Contributor

When generalized numeric output editing of real data maps to Fw.d output editing, either two or four trailing blanks are emitted depending on the presence and value of 'e'. The code that detects field width overflow didn't take these trailing blanks into account, and sometimes the field width adjustment was producing an F0.d output edit descriptor (no fixed field width). Fix by retaining the original field width, but requiring it to also accommodate the trailing blanks.

Fixes llvm-test-suite/Fortran/gfortran/regression/fmt_g.f.

When generalized numeric output editing of real data maps to
Fw.d output editing, either two or four trailing blanks are
emitted depending on the presence and value of 'e'.  The code
that detects field width overflow didn't take these trailing
blanks into account, and sometimes the field width adjustment
was producing an F0.d output edit descriptor (no fixed field
width).  Fix by retaining the original field width, but requiring
it to also accommodate the trailing blanks.

Fixes llvm-test-suite/Fortran/gfortran/regression/fmt_g.f.
@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Dec 13, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Dec 13, 2023

@llvm/pr-subscribers-flang-runtime

Author: Peter Klausler (klausler)

Changes

When generalized numeric output editing of real data maps to Fw.d output editing, either two or four trailing blanks are emitted depending on the presence and value of 'e'. The code that detects field width overflow didn't take these trailing blanks into account, and sometimes the field width adjustment was producing an F0.d output edit descriptor (no fixed field width). Fix by retaining the original field width, but requiring it to also accommodate the trailing blanks.

Fixes llvm-test-suite/Fortran/gfortran/regression/fmt_g.f.


Full diff: https://github.com/llvm/llvm-project/pull/75263.diff

1 Files Affected:

  • (modified) flang/runtime/edit-output.cpp (+10-5)
diff --git a/flang/runtime/edit-output.cpp b/flang/runtime/edit-output.cpp
index a4ce0b12f9111..a2f0893cd7743 100644
--- a/flang/runtime/edit-output.cpp
+++ b/flang/runtime/edit-output.cpp
@@ -341,11 +341,12 @@ bool RealOutputEditing<KIND>::EditEorDOutput(const DataEdit &edit) {
         ConvertToDecimal(significantDigits, edit.modes.round, flags)};
     if (IsInfOrNaN(converted.str, static_cast<int>(converted.length))) {
       return editWidth > 0 &&
-              converted.length > static_cast<std::size_t>(editWidth)
+              converted.length + trailingBlanks_ >
+                  static_cast<std::size_t>(editWidth)
           ? EmitRepeated(io_, '*', editWidth)
           : EmitPrefix(edit, converted.length, editWidth) &&
               EmitAscii(io_, converted.str, converted.length) &&
-              EmitSuffix(edit);
+              EmitRepeated(io_, ' ', trailingBlanks_) && EmitSuffix(edit);
     }
     if (!IsZero()) {
       converted.decimalExponent -= scale;
@@ -522,8 +523,9 @@ bool RealOutputEditing<KIND>::EditFOutput(const DataEdit &edit) {
       zeroesBeforePoint = 1; // "." -> "0."
     }
     int totalLength{signLength + digitsBeforePoint + zeroesBeforePoint +
-        1 /*'.'*/ + zeroesAfterPoint + digitsAfterPoint + trailingZeroes};
-    int width{editWidth > 0 ? editWidth : totalLength};
+        1 /*'.'*/ + zeroesAfterPoint + digitsAfterPoint + trailingZeroes +
+        trailingBlanks_ /* G editing converted to F */};
+    int width{editWidth > 0 || trailingBlanks_ ? editWidth : totalLength};
     if (totalLength > width) {
       return EmitRepeated(io_, '*', width);
     }
@@ -574,8 +576,11 @@ DataEdit RealOutputEditing<KIND>::EditForGOutput(DataEdit edit) {
   trailingBlanks_ = 0;
   if (editWidth > 0) {
     int expoDigits{edit.expoDigits.value_or(0)};
+    // F'2023 13.7.5.2.3 p5: "If 0 <= s <= d, the scale factor has no effect
+    // and F(w − n).(d − s),n(’b’) editing is used where b is a blank and
+    // n is 4 for Gw.d editing, e + 2 for Gw.dEe editing if e > 0, and
+    // 4 for Gw.dE0 editing."
     trailingBlanks_ = expoDigits > 0 ? expoDigits + 2 : 4; // 'n'
-    *edit.width = std::max(0, editWidth - trailingBlanks_);
   }
   if (edit.digits.has_value()) {
     *edit.digits = std::max(0, *edit.digits - expo);

Copy link
Contributor

@vzakhari vzakhari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@klausler klausler merged commit 933882f into llvm:main Dec 26, 2023
6 checks passed
@klausler klausler deleted the fmt-g branch December 26, 2023 23:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants