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

Conversation

klausler
Copy link
Contributor

Extend decimal->binary conversion to detect underflow cases and raise the corresponding floating-point exception.

@klausler klausler requested a review from vdonaldson December 12, 2023 18:39
@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Dec 12, 2023
@llvmbot
Copy link
Member

llvmbot commented Dec 12, 2023

@llvm/pr-subscribers-flang-runtime

Author: Peter Klausler (klausler)

Changes

Extend decimal->binary conversion to detect underflow cases and raise the corresponding floating-point exception.


Full diff: https://github.com/llvm/llvm-project/pull/75232.diff

3 Files Affected:

  • (modified) flang/include/flang/Decimal/decimal.h (+1)
  • (modified) flang/lib/Decimal/decimal-to-binary.cpp (+8-3)
  • (modified) flang/runtime/edit-input.cpp (+3)
diff --git a/flang/include/flang/Decimal/decimal.h b/flang/include/flang/Decimal/decimal.h
index a4e0ee7c84746..f0997fb63df01 100644
--- a/flang/include/flang/Decimal/decimal.h
+++ b/flang/include/flang/Decimal/decimal.h
@@ -34,6 +34,7 @@ enum ConversionResultFlags {
   Overflow = 1,
   Inexact = 2,
   Invalid = 4,
+  Underflow = 8,
 };
 
 struct ConversionToDecimalResult {
diff --git a/flang/lib/Decimal/decimal-to-binary.cpp b/flang/lib/Decimal/decimal-to-binary.cpp
index d5b66b9fb9338..663deb564a01e 100644
--- a/flang/lib/Decimal/decimal-to-binary.cpp
+++ b/flang/lib/Decimal/decimal-to-binary.cpp
@@ -261,6 +261,7 @@ ConversionToBinaryResult<PREC> IntermediateFloat<PREC>::ToBinary(
         (isNegative && rounding == RoundDown)) {
       // round to minimum nonzero value
     } else {
+      flags |= Underflow;
       return {Binary{}, static_cast<enum ConversionResultFlags>(flags)};
     }
   } else {
@@ -342,7 +343,8 @@ BigRadixFloatingPointNumber<PREC, LOG10RADIX>::ConvertToBinary() {
         (isNegative_ && rounding_ == RoundDown)) {
       return {Real{Raw{1} | SignBit()}}; // return least nonzero value
     } else { // underflow to +/-0.
-      return {Real{SignBit()}, Inexact};
+      return {Real{SignBit()},
+          static_cast<enum ConversionResultFlags>(Inexact | Underflow)};
     }
   } else if (exponent_ > crazy) { // overflow to +/-Inf.
     return {Real{Infinity()}, Overflow};
@@ -417,8 +419,11 @@ BigRadixFloatingPointNumber<PREC, LOG10RADIX>::ConvertToBinary(
   if (ParseNumber(p, inexact, limit)) {
     auto result{ConvertToBinary()};
     if (inexact) {
-      result.flags =
-          static_cast<enum ConversionResultFlags>(result.flags | Inexact);
+      int flags{result.flags | Inexact};
+      if (result.binary.IsZero()) {
+        flags |= Underflow;
+      }
+      result.flags = static_cast<enum ConversionResultFlags>(flags);
     }
     return result;
   } else {
diff --git a/flang/runtime/edit-input.cpp b/flang/runtime/edit-input.cpp
index 822099b5141b1..49156603ea265 100644
--- a/flang/runtime/edit-input.cpp
+++ b/flang/runtime/edit-input.cpp
@@ -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);
   }

Extend decimal->binary conversion to detect underflow cases
and raise the corresponding floating-point exception.
Underflow includes subnormal results.
@klausler
Copy link
Contributor Author

Updated to interpret subnormal results as underflows.

@klausler klausler merged commit befdfae into llvm:main Dec 26, 2023
@klausler klausler deleted the bug1449 branch December 26, 2023 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants