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., 59 Temple Place - Suite 330,
29 Boston, MA 02111-1307, USA. */
36 #include "libgfortran.h"
40 extern void minloc0_4_i4 (gfc_array_i4
* retarray
, gfc_array_i4
*array
);
41 export_proto(minloc0_4_i4
);
44 minloc0_4_i4 (gfc_array_i4
* retarray
, gfc_array_i4
*array
)
46 index_type count
[GFC_MAX_DIMENSIONS
];
47 index_type extent
[GFC_MAX_DIMENSIONS
];
48 index_type sstride
[GFC_MAX_DIMENSIONS
];
55 rank
= GFC_DESCRIPTOR_RANK (array
);
57 runtime_error ("Rank of array needs to be > 0");
59 if (retarray
->data
== NULL
)
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;
66 retarray
->data
= internal_malloc_size (sizeof (GFC_INTEGER_4
) * rank
);
70 if (GFC_DESCRIPTOR_RANK (retarray
) != 1)
71 runtime_error ("rank of return array does not equal 1");
73 if (retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
!= rank
)
74 runtime_error ("dimension of return array incorrect");
76 if (retarray
->dim
[0].stride
== 0)
77 retarray
->dim
[0].stride
= 1;
80 /* TODO: It should be a front end job to correctly set the strides. */
82 if (array
->dim
[0].stride
== 0)
83 array
->dim
[0].stride
= 1;
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
] = 1;
108 GFC_INTEGER_4 minval
;
110 minval
= GFC_INTEGER_4_HUGE
;
115 /* Implementation start. */
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 proabably not worth it. */
136 base
-= sstride
[n
] * extent
[n
];
140 /* Break out of the loop. */
155 extern void mminloc0_4_i4 (gfc_array_i4
*, gfc_array_i4
*, gfc_array_l4
*);
156 export_proto(mminloc0_4_i4
);
159 mminloc0_4_i4 (gfc_array_i4
* retarray
, gfc_array_i4
*array
,
162 index_type count
[GFC_MAX_DIMENSIONS
];
163 index_type extent
[GFC_MAX_DIMENSIONS
];
164 index_type sstride
[GFC_MAX_DIMENSIONS
];
165 index_type mstride
[GFC_MAX_DIMENSIONS
];
169 GFC_LOGICAL_4
*mbase
;
173 rank
= GFC_DESCRIPTOR_RANK (array
);
175 runtime_error ("Rank of array needs to be > 0");
177 if (retarray
->data
== NULL
)
179 retarray
->dim
[0].lbound
= 0;
180 retarray
->dim
[0].ubound
= rank
-1;
181 retarray
->dim
[0].stride
= 1;
182 retarray
->dtype
= (retarray
->dtype
& ~GFC_DTYPE_RANK_MASK
) | 1;
183 retarray
->offset
= 0;
184 retarray
->data
= internal_malloc_size (sizeof (GFC_INTEGER_4
) * rank
);
188 if (GFC_DESCRIPTOR_RANK (retarray
) != 1)
189 runtime_error ("rank of return array does not equal 1");
191 if (retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
!= rank
)
192 runtime_error ("dimension of return array incorrect");
194 if (retarray
->dim
[0].stride
== 0)
195 retarray
->dim
[0].stride
= 1;
198 /* TODO: It should be a front end job to correctly set the strides. */
200 if (array
->dim
[0].stride
== 0)
201 array
->dim
[0].stride
= 1;
203 if (mask
->dim
[0].stride
== 0)
204 mask
->dim
[0].stride
= 1;
206 dstride
= retarray
->dim
[0].stride
;
207 dest
= retarray
->data
;
208 for (n
= 0; n
< rank
; n
++)
210 sstride
[n
] = array
->dim
[n
].stride
;
211 mstride
[n
] = mask
->dim
[n
].stride
;
212 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
216 /* Set the return value. */
217 for (n
= 0; n
< rank
; n
++)
218 dest
[n
* dstride
] = 0;
226 if (GFC_DESCRIPTOR_SIZE (mask
) != 4)
228 /* This allows the same loop to be used for all logical types. */
229 assert (GFC_DESCRIPTOR_SIZE (mask
) == 8);
230 for (n
= 0; n
< rank
; n
++)
232 mbase
= (GFOR_POINTER_L8_TO_L4 (mbase
));
236 /* Initialize the return value. */
237 for (n
= 0; n
< rank
; n
++)
238 dest
[n
* dstride
] = 1;
241 GFC_INTEGER_4 minval
;
243 minval
= GFC_INTEGER_4_HUGE
;
248 /* Implementation start. */
250 if (*mbase
&& *base
< minval
)
253 for (n
= 0; n
< rank
; n
++)
254 dest
[n
* dstride
] = count
[n
] + 1;
256 /* Implementation end. */
258 /* Advance to the next element. */
263 while (count
[n
] == extent
[n
])
265 /* When we get to the end of a dimension, reset it and increment
266 the next dimension. */
268 /* We could precalculate these products, but this is a less
269 frequently used path so proabably not worth it. */
270 base
-= sstride
[n
] * extent
[n
];
271 mbase
-= mstride
[n
] * extent
[n
];
275 /* Break out of the loop. */