OSDN Git Service

9d7a1a4c599e4b6c5ee4586a7db92b9f9eeeb31d
[pf3gnuchains/gcc-fork.git] / libgfortran / generated / minloc0_4_i8.c
1 /* Implementation of the MINLOC intrinsic
2    Copyright 2002 Free Software Foundation, Inc.
3    Contributed by Paul Brook <paul@nowt.org>
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
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.
11
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.)
20
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.
25
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., 59 Temple Place - Suite 330,
29 Boston, MA 02111-1307, USA.  */
30
31 #include "config.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <float.h>
35 #include <limits.h>
36 #include "libgfortran.h"
37
38
39
40 extern void minloc0_4_i8 (gfc_array_i4 * retarray, gfc_array_i8 *array);
41 export_proto(minloc0_4_i8);
42
43 void
44 minloc0_4_i8 (gfc_array_i4 * retarray, gfc_array_i8 *array)
45 {
46   index_type count[GFC_MAX_DIMENSIONS];
47   index_type extent[GFC_MAX_DIMENSIONS];
48   index_type sstride[GFC_MAX_DIMENSIONS];
49   index_type dstride;
50   GFC_INTEGER_8 *base;
51   GFC_INTEGER_4 *dest;
52   index_type rank;
53   index_type n;
54
55   rank = GFC_DESCRIPTOR_RANK (array);
56   if (rank <= 0)
57     runtime_error ("Rank of array needs to be > 0");
58
59   if (retarray->data == NULL)
60     {
61       retarray->dim[0].lbound = 0;
62       retarray->dim[0].ubound = rank-1;
63       retarray->dim[0].stride = 1;
64       retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
65       retarray->base = 0;
66       retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
67     }
68   else
69     {
70       if (GFC_DESCRIPTOR_RANK (retarray) != 1)
71         runtime_error ("rank of return array does not equal 1");
72
73       if (retarray->dim[0].ubound + 1 - retarray->dim[0].lbound != rank)
74         runtime_error ("dimension of return array incorrect");
75
76       if (retarray->dim[0].stride == 0)
77         retarray->dim[0].stride = 1;
78     }
79   if (array->dim[0].stride == 0)
80     array->dim[0].stride = 1;
81
82   dstride = retarray->dim[0].stride;
83   dest = retarray->data;
84   for (n = 0; n < rank; n++)
85     {
86       sstride[n] = array->dim[n].stride;
87       extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
88       count[n] = 0;
89       if (extent[n] <= 0)
90         {
91           /* Set the return value.  */
92           for (n = 0; n < rank; n++)
93             dest[n * dstride] = 0;
94           return;
95         }
96     }
97
98   base = array->data;
99
100   /* Initialize the return value.  */
101   for (n = 0; n < rank; n++)
102     dest[n * dstride] = 1;
103   {
104
105   GFC_INTEGER_8 minval;
106
107   minval = GFC_INTEGER_8_HUGE;
108
109   while (base)
110     {
111       {
112         /* Implementation start.  */
113
114   if (*base < minval)
115     {
116       minval = *base;
117       for (n = 0; n < rank; n++)
118         dest[n * dstride] = count[n] + 1;
119     }
120         /* Implementation end.  */
121       }
122       /* Advance to the next element.  */
123       count[0]++;
124       base += sstride[0];
125       n = 0;
126       while (count[n] == extent[n])
127         {
128           /* When we get to the end of a dimension, reset it and increment
129              the next dimension.  */
130           count[n] = 0;
131           /* We could precalculate these products, but this is a less
132              frequently used path so proabably not worth it.  */
133           base -= sstride[n] * extent[n];
134           n++;
135           if (n == rank)
136             {
137               /* Break out of the loop.  */
138               base = NULL;
139               break;
140             }
141           else
142             {
143               count[n]++;
144               base += sstride[n];
145             }
146         }
147     }
148   }
149 }
150
151
152 extern void mminloc0_4_i8 (gfc_array_i4 *, gfc_array_i8 *, gfc_array_l4 *);
153 export_proto(mminloc0_4_i8);
154
155 void
156 mminloc0_4_i8 (gfc_array_i4 * retarray, gfc_array_i8 *array,
157                                   gfc_array_l4 * mask)
158 {
159   index_type count[GFC_MAX_DIMENSIONS];
160   index_type extent[GFC_MAX_DIMENSIONS];
161   index_type sstride[GFC_MAX_DIMENSIONS];
162   index_type mstride[GFC_MAX_DIMENSIONS];
163   index_type dstride;
164   GFC_INTEGER_4 *dest;
165   GFC_INTEGER_8 *base;
166   GFC_LOGICAL_4 *mbase;
167   int rank;
168   index_type n;
169
170   rank = GFC_DESCRIPTOR_RANK (array);
171   if (rank <= 0)
172     runtime_error ("Rank of array needs to be > 0");
173
174   if (retarray->data == NULL)
175     {
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->base = 0;
181       retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
182     }
183   else
184     {
185       if (GFC_DESCRIPTOR_RANK (retarray) != 1)
186         runtime_error ("rank of return array does not equal 1");
187
188       if (retarray->dim[0].ubound + 1 - retarray->dim[0].lbound != rank)
189         runtime_error ("dimension of return array incorrect");
190
191       if (retarray->dim[0].stride == 0)
192         retarray->dim[0].stride = 1;
193     }
194
195   if (array->dim[0].stride == 0)
196     array->dim[0].stride = 1;
197
198   if (mask->dim[0].stride == 0)
199     mask->dim[0].stride = 1;
200
201   dstride = retarray->dim[0].stride;
202   dest = retarray->data;
203   for (n = 0; n < rank; n++)
204     {
205       sstride[n] = array->dim[n].stride;
206       mstride[n] = mask->dim[n].stride;
207       extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
208       count[n] = 0;
209       if (extent[n] <= 0)
210         {
211           /* Set the return value.  */
212           for (n = 0; n < rank; n++)
213             dest[n * dstride] = 0;
214           return;
215         }
216     }
217
218   base = array->data;
219   mbase = mask->data;
220
221   if (GFC_DESCRIPTOR_SIZE (mask) != 4)
222     {
223       /* This allows the same loop to be used for all logical types.  */
224       assert (GFC_DESCRIPTOR_SIZE (mask) == 8);
225       for (n = 0; n < rank; n++)
226         mstride[n] <<= 1;
227       mbase = (GFOR_POINTER_L8_TO_L4 (mbase));
228     }
229
230
231   /* Initialize the return value.  */
232   for (n = 0; n < rank; n++)
233     dest[n * dstride] = 1;
234   {
235
236   GFC_INTEGER_8 minval;
237
238   minval = GFC_INTEGER_8_HUGE;
239
240   while (base)
241     {
242       {
243         /* Implementation start.  */
244
245   if (*mbase && *base < minval)
246     {
247       minval = *base;
248       for (n = 0; n < rank; n++)
249         dest[n * dstride] = count[n] + 1;
250     }
251         /* Implementation end.  */
252       }
253       /* Advance to the next element.  */
254       count[0]++;
255       base += sstride[0];
256       mbase += mstride[0];
257       n = 0;
258       while (count[n] == extent[n])
259         {
260           /* When we get to the end of a dimension, reset it and increment
261              the next dimension.  */
262           count[n] = 0;
263           /* We could precalculate these products, but this is a less
264              frequently used path so proabably not worth it.  */
265           base -= sstride[n] * extent[n];
266           mbase -= mstride[n] * extent[n];
267           n++;
268           if (n == rank)
269             {
270               /* Break out of the loop.  */
271               base = NULL;
272               break;
273             }
274           else
275             {
276               count[n]++;
277               base += sstride[n];
278               mbase += mstride[n];
279             }
280         }
281     }
282   }
283 }