Merge from mainline, version 106440, to sh-elf-4_1-branch .
[official-gcc.git] / libgfortran / generated / minloc0_4_i4.c
blobe3b15ae895b804f04992dcf492493754bf278e27
1 /* Implementation of the MINLOC 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 <limits.h>
36 #include "libgfortran.h"
39 #if defined (HAVE_GFC_INTEGER_4) && defined (HAVE_GFC_INTEGER_4)
42 extern void minloc0_4_i4 (gfc_array_i4 * retarray, gfc_array_i4 *array);
43 export_proto(minloc0_4_i4);
45 void
46 minloc0_4_i4 (gfc_array_i4 * retarray, gfc_array_i4 *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 GFC_INTEGER_4 *base;
53 GFC_INTEGER_4 *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_4) * 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");
78 if (retarray->dim[0].stride == 0)
79 retarray->dim[0].stride = 1;
82 /* TODO: It should be a front end job to correctly set the strides. */
84 if (array->dim[0].stride == 0)
85 array->dim[0].stride = 1;
87 dstride = retarray->dim[0].stride;
88 dest = retarray->data;
89 for (n = 0; n < rank; n++)
91 sstride[n] = array->dim[n].stride;
92 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
93 count[n] = 0;
94 if (extent[n] <= 0)
96 /* Set the return value. */
97 for (n = 0; n < rank; n++)
98 dest[n * dstride] = 0;
99 return;
103 base = array->data;
105 /* Initialize the return value. */
106 for (n = 0; n < rank; n++)
107 dest[n * dstride] = 1;
110 GFC_INTEGER_4 minval;
112 minval = GFC_INTEGER_4_HUGE;
114 while (base)
117 /* Implementation start. */
119 if (*base < minval)
121 minval = *base;
122 for (n = 0; n < rank; n++)
123 dest[n * dstride] = count[n] + 1;
125 /* Implementation end. */
127 /* Advance to the next element. */
128 count[0]++;
129 base += sstride[0];
130 n = 0;
131 while (count[n] == extent[n])
133 /* When we get to the end of a dimension, reset it and increment
134 the next dimension. */
135 count[n] = 0;
136 /* We could precalculate these products, but this is a less
137 frequently used path so proabably not worth it. */
138 base -= sstride[n] * extent[n];
139 n++;
140 if (n == rank)
142 /* Break out of the loop. */
143 base = NULL;
144 break;
146 else
148 count[n]++;
149 base += sstride[n];
157 extern void mminloc0_4_i4 (gfc_array_i4 *, gfc_array_i4 *, gfc_array_l4 *);
158 export_proto(mminloc0_4_i4);
160 void
161 mminloc0_4_i4 (gfc_array_i4 * retarray, gfc_array_i4 *array,
162 gfc_array_l4 * mask)
164 index_type count[GFC_MAX_DIMENSIONS];
165 index_type extent[GFC_MAX_DIMENSIONS];
166 index_type sstride[GFC_MAX_DIMENSIONS];
167 index_type mstride[GFC_MAX_DIMENSIONS];
168 index_type dstride;
169 GFC_INTEGER_4 *dest;
170 GFC_INTEGER_4 *base;
171 GFC_LOGICAL_4 *mbase;
172 int rank;
173 index_type n;
175 rank = GFC_DESCRIPTOR_RANK (array);
176 if (rank <= 0)
177 runtime_error ("Rank of array needs to be > 0");
179 if (retarray->data == NULL)
181 retarray->dim[0].lbound = 0;
182 retarray->dim[0].ubound = rank-1;
183 retarray->dim[0].stride = 1;
184 retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
185 retarray->offset = 0;
186 retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
188 else
190 if (GFC_DESCRIPTOR_RANK (retarray) != 1)
191 runtime_error ("rank of return array does not equal 1");
193 if (retarray->dim[0].ubound + 1 - retarray->dim[0].lbound != rank)
194 runtime_error ("dimension of return array incorrect");
196 if (retarray->dim[0].stride == 0)
197 retarray->dim[0].stride = 1;
200 /* TODO: It should be a front end job to correctly set the strides. */
202 if (array->dim[0].stride == 0)
203 array->dim[0].stride = 1;
205 if (mask->dim[0].stride == 0)
206 mask->dim[0].stride = 1;
208 dstride = retarray->dim[0].stride;
209 dest = retarray->data;
210 for (n = 0; n < rank; n++)
212 sstride[n] = array->dim[n].stride;
213 mstride[n] = mask->dim[n].stride;
214 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
215 count[n] = 0;
216 if (extent[n] <= 0)
218 /* Set the return value. */
219 for (n = 0; n < rank; n++)
220 dest[n * dstride] = 0;
221 return;
225 base = array->data;
226 mbase = mask->data;
228 if (GFC_DESCRIPTOR_SIZE (mask) != 4)
230 /* This allows the same loop to be used for all logical types. */
231 assert (GFC_DESCRIPTOR_SIZE (mask) == 8);
232 for (n = 0; n < rank; n++)
233 mstride[n] <<= 1;
234 mbase = (GFOR_POINTER_L8_TO_L4 (mbase));
238 /* Initialize the return value. */
239 for (n = 0; n < rank; n++)
240 dest[n * dstride] = 1;
243 GFC_INTEGER_4 minval;
245 minval = GFC_INTEGER_4_HUGE;
247 while (base)
250 /* Implementation start. */
252 if (*mbase && *base < minval)
254 minval = *base;
255 for (n = 0; n < rank; n++)
256 dest[n * dstride] = count[n] + 1;
258 /* Implementation end. */
260 /* Advance to the next element. */
261 count[0]++;
262 base += sstride[0];
263 mbase += mstride[0];
264 n = 0;
265 while (count[n] == extent[n])
267 /* When we get to the end of a dimension, reset it and increment
268 the next dimension. */
269 count[n] = 0;
270 /* We could precalculate these products, but this is a less
271 frequently used path so proabably not worth it. */
272 base -= sstride[n] * extent[n];
273 mbase -= mstride[n] * extent[n];
274 n++;
275 if (n == rank)
277 /* Break out of the loop. */
278 base = NULL;
279 break;
281 else
283 count[n]++;
284 base += sstride[n];
285 mbase += mstride[n];
292 #endif