Skip to content

[flang][runtime] Improve confusing list-directed REAL(2) output #89846

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

Merged
merged 1 commit into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions flang/runtime/edit-output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ RT_API_ATTRS bool RealOutputEditing<KIND>::EditFOutput(const DataEdit &edit) {
fracDigits = sizeof buffer_ - 2; // sign & NUL
}
}
bool emitTrailingZeroes{!(flags & decimal::Minimize)};
// Multiple conversions may be needed to get the right number of
// effective rounded fractional digits.
bool canIncrease{true};
Expand Down Expand Up @@ -526,11 +527,18 @@ RT_API_ATTRS bool RealOutputEditing<KIND>::EditFOutput(const DataEdit &edit) {
}
int digitsBeforePoint{std::max(0, std::min(expo, convertedDigits))};
int zeroesBeforePoint{std::max(0, expo - digitsBeforePoint)};
if (zeroesBeforePoint > 0 && (flags & decimal::Minimize)) {
// If a minimized result looks like an integer, emit all of
// its digits rather than clipping some to zeroes.
// This can happen with HUGE(0._2) == 65504._2.
flags &= ~decimal::Minimize;
continue;
}
int zeroesAfterPoint{std::min(fracDigits, std::max(0, -expo))};
int digitsAfterPoint{convertedDigits - digitsBeforePoint};
int trailingZeroes{flags & decimal::Minimize
? 0
: std::max(0, fracDigits - (zeroesAfterPoint + digitsAfterPoint))};
int trailingZeroes{emitTrailingZeroes
? std::max(0, fracDigits - (zeroesAfterPoint + digitsAfterPoint))
: 0};
if (digitsBeforePoint + zeroesBeforePoint + zeroesAfterPoint +
digitsAfterPoint + trailingZeroes ==
0) {
Expand Down
17 changes: 17 additions & 0 deletions flang/unittests/Runtime/NumericalFormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -958,3 +958,20 @@ TEST(IOApiTests, EditDoubleInputValues) {
<< "', want " << want << ", got " << u.raw;
}
}

// regression test for confusing digit minimization
TEST(IOApiTests, ConfusingMinimization) {
char buffer[8]{};
auto cookie{IONAME(BeginInternalListOutput)(buffer, sizeof buffer)};
StaticDescriptor<0> staticDescriptor;
Descriptor &desc{staticDescriptor.descriptor()};
std::uint16_t x{0x7bff}; // HUGE(0._2)
desc.Establish(TypeCode{CFI_type_half_float}, sizeof x, &x, 0, nullptr);
desc.Check();
EXPECT_TRUE(IONAME(OutputDescriptor)(cookie, desc));
auto status{IONAME(EndIoStatement)(cookie)};
EXPECT_EQ(status, 0);
std::string got{std::string{buffer, sizeof buffer}};
EXPECT_TRUE(CompareFormattedStrings(" 65504. ", got))
<< "expected ' 65504. ', got '" << got << '\''; // not 65500.!
}