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
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"
37 #if defined (HAVE_GFC_INTEGER_4) && defined (HAVE_GFC_INTEGER_4)
40 extern void minloc0_4_i4 (gfc_array_i4
* const restrict retarray
,
41 gfc_array_i4
* const restrict array
);
42 export_proto(minloc0_4_i4
);
45 minloc0_4_i4 (gfc_array_i4
* 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
];
52 const GFC_INTEGER_4
*base
;
57 rank
= GFC_DESCRIPTOR_RANK (array
);
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;
68 retarray
->data
= internal_malloc_size (sizeof (GFC_INTEGER_4
) * rank
);
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
;
88 /* Set the return value. */
89 for (n
= 0; n
< rank
; n
++)
90 dest
[n
* dstride
] = 0;
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
;
109 /* Implementation start. */
111 if (*base
< minval
|| !dest
[0])
114 for (n
= 0; n
< rank
; n
++)
115 dest
[n
* dstride
] = count
[n
] + 1;
117 /* Implementation end. */
119 /* Advance to the next element. */
123 while (count
[n
] == extent
[n
])
125 /* When we get to the end of a dimension, reset it and increment
126 the next dimension. */
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
];
134 /* Break out of the loop. */
149 extern void mminloc0_4_i4 (gfc_array_i4
* const restrict
,
150 gfc_array_i4
* const restrict
, gfc_array_l1
* const restrict
);
151 export_proto(mminloc0_4_i4
);
154 mminloc0_4_i4 (gfc_array_i4
* 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
];
164 const GFC_INTEGER_4
*base
;
165 GFC_LOGICAL_1
*mbase
;
170 rank
= GFC_DESCRIPTOR_RANK (array
);
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_4
) * rank
);
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
);
196 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
197 #ifdef HAVE_GFC_LOGICAL_16
201 mbase
= GFOR_POINTER_TO_L1 (mbase
, mask_kind
);
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
;
215 /* Set the return value. */
216 for (n
= 0; n
< rank
; n
++)
217 dest
[n
* dstride
] = 0;
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
;
236 /* Implementation start. */
238 if (*mbase
&& (*base
< minval
|| !dest
[0]))
241 for (n
= 0; n
< rank
; n
++)
242 dest
[n
* dstride
] = count
[n
] + 1;
244 /* Implementation end. */
246 /* Advance to the next element. */
251 while (count
[n
] == extent
[n
])
253 /* When we get to the end of a dimension, reset it and increment
254 the next dimension. */
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
];
263 /* Break out of the loop. */
279 extern void sminloc0_4_i4 (gfc_array_i4
* const restrict
,
280 gfc_array_i4
* const restrict
, GFC_LOGICAL_4
*);
281 export_proto(sminloc0_4_i4
);
284 sminloc0_4_i4 (gfc_array_i4
* const restrict retarray
,
285 gfc_array_i4
* const restrict array
,
286 GFC_LOGICAL_4
* mask
)
295 minloc0_4_i4 (retarray
, array
);
299 rank
= GFC_DESCRIPTOR_RANK (array
);
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_4
) * rank
);
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 ;