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_REAL_4) && defined (HAVE_GFC_INTEGER_4)
42 extern void minloc0_4_r4 (gfc_array_i4
* retarray
, gfc_array_r4
*array
);
43 export_proto(minloc0_4_r4
);
46 minloc0_4_r4 (gfc_array_i4
* retarray
, gfc_array_r4
*array
)
48 index_type count
[GFC_MAX_DIMENSIONS
];
49 index_type extent
[GFC_MAX_DIMENSIONS
];
50 index_type sstride
[GFC_MAX_DIMENSIONS
];
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");
78 if (retarray
->dim
[0].stride
== 0)
79 retarray
->dim
[0].stride
= 1;
82 /* TODO: It should be a front end job to correctly set the strides. */
84 if (array
->dim
[0].stride
== 0)
85 array
->dim
[0].stride
= 1;
87 dstride
= retarray
->dim
[0].stride
;
88 dest
= retarray
->data
;
89 for (n
= 0; n
< rank
; n
++)
91 sstride
[n
] = array
->dim
[n
].stride
;
92 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
96 /* Set the return value. */
97 for (n
= 0; n
< rank
; n
++)
98 dest
[n
* dstride
] = 0;
105 /* Initialize the return value. */
106 for (n
= 0; n
< rank
; n
++)
107 dest
[n
* dstride
] = 1;
112 minval
= GFC_REAL_4_HUGE
;
117 /* Implementation start. */
122 for (n
= 0; n
< rank
; n
++)
123 dest
[n
* dstride
] = count
[n
] + 1;
125 /* Implementation end. */
127 /* Advance to the next element. */
131 while (count
[n
] == extent
[n
])
133 /* When we get to the end of a dimension, reset it and increment
134 the next dimension. */
136 /* We could precalculate these products, but this is a less
137 frequently used path so proabably not worth it. */
138 base
-= sstride
[n
] * extent
[n
];
142 /* Break out of the loop. */
157 extern void mminloc0_4_r4 (gfc_array_i4
*, gfc_array_r4
*, gfc_array_l4
*);
158 export_proto(mminloc0_4_r4
);
161 mminloc0_4_r4 (gfc_array_i4
* retarray
, gfc_array_r4
*array
,
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
];
171 GFC_LOGICAL_4
*mbase
;
175 rank
= GFC_DESCRIPTOR_RANK (array
);
177 runtime_error ("Rank of array needs to be > 0");
179 if (retarray
->data
== NULL
)
181 retarray
->dim
[0].lbound
= 0;
182 retarray
->dim
[0].ubound
= rank
-1;
183 retarray
->dim
[0].stride
= 1;
184 retarray
->dtype
= (retarray
->dtype
& ~GFC_DTYPE_RANK_MASK
) | 1;
185 retarray
->offset
= 0;
186 retarray
->data
= internal_malloc_size (sizeof (GFC_INTEGER_4
) * rank
);
190 if (GFC_DESCRIPTOR_RANK (retarray
) != 1)
191 runtime_error ("rank of return array does not equal 1");
193 if (retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
!= rank
)
194 runtime_error ("dimension of return array incorrect");
196 if (retarray
->dim
[0].stride
== 0)
197 retarray
->dim
[0].stride
= 1;
200 /* TODO: It should be a front end job to correctly set the strides. */
202 if (array
->dim
[0].stride
== 0)
203 array
->dim
[0].stride
= 1;
205 if (mask
->dim
[0].stride
== 0)
206 mask
->dim
[0].stride
= 1;
208 dstride
= retarray
->dim
[0].stride
;
209 dest
= retarray
->data
;
210 for (n
= 0; n
< rank
; n
++)
212 sstride
[n
] = array
->dim
[n
].stride
;
213 mstride
[n
] = mask
->dim
[n
].stride
;
214 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
218 /* Set the return value. */
219 for (n
= 0; n
< rank
; n
++)
220 dest
[n
* dstride
] = 0;
228 if (GFC_DESCRIPTOR_SIZE (mask
) != 4)
230 /* This allows the same loop to be used for all logical types. */
231 assert (GFC_DESCRIPTOR_SIZE (mask
) == 8);
232 for (n
= 0; n
< rank
; n
++)
234 mbase
= (GFOR_POINTER_L8_TO_L4 (mbase
));
238 /* Initialize the return value. */
239 for (n
= 0; n
< rank
; n
++)
240 dest
[n
* dstride
] = 1;
245 minval
= GFC_REAL_4_HUGE
;
250 /* Implementation start. */
252 if (*mbase
&& *base
< minval
)
255 for (n
= 0; n
< rank
; n
++)
256 dest
[n
* dstride
] = count
[n
] + 1;
258 /* Implementation end. */
260 /* Advance to the next element. */
265 while (count
[n
] == extent
[n
])
267 /* When we get to the end of a dimension, reset it and increment
268 the next dimension. */
270 /* We could precalculate these products, but this is a less
271 frequently used path so proabably not worth it. */
272 base
-= sstride
[n
] * extent
[n
];
273 mbase
-= mstride
[n
] * extent
[n
];
277 /* Break out of the loop. */