[flang-commits] [flang] [flang][runtime] Fix RU/RD results when rounding to least nonzero (PR #75878)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Mon Dec 18 16:28:27 PST 2023


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/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.

>From ca1215e90d6a6e352a11ea61f1904d420b0a2ed0 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Mon, 18 Dec 2023 16:25:03 -0800
Subject: [PATCH] [flang][runtime] Fix RU/RD results when rounding to least
 nonzero

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.
---
 flang/lib/Decimal/decimal-to-binary.cpp         | 1 +
 flang/unittests/Runtime/NumericalFormatTest.cpp | 4 ++++
 2 files changed, 5 insertions(+)

diff --git a/flang/lib/Decimal/decimal-to-binary.cpp b/flang/lib/Decimal/decimal-to-binary.cpp
index d5b66b9fb93388..3d41937f98c3e7 100644
--- a/flang/lib/Decimal/decimal-to-binary.cpp
+++ b/flang/lib/Decimal/decimal-to-binary.cpp
@@ -260,6 +260,7 @@ ConversionToBinaryResult<PREC> IntermediateFloat<PREC>::ToBinary(
     if ((!isNegative && rounding == RoundUp) ||
         (isNegative && rounding == RoundDown)) {
       // round to minimum nonzero value
+      expo = 0;
     } else {
       return {Binary{}, static_cast<enum ConversionResultFlags>(flags)};
     }
diff --git a/flang/unittests/Runtime/NumericalFormatTest.cpp b/flang/unittests/Runtime/NumericalFormatTest.cpp
index b5b8eb05943732..54c3174967e072 100644
--- a/flang/unittests/Runtime/NumericalFormatTest.cpp
+++ b/flang/unittests/Runtime/NumericalFormatTest.cpp
@@ -881,6 +881,10 @@ TEST(IOApiTests, EditDoubleInputValues) {
       {"(EX22.0)", "0X.FFFFFFFFFFFFF8P1024", 0x7fefffffffffffff}, // max finite
       {"(EX22.0)", "0X.8P1025             ", 0x7ff0000000000000}, // +Inf
       {"(EX22.0)", "-0X.8P1025            ", 0xfff0000000000000}, // -Inf
+      {"(E9.1)", " 1.0E-325", 0x0},
+      {"(RU,E9.1)", " 1.0E-325", 0x1},
+      {"(E9.1)", "-1.0E-325", 0x0},
+      {"(RD,E9.1)", "-1.0E-325", 0x8000000000000001},
   };
   for (auto const &[format, data, want] : testCases) {
     auto cookie{IONAME(BeginInternalFormattedInput)(



More information about the flang-commits mailing list