| 1 |
! { dg-do run } |
| 2 |
! |
| 3 |
! PR fortran/90608 |
| 4 |
! Check that the evaluation of MAXLOC's BACK argument is made only once |
| 5 |
! before the scalarization loops, when the DIM argument is present. |
| 6 |
|
| 7 |
program p |
| 8 |
implicit none |
| 9 |
integer, parameter :: data60(*) = (/ 7, 4, 5, 3, 9, 0, 6, 4, 5, 5, & |
| 10 |
8, 2, 6, 7, 8, 7, 4, 5, 3, 9, & |
| 11 |
0, 6, 4, 5, 5, 8, 2, 6, 7, 8, & |
| 12 |
7, 4, 5, 3, 9, 0, 6, 4, 5, 5, & |
| 13 |
8, 2, 6, 7, 8, 7, 4, 5, 3, 9, & |
| 14 |
0, 6, 4, 5, 5, 8, 2, 6, 7, 8 /) |
| 15 |
logical, parameter :: mask60(*) = (/ .true. , .false., .false., .false., & |
| 16 |
.true. , .false., .true. , .false., & |
| 17 |
.false., .true. , .true. , .false., & |
| 18 |
.true. , .true. , .true. , .true. , & |
| 19 |
.false., .true. , .false., .true. , & |
| 20 |
.false., .true. , .false., .true. , & |
| 21 |
.true. , .false., .false., .true. , & |
| 22 |
.true. , .true. , .true. , .false., & |
| 23 |
.false., .false., .true. , .false., & |
| 24 |
.true. , .false., .true. , .true. , & |
| 25 |
.true. , .false., .true. , .true. , & |
| 26 |
.false., .true. , .false., .true. , & |
| 27 |
.false., .true. , .false., .false., & |
| 28 |
.false., .true. , .true. , .true. , & |
| 29 |
.false., .true. , .false., .true. /) |
| 30 |
integer :: calls_count = 0 |
| 31 |
call check_int_const_shape_rank_3 |
| 32 |
call check_int_const_shape_rank_3_scalar_mask |
| 33 |
call check_int_const_shape_rank_3_optional_mask_present |
| 34 |
call check_int_const_shape_rank_3_optional_mask_absent |
| 35 |
call check_int_const_shape_rank_3_array_mask |
| 36 |
call check_int_const_shape_empty_4 |
| 37 |
call check_int_alloc_rank_3 |
| 38 |
call check_int_alloc_rank_3_scalar_mask |
| 39 |
call check_int_alloc_rank_3_array_mask |
| 40 |
call check_int_alloc_empty_4 |
| 41 |
call check_real_const_shape_rank_3 |
| 42 |
call check_real_const_shape_rank_3_scalar_mask |
| 43 |
call check_real_const_shape_rank_3_optional_mask_present |
| 44 |
call check_real_const_shape_rank_3_optional_mask_absent |
| 45 |
call check_real_const_shape_rank_3_array_mask |
| 46 |
call check_real_const_shape_empty_4 |
| 47 |
call check_real_alloc_rank_3 |
| 48 |
call check_real_alloc_rank_3_scalar_mask |
| 49 |
call check_real_alloc_rank_3_array_mask |
| 50 |
call check_real_alloc_empty_4 |
| 51 |
contains |
| 52 |
function get_scalar_false() |
| 53 |
logical :: get_scalar_false |
| 54 |
calls_count = calls_count + 1 |
| 55 |
get_scalar_false = .false. |
| 56 |
end function |
| 57 |
subroutine check_int_const_shape_rank_3() |
| 58 |
integer :: a(3,4,5) |
| 59 |
integer, allocatable :: r(:,:) |
| 60 |
a = reshape(data60, shape(a)) |
| 61 |
calls_count = 0 |
| 62 |
r = maxloc(a, dim = 1, back = get_scalar_false()) |
| 63 |
if (calls_count /= 1) error stop 12 |
| 64 |
r = maxloc(a, dim = 2, back = get_scalar_false()) |
| 65 |
if (calls_count /= 2) error stop 15 |
| 66 |
r = maxloc(a, dim = 3, back = get_scalar_false()) |
| 67 |
if (calls_count /= 3) error stop 18 |
| 68 |
end subroutine |
| 69 |
subroutine check_int_const_shape_rank_3_scalar_mask() |
| 70 |
integer :: a(3,4,5) |
| 71 |
integer, allocatable :: r(:,:) |
| 72 |
a = reshape(data60, shape(a)) |
| 73 |
calls_count = 0 |
| 74 |
! We only check the case of a .true. mask. |
| 75 |
! If the mask is .false., the back argument is not necessary to deduce |
| 76 |
! the value returned by maxloc, so the compiler is free to elide it, |
| 77 |
! and the value of calls_count is undefined in that case. |
| 78 |
r = maxloc(a, dim = 1, mask = .true., back = get_scalar_false()) |
| 79 |
if (calls_count /= 1) error stop 22 |
| 80 |
r = maxloc(a, dim = 2, mask = .true., back = get_scalar_false()) |
| 81 |
if (calls_count /= 2) error stop 25 |
| 82 |
r = maxloc(a, dim = 3, mask = .true., back = get_scalar_false()) |
| 83 |
if (calls_count /= 3) error stop 28 |
| 84 |
end subroutine |
| 85 |
subroutine call_maxloc_int(r, a, m, b) |
| 86 |
integer :: a(:,:,:) |
| 87 |
logical, optional :: m(:,:,:) |
| 88 |
logical, optional :: b |
| 89 |
integer, allocatable :: r(:,:) |
| 90 |
r = maxloc(a, dim = 2, mask = m, back = b) |
| 91 |
end subroutine |
| 92 |
subroutine check_int_const_shape_rank_3_optional_mask_present() |
| 93 |
integer :: a(3,4,5) |
| 94 |
logical :: m(3,4,5) |
| 95 |
integer, allocatable :: r(:,:) |
| 96 |
a = reshape(data60, shape(a)) |
| 97 |
m = reshape(mask60, shape(m)) |
| 98 |
calls_count = 0 |
| 99 |
call call_maxloc_int(r, a, m, get_scalar_false()) |
| 100 |
if (calls_count /= 1) error stop 45 |
| 101 |
end subroutine |
| 102 |
subroutine check_int_const_shape_rank_3_optional_mask_absent() |
| 103 |
integer :: a(3,4,5) |
| 104 |
integer, allocatable :: r(:,:) |
| 105 |
a = reshape(data60, shape(a)) |
| 106 |
calls_count = 0 |
| 107 |
call call_maxloc_int(r, a, b = get_scalar_false()) |
| 108 |
if (calls_count /= 1) error stop 55 |
| 109 |
end subroutine |
| 110 |
subroutine check_int_const_shape_rank_3_array_mask() |
| 111 |
integer :: a(3,4,5) |
| 112 |
logical :: m(3,4,5) |
| 113 |
integer, allocatable :: r(:,:) |
| 114 |
a = reshape(data60, shape(a)) |
| 115 |
m = reshape(mask60, shape(m)) |
| 116 |
calls_count = 0 |
| 117 |
r = maxloc(a, dim = 1, mask = m, back = get_scalar_false()) |
| 118 |
if (calls_count /= 1) error stop 62 |
| 119 |
r = maxloc(a, dim = 2, mask = m, back = get_scalar_false()) |
| 120 |
if (calls_count /= 2) error stop 65 |
| 121 |
r = maxloc(a, dim = 3, mask = m, back = get_scalar_false()) |
| 122 |
if (calls_count /= 3) error stop 68 |
| 123 |
end subroutine |
| 124 |
subroutine check_int_const_shape_empty_4() |
| 125 |
integer :: a(9,3,0,7) |
| 126 |
logical :: m(9,3,0,7) |
| 127 |
integer, allocatable :: r(:,:,:) |
| 128 |
a = reshape((/ integer:: /), shape(a)) |
| 129 |
m = reshape((/ logical:: /), shape(m)) |
| 130 |
calls_count = 0 |
| 131 |
r = maxloc(a, dim = 1, mask = m, back = get_scalar_false()) |
| 132 |
if (calls_count /= 1) error stop 72 |
| 133 |
r = maxloc(a, dim = 2, mask = m, back = get_scalar_false()) |
| 134 |
if (calls_count /= 2) error stop 74 |
| 135 |
r = maxloc(a, dim = 3, mask = m, back = get_scalar_false()) |
| 136 |
if (calls_count /= 3) error stop 76 |
| 137 |
r = maxloc(a, dim = 4, mask = m, back = get_scalar_false()) |
| 138 |
if (calls_count /= 4) error stop 78 |
| 139 |
end subroutine |
| 140 |
subroutine check_int_alloc_rank_3() |
| 141 |
integer, allocatable :: a(:,:,:) |
| 142 |
integer, allocatable :: r(:,:) |
| 143 |
allocate(a(3,4,5)) |
| 144 |
a(:,:,:) = reshape(data60, shape(a)) |
| 145 |
calls_count = 0 |
| 146 |
r = maxloc(a, dim = 1, back = get_scalar_false()) |
| 147 |
if (calls_count /= 1) error stop 82 |
| 148 |
r = maxloc(a, dim = 2, back = get_scalar_false()) |
| 149 |
if (calls_count /= 2) error stop 85 |
| 150 |
r = maxloc(a, dim = 3, back = get_scalar_false()) |
| 151 |
if (calls_count /= 3) error stop 88 |
| 152 |
end subroutine |
| 153 |
subroutine check_int_alloc_rank_3_scalar_mask() |
| 154 |
integer, allocatable :: a(:,:,:) |
| 155 |
integer, allocatable :: r(:,:) |
| 156 |
allocate(a(3,4,5)) |
| 157 |
a(:,:,:) = reshape(data60, shape(a)) |
| 158 |
calls_count = 0 |
| 159 |
! We only check the case of a .true. mask. |
| 160 |
! If the mask is .false., the back argument is not necessary to deduce |
| 161 |
! the value returned by maxloc, so the compiler is free to elide it, |
| 162 |
! and the value of calls_count is undefined in that case. |
| 163 |
r = maxloc(a, dim = 1, mask = .true., back = get_scalar_false()) |
| 164 |
if (calls_count /= 1) error stop 92 |
| 165 |
r = maxloc(a, dim = 2, mask = .true., back = get_scalar_false()) |
| 166 |
if (calls_count /= 2) error stop 95 |
| 167 |
r = maxloc(a, dim = 3, mask = .true., back = get_scalar_false()) |
| 168 |
if (calls_count /= 3) error stop 98 |
| 169 |
end subroutine |
| 170 |
subroutine check_int_alloc_rank_3_array_mask() |
| 171 |
integer, allocatable :: a(:,:,:) |
| 172 |
logical, allocatable :: m(:,:,:) |
| 173 |
integer, allocatable :: r(:,:) |
| 174 |
allocate(a(3,4,5), m(3,4,5)) |
| 175 |
a(:,:,:) = reshape(data60, shape(a)) |
| 176 |
m(:,:,:) = reshape(mask60, shape(m)) |
| 177 |
calls_count = 0 |
| 178 |
r = maxloc(a, dim = 1, mask = m, back = get_scalar_false()) |
| 179 |
if (calls_count /= 1) error stop 102 |
| 180 |
r = maxloc(a, dim = 2, mask = m, back = get_scalar_false()) |
| 181 |
if (calls_count /= 2) error stop 105 |
| 182 |
r = maxloc(a, dim = 3, mask = m, back = get_scalar_false()) |
| 183 |
if (calls_count /= 3) error stop 108 |
| 184 |
end subroutine |
| 185 |
subroutine check_int_alloc_empty_4() |
| 186 |
integer, allocatable :: a(:,:,:,:) |
| 187 |
logical, allocatable :: m(:,:,:,:) |
| 188 |
integer, allocatable :: r(:,:,:) |
| 189 |
allocate(a(9,3,0,7), m(9,3,0,7)) |
| 190 |
a(:,:,:,:) = reshape((/ integer:: /), shape(a)) |
| 191 |
m(:,:,:,:) = reshape((/ logical:: /), shape(m)) |
| 192 |
calls_count = 0 |
| 193 |
r = maxloc(a, dim = 1, mask = m, back = get_scalar_false()) |
| 194 |
if (calls_count /= 1) error stop 112 |
| 195 |
r = maxloc(a, dim = 2, mask = m, back = get_scalar_false()) |
| 196 |
if (calls_count /= 2) error stop 114 |
| 197 |
r = maxloc(a, dim = 3, mask = m, back = get_scalar_false()) |
| 198 |
if (calls_count /= 3) error stop 116 |
| 199 |
r = maxloc(a, dim = 4, mask = m, back = get_scalar_false()) |
| 200 |
if (calls_count /= 4) error stop 118 |
| 201 |
end subroutine |
| 202 |
subroutine check_real_const_shape_rank_3() |
| 203 |
real :: a(3,4,5) |
| 204 |
logical :: m(3,4,5) |
| 205 |
integer, allocatable :: r(:,:) |
| 206 |
a = reshape((/ real:: data60 /), shape(a)) |
| 207 |
m = reshape(mask60, shape(m)) |
| 208 |
calls_count = 0 |
| 209 |
r = maxloc(a, dim = 1, back = get_scalar_false()) |
| 210 |
if (calls_count /= 1) error stop 122 |
| 211 |
r = maxloc(a, dim = 2, back = get_scalar_false()) |
| 212 |
if (calls_count /= 2) error stop 125 |
| 213 |
r = maxloc(a, dim = 3, back = get_scalar_false()) |
| 214 |
if (calls_count /= 3) error stop 128 |
| 215 |
end subroutine |
| 216 |
subroutine check_real_const_shape_rank_3_scalar_mask() |
| 217 |
real :: a(3,4,5) |
| 218 |
integer, allocatable :: r(:,:) |
| 219 |
a = reshape((/ real:: data60 /), shape(a)) |
| 220 |
calls_count = 0 |
| 221 |
! We only check the case of a .true. mask. |
| 222 |
! If the mask is .false., the back argument is not necessary to deduce |
| 223 |
! the value returned by maxloc, so the compiler is free to elide it, |
| 224 |
! and the value of calls_count is undefined in that case. |
| 225 |
r = maxloc(a, dim = 1, mask = .true., back = get_scalar_false()) |
| 226 |
if (calls_count /= 1) error stop 132 |
| 227 |
r = maxloc(a, dim = 2, mask = .true., back = get_scalar_false()) |
| 228 |
if (calls_count /= 2) error stop 135 |
| 229 |
r = maxloc(a, dim = 3, mask = .true., back = get_scalar_false()) |
| 230 |
if (calls_count /= 3) error stop 138 |
| 231 |
end subroutine |
| 232 |
subroutine check_real_const_shape_rank_3_array_mask() |
| 233 |
real :: a(3,4,5) |
| 234 |
logical :: m(3,4,5) |
| 235 |
integer, allocatable :: r(:,:) |
| 236 |
a = reshape((/ real:: data60 /), shape(a)) |
| 237 |
m = reshape(mask60, shape(m)) |
| 238 |
calls_count = 0 |
| 239 |
r = maxloc(a, dim = 1, mask = m, back = get_scalar_false()) |
| 240 |
if (calls_count /= 1) error stop 142 |
| 241 |
r = maxloc(a, dim = 2, mask = m, back = get_scalar_false()) |
| 242 |
if (calls_count /= 2) error stop 145 |
| 243 |
r = maxloc(a, dim = 3, mask = m, back = get_scalar_false()) |
| 244 |
if (calls_count /= 3) error stop 148 |
| 245 |
end subroutine |
| 246 |
subroutine call_maxloc_real(r, a, m, b) |
| 247 |
real :: a(:,:,:) |
| 248 |
logical, optional :: m(:,:,:) |
| 249 |
logical, optional :: b |
| 250 |
integer, allocatable :: r(:,:) |
| 251 |
r = maxloc(a, dim = 2, mask = m, back = b) |
| 252 |
end subroutine |
| 253 |
subroutine check_real_const_shape_rank_3_optional_mask_present() |
| 254 |
real :: a(3,4,5) |
| 255 |
logical :: m(3,4,5) |
| 256 |
integer, allocatable :: r(:,:) |
| 257 |
a = reshape((/ real:: data60 /), shape(a)) |
| 258 |
m = reshape(mask60, shape(m)) |
| 259 |
calls_count = 0 |
| 260 |
call call_maxloc_real(r, a, m, get_scalar_false()) |
| 261 |
if (calls_count /= 1) error stop 155 |
| 262 |
end subroutine |
| 263 |
subroutine check_real_const_shape_rank_3_optional_mask_absent() |
| 264 |
real :: a(3,4,5) |
| 265 |
integer, allocatable :: r(:,:) |
| 266 |
a = reshape((/ real:: data60 /), shape(a)) |
| 267 |
calls_count = 0 |
| 268 |
call call_maxloc_real(r, a, b = get_scalar_false()) |
| 269 |
if (calls_count /= 1) error stop 165 |
| 270 |
end subroutine |
| 271 |
subroutine check_real_const_shape_empty_4() |
| 272 |
real :: a(9,3,0,7) |
| 273 |
logical :: m(9,3,0,7) |
| 274 |
integer, allocatable :: r(:,:,:) |
| 275 |
a = reshape((/ real:: /), shape(a)) |
| 276 |
m = reshape((/ logical:: /), shape(m)) |
| 277 |
calls_count = 0 |
| 278 |
r = maxloc(a, dim = 1, mask = m, back = get_scalar_false()) |
| 279 |
if (calls_count /= 1) error stop 172 |
| 280 |
r = maxloc(a, dim = 2, mask = m, back = get_scalar_false()) |
| 281 |
if (calls_count /= 2) error stop 174 |
| 282 |
r = maxloc(a, dim = 3, mask = m, back = get_scalar_false()) |
| 283 |
if (calls_count /= 3) error stop 176 |
| 284 |
r = maxloc(a, dim = 4, mask = m, back = get_scalar_false()) |
| 285 |
if (calls_count /= 4) error stop 178 |
| 286 |
end subroutine |
| 287 |
subroutine check_real_alloc_rank_3() |
| 288 |
real, allocatable :: a(:,:,:) |
| 289 |
integer, allocatable :: r(:,:) |
| 290 |
allocate(a(3,4,5)) |
| 291 |
a(:,:,:) = reshape((/ real:: data60 /), shape(a)) |
| 292 |
calls_count = 0 |
| 293 |
r = maxloc(a, dim = 1, back = get_scalar_false()) |
| 294 |
if (calls_count /= 1) error stop 182 |
| 295 |
r = maxloc(a, dim = 2, back = get_scalar_false()) |
| 296 |
if (calls_count /= 2) error stop 185 |
| 297 |
r = maxloc(a, dim = 3, back = get_scalar_false()) |
| 298 |
if (calls_count /= 3) error stop 188 |
| 299 |
end subroutine |
| 300 |
subroutine check_real_alloc_rank_3_scalar_mask() |
| 301 |
real, allocatable :: a(:,:,:) |
| 302 |
integer, allocatable :: r(:,:) |
| 303 |
allocate(a(3,4,5)) |
| 304 |
a(:,:,:) = reshape((/ real:: data60 /), shape(a)) |
| 305 |
calls_count = 0 |
| 306 |
! We only check the case of a .true. mask. |
| 307 |
! If the mask is .false., the back argument is not necessary to deduce |
| 308 |
! the value returned by maxloc, so the compiler is free to elide it, |
| 309 |
! and the value of calls_count is undefined in that case. |
| 310 |
r = maxloc(a, dim = 1, mask = .true., back = get_scalar_false()) |
| 311 |
if (calls_count /= 1) error stop 192 |
| 312 |
r = maxloc(a, dim = 2, mask = .true., back = get_scalar_false()) |
| 313 |
if (calls_count /= 2) error stop 195 |
| 314 |
r = maxloc(a, dim = 3, mask = .true., back = get_scalar_false()) |
| 315 |
if (calls_count /= 3) error stop 198 |
| 316 |
end subroutine |
| 317 |
subroutine check_real_alloc_rank_3_array_mask() |
| 318 |
real, allocatable :: a(:,:,:) |
| 319 |
logical, allocatable :: m(:,:,:) |
| 320 |
integer, allocatable :: r(:,:) |
| 321 |
allocate(a(3,4,5), m(3,4,5)) |
| 322 |
a(:,:,:) = reshape((/ real:: data60 /), shape(a)) |
| 323 |
m(:,:,:) = reshape(mask60, shape(m)) |
| 324 |
calls_count = 0 |
| 325 |
r = maxloc(a, dim = 1, mask = m, back = get_scalar_false()) |
| 326 |
if (calls_count /= 1) error stop 202 |
| 327 |
r = maxloc(a, dim = 2, mask = m, back = get_scalar_false()) |
| 328 |
if (calls_count /= 2) error stop 205 |
| 329 |
r = maxloc(a, dim = 3, mask = m, back = get_scalar_false()) |
| 330 |
if (calls_count /= 3) error stop 208 |
| 331 |
end subroutine |
| 332 |
subroutine check_real_alloc_empty_4() |
| 333 |
real, allocatable :: a(:,:,:,:) |
| 334 |
logical, allocatable :: m(:,:,:,:) |
| 335 |
integer, allocatable :: r(:,:,:) |
| 336 |
allocate(a(9,3,0,7), m(9,3,0,7)) |
| 337 |
a(:,:,:,:) = reshape((/ real:: /), shape(a)) |
| 338 |
m(:,:,:,:) = reshape((/ logical :: /), shape(m)) |
| 339 |
calls_count = 0 |
| 340 |
r = maxloc(a, dim = 1, mask = m, back = get_scalar_false()) |
| 341 |
if (calls_count /= 1) error stop 212 |
| 342 |
r = maxloc(a, dim = 2, mask = m, back = get_scalar_false()) |
| 343 |
if (calls_count /= 2) error stop 214 |
| 344 |
r = maxloc(a, dim = 3, mask = m, back = get_scalar_false()) |
| 345 |
if (calls_count /= 3) error stop 216 |
| 346 |
r = maxloc(a, dim = 4, mask = m, back = get_scalar_false()) |
| 347 |
if (calls_count /= 4) error stop 218 |
| 348 |
end subroutine |
| 349 |
end program p |