https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110311

--- Comment #24 from Jürgen Reuter <juergen.reuter at desy dot de> ---
Here is a first reproducer without the need for OCaml, unfortunately a bit too
big to be uploaded, here is the link:
https://www.desy.de/~reuter/downloads/repro001.tar.xz
the tarball contains Fortran files that compile to two binaries, ./whizard and
./whizard_check.
After compilation, perform ./whizard r1.sin 
to run the program. There will be NaNs generated in our RNG stream random
number generator. They originate from an erroneous optimization by the
gcc/gfortran tree-optimizer. This code resides in rng_stream_sub.f90, in the
function mult_mod. Eliminating the intrinsic function mod and explicitly doing
the calculation makes the problem go away.
  function mult_mod (a, b, c, m) result (v)
    real(default), intent(in) :: a
    real(default), intent(in) :: b
    real(default), intent(in) :: c
    real(default), intent(in) :: m
    real(default) :: v
    integer :: a1
    real(default) :: a2
    v = a * b + c
    if (v >= two53 .or. v <= -two53) then
       a1 = int (a / two17)
       a2 = a - a1 * two17
       v = mmm_mod (a1 * b, m)
       v = v * two17 + a2 * b + c
    end if
    v = mmm_mod (v, m)
    if (v < 0.0_default) v = v + m
  contains
    elemental function mmm_mod (x1, x2) result (res)
      real(default), intent(in) :: x1, x2
      real(default) :: res
      res = x1 - int(x1/x2) * x2
    end function mmm_mod
  end function mult_mod

Reply via email to