2018-03-08 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgfortran / generated / minval_i2.c
blob6539ae10b6013563fe68fe23d22b938aa0bb11e2
1 /* Implementation of the MINVAL intrinsic
2 Copyright (C) 2002-2018 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 minval_i2 (gfc_array_i2 * const restrict,
33 gfc_array_i2 * const restrict, const index_type * const restrict);
34 export_proto(minval_i2);
36 void
37 minval_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 #ifdef HAVE_BACK_ARG
55 assert(back == 0);
56 #endif
58 /* Make dim zero based to avoid confusion. */
59 rank = GFC_DESCRIPTOR_RANK (array) - 1;
60 dim = (*pdim) - 1;
62 if (unlikely (dim < 0 || dim > rank))
64 runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
65 "is %ld, should be between 1 and %ld",
66 (long int) dim + 1, (long int) rank + 1);
69 len = GFC_DESCRIPTOR_EXTENT(array,dim);
70 if (len < 0)
71 len = 0;
72 delta = GFC_DESCRIPTOR_STRIDE(array,dim);
74 for (n = 0; n < dim; n++)
76 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
77 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
79 if (extent[n] < 0)
80 extent[n] = 0;
82 for (n = dim; n < rank; n++)
84 sstride[n] = GFC_DESCRIPTOR_STRIDE(array, n + 1);
85 extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
87 if (extent[n] < 0)
88 extent[n] = 0;
91 if (retarray->base_addr == NULL)
93 size_t alloc_size, str;
95 for (n = 0; n < rank; n++)
97 if (n == 0)
98 str = 1;
99 else
100 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
102 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
106 retarray->offset = 0;
107 GFC_DTYPE_COPY_SETRANK(retarray,array,rank);
109 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
111 retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_2));
112 if (alloc_size == 0)
114 /* Make sure we have a zero-sized array. */
115 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
116 return;
120 else
122 if (rank != GFC_DESCRIPTOR_RANK (retarray))
123 runtime_error ("rank of return array incorrect in"
124 " MINVAL intrinsic: is %ld, should be %ld",
125 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
126 (long int) rank);
128 if (unlikely (compile_options.bounds_check))
129 bounds_ifunction_return ((array_t *) retarray, extent,
130 "return value", "MINVAL");
133 for (n = 0; n < rank; n++)
135 count[n] = 0;
136 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
137 if (extent[n] <= 0)
138 return;
141 base = array->base_addr;
142 dest = retarray->base_addr;
144 continue_loop = 1;
145 while (continue_loop)
147 const GFC_INTEGER_2 * restrict src;
148 GFC_INTEGER_2 result;
149 src = base;
152 #if defined (GFC_INTEGER_2_INFINITY)
153 result = GFC_INTEGER_2_INFINITY;
154 #else
155 result = GFC_INTEGER_2_HUGE;
156 #endif
157 if (len <= 0)
158 *dest = GFC_INTEGER_2_HUGE;
159 else
161 for (n = 0; n < len; n++, src += delta)
164 #if defined (GFC_INTEGER_2_QUIET_NAN)
165 if (*src <= result)
166 break;
168 if (unlikely (n >= len))
169 result = GFC_INTEGER_2_QUIET_NAN;
170 else for (; n < len; n++, src += delta)
172 #endif
173 if (*src < result)
174 result = *src;
177 *dest = result;
180 /* Advance to the next element. */
181 count[0]++;
182 base += sstride[0];
183 dest += dstride[0];
184 n = 0;
185 while (count[n] == extent[n])
187 /* When we get to the end of a dimension, reset it and increment
188 the next dimension. */
189 count[n] = 0;
190 /* We could precalculate these products, but this is a less
191 frequently used path so probably not worth it. */
192 base -= sstride[n] * extent[n];
193 dest -= dstride[n] * extent[n];
194 n++;
195 if (n >= rank)
197 /* Break out of the loop. */
198 continue_loop = 0;
199 break;
201 else
203 count[n]++;
204 base += sstride[n];
205 dest += dstride[n];
212 extern void mminval_i2 (gfc_array_i2 * const restrict,
213 gfc_array_i2 * const restrict, const index_type * const restrict,
214 gfc_array_l1 * const restrict);
215 export_proto(mminval_i2);
217 void
218 mminval_i2 (gfc_array_i2 * const restrict retarray,
219 gfc_array_i2 * const restrict array,
220 const index_type * const restrict pdim,
221 gfc_array_l1 * const restrict mask)
223 index_type count[GFC_MAX_DIMENSIONS];
224 index_type extent[GFC_MAX_DIMENSIONS];
225 index_type sstride[GFC_MAX_DIMENSIONS];
226 index_type dstride[GFC_MAX_DIMENSIONS];
227 index_type mstride[GFC_MAX_DIMENSIONS];
228 GFC_INTEGER_2 * restrict dest;
229 const GFC_INTEGER_2 * restrict base;
230 const GFC_LOGICAL_1 * restrict mbase;
231 index_type rank;
232 index_type dim;
233 index_type n;
234 index_type len;
235 index_type delta;
236 index_type mdelta;
237 int mask_kind;
239 #ifdef HAVE_BACK_ARG
240 assert (back == 0);
241 #endif
242 dim = (*pdim) - 1;
243 rank = GFC_DESCRIPTOR_RANK (array) - 1;
246 if (unlikely (dim < 0 || dim > rank))
248 runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
249 "is %ld, should be between 1 and %ld",
250 (long int) dim + 1, (long int) rank + 1);
253 len = GFC_DESCRIPTOR_EXTENT(array,dim);
254 if (len <= 0)
255 return;
257 mbase = mask->base_addr;
259 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
261 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
262 #ifdef HAVE_GFC_LOGICAL_16
263 || mask_kind == 16
264 #endif
266 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
267 else
268 runtime_error ("Funny sized logical array");
270 delta = GFC_DESCRIPTOR_STRIDE(array,dim);
271 mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim);
273 for (n = 0; n < dim; n++)
275 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
276 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
277 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
279 if (extent[n] < 0)
280 extent[n] = 0;
283 for (n = dim; n < rank; n++)
285 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n + 1);
286 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask, n + 1);
287 extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
289 if (extent[n] < 0)
290 extent[n] = 0;
293 if (retarray->base_addr == NULL)
295 size_t alloc_size, str;
297 for (n = 0; n < rank; n++)
299 if (n == 0)
300 str = 1;
301 else
302 str= GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
304 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
308 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
310 retarray->offset = 0;
311 GFC_DTYPE_COPY_SETRANK(retarray,array,rank);
313 if (alloc_size == 0)
315 /* Make sure we have a zero-sized array. */
316 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
317 return;
319 else
320 retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_2));
323 else
325 if (rank != GFC_DESCRIPTOR_RANK (retarray))
326 runtime_error ("rank of return array incorrect in MINVAL intrinsic");
328 if (unlikely (compile_options.bounds_check))
330 bounds_ifunction_return ((array_t *) retarray, extent,
331 "return value", "MINVAL");
332 bounds_equal_extents ((array_t *) mask, (array_t *) array,
333 "MASK argument", "MINVAL");
337 for (n = 0; n < rank; n++)
339 count[n] = 0;
340 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
341 if (extent[n] <= 0)
342 return;
345 dest = retarray->base_addr;
346 base = array->base_addr;
348 while (base)
350 const GFC_INTEGER_2 * restrict src;
351 const GFC_LOGICAL_1 * restrict msrc;
352 GFC_INTEGER_2 result;
353 src = base;
354 msrc = mbase;
357 #if defined (GFC_INTEGER_2_INFINITY)
358 result = GFC_INTEGER_2_INFINITY;
359 #else
360 result = GFC_INTEGER_2_HUGE;
361 #endif
362 #if defined (GFC_INTEGER_2_QUIET_NAN)
363 int non_empty_p = 0;
364 #endif
365 for (n = 0; n < len; n++, src += delta, msrc += mdelta)
368 #if defined (GFC_INTEGER_2_INFINITY) || defined (GFC_INTEGER_2_QUIET_NAN)
369 if (*msrc)
371 #if defined (GFC_INTEGER_2_QUIET_NAN)
372 non_empty_p = 1;
373 if (*src <= result)
374 #endif
375 break;
378 if (unlikely (n >= len))
380 #if defined (GFC_INTEGER_2_QUIET_NAN)
381 result = non_empty_p ? GFC_INTEGER_2_QUIET_NAN : GFC_INTEGER_2_HUGE;
382 #else
383 result = GFC_INTEGER_2_HUGE;
384 #endif
386 else for (; n < len; n++, src += delta, msrc += mdelta)
388 #endif
389 if (*msrc && *src < result)
390 result = *src;
392 *dest = result;
394 /* Advance to the next element. */
395 count[0]++;
396 base += sstride[0];
397 mbase += mstride[0];
398 dest += dstride[0];
399 n = 0;
400 while (count[n] == extent[n])
402 /* When we get to the end of a dimension, reset it and increment
403 the next dimension. */
404 count[n] = 0;
405 /* We could precalculate these products, but this is a less
406 frequently used path so probably not worth it. */
407 base -= sstride[n] * extent[n];
408 mbase -= mstride[n] * extent[n];
409 dest -= dstride[n] * extent[n];
410 n++;
411 if (n >= rank)
413 /* Break out of the loop. */
414 base = NULL;
415 break;
417 else
419 count[n]++;
420 base += sstride[n];
421 mbase += mstride[n];
422 dest += dstride[n];
429 extern void sminval_i2 (gfc_array_i2 * const restrict,
430 gfc_array_i2 * const restrict, const index_type * const restrict,
431 GFC_LOGICAL_4 *);
432 export_proto(sminval_i2);
434 void
435 sminval_i2 (gfc_array_i2 * const restrict retarray,
436 gfc_array_i2 * const restrict array,
437 const index_type * const restrict pdim,
438 GFC_LOGICAL_4 * mask)
440 index_type count[GFC_MAX_DIMENSIONS];
441 index_type extent[GFC_MAX_DIMENSIONS];
442 index_type dstride[GFC_MAX_DIMENSIONS];
443 GFC_INTEGER_2 * restrict dest;
444 index_type rank;
445 index_type n;
446 index_type dim;
449 if (*mask)
451 #ifdef HAVE_BACK_ARG
452 minval_i2 (retarray, array, pdim, back);
453 #else
454 minval_i2 (retarray, array, pdim);
455 #endif
456 return;
458 /* Make dim zero based to avoid confusion. */
459 dim = (*pdim) - 1;
460 rank = GFC_DESCRIPTOR_RANK (array) - 1;
462 if (unlikely (dim < 0 || dim > rank))
464 runtime_error ("Dim argument incorrect in MINVAL intrinsic: "
465 "is %ld, should be between 1 and %ld",
466 (long int) dim + 1, (long int) rank + 1);
469 for (n = 0; n < dim; n++)
471 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
473 if (extent[n] <= 0)
474 extent[n] = 0;
477 for (n = dim; n < rank; n++)
479 extent[n] =
480 GFC_DESCRIPTOR_EXTENT(array,n + 1);
482 if (extent[n] <= 0)
483 extent[n] = 0;
486 if (retarray->base_addr == NULL)
488 size_t alloc_size, str;
490 for (n = 0; n < rank; n++)
492 if (n == 0)
493 str = 1;
494 else
495 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
497 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
501 retarray->offset = 0;
502 GFC_DTYPE_COPY_SETRANK(retarray,array,rank);
504 alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
506 if (alloc_size == 0)
508 /* Make sure we have a zero-sized array. */
509 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
510 return;
512 else
513 retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_2));
515 else
517 if (rank != GFC_DESCRIPTOR_RANK (retarray))
518 runtime_error ("rank of return array incorrect in"
519 " MINVAL intrinsic: is %ld, should be %ld",
520 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
521 (long int) rank);
523 if (unlikely (compile_options.bounds_check))
525 for (n=0; n < rank; n++)
527 index_type ret_extent;
529 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
530 if (extent[n] != ret_extent)
531 runtime_error ("Incorrect extent in return value of"
532 " MINVAL intrinsic in dimension %ld:"
533 " is %ld, should be %ld", (long int) n + 1,
534 (long int) ret_extent, (long int) extent[n]);
539 for (n = 0; n < rank; n++)
541 count[n] = 0;
542 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
545 dest = retarray->base_addr;
547 while(1)
549 *dest = GFC_INTEGER_2_HUGE;
550 count[0]++;
551 dest += dstride[0];
552 n = 0;
553 while (count[n] == extent[n])
555 /* When we get to the end of a dimension, reset it and increment
556 the next dimension. */
557 count[n] = 0;
558 /* We could precalculate these products, but this is a less
559 frequently used path so probably not worth it. */
560 dest -= dstride[n] * extent[n];
561 n++;
562 if (n >= rank)
563 return;
564 else
566 count[n]++;
567 dest += dstride[n];
573 #endif