]> git.ipfire.org Git - thirdparty/gcc.git/blame - libgfortran/generated/maxloc1_16_i16.c
Update copyright years.
[thirdparty/gcc.git] / libgfortran / generated / maxloc1_16_i16.c
CommitLineData
920e54ef 1/* Implementation of the MAXLOC intrinsic
fbd26352 2 Copyright (C) 2002-2019 Free Software Foundation, Inc.
920e54ef 3 Contributed by Paul Brook <paul@nowt.org>
4
b4ba8232 5This file is part of the GNU Fortran runtime library (libgfortran).
920e54ef 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
6bc9506f 10version 3 of the License, or (at your option) any later version.
920e54ef 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
6bc9506f 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/>. */
920e54ef 25
41f2d5e8 26#include "libgfortran.h"
cb458068 27#include <assert.h>
920e54ef 28
29
30#if defined (HAVE_GFC_INTEGER_16) && defined (HAVE_GFC_INTEGER_16)
31
cb458068 32#define HAVE_BACK_ARG 1
33
920e54ef 34
b4cafd67 35extern void maxloc1_16_i16 (gfc_array_i16 * const restrict,
cb458068 36 gfc_array_i16 * const restrict, const index_type * const restrict, GFC_LOGICAL_4 back);
920e54ef 37export_proto(maxloc1_16_i16);
38
39void
b4cafd67 40maxloc1_16_i16 (gfc_array_i16 * const restrict retarray,
41 gfc_array_i16 * const restrict array,
cb458068 42 const index_type * const restrict pdim, GFC_LOGICAL_4 back)
920e54ef 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];
b4cafd67 48 const GFC_INTEGER_16 * restrict base;
49 GFC_INTEGER_16 * restrict dest;
920e54ef 50 index_type rank;
51 index_type n;
52 index_type len;
53 index_type delta;
54 index_type dim;
393a1b6c 55 int continue_loop;
920e54ef 56
57 /* Make dim zero based to avoid confusion. */
920e54ef 58 rank = GFC_DESCRIPTOR_RANK (array) - 1;
3a33b9df 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 }
920e54ef 67
827aef63 68 len = GFC_DESCRIPTOR_EXTENT(array,dim);
393a1b6c 69 if (len < 0)
70 len = 0;
827aef63 71 delta = GFC_DESCRIPTOR_STRIDE(array,dim);
920e54ef 72
73 for (n = 0; n < dim; n++)
74 {
827aef63 75 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
76 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
a14c06e9 77
78 if (extent[n] < 0)
79 extent[n] = 0;
920e54ef 80 }
81 for (n = dim; n < rank; n++)
82 {
827aef63 83 sstride[n] = GFC_DESCRIPTOR_STRIDE(array, n + 1);
84 extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
a14c06e9 85
86 if (extent[n] < 0)
87 extent[n] = 0;
920e54ef 88 }
89
553877d9 90 if (retarray->base_addr == NULL)
920e54ef 91 {
827aef63 92 size_t alloc_size, str;
a14c06e9 93
920e54ef 94 for (n = 0; n < rank; n++)
7ebee933 95 {
96 if (n == 0)
827aef63 97 str = 1;
7ebee933 98 else
99 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
827aef63 100
101 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
102
7ebee933 103 }
920e54ef 104
920e54ef 105 retarray->offset = 0;
0bb0be20 106 retarray->dtype.rank = rank;
a14c06e9 107
af1e9051 108 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
a14c06e9 109
af1e9051 110 retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_16));
a14c06e9 111 if (alloc_size == 0)
112 {
113 /* Make sure we have a zero-sized array. */
827aef63 114 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
a14c06e9 115 return;
827aef63 116
a14c06e9 117 }
920e54ef 118 }
119 else
120 {
920e54ef 121 if (rank != GFC_DESCRIPTOR_RANK (retarray))
8dec97a0 122 runtime_error ("rank of return array incorrect in"
ae66f8f3 123 " MAXLOC intrinsic: is %ld, should be %ld",
124 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
125 (long int) rank);
8dec97a0 126
c7fb575f 127 if (unlikely (compile_options.bounds_check))
5d04d450 128 bounds_ifunction_return ((array_t *) retarray, extent,
129 "return value", "MAXLOC");
920e54ef 130 }
131
132 for (n = 0; n < rank; n++)
133 {
134 count[n] = 0;
827aef63 135 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
920e54ef 136 if (extent[n] <= 0)
7cbf86e5 137 return;
920e54ef 138 }
139
553877d9 140 base = array->base_addr;
141 dest = retarray->base_addr;
920e54ef 142
393a1b6c 143 continue_loop = 1;
144 while (continue_loop)
920e54ef 145 {
b4cafd67 146 const GFC_INTEGER_16 * restrict src;
920e54ef 147 GFC_INTEGER_16 result;
148 src = base;
149 {
150
7ebee933 151 GFC_INTEGER_16 maxval;
152#if defined (GFC_INTEGER_16_INFINITY)
153 maxval = -GFC_INTEGER_16_INFINITY;
154#else
155 maxval = (-GFC_INTEGER_16_HUGE-1);
156#endif
157 result = 1;
158 if (len <= 0)
920e54ef 159 *dest = 0;
160 else
161 {
ca96069a 162#if ! defined HAVE_BACK_ARG
920e54ef 163 for (n = 0; n < len; n++, src += delta)
164 {
ca96069a 165#endif
920e54ef 166
7ebee933 167#if defined (GFC_INTEGER_16_QUIET_NAN)
ca96069a 168 for (n = 0; n < len; n++, src += delta)
169 {
7ebee933 170 if (*src >= maxval)
171 {
172 maxval = *src;
173 result = (GFC_INTEGER_16)n + 1;
174 break;
175 }
176 }
ca96069a 177#else
178 n = 0;
179#endif
7ebee933 180 for (; n < len; n++, src += delta)
181 {
ca96069a 182 if (back ? *src >= maxval : *src > maxval)
7ebee933 183 {
184 maxval = *src;
185 result = (GFC_INTEGER_16)n + 1;
186 }
187 }
b4ba8232 188
920e54ef 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])
7ebee933 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++;
bacde0a1 207 if (n >= rank)
7ebee933 208 {
bacde0a1 209 /* Break out of the loop. */
393a1b6c 210 continue_loop = 0;
211 break;
7ebee933 212 }
213 else
214 {
215 count[n]++;
216 base += sstride[n];
217 dest += dstride[n];
218 }
219 }
920e54ef 220 }
221}
222
223
b4cafd67 224extern void mmaxloc1_16_i16 (gfc_array_i16 * const restrict,
225 gfc_array_i16 * const restrict, const index_type * const restrict,
cb458068 226 gfc_array_l1 * const restrict, GFC_LOGICAL_4 back);
920e54ef 227export_proto(mmaxloc1_16_i16);
228
229void
b4cafd67 230mmaxloc1_16_i16 (gfc_array_i16 * const restrict retarray,
231 gfc_array_i16 * const restrict array,
232 const index_type * const restrict pdim,
cb458068 233 gfc_array_l1 * const restrict mask, GFC_LOGICAL_4 back)
920e54ef 234{
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];
b4cafd67 240 GFC_INTEGER_16 * restrict dest;
241 const GFC_INTEGER_16 * restrict base;
7ed8f627 242 const GFC_LOGICAL_1 * restrict mbase;
3a33b9df 243 index_type rank;
244 index_type dim;
920e54ef 245 index_type n;
246 index_type len;
247 index_type delta;
248 index_type mdelta;
7ed8f627 249 int mask_kind;
920e54ef 250
538bdcdc 251 if (mask == NULL)
252 {
253#ifdef HAVE_BACK_ARG
254 maxloc1_16_i16 (retarray, array, pdim, back);
255#else
256 maxloc1_16_i16 (retarray, array, pdim);
257#endif
258 return;
259 }
260
920e54ef 261 dim = (*pdim) - 1;
262 rank = GFC_DESCRIPTOR_RANK (array) - 1;
263
3a33b9df 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
827aef63 272 len = GFC_DESCRIPTOR_EXTENT(array,dim);
920e54ef 273 if (len <= 0)
274 return;
7ed8f627 275
553877d9 276 mbase = mask->base_addr;
7ed8f627 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
827aef63 289 delta = GFC_DESCRIPTOR_STRIDE(array,dim);
290 mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim);
920e54ef 291
292 for (n = 0; n < dim; n++)
293 {
827aef63 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);
a14c06e9 297
298 if (extent[n] < 0)
299 extent[n] = 0;
300
920e54ef 301 }
302 for (n = dim; n < rank; n++)
303 {
827aef63 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);
a14c06e9 307
308 if (extent[n] < 0)
309 extent[n] = 0;
920e54ef 310 }
311
553877d9 312 if (retarray->base_addr == NULL)
920e54ef 313 {
827aef63 314 size_t alloc_size, str;
a14c06e9 315
920e54ef 316 for (n = 0; n < rank; n++)
7ebee933 317 {
318 if (n == 0)
319 str = 1;
320 else
321 str= GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
827aef63 322
323 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
324
7ebee933 325 }
920e54ef 326
af1e9051 327 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
a14c06e9 328
920e54ef 329 retarray->offset = 0;
0bb0be20 330 retarray->dtype.rank = rank;
a14c06e9 331
332 if (alloc_size == 0)
333 {
334 /* Make sure we have a zero-sized array. */
827aef63 335 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
a14c06e9 336 return;
337 }
338 else
af1e9051 339 retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_16));
a14c06e9 340
920e54ef 341 }
342 else
343 {
920e54ef 344 if (rank != GFC_DESCRIPTOR_RANK (retarray))
8dec97a0 345 runtime_error ("rank of return array incorrect in MAXLOC intrinsic");
346
c7fb575f 347 if (unlikely (compile_options.bounds_check))
8dec97a0 348 {
5d04d450 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");
8dec97a0 353 }
920e54ef 354 }
355
356 for (n = 0; n < rank; n++)
357 {
358 count[n] = 0;
827aef63 359 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
920e54ef 360 if (extent[n] <= 0)
7ebee933 361 return;
920e54ef 362 }
363
553877d9 364 dest = retarray->base_addr;
365 base = array->base_addr;
920e54ef 366
367 while (base)
368 {
b4cafd67 369 const GFC_INTEGER_16 * restrict src;
7ed8f627 370 const GFC_LOGICAL_1 * restrict msrc;
920e54ef 371 GFC_INTEGER_16 result;
372 src = base;
373 msrc = mbase;
374 {
375
7ebee933 376 GFC_INTEGER_16 maxval;
377#if defined (GFC_INTEGER_16_INFINITY)
378 maxval = -GFC_INTEGER_16_INFINITY;
379#else
380 maxval = (-GFC_INTEGER_16_HUGE-1);
381#endif
382#if defined (GFC_INTEGER_16_QUIET_NAN)
383 GFC_INTEGER_16 result2 = 0;
384#endif
385 result = 0;
08e1eb56 386 for (n = 0; n < len; n++, src += delta, msrc += mdelta)
920e54ef 387 {
920e54ef 388
7ebee933 389 if (*msrc)
390 {
391#if defined (GFC_INTEGER_16_QUIET_NAN)
392 if (!result2)
393 result2 = (GFC_INTEGER_16)n + 1;
394 if (*src >= maxval)
395#endif
396 {
397 maxval = *src;
398 result = (GFC_INTEGER_16)n + 1;
399 break;
400 }
401 }
402 }
403#if defined (GFC_INTEGER_16_QUIET_NAN)
404 if (unlikely (n >= len))
405 result = result2;
406 else
407#endif
ca96069a 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_16)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_16)n + 1;
424 }
920e54ef 425 }
08e1eb56 426 *dest = result;
920e54ef 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])
7ebee933 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++;
bacde0a1 445 if (n >= rank)
7ebee933 446 {
bacde0a1 447 /* Break out of the loop. */
7ebee933 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 }
920e54ef 459 }
460}
461
4292b27d 462
463extern void smaxloc1_16_i16 (gfc_array_i16 * const restrict,
464 gfc_array_i16 * const restrict, const index_type * const restrict,
cb458068 465 GFC_LOGICAL_4 *, GFC_LOGICAL_4 back);
4292b27d 466export_proto(smaxloc1_16_i16);
467
468void
469smaxloc1_16_i16 (gfc_array_i16 * const restrict retarray,
470 gfc_array_i16 * const restrict array,
471 const index_type * const restrict pdim,
cb458068 472 GFC_LOGICAL_4 * mask, GFC_LOGICAL_4 back)
4292b27d 473{
f955bfc4 474 index_type count[GFC_MAX_DIMENSIONS];
475 index_type extent[GFC_MAX_DIMENSIONS];
f955bfc4 476 index_type dstride[GFC_MAX_DIMENSIONS];
477 GFC_INTEGER_16 * restrict dest;
4292b27d 478 index_type rank;
479 index_type n;
f955bfc4 480 index_type dim;
481
4292b27d 482
538bdcdc 483 if (mask == NULL || *mask)
4292b27d 484 {
cb458068 485#ifdef HAVE_BACK_ARG
486 maxloc1_16_i16 (retarray, array, pdim, back);
487#else
4292b27d 488 maxloc1_16_i16 (retarray, array, pdim);
cb458068 489#endif
4292b27d 490 return;
491 }
f955bfc4 492 /* Make dim zero based to avoid confusion. */
493 dim = (*pdim) - 1;
494 rank = GFC_DESCRIPTOR_RANK (array) - 1;
495
3a33b9df 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
f955bfc4 503 for (n = 0; n < dim; n++)
504 {
827aef63 505 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
f955bfc4 506
507 if (extent[n] <= 0)
508 extent[n] = 0;
509 }
510
511 for (n = dim; n < rank; n++)
512 {
f955bfc4 513 extent[n] =
7ebee933 514 GFC_DESCRIPTOR_EXTENT(array,n + 1);
f955bfc4 515
516 if (extent[n] <= 0)
7ebee933 517 extent[n] = 0;
f955bfc4 518 }
4292b27d 519
553877d9 520 if (retarray->base_addr == NULL)
4292b27d 521 {
827aef63 522 size_t alloc_size, str;
f955bfc4 523
524 for (n = 0; n < rank; n++)
7ebee933 525 {
526 if (n == 0)
527 str = 1;
528 else
529 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
827aef63 530
531 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
532
7ebee933 533 }
f955bfc4 534
4292b27d 535 retarray->offset = 0;
0bb0be20 536 retarray->dtype.rank = rank;
f955bfc4 537
af1e9051 538 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
f955bfc4 539
540 if (alloc_size == 0)
541 {
542 /* Make sure we have a zero-sized array. */
827aef63 543 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
f955bfc4 544 return;
545 }
546 else
af1e9051 547 retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_16));
4292b27d 548 }
549 else
550 {
f955bfc4 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
c7fb575f 557 if (unlikely (compile_options.bounds_check))
8dec97a0 558 {
f955bfc4 559 for (n=0; n < rank; n++)
560 {
561 index_type ret_extent;
4292b27d 562
827aef63 563 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
f955bfc4 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 }
8dec97a0 570 }
571 }
4292b27d 572
f955bfc4 573 for (n = 0; n < rank; n++)
574 {
575 count[n] = 0;
827aef63 576 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
f955bfc4 577 }
578
553877d9 579 dest = retarray->base_addr;
f955bfc4 580
581 while(1)
582 {
583 *dest = 0;
584 count[0]++;
585 dest += dstride[0];
586 n = 0;
587 while (count[n] == extent[n])
7ebee933 588 {
f955bfc4 589 /* When we get to the end of a dimension, reset it and increment
7ebee933 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++;
bacde0a1 596 if (n >= rank)
f955bfc4 597 return;
7ebee933 598 else
599 {
600 count[n]++;
601 dest += dstride[n];
602 }
f955bfc4 603 }
604 }
4292b27d 605}
606
920e54ef 607#endif