Skip to content

[flang][runtime] Detect & signal underflow when reading reals #75232

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
Dec 26, 2023
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
1 change: 1 addition & 0 deletions flang/include/flang/Decimal/decimal.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ enum ConversionResultFlags {
Overflow = 1,
Inexact = 2,
Invalid = 4,
Underflow = 8,
};

struct ConversionToDecimalResult {
Expand Down
31 changes: 21 additions & 10 deletions flang/lib/Decimal/decimal-to-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,17 @@ ConversionToBinaryResult<PREC> IntermediateFloat<PREC>::ToBinary(
if (guard != 0) {
flags |= Inexact;
}
if (fraction == 0 && guard <= oneHalf) {
if ((!isNegative && rounding == RoundUp) ||
(isNegative && rounding == RoundDown)) {
// round to minimum nonzero value
} else {
return {Binary{}, static_cast<enum ConversionResultFlags>(flags)};
if (fraction == 0) {
if (guard <= oneHalf) {
if ((!isNegative && rounding == RoundUp) ||
(isNegative && rounding == RoundDown)) {
// round to minimum nonzero value
} else { // round to zero
if (guard != 0) {
flags |= Underflow;
}
return {Binary{}, static_cast<enum ConversionResultFlags>(flags)};
}
}
} else {
// The value is nonzero; normalize it.
Expand Down Expand Up @@ -301,8 +306,10 @@ ConversionToBinaryResult<PREC> IntermediateFloat<PREC>::ToBinary(
}
if (expo == 1 && fraction < topBit) {
expo = 0; // subnormal
}
if (expo >= Binary::maxExponent) {
flags |= Underflow;
} else if (expo == 0) {
flags |= Underflow;
} else if (expo >= Binary::maxExponent) {
expo = Binary::maxExponent; // Inf
flags |= Overflow;
if constexpr (Binary::bits == 80) { // x87
Expand Down Expand Up @@ -338,11 +345,15 @@ BigRadixFloatingPointNumber<PREC, LOG10RADIX>::ConvertToBinary() {
// Sanity checks for ridiculous exponents
static constexpr int crazy{2 * Real::decimalRange + log10Radix};
if (exponent_ < -crazy) {
enum ConversionResultFlags flags {
static_cast<enum ConversionResultFlags>(Inexact | Underflow)
};
if ((!isNegative_ && rounding_ == RoundUp) ||
(isNegative_ && rounding_ == RoundDown)) {
return {Real{Raw{1} | SignBit()}}; // return least nonzero value
// return least nonzero value
return {Real{Raw{1} | SignBit()}, flags};
} else { // underflow to +/-0.
return {Real{SignBit()}, Inexact};
return {Real{SignBit()}, flags};
}
} else if (exponent_ > crazy) { // overflow to +/-Inf.
return {Real{Infinity()}, Overflow};
Expand Down
7 changes: 6 additions & 1 deletion flang/runtime/edit-input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,9 @@ static void RaiseFPExceptions(decimal::ConversionResultFlags flags) {
if (flags & decimal::ConversionResultFlags::Overflow) {
RAISE(FE_OVERFLOW);
}
if (flags & decimal::ConversionResultFlags::Underflow) {
RAISE(FE_UNDERFLOW);
}
if (flags & decimal::ConversionResultFlags::Inexact) {
RAISE(FE_INEXACT);
}
Expand Down Expand Up @@ -640,10 +643,12 @@ decimal::ConversionToBinaryResult<binaryPrecision> ConvertHexadecimal(
}
// Package & return result
constexpr RawType significandMask{(one << RealType::significandBits) - 1};
int flags{(roundingBit | guardBit) ? decimal::Inexact : decimal::Exact};
if (!fraction) {
expo = 0;
} else if (expo == 1 && !(fraction >> (binaryPrecision - 1))) {
expo = 0; // subnormal
flags |= decimal::Underflow;
} else if (expo >= RealType::maxExponent) {
expo = RealType::maxExponent; // +/-Inf
fraction = 0;
Expand All @@ -653,7 +658,7 @@ decimal::ConversionToBinaryResult<binaryPrecision> ConvertHexadecimal(
return decimal::ConversionToBinaryResult<binaryPrecision>{
RealType{static_cast<RawType>(signBit |
static_cast<RawType>(expo) << RealType::significandBits | fraction)},
(roundingBit | guardBit) ? decimal::Inexact : decimal::Exact};
static_cast<enum decimal::ConversionResultFlags>(flags)};
}

template <int KIND>
Expand Down