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_16) && defined (HAVE_GFC_INTEGER_8)
42 extern void minloc1_8_r16 (gfc_array_i8
*, gfc_array_r16
*, index_type
*);
43 export_proto(minloc1_8_r16
);
46 minloc1_8_r16 (gfc_array_i8
*retarray
, gfc_array_r16
*array
, index_type
*pdim
)
48 index_type count
[GFC_MAX_DIMENSIONS
];
49 index_type extent
[GFC_MAX_DIMENSIONS
];
50 index_type sstride
[GFC_MAX_DIMENSIONS
];
51 index_type dstride
[GFC_MAX_DIMENSIONS
];
60 /* Make dim zero based to avoid confusion. */
62 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
64 /* TODO: It should be a front end job to correctly set the strides. */
66 if (array
->dim
[0].stride
== 0)
67 array
->dim
[0].stride
= 1;
69 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
70 delta
= array
->dim
[dim
].stride
;
72 for (n
= 0; n
< dim
; n
++)
74 sstride
[n
] = array
->dim
[n
].stride
;
75 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
77 for (n
= dim
; n
< rank
; n
++)
79 sstride
[n
] = array
->dim
[n
+ 1].stride
;
81 array
->dim
[n
+ 1].ubound
+ 1 - array
->dim
[n
+ 1].lbound
;
84 if (retarray
->data
== NULL
)
86 for (n
= 0; n
< rank
; n
++)
88 retarray
->dim
[n
].lbound
= 0;
89 retarray
->dim
[n
].ubound
= extent
[n
]-1;
91 retarray
->dim
[n
].stride
= 1;
93 retarray
->dim
[n
].stride
= retarray
->dim
[n
-1].stride
* extent
[n
-1];
97 = internal_malloc_size (sizeof (GFC_INTEGER_8
)
98 * retarray
->dim
[rank
-1].stride
100 retarray
->offset
= 0;
101 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
105 if (retarray
->dim
[0].stride
== 0)
106 retarray
->dim
[0].stride
= 1;
108 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
109 runtime_error ("rank of return array incorrect");
112 for (n
= 0; n
< rank
; n
++)
115 dstride
[n
] = retarray
->dim
[n
].stride
;
121 dest
= retarray
->data
;
126 GFC_INTEGER_8 result
;
131 minval
= GFC_REAL_16_HUGE
;
137 for (n
= 0; n
< len
; n
++, src
+= delta
)
143 result
= (GFC_INTEGER_8
)n
+ 1;
149 /* Advance to the next element. */
154 while (count
[n
] == extent
[n
])
156 /* When we get to the end of a dimension, reset it and increment
157 the next dimension. */
159 /* We could precalculate these products, but this is a less
160 frequently used path so proabably not worth it. */
161 base
-= sstride
[n
] * extent
[n
];
162 dest
-= dstride
[n
] * extent
[n
];
166 /* Break out of the look. */
181 extern void mminloc1_8_r16 (gfc_array_i8
*, gfc_array_r16
*, index_type
*,
183 export_proto(mminloc1_8_r16
);
186 mminloc1_8_r16 (gfc_array_i8
* retarray
, gfc_array_r16
* array
,
187 index_type
*pdim
, gfc_array_l4
* mask
)
189 index_type count
[GFC_MAX_DIMENSIONS
];
190 index_type extent
[GFC_MAX_DIMENSIONS
];
191 index_type sstride
[GFC_MAX_DIMENSIONS
];
192 index_type dstride
[GFC_MAX_DIMENSIONS
];
193 index_type mstride
[GFC_MAX_DIMENSIONS
];
196 GFC_LOGICAL_4
*mbase
;
205 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
207 /* TODO: It should be a front end job to correctly set the strides. */
209 if (array
->dim
[0].stride
== 0)
210 array
->dim
[0].stride
= 1;
212 if (mask
->dim
[0].stride
== 0)
213 mask
->dim
[0].stride
= 1;
215 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
218 delta
= array
->dim
[dim
].stride
;
219 mdelta
= mask
->dim
[dim
].stride
;
221 for (n
= 0; n
< dim
; n
++)
223 sstride
[n
] = array
->dim
[n
].stride
;
224 mstride
[n
] = mask
->dim
[n
].stride
;
225 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
227 for (n
= dim
; n
< rank
; n
++)
229 sstride
[n
] = array
->dim
[n
+ 1].stride
;
230 mstride
[n
] = mask
->dim
[n
+ 1].stride
;
232 array
->dim
[n
+ 1].ubound
+ 1 - array
->dim
[n
+ 1].lbound
;
235 if (retarray
->data
== NULL
)
237 for (n
= 0; n
< rank
; n
++)
239 retarray
->dim
[n
].lbound
= 0;
240 retarray
->dim
[n
].ubound
= extent
[n
]-1;
242 retarray
->dim
[n
].stride
= 1;
244 retarray
->dim
[n
].stride
= retarray
->dim
[n
-1].stride
* extent
[n
-1];
248 = internal_malloc_size (sizeof (GFC_INTEGER_8
)
249 * retarray
->dim
[rank
-1].stride
251 retarray
->offset
= 0;
252 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
256 if (retarray
->dim
[0].stride
== 0)
257 retarray
->dim
[0].stride
= 1;
259 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
260 runtime_error ("rank of return array incorrect");
263 for (n
= 0; n
< rank
; n
++)
266 dstride
[n
] = retarray
->dim
[n
].stride
;
271 dest
= retarray
->data
;
275 if (GFC_DESCRIPTOR_SIZE (mask
) != 4)
277 /* This allows the same loop to be used for all logical types. */
278 assert (GFC_DESCRIPTOR_SIZE (mask
) == 8);
279 for (n
= 0; n
< rank
; n
++)
282 mbase
= (GFOR_POINTER_L8_TO_L4 (mbase
));
289 GFC_INTEGER_8 result
;
295 minval
= GFC_REAL_16_HUGE
;
301 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
304 if (*msrc
&& *src
< minval
)
307 result
= (GFC_INTEGER_8
)n
+ 1;
313 /* Advance to the next element. */
319 while (count
[n
] == extent
[n
])
321 /* When we get to the end of a dimension, reset it and increment
322 the next dimension. */
324 /* We could precalculate these products, but this is a less
325 frequently used path so proabably not worth it. */
326 base
-= sstride
[n
] * extent
[n
];
327 mbase
-= mstride
[n
] * extent
[n
];
328 dest
-= dstride
[n
] * extent
[n
];
332 /* Break out of the look. */