Skip to content

Commit 4c1f488

Browse files
authored
[flang] Fix folding of NEAREST(TINY(1.),-1.) (llvm#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.
1 parent 78348b6 commit 4c1f488

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

flang/lib/Evaluate/real.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ ValueWithRealFlags<Real<W, P>> Real<W, P>::NEAREST(bool upward) const {
350350
isNegative = !isNegative;
351351
} else {
352352
auto sub1{fraction.SubtractSigned(one)};
353-
if (sub1.overflow) {
353+
if (sub1.overflow && expo > 1) {
354354
nearest = Fraction{0}.NOT();
355355
--expo;
356356
} else {

flang/test/Evaluate/fold-nearest.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ module m1
2626
logical, parameter :: test_14 = nearest(0., negZero) == -minSubnormal
2727
!WARN: warning: NEAREST: S argument is zero
2828
logical, parameter :: test_15 = nearest(negZero, 0.) == minSubnormal
29+
logical, parameter :: test_16 = nearest(tiny(1.),-1.) == 1.1754942E-38
30+
logical, parameter :: test_17 = nearest(tiny(1.),1.) == 1.1754945E-38
2931
end module
3032

3133
module m2

0 commit comments

Comments
 (0)