Bug 109511 - incorrectly rejects set_exponent with constant negative exponent
Summary: incorrectly rejects set_exponent with constant negative exponent
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 12.2.0
: P3 normal
Target Milestone: 10.5
Assignee: anlauf
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2023-04-14 09:52 UTC by Sébastien Villemot
Modified: 2023-04-16 19:02 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2023-04-14 00:00:00


Attachments
Patch (525 bytes, patch)
2023-04-14 17:36 UTC, anlauf
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Sébastien Villemot 2023-04-14 09:52:45 UTC
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.
Comment 1 anlauf 2023-04-14 11:54:09 UTC
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.
Comment 2 anlauf 2023-04-14 17:19:00 UTC
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.
Comment 3 anlauf 2023-04-14 17:36:27 UTC
Created attachment 54860 [details]
Patch

Patch I am testing.
Comment 4 anlauf 2023-04-14 19:01:17 UTC
Submitted, with an additional fix for negative X:

https://gcc.gnu.org/pipermail/fortran/2023-April/059182.html
Comment 5 Sébastien Villemot 2023-04-14 19:23:01 UTC
Thanks for your work on this issue!
Comment 6 GCC Commits 2023-04-14 19:51:43 UTC
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.
Comment 7 GCC Commits 2023-04-15 13:44:42 UTC
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)
Comment 8 GCC Commits 2023-04-16 18:52:52 UTC
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)
Comment 9 GCC Commits 2023-04-16 19:01:12 UTC
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)
Comment 10 anlauf 2023-04-16 19:02:31 UTC
Fixed on mainline for gcc-13, and on all open branches.  Closing.

Thanks for the report!