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 (libgfor).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 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 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
27 #include "libgfortran.h"
32 __minloc0_4_i4 (gfc_array_i4
* retarray
, gfc_array_i4
*array
)
34 index_type count
[GFC_MAX_DIMENSIONS
];
35 index_type extent
[GFC_MAX_DIMENSIONS
];
36 index_type sstride
[GFC_MAX_DIMENSIONS
];
43 rank
= GFC_DESCRIPTOR_RANK (array
);
45 assert (GFC_DESCRIPTOR_RANK (retarray
) == 1);
46 assert (retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
== rank
);
47 if (array
->dim
[0].stride
== 0)
48 array
->dim
[0].stride
= 1;
49 if (retarray
->dim
[0].stride
== 0)
50 retarray
->dim
[0].stride
= 1;
52 dstride
= retarray
->dim
[0].stride
;
53 for (n
= 0; n
< rank
; n
++)
55 sstride
[n
] = array
->dim
[n
].stride
;
56 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
63 dest
= retarray
->data
;
68 /* Initialize the return value. */
69 for (n
= 0; n
< rank
; n
++)
70 dest
[n
* dstride
] = 0;
71 minval
= GFC_INTEGER_4_HUGE
;
76 /* Implementation start. */
81 for (n
= 0; n
< rank
; n
++)
82 dest
[n
* dstride
] = count
[n
] + 1;
84 /* Implementation end. */
86 /* Advance to the next element. */
90 while (count
[n
] == extent
[n
])
92 /* When we get to the end of a dimension, reset it and increment
93 the next dimension. */
95 /* We could precalculate these products, but this is a less
96 frequently used path so proabably not worth it. */
97 base
-= sstride
[n
] * extent
[n
];
101 /* Break out of the loop. */
116 __mminloc0_4_i4 (gfc_array_i4
* retarray
, gfc_array_i4
*array
, gfc_array_l4
* mask
)
118 index_type count
[GFC_MAX_DIMENSIONS
];
119 index_type extent
[GFC_MAX_DIMENSIONS
];
120 index_type sstride
[GFC_MAX_DIMENSIONS
];
121 index_type mstride
[GFC_MAX_DIMENSIONS
];
125 GFC_LOGICAL_4
*mbase
;
129 rank
= GFC_DESCRIPTOR_RANK (array
);
131 assert (GFC_DESCRIPTOR_RANK (retarray
) == 1);
132 assert (retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
== rank
);
133 assert (GFC_DESCRIPTOR_RANK (mask
) == rank
);
135 if (array
->dim
[0].stride
== 0)
136 array
->dim
[0].stride
= 1;
137 if (retarray
->dim
[0].stride
== 0)
138 retarray
->dim
[0].stride
= 1;
139 if (retarray
->dim
[0].stride
== 0)
140 retarray
->dim
[0].stride
= 1;
142 dstride
= retarray
->dim
[0].stride
;
143 for (n
= 0; n
< rank
; n
++)
145 sstride
[n
] = array
->dim
[n
].stride
;
146 mstride
[n
] = mask
->dim
[n
].stride
;
147 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
153 dest
= retarray
->data
;
157 if (GFC_DESCRIPTOR_SIZE (mask
) != 4)
159 /* This allows the same loop to be used for all logical types. */
160 assert (GFC_DESCRIPTOR_SIZE (mask
) == 8);
161 for (n
= 0; n
< rank
; n
++)
163 mbase
= (GFOR_POINTER_L8_TO_L4 (mbase
));
168 GFC_INTEGER_4 minval
;
170 /* Initialize the return value. */
171 for (n
= 0; n
< rank
; n
++)
172 dest
[n
* dstride
] = 0;
173 minval
= GFC_INTEGER_4_HUGE
;
178 /* Implementation start. */
180 if (*mbase
&& *base
< minval
)
183 for (n
= 0; n
< rank
; n
++)
184 dest
[n
* dstride
] = count
[n
] + 1;
186 /* Implementation end. */
188 /* Advance to the next element. */
193 while (count
[n
] == extent
[n
])
195 /* When we get to the end of a dimension, reset it and increment
196 the next dimension. */
198 /* We could precalculate these products, but this is a less
199 frequently used path so proabably not worth it. */
200 base
-= sstride
[n
] * extent
[n
];
201 mbase
-= mstride
[n
] * extent
[n
];
205 /* Break out of the loop. */