Skip to content

Commit 28007da

Browse files
committed
[flang][runtime] Don't round hexadecimal floating-point input
Fortran 2023 subclause 13.7.2.3.8 discusses input rounding only in the context of decimal-to-binary conversion. There is no mention of rounding for hexadecimal floating-point input conversion. At least one Fortran compiler seems to have interpreted this silence as implying no rounding. (Note that this is not the same thing as rounding to zero (RZ), which would return +/-HUGE() for overflow.)
1 parent 90c397f commit 28007da

File tree

2 files changed

+9
-35
lines changed

2 files changed

+9
-35
lines changed

flang/docs/Extensions.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,12 @@ end
645645
only in function references, but not an explicit `INTRINSIC` statement,
646646
its name is not brought into other scopes by a `USE` statement.
647647

648+
* Should hexadecimal floating-point input editing apply any rounding?
649+
F'2023 subclause 13.7.2.3.8 only discusses rounding in the context of
650+
decimal-to-binary conversion; it would seem to not apply, and so
651+
we don't round. This seems to be how the Intel Fortran compilers
652+
behave.
653+
648654
## De Facto Standard Features
649655

650656
* `EXTENDS_TYPE_OF()` returns `.TRUE.` if both of its arguments have the

flang/runtime/edit-input.cpp

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -624,31 +624,6 @@ decimal::ConversionToBinaryResult<binaryPrecision> ConvertHexadecimal(
624624
fraction <<= 1;
625625
--expo;
626626
}
627-
// Rounding
628-
bool increase{false};
629-
switch (rounding) {
630-
case decimal::RoundNearest: // RN & RP
631-
increase = roundingBit && (guardBit | ((int)fraction & 1));
632-
break;
633-
case decimal::RoundUp: // RU
634-
increase = !isNegative && (roundingBit | guardBit);
635-
break;
636-
case decimal::RoundDown: // RD
637-
increase = isNegative && (roundingBit | guardBit);
638-
break;
639-
case decimal::RoundToZero: // RZ
640-
break;
641-
case decimal::RoundCompatible: // RC
642-
increase = roundingBit != 0;
643-
break;
644-
}
645-
if (increase) {
646-
++fraction;
647-
if (fraction >> binaryPrecision) {
648-
fraction >>= 1;
649-
++expo;
650-
}
651-
}
652627
}
653628
// Package & return result
654629
constexpr RawType significandMask{(one << RealType::significandBits) - 1};
@@ -659,16 +634,9 @@ decimal::ConversionToBinaryResult<binaryPrecision> ConvertHexadecimal(
659634
expo = 0; // subnormal
660635
flags |= decimal::Underflow;
661636
} else if (expo >= RealType::maxExponent) {
662-
if (rounding == decimal::RoundToZero ||
663-
(rounding == decimal::RoundDown && !isNegative) ||
664-
(rounding == decimal::RoundUp && isNegative)) {
665-
expo = RealType::maxExponent - 1; // +/-HUGE()
666-
fraction = significandMask;
667-
} else {
668-
expo = RealType::maxExponent; // +/-Inf
669-
fraction = 0;
670-
flags |= decimal::Overflow;
671-
}
637+
expo = RealType::maxExponent; // +/-Inf
638+
fraction = 0;
639+
flags |= decimal::Overflow;
672640
} else {
673641
fraction &= significandMask; // remove explicit normalization unless x87
674642
}

0 commit comments

Comments
 (0)