[flang-commits] [flang] [flang] Fix folding of NEAREST(TINY(1.), -1.) (PR #76590)

via flang-commits flang-commits at lists.llvm.org
Fri Dec 29 14:18:28 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

<details>
<summary>Changes</summary>

The code to fold NEAREST would return a value that's too large when transitioning from a normal number to a subnormal.

Fixes llvm-test-suite/Fortran/gfortran/regression/nearest_1.f90.

---
Full diff: https://github.com/llvm/llvm-project/pull/76590.diff


2 Files Affected:

- (modified) flang/lib/Evaluate/real.cpp (+1-1) 
- (modified) flang/test/Evaluate/fold-nearest.f90 (+2) 


``````````diff
diff --git a/flang/lib/Evaluate/real.cpp b/flang/lib/Evaluate/real.cpp
index 4fecaa1a131f81..cb8be98bb6f40f 100644
--- a/flang/lib/Evaluate/real.cpp
+++ b/flang/lib/Evaluate/real.cpp
@@ -350,7 +350,7 @@ ValueWithRealFlags<Real<W, P>> Real<W, P>::NEAREST(bool upward) const {
         isNegative = !isNegative;
       } else {
         auto sub1{fraction.SubtractSigned(one)};
-        if (sub1.overflow) {
+        if (sub1.overflow && expo > 1) {
           nearest = Fraction{0}.NOT();
           --expo;
         } else {
diff --git a/flang/test/Evaluate/fold-nearest.f90 b/flang/test/Evaluate/fold-nearest.f90
index 99af303128411c..bd8b020c392ac3 100644
--- a/flang/test/Evaluate/fold-nearest.f90
+++ b/flang/test/Evaluate/fold-nearest.f90
@@ -26,6 +26,8 @@ module m1
   logical, parameter :: test_14 = nearest(0., negZero) == -minSubnormal
   !WARN: warning: NEAREST: S argument is zero
   logical, parameter :: test_15 = nearest(negZero, 0.) == minSubnormal
+  logical, parameter :: test_16 = nearest(tiny(1.),-1.) == 1.1754942E-38
+  logical, parameter :: test_17 = nearest(tiny(1.),1.) == 1.1754945E-38
 end module
 
 module m2

``````````

</details>


https://github.com/llvm/llvm-project/pull/76590


More information about the flang-commits mailing list