Skip to content

[flang][runtime] Round hex REAL input correctly with excess digits #85587

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
Mar 18, 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
71 changes: 42 additions & 29 deletions flang/runtime/edit-input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -614,11 +614,23 @@ decimal::ConversionToBinaryResult<binaryPrecision> ConvertHexadecimal(
} else {
break;
}
while (fraction >> binaryPrecision) {
guardBit |= roundingBit;
roundingBit = (int)fraction & 1;
fraction >>= 1;
++expo;
if (fraction >> binaryPrecision) {
while (fraction >> binaryPrecision) {
guardBit |= roundingBit;
roundingBit = (int)fraction & 1;
fraction >>= 1;
++expo;
}
// Consume excess digits
while (*++p) {
if (*p == '0') {
} else if ((*p >= '1' && *p <= '9') || (*p >= 'A' && *p <= 'F')) {
guardBit = 1;
} else {
break;
}
}
break;
}
}
if (fraction) {
Expand All @@ -633,31 +645,32 @@ decimal::ConversionToBinaryResult<binaryPrecision> ConvertHexadecimal(
while (expo > 1 && !(fraction >> (binaryPrecision - 1))) {
fraction <<= 1;
--expo;
guardBit = roundingBit = 0;
}
// Rounding
bool increase{false};
switch (rounding) {
case decimal::RoundNearest: // RN & RP
increase = roundingBit && (guardBit | ((int)fraction & 1));
break;
case decimal::RoundUp: // RU
increase = !isNegative && (roundingBit | guardBit);
break;
case decimal::RoundDown: // RD
increase = isNegative && (roundingBit | guardBit);
break;
case decimal::RoundToZero: // RZ
break;
case decimal::RoundCompatible: // RC
increase = roundingBit != 0;
break;
}
if (increase) {
++fraction;
if (fraction >> binaryPrecision) {
fraction >>= 1;
++expo;
}
}
// Rounding
bool increase{false};
switch (rounding) {
case decimal::RoundNearest: // RN & RP
increase = roundingBit && (guardBit | ((int)fraction & 1));
break;
case decimal::RoundUp: // RU
increase = !isNegative && (roundingBit | guardBit);
break;
case decimal::RoundDown: // RD
increase = isNegative && (roundingBit | guardBit);
break;
case decimal::RoundToZero: // RZ
break;
case decimal::RoundCompatible: // RC
increase = roundingBit != 0;
break;
}
if (increase) {
++fraction;
if (fraction >> binaryPrecision) {
fraction >>= 1;
++expo;
}
}
// Package & return result
Expand Down
8 changes: 8 additions & 0 deletions flang/unittests/Runtime/NumericalFormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,14 @@ TEST(IOApiTests, EditDoubleInputValues) {
0}, // max finite
{"(EX22.0)", "0X.8P1025 ", 0x7ff0000000000000, ovf}, // +Inf
{"(EX22.0)", "-0X.8P1025 ", 0xfff0000000000000, ovf}, // -Inf
{"(RC,EX22.0)", "0X1.0000000000000P0 ", 0x3ff0000000000000, 0},
{"(RC,EX22.0)", "0X1.00000000000008P0 ", 0x3ff0000000000001, 0},
{"(RC,EX22.0)", "0X1.000000000000008P0 ", 0x3ff0000000000000, 0},
{"(RC,EX22.0)", "0X1.00000000000004P0 ", 0x3ff0000000000000, 0},
{"(RC,EX22.0)", "0X.80000000000000P1 ", 0x3ff0000000000000, 0},
{"(RC,EX22.0)", "0X.80000000000004P1 ", 0x3ff0000000000001, 0},
{"(RC,EX22.0)", "0X.800000000000004P1 ", 0x3ff0000000000000, 0},
{"(RC,EX22.0)", "0X.80000000000002P1 ", 0x3ff0000000000000, 0},
{"(RZ,F7.0)", " 2.e308", 0x7fefffffffffffff, 0}, // +HUGE()
{"(RD,F7.0)", " 2.e308", 0x7fefffffffffffff, 0}, // +HUGE()
{"(RU,F7.0)", " 2.e308", 0x7ff0000000000000, ovf}, // +Inf
Expand Down