[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
Tue Dec 26 15:04:51 PST 2023


https://github.com/klausler updated https://github.com/llvm/llvm-project/pull/75878

>From 145640fae961346097f78d9c28549363384f4626 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 780979f747f5ba..8a48ed0f261670 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(
       if ((!isNegative && rounding == RoundUp) ||
           (isNegative && rounding == RoundDown)) {
         // round to minimum nonzero value
+        expo = 0;
       } else { // round to zero
         if (guard != 0) {
           flags |= Underflow;
diff --git a/flang/unittests/Runtime/NumericalFormatTest.cpp b/flang/unittests/Runtime/NumericalFormatTest.cpp
index 69637d8c6cb4cf..8eb67f31280ab3 100644
--- a/flang/unittests/Runtime/NumericalFormatTest.cpp
+++ b/flang/unittests/Runtime/NumericalFormatTest.cpp
@@ -897,6 +897,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