Merged with mainline at revision 128810.
[official-gcc.git] / libgfortran / generated / minloc0_16_i4.c
blob67062d3d9957b2ca5865089839d961cd8087f8e9
1 /* Implementation of the MINLOC 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>
34 #include <limits.h>
37 #if defined (HAVE_GFC_INTEGER_4) && defined (HAVE_GFC_INTEGER_16)
40 extern void minloc0_16_i4 (gfc_array_i16 * const restrict retarray,
41 gfc_array_i4 * const restrict array);
42 export_proto(minloc0_16_i4);
44 void
45 minloc0_16_i4 (gfc_array_i16 * const restrict retarray,
46 gfc_array_i4 * const restrict array)
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;
52 const GFC_INTEGER_4 *base;
53 GFC_INTEGER_16 *dest;
54 index_type rank;
55 index_type n;
57 rank = GFC_DESCRIPTOR_RANK (array);
58 if (rank <= 0)
59 runtime_error ("Rank of array needs to be > 0");
61 if (retarray->data == NULL)
63 retarray->dim[0].lbound = 0;
64 retarray->dim[0].ubound = rank-1;
65 retarray->dim[0].stride = 1;
66 retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
67 retarray->offset = 0;
68 retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
70 else
72 if (GFC_DESCRIPTOR_RANK (retarray) != 1)
73 runtime_error ("rank of return array does not equal 1");
75 if (retarray->dim[0].ubound + 1 - retarray->dim[0].lbound != rank)
76 runtime_error ("dimension of return array incorrect");
79 dstride = retarray->dim[0].stride;
80 dest = retarray->data;
81 for (n = 0; n < rank; n++)
83 sstride[n] = array->dim[n].stride;
84 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
85 count[n] = 0;
86 if (extent[n] <= 0)
88 /* Set the return value. */
89 for (n = 0; n < rank; n++)
90 dest[n * dstride] = 0;
91 return;
95 base = array->data;
97 /* Initialize the return value. */
98 for (n = 0; n < rank; n++)
99 dest[n * dstride] = 0;
102 GFC_INTEGER_4 minval;
104 minval = GFC_INTEGER_4_HUGE;
106 while (base)
109 /* Implementation start. */
111 if (*base < minval || !dest[0])
113 minval = *base;
114 for (n = 0; n < rank; n++)
115 dest[n * dstride] = count[n] + 1;
117 /* Implementation end. */
119 /* Advance to the next element. */
120 count[0]++;
121 base += sstride[0];
122 n = 0;
123 while (count[n] == extent[n])
125 /* When we get to the end of a dimension, reset it and increment
126 the next dimension. */
127 count[n] = 0;
128 /* We could precalculate these products, but this is a less
129 frequently used path so probably not worth it. */
130 base -= sstride[n] * extent[n];
131 n++;
132 if (n == rank)
134 /* Break out of the loop. */
135 base = NULL;
136 break;
138 else
140 count[n]++;
141 base += sstride[n];
149 extern void mminloc0_16_i4 (gfc_array_i16 * const restrict,
150 gfc_array_i4 * const restrict, gfc_array_l1 * const restrict);
151 export_proto(mminloc0_16_i4);
153 void
154 mminloc0_16_i4 (gfc_array_i16 * const restrict retarray,
155 gfc_array_i4 * const restrict array,
156 gfc_array_l1 * const restrict mask)
158 index_type count[GFC_MAX_DIMENSIONS];
159 index_type extent[GFC_MAX_DIMENSIONS];
160 index_type sstride[GFC_MAX_DIMENSIONS];
161 index_type mstride[GFC_MAX_DIMENSIONS];
162 index_type dstride;
163 GFC_INTEGER_16 *dest;
164 const GFC_INTEGER_4 *base;
165 GFC_LOGICAL_1 *mbase;
166 int rank;
167 index_type n;
168 int mask_kind;
170 rank = GFC_DESCRIPTOR_RANK (array);
171 if (rank <= 0)
172 runtime_error ("Rank of array needs to be > 0");
174 if (retarray->data == NULL)
176 retarray->dim[0].lbound = 0;
177 retarray->dim[0].ubound = rank-1;
178 retarray->dim[0].stride = 1;
179 retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
180 retarray->offset = 0;
181 retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
183 else
185 if (GFC_DESCRIPTOR_RANK (retarray) != 1)
186 runtime_error ("rank of return array does not equal 1");
188 if (retarray->dim[0].ubound + 1 - retarray->dim[0].lbound != rank)
189 runtime_error ("dimension of return array incorrect");
192 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
194 mbase = mask->data;
196 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
197 #ifdef HAVE_GFC_LOGICAL_16
198 || mask_kind == 16
199 #endif
201 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
202 else
203 runtime_error ("Funny sized logical array");
205 dstride = retarray->dim[0].stride;
206 dest = retarray->data;
207 for (n = 0; n < rank; n++)
209 sstride[n] = array->dim[n].stride;
210 mstride[n] = mask->dim[n].stride * mask_kind;
211 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
212 count[n] = 0;
213 if (extent[n] <= 0)
215 /* Set the return value. */
216 for (n = 0; n < rank; n++)
217 dest[n * dstride] = 0;
218 return;
222 base = array->data;
224 /* Initialize the return value. */
225 for (n = 0; n < rank; n++)
226 dest[n * dstride] = 0;
229 GFC_INTEGER_4 minval;
231 minval = GFC_INTEGER_4_HUGE;
233 while (base)
236 /* Implementation start. */
238 if (*mbase && (*base < minval || !dest[0]))
240 minval = *base;
241 for (n = 0; n < rank; n++)
242 dest[n * dstride] = count[n] + 1;
244 /* Implementation end. */
246 /* Advance to the next element. */
247 count[0]++;
248 base += sstride[0];
249 mbase += mstride[0];
250 n = 0;
251 while (count[n] == extent[n])
253 /* When we get to the end of a dimension, reset it and increment
254 the next dimension. */
255 count[n] = 0;
256 /* We could precalculate these products, but this is a less
257 frequently used path so probably not worth it. */
258 base -= sstride[n] * extent[n];
259 mbase -= mstride[n] * extent[n];
260 n++;
261 if (n == rank)
263 /* Break out of the loop. */
264 base = NULL;
265 break;
267 else
269 count[n]++;
270 base += sstride[n];
271 mbase += mstride[n];
279 extern void sminloc0_16_i4 (gfc_array_i16 * const restrict,
280 gfc_array_i4 * const restrict, GFC_LOGICAL_4 *);
281 export_proto(sminloc0_16_i4);
283 void
284 sminloc0_16_i4 (gfc_array_i16 * const restrict retarray,
285 gfc_array_i4 * const restrict array,
286 GFC_LOGICAL_4 * mask)
288 index_type rank;
289 index_type dstride;
290 index_type n;
291 GFC_INTEGER_16 *dest;
293 if (*mask)
295 minloc0_16_i4 (retarray, array);
296 return;
299 rank = GFC_DESCRIPTOR_RANK (array);
301 if (rank <= 0)
302 runtime_error ("Rank of array needs to be > 0");
304 if (retarray->data == NULL)
306 retarray->dim[0].lbound = 0;
307 retarray->dim[0].ubound = rank-1;
308 retarray->dim[0].stride = 1;
309 retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
310 retarray->offset = 0;
311 retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
313 else
315 if (GFC_DESCRIPTOR_RANK (retarray) != 1)
316 runtime_error ("rank of return array does not equal 1");
318 if (retarray->dim[0].ubound + 1 - retarray->dim[0].lbound != rank)
319 runtime_error ("dimension of return array incorrect");
322 dstride = retarray->dim[0].stride;
323 dest = retarray->data;
324 for (n = 0; n<rank; n++)
325 dest[n * dstride] = 0 ;
327 #endif