Merge -r 127928:132243 from trunk
[official-gcc.git] / libgfortran / generated / minval_r10.c
blobf4d467c0d992f8fcba563022eefcf2f579960ac6
1 /* Implementation of the MINVAL intrinsic
2 Copyright 2002, 2007 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 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 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
31 #include "libgfortran.h"
32 #include <stdlib.h>
33 #include <assert.h>
36 #if defined (HAVE_GFC_REAL_10) && defined (HAVE_GFC_REAL_10)
39 extern void minval_r10 (gfc_array_r10 * const restrict,
40 gfc_array_r10 * const restrict, const index_type * const restrict);
41 export_proto(minval_r10);
43 void
44 minval_r10 (gfc_array_r10 * const restrict retarray,
45 gfc_array_r10 * const restrict array,
46 const index_type * const restrict pdim)
48 index_type count[GFC_MAX_DIMENSIONS];
49 index_type extent[GFC_MAX_DIMENSIONS];
50 index_type sstride[GFC_MAX_DIMENSIONS];
51 index_type dstride[GFC_MAX_DIMENSIONS];
52 const GFC_REAL_10 * restrict base;
53 GFC_REAL_10 * restrict dest;
54 index_type rank;
55 index_type n;
56 index_type len;
57 index_type delta;
58 index_type dim;
60 /* Make dim zero based to avoid confusion. */
61 dim = (*pdim) - 1;
62 rank = GFC_DESCRIPTOR_RANK (array) - 1;
64 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
65 delta = array->dim[dim].stride;
67 for (n = 0; n < dim; n++)
69 sstride[n] = array->dim[n].stride;
70 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
72 if (extent[n] < 0)
73 extent[n] = 0;
75 for (n = dim; n < rank; n++)
77 sstride[n] = array->dim[n + 1].stride;
78 extent[n] =
79 array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
81 if (extent[n] < 0)
82 extent[n] = 0;
85 if (retarray->data == NULL)
87 size_t alloc_size;
89 for (n = 0; n < rank; n++)
91 retarray->dim[n].lbound = 0;
92 retarray->dim[n].ubound = extent[n]-1;
93 if (n == 0)
94 retarray->dim[n].stride = 1;
95 else
96 retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
99 retarray->offset = 0;
100 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
102 alloc_size = sizeof (GFC_REAL_10) * retarray->dim[rank-1].stride
103 * extent[rank-1];
105 if (alloc_size == 0)
107 /* Make sure we have a zero-sized array. */
108 retarray->dim[0].lbound = 0;
109 retarray->dim[0].ubound = -1;
110 return;
112 else
113 retarray->data = internal_malloc_size (alloc_size);
115 else
117 if (rank != GFC_DESCRIPTOR_RANK (retarray))
118 runtime_error ("rank of return array incorrect in"
119 " MINVAL intrinsic: is %ld, should be %ld",
120 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
121 (long int) rank);
123 if (compile_options.bounds_check)
125 for (n=0; n < rank; n++)
127 index_type ret_extent;
129 ret_extent = retarray->dim[n].ubound + 1
130 - retarray->dim[n].lbound;
131 if (extent[n] != ret_extent)
132 runtime_error ("Incorrect extent in return value of"
133 " MINVAL intrinsic in dimension %ld:"
134 " is %ld, should be %ld", (long int) n + 1,
135 (long int) ret_extent, (long int) extent[n]);
140 for (n = 0; n < rank; n++)
142 count[n] = 0;
143 dstride[n] = retarray->dim[n].stride;
144 if (extent[n] <= 0)
145 len = 0;
148 base = array->data;
149 dest = retarray->data;
151 while (base)
153 const GFC_REAL_10 * restrict src;
154 GFC_REAL_10 result;
155 src = base;
158 result = GFC_REAL_10_HUGE;
159 if (len <= 0)
160 *dest = GFC_REAL_10_HUGE;
161 else
163 for (n = 0; n < len; n++, src += delta)
166 if (*src < result)
167 result = *src;
169 *dest = result;
172 /* Advance to the next element. */
173 count[0]++;
174 base += sstride[0];
175 dest += dstride[0];
176 n = 0;
177 while (count[n] == extent[n])
179 /* When we get to the end of a dimension, reset it and increment
180 the next dimension. */
181 count[n] = 0;
182 /* We could precalculate these products, but this is a less
183 frequently used path so probably not worth it. */
184 base -= sstride[n] * extent[n];
185 dest -= dstride[n] * extent[n];
186 n++;
187 if (n == rank)
189 /* Break out of the look. */
190 base = NULL;
191 break;
193 else
195 count[n]++;
196 base += sstride[n];
197 dest += dstride[n];
204 extern void mminval_r10 (gfc_array_r10 * const restrict,
205 gfc_array_r10 * const restrict, const index_type * const restrict,
206 gfc_array_l1 * const restrict);
207 export_proto(mminval_r10);
209 void
210 mminval_r10 (gfc_array_r10 * const restrict retarray,
211 gfc_array_r10 * const restrict array,
212 const index_type * const restrict pdim,
213 gfc_array_l1 * const restrict mask)
215 index_type count[GFC_MAX_DIMENSIONS];
216 index_type extent[GFC_MAX_DIMENSIONS];
217 index_type sstride[GFC_MAX_DIMENSIONS];
218 index_type dstride[GFC_MAX_DIMENSIONS];
219 index_type mstride[GFC_MAX_DIMENSIONS];
220 GFC_REAL_10 * restrict dest;
221 const GFC_REAL_10 * restrict base;
222 const GFC_LOGICAL_1 * restrict mbase;
223 int rank;
224 int dim;
225 index_type n;
226 index_type len;
227 index_type delta;
228 index_type mdelta;
229 int mask_kind;
231 dim = (*pdim) - 1;
232 rank = GFC_DESCRIPTOR_RANK (array) - 1;
234 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
235 if (len <= 0)
236 return;
238 mbase = mask->data;
240 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
242 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
243 #ifdef HAVE_GFC_LOGICAL_16
244 || mask_kind == 16
245 #endif
247 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
248 else
249 runtime_error ("Funny sized logical array");
251 delta = array->dim[dim].stride;
252 mdelta = mask->dim[dim].stride * mask_kind;
254 for (n = 0; n < dim; n++)
256 sstride[n] = array->dim[n].stride;
257 mstride[n] = mask->dim[n].stride * mask_kind;
258 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
260 if (extent[n] < 0)
261 extent[n] = 0;
264 for (n = dim; n < rank; n++)
266 sstride[n] = array->dim[n + 1].stride;
267 mstride[n] = mask->dim[n + 1].stride * mask_kind;
268 extent[n] =
269 array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
271 if (extent[n] < 0)
272 extent[n] = 0;
275 if (retarray->data == NULL)
277 size_t alloc_size;
279 for (n = 0; n < rank; n++)
281 retarray->dim[n].lbound = 0;
282 retarray->dim[n].ubound = extent[n]-1;
283 if (n == 0)
284 retarray->dim[n].stride = 1;
285 else
286 retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
289 alloc_size = sizeof (GFC_REAL_10) * retarray->dim[rank-1].stride
290 * extent[rank-1];
292 retarray->offset = 0;
293 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
295 if (alloc_size == 0)
297 /* Make sure we have a zero-sized array. */
298 retarray->dim[0].lbound = 0;
299 retarray->dim[0].ubound = -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 (compile_options.bounds_check)
313 for (n=0; n < rank; n++)
315 index_type ret_extent;
317 ret_extent = retarray->dim[n].ubound + 1
318 - retarray->dim[n].lbound;
319 if (extent[n] != ret_extent)
320 runtime_error ("Incorrect extent in return value of"
321 " MINVAL intrinsic in dimension %ld:"
322 " is %ld, should be %ld", (long int) n + 1,
323 (long int) ret_extent, (long int) extent[n]);
325 for (n=0; n<= rank; n++)
327 index_type mask_extent, array_extent;
329 array_extent = array->dim[n].ubound + 1 - array->dim[n].lbound;
330 mask_extent = mask->dim[n].ubound + 1 - mask->dim[n].lbound;
331 if (array_extent != mask_extent)
332 runtime_error ("Incorrect extent in MASK argument of"
333 " MINVAL intrinsic in dimension %ld:"
334 " is %ld, should be %ld", (long int) n + 1,
335 (long int) mask_extent, (long int) array_extent);
340 for (n = 0; n < rank; n++)
342 count[n] = 0;
343 dstride[n] = retarray->dim[n].stride;
344 if (extent[n] <= 0)
345 return;
348 dest = retarray->data;
349 base = array->data;
351 while (base)
353 const GFC_REAL_10 * restrict src;
354 const GFC_LOGICAL_1 * restrict msrc;
355 GFC_REAL_10 result;
356 src = base;
357 msrc = mbase;
360 result = GFC_REAL_10_HUGE;
361 if (len <= 0)
362 *dest = GFC_REAL_10_HUGE;
363 else
365 for (n = 0; n < len; n++, src += delta, msrc += mdelta)
368 if (*msrc && *src < result)
369 result = *src;
371 *dest = result;
374 /* Advance to the next element. */
375 count[0]++;
376 base += sstride[0];
377 mbase += mstride[0];
378 dest += dstride[0];
379 n = 0;
380 while (count[n] == extent[n])
382 /* When we get to the end of a dimension, reset it and increment
383 the next dimension. */
384 count[n] = 0;
385 /* We could precalculate these products, but this is a less
386 frequently used path so probably not worth it. */
387 base -= sstride[n] * extent[n];
388 mbase -= mstride[n] * extent[n];
389 dest -= dstride[n] * extent[n];
390 n++;
391 if (n == rank)
393 /* Break out of the look. */
394 base = NULL;
395 break;
397 else
399 count[n]++;
400 base += sstride[n];
401 mbase += mstride[n];
402 dest += dstride[n];
409 extern void sminval_r10 (gfc_array_r10 * const restrict,
410 gfc_array_r10 * const restrict, const index_type * const restrict,
411 GFC_LOGICAL_4 *);
412 export_proto(sminval_r10);
414 void
415 sminval_r10 (gfc_array_r10 * const restrict retarray,
416 gfc_array_r10 * const restrict array,
417 const index_type * const restrict pdim,
418 GFC_LOGICAL_4 * mask)
420 index_type rank;
421 index_type n;
422 index_type dstride;
423 GFC_REAL_10 *dest;
425 if (*mask)
427 minval_r10 (retarray, array, pdim);
428 return;
430 rank = GFC_DESCRIPTOR_RANK (array);
431 if (rank <= 0)
432 runtime_error ("Rank of array needs to be > 0");
434 if (retarray->data == NULL)
436 retarray->dim[0].lbound = 0;
437 retarray->dim[0].ubound = rank-1;
438 retarray->dim[0].stride = 1;
439 retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
440 retarray->offset = 0;
441 retarray->data = internal_malloc_size (sizeof (GFC_REAL_10) * rank);
443 else
445 if (compile_options.bounds_check)
447 int ret_rank;
448 index_type ret_extent;
450 ret_rank = GFC_DESCRIPTOR_RANK (retarray);
451 if (ret_rank != 1)
452 runtime_error ("rank of return array in MINVAL intrinsic"
453 " should be 1, is %ld", (long int) ret_rank);
455 ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
456 if (ret_extent != rank)
457 runtime_error ("dimension of return array incorrect");
460 dstride = retarray->dim[0].stride;
461 dest = retarray->data;
463 for (n = 0; n < rank; n++)
464 dest[n * dstride] = GFC_REAL_10_HUGE ;
467 #endif