[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:52:20 PST 2023


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

>From 782c255823f4883bcb30904f4435de640dd9cbf1 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 05a5f038353e0b..d38af0f9b80050 100644
--- a/flang/lib/Decimal/decimal-to-binary.cpp
+++ b/flang/lib/Decimal/decimal-to-binary.cpp
@@ -270,6 +270,7 @@ ConversionToBinaryResult<PREC> IntermediateFloat<PREC>::ToBinary(
       if ((!isNegative && rounding == RoundUp) ||
           (isNegative && rounding == RoundDown)) {
         // round to least 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 bf954a84444ac3..0f261a9f64a15e 100644
--- a/flang/unittests/Runtime/NumericalFormatTest.cpp
+++ b/flang/unittests/Runtime/NumericalFormatTest.cpp
@@ -914,6 +914,10 @@ TEST(IOApiTests, EditDoubleInputValues) {
       {"(RZ,F7.0)", "-1.e999", 0xffefffffffffffff, 0}, // -HUGE()
       {"(RD,F7.0)", "-1.e999", 0xfff0000000000000, ovf}, // -Inf
       {"(RU,F7.0)", "-1.e999", 0xffefffffffffffff, 0}, // -HUGE()
+      {"(E9.1)", " 1.0E-325", 0x0, 0},
+      {"(RU,E9.1)", " 1.0E-325", 0x1,0 },
+      {"(E9.1)", "-1.0E-325", 0x0, 0},
+      {"(RD,E9.1)", "-1.0E-325", 0x8000000000000001, 0},
   };
   for (auto const &[format, data, want, iostat] : testCases) {
     auto cookie{IONAME(BeginInternalFormattedInput)(



More information about the flang-commits mailing list