[flang-commits] [flang] [flang][runtime] Underflow real input to -0. when negative (PR #82443)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Tue Feb 20 16:21:27 PST 2024


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/82443

When the result of real input editing underflows to zero, return -0. when the input field had a minus sign.

>From 04ee6664c1496407a9ab7f127119cd70d76b4cdf Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Tue, 20 Feb 2024 16:19:27 -0800
Subject: [PATCH] [flang][runtime] Underflow real input to -0. when negative

When the result of real input editing underflows to zero, return
-0. when the input field had a minus sign.
---
 flang/lib/Decimal/decimal-to-binary.cpp         | 8 +++++++-
 flang/unittests/Runtime/NumericalFormatTest.cpp | 2 +-
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/flang/lib/Decimal/decimal-to-binary.cpp b/flang/lib/Decimal/decimal-to-binary.cpp
index d38af0f9b80050..c5cdb72e355f62 100644
--- a/flang/lib/Decimal/decimal-to-binary.cpp
+++ b/flang/lib/Decimal/decimal-to-binary.cpp
@@ -14,6 +14,7 @@
 #include <cinttypes>
 #include <cstring>
 #include <ctype.h>
+#include <utility>
 
 namespace Fortran::decimal {
 
@@ -275,7 +276,12 @@ ConversionToBinaryResult<PREC> IntermediateFloat<PREC>::ToBinary(
         if (guard != 0) {
           flags |= Underflow;
         }
-        return {Binary{}, static_cast<enum ConversionResultFlags>(flags)};
+        Binary zero;
+        if (isNegative) {
+          zero.Negate();
+        }
+        return {
+            std::move(zero), static_cast<enum ConversionResultFlags>(flags)};
       }
     }
   } else {
diff --git a/flang/unittests/Runtime/NumericalFormatTest.cpp b/flang/unittests/Runtime/NumericalFormatTest.cpp
index 03a2be3ca56df7..37eecd7708a1eb 100644
--- a/flang/unittests/Runtime/NumericalFormatTest.cpp
+++ b/flang/unittests/Runtime/NumericalFormatTest.cpp
@@ -916,7 +916,7 @@ TEST(IOApiTests, EditDoubleInputValues) {
       {"(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},
+      {"(E9.1)", "-1.0E-325", 0x8000000000000000, 0},
       {"(RD,E9.1)", "-1.0E-325", 0x8000000000000001, 0},
   };
   for (auto const &[format, data, want, iostat] : testCases) {



More information about the flang-commits mailing list