1 /* Implementation of the MAXVAL 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. */
35 #include "libgfortran.h"
38 #if defined (HAVE_GFC_INTEGER_8) && defined (HAVE_GFC_INTEGER_8)
41 extern void maxval_i8 (gfc_array_i8
* const restrict
,
42 gfc_array_i8
* const restrict
, const index_type
* const restrict
);
43 export_proto(maxval_i8
);
46 maxval_i8 (gfc_array_i8
* const restrict retarray
,
47 gfc_array_i8
* const restrict array
,
48 const index_type
* const restrict pdim
)
50 index_type count
[GFC_MAX_DIMENSIONS
];
51 index_type extent
[GFC_MAX_DIMENSIONS
];
52 index_type sstride
[GFC_MAX_DIMENSIONS
];
53 index_type dstride
[GFC_MAX_DIMENSIONS
];
54 const GFC_INTEGER_8
* restrict base
;
55 GFC_INTEGER_8
* restrict dest
;
62 /* Make dim zero based to avoid confusion. */
64 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
66 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
67 delta
= array
->dim
[dim
].stride
;
69 for (n
= 0; n
< dim
; n
++)
71 sstride
[n
] = array
->dim
[n
].stride
;
72 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
74 for (n
= dim
; n
< rank
; n
++)
76 sstride
[n
] = array
->dim
[n
+ 1].stride
;
78 array
->dim
[n
+ 1].ubound
+ 1 - array
->dim
[n
+ 1].lbound
;
81 if (retarray
->data
== NULL
)
83 for (n
= 0; n
< rank
; n
++)
85 retarray
->dim
[n
].lbound
= 0;
86 retarray
->dim
[n
].ubound
= extent
[n
]-1;
88 retarray
->dim
[n
].stride
= 1;
90 retarray
->dim
[n
].stride
= retarray
->dim
[n
-1].stride
* extent
[n
-1];
94 = internal_malloc_size (sizeof (GFC_INTEGER_8
)
95 * retarray
->dim
[rank
-1].stride
98 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
102 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
103 runtime_error ("rank of return array incorrect");
106 for (n
= 0; n
< rank
; n
++)
109 dstride
[n
] = retarray
->dim
[n
].stride
;
115 dest
= retarray
->data
;
119 const GFC_INTEGER_8
* restrict src
;
120 GFC_INTEGER_8 result
;
124 result
= -GFC_INTEGER_8_HUGE
;
126 *dest
= -GFC_INTEGER_8_HUGE
;
129 for (n
= 0; n
< len
; n
++, src
+= delta
)
138 /* Advance to the next element. */
143 while (count
[n
] == extent
[n
])
145 /* When we get to the end of a dimension, reset it and increment
146 the next dimension. */
148 /* We could precalculate these products, but this is a less
149 frequently used path so proabably not worth it. */
150 base
-= sstride
[n
] * extent
[n
];
151 dest
-= dstride
[n
] * extent
[n
];
155 /* Break out of the look. */
170 extern void mmaxval_i8 (gfc_array_i8
* const restrict
,
171 gfc_array_i8
* const restrict
, const index_type
* const restrict
,
172 gfc_array_l4
* const restrict
);
173 export_proto(mmaxval_i8
);
176 mmaxval_i8 (gfc_array_i8
* const restrict retarray
,
177 gfc_array_i8
* const restrict array
,
178 const index_type
* const restrict pdim
,
179 gfc_array_l4
* const restrict mask
)
181 index_type count
[GFC_MAX_DIMENSIONS
];
182 index_type extent
[GFC_MAX_DIMENSIONS
];
183 index_type sstride
[GFC_MAX_DIMENSIONS
];
184 index_type dstride
[GFC_MAX_DIMENSIONS
];
185 index_type mstride
[GFC_MAX_DIMENSIONS
];
186 GFC_INTEGER_8
* restrict dest
;
187 const GFC_INTEGER_8
* restrict base
;
188 const GFC_LOGICAL_4
* restrict mbase
;
197 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
199 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
202 delta
= array
->dim
[dim
].stride
;
203 mdelta
= mask
->dim
[dim
].stride
;
205 for (n
= 0; n
< dim
; n
++)
207 sstride
[n
] = array
->dim
[n
].stride
;
208 mstride
[n
] = mask
->dim
[n
].stride
;
209 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
211 for (n
= dim
; n
< rank
; n
++)
213 sstride
[n
] = array
->dim
[n
+ 1].stride
;
214 mstride
[n
] = mask
->dim
[n
+ 1].stride
;
216 array
->dim
[n
+ 1].ubound
+ 1 - array
->dim
[n
+ 1].lbound
;
219 if (retarray
->data
== NULL
)
221 for (n
= 0; n
< rank
; n
++)
223 retarray
->dim
[n
].lbound
= 0;
224 retarray
->dim
[n
].ubound
= extent
[n
]-1;
226 retarray
->dim
[n
].stride
= 1;
228 retarray
->dim
[n
].stride
= retarray
->dim
[n
-1].stride
* extent
[n
-1];
232 = internal_malloc_size (sizeof (GFC_INTEGER_8
)
233 * retarray
->dim
[rank
-1].stride
235 retarray
->offset
= 0;
236 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
240 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
241 runtime_error ("rank of return array incorrect");
244 for (n
= 0; n
< rank
; n
++)
247 dstride
[n
] = retarray
->dim
[n
].stride
;
252 dest
= retarray
->data
;
256 if (GFC_DESCRIPTOR_SIZE (mask
) != 4)
258 /* This allows the same loop to be used for all logical types. */
259 assert (GFC_DESCRIPTOR_SIZE (mask
) == 8);
260 for (n
= 0; n
< rank
; n
++)
263 mbase
= (GFOR_POINTER_L8_TO_L4 (mbase
));
268 const GFC_INTEGER_8
* restrict src
;
269 const GFC_LOGICAL_4
* restrict msrc
;
270 GFC_INTEGER_8 result
;
275 result
= -GFC_INTEGER_8_HUGE
;
277 *dest
= -GFC_INTEGER_8_HUGE
;
280 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
283 if (*msrc
&& *src
> result
)
289 /* Advance to the next element. */
295 while (count
[n
] == extent
[n
])
297 /* When we get to the end of a dimension, reset it and increment
298 the next dimension. */
300 /* We could precalculate these products, but this is a less
301 frequently used path so proabably not worth it. */
302 base
-= sstride
[n
] * extent
[n
];
303 mbase
-= mstride
[n
] * extent
[n
];
304 dest
-= dstride
[n
] * extent
[n
];
308 /* Break out of the look. */
324 extern void smaxval_i8 (gfc_array_i8
* const restrict
,
325 gfc_array_i8
* const restrict
, const index_type
* const restrict
,
327 export_proto(smaxval_i8
);
330 smaxval_i8 (gfc_array_i8
* const restrict retarray
,
331 gfc_array_i8
* const restrict array
,
332 const index_type
* const restrict pdim
,
333 GFC_LOGICAL_4
* mask
)
342 maxval_i8 (retarray
, array
, pdim
);
345 rank
= GFC_DESCRIPTOR_RANK (array
);
347 runtime_error ("Rank of array needs to be > 0");
349 if (retarray
->data
== NULL
)
351 retarray
->dim
[0].lbound
= 0;
352 retarray
->dim
[0].ubound
= rank
-1;
353 retarray
->dim
[0].stride
= 1;
354 retarray
->dtype
= (retarray
->dtype
& ~GFC_DTYPE_RANK_MASK
) | 1;
355 retarray
->offset
= 0;
356 retarray
->data
= internal_malloc_size (sizeof (GFC_INTEGER_8
) * rank
);
360 if (GFC_DESCRIPTOR_RANK (retarray
) != 1)
361 runtime_error ("rank of return array does not equal 1");
363 if (retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
!= rank
)
364 runtime_error ("dimension of return array incorrect");
367 dstride
= retarray
->dim
[0].stride
;
368 dest
= retarray
->data
;
370 for (n
= 0; n
< rank
; n
++)
371 dest
[n
* dstride
] = -GFC_INTEGER_8_HUGE
;