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

            Bug ID: 127846
           Summary: MINLOC/MAXLOC with MASK and BACK=.true. ignore the
                    mask
           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: ---

Possibly related to #127845 that I also just filed. 

minloc(y, mask=m, back=.true.) on a rank-2 array returns a position also where
the mask is .false. In the test case, row 2 of the mask is all .false., yet
minloc returns (2,4) and maxloc returns (2,3). The calls go to
_gfortran_mminloc0_4_r8 and _gfortran_mmaxloc0_4_r8, they do not seem to
advance the mask properly.

program minloc_mask_back
    implicit none
    real(8) :: y(3,4)
    logical :: m(3,4)
    integer :: i, j, l
    l = 0
    do j = 1, 4; do i = 1, 3
        y(i,j) = real(mod(l*7,5)+1, 8); m(i,j) = mod(l,3) /= 1; l = l + 1
    end do; end do
    print '(a)', 'y                    m'
    do i = 1, 3
        print '(4f4.1,6x,4l2)', y(i,:), m(i,:)
    end do
    print '(a,2i3,a)', 'minloc(y, mask=m, back=.true.) =', minloc(y, mask=m,
back=.true.), &
                       '   expected  3  2 (m(2,4) is F)'
    print '(a,2i3,a)', 'maxloc(y, mask=m, back=.true.) =', maxloc(y, mask=m,
back=.true.), &
                       '   expected  3  1 (m(2,3) is F)'
end program minloc_mask_back

Check on Compiler Explorer: https://godbolt.org/z/Mcf4n4bTT

The defect: In libgfortran/m4/minloc0.m4, the masked routine's back branch,
lines 124-135 on trunk, advances base but not mbase: changing to `mbase +=
mstride[0];` might fix this (untested).

Thank you!
Federico
Federico

Reply via email to