Skip to content

Commit f45723c

Browse files
authored
[flang][runtime] Fix RU/RD results when rounding to least nonzero (#75878)
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.
1 parent 1346037 commit f45723c

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

flang/lib/Decimal/decimal-to-binary.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ ConversionToBinaryResult<PREC> IntermediateFloat<PREC>::ToBinary(
270270
if ((!isNegative && rounding == RoundUp) ||
271271
(isNegative && rounding == RoundDown)) {
272272
// round to least nonzero value
273+
expo = 0;
273274
} else { // round to zero
274275
if (guard != 0) {
275276
flags |= Underflow;

flang/unittests/Runtime/NumericalFormatTest.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,10 @@ TEST(IOApiTests, EditDoubleInputValues) {
914914
{"(RZ,F7.0)", "-1.e999", 0xffefffffffffffff, 0}, // -HUGE()
915915
{"(RD,F7.0)", "-1.e999", 0xfff0000000000000, ovf}, // -Inf
916916
{"(RU,F7.0)", "-1.e999", 0xffefffffffffffff, 0}, // -HUGE()
917+
{"(E9.1)", " 1.0E-325", 0x0, 0},
918+
{"(RU,E9.1)", " 1.0E-325", 0x1, 0},
919+
{"(E9.1)", "-1.0E-325", 0x0, 0},
920+
{"(RD,E9.1)", "-1.0E-325", 0x8000000000000001, 0},
917921
};
918922
for (auto const &[format, data, want, iostat] : testCases) {
919923
auto cookie{IONAME(BeginInternalFormattedInput)(

0 commit comments

Comments
 (0)