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

            Bug ID: 118580
           Summary: Incorrect complex (sp) - real (dp) operation within
                    maxval
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: federico.perini at gmail dot com
  Target Milestone: ---

Sample program: 

```
program complex_eye
  implicit none
  integer, parameter :: dp = kind(0.d0)
  integer, parameter :: sp = kind(0.0)
  integer, parameter :: k = 2
  integer :: i,j
  complex(sp), dimension(k,k) :: A, B
  real(dp) :: eye(k, k), mx, mxB

  do j = 1,k; do i=1, k
    eye(i,j) = merge(1,0,i==j)
      A(i,j) = merge(1,0,i==j)
  enddo

  B   = A - eye
  mx  = maxval(abs(A-eye))
  mxB = maxval(abs(B))

  if (mx>1.0e-6) then 
     write(*, *) "maxval(abs(A - eye)) = ", mx ! Should be 0, it is 1.0
     write(*, *) "maxval(abs(B))       = ", mxB
     stop 1
  else
     stop 0
  endif    

end program complex_eye
```

when `maxval` is run on a temporary expression, it returns a wrong value (often
1.0, but sometimes junk values). when the operation is saved in variable B
first, it returns the correct value.

In Compiler explorer, see https://godbolt.org/z/E6dWcEc9s : 
- fails on all versions of gfortran from 4.9.4 to 15.0
- fails with any optimization levels, also -O0

We found this issue after an update to the Fortran Standard Library, but the
issue seems not related to the library itself. 

Thank you,
Federico

Reply via email to