1 /* Implementation of the MAXLOC 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_8) && defined (HAVE_GFC_INTEGER_4)
42 extern void maxloc1_4_r8 (gfc_array_i4
* const restrict
,
43 gfc_array_r8
* const restrict
, const index_type
* const restrict
);
44 export_proto(maxloc1_4_r8
);
47 maxloc1_4_r8 (gfc_array_i4
* const restrict retarray
,
48 gfc_array_r8
* const restrict array
,
49 const index_type
* const restrict pdim
)
51 index_type count
[GFC_MAX_DIMENSIONS
];
52 index_type extent
[GFC_MAX_DIMENSIONS
];
53 index_type sstride
[GFC_MAX_DIMENSIONS
];
54 index_type dstride
[GFC_MAX_DIMENSIONS
];
55 const GFC_REAL_8
* restrict base
;
56 GFC_INTEGER_4
* restrict dest
;
63 /* Make dim zero based to avoid confusion. */
65 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
67 /* TODO: It should be a front end job to correctly set the strides. */
69 if (array
->dim
[0].stride
== 0)
70 array
->dim
[0].stride
= 1;
72 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
73 delta
= array
->dim
[dim
].stride
;
75 for (n
= 0; n
< dim
; n
++)
77 sstride
[n
] = array
->dim
[n
].stride
;
78 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
80 for (n
= dim
; n
< rank
; n
++)
82 sstride
[n
] = array
->dim
[n
+ 1].stride
;
84 array
->dim
[n
+ 1].ubound
+ 1 - array
->dim
[n
+ 1].lbound
;
87 if (retarray
->data
== NULL
)
89 for (n
= 0; n
< rank
; n
++)
91 retarray
->dim
[n
].lbound
= 0;
92 retarray
->dim
[n
].ubound
= extent
[n
]-1;
94 retarray
->dim
[n
].stride
= 1;
96 retarray
->dim
[n
].stride
= retarray
->dim
[n
-1].stride
* extent
[n
-1];
100 = internal_malloc_size (sizeof (GFC_INTEGER_4
)
101 * retarray
->dim
[rank
-1].stride
103 retarray
->offset
= 0;
104 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
108 if (retarray
->dim
[0].stride
== 0)
109 retarray
->dim
[0].stride
= 1;
111 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
112 runtime_error ("rank of return array incorrect");
115 for (n
= 0; n
< rank
; n
++)
118 dstride
[n
] = retarray
->dim
[n
].stride
;
124 dest
= retarray
->data
;
128 const GFC_REAL_8
* restrict src
;
129 GFC_INTEGER_4 result
;
134 maxval
= -GFC_REAL_8_HUGE
;
140 for (n
= 0; n
< len
; n
++, src
+= delta
)
146 result
= (GFC_INTEGER_4
)n
+ 1;
152 /* Advance to the next element. */
157 while (count
[n
] == extent
[n
])
159 /* When we get to the end of a dimension, reset it and increment
160 the next dimension. */
162 /* We could precalculate these products, but this is a less
163 frequently used path so proabably not worth it. */
164 base
-= sstride
[n
] * extent
[n
];
165 dest
-= dstride
[n
] * extent
[n
];
169 /* Break out of the look. */
184 extern void mmaxloc1_4_r8 (gfc_array_i4
* const restrict
,
185 gfc_array_r8
* const restrict
, const index_type
* const restrict
,
186 gfc_array_l4
* const restrict
);
187 export_proto(mmaxloc1_4_r8
);
190 mmaxloc1_4_r8 (gfc_array_i4
* const restrict retarray
,
191 gfc_array_r8
* const restrict array
,
192 const index_type
* const restrict pdim
,
193 gfc_array_l4
* const restrict mask
)
195 index_type count
[GFC_MAX_DIMENSIONS
];
196 index_type extent
[GFC_MAX_DIMENSIONS
];
197 index_type sstride
[GFC_MAX_DIMENSIONS
];
198 index_type dstride
[GFC_MAX_DIMENSIONS
];
199 index_type mstride
[GFC_MAX_DIMENSIONS
];
200 GFC_INTEGER_4
* restrict dest
;
201 const GFC_REAL_8
* restrict base
;
202 const GFC_LOGICAL_4
* restrict mbase
;
211 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
213 /* TODO: It should be a front end job to correctly set the strides. */
215 if (array
->dim
[0].stride
== 0)
216 array
->dim
[0].stride
= 1;
218 if (mask
->dim
[0].stride
== 0)
219 mask
->dim
[0].stride
= 1;
221 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
224 delta
= array
->dim
[dim
].stride
;
225 mdelta
= mask
->dim
[dim
].stride
;
227 for (n
= 0; n
< dim
; n
++)
229 sstride
[n
] = array
->dim
[n
].stride
;
230 mstride
[n
] = mask
->dim
[n
].stride
;
231 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
233 for (n
= dim
; n
< rank
; n
++)
235 sstride
[n
] = array
->dim
[n
+ 1].stride
;
236 mstride
[n
] = mask
->dim
[n
+ 1].stride
;
238 array
->dim
[n
+ 1].ubound
+ 1 - array
->dim
[n
+ 1].lbound
;
241 if (retarray
->data
== NULL
)
243 for (n
= 0; n
< rank
; n
++)
245 retarray
->dim
[n
].lbound
= 0;
246 retarray
->dim
[n
].ubound
= extent
[n
]-1;
248 retarray
->dim
[n
].stride
= 1;
250 retarray
->dim
[n
].stride
= retarray
->dim
[n
-1].stride
* extent
[n
-1];
254 = internal_malloc_size (sizeof (GFC_INTEGER_4
)
255 * retarray
->dim
[rank
-1].stride
257 retarray
->offset
= 0;
258 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
262 if (retarray
->dim
[0].stride
== 0)
263 retarray
->dim
[0].stride
= 1;
265 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
266 runtime_error ("rank of return array incorrect");
269 for (n
= 0; n
< rank
; n
++)
272 dstride
[n
] = retarray
->dim
[n
].stride
;
277 dest
= retarray
->data
;
281 if (GFC_DESCRIPTOR_SIZE (mask
) != 4)
283 /* This allows the same loop to be used for all logical types. */
284 assert (GFC_DESCRIPTOR_SIZE (mask
) == 8);
285 for (n
= 0; n
< rank
; n
++)
288 mbase
= (GFOR_POINTER_L8_TO_L4 (mbase
));
293 const GFC_REAL_8
* restrict src
;
294 const GFC_LOGICAL_4
* restrict msrc
;
295 GFC_INTEGER_4 result
;
301 maxval
= -GFC_REAL_8_HUGE
;
307 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
310 if (*msrc
&& *src
> maxval
)
313 result
= (GFC_INTEGER_4
)n
+ 1;
319 /* Advance to the next element. */
325 while (count
[n
] == extent
[n
])
327 /* When we get to the end of a dimension, reset it and increment
328 the next dimension. */
330 /* We could precalculate these products, but this is a less
331 frequently used path so proabably not worth it. */
332 base
-= sstride
[n
] * extent
[n
];
333 mbase
-= mstride
[n
] * extent
[n
];
334 dest
-= dstride
[n
] * extent
[n
];
338 /* Break out of the look. */