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

via flang-commits flang-commits at lists.llvm.org
Tue Jan 2 09:38:56 PST 2024


Author: Peter Klausler
Date: 2024-01-02T09:38:52-08:00
New Revision: 4c1f488b78237e3388ac44d587b7b2e0c1d772b9

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

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

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.

Added: 
    

Modified: 
    flang/lib/Evaluate/real.cpp
    flang/test/Evaluate/fold-nearest.f90

Removed: 
    


################################################################################
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


        


More information about the flang-commits mailing list