Skip to content

Commit

Permalink
[flang][runtime] Fix RU/RD results when rounding to least nonzero (#7…
Browse files Browse the repository at this point in the history
…5878)

When rounding what otherwise would have been a result that underflowed
to zero up (RU) or down (RD) to the least magnitude nonzero subnormal
number, ensure that the original exponent value doesn't perturb the
result.
  • Loading branch information
klausler committed Dec 26, 2023
1 parent 1346037 commit f45723c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions flang/lib/Decimal/decimal-to-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ ConversionToBinaryResult<PREC> IntermediateFloat<PREC>::ToBinary(
if ((!isNegative && rounding == RoundUp) ||
(isNegative && rounding == RoundDown)) {
// round to least nonzero value
expo = 0;
} else { // round to zero
if (guard != 0) {
flags |= Underflow;
Expand Down
4 changes: 4 additions & 0 deletions flang/unittests/Runtime/NumericalFormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,10 @@ TEST(IOApiTests, EditDoubleInputValues) {
{"(RZ,F7.0)", "-1.e999", 0xffefffffffffffff, 0}, // -HUGE()
{"(RD,F7.0)", "-1.e999", 0xfff0000000000000, ovf}, // -Inf
{"(RU,F7.0)", "-1.e999", 0xffefffffffffffff, 0}, // -HUGE()
{"(E9.1)", " 1.0E-325", 0x0, 0},
{"(RU,E9.1)", " 1.0E-325", 0x1, 0},
{"(E9.1)", "-1.0E-325", 0x0, 0},
{"(RD,E9.1)", "-1.0E-325", 0x8000000000000001, 0},
};
for (auto const &[format, data, want, iostat] : testCases) {
auto cookie{IONAME(BeginInternalFormattedInput)(
Expand Down

0 comments on commit f45723c

Please sign in to comment.