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_2) && defined (HAVE_GFC_INTEGER_2)
41 extern void maxval_i2 (gfc_array_i2
* const restrict
,
42 gfc_array_i2
* const restrict
, const index_type
* const restrict
);
43 export_proto(maxval_i2
);
46 maxval_i2 (gfc_array_i2
* const restrict retarray
,
47 gfc_array_i2
* 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_2
* restrict base
;
55 GFC_INTEGER_2
* 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
;
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
;
87 if (retarray
->data
== NULL
)
91 for (n
= 0; n
< rank
; n
++)
93 retarray
->dim
[n
].lbound
= 0;
94 retarray
->dim
[n
].ubound
= extent
[n
]-1;
96 retarray
->dim
[n
].stride
= 1;
98 retarray
->dim
[n
].stride
= retarray
->dim
[n
-1].stride
* extent
[n
-1];
101 retarray
->offset
= 0;
102 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
104 alloc_size
= sizeof (GFC_INTEGER_2
) * retarray
->dim
[rank
-1].stride
109 /* Make sure we have a zero-sized array. */
110 retarray
->dim
[0].lbound
= 0;
111 retarray
->dim
[0].ubound
= -1;
115 retarray
->data
= internal_malloc_size (alloc_size
);
119 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
120 runtime_error ("rank of return array incorrect");
123 for (n
= 0; n
< rank
; n
++)
126 dstride
[n
] = retarray
->dim
[n
].stride
;
132 dest
= retarray
->data
;
136 const GFC_INTEGER_2
* restrict src
;
137 GFC_INTEGER_2 result
;
141 result
= (-GFC_INTEGER_2_HUGE
-1);
143 *dest
= (-GFC_INTEGER_2_HUGE
-1);
146 for (n
= 0; n
< len
; n
++, src
+= delta
)
155 /* Advance to the next element. */
160 while (count
[n
] == extent
[n
])
162 /* When we get to the end of a dimension, reset it and increment
163 the next dimension. */
165 /* We could precalculate these products, but this is a less
166 frequently used path so probably not worth it. */
167 base
-= sstride
[n
] * extent
[n
];
168 dest
-= dstride
[n
] * extent
[n
];
172 /* Break out of the look. */
187 extern void mmaxval_i2 (gfc_array_i2
* const restrict
,
188 gfc_array_i2
* const restrict
, const index_type
* const restrict
,
189 gfc_array_l4
* const restrict
);
190 export_proto(mmaxval_i2
);
193 mmaxval_i2 (gfc_array_i2
* const restrict retarray
,
194 gfc_array_i2
* const restrict array
,
195 const index_type
* const restrict pdim
,
196 gfc_array_l4
* const restrict mask
)
198 index_type count
[GFC_MAX_DIMENSIONS
];
199 index_type extent
[GFC_MAX_DIMENSIONS
];
200 index_type sstride
[GFC_MAX_DIMENSIONS
];
201 index_type dstride
[GFC_MAX_DIMENSIONS
];
202 index_type mstride
[GFC_MAX_DIMENSIONS
];
203 GFC_INTEGER_2
* restrict dest
;
204 const GFC_INTEGER_2
* restrict base
;
205 const GFC_LOGICAL_4
* restrict mbase
;
214 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
216 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
219 delta
= array
->dim
[dim
].stride
;
220 mdelta
= mask
->dim
[dim
].stride
;
222 for (n
= 0; n
< dim
; n
++)
224 sstride
[n
] = array
->dim
[n
].stride
;
225 mstride
[n
] = mask
->dim
[n
].stride
;
226 extent
[n
] = array
->dim
[n
].ubound
+ 1 - array
->dim
[n
].lbound
;
232 for (n
= dim
; n
< rank
; n
++)
234 sstride
[n
] = array
->dim
[n
+ 1].stride
;
235 mstride
[n
] = mask
->dim
[n
+ 1].stride
;
237 array
->dim
[n
+ 1].ubound
+ 1 - array
->dim
[n
+ 1].lbound
;
243 if (retarray
->data
== NULL
)
247 for (n
= 0; n
< rank
; n
++)
249 retarray
->dim
[n
].lbound
= 0;
250 retarray
->dim
[n
].ubound
= extent
[n
]-1;
252 retarray
->dim
[n
].stride
= 1;
254 retarray
->dim
[n
].stride
= retarray
->dim
[n
-1].stride
* extent
[n
-1];
257 alloc_size
= sizeof (GFC_INTEGER_2
) * retarray
->dim
[rank
-1].stride
260 retarray
->offset
= 0;
261 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
265 /* Make sure we have a zero-sized array. */
266 retarray
->dim
[0].lbound
= 0;
267 retarray
->dim
[0].ubound
= -1;
271 retarray
->data
= internal_malloc_size (alloc_size
);
276 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
277 runtime_error ("rank of return array incorrect");
280 for (n
= 0; n
< rank
; n
++)
283 dstride
[n
] = retarray
->dim
[n
].stride
;
288 dest
= retarray
->data
;
292 if (GFC_DESCRIPTOR_SIZE (mask
) != 4)
294 /* This allows the same loop to be used for all logical types. */
295 assert (GFC_DESCRIPTOR_SIZE (mask
) == 8);
296 for (n
= 0; n
< rank
; n
++)
299 mbase
= (GFOR_POINTER_L8_TO_L4 (mbase
));
304 const GFC_INTEGER_2
* restrict src
;
305 const GFC_LOGICAL_4
* restrict msrc
;
306 GFC_INTEGER_2 result
;
311 result
= (-GFC_INTEGER_2_HUGE
-1);
313 *dest
= (-GFC_INTEGER_2_HUGE
-1);
316 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
319 if (*msrc
&& *src
> result
)
325 /* Advance to the next element. */
331 while (count
[n
] == extent
[n
])
333 /* When we get to the end of a dimension, reset it and increment
334 the next dimension. */
336 /* We could precalculate these products, but this is a less
337 frequently used path so probably not worth it. */
338 base
-= sstride
[n
] * extent
[n
];
339 mbase
-= mstride
[n
] * extent
[n
];
340 dest
-= dstride
[n
] * extent
[n
];
344 /* Break out of the look. */
360 extern void smaxval_i2 (gfc_array_i2
* const restrict
,
361 gfc_array_i2
* const restrict
, const index_type
* const restrict
,
363 export_proto(smaxval_i2
);
366 smaxval_i2 (gfc_array_i2
* const restrict retarray
,
367 gfc_array_i2
* const restrict array
,
368 const index_type
* const restrict pdim
,
369 GFC_LOGICAL_4
* mask
)
378 maxval_i2 (retarray
, array
, pdim
);
381 rank
= GFC_DESCRIPTOR_RANK (array
);
383 runtime_error ("Rank of array needs to be > 0");
385 if (retarray
->data
== NULL
)
387 retarray
->dim
[0].lbound
= 0;
388 retarray
->dim
[0].ubound
= rank
-1;
389 retarray
->dim
[0].stride
= 1;
390 retarray
->dtype
= (retarray
->dtype
& ~GFC_DTYPE_RANK_MASK
) | 1;
391 retarray
->offset
= 0;
392 retarray
->data
= internal_malloc_size (sizeof (GFC_INTEGER_2
) * rank
);
396 if (GFC_DESCRIPTOR_RANK (retarray
) != 1)
397 runtime_error ("rank of return array does not equal 1");
399 if (retarray
->dim
[0].ubound
+ 1 - retarray
->dim
[0].lbound
!= rank
)
400 runtime_error ("dimension of return array incorrect");
403 dstride
= retarray
->dim
[0].stride
;
404 dest
= retarray
->data
;
406 for (n
= 0; n
< rank
; n
++)
407 dest
[n
* dstride
] = (-GFC_INTEGER_2_HUGE
-1) ;