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

            Bug ID: 127845
           Summary: FINDLOC with DIM and MASK returns wrong results
           Product: gcc
           Version: 16.2.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: ---

findloc(x, value, dim=d, mask=m) misses matching values, and can report false
ones, once the result has more than one line in the second dimension. 


In the test case, element (2,3) of the result is 0, but x(2,3,2) = 3.0 with
m(2,3,2) = .true., so it should be 2. The call goes to _gfortran_mfindloc1_r8
(seen with -S). It is possible that the mask of the wrong line is used
(mfindloc1_* never advances the mask on carry).

Test program:

program findloc_dim_mask
    implicit none
    real(8) :: x(2,3,2)
    logical :: m(2,3,2)
    integer :: i, j, k, l
    l = 0
    do k = 1, 2; do j = 1, 3; do i = 1, 2
        x(i,j,k) = real(mod(l*7,5)+1, 8); m(i,j,k) = mod(l,3) /= 1; l = l + 1
    end do; end do; end do
    do k = 1, 2
        print '(a,i0,a,i0,a)', 'x(:,:,', k, ')          m(:,:,', k, ')'
        do i = 1, 2
            print '(3f4.1,6x,3l2)', x(i,:,k), m(i,:,k)
        end do
    end do
    print '(a)', 'findloc(x, 3.0d0, dim=3, mask=m), a 2x3 array, got:'
    block
        integer :: r(2,3)
        r = findloc(x, 3.0d0, dim=3, mask=m)
        do i = 1, 2
            print '(4x,3i2)', r(i,:)
        end do
    end block
    print '(a)', '  expected:'
    print '(4x,3i2)', 2, 0, 0
    print '(4x,3i2)', 0, 0, 2
    print '(a)', '  (x(2,3,2) = 3.0 and m(2,3,2) = T, so element (2,3) is 2)'
end program findloc_dim_mask

It seems like all versions of gfortran have this issue (ifx and flang report
the expected result, check on Compiler Explorer at
https://godbolt.org/z/cf5M8va7n). 

Thank you!
Federico

Reply via email to