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
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. */
36 #include "libgfortran.h"
39 #if defined (HAVE_GFC_INTEGER_8) && defined (HAVE_GFC_INTEGER_8)
42 extern void minloc0_8_i8 (gfc_array_i8
* const restrict retarray
,
43 gfc_array_i8
* const restrict array
);
44 export_proto(minloc0_8_i8
);
47 minloc0_8_i8 (gfc_array_i8
* const restrict retarray
,
48 gfc_array_i8
* const restrict array
)
50 index_type count
[GFC_MAX_DIMENSIONS
];
51 index_type extent
[GFC_MAX_DIMENSIONS
];
52 index_type sstride
[GFC_MAX_DIMENSIONS
];
54 const GFC_INTEGER_8
*base
;
59 rank
= GFC_DESCRIPTOR_RANK (array
);
61 runtime_error ("Rank of array needs to be > 0");
63 if (retarray
->data
== NULL
)
65 retarray
->dim
[0].lbound
= 0;
66 retarray
->dim
[0].ubound
= rank
-1;
67 retarray
->dim
[0].stride
= 1;
68 retarray
->dtype
= (retarray
->dtype
& ~GFC_DTYPE_RANK_MASK
) | 1;
70 retarray
->data
= internal_malloc_size (sizeof (GFC_INTEGER_8
) * rank
);
74 if (GFC_DESCRIPTOR_RANK (retarray
) != 1)
75 runtime_error ("rank of return array does not equal 1");
77 if (retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
!= rank
)
78 runtime_error ("dimension of return array incorrect");
80 if (retarray
->dim
[0].stride
== 0)
81 retarray
->dim
[0].stride
= 1;
84 /* TODO: It should be a front end job to correctly set the strides. */
86 if (array
->dim
[0].stride
== 0)
87 array
->dim
[0].stride
= 1;
89 dstride
= retarray
->dim
[0].stride
;
90 dest
= retarray
->data
;
91 for (n
= 0; n
< rank
; n
++)
93 sstride
[n
] = array
->dim
[n
].stride
;
94 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
98 /* Set the return value. */
99 for (n
= 0; n
< rank
; n
++)
100 dest
[n
* dstride
] = 0;
107 /* Initialize the return value. */
108 for (n
= 0; n
< rank
; n
++)
109 dest
[n
* dstride
] = 0;
112 GFC_INTEGER_8 minval
;
114 minval
= GFC_INTEGER_8_HUGE
;
119 /* Implementation start. */
121 if (*base
< minval
|| !dest
[0])
124 for (n
= 0; n
< rank
; n
++)
125 dest
[n
* dstride
] = count
[n
] + 1;
127 /* Implementation end. */
129 /* Advance to the next element. */
133 while (count
[n
] == extent
[n
])
135 /* When we get to the end of a dimension, reset it and increment
136 the next dimension. */
138 /* We could precalculate these products, but this is a less
139 frequently used path so proabably not worth it. */
140 base
-= sstride
[n
] * extent
[n
];
144 /* Break out of the loop. */
159 extern void mminloc0_8_i8 (gfc_array_i8
* const restrict
,
160 gfc_array_i8
* const restrict
, gfc_array_l4
* const restrict
);
161 export_proto(mminloc0_8_i8
);
164 mminloc0_8_i8 (gfc_array_i8
* const restrict retarray
,
165 gfc_array_i8
* const restrict array
,
166 gfc_array_l4
* const restrict mask
)
168 index_type count
[GFC_MAX_DIMENSIONS
];
169 index_type extent
[GFC_MAX_DIMENSIONS
];
170 index_type sstride
[GFC_MAX_DIMENSIONS
];
171 index_type mstride
[GFC_MAX_DIMENSIONS
];
174 const GFC_INTEGER_8
*base
;
175 GFC_LOGICAL_4
*mbase
;
179 rank
= GFC_DESCRIPTOR_RANK (array
);
181 runtime_error ("Rank of array needs to be > 0");
183 if (retarray
->data
== NULL
)
185 retarray
->dim
[0].lbound
= 0;
186 retarray
->dim
[0].ubound
= rank
-1;
187 retarray
->dim
[0].stride
= 1;
188 retarray
->dtype
= (retarray
->dtype
& ~GFC_DTYPE_RANK_MASK
) | 1;
189 retarray
->offset
= 0;
190 retarray
->data
= internal_malloc_size (sizeof (GFC_INTEGER_8
) * rank
);
194 if (GFC_DESCRIPTOR_RANK (retarray
) != 1)
195 runtime_error ("rank of return array does not equal 1");
197 if (retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
!= rank
)
198 runtime_error ("dimension of return array incorrect");
200 if (retarray
->dim
[0].stride
== 0)
201 retarray
->dim
[0].stride
= 1;
204 /* TODO: It should be a front end job to correctly set the strides. */
206 if (array
->dim
[0].stride
== 0)
207 array
->dim
[0].stride
= 1;
209 if (mask
->dim
[0].stride
== 0)
210 mask
->dim
[0].stride
= 1;
212 dstride
= retarray
->dim
[0].stride
;
213 dest
= retarray
->data
;
214 for (n
= 0; n
< rank
; n
++)
216 sstride
[n
] = array
->dim
[n
].stride
;
217 mstride
[n
] = mask
->dim
[n
].stride
;
218 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
222 /* Set the return value. */
223 for (n
= 0; n
< rank
; n
++)
224 dest
[n
* dstride
] = 0;
232 if (GFC_DESCRIPTOR_SIZE (mask
) != 4)
234 /* This allows the same loop to be used for all logical types. */
235 assert (GFC_DESCRIPTOR_SIZE (mask
) == 8);
236 for (n
= 0; n
< rank
; n
++)
238 mbase
= (GFOR_POINTER_L8_TO_L4 (mbase
));
242 /* Initialize the return value. */
243 for (n
= 0; n
< rank
; n
++)
244 dest
[n
* dstride
] = 0;
247 GFC_INTEGER_8 minval
;
249 minval
= GFC_INTEGER_8_HUGE
;
254 /* Implementation start. */
256 if (*mbase
&& (*base
< minval
|| !dest
[0]))
259 for (n
= 0; n
< rank
; n
++)
260 dest
[n
* dstride
] = count
[n
] + 1;
262 /* Implementation end. */
264 /* Advance to the next element. */
269 while (count
[n
] == extent
[n
])
271 /* When we get to the end of a dimension, reset it and increment
272 the next dimension. */
274 /* We could precalculate these products, but this is a less
275 frequently used path so proabably not worth it. */
276 base
-= sstride
[n
] * extent
[n
];
277 mbase
-= mstride
[n
] * extent
[n
];
281 /* Break out of the loop. */
297 extern void sminloc0_8_i8 (gfc_array_i8
* const restrict
,
298 gfc_array_i8
* const restrict
, GFC_LOGICAL_4
*);
299 export_proto(sminloc0_8_i8
);
302 sminloc0_8_i8 (gfc_array_i8
* const restrict retarray
,
303 gfc_array_i8
* const restrict array
,
304 GFC_LOGICAL_4
* mask
)
313 minloc0_8_i8 (retarray
, array
);
317 rank
= GFC_DESCRIPTOR_RANK (array
);
320 runtime_error ("Rank of array needs to be > 0");
322 if (retarray
->data
== NULL
)
324 retarray
->dim
[0].lbound
= 0;
325 retarray
->dim
[0].ubound
= rank
-1;
326 retarray
->dim
[0].stride
= 1;
327 retarray
->dtype
= (retarray
->dtype
& ~GFC_DTYPE_RANK_MASK
) | 1;
328 retarray
->offset
= 0;
329 retarray
->data
= internal_malloc_size (sizeof (GFC_INTEGER_8
) * rank
);
333 if (GFC_DESCRIPTOR_RANK (retarray
) != 1)
334 runtime_error ("rank of return array does not equal 1");
336 if (retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
!= rank
)
337 runtime_error ("dimension of return array incorrect");
339 if (retarray
->dim
[0].stride
== 0)
340 retarray
->dim
[0].stride
= 1;
343 dstride
= retarray
->dim
[0].stride
;
344 dest
= retarray
->data
;
345 for (n
= 0; n
<rank
; n
++)
346 dest
[n
* dstride
] = 0 ;