1 /* Implementation of the MAXVAL intrinsic
2 Copyright (C) 2002-2018 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 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 3 of the License, or (at your option) any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
29 #if defined (HAVE_GFC_REAL_10) && defined (HAVE_GFC_REAL_10)
32 extern void maxval_r10 (gfc_array_r10
* const restrict
,
33 gfc_array_r10
* const restrict
, const index_type
* const restrict
);
34 export_proto(maxval_r10
);
37 maxval_r10 (gfc_array_r10
* const restrict retarray
,
38 gfc_array_r10
* const restrict array
,
39 const index_type
* const restrict pdim
)
41 index_type count
[GFC_MAX_DIMENSIONS
];
42 index_type extent
[GFC_MAX_DIMENSIONS
];
43 index_type sstride
[GFC_MAX_DIMENSIONS
];
44 index_type dstride
[GFC_MAX_DIMENSIONS
];
45 const GFC_REAL_10
* restrict base
;
46 GFC_REAL_10
* restrict dest
;
58 /* Make dim zero based to avoid confusion. */
59 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
62 if (unlikely (dim
< 0 || dim
> rank
))
64 runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
65 "is %ld, should be between 1 and %ld",
66 (long int) dim
+ 1, (long int) rank
+ 1);
69 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
72 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
74 for (n
= 0; n
< dim
; n
++)
76 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
77 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
82 for (n
= dim
; n
< rank
; n
++)
84 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
, n
+ 1);
85 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
91 if (retarray
->base_addr
== NULL
)
93 size_t alloc_size
, str
;
95 for (n
= 0; n
< rank
; n
++)
100 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
102 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
106 retarray
->offset
= 0;
107 GFC_DTYPE_COPY_SETRANK(retarray
,array
,rank
);
109 alloc_size
= GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1) * extent
[rank
-1];
111 retarray
->base_addr
= xmallocarray (alloc_size
, sizeof (GFC_REAL_10
));
114 /* Make sure we have a zero-sized array. */
115 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
122 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
123 runtime_error ("rank of return array incorrect in"
124 " MAXVAL intrinsic: is %ld, should be %ld",
125 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
128 if (unlikely (compile_options
.bounds_check
))
129 bounds_ifunction_return ((array_t
*) retarray
, extent
,
130 "return value", "MAXVAL");
133 for (n
= 0; n
< rank
; n
++)
136 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
141 base
= array
->base_addr
;
142 dest
= retarray
->base_addr
;
145 while (continue_loop
)
147 const GFC_REAL_10
* restrict src
;
152 #if defined (GFC_REAL_10_INFINITY)
153 result
= -GFC_REAL_10_INFINITY
;
155 result
= -GFC_REAL_10_HUGE
;
158 *dest
= -GFC_REAL_10_HUGE
;
161 for (n
= 0; n
< len
; n
++, src
+= delta
)
164 #if defined (GFC_REAL_10_QUIET_NAN)
168 if (unlikely (n
>= len
))
169 result
= GFC_REAL_10_QUIET_NAN
;
170 else for (; n
< len
; n
++, src
+= delta
)
180 /* Advance to the next element. */
185 while (count
[n
] == extent
[n
])
187 /* When we get to the end of a dimension, reset it and increment
188 the next dimension. */
190 /* We could precalculate these products, but this is a less
191 frequently used path so probably not worth it. */
192 base
-= sstride
[n
] * extent
[n
];
193 dest
-= dstride
[n
] * extent
[n
];
197 /* Break out of the loop. */
212 extern void mmaxval_r10 (gfc_array_r10
* const restrict
,
213 gfc_array_r10
* const restrict
, const index_type
* const restrict
,
214 gfc_array_l1
* const restrict
);
215 export_proto(mmaxval_r10
);
218 mmaxval_r10 (gfc_array_r10
* const restrict retarray
,
219 gfc_array_r10
* const restrict array
,
220 const index_type
* const restrict pdim
,
221 gfc_array_l1
* const restrict mask
)
223 index_type count
[GFC_MAX_DIMENSIONS
];
224 index_type extent
[GFC_MAX_DIMENSIONS
];
225 index_type sstride
[GFC_MAX_DIMENSIONS
];
226 index_type dstride
[GFC_MAX_DIMENSIONS
];
227 index_type mstride
[GFC_MAX_DIMENSIONS
];
228 GFC_REAL_10
* restrict dest
;
229 const GFC_REAL_10
* restrict base
;
230 const GFC_LOGICAL_1
* restrict mbase
;
243 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
246 if (unlikely (dim
< 0 || dim
> rank
))
248 runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
249 "is %ld, should be between 1 and %ld",
250 (long int) dim
+ 1, (long int) rank
+ 1);
253 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
257 mbase
= mask
->base_addr
;
259 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
261 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
262 #ifdef HAVE_GFC_LOGICAL_16
266 mbase
= GFOR_POINTER_TO_L1 (mbase
, mask_kind
);
268 runtime_error ("Funny sized logical array");
270 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
271 mdelta
= GFC_DESCRIPTOR_STRIDE_BYTES(mask
,dim
);
273 for (n
= 0; n
< dim
; n
++)
275 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
276 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
,n
);
277 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
283 for (n
= dim
; n
< rank
; n
++)
285 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
+ 1);
286 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
, n
+ 1);
287 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
293 if (retarray
->base_addr
== NULL
)
295 size_t alloc_size
, str
;
297 for (n
= 0; n
< rank
; n
++)
302 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
304 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
308 alloc_size
= GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1) * extent
[rank
-1];
310 retarray
->offset
= 0;
311 GFC_DTYPE_COPY_SETRANK(retarray
,array
,rank
);
315 /* Make sure we have a zero-sized array. */
316 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
320 retarray
->base_addr
= xmallocarray (alloc_size
, sizeof (GFC_REAL_10
));
325 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
326 runtime_error ("rank of return array incorrect in MAXVAL intrinsic");
328 if (unlikely (compile_options
.bounds_check
))
330 bounds_ifunction_return ((array_t
*) retarray
, extent
,
331 "return value", "MAXVAL");
332 bounds_equal_extents ((array_t
*) mask
, (array_t
*) array
,
333 "MASK argument", "MAXVAL");
337 for (n
= 0; n
< rank
; n
++)
340 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
345 dest
= retarray
->base_addr
;
346 base
= array
->base_addr
;
350 const GFC_REAL_10
* restrict src
;
351 const GFC_LOGICAL_1
* restrict msrc
;
357 #if defined (GFC_REAL_10_INFINITY)
358 result
= -GFC_REAL_10_INFINITY
;
360 result
= -GFC_REAL_10_HUGE
;
362 #if defined (GFC_REAL_10_QUIET_NAN)
365 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
368 #if defined (GFC_REAL_10_INFINITY) || defined (GFC_REAL_10_QUIET_NAN)
371 #if defined (GFC_REAL_10_QUIET_NAN)
378 if (unlikely (n
>= len
))
380 #if defined (GFC_REAL_10_QUIET_NAN)
381 result
= non_empty_p
? GFC_REAL_10_QUIET_NAN
: -GFC_REAL_10_HUGE
;
383 result
= -GFC_REAL_10_HUGE
;
386 else for (; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
389 if (*msrc
&& *src
> result
)
394 /* Advance to the next element. */
400 while (count
[n
] == extent
[n
])
402 /* When we get to the end of a dimension, reset it and increment
403 the next dimension. */
405 /* We could precalculate these products, but this is a less
406 frequently used path so probably not worth it. */
407 base
-= sstride
[n
] * extent
[n
];
408 mbase
-= mstride
[n
] * extent
[n
];
409 dest
-= dstride
[n
] * extent
[n
];
413 /* Break out of the loop. */
429 extern void smaxval_r10 (gfc_array_r10
* const restrict
,
430 gfc_array_r10
* const restrict
, const index_type
* const restrict
,
432 export_proto(smaxval_r10
);
435 smaxval_r10 (gfc_array_r10
* const restrict retarray
,
436 gfc_array_r10
* const restrict array
,
437 const index_type
* const restrict pdim
,
438 GFC_LOGICAL_4
* mask
)
440 index_type count
[GFC_MAX_DIMENSIONS
];
441 index_type extent
[GFC_MAX_DIMENSIONS
];
442 index_type dstride
[GFC_MAX_DIMENSIONS
];
443 GFC_REAL_10
* restrict dest
;
452 maxval_r10 (retarray
, array
, pdim
, back
);
454 maxval_r10 (retarray
, array
, pdim
);
458 /* Make dim zero based to avoid confusion. */
460 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
462 if (unlikely (dim
< 0 || dim
> rank
))
464 runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
465 "is %ld, should be between 1 and %ld",
466 (long int) dim
+ 1, (long int) rank
+ 1);
469 for (n
= 0; n
< dim
; n
++)
471 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
477 for (n
= dim
; n
< rank
; n
++)
480 GFC_DESCRIPTOR_EXTENT(array
,n
+ 1);
486 if (retarray
->base_addr
== NULL
)
488 size_t alloc_size
, str
;
490 for (n
= 0; n
< rank
; n
++)
495 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
497 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
501 retarray
->offset
= 0;
502 GFC_DTYPE_COPY_SETRANK(retarray
,array
,rank
);
504 alloc_size
= GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1) * extent
[rank
-1];
508 /* Make sure we have a zero-sized array. */
509 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
513 retarray
->base_addr
= xmallocarray (alloc_size
, sizeof (GFC_REAL_10
));
517 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
518 runtime_error ("rank of return array incorrect in"
519 " MAXVAL intrinsic: is %ld, should be %ld",
520 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
523 if (unlikely (compile_options
.bounds_check
))
525 for (n
=0; n
< rank
; n
++)
527 index_type ret_extent
;
529 ret_extent
= GFC_DESCRIPTOR_EXTENT(retarray
,n
);
530 if (extent
[n
] != ret_extent
)
531 runtime_error ("Incorrect extent in return value of"
532 " MAXVAL intrinsic in dimension %ld:"
533 " is %ld, should be %ld", (long int) n
+ 1,
534 (long int) ret_extent
, (long int) extent
[n
]);
539 for (n
= 0; n
< rank
; n
++)
542 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
545 dest
= retarray
->base_addr
;
549 *dest
= -GFC_REAL_10_HUGE
;
553 while (count
[n
] == extent
[n
])
555 /* When we get to the end of a dimension, reset it and increment
556 the next dimension. */
558 /* We could precalculate these products, but this is a less
559 frequently used path so probably not worth it. */
560 dest
-= dstride
[n
] * extent
[n
];