* [PATCH] Fortran: simplification of NEAREST for large argument [PR109186]
@ 2023-03-19 20:44 Harald Anlauf
0 siblings, 0 replies; only message in thread
From: Harald Anlauf @ 2023-03-19 20:44 UTC (permalink / raw)
To: fortran, gcc-patches
[-- Attachment #1: Type: text/plain, Size: 747 bytes --]
Dear all,
I intend to commit the attached obvious patch within 24h unless there
are comments.
The issue is an off-by-one error in setting up the maximum exponent
of the real kind that is passed to mpfr, so that model numbers between
huge(x)/2 and huge(x), when given as an argument to NEAREST(arg,-1.0),
are rounded down to huge(x)/2 during compile-time simplification.
As no such issue is observed at run-time, the testcase compares the
compile-time and run-time results for corner cases.
Regtested on x86_64-pc-linux-gnu.
As this is sort of a wrong-code issue, I intend to backport to all
open branches.
(The issue was apparently introduced in r0-84566-gb6f63e898498e6
without noticing, so it is technically a regression.)
Thanks,
Harald
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pr109186.diff --]
[-- Type: text/x-patch, Size: 2493 bytes --]
From 9391bd0eeef8e069d9e49f9aa277160b43aaf4f3 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Sun, 19 Mar 2023 21:29:46 +0100
Subject: [PATCH] Fortran: simplification of NEAREST for large argument
[PR109186]
gcc/fortran/ChangeLog:
PR fortran/109186
* simplify.cc (gfc_simplify_nearest): Fix off-by-one error in setting
up real kind-specific maximum exponent for mpfr.
gcc/testsuite/ChangeLog:
PR fortran/109186
* gfortran.dg/nearest_6.f90: New test.
---
gcc/fortran/simplify.cc | 2 +-
gcc/testsuite/gfortran.dg/nearest_6.f90 | 26 +++++++++++++++++++++++++
2 files changed, 27 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/gfortran.dg/nearest_6.f90
diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index 20ea38e0007..ecf0e3558df 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -6114,7 +6114,7 @@ gfc_simplify_nearest (gfc_expr *x, gfc_expr *s)
kind = gfc_validate_kind (BT_REAL, x->ts.kind, 0);
mpfr_set_emin ((mpfr_exp_t) gfc_real_kinds[kind].min_exponent -
mpfr_get_prec(result->value.real) + 1);
- mpfr_set_emax ((mpfr_exp_t) gfc_real_kinds[kind].max_exponent - 1);
+ mpfr_set_emax ((mpfr_exp_t) gfc_real_kinds[kind].max_exponent);
mpfr_check_range (result->value.real, 0, MPFR_RNDU);
if (mpfr_sgn (s->value.real) > 0)
diff --git a/gcc/testsuite/gfortran.dg/nearest_6.f90 b/gcc/testsuite/gfortran.dg/nearest_6.f90
new file mode 100644
index 00000000000..00d1ebe618c
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/nearest_6.f90
@@ -0,0 +1,26 @@
+! { dg-do run }
+! PR fortran/109186 - Verify that NEAREST produces same results at
+! compile-time and run-time for corner cases
+! Reported by John Harper
+
+program p
+ implicit none
+ integer, parameter :: sp = selected_real_kind (6)
+ integer, parameter :: dp = selected_real_kind (13)
+ real(sp), parameter :: x1 = huge (1._sp), t1 = tiny (1._sp)
+ real(dp), parameter :: x2 = huge (1._dp), t2 = tiny (1._dp)
+ real(sp), volatile :: y1, z1
+ real(dp), volatile :: y2, z2
+ y1 = x1
+ z1 = nearest (y1, -1._sp)
+ if (nearest (x1, -1._sp) /= z1) stop 1
+ y2 = x2
+ z2 = nearest (y2, -1._dp)
+ if (nearest (x2, -1._dp) /= z2) stop 2
+ y1 = t1
+ z1 = nearest (y1, 1._sp)
+ if (nearest (t1, 1._sp) /= z1) stop 3
+ y2 = t2
+ z2 = nearest (y2, 1._dp)
+ if (nearest (t2, 1._dp) /= z2) stop 4
+end
--
2.35.3
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-03-19 20:44 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-19 20:44 [PATCH] Fortran: simplification of NEAREST for large argument [PR109186] Harald Anlauf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).