* doc/tm.texi (MALLOC_ABI_ALIGNMENT): New macro. Alignment, in bits,
[official-gcc.git] / libgfortran / generated / minval_i4.c
blob96bfe32bde6c6cc46c777271b9a08473b0828a98
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_INTEGER_4) && defined (HAVE_GFC_INTEGER_4)
39 extern void minval_i4 (gfc_array_i4 * const restrict,
40 gfc_array_i4 * const restrict, const index_type * const restrict);
41 export_proto(minval_i4);
43 void
44 minval_i4 (gfc_array_i4 * const restrict retarray,
45 gfc_array_i4 * 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_INTEGER_4 * restrict base;
53 GFC_INTEGER_4 * restrict dest;
54 index_type rank;
55 index_type n;
56 index_type len;
57 index_type delta;
58 index_type dim;
59 int continue_loop;
61 /* Make dim zero based to avoid confusion. */
62 dim = (*pdim) - 1;
63 rank = GFC_DESCRIPTOR_RANK (array) - 1;
65 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
66 if (len < 0)
67 len = 0;
68 delta = array->dim[dim].stride;
70 for (n = 0; n < dim; n++)
72 sstride[n] = array->dim[n].stride;
73 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
75 if (extent[n] < 0)
76 extent[n] = 0;
78 for (n = dim; n < rank; n++)
80 sstride[n] = array->dim[n + 1].stride;
81 extent[n] =
82 array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
84 if (extent[n] < 0)
85 extent[n] = 0;
88 if (retarray->data == NULL)
90 size_t alloc_size;
92 for (n = 0; n < rank; n++)
94 retarray->dim[n].lbound = 0;
95 retarray->dim[n].ubound = extent[n]-1;
96 if (n == 0)
97 retarray->dim[n].stride = 1;
98 else
99 retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
102 retarray->offset = 0;
103 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
105 alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
106 * extent[rank-1];
108 if (alloc_size == 0)
110 /* Make sure we have a zero-sized array. */
111 retarray->dim[0].lbound = 0;
112 retarray->dim[0].ubound = -1;
113 return;
115 else
116 retarray->data = internal_malloc_size (alloc_size);
118 else
120 if (rank != GFC_DESCRIPTOR_RANK (retarray))
121 runtime_error ("rank of return array incorrect in"
122 " MINVAL intrinsic: is %ld, should be %ld",
123 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
124 (long int) rank);
126 if (compile_options.bounds_check)
128 for (n=0; n < rank; n++)
130 index_type ret_extent;
132 ret_extent = retarray->dim[n].ubound + 1
133 - retarray->dim[n].lbound;
134 if (extent[n] != ret_extent)
135 runtime_error ("Incorrect extent in return value of"
136 " MINVAL intrinsic in dimension %ld:"
137 " is %ld, should be %ld", (long int) n + 1,
138 (long int) ret_extent, (long int) extent[n]);
143 for (n = 0; n < rank; n++)
145 count[n] = 0;
146 dstride[n] = retarray->dim[n].stride;
147 if (extent[n] <= 0)
148 len = 0;
151 base = array->data;
152 dest = retarray->data;
154 continue_loop = 1;
155 while (continue_loop)
157 const GFC_INTEGER_4 * restrict src;
158 GFC_INTEGER_4 result;
159 src = base;
162 result = GFC_INTEGER_4_HUGE;
163 if (len <= 0)
164 *dest = GFC_INTEGER_4_HUGE;
165 else
167 for (n = 0; n < len; n++, src += delta)
170 if (*src < result)
171 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 look. */
194 continue_loop = 0;
195 break;
197 else
199 count[n]++;
200 base += sstride[n];
201 dest += dstride[n];
208 extern void mminval_i4 (gfc_array_i4 * const restrict,
209 gfc_array_i4 * const restrict, const index_type * const restrict,
210 gfc_array_l1 * const restrict);
211 export_proto(mminval_i4);
213 void
214 mminval_i4 (gfc_array_i4 * const restrict retarray,
215 gfc_array_i4 * 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_4 * restrict dest;
225 const GFC_INTEGER_4 * restrict base;
226 const GFC_LOGICAL_1 * restrict mbase;
227 int rank;
228 int 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;
238 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
239 if (len <= 0)
240 return;
242 mbase = mask->data;
244 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
246 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
247 #ifdef HAVE_GFC_LOGICAL_16
248 || mask_kind == 16
249 #endif
251 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
252 else
253 runtime_error ("Funny sized logical array");
255 delta = array->dim[dim].stride;
256 mdelta = mask->dim[dim].stride * mask_kind;
258 for (n = 0; n < dim; n++)
260 sstride[n] = array->dim[n].stride;
261 mstride[n] = mask->dim[n].stride * mask_kind;
262 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
264 if (extent[n] < 0)
265 extent[n] = 0;
268 for (n = dim; n < rank; n++)
270 sstride[n] = array->dim[n + 1].stride;
271 mstride[n] = mask->dim[n + 1].stride * mask_kind;
272 extent[n] =
273 array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
275 if (extent[n] < 0)
276 extent[n] = 0;
279 if (retarray->data == NULL)
281 size_t alloc_size;
283 for (n = 0; n < rank; n++)
285 retarray->dim[n].lbound = 0;
286 retarray->dim[n].ubound = extent[n]-1;
287 if (n == 0)
288 retarray->dim[n].stride = 1;
289 else
290 retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
293 alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
294 * extent[rank-1];
296 retarray->offset = 0;
297 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
299 if (alloc_size == 0)
301 /* Make sure we have a zero-sized array. */
302 retarray->dim[0].lbound = 0;
303 retarray->dim[0].ubound = -1;
304 return;
306 else
307 retarray->data = internal_malloc_size (alloc_size);
310 else
312 if (rank != GFC_DESCRIPTOR_RANK (retarray))
313 runtime_error ("rank of return array incorrect in MINVAL intrinsic");
315 if (compile_options.bounds_check)
317 for (n=0; n < rank; n++)
319 index_type ret_extent;
321 ret_extent = retarray->dim[n].ubound + 1
322 - retarray->dim[n].lbound;
323 if (extent[n] != ret_extent)
324 runtime_error ("Incorrect extent in return value of"
325 " MINVAL intrinsic in dimension %ld:"
326 " is %ld, should be %ld", (long int) n + 1,
327 (long int) ret_extent, (long int) extent[n]);
329 for (n=0; n<= rank; n++)
331 index_type mask_extent, array_extent;
333 array_extent = array->dim[n].ubound + 1 - array->dim[n].lbound;
334 mask_extent = mask->dim[n].ubound + 1 - mask->dim[n].lbound;
335 if (array_extent != mask_extent)
336 runtime_error ("Incorrect extent in MASK argument of"
337 " MINVAL intrinsic in dimension %ld:"
338 " is %ld, should be %ld", (long int) n + 1,
339 (long int) mask_extent, (long int) array_extent);
344 for (n = 0; n < rank; n++)
346 count[n] = 0;
347 dstride[n] = retarray->dim[n].stride;
348 if (extent[n] <= 0)
349 return;
352 dest = retarray->data;
353 base = array->data;
355 while (base)
357 const GFC_INTEGER_4 * restrict src;
358 const GFC_LOGICAL_1 * restrict msrc;
359 GFC_INTEGER_4 result;
360 src = base;
361 msrc = mbase;
364 result = GFC_INTEGER_4_HUGE;
365 if (len <= 0)
366 *dest = GFC_INTEGER_4_HUGE;
367 else
369 for (n = 0; n < len; n++, src += delta, msrc += mdelta)
372 if (*msrc && *src < result)
373 result = *src;
375 *dest = result;
378 /* Advance to the next element. */
379 count[0]++;
380 base += sstride[0];
381 mbase += mstride[0];
382 dest += dstride[0];
383 n = 0;
384 while (count[n] == extent[n])
386 /* When we get to the end of a dimension, reset it and increment
387 the next dimension. */
388 count[n] = 0;
389 /* We could precalculate these products, but this is a less
390 frequently used path so probably not worth it. */
391 base -= sstride[n] * extent[n];
392 mbase -= mstride[n] * extent[n];
393 dest -= dstride[n] * extent[n];
394 n++;
395 if (n == rank)
397 /* Break out of the look. */
398 base = NULL;
399 break;
401 else
403 count[n]++;
404 base += sstride[n];
405 mbase += mstride[n];
406 dest += dstride[n];
413 extern void sminval_i4 (gfc_array_i4 * const restrict,
414 gfc_array_i4 * const restrict, const index_type * const restrict,
415 GFC_LOGICAL_4 *);
416 export_proto(sminval_i4);
418 void
419 sminval_i4 (gfc_array_i4 * const restrict retarray,
420 gfc_array_i4 * const restrict array,
421 const index_type * const restrict pdim,
422 GFC_LOGICAL_4 * mask)
424 index_type count[GFC_MAX_DIMENSIONS];
425 index_type extent[GFC_MAX_DIMENSIONS];
426 index_type sstride[GFC_MAX_DIMENSIONS];
427 index_type dstride[GFC_MAX_DIMENSIONS];
428 GFC_INTEGER_4 * restrict dest;
429 index_type rank;
430 index_type n;
431 index_type dim;
434 if (*mask)
436 minval_i4 (retarray, array, pdim);
437 return;
439 /* Make dim zero based to avoid confusion. */
440 dim = (*pdim) - 1;
441 rank = GFC_DESCRIPTOR_RANK (array) - 1;
443 for (n = 0; n < dim; n++)
445 sstride[n] = array->dim[n].stride;
446 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
448 if (extent[n] <= 0)
449 extent[n] = 0;
452 for (n = dim; n < rank; n++)
454 sstride[n] = array->dim[n + 1].stride;
455 extent[n] =
456 array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
458 if (extent[n] <= 0)
459 extent[n] = 0;
462 if (retarray->data == NULL)
464 size_t alloc_size;
466 for (n = 0; n < rank; n++)
468 retarray->dim[n].lbound = 0;
469 retarray->dim[n].ubound = extent[n]-1;
470 if (n == 0)
471 retarray->dim[n].stride = 1;
472 else
473 retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
476 retarray->offset = 0;
477 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
479 alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
480 * extent[rank-1];
482 if (alloc_size == 0)
484 /* Make sure we have a zero-sized array. */
485 retarray->dim[0].lbound = 0;
486 retarray->dim[0].ubound = -1;
487 return;
489 else
490 retarray->data = internal_malloc_size (alloc_size);
492 else
494 if (rank != GFC_DESCRIPTOR_RANK (retarray))
495 runtime_error ("rank of return array incorrect in"
496 " MINVAL intrinsic: is %ld, should be %ld",
497 (long int) (GFC_DESCRIPTOR_RANK (retarray)),
498 (long int) rank);
500 if (compile_options.bounds_check)
502 for (n=0; n < rank; n++)
504 index_type ret_extent;
506 ret_extent = retarray->dim[n].ubound + 1
507 - retarray->dim[n].lbound;
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] = retarray->dim[n].stride;
523 dest = retarray->data;
525 while(1)
527 *dest = GFC_INTEGER_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