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"
31 extern void __minloc0_4_r4 (gfc_array_i4
* retarray
, gfc_array_r4
*array
);
32 export_proto_np(__minloc0_4_r4
);
35 __minloc0_4_r4 (gfc_array_i4
* retarray
, gfc_array_r4
*array
)
37 index_type count
[GFC_MAX_DIMENSIONS
];
38 index_type extent
[GFC_MAX_DIMENSIONS
];
39 index_type sstride
[GFC_MAX_DIMENSIONS
];
46 rank
= GFC_DESCRIPTOR_RANK (array
);
48 assert (GFC_DESCRIPTOR_RANK (retarray
) == 1);
49 assert (retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
== rank
);
50 if (array
->dim
[0].stride
== 0)
51 array
->dim
[0].stride
= 1;
52 if (retarray
->dim
[0].stride
== 0)
53 retarray
->dim
[0].stride
= 1;
55 dstride
= retarray
->dim
[0].stride
;
56 dest
= retarray
->data
;
57 for (n
= 0; n
< rank
; n
++)
59 sstride
[n
] = array
->dim
[n
].stride
;
60 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
64 /* Set the return value. */
65 for (n
= 0; n
< rank
; n
++)
66 dest
[n
* dstride
] = 0;
73 /* Initialize the return value. */
74 for (n
= 0; n
< rank
; n
++)
75 dest
[n
* dstride
] = 1;
80 minval
= GFC_REAL_4_HUGE
;
85 /* Implementation start. */
90 for (n
= 0; n
< rank
; n
++)
91 dest
[n
* dstride
] = count
[n
] + 1;
93 /* Implementation end. */
95 /* Advance to the next element. */
99 while (count
[n
] == extent
[n
])
101 /* When we get to the end of a dimension, reset it and increment
102 the next dimension. */
104 /* We could precalculate these products, but this is a less
105 frequently used path so proabably not worth it. */
106 base
-= sstride
[n
] * extent
[n
];
110 /* Break out of the loop. */
125 extern void __mminloc0_4_r4 (gfc_array_i4
*, gfc_array_r4
*, gfc_array_l4
*);
126 export_proto_np(__mminloc0_4_r4
);
129 __mminloc0_4_r4 (gfc_array_i4
* retarray
, gfc_array_r4
*array
, gfc_array_l4
* mask
)
131 index_type count
[GFC_MAX_DIMENSIONS
];
132 index_type extent
[GFC_MAX_DIMENSIONS
];
133 index_type sstride
[GFC_MAX_DIMENSIONS
];
134 index_type mstride
[GFC_MAX_DIMENSIONS
];
138 GFC_LOGICAL_4
*mbase
;
142 rank
= GFC_DESCRIPTOR_RANK (array
);
144 assert (GFC_DESCRIPTOR_RANK (retarray
) == 1);
145 assert (retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
== rank
);
146 assert (GFC_DESCRIPTOR_RANK (mask
) == rank
);
148 if (array
->dim
[0].stride
== 0)
149 array
->dim
[0].stride
= 1;
150 if (retarray
->dim
[0].stride
== 0)
151 retarray
->dim
[0].stride
= 1;
152 if (retarray
->dim
[0].stride
== 0)
153 retarray
->dim
[0].stride
= 1;
155 dstride
= retarray
->dim
[0].stride
;
156 dest
= retarray
->data
;
157 for (n
= 0; n
< rank
; n
++)
159 sstride
[n
] = array
->dim
[n
].stride
;
160 mstride
[n
] = mask
->dim
[n
].stride
;
161 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
165 /* Set the return value. */
166 for (n
= 0; n
< rank
; n
++)
167 dest
[n
* dstride
] = 0;
175 if (GFC_DESCRIPTOR_SIZE (mask
) != 4)
177 /* This allows the same loop to be used for all logical types. */
178 assert (GFC_DESCRIPTOR_SIZE (mask
) == 8);
179 for (n
= 0; n
< rank
; n
++)
181 mbase
= (GFOR_POINTER_L8_TO_L4 (mbase
));
185 /* Initialize the return value. */
186 for (n
= 0; n
< rank
; n
++)
187 dest
[n
* dstride
] = 1;
192 minval
= GFC_REAL_4_HUGE
;
197 /* Implementation start. */
199 if (*mbase
&& *base
< minval
)
202 for (n
= 0; n
< rank
; n
++)
203 dest
[n
* dstride
] = count
[n
] + 1;
205 /* Implementation end. */
207 /* Advance to the next element. */
212 while (count
[n
] == extent
[n
])
214 /* When we get to the end of a dimension, reset it and increment
215 the next dimension. */
217 /* We could precalculate these products, but this is a less
218 frequently used path so proabably not worth it. */
219 base
-= sstride
[n
] * extent
[n
];
220 mbase
-= mstride
[n
] * extent
[n
];
224 /* Break out of the loop. */