2007-02-13 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / libgfortran / generated / minval_i4.c
blob9bea78aa57c44eaccb3933956146a6ec4bdd93b3
1 /* Implementation of the MINVAL intrinsic
2 Copyright 2002 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 "config.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <float.h>
35 #include "libgfortran.h"
38 #if defined (HAVE_GFC_INTEGER_4) && defined (HAVE_GFC_INTEGER_4)
41 extern void minval_i4 (gfc_array_i4 * const restrict,
42 gfc_array_i4 * const restrict, const index_type * const restrict);
43 export_proto(minval_i4);
45 void
46 minval_i4 (gfc_array_i4 * const restrict retarray,
47 gfc_array_i4 * const restrict array,
48 const index_type * const restrict pdim)
50 index_type count[GFC_MAX_DIMENSIONS];
51 index_type extent[GFC_MAX_DIMENSIONS];
52 index_type sstride[GFC_MAX_DIMENSIONS];
53 index_type dstride[GFC_MAX_DIMENSIONS];
54 const GFC_INTEGER_4 * restrict base;
55 GFC_INTEGER_4 * restrict dest;
56 index_type rank;
57 index_type n;
58 index_type len;
59 index_type delta;
60 index_type dim;
62 /* Make dim zero based to avoid confusion. */
63 dim = (*pdim) - 1;
64 rank = GFC_DESCRIPTOR_RANK (array) - 1;
66 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
67 delta = array->dim[dim].stride;
69 for (n = 0; n < dim; n++)
71 sstride[n] = array->dim[n].stride;
72 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
74 if (extent[n] < 0)
75 extent[n] = 0;
77 for (n = dim; n < rank; n++)
79 sstride[n] = array->dim[n + 1].stride;
80 extent[n] =
81 array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
83 if (extent[n] < 0)
84 extent[n] = 0;
87 if (retarray->data == NULL)
89 size_t alloc_size;
91 for (n = 0; n < rank; n++)
93 retarray->dim[n].lbound = 0;
94 retarray->dim[n].ubound = extent[n]-1;
95 if (n == 0)
96 retarray->dim[n].stride = 1;
97 else
98 retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
101 retarray->offset = 0;
102 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
104 alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
105 * extent[rank-1];
107 if (alloc_size == 0)
109 /* Make sure we have a zero-sized array. */
110 retarray->dim[0].lbound = 0;
111 retarray->dim[0].ubound = -1;
112 return;
114 else
115 retarray->data = internal_malloc_size (alloc_size);
117 else
119 if (rank != GFC_DESCRIPTOR_RANK (retarray))
120 runtime_error ("rank of return array incorrect");
123 for (n = 0; n < rank; n++)
125 count[n] = 0;
126 dstride[n] = retarray->dim[n].stride;
127 if (extent[n] <= 0)
128 len = 0;
131 base = array->data;
132 dest = retarray->data;
134 while (base)
136 const GFC_INTEGER_4 * restrict src;
137 GFC_INTEGER_4 result;
138 src = base;
141 result = GFC_INTEGER_4_HUGE;
142 if (len <= 0)
143 *dest = GFC_INTEGER_4_HUGE;
144 else
146 for (n = 0; n < len; n++, src += delta)
149 if (*src < result)
150 result = *src;
152 *dest = result;
155 /* Advance to the next element. */
156 count[0]++;
157 base += sstride[0];
158 dest += dstride[0];
159 n = 0;
160 while (count[n] == extent[n])
162 /* When we get to the end of a dimension, reset it and increment
163 the next dimension. */
164 count[n] = 0;
165 /* We could precalculate these products, but this is a less
166 frequently used path so probably not worth it. */
167 base -= sstride[n] * extent[n];
168 dest -= dstride[n] * extent[n];
169 n++;
170 if (n == rank)
172 /* Break out of the look. */
173 base = NULL;
174 break;
176 else
178 count[n]++;
179 base += sstride[n];
180 dest += dstride[n];
187 extern void mminval_i4 (gfc_array_i4 * const restrict,
188 gfc_array_i4 * const restrict, const index_type * const restrict,
189 gfc_array_l4 * const restrict);
190 export_proto(mminval_i4);
192 void
193 mminval_i4 (gfc_array_i4 * const restrict retarray,
194 gfc_array_i4 * const restrict array,
195 const index_type * const restrict pdim,
196 gfc_array_l4 * const restrict mask)
198 index_type count[GFC_MAX_DIMENSIONS];
199 index_type extent[GFC_MAX_DIMENSIONS];
200 index_type sstride[GFC_MAX_DIMENSIONS];
201 index_type dstride[GFC_MAX_DIMENSIONS];
202 index_type mstride[GFC_MAX_DIMENSIONS];
203 GFC_INTEGER_4 * restrict dest;
204 const GFC_INTEGER_4 * restrict base;
205 const GFC_LOGICAL_4 * restrict mbase;
206 int rank;
207 int dim;
208 index_type n;
209 index_type len;
210 index_type delta;
211 index_type mdelta;
213 dim = (*pdim) - 1;
214 rank = GFC_DESCRIPTOR_RANK (array) - 1;
216 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
217 if (len <= 0)
218 return;
219 delta = array->dim[dim].stride;
220 mdelta = mask->dim[dim].stride;
222 for (n = 0; n < dim; n++)
224 sstride[n] = array->dim[n].stride;
225 mstride[n] = mask->dim[n].stride;
226 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
228 if (extent[n] < 0)
229 extent[n] = 0;
232 for (n = dim; n < rank; n++)
234 sstride[n] = array->dim[n + 1].stride;
235 mstride[n] = mask->dim[n + 1].stride;
236 extent[n] =
237 array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
239 if (extent[n] < 0)
240 extent[n] = 0;
243 if (retarray->data == NULL)
245 size_t alloc_size;
247 for (n = 0; n < rank; n++)
249 retarray->dim[n].lbound = 0;
250 retarray->dim[n].ubound = extent[n]-1;
251 if (n == 0)
252 retarray->dim[n].stride = 1;
253 else
254 retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
257 alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
258 * extent[rank-1];
260 retarray->offset = 0;
261 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
263 if (alloc_size == 0)
265 /* Make sure we have a zero-sized array. */
266 retarray->dim[0].lbound = 0;
267 retarray->dim[0].ubound = -1;
268 return;
270 else
271 retarray->data = internal_malloc_size (alloc_size);
274 else
276 if (rank != GFC_DESCRIPTOR_RANK (retarray))
277 runtime_error ("rank of return array incorrect");
280 for (n = 0; n < rank; n++)
282 count[n] = 0;
283 dstride[n] = retarray->dim[n].stride;
284 if (extent[n] <= 0)
285 return;
288 dest = retarray->data;
289 base = array->data;
290 mbase = mask->data;
292 if (GFC_DESCRIPTOR_SIZE (mask) != 4)
294 /* This allows the same loop to be used for all logical types. */
295 assert (GFC_DESCRIPTOR_SIZE (mask) == 8);
296 for (n = 0; n < rank; n++)
297 mstride[n] <<= 1;
298 mdelta <<= 1;
299 mbase = (GFOR_POINTER_L8_TO_L4 (mbase));
302 while (base)
304 const GFC_INTEGER_4 * restrict src;
305 const GFC_LOGICAL_4 * restrict msrc;
306 GFC_INTEGER_4 result;
307 src = base;
308 msrc = mbase;
311 result = GFC_INTEGER_4_HUGE;
312 if (len <= 0)
313 *dest = GFC_INTEGER_4_HUGE;
314 else
316 for (n = 0; n < len; n++, src += delta, msrc += mdelta)
319 if (*msrc && *src < result)
320 result = *src;
322 *dest = result;
325 /* Advance to the next element. */
326 count[0]++;
327 base += sstride[0];
328 mbase += mstride[0];
329 dest += dstride[0];
330 n = 0;
331 while (count[n] == extent[n])
333 /* When we get to the end of a dimension, reset it and increment
334 the next dimension. */
335 count[n] = 0;
336 /* We could precalculate these products, but this is a less
337 frequently used path so probably not worth it. */
338 base -= sstride[n] * extent[n];
339 mbase -= mstride[n] * extent[n];
340 dest -= dstride[n] * extent[n];
341 n++;
342 if (n == rank)
344 /* Break out of the look. */
345 base = NULL;
346 break;
348 else
350 count[n]++;
351 base += sstride[n];
352 mbase += mstride[n];
353 dest += dstride[n];
360 extern void sminval_i4 (gfc_array_i4 * const restrict,
361 gfc_array_i4 * const restrict, const index_type * const restrict,
362 GFC_LOGICAL_4 *);
363 export_proto(sminval_i4);
365 void
366 sminval_i4 (gfc_array_i4 * const restrict retarray,
367 gfc_array_i4 * const restrict array,
368 const index_type * const restrict pdim,
369 GFC_LOGICAL_4 * mask)
371 index_type rank;
372 index_type n;
373 index_type dstride;
374 GFC_INTEGER_4 *dest;
376 if (*mask)
378 minval_i4 (retarray, array, pdim);
379 return;
381 rank = GFC_DESCRIPTOR_RANK (array);
382 if (rank <= 0)
383 runtime_error ("Rank of array needs to be > 0");
385 if (retarray->data == NULL)
387 retarray->dim[0].lbound = 0;
388 retarray->dim[0].ubound = rank-1;
389 retarray->dim[0].stride = 1;
390 retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
391 retarray->offset = 0;
392 retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
394 else
396 if (GFC_DESCRIPTOR_RANK (retarray) != 1)
397 runtime_error ("rank of return array does not equal 1");
399 if (retarray->dim[0].ubound + 1 - retarray->dim[0].lbound != rank)
400 runtime_error ("dimension of return array incorrect");
403 dstride = retarray->dim[0].stride;
404 dest = retarray->data;
406 for (n = 0; n < rank; n++)
407 dest[n * dstride] = GFC_INTEGER_4_HUGE ;
410 #endif