]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/generated/minloc1_16_r4.c
libgfortran: Don't skip allocation if size is zero [PR112412]
[thirdparty/gcc.git] / libgfortran / generated / minloc1_16_r4.c
CommitLineData
644cb69f 1/* Implementation of the MINLOC intrinsic
83ffe9cd 2 Copyright (C) 2002-2023 Free Software Foundation, Inc.
644cb69f
FXC
3 Contributed by Paul Brook <paul@nowt.org>
4
0cd0559e 5This file is part of the GNU Fortran runtime library (libgfortran).
644cb69f
FXC
6
7Libgfortran is free software; you can redistribute it and/or
8modify it under the terms of the GNU General Public
9License as published by the Free Software Foundation; either
748086b7 10version 3 of the License, or (at your option) any later version.
644cb69f
FXC
11
12Libgfortran is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
748086b7
JJ
17Under Section 7 of GPL version 3, you are granted additional
18permissions described in the GCC Runtime Library Exception, version
193.1, as published by the Free Software Foundation.
20
21You should have received a copy of the GNU General Public License and
22a copy of the GCC Runtime Library Exception along with this program;
23see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24<http://www.gnu.org/licenses/>. */
644cb69f 25
36ae8a61 26#include "libgfortran.h"
64b1806b 27#include <assert.h>
644cb69f
FXC
28
29
30#if defined (HAVE_GFC_REAL_4) && defined (HAVE_GFC_INTEGER_16)
31
64b1806b
TK
32#define HAVE_BACK_ARG 1
33
644cb69f 34
64acfd99 35extern void minloc1_16_r4 (gfc_array_i16 * const restrict,
64b1806b 36 gfc_array_r4 * const restrict, const index_type * const restrict, GFC_LOGICAL_4 back);
644cb69f
FXC
37export_proto(minloc1_16_r4);
38
39void
64acfd99
JB
40minloc1_16_r4 (gfc_array_i16 * const restrict retarray,
41 gfc_array_r4 * const restrict array,
64b1806b 42 const index_type * const restrict pdim, GFC_LOGICAL_4 back)
644cb69f
FXC
43{
44 index_type count[GFC_MAX_DIMENSIONS];
45 index_type extent[GFC_MAX_DIMENSIONS];
46 index_type sstride[GFC_MAX_DIMENSIONS];
47 index_type dstride[GFC_MAX_DIMENSIONS];
64acfd99
JB
48 const GFC_REAL_4 * restrict base;
49 GFC_INTEGER_16 * restrict dest;
644cb69f
FXC
50 index_type rank;
51 index_type n;
52 index_type len;
53 index_type delta;
54 index_type dim;
da96f5ab 55 int continue_loop;
644cb69f
FXC
56
57 /* Make dim zero based to avoid confusion. */
644cb69f 58 rank = GFC_DESCRIPTOR_RANK (array) - 1;
cfdf6ff6
TK
59 dim = (*pdim) - 1;
60
61 if (unlikely (dim < 0 || dim > rank))
62 {
63 runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
64 "is %ld, should be between 1 and %ld",
65 (long int) dim + 1, (long int) rank + 1);
66 }
644cb69f 67
dfb55fdc 68 len = GFC_DESCRIPTOR_EXTENT(array,dim);
da96f5ab
TK
69 if (len < 0)
70 len = 0;
dfb55fdc 71 delta = GFC_DESCRIPTOR_STRIDE(array,dim);
644cb69f
FXC
72
73 for (n = 0; n < dim; n++)
74 {
dfb55fdc
TK
75 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
76 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
80ee04b9
TK
77
78 if (extent[n] < 0)
79 extent[n] = 0;
644cb69f
FXC
80 }
81 for (n = dim; n < rank; n++)
82 {
dfb55fdc
TK
83 sstride[n] = GFC_DESCRIPTOR_STRIDE(array, n + 1);
84 extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
80ee04b9
TK
85
86 if (extent[n] < 0)
87 extent[n] = 0;
644cb69f
FXC
88 }
89
21d1335b 90 if (retarray->base_addr == NULL)
644cb69f 91 {
dfb55fdc 92 size_t alloc_size, str;
80ee04b9 93
644cb69f 94 for (n = 0; n < rank; n++)
80927a56
JJ
95 {
96 if (n == 0)
dfb55fdc 97 str = 1;
80927a56
JJ
98 else
99 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
dfb55fdc
TK
100
101 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
102
80927a56 103 }
644cb69f 104
644cb69f 105 retarray->offset = 0;
ca708a2b 106 retarray->dtype.rank = rank;
80ee04b9 107
92e6f3a4 108 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
80ee04b9 109
92e6f3a4 110 retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_16));
80ee04b9
TK
111 if (alloc_size == 0)
112 {
113 /* Make sure we have a zero-sized array. */
dfb55fdc 114 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
80ee04b9 115 return;
dfb55fdc 116
80ee04b9 117 }
644cb69f
FXC
118 }
119 else
120 {
644cb69f 121 if (rank != GFC_DESCRIPTOR_RANK (retarray))
fd6590f8 122 runtime_error ("rank of return array incorrect in"
ccacefc7
TK
123 " MINLOC intrinsic: is %ld, should be %ld",
124 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
125 (long int) rank);
fd6590f8 126
9731c4a3 127 if (unlikely (compile_options.bounds_check))
16bff921
TK
128 bounds_ifunction_return ((array_t *) retarray, extent,
129 "return value", "MINLOC");
644cb69f
FXC
130 }
131
132 for (n = 0; n < rank; n++)
133 {
134 count[n] = 0;
dfb55fdc 135 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
644cb69f 136 if (extent[n] <= 0)
facc1285 137 return;
644cb69f
FXC
138 }
139
21d1335b
TB
140 base = array->base_addr;
141 dest = retarray->base_addr;
644cb69f 142
da96f5ab
TK
143 continue_loop = 1;
144 while (continue_loop)
644cb69f 145 {
64acfd99 146 const GFC_REAL_4 * restrict src;
644cb69f
FXC
147 GFC_INTEGER_16 result;
148 src = base;
149 {
150
80927a56
JJ
151 GFC_REAL_4 minval;
152#if defined (GFC_REAL_4_INFINITY)
153 minval = GFC_REAL_4_INFINITY;
154#else
155 minval = GFC_REAL_4_HUGE;
156#endif
157 result = 1;
158 if (len <= 0)
644cb69f
FXC
159 *dest = 0;
160 else
161 {
b573f931 162#if ! defined HAVE_BACK_ARG
644cb69f
FXC
163 for (n = 0; n < len; n++, src += delta)
164 {
b573f931 165#endif
644cb69f 166
80927a56 167#if defined (GFC_REAL_4_QUIET_NAN)
b573f931
TK
168 for (n = 0; n < len; n++, src += delta)
169 {
80927a56
JJ
170 if (*src <= minval)
171 {
172 minval = *src;
173 result = (GFC_INTEGER_16)n + 1;
174 break;
175 }
176 }
b573f931
TK
177#else
178 n = 0;
80927a56 179#endif
b573f931
TK
180 if (back)
181 for (; n < len; n++, src += delta)
182 {
183 if (unlikely (*src <= minval))
184 {
185 minval = *src;
186 result = (GFC_INTEGER_16)n + 1;
187 }
188 }
189 else
190 for (; n < len; n++, src += delta)
191 {
192 if (unlikely (*src < minval))
193 {
194 minval = *src;
195 result = (GFC_INTEGER_16) n + 1;
196 }
80927a56 197 }
0cd0559e 198
644cb69f
FXC
199 *dest = result;
200 }
201 }
202 /* Advance to the next element. */
203 count[0]++;
204 base += sstride[0];
205 dest += dstride[0];
206 n = 0;
207 while (count[n] == extent[n])
80927a56
JJ
208 {
209 /* When we get to the end of a dimension, reset it and increment
210 the next dimension. */
211 count[n] = 0;
212 /* We could precalculate these products, but this is a less
213 frequently used path so probably not worth it. */
214 base -= sstride[n] * extent[n];
215 dest -= dstride[n] * extent[n];
216 n++;
80dd631f 217 if (n >= rank)
80927a56 218 {
80dd631f 219 /* Break out of the loop. */
da96f5ab
TK
220 continue_loop = 0;
221 break;
80927a56
JJ
222 }
223 else
224 {
225 count[n]++;
226 base += sstride[n];
227 dest += dstride[n];
228 }
229 }
644cb69f
FXC
230 }
231}
232
233
64acfd99
JB
234extern void mminloc1_16_r4 (gfc_array_i16 * const restrict,
235 gfc_array_r4 * const restrict, const index_type * const restrict,
64b1806b 236 gfc_array_l1 * const restrict, GFC_LOGICAL_4 back);
644cb69f
FXC
237export_proto(mminloc1_16_r4);
238
239void
64acfd99
JB
240mminloc1_16_r4 (gfc_array_i16 * const restrict retarray,
241 gfc_array_r4 * const restrict array,
242 const index_type * const restrict pdim,
64b1806b 243 gfc_array_l1 * const restrict mask, GFC_LOGICAL_4 back)
644cb69f
FXC
244{
245 index_type count[GFC_MAX_DIMENSIONS];
246 index_type extent[GFC_MAX_DIMENSIONS];
247 index_type sstride[GFC_MAX_DIMENSIONS];
248 index_type dstride[GFC_MAX_DIMENSIONS];
249 index_type mstride[GFC_MAX_DIMENSIONS];
64acfd99
JB
250 GFC_INTEGER_16 * restrict dest;
251 const GFC_REAL_4 * restrict base;
28dc6b33 252 const GFC_LOGICAL_1 * restrict mbase;
cfdf6ff6
TK
253 index_type rank;
254 index_type dim;
644cb69f
FXC
255 index_type n;
256 index_type len;
257 index_type delta;
258 index_type mdelta;
28dc6b33 259 int mask_kind;
644cb69f 260
2ea47ee9
TK
261 if (mask == NULL)
262 {
263#ifdef HAVE_BACK_ARG
264 minloc1_16_r4 (retarray, array, pdim, back);
265#else
266 minloc1_16_r4 (retarray, array, pdim);
267#endif
268 return;
269 }
270
644cb69f
FXC
271 dim = (*pdim) - 1;
272 rank = GFC_DESCRIPTOR_RANK (array) - 1;
273
cfdf6ff6
TK
274
275 if (unlikely (dim < 0 || dim > rank))
276 {
277 runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
278 "is %ld, should be between 1 and %ld",
279 (long int) dim + 1, (long int) rank + 1);
280 }
281
dfb55fdc 282 len = GFC_DESCRIPTOR_EXTENT(array,dim);
644cb69f
FXC
283 if (len <= 0)
284 return;
28dc6b33 285
21d1335b 286 mbase = mask->base_addr;
28dc6b33
TK
287
288 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
289
290 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
291#ifdef HAVE_GFC_LOGICAL_16
292 || mask_kind == 16
293#endif
294 )
295 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
296 else
297 runtime_error ("Funny sized logical array");
298
dfb55fdc
TK
299 delta = GFC_DESCRIPTOR_STRIDE(array,dim);
300 mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim);
644cb69f
FXC
301
302 for (n = 0; n < dim; n++)
303 {
dfb55fdc
TK
304 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
305 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
306 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
80ee04b9
TK
307
308 if (extent[n] < 0)
309 extent[n] = 0;
310
644cb69f
FXC
311 }
312 for (n = dim; n < rank; n++)
313 {
dfb55fdc
TK
314 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n + 1);
315 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask, n + 1);
316 extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
80ee04b9
TK
317
318 if (extent[n] < 0)
319 extent[n] = 0;
644cb69f
FXC
320 }
321
21d1335b 322 if (retarray->base_addr == NULL)
644cb69f 323 {
dfb55fdc 324 size_t alloc_size, str;
80ee04b9 325
644cb69f 326 for (n = 0; n < rank; n++)
80927a56
JJ
327 {
328 if (n == 0)
329 str = 1;
330 else
331 str= GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
dfb55fdc
TK
332
333 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
334
80927a56 335 }
644cb69f 336
92e6f3a4 337 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
80ee04b9 338
644cb69f 339 retarray->offset = 0;
ca708a2b 340 retarray->dtype.rank = rank;
80ee04b9 341
d56bf419 342 retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_16));
80ee04b9
TK
343 if (alloc_size == 0)
344 {
345 /* Make sure we have a zero-sized array. */
dfb55fdc 346 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
80ee04b9
TK
347 return;
348 }
644cb69f
FXC
349 }
350 else
351 {
644cb69f 352 if (rank != GFC_DESCRIPTOR_RANK (retarray))
fd6590f8
TK
353 runtime_error ("rank of return array incorrect in MINLOC intrinsic");
354
9731c4a3 355 if (unlikely (compile_options.bounds_check))
fd6590f8 356 {
16bff921
TK
357 bounds_ifunction_return ((array_t *) retarray, extent,
358 "return value", "MINLOC");
359 bounds_equal_extents ((array_t *) mask, (array_t *) array,
360 "MASK argument", "MINLOC");
fd6590f8 361 }
644cb69f
FXC
362 }
363
364 for (n = 0; n < rank; n++)
365 {
366 count[n] = 0;
dfb55fdc 367 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
644cb69f 368 if (extent[n] <= 0)
80927a56 369 return;
644cb69f
FXC
370 }
371
21d1335b
TB
372 dest = retarray->base_addr;
373 base = array->base_addr;
644cb69f
FXC
374
375 while (base)
376 {
64acfd99 377 const GFC_REAL_4 * restrict src;
28dc6b33 378 const GFC_LOGICAL_1 * restrict msrc;
644cb69f
FXC
379 GFC_INTEGER_16 result;
380 src = base;
381 msrc = mbase;
382 {
383
80927a56
JJ
384 GFC_REAL_4 minval;
385#if defined (GFC_REAL_4_INFINITY)
386 minval = GFC_REAL_4_INFINITY;
387#else
388 minval = GFC_REAL_4_HUGE;
389#endif
390#if defined (GFC_REAL_4_QUIET_NAN)
391 GFC_INTEGER_16 result2 = 0;
392#endif
393 result = 0;
036e1775 394 for (n = 0; n < len; n++, src += delta, msrc += mdelta)
644cb69f 395 {
644cb69f 396
80927a56
JJ
397 if (*msrc)
398 {
399#if defined (GFC_REAL_4_QUIET_NAN)
400 if (!result2)
401 result2 = (GFC_INTEGER_16)n + 1;
402 if (*src <= minval)
403#endif
404 {
405 minval = *src;
406 result = (GFC_INTEGER_16)n + 1;
407 break;
408 }
409 }
410 }
411#if defined (GFC_REAL_4_QUIET_NAN)
412 if (unlikely (n >= len))
413 result = result2;
414 else
415#endif
b573f931
TK
416 if (back)
417 for (; n < len; n++, src += delta, msrc += mdelta)
418 {
419 if (*msrc && unlikely (*src <= minval))
420 {
421 minval = *src;
422 result = (GFC_INTEGER_16)n + 1;
423 }
424 }
425 else
426 for (; n < len; n++, src += delta, msrc += mdelta)
80927a56 427 {
b573f931
TK
428 if (*msrc && unlikely (*src < minval))
429 {
430 minval = *src;
431 result = (GFC_INTEGER_16) n + 1;
432 }
644cb69f 433 }
036e1775 434 *dest = result;
644cb69f
FXC
435 }
436 /* Advance to the next element. */
437 count[0]++;
438 base += sstride[0];
439 mbase += mstride[0];
440 dest += dstride[0];
441 n = 0;
442 while (count[n] == extent[n])
80927a56
JJ
443 {
444 /* When we get to the end of a dimension, reset it and increment
445 the next dimension. */
446 count[n] = 0;
447 /* We could precalculate these products, but this is a less
448 frequently used path so probably not worth it. */
449 base -= sstride[n] * extent[n];
450 mbase -= mstride[n] * extent[n];
451 dest -= dstride[n] * extent[n];
452 n++;
80dd631f 453 if (n >= rank)
80927a56 454 {
80dd631f 455 /* Break out of the loop. */
80927a56
JJ
456 base = NULL;
457 break;
458 }
459 else
460 {
461 count[n]++;
462 base += sstride[n];
463 mbase += mstride[n];
464 dest += dstride[n];
465 }
466 }
644cb69f
FXC
467 }
468}
469
97a62038
TK
470
471extern void sminloc1_16_r4 (gfc_array_i16 * const restrict,
472 gfc_array_r4 * const restrict, const index_type * const restrict,
64b1806b 473 GFC_LOGICAL_4 *, GFC_LOGICAL_4 back);
97a62038
TK
474export_proto(sminloc1_16_r4);
475
476void
477sminloc1_16_r4 (gfc_array_i16 * const restrict retarray,
478 gfc_array_r4 * const restrict array,
479 const index_type * const restrict pdim,
64b1806b 480 GFC_LOGICAL_4 * mask, GFC_LOGICAL_4 back)
97a62038 481{
802367d7
TK
482 index_type count[GFC_MAX_DIMENSIONS];
483 index_type extent[GFC_MAX_DIMENSIONS];
802367d7
TK
484 index_type dstride[GFC_MAX_DIMENSIONS];
485 GFC_INTEGER_16 * restrict dest;
97a62038
TK
486 index_type rank;
487 index_type n;
802367d7
TK
488 index_type dim;
489
97a62038 490
2ea47ee9 491 if (mask == NULL || *mask)
97a62038 492 {
64b1806b
TK
493#ifdef HAVE_BACK_ARG
494 minloc1_16_r4 (retarray, array, pdim, back);
495#else
97a62038 496 minloc1_16_r4 (retarray, array, pdim);
64b1806b 497#endif
97a62038
TK
498 return;
499 }
802367d7
TK
500 /* Make dim zero based to avoid confusion. */
501 dim = (*pdim) - 1;
502 rank = GFC_DESCRIPTOR_RANK (array) - 1;
503
cfdf6ff6
TK
504 if (unlikely (dim < 0 || dim > rank))
505 {
506 runtime_error ("Dim argument incorrect in MINLOC intrinsic: "
507 "is %ld, should be between 1 and %ld",
508 (long int) dim + 1, (long int) rank + 1);
509 }
510
802367d7
TK
511 for (n = 0; n < dim; n++)
512 {
dfb55fdc 513 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
802367d7
TK
514
515 if (extent[n] <= 0)
516 extent[n] = 0;
517 }
518
519 for (n = dim; n < rank; n++)
520 {
802367d7 521 extent[n] =
80927a56 522 GFC_DESCRIPTOR_EXTENT(array,n + 1);
802367d7
TK
523
524 if (extent[n] <= 0)
80927a56 525 extent[n] = 0;
802367d7 526 }
97a62038 527
21d1335b 528 if (retarray->base_addr == NULL)
97a62038 529 {
dfb55fdc 530 size_t alloc_size, str;
802367d7
TK
531
532 for (n = 0; n < rank; n++)
80927a56
JJ
533 {
534 if (n == 0)
535 str = 1;
536 else
537 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
dfb55fdc
TK
538
539 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
540
80927a56 541 }
802367d7 542
97a62038 543 retarray->offset = 0;
ca708a2b 544 retarray->dtype.rank = rank;
802367d7 545
92e6f3a4 546 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
802367d7 547
d56bf419 548 retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_16));
802367d7
TK
549 if (alloc_size == 0)
550 {
551 /* Make sure we have a zero-sized array. */
dfb55fdc 552 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
802367d7
TK
553 return;
554 }
97a62038
TK
555 }
556 else
557 {
802367d7
TK
558 if (rank != GFC_DESCRIPTOR_RANK (retarray))
559 runtime_error ("rank of return array incorrect in"
560 " MINLOC intrinsic: is %ld, should be %ld",
561 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
562 (long int) rank);
563
9731c4a3 564 if (unlikely (compile_options.bounds_check))
fd6590f8 565 {
802367d7
TK
566 for (n=0; n < rank; n++)
567 {
568 index_type ret_extent;
97a62038 569
dfb55fdc 570 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
802367d7
TK
571 if (extent[n] != ret_extent)
572 runtime_error ("Incorrect extent in return value of"
573 " MINLOC intrinsic in dimension %ld:"
574 " is %ld, should be %ld", (long int) n + 1,
575 (long int) ret_extent, (long int) extent[n]);
576 }
fd6590f8
TK
577 }
578 }
97a62038 579
802367d7
TK
580 for (n = 0; n < rank; n++)
581 {
582 count[n] = 0;
dfb55fdc 583 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
802367d7
TK
584 }
585
21d1335b 586 dest = retarray->base_addr;
802367d7
TK
587
588 while(1)
589 {
590 *dest = 0;
591 count[0]++;
592 dest += dstride[0];
593 n = 0;
594 while (count[n] == extent[n])
80927a56 595 {
802367d7 596 /* When we get to the end of a dimension, reset it and increment
80927a56
JJ
597 the next dimension. */
598 count[n] = 0;
599 /* We could precalculate these products, but this is a less
600 frequently used path so probably not worth it. */
601 dest -= dstride[n] * extent[n];
602 n++;
80dd631f 603 if (n >= rank)
802367d7 604 return;
80927a56
JJ
605 else
606 {
607 count[n]++;
608 dest += dstride[n];
609 }
802367d7
TK
610 }
611 }
97a62038
TK
612}
613
644cb69f 614#endif