Merge from mainline (163495:164578).
[official-gcc/graphite-test-results.git] / libgfortran / generated / minval_r4.c
blob4d5b8b0cfb36236f8eaa64af1f8a6fbd6a37f86d
1 /* Implementation of the MINVAL intrinsic
2 Copyright 2002, 2007, 2009, 2010 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"
27 #include <stdlib.h>
28 #include <assert.h>
31 #if defined (HAVE_GFC_REAL_4) && defined (HAVE_GFC_REAL_4)
34 extern void minval_r4 (gfc_array_r4 * const restrict,
35 gfc_array_r4 * const restrict, const index_type * const restrict);
36 export_proto(minval_r4);
38 void
39 minval_r4 (gfc_array_r4 * const restrict retarray,
40 gfc_array_r4 * const restrict array,
41 const index_type * const restrict pdim)
43 index_type count[GFC_MAX_DIMENSIONS];
44 index_type extent[GFC_MAX_DIMENSIONS];
45 index_type sstride[GFC_MAX_DIMENSIONS];
46 index_type dstride[GFC_MAX_DIMENSIONS];
47 const GFC_REAL_4 * restrict base;
48 GFC_REAL_4 * restrict dest;
49 index_type rank;
50 index_type n;
51 index_type len;
52 index_type delta;
53 index_type dim;
54 int continue_loop;
56 /* Make dim zero based to avoid confusion. */
57 dim = (*pdim) - 1;
58 rank = GFC_DESCRIPTOR_RANK (array) - 1;
60 len = GFC_DESCRIPTOR_EXTENT(array,dim);
61 if (len < 0)
62 len = 0;
63 delta = GFC_DESCRIPTOR_STRIDE(array,dim);
65 for (n = 0; n < dim; n++)
67 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
68 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
70 if (extent[n] < 0)
71 extent[n] = 0;
73 for (n = dim; n < rank; n++)
75 sstride[n] = GFC_DESCRIPTOR_STRIDE(array, n + 1);
76 extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
78 if (extent[n] < 0)
79 extent[n] = 0;
82 if (retarray->data == NULL)
84 size_t alloc_size, str;
86 for (n = 0; n < rank; n++)
88 if (n == 0)
89 str = 1;
90 else
91 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
93 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
97 retarray->offset = 0;
98 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
100 alloc_size = sizeof (GFC_REAL_4) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
101 * extent[rank-1];
103 if (alloc_size == 0)
105 /* Make sure we have a zero-sized array. */
106 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
107 return;
110 else
111 retarray->data = internal_malloc_size (alloc_size);
113 else
115 if (rank != GFC_DESCRIPTOR_RANK (retarray))
116 runtime_error ("rank of return array incorrect in"
117 " MINVAL intrinsic: is %ld, should be %ld",
118 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
119 (long int) rank);
121 if (unlikely (compile_options.bounds_check))
122 bounds_ifunction_return ((array_t *) retarray, extent,
123 "return value", "MINVAL");
126 for (n = 0; n < rank; n++)
128 count[n] = 0;
129 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
130 if (extent[n] <= 0)
131 len = 0;
134 base = array->data;
135 dest = retarray->data;
137 continue_loop = 1;
138 while (continue_loop)
140 const GFC_REAL_4 * restrict src;
141 GFC_REAL_4 result;
142 src = base;
145 #if defined (GFC_REAL_4_INFINITY)
146 result = GFC_REAL_4_INFINITY;
147 #else
148 result = GFC_REAL_4_HUGE;
149 #endif
150 if (len <= 0)
151 *dest = GFC_REAL_4_HUGE;
152 else
154 for (n = 0; n < len; n++, src += delta)
157 #if defined (GFC_REAL_4_QUIET_NAN)
158 if (*src <= result)
159 break;
161 if (unlikely (n >= len))
162 result = GFC_REAL_4_QUIET_NAN;
163 else for (; n < len; n++, src += delta)
165 #endif
166 if (*src < result)
167 result = *src;
170 *dest = result;
173 /* Advance to the next element. */
174 count[0]++;
175 base += sstride[0];
176 dest += dstride[0];
177 n = 0;
178 while (count[n] == extent[n])
180 /* When we get to the end of a dimension, reset it and increment
181 the next dimension. */
182 count[n] = 0;
183 /* We could precalculate these products, but this is a less
184 frequently used path so probably not worth it. */
185 base -= sstride[n] * extent[n];
186 dest -= dstride[n] * extent[n];
187 n++;
188 if (n == rank)
190 /* Break out of the look. */
191 continue_loop = 0;
192 break;
194 else
196 count[n]++;
197 base += sstride[n];
198 dest += dstride[n];
205 extern void mminval_r4 (gfc_array_r4 * const restrict,
206 gfc_array_r4 * const restrict, const index_type * const restrict,
207 gfc_array_l1 * const restrict);
208 export_proto(mminval_r4);
210 void
211 mminval_r4 (gfc_array_r4 * const restrict retarray,
212 gfc_array_r4 * const restrict array,
213 const index_type * const restrict pdim,
214 gfc_array_l1 * const restrict mask)
216 index_type count[GFC_MAX_DIMENSIONS];
217 index_type extent[GFC_MAX_DIMENSIONS];
218 index_type sstride[GFC_MAX_DIMENSIONS];
219 index_type dstride[GFC_MAX_DIMENSIONS];
220 index_type mstride[GFC_MAX_DIMENSIONS];
221 GFC_REAL_4 * restrict dest;
222 const GFC_REAL_4 * restrict base;
223 const GFC_LOGICAL_1 * restrict mbase;
224 int rank;
225 int dim;
226 index_type n;
227 index_type len;
228 index_type delta;
229 index_type mdelta;
230 int mask_kind;
232 dim = (*pdim) - 1;
233 rank = GFC_DESCRIPTOR_RANK (array) - 1;
235 len = GFC_DESCRIPTOR_EXTENT(array,dim);
236 if (len <= 0)
237 return;
239 mbase = mask->data;
241 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
243 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
244 #ifdef HAVE_GFC_LOGICAL_16
245 || mask_kind == 16
246 #endif
248 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
249 else
250 runtime_error ("Funny sized logical array");
252 delta = GFC_DESCRIPTOR_STRIDE(array,dim);
253 mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim);
255 for (n = 0; n < dim; n++)
257 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
258 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
259 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
261 if (extent[n] < 0)
262 extent[n] = 0;
265 for (n = dim; n < rank; n++)
267 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n + 1);
268 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask, n + 1);
269 extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
271 if (extent[n] < 0)
272 extent[n] = 0;
275 if (retarray->data == NULL)
277 size_t alloc_size, str;
279 for (n = 0; n < rank; n++)
281 if (n == 0)
282 str = 1;
283 else
284 str= GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
286 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
290 alloc_size = sizeof (GFC_REAL_4) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
291 * extent[rank-1];
293 retarray->offset = 0;
294 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
296 if (alloc_size == 0)
298 /* Make sure we have a zero-sized array. */
299 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
300 return;
302 else
303 retarray->data = internal_malloc_size (alloc_size);
306 else
308 if (rank != GFC_DESCRIPTOR_RANK (retarray))
309 runtime_error ("rank of return array incorrect in MINVAL intrinsic");
311 if (unlikely (compile_options.bounds_check))
313 bounds_ifunction_return ((array_t *) retarray, extent,
314 "return value", "MINVAL");
315 bounds_equal_extents ((array_t *) mask, (array_t *) array,
316 "MASK argument", "MINVAL");
320 for (n = 0; n < rank; n++)
322 count[n] = 0;
323 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
324 if (extent[n] <= 0)
325 return;
328 dest = retarray->data;
329 base = array->data;
331 while (base)
333 const GFC_REAL_4 * restrict src;
334 const GFC_LOGICAL_1 * restrict msrc;
335 GFC_REAL_4 result;
336 src = base;
337 msrc = mbase;
340 #if defined (GFC_REAL_4_INFINITY)
341 result = GFC_REAL_4_INFINITY;
342 #else
343 result = GFC_REAL_4_HUGE;
344 #endif
345 #if defined (GFC_REAL_4_QUIET_NAN)
346 int non_empty_p = 0;
347 #endif
348 if (len <= 0)
349 *dest = GFC_REAL_4_HUGE;
350 else
352 for (n = 0; n < len; n++, src += delta, msrc += mdelta)
355 #if defined (GFC_REAL_4_INFINITY) || defined (GFC_REAL_4_QUIET_NAN)
356 if (*msrc)
358 #if defined (GFC_REAL_4_QUIET_NAN)
359 non_empty_p = 1;
360 if (*src <= result)
361 #endif
362 break;
365 if (unlikely (n >= len))
367 #if defined (GFC_REAL_4_QUIET_NAN)
368 result = non_empty_p ? GFC_REAL_4_QUIET_NAN : GFC_REAL_4_HUGE;
369 #else
370 result = GFC_REAL_4_HUGE;
371 #endif
373 else for (; n < len; n++, src += delta, msrc += mdelta)
375 #endif
376 if (*msrc && *src < result)
377 result = *src;
379 *dest = result;
382 /* Advance to the next element. */
383 count[0]++;
384 base += sstride[0];
385 mbase += mstride[0];
386 dest += dstride[0];
387 n = 0;
388 while (count[n] == extent[n])
390 /* When we get to the end of a dimension, reset it and increment
391 the next dimension. */
392 count[n] = 0;
393 /* We could precalculate these products, but this is a less
394 frequently used path so probably not worth it. */
395 base -= sstride[n] * extent[n];
396 mbase -= mstride[n] * extent[n];
397 dest -= dstride[n] * extent[n];
398 n++;
399 if (n == rank)
401 /* Break out of the look. */
402 base = NULL;
403 break;
405 else
407 count[n]++;
408 base += sstride[n];
409 mbase += mstride[n];
410 dest += dstride[n];
417 extern void sminval_r4 (gfc_array_r4 * const restrict,
418 gfc_array_r4 * const restrict, const index_type * const restrict,
419 GFC_LOGICAL_4 *);
420 export_proto(sminval_r4);
422 void
423 sminval_r4 (gfc_array_r4 * const restrict retarray,
424 gfc_array_r4 * const restrict array,
425 const index_type * const restrict pdim,
426 GFC_LOGICAL_4 * mask)
428 index_type count[GFC_MAX_DIMENSIONS];
429 index_type extent[GFC_MAX_DIMENSIONS];
430 index_type dstride[GFC_MAX_DIMENSIONS];
431 GFC_REAL_4 * restrict dest;
432 index_type rank;
433 index_type n;
434 index_type dim;
437 if (*mask)
439 minval_r4 (retarray, array, pdim);
440 return;
442 /* Make dim zero based to avoid confusion. */
443 dim = (*pdim) - 1;
444 rank = GFC_DESCRIPTOR_RANK (array) - 1;
446 for (n = 0; n < dim; n++)
448 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
450 if (extent[n] <= 0)
451 extent[n] = 0;
454 for (n = dim; n < rank; n++)
456 extent[n] =
457 GFC_DESCRIPTOR_EXTENT(array,n + 1);
459 if (extent[n] <= 0)
460 extent[n] = 0;
463 if (retarray->data == NULL)
465 size_t alloc_size, str;
467 for (n = 0; n < rank; n++)
469 if (n == 0)
470 str = 1;
471 else
472 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
474 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
478 retarray->offset = 0;
479 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
481 alloc_size = sizeof (GFC_REAL_4) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
482 * extent[rank-1];
484 if (alloc_size == 0)
486 /* Make sure we have a zero-sized array. */
487 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
488 return;
490 else
491 retarray->data = internal_malloc_size (alloc_size);
493 else
495 if (rank != GFC_DESCRIPTOR_RANK (retarray))
496 runtime_error ("rank of return array incorrect in"
497 " MINVAL intrinsic: is %ld, should be %ld",
498 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
499 (long int) rank);
501 if (unlikely (compile_options.bounds_check))
503 for (n=0; n < rank; n++)
505 index_type ret_extent;
507 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
508 if (extent[n] != ret_extent)
509 runtime_error ("Incorrect extent in return value of"
510 " MINVAL intrinsic in dimension %ld:"
511 " is %ld, should be %ld", (long int) n + 1,
512 (long int) ret_extent, (long int) extent[n]);
517 for (n = 0; n < rank; n++)
519 count[n] = 0;
520 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
523 dest = retarray->data;
525 while(1)
527 *dest = GFC_REAL_4_HUGE;
528 count[0]++;
529 dest += dstride[0];
530 n = 0;
531 while (count[n] == extent[n])
533 /* When we get to the end of a dimension, reset it and increment
534 the next dimension. */
535 count[n] = 0;
536 /* We could precalculate these products, but this is a less
537 frequently used path so probably not worth it. */
538 dest -= dstride[n] * extent[n];
539 n++;
540 if (n == rank)
541 return;
542 else
544 count[n]++;
545 dest += dstride[n];
551 #endif