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

via flang-commits flang-commits at lists.llvm.org
Tue Dec 26 15:57:39 PST 2023


Author: Peter Klausler
Date: 2023-12-26T15:57:35-08:00
New Revision: f45723cded56b1e66b572fba0b71d117db6caa2e

URL: https://github.com/llvm/llvm-project/commit/f45723cded56b1e66b572fba0b71d117db6caa2e
DIFF: https://github.com/llvm/llvm-project/commit/f45723cded56b1e66b572fba0b71d117db6caa2e.diff

LOG: [flang][runtime] Fix RU/RD results when rounding to least nonzero (#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.

Added: 
    

Modified: 
    flang/lib/Decimal/decimal-to-binary.cpp
    flang/unittests/Runtime/NumericalFormatTest.cpp

Removed: 
    


################################################################################
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..9dd2771fe4a753 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