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_10) && defined (HAVE_GFC_INTEGER_4)
42 extern void maxloc1_4_r10 (gfc_array_i4
* const restrict
,
43 gfc_array_r10
* const restrict
, const index_type
* const restrict
);
44 export_proto(maxloc1_4_r10
);
47 maxloc1_4_r10 (gfc_array_i4
* const restrict retarray
,
48 gfc_array_r10
* 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_10
* restrict base
;
56 GFC_INTEGER_4
* restrict dest
;
63 /* Make dim zero based to avoid confusion. */
65 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
67 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
68 delta
= array
->dim
[dim
].stride
;
70 for (n
= 0; n
< dim
; n
++)
72 sstride
[n
] = array
->dim
[n
].stride
;
73 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
78 for (n
= dim
; n
< rank
; n
++)
80 sstride
[n
] = array
->dim
[n
+ 1].stride
;
82 array
->dim
[n
+ 1].ubound
+ 1 - array
->dim
[n
+ 1].lbound
;
88 if (retarray
->data
== NULL
)
92 for (n
= 0; n
< rank
; n
++)
94 retarray
->dim
[n
].lbound
= 0;
95 retarray
->dim
[n
].ubound
= extent
[n
]-1;
97 retarray
->dim
[n
].stride
= 1;
99 retarray
->dim
[n
].stride
= retarray
->dim
[n
-1].stride
* extent
[n
-1];
102 retarray
->offset
= 0;
103 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
105 alloc_size
= sizeof (GFC_INTEGER_4
) * retarray
->dim
[rank
-1].stride
110 /* Make sure we have a zero-sized array. */
111 retarray
->dim
[0].lbound
= 0;
112 retarray
->dim
[0].ubound
= -1;
116 retarray
->data
= internal_malloc_size (alloc_size
);
120 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
121 runtime_error ("rank of return array incorrect");
124 for (n
= 0; n
< rank
; n
++)
127 dstride
[n
] = retarray
->dim
[n
].stride
;
133 dest
= retarray
->data
;
137 const GFC_REAL_10
* restrict src
;
138 GFC_INTEGER_4 result
;
143 maxval
= -GFC_REAL_10_HUGE
;
149 for (n
= 0; n
< len
; n
++, src
+= delta
)
152 if (*src
> maxval
|| !result
)
155 result
= (GFC_INTEGER_4
)n
+ 1;
161 /* Advance to the next element. */
166 while (count
[n
] == extent
[n
])
168 /* When we get to the end of a dimension, reset it and increment
169 the next dimension. */
171 /* We could precalculate these products, but this is a less
172 frequently used path so probably not worth it. */
173 base
-= sstride
[n
] * extent
[n
];
174 dest
-= dstride
[n
] * extent
[n
];
178 /* Break out of the look. */
193 extern void mmaxloc1_4_r10 (gfc_array_i4
* const restrict
,
194 gfc_array_r10
* const restrict
, const index_type
* const restrict
,
195 gfc_array_l4
* const restrict
);
196 export_proto(mmaxloc1_4_r10
);
199 mmaxloc1_4_r10 (gfc_array_i4
* const restrict retarray
,
200 gfc_array_r10
* const restrict array
,
201 const index_type
* const restrict pdim
,
202 gfc_array_l4
* const restrict mask
)
204 index_type count
[GFC_MAX_DIMENSIONS
];
205 index_type extent
[GFC_MAX_DIMENSIONS
];
206 index_type sstride
[GFC_MAX_DIMENSIONS
];
207 index_type dstride
[GFC_MAX_DIMENSIONS
];
208 index_type mstride
[GFC_MAX_DIMENSIONS
];
209 GFC_INTEGER_4
* restrict dest
;
210 const GFC_REAL_10
* restrict base
;
211 const GFC_LOGICAL_4
* restrict mbase
;
220 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
222 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
225 delta
= array
->dim
[dim
].stride
;
226 mdelta
= mask
->dim
[dim
].stride
;
228 for (n
= 0; n
< dim
; n
++)
230 sstride
[n
] = array
->dim
[n
].stride
;
231 mstride
[n
] = mask
->dim
[n
].stride
;
232 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
238 for (n
= dim
; n
< rank
; n
++)
240 sstride
[n
] = array
->dim
[n
+ 1].stride
;
241 mstride
[n
] = mask
->dim
[n
+ 1].stride
;
243 array
->dim
[n
+ 1].ubound
+ 1 - array
->dim
[n
+ 1].lbound
;
249 if (retarray
->data
== NULL
)
253 for (n
= 0; n
< rank
; n
++)
255 retarray
->dim
[n
].lbound
= 0;
256 retarray
->dim
[n
].ubound
= extent
[n
]-1;
258 retarray
->dim
[n
].stride
= 1;
260 retarray
->dim
[n
].stride
= retarray
->dim
[n
-1].stride
* extent
[n
-1];
263 alloc_size
= sizeof (GFC_INTEGER_4
) * retarray
->dim
[rank
-1].stride
266 retarray
->offset
= 0;
267 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
271 /* Make sure we have a zero-sized array. */
272 retarray
->dim
[0].lbound
= 0;
273 retarray
->dim
[0].ubound
= -1;
277 retarray
->data
= internal_malloc_size (alloc_size
);
282 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
283 runtime_error ("rank of return array incorrect");
286 for (n
= 0; n
< rank
; n
++)
289 dstride
[n
] = retarray
->dim
[n
].stride
;
294 dest
= retarray
->data
;
298 if (GFC_DESCRIPTOR_SIZE (mask
) != 4)
300 /* This allows the same loop to be used for all logical types. */
301 assert (GFC_DESCRIPTOR_SIZE (mask
) == 8);
302 for (n
= 0; n
< rank
; n
++)
305 mbase
= (GFOR_POINTER_L8_TO_L4 (mbase
));
310 const GFC_REAL_10
* restrict src
;
311 const GFC_LOGICAL_4
* restrict msrc
;
312 GFC_INTEGER_4 result
;
318 maxval
= -GFC_REAL_10_HUGE
;
324 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
327 if (*msrc
&& (*src
> maxval
|| !result
))
330 result
= (GFC_INTEGER_4
)n
+ 1;
336 /* Advance to the next element. */
342 while (count
[n
] == extent
[n
])
344 /* When we get to the end of a dimension, reset it and increment
345 the next dimension. */
347 /* We could precalculate these products, but this is a less
348 frequently used path so probably not worth it. */
349 base
-= sstride
[n
] * extent
[n
];
350 mbase
-= mstride
[n
] * extent
[n
];
351 dest
-= dstride
[n
] * extent
[n
];
355 /* Break out of the look. */
371 extern void smaxloc1_4_r10 (gfc_array_i4
* const restrict
,
372 gfc_array_r10
* const restrict
, const index_type
* const restrict
,
374 export_proto(smaxloc1_4_r10
);
377 smaxloc1_4_r10 (gfc_array_i4
* const restrict retarray
,
378 gfc_array_r10
* const restrict array
,
379 const index_type
* const restrict pdim
,
380 GFC_LOGICAL_4
* mask
)
389 maxloc1_4_r10 (retarray
, array
, pdim
);
392 rank
= GFC_DESCRIPTOR_RANK (array
);
394 runtime_error ("Rank of array needs to be > 0");
396 if (retarray
->data
== NULL
)
398 retarray
->dim
[0].lbound
= 0;
399 retarray
->dim
[0].ubound
= rank
-1;
400 retarray
->dim
[0].stride
= 1;
401 retarray
->dtype
= (retarray
->dtype
& ~GFC_DTYPE_RANK_MASK
) | 1;
402 retarray
->offset
= 0;
403 retarray
->data
= internal_malloc_size (sizeof (GFC_INTEGER_4
) * rank
);
407 if (GFC_DESCRIPTOR_RANK (retarray
) != 1)
408 runtime_error ("rank of return array does not equal 1");
410 if (retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
!= rank
)
411 runtime_error ("dimension of return array incorrect");
414 dstride
= retarray
->dim
[0].stride
;
415 dest
= retarray
->data
;
417 for (n
= 0; n
< rank
; n
++)
418 dest
[n
* dstride
] = 0 ;