1 /* Implementation of the MINLOC intrinsic
2 Copyright 2002, 2007, 2009 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 3 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 General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
32 #if defined (HAVE_GFC_REAL_4) && defined (HAVE_GFC_INTEGER_4)
35 extern void minloc0_4_r4 (gfc_array_i4
* const restrict retarray
,
36 gfc_array_r4
* const restrict array
);
37 export_proto(minloc0_4_r4
);
40 minloc0_4_r4 (gfc_array_i4
* const restrict retarray
,
41 gfc_array_r4
* const restrict array
)
43 index_type count
[GFC_MAX_DIMENSIONS
];
44 index_type extent
[GFC_MAX_DIMENSIONS
];
45 index_type sstride
[GFC_MAX_DIMENSIONS
];
47 const GFC_REAL_4
*base
;
48 GFC_INTEGER_4
* restrict dest
;
52 rank
= GFC_DESCRIPTOR_RANK (array
);
54 runtime_error ("Rank of array needs to be > 0");
56 if (retarray
->data
== NULL
)
58 retarray
->dim
[0].lbound
= 0;
59 retarray
->dim
[0].ubound
= rank
-1;
60 retarray
->dim
[0].stride
= 1;
61 retarray
->dtype
= (retarray
->dtype
& ~GFC_DTYPE_RANK_MASK
) | 1;
63 retarray
->data
= internal_malloc_size (sizeof (GFC_INTEGER_4
) * rank
);
67 if (unlikely (compile_options
.bounds_check
))
70 index_type ret_extent
;
72 ret_rank
= GFC_DESCRIPTOR_RANK (retarray
);
74 runtime_error ("rank of return array in MINLOC intrinsic"
75 " should be 1, is %ld", (long int) ret_rank
);
77 ret_extent
= retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
;
78 if (ret_extent
!= rank
)
79 runtime_error ("Incorrect extent in return value of"
80 " MINLOC intrnisic: is %ld, should be %ld",
81 (long int) ret_extent
, (long int) rank
);
85 dstride
= retarray
->dim
[0].stride
;
86 dest
= retarray
->data
;
87 for (n
= 0; n
< rank
; n
++)
89 sstride
[n
] = array
->dim
[n
].stride
;
90 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
94 /* Set the return value. */
95 for (n
= 0; n
< rank
; n
++)
96 dest
[n
* dstride
] = 0;
103 /* Initialize the return value. */
104 for (n
= 0; n
< rank
; n
++)
105 dest
[n
* dstride
] = 0;
110 minval
= GFC_REAL_4_HUGE
;
115 /* Implementation start. */
117 if (*base
< minval
|| !dest
[0])
120 for (n
= 0; n
< rank
; n
++)
121 dest
[n
* dstride
] = count
[n
] + 1;
123 /* Implementation end. */
125 /* Advance to the next element. */
129 while (count
[n
] == extent
[n
])
131 /* When we get to the end of a dimension, reset it and increment
132 the next dimension. */
134 /* We could precalculate these products, but this is a less
135 frequently used path so probably not worth it. */
136 base
-= sstride
[n
] * extent
[n
];
140 /* Break out of the loop. */
155 extern void mminloc0_4_r4 (gfc_array_i4
* const restrict
,
156 gfc_array_r4
* const restrict
, gfc_array_l1
* const restrict
);
157 export_proto(mminloc0_4_r4
);
160 mminloc0_4_r4 (gfc_array_i4
* const restrict retarray
,
161 gfc_array_r4
* const restrict array
,
162 gfc_array_l1
* const restrict 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
];
170 const GFC_REAL_4
*base
;
171 GFC_LOGICAL_1
*mbase
;
176 rank
= GFC_DESCRIPTOR_RANK (array
);
178 runtime_error ("Rank of array needs to be > 0");
180 if (retarray
->data
== NULL
)
182 retarray
->dim
[0].lbound
= 0;
183 retarray
->dim
[0].ubound
= rank
-1;
184 retarray
->dim
[0].stride
= 1;
185 retarray
->dtype
= (retarray
->dtype
& ~GFC_DTYPE_RANK_MASK
) | 1;
186 retarray
->offset
= 0;
187 retarray
->data
= internal_malloc_size (sizeof (GFC_INTEGER_4
) * rank
);
191 if (unlikely (compile_options
.bounds_check
))
193 int ret_rank
, mask_rank
;
194 index_type ret_extent
;
196 index_type array_extent
, mask_extent
;
198 ret_rank
= GFC_DESCRIPTOR_RANK (retarray
);
200 runtime_error ("rank of return array in MINLOC intrinsic"
201 " should be 1, is %ld", (long int) ret_rank
);
203 ret_extent
= retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
;
204 if (ret_extent
!= rank
)
205 runtime_error ("Incorrect extent in return value of"
206 " MINLOC intrnisic: is %ld, should be %ld",
207 (long int) ret_extent
, (long int) rank
);
209 mask_rank
= GFC_DESCRIPTOR_RANK (mask
);
210 if (rank
!= mask_rank
)
211 runtime_error ("rank of MASK argument in MINLOC intrnisic"
212 "should be %ld, is %ld", (long int) rank
,
213 (long int) mask_rank
);
215 for (n
=0; n
<rank
; n
++)
217 array_extent
= array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
218 mask_extent
= mask
->dim
[n
].ubound
+ 1 - mask
->dim
[n
].lbound
;
219 if (array_extent
!= mask_extent
)
220 runtime_error ("Incorrect extent in MASK argument of"
221 " MINLOC intrinsic in dimension %ld:"
222 " is %ld, should be %ld", (long int) n
+ 1,
223 (long int) mask_extent
, (long int) array_extent
);
228 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
232 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
233 #ifdef HAVE_GFC_LOGICAL_16
237 mbase
= GFOR_POINTER_TO_L1 (mbase
, mask_kind
);
239 runtime_error ("Funny sized logical array");
241 dstride
= retarray
->dim
[0].stride
;
242 dest
= retarray
->data
;
243 for (n
= 0; n
< rank
; n
++)
245 sstride
[n
] = array
->dim
[n
].stride
;
246 mstride
[n
] = mask
->dim
[n
].stride
* mask_kind
;
247 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
251 /* Set the return value. */
252 for (n
= 0; n
< rank
; n
++)
253 dest
[n
* dstride
] = 0;
260 /* Initialize the return value. */
261 for (n
= 0; n
< rank
; n
++)
262 dest
[n
* dstride
] = 0;
267 minval
= GFC_REAL_4_HUGE
;
272 /* Implementation start. */
274 if (*mbase
&& (*base
< minval
|| !dest
[0]))
277 for (n
= 0; n
< rank
; n
++)
278 dest
[n
* dstride
] = count
[n
] + 1;
280 /* Implementation end. */
282 /* Advance to the next element. */
287 while (count
[n
] == extent
[n
])
289 /* When we get to the end of a dimension, reset it and increment
290 the next dimension. */
292 /* We could precalculate these products, but this is a less
293 frequently used path so probably not worth it. */
294 base
-= sstride
[n
] * extent
[n
];
295 mbase
-= mstride
[n
] * extent
[n
];
299 /* Break out of the loop. */
315 extern void sminloc0_4_r4 (gfc_array_i4
* const restrict
,
316 gfc_array_r4
* const restrict
, GFC_LOGICAL_4
*);
317 export_proto(sminloc0_4_r4
);
320 sminloc0_4_r4 (gfc_array_i4
* const restrict retarray
,
321 gfc_array_r4
* const restrict array
,
322 GFC_LOGICAL_4
* mask
)
331 minloc0_4_r4 (retarray
, array
);
335 rank
= GFC_DESCRIPTOR_RANK (array
);
338 runtime_error ("Rank of array needs to be > 0");
340 if (retarray
->data
== NULL
)
342 retarray
->dim
[0].lbound
= 0;
343 retarray
->dim
[0].ubound
= rank
-1;
344 retarray
->dim
[0].stride
= 1;
345 retarray
->dtype
= (retarray
->dtype
& ~GFC_DTYPE_RANK_MASK
) | 1;
346 retarray
->offset
= 0;
347 retarray
->data
= internal_malloc_size (sizeof (GFC_INTEGER_4
) * rank
);
351 if (unlikely (compile_options
.bounds_check
))
354 index_type ret_extent
;
356 ret_rank
= GFC_DESCRIPTOR_RANK (retarray
);
358 runtime_error ("rank of return array in MINLOC intrinsic"
359 " should be 1, is %ld", (long int) ret_rank
);
361 ret_extent
= retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
;
362 if (ret_extent
!= rank
)
363 runtime_error ("dimension of return array incorrect");
367 dstride
= retarray
->dim
[0].stride
;
368 dest
= retarray
->data
;
369 for (n
= 0; n
<rank
; n
++)
370 dest
[n
* dstride
] = 0 ;