Improve max_insns_skipped logic
[official-gcc.git] / libgfortran / generated / maxval_i2.c
blob9a5a6b876a48180aae0c4cbbdc162c912eaf3c0c
1 /* Implementation of the MAXVAL intrinsic
2 Copyright (C) 2002-2017 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
29 #if defined (HAVE_GFC_INTEGER_2) && defined (HAVE_GFC_INTEGER_2)
32 extern void maxval_i2 (gfc_array_i2 * const restrict,
33 gfc_array_i2 * const restrict, const index_type * const restrict);
34 export_proto(maxval_i2);
36 void
37 maxval_i2 (gfc_array_i2 * const restrict retarray,
38 gfc_array_i2 * const restrict array,
39 const index_type * const restrict pdim)
41 index_type count[GFC_MAX_DIMENSIONS];
42 index_type extent[GFC_MAX_DIMENSIONS];
43 index_type sstride[GFC_MAX_DIMENSIONS];
44 index_type dstride[GFC_MAX_DIMENSIONS];
45 const GFC_INTEGER_2 * restrict base;
46 GFC_INTEGER_2 * restrict dest;
47 index_type rank;
48 index_type n;
49 index_type len;
50 index_type delta;
51 index_type dim;
52 int continue_loop;
54 /* Make dim zero based to avoid confusion. */
55 rank = GFC_DESCRIPTOR_RANK (array) - 1;
56 dim = (*pdim) - 1;
58 if (unlikely (dim < 0 || dim > rank))
60 runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
61 "is %ld, should be between 1 and %ld",
62 (long int) dim + 1, (long int) rank + 1);
65 len = GFC_DESCRIPTOR_EXTENT(array,dim);
66 if (len < 0)
67 len = 0;
68 delta = GFC_DESCRIPTOR_STRIDE(array,dim);
70 for (n = 0; n < dim; n++)
72 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
73 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
75 if (extent[n] < 0)
76 extent[n] = 0;
78 for (n = dim; n < rank; n++)
80 sstride[n] = GFC_DESCRIPTOR_STRIDE(array, n + 1);
81 extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
83 if (extent[n] < 0)
84 extent[n] = 0;
87 if (retarray->base_addr == NULL)
89 size_t alloc_size, str;
91 for (n = 0; n < rank; n++)
93 if (n == 0)
94 str = 1;
95 else
96 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
98 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
102 retarray->offset = 0;
103 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
105 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
107 retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_2));
108 if (alloc_size == 0)
110 /* Make sure we have a zero-sized array. */
111 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
112 return;
116 else
118 if (rank != GFC_DESCRIPTOR_RANK (retarray))
119 runtime_error ("rank of return array incorrect in"
120 " MAXVAL intrinsic: is %ld, should be %ld",
121 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
122 (long int) rank);
124 if (unlikely (compile_options.bounds_check))
125 bounds_ifunction_return ((array_t *) retarray, extent,
126 "return value", "MAXVAL");
129 for (n = 0; n < rank; n++)
131 count[n] = 0;
132 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
133 if (extent[n] <= 0)
134 return;
137 base = array->base_addr;
138 dest = retarray->base_addr;
140 continue_loop = 1;
141 while (continue_loop)
143 const GFC_INTEGER_2 * restrict src;
144 GFC_INTEGER_2 result;
145 src = base;
148 #if defined (GFC_INTEGER_2_INFINITY)
149 result = -GFC_INTEGER_2_INFINITY;
150 #else
151 result = (-GFC_INTEGER_2_HUGE-1);
152 #endif
153 if (len <= 0)
154 *dest = (-GFC_INTEGER_2_HUGE-1);
155 else
157 for (n = 0; n < len; n++, src += delta)
160 #if defined (GFC_INTEGER_2_QUIET_NAN)
161 if (*src >= result)
162 break;
164 if (unlikely (n >= len))
165 result = GFC_INTEGER_2_QUIET_NAN;
166 else for (; n < len; n++, src += delta)
168 #endif
169 if (*src > result)
170 result = *src;
173 *dest = result;
176 /* Advance to the next element. */
177 count[0]++;
178 base += sstride[0];
179 dest += dstride[0];
180 n = 0;
181 while (count[n] == extent[n])
183 /* When we get to the end of a dimension, reset it and increment
184 the next dimension. */
185 count[n] = 0;
186 /* We could precalculate these products, but this is a less
187 frequently used path so probably not worth it. */
188 base -= sstride[n] * extent[n];
189 dest -= dstride[n] * extent[n];
190 n++;
191 if (n >= rank)
193 /* Break out of the loop. */
194 continue_loop = 0;
195 break;
197 else
199 count[n]++;
200 base += sstride[n];
201 dest += dstride[n];
208 extern void mmaxval_i2 (gfc_array_i2 * const restrict,
209 gfc_array_i2 * const restrict, const index_type * const restrict,
210 gfc_array_l1 * const restrict);
211 export_proto(mmaxval_i2);
213 void
214 mmaxval_i2 (gfc_array_i2 * const restrict retarray,
215 gfc_array_i2 * const restrict array,
216 const index_type * const restrict pdim,
217 gfc_array_l1 * const restrict mask)
219 index_type count[GFC_MAX_DIMENSIONS];
220 index_type extent[GFC_MAX_DIMENSIONS];
221 index_type sstride[GFC_MAX_DIMENSIONS];
222 index_type dstride[GFC_MAX_DIMENSIONS];
223 index_type mstride[GFC_MAX_DIMENSIONS];
224 GFC_INTEGER_2 * restrict dest;
225 const GFC_INTEGER_2 * restrict base;
226 const GFC_LOGICAL_1 * restrict mbase;
227 index_type rank;
228 index_type dim;
229 index_type n;
230 index_type len;
231 index_type delta;
232 index_type mdelta;
233 int mask_kind;
235 dim = (*pdim) - 1;
236 rank = GFC_DESCRIPTOR_RANK (array) - 1;
239 if (unlikely (dim < 0 || dim > rank))
241 runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
242 "is %ld, should be between 1 and %ld",
243 (long int) dim + 1, (long int) rank + 1);
246 len = GFC_DESCRIPTOR_EXTENT(array,dim);
247 if (len <= 0)
248 return;
250 mbase = mask->base_addr;
252 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
254 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
255 #ifdef HAVE_GFC_LOGICAL_16
256 || mask_kind == 16
257 #endif
259 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
260 else
261 runtime_error ("Funny sized logical array");
263 delta = GFC_DESCRIPTOR_STRIDE(array,dim);
264 mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim);
266 for (n = 0; n < dim; n++)
268 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
269 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
270 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
272 if (extent[n] < 0)
273 extent[n] = 0;
276 for (n = dim; n < rank; n++)
278 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n + 1);
279 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask, n + 1);
280 extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
282 if (extent[n] < 0)
283 extent[n] = 0;
286 if (retarray->base_addr == NULL)
288 size_t alloc_size, str;
290 for (n = 0; n < rank; n++)
292 if (n == 0)
293 str = 1;
294 else
295 str= GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
297 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
301 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
303 retarray->offset = 0;
304 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
306 if (alloc_size == 0)
308 /* Make sure we have a zero-sized array. */
309 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
310 return;
312 else
313 retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_2));
316 else
318 if (rank != GFC_DESCRIPTOR_RANK (retarray))
319 runtime_error ("rank of return array incorrect in MAXVAL intrinsic");
321 if (unlikely (compile_options.bounds_check))
323 bounds_ifunction_return ((array_t *) retarray, extent,
324 "return value", "MAXVAL");
325 bounds_equal_extents ((array_t *) mask, (array_t *) array,
326 "MASK argument", "MAXVAL");
330 for (n = 0; n < rank; n++)
332 count[n] = 0;
333 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
334 if (extent[n] <= 0)
335 return;
338 dest = retarray->base_addr;
339 base = array->base_addr;
341 while (base)
343 const GFC_INTEGER_2 * restrict src;
344 const GFC_LOGICAL_1 * restrict msrc;
345 GFC_INTEGER_2 result;
346 src = base;
347 msrc = mbase;
350 #if defined (GFC_INTEGER_2_INFINITY)
351 result = -GFC_INTEGER_2_INFINITY;
352 #else
353 result = (-GFC_INTEGER_2_HUGE-1);
354 #endif
355 #if defined (GFC_INTEGER_2_QUIET_NAN)
356 int non_empty_p = 0;
357 #endif
358 for (n = 0; n < len; n++, src += delta, msrc += mdelta)
361 #if defined (GFC_INTEGER_2_INFINITY) || defined (GFC_INTEGER_2_QUIET_NAN)
362 if (*msrc)
364 #if defined (GFC_INTEGER_2_QUIET_NAN)
365 non_empty_p = 1;
366 if (*src >= result)
367 #endif
368 break;
371 if (unlikely (n >= len))
373 #if defined (GFC_INTEGER_2_QUIET_NAN)
374 result = non_empty_p ? GFC_INTEGER_2_QUIET_NAN : (-GFC_INTEGER_2_HUGE-1);
375 #else
376 result = (-GFC_INTEGER_2_HUGE-1);
377 #endif
379 else for (; n < len; n++, src += delta, msrc += mdelta)
381 #endif
382 if (*msrc && *src > result)
383 result = *src;
385 *dest = result;
387 /* Advance to the next element. */
388 count[0]++;
389 base += sstride[0];
390 mbase += mstride[0];
391 dest += dstride[0];
392 n = 0;
393 while (count[n] == extent[n])
395 /* When we get to the end of a dimension, reset it and increment
396 the next dimension. */
397 count[n] = 0;
398 /* We could precalculate these products, but this is a less
399 frequently used path so probably not worth it. */
400 base -= sstride[n] * extent[n];
401 mbase -= mstride[n] * extent[n];
402 dest -= dstride[n] * extent[n];
403 n++;
404 if (n >= rank)
406 /* Break out of the loop. */
407 base = NULL;
408 break;
410 else
412 count[n]++;
413 base += sstride[n];
414 mbase += mstride[n];
415 dest += dstride[n];
422 extern void smaxval_i2 (gfc_array_i2 * const restrict,
423 gfc_array_i2 * const restrict, const index_type * const restrict,
424 GFC_LOGICAL_4 *);
425 export_proto(smaxval_i2);
427 void
428 smaxval_i2 (gfc_array_i2 * const restrict retarray,
429 gfc_array_i2 * const restrict array,
430 const index_type * const restrict pdim,
431 GFC_LOGICAL_4 * mask)
433 index_type count[GFC_MAX_DIMENSIONS];
434 index_type extent[GFC_MAX_DIMENSIONS];
435 index_type dstride[GFC_MAX_DIMENSIONS];
436 GFC_INTEGER_2 * restrict dest;
437 index_type rank;
438 index_type n;
439 index_type dim;
442 if (*mask)
444 maxval_i2 (retarray, array, pdim);
445 return;
447 /* Make dim zero based to avoid confusion. */
448 dim = (*pdim) - 1;
449 rank = GFC_DESCRIPTOR_RANK (array) - 1;
451 if (unlikely (dim < 0 || dim > rank))
453 runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
454 "is %ld, should be between 1 and %ld",
455 (long int) dim + 1, (long int) rank + 1);
458 for (n = 0; n < dim; n++)
460 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
462 if (extent[n] <= 0)
463 extent[n] = 0;
466 for (n = dim; n < rank; n++)
468 extent[n] =
469 GFC_DESCRIPTOR_EXTENT(array,n + 1);
471 if (extent[n] <= 0)
472 extent[n] = 0;
475 if (retarray->base_addr == NULL)
477 size_t alloc_size, str;
479 for (n = 0; n < rank; n++)
481 if (n == 0)
482 str = 1;
483 else
484 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
486 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
490 retarray->offset = 0;
491 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
493 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
495 if (alloc_size == 0)
497 /* Make sure we have a zero-sized array. */
498 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
499 return;
501 else
502 retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_2));
504 else
506 if (rank != GFC_DESCRIPTOR_RANK (retarray))
507 runtime_error ("rank of return array incorrect in"
508 " MAXVAL intrinsic: is %ld, should be %ld",
509 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
510 (long int) rank);
512 if (unlikely (compile_options.bounds_check))
514 for (n=0; n < rank; n++)
516 index_type ret_extent;
518 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
519 if (extent[n] != ret_extent)
520 runtime_error ("Incorrect extent in return value of"
521 " MAXVAL intrinsic in dimension %ld:"
522 " is %ld, should be %ld", (long int) n + 1,
523 (long int) ret_extent, (long int) extent[n]);
528 for (n = 0; n < rank; n++)
530 count[n] = 0;
531 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
534 dest = retarray->base_addr;
536 while(1)
538 *dest = (-GFC_INTEGER_2_HUGE-1);
539 count[0]++;
540 dest += dstride[0];
541 n = 0;
542 while (count[n] == extent[n])
544 /* When we get to the end of a dimension, reset it and increment
545 the next dimension. */
546 count[n] = 0;
547 /* We could precalculate these products, but this is a less
548 frequently used path so probably not worth it. */
549 dest -= dstride[n] * extent[n];
550 n++;
551 if (n >= rank)
552 return;
553 else
555 count[n]++;
556 dest += dstride[n];
562 #endif