1 /* Implementation of the MAXVAL intrinsic
2 Copyright (C) 2002-2016 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"
31 #if defined (HAVE_GFC_REAL_10) && defined (HAVE_GFC_REAL_10)
34 extern void maxval_r10 (gfc_array_r10
* const restrict
,
35 gfc_array_r10
* const restrict
, const index_type
* const restrict
);
36 export_proto(maxval_r10
);
39 maxval_r10 (gfc_array_r10
* const restrict retarray
,
40 gfc_array_r10
* const restrict array
,
41 const index_type
* const restrict pdim
)
43 index_type count
[GFC_MAX_DIMENSIONS
];
44 index_type extent
[GFC_MAX_DIMENSIONS
];
45 index_type sstride
[GFC_MAX_DIMENSIONS
];
46 index_type dstride
[GFC_MAX_DIMENSIONS
];
47 const GFC_REAL_10
* restrict base
;
48 GFC_REAL_10
* restrict dest
;
56 /* Make dim zero based to avoid confusion. */
58 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
60 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
63 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
65 for (n
= 0; n
< dim
; n
++)
67 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
68 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
73 for (n
= dim
; n
< rank
; n
++)
75 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
, n
+ 1);
76 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
82 if (retarray
->base_addr
== NULL
)
84 size_t alloc_size
, str
;
86 for (n
= 0; n
< rank
; n
++)
91 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
93 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
98 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
100 alloc_size
= GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1) * extent
[rank
-1];
102 retarray
->base_addr
= xmallocarray (alloc_size
, sizeof (GFC_REAL_10
));
105 /* Make sure we have a zero-sized array. */
106 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
113 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
114 runtime_error ("rank of return array incorrect in"
115 " MAXVAL intrinsic: is %ld, should be %ld",
116 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
119 if (unlikely (compile_options
.bounds_check
))
120 bounds_ifunction_return ((array_t
*) retarray
, extent
,
121 "return value", "MAXVAL");
124 for (n
= 0; n
< rank
; n
++)
127 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
132 base
= array
->base_addr
;
133 dest
= retarray
->base_addr
;
136 while (continue_loop
)
138 const GFC_REAL_10
* restrict src
;
143 #if defined (GFC_REAL_10_INFINITY)
144 result
= -GFC_REAL_10_INFINITY
;
146 result
= -GFC_REAL_10_HUGE
;
149 *dest
= -GFC_REAL_10_HUGE
;
152 for (n
= 0; n
< len
; n
++, src
+= delta
)
155 #if defined (GFC_REAL_10_QUIET_NAN)
159 if (unlikely (n
>= len
))
160 result
= GFC_REAL_10_QUIET_NAN
;
161 else for (; n
< len
; n
++, src
+= delta
)
171 /* Advance to the next element. */
176 while (count
[n
] == extent
[n
])
178 /* When we get to the end of a dimension, reset it and increment
179 the next dimension. */
181 /* We could precalculate these products, but this is a less
182 frequently used path so probably not worth it. */
183 base
-= sstride
[n
] * extent
[n
];
184 dest
-= dstride
[n
] * extent
[n
];
188 /* Break out of the look. */
203 extern void mmaxval_r10 (gfc_array_r10
* const restrict
,
204 gfc_array_r10
* const restrict
, const index_type
* const restrict
,
205 gfc_array_l1
* const restrict
);
206 export_proto(mmaxval_r10
);
209 mmaxval_r10 (gfc_array_r10
* const restrict retarray
,
210 gfc_array_r10
* const restrict array
,
211 const index_type
* const restrict pdim
,
212 gfc_array_l1
* const restrict mask
)
214 index_type count
[GFC_MAX_DIMENSIONS
];
215 index_type extent
[GFC_MAX_DIMENSIONS
];
216 index_type sstride
[GFC_MAX_DIMENSIONS
];
217 index_type dstride
[GFC_MAX_DIMENSIONS
];
218 index_type mstride
[GFC_MAX_DIMENSIONS
];
219 GFC_REAL_10
* restrict dest
;
220 const GFC_REAL_10
* restrict base
;
221 const GFC_LOGICAL_1
* restrict mbase
;
231 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
233 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
237 mbase
= mask
->base_addr
;
239 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
241 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
242 #ifdef HAVE_GFC_LOGICAL_16
246 mbase
= GFOR_POINTER_TO_L1 (mbase
, mask_kind
);
248 runtime_error ("Funny sized logical array");
250 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
251 mdelta
= GFC_DESCRIPTOR_STRIDE_BYTES(mask
,dim
);
253 for (n
= 0; n
< dim
; n
++)
255 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
256 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
,n
);
257 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
263 for (n
= dim
; n
< rank
; n
++)
265 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
+ 1);
266 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
, n
+ 1);
267 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
273 if (retarray
->base_addr
== NULL
)
275 size_t alloc_size
, str
;
277 for (n
= 0; n
< rank
; n
++)
282 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
284 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
288 alloc_size
= GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1) * extent
[rank
-1];
290 retarray
->offset
= 0;
291 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
295 /* Make sure we have a zero-sized array. */
296 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
300 retarray
->base_addr
= xmallocarray (alloc_size
, sizeof (GFC_REAL_10
));
305 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
306 runtime_error ("rank of return array incorrect in MAXVAL intrinsic");
308 if (unlikely (compile_options
.bounds_check
))
310 bounds_ifunction_return ((array_t
*) retarray
, extent
,
311 "return value", "MAXVAL");
312 bounds_equal_extents ((array_t
*) mask
, (array_t
*) array
,
313 "MASK argument", "MAXVAL");
317 for (n
= 0; n
< rank
; n
++)
320 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
325 dest
= retarray
->base_addr
;
326 base
= array
->base_addr
;
330 const GFC_REAL_10
* restrict src
;
331 const GFC_LOGICAL_1
* restrict msrc
;
337 #if defined (GFC_REAL_10_INFINITY)
338 result
= -GFC_REAL_10_INFINITY
;
340 result
= -GFC_REAL_10_HUGE
;
342 #if defined (GFC_REAL_10_QUIET_NAN)
345 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
348 #if defined (GFC_REAL_10_INFINITY) || defined (GFC_REAL_10_QUIET_NAN)
351 #if defined (GFC_REAL_10_QUIET_NAN)
358 if (unlikely (n
>= len
))
360 #if defined (GFC_REAL_10_QUIET_NAN)
361 result
= non_empty_p
? GFC_REAL_10_QUIET_NAN
: -GFC_REAL_10_HUGE
;
363 result
= -GFC_REAL_10_HUGE
;
366 else for (; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
369 if (*msrc
&& *src
> result
)
374 /* Advance to the next element. */
380 while (count
[n
] == extent
[n
])
382 /* When we get to the end of a dimension, reset it and increment
383 the next dimension. */
385 /* We could precalculate these products, but this is a less
386 frequently used path so probably not worth it. */
387 base
-= sstride
[n
] * extent
[n
];
388 mbase
-= mstride
[n
] * extent
[n
];
389 dest
-= dstride
[n
] * extent
[n
];
393 /* Break out of the look. */
409 extern void smaxval_r10 (gfc_array_r10
* const restrict
,
410 gfc_array_r10
* const restrict
, const index_type
* const restrict
,
412 export_proto(smaxval_r10
);
415 smaxval_r10 (gfc_array_r10
* const restrict retarray
,
416 gfc_array_r10
* const restrict array
,
417 const index_type
* const restrict pdim
,
418 GFC_LOGICAL_4
* mask
)
420 index_type count
[GFC_MAX_DIMENSIONS
];
421 index_type extent
[GFC_MAX_DIMENSIONS
];
422 index_type dstride
[GFC_MAX_DIMENSIONS
];
423 GFC_REAL_10
* restrict dest
;
431 maxval_r10 (retarray
, array
, pdim
);
434 /* Make dim zero based to avoid confusion. */
436 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
438 for (n
= 0; n
< dim
; n
++)
440 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
446 for (n
= dim
; n
< rank
; n
++)
449 GFC_DESCRIPTOR_EXTENT(array
,n
+ 1);
455 if (retarray
->base_addr
== NULL
)
457 size_t alloc_size
, str
;
459 for (n
= 0; n
< rank
; n
++)
464 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
466 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
470 retarray
->offset
= 0;
471 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
473 alloc_size
= GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1) * extent
[rank
-1];
477 /* Make sure we have a zero-sized array. */
478 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
482 retarray
->base_addr
= xmallocarray (alloc_size
, sizeof (GFC_REAL_10
));
486 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
487 runtime_error ("rank of return array incorrect in"
488 " MAXVAL intrinsic: is %ld, should be %ld",
489 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
492 if (unlikely (compile_options
.bounds_check
))
494 for (n
=0; n
< rank
; n
++)
496 index_type ret_extent
;
498 ret_extent
= GFC_DESCRIPTOR_EXTENT(retarray
,n
);
499 if (extent
[n
] != ret_extent
)
500 runtime_error ("Incorrect extent in return value of"
501 " MAXVAL intrinsic in dimension %ld:"
502 " is %ld, should be %ld", (long int) n
+ 1,
503 (long int) ret_extent
, (long int) extent
[n
]);
508 for (n
= 0; n
< rank
; n
++)
511 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
514 dest
= retarray
->base_addr
;
518 *dest
= -GFC_REAL_10_HUGE
;
522 while (count
[n
] == extent
[n
])
524 /* When we get to the end of a dimension, reset it and increment
525 the next dimension. */
527 /* We could precalculate these products, but this is a less
528 frequently used path so probably not worth it. */
529 dest
-= dstride
[n
] * extent
[n
];