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 (libgfor).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
27 #include "libgfortran.h"
32 __minloc0_8_i8 (gfc_array_i8 * retarray, gfc_array_i8 *array)
34 index_type count[GFC_MAX_DIMENSIONS];
35 index_type extent[GFC_MAX_DIMENSIONS];
36 index_type sstride[GFC_MAX_DIMENSIONS];
43 rank = GFC_DESCRIPTOR_RANK (array);
45 assert (GFC_DESCRIPTOR_RANK (retarray) == 1);
46 assert (retarray->dim[0].ubound + 1 - retarray->dim[0].lbound == rank);
47 if (array->dim[0].stride == 0)
48 array->dim[0].stride = 1;
49 if (retarray->dim[0].stride == 0)
50 retarray->dim[0].stride = 1;
52 dstride = retarray->dim[0].stride;
53 dest = retarray->data;
54 for (n = 0; n < rank; n++)
56 sstride[n] = array->dim[n].stride;
57 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
61 /* Set the return value. */
62 for (n = 0; n < rank; n++)
63 dest[n * dstride] = 0;
70 /* Initialize the return value. */
71 for (n = 0; n < rank; n++)
72 dest[n * dstride] = 1;
77 minval = GFC_INTEGER_8_HUGE;
82 /* Implementation start. */
87 for (n = 0; n < rank; n++)
88 dest[n * dstride] = count[n] + 1;
90 /* Implementation end. */
92 /* Advance to the next element. */
96 while (count[n] == extent[n])
98 /* When we get to the end of a dimension, reset it and increment
99 the next dimension. */
101 /* We could precalculate these products, but this is a less
102 frequently used path so proabably not worth it. */
103 base -= sstride[n] * extent[n];
107 /* Break out of the loop. */
122 __mminloc0_8_i8 (gfc_array_i8 * retarray, gfc_array_i8 *array, gfc_array_l4 * mask)
124 index_type count[GFC_MAX_DIMENSIONS];
125 index_type extent[GFC_MAX_DIMENSIONS];
126 index_type sstride[GFC_MAX_DIMENSIONS];
127 index_type mstride[GFC_MAX_DIMENSIONS];
131 GFC_LOGICAL_4 *mbase;
135 rank = GFC_DESCRIPTOR_RANK (array);
137 assert (GFC_DESCRIPTOR_RANK (retarray) == 1);
138 assert (retarray->dim[0].ubound + 1 - retarray->dim[0].lbound == rank);
139 assert (GFC_DESCRIPTOR_RANK (mask) == rank);
141 if (array->dim[0].stride == 0)
142 array->dim[0].stride = 1;
143 if (retarray->dim[0].stride == 0)
144 retarray->dim[0].stride = 1;
145 if (retarray->dim[0].stride == 0)
146 retarray->dim[0].stride = 1;
148 dstride = retarray->dim[0].stride;
149 dest = retarray->data;
150 for (n = 0; n < rank; n++)
152 sstride[n] = array->dim[n].stride;
153 mstride[n] = mask->dim[n].stride;
154 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
158 /* Set the return value. */
159 for (n = 0; n < rank; n++)
160 dest[n * dstride] = 0;
168 if (GFC_DESCRIPTOR_SIZE (mask) != 4)
170 /* This allows the same loop to be used for all logical types. */
171 assert (GFC_DESCRIPTOR_SIZE (mask) == 8);
172 for (n = 0; n < rank; n++)
174 mbase = (GFOR_POINTER_L8_TO_L4 (mbase));
178 /* Initialize the return value. */
179 for (n = 0; n < rank; n++)
180 dest[n * dstride] = 1;
183 GFC_INTEGER_8 minval;
185 minval = GFC_INTEGER_8_HUGE;
190 /* Implementation start. */
192 if (*mbase && *base < minval)
195 for (n = 0; n < rank; n++)
196 dest[n * dstride] = count[n] + 1;
198 /* Implementation end. */
200 /* Advance to the next element. */
205 while (count[n] == extent[n])
207 /* When we get to the end of a dimension, reset it and increment
208 the next dimension. */
210 /* We could precalculate these products, but this is a less
211 frequently used path so proabably not worth it. */
212 base -= sstride[n] * extent[n];
213 mbase -= mstride[n] * extent[n];
217 /* Break out of the loop. */