Skip to content

Commit df6afee

Browse files
committed
[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
1 parent 5ca68d5 commit df6afee

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

flang/runtime/edit-output.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ const char *RealOutputEditingBase::FormatExponent(
199199
}
200200
*--exponent = expo < 0 ? '-' : '+';
201201
if (edit.expoDigits || edit.IsListDirected() || exponent + 3 == eEnd) {
202-
*--exponent = edit.descriptor == 'D' ? 'D' : 'E'; // not 'G'
202+
*--exponent = edit.descriptor == 'D' ? 'D' : 'E'; // not 'G' or 'Q'
203203
}
204204
length = eEnd - exponent;
205205
return overflow ? nullptr : exponent;
@@ -264,9 +264,7 @@ bool RealOutputEditing<binaryPrecision>::EditEorDOutput(const DataEdit &edit) {
264264
if (editWidth == 0) { // "the processor selects the field width"
265265
if (edit.digits.has_value()) { // E0.d
266266
if (editDigits == 0) { // E0.0
267-
editWidth = 7; // -.0E+ee
268-
} else {
269-
editWidth = editDigits + 6; // -.666E+ee
267+
significantDigits = 1;
270268
}
271269
} else { // E0
272270
flags |= decimal::Minimize;
@@ -485,7 +483,7 @@ DataEdit RealOutputEditing<binaryPrecision>::EditForGOutput(DataEdit edit) {
485483
int significantDigits{
486484
edit.digits.value_or(BinaryFloatingPoint::decimalPrecision)}; // 'd'
487485
if (editWidth > 0 && significantDigits == 0) {
488-
return edit; // Gw.0 -> Ew.0 for w > 0
486+
return edit; // Gw.0Ee -> Ew.0Ee for w > 0
489487
}
490488
int flags{0};
491489
if (edit.modes.editingFlags & signPlus) {
@@ -498,7 +496,10 @@ DataEdit RealOutputEditing<binaryPrecision>::EditForGOutput(DataEdit edit) {
498496
}
499497
int expo{IsZero() ? 1 : converted.decimalExponent}; // 's'
500498
if (expo < 0 || expo > significantDigits) {
501-
return edit; // Ew.d
499+
if (editWidth == 0 && !edit.expoDigits) { // G0.d -> G0.dE0
500+
edit.expoDigits = 0;
501+
}
502+
return edit; // Ew.dEe
502503
}
503504
edit.descriptor = 'F';
504505
edit.modes.scale = 0; // kP is ignored for G when no exponent field

flang/unittests/Runtime/NumericalFormatTest.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,9 +394,9 @@ TEST(IOApiTests, FormatDoubleValues) {
394394
{"(E62.55,';')",
395395
" 0.1000000000000000055511151231257827021181583404541015625E+"
396396
"00;"},
397-
{"(E0.0,';')", "0.E+00;"},
397+
{"(E0.0,';')", ".1E+00;"},
398398
{"(E0.55,';')",
399-
"0.1000000000000000055511151231257827021181583404541015625E+"
399+
".1000000000000000055511151231257827021181583404541015625E+"
400400
"00;"},
401401
{"(E0,';')", ".1E+00;"},
402402
{"(F58.55,';')",
@@ -491,7 +491,7 @@ TEST(IOApiTests, FormatDoubleValues) {
491491
"701797267771758512566055119913150489110145103786273816725095"
492492
"583738973359899366480994116420570263709027924276754456522908"
493493
"75386825064197182655334472656250-323;"},
494-
{"(G0,';')", ".5-323;"},
494+
{"(G0,';')", ".5E-323;"},
495495
{"(E757.750,';')",
496496
" 0."
497497
"494065645841246544176568792868221372365059802614324764425585"
@@ -586,7 +586,7 @@ TEST(IOApiTests, FormatDoubleValues) {
586586
"408698898317506783884692609277397797285865965494109136909540"
587587
"61364675687023986783152906809846172109246253967285156250-"
588588
"307;"},
589-
{"(G0,';')", ".22250738585072014-307;"},
589+
{"(G0,';')", ".22250738585072014E-307;"},
590590
}},
591591
{// greatest finite
592592
0x7fefffffffffffffuLL,
@@ -616,7 +616,7 @@ TEST(IOApiTests, FormatDoubleValues) {
616616
"090389328944075868508455133942304583236903222948165808559332"
617617
"123348274797826204144723168738177180919299881250404026184124"
618618
"8583680000+306;"},
619-
{"(G0,';')", ".17976931348623157+309;"},
619+
{"(G0,';')", ".17976931348623157E+309;"},
620620
}},
621621
};
622622

0 commit comments

Comments
 (0)