The following program fails to compile: program exponent print *, set_exponent(1., -1) end program exponent With the following error message: exponent.f08:2:24: 2 | print *, set_exponent(1., -1) | 1 Error: Result of SET_EXPONENT overflows its kind at (1) But as I understand it, this is a valid program. Note that the problem does not occur if the exponent is positive. It also does not occur if the exponent is variable (even with a negative value). So it looks like the static analyzer is overly restrictive.
Confirmed. The simplification seems to use the old mpfr_mul_2exp which takes an unsigned exponent. There is the newer mpfr_mul_2si which accepts a signed exponent.
It's even worse, as simplification for arguments X below one gives wrong results: set_exponent(0.75, 3) gives 3.00000000, while the runtime version correctly prints 6.000000 All gfortran versions seem to be affected.
Created attachment 54860 [details] Patch Patch I am testing.
Submitted, with an additional fix for negative X: https://gcc.gnu.org/pipermail/fortran/2023-April/059182.html
Thanks for your work on this issue!
The master branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>: https://gcc.gnu.org/g:fa4cb42870df60deb8888dbd51e2ddc6d6ab9e6a commit r13-7188-gfa4cb42870df60deb8888dbd51e2ddc6d6ab9e6a Author: Harald Anlauf <anlauf@gmx.de> Date: Fri Apr 14 20:45:19 2023 +0200 Fortran: fix compile-time simplification of SET_EXPONENT [PR109511] gcc/fortran/ChangeLog: PR fortran/109511 * simplify.cc (gfc_simplify_set_exponent): Fix implementation of compile-time simplification of intrinsic SET_EXPONENT for argument X < 1 and for I < 0. gcc/testsuite/ChangeLog: PR fortran/109511 * gfortran.dg/set_exponent_1.f90: New test.
The releases/gcc-12 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>: https://gcc.gnu.org/g:64efeac03cb9fa2603bb6628712f3a2541aef1cb commit r12-9402-g64efeac03cb9fa2603bb6628712f3a2541aef1cb Author: Harald Anlauf <anlauf@gmx.de> Date: Fri Apr 14 20:45:19 2023 +0200 Fortran: fix compile-time simplification of SET_EXPONENT [PR109511] gcc/fortran/ChangeLog: PR fortran/109511 * simplify.cc (gfc_simplify_set_exponent): Fix implementation of compile-time simplification of intrinsic SET_EXPONENT for argument X < 1 and for I < 0. gcc/testsuite/ChangeLog: PR fortran/109511 * gfortran.dg/set_exponent_1.f90: New test. (cherry picked from commit fa4cb42870df60deb8888dbd51e2ddc6d6ab9e6a)
The releases/gcc-11 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>: https://gcc.gnu.org/g:dfcdb457c462b9e35eb54d3b758221a119067e24 commit r11-10625-gdfcdb457c462b9e35eb54d3b758221a119067e24 Author: Harald Anlauf <anlauf@gmx.de> Date: Fri Apr 14 20:45:19 2023 +0200 Fortran: fix compile-time simplification of SET_EXPONENT [PR109511] gcc/fortran/ChangeLog: PR fortran/109511 * simplify.c (gfc_simplify_set_exponent): Fix implementation of compile-time simplification of intrinsic SET_EXPONENT for argument X < 1 and for I < 0. gcc/testsuite/ChangeLog: PR fortran/109511 * gfortran.dg/set_exponent_1.f90: New test. (cherry picked from commit fa4cb42870df60deb8888dbd51e2ddc6d6ab9e6a)
The releases/gcc-10 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>: https://gcc.gnu.org/g:dd2cd1e70509aaeb3db3e7b322d8357bac817632 commit r10-11289-gdd2cd1e70509aaeb3db3e7b322d8357bac817632 Author: Harald Anlauf <anlauf@gmx.de> Date: Fri Apr 14 20:45:19 2023 +0200 Fortran: fix compile-time simplification of SET_EXPONENT [PR109511] gcc/fortran/ChangeLog: PR fortran/109511 * simplify.c (gfc_simplify_set_exponent): Fix implementation of compile-time simplification of intrinsic SET_EXPONENT for argument X < 1 and for I < 0. gcc/testsuite/ChangeLog: PR fortran/109511 * gfortran.dg/set_exponent_1.f90: New test. (cherry picked from commit fa4cb42870df60deb8888dbd51e2ddc6d6ab9e6a)
Fixed on mainline for gcc-13, and on all open branches. Closing. Thanks for the report!