[flang-commits] [flang] df6afee - [flang][runtime] Improve G0 output editing

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Thu Jun 23 11:27:36 PDT 2022


Author: Peter Klausler
Date: 2022-06-23T11:27:25-07:00
New Revision: df6afee9859f982fa927dc90398e82d689f19db6

URL: https://github.com/llvm/llvm-project/commit/df6afee9859f982fa927dc90398e82d689f19db6
DIFF: https://github.com/llvm/llvm-project/commit/df6afee9859f982fa927dc90398e82d689f19db6.diff

LOG: [flang][runtime] Improve G0 output editing

G0 output editing should never overflow an output field and fill it
with asterisks.  It should also never elide the "E" in an exponent
field, even if it has more than three digits.

Differential Revision: https://reviews.llvm.org/D128396

Added: 
    

Modified: 
    flang/runtime/edit-output.cpp
    flang/unittests/Runtime/NumericalFormatTest.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/edit-output.cpp b/flang/runtime/edit-output.cpp
index 9b25ef2fba16f..7dc60cbd3adbd 100644
--- a/flang/runtime/edit-output.cpp
+++ b/flang/runtime/edit-output.cpp
@@ -199,7 +199,7 @@ const char *RealOutputEditingBase::FormatExponent(
   }
   *--exponent = expo < 0 ? '-' : '+';
   if (edit.expoDigits || edit.IsListDirected() || exponent + 3 == eEnd) {
-    *--exponent = edit.descriptor == 'D' ? 'D' : 'E'; // not 'G'
+    *--exponent = edit.descriptor == 'D' ? 'D' : 'E'; // not 'G' or 'Q'
   }
   length = eEnd - exponent;
   return overflow ? nullptr : exponent;
@@ -264,9 +264,7 @@ bool RealOutputEditing<binaryPrecision>::EditEorDOutput(const DataEdit &edit) {
   if (editWidth == 0) { // "the processor selects the field width"
     if (edit.digits.has_value()) { // E0.d
       if (editDigits == 0) { // E0.0
-        editWidth = 7; // -.0E+ee
-      } else {
-        editWidth = editDigits + 6; // -.666E+ee
+        significantDigits = 1;
       }
     } else { // E0
       flags |= decimal::Minimize;
@@ -485,7 +483,7 @@ DataEdit RealOutputEditing<binaryPrecision>::EditForGOutput(DataEdit edit) {
   int significantDigits{
       edit.digits.value_or(BinaryFloatingPoint::decimalPrecision)}; // 'd'
   if (editWidth > 0 && significantDigits == 0) {
-    return edit; // Gw.0 -> Ew.0 for w > 0
+    return edit; // Gw.0Ee -> Ew.0Ee for w > 0
   }
   int flags{0};
   if (edit.modes.editingFlags & signPlus) {
@@ -498,7 +496,10 @@ DataEdit RealOutputEditing<binaryPrecision>::EditForGOutput(DataEdit edit) {
   }
   int expo{IsZero() ? 1 : converted.decimalExponent}; // 's'
   if (expo < 0 || expo > significantDigits) {
-    return edit; // Ew.d
+    if (editWidth == 0 && !edit.expoDigits) { // G0.d -> G0.dE0
+      edit.expoDigits = 0;
+    }
+    return edit; // Ew.dEe
   }
   edit.descriptor = 'F';
   edit.modes.scale = 0; // kP is ignored for G when no exponent field

diff  --git a/flang/unittests/Runtime/NumericalFormatTest.cpp b/flang/unittests/Runtime/NumericalFormatTest.cpp
index d48e29bcc6e97..40191137fe189 100644
--- a/flang/unittests/Runtime/NumericalFormatTest.cpp
+++ b/flang/unittests/Runtime/NumericalFormatTest.cpp
@@ -394,9 +394,9 @@ TEST(IOApiTests, FormatDoubleValues) {
               {"(E62.55,';')",
                   " 0.1000000000000000055511151231257827021181583404541015625E+"
                   "00;"},
-              {"(E0.0,';')", "0.E+00;"},
+              {"(E0.0,';')", ".1E+00;"},
               {"(E0.55,';')",
-                  "0.1000000000000000055511151231257827021181583404541015625E+"
+                  ".1000000000000000055511151231257827021181583404541015625E+"
                   "00;"},
               {"(E0,';')", ".1E+00;"},
               {"(F58.55,';')",
@@ -491,7 +491,7 @@ TEST(IOApiTests, FormatDoubleValues) {
                   "701797267771758512566055119913150489110145103786273816725095"
                   "583738973359899366480994116420570263709027924276754456522908"
                   "75386825064197182655334472656250-323;"},
-              {"(G0,';')", ".5-323;"},
+              {"(G0,';')", ".5E-323;"},
               {"(E757.750,';')",
                   " 0."
                   "494065645841246544176568792868221372365059802614324764425585"
@@ -586,7 +586,7 @@ TEST(IOApiTests, FormatDoubleValues) {
                   "408698898317506783884692609277397797285865965494109136909540"
                   "61364675687023986783152906809846172109246253967285156250-"
                   "307;"},
-              {"(G0,';')", ".22250738585072014-307;"},
+              {"(G0,';')", ".22250738585072014E-307;"},
           }},
       {// greatest finite
           0x7fefffffffffffffuLL,
@@ -616,7 +616,7 @@ TEST(IOApiTests, FormatDoubleValues) {
                   "090389328944075868508455133942304583236903222948165808559332"
                   "123348274797826204144723168738177180919299881250404026184124"
                   "8583680000+306;"},
-              {"(G0,';')", ".17976931348623157+309;"},
+              {"(G0,';')", ".17976931348623157E+309;"},
           }},
   };
 


        


More information about the flang-commits mailing list