[flang][runtime] Fix directed UP/DOWN rounding edge case for Fw.d output

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.

Differential Revision: https://reviews.llvm.org/D129021
This commit is contained in:
Peter Klausler 2022-07-01 11:46:58 -07:00
parent 715283aa17
commit 061df07389

View File

@ -419,15 +419,22 @@ bool RealOutputEditing<binaryPrecision>::EditFOutput(const DataEdit &edit) {
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;