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