1 /* Implementation of the MAXVAL intrinsic
2 Copyright 2002, 2007, 2009, 2010 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_16) && defined (HAVE_GFC_REAL_16)
34 extern void maxval_r16 (gfc_array_r16
* const restrict
,
35 gfc_array_r16
* const restrict
, const index_type
* const restrict
);
36 export_proto(maxval_r16
);
39 maxval_r16 (gfc_array_r16
* const restrict retarray
,
40 gfc_array_r16
* 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_16
* restrict base
;
48 GFC_REAL_16
* 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
->data
== 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
= sizeof (GFC_REAL_16
) * GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1)
105 /* Make sure we have a zero-sized array. */
106 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
111 retarray
->data
= internal_malloc_size (alloc_size
);
115 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
116 runtime_error ("rank of return array incorrect in"
117 " MAXVAL intrinsic: is %ld, should be %ld",
118 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
121 if (unlikely (compile_options
.bounds_check
))
122 bounds_ifunction_return ((array_t
*) retarray
, extent
,
123 "return value", "MAXVAL");
126 for (n
= 0; n
< rank
; n
++)
129 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
135 dest
= retarray
->data
;
138 while (continue_loop
)
140 const GFC_REAL_16
* restrict src
;
145 #if defined (GFC_REAL_16_INFINITY)
146 result
= -GFC_REAL_16_INFINITY
;
148 result
= -GFC_REAL_16_HUGE
;
151 *dest
= -GFC_REAL_16_HUGE
;
154 for (n
= 0; n
< len
; n
++, src
+= delta
)
157 #if defined (GFC_REAL_16_QUIET_NAN)
161 if (unlikely (n
>= len
))
162 result
= GFC_REAL_16_QUIET_NAN
;
163 else for (; n
< len
; n
++, src
+= delta
)
173 /* Advance to the next element. */
178 while (count
[n
] == extent
[n
])
180 /* When we get to the end of a dimension, reset it and increment
181 the next dimension. */
183 /* We could precalculate these products, but this is a less
184 frequently used path so probably not worth it. */
185 base
-= sstride
[n
] * extent
[n
];
186 dest
-= dstride
[n
] * extent
[n
];
190 /* Break out of the look. */
205 extern void mmaxval_r16 (gfc_array_r16
* const restrict
,
206 gfc_array_r16
* const restrict
, const index_type
* const restrict
,
207 gfc_array_l1
* const restrict
);
208 export_proto(mmaxval_r16
);
211 mmaxval_r16 (gfc_array_r16
* const restrict retarray
,
212 gfc_array_r16
* const restrict array
,
213 const index_type
* const restrict pdim
,
214 gfc_array_l1
* const restrict mask
)
216 index_type count
[GFC_MAX_DIMENSIONS
];
217 index_type extent
[GFC_MAX_DIMENSIONS
];
218 index_type sstride
[GFC_MAX_DIMENSIONS
];
219 index_type dstride
[GFC_MAX_DIMENSIONS
];
220 index_type mstride
[GFC_MAX_DIMENSIONS
];
221 GFC_REAL_16
* restrict dest
;
222 const GFC_REAL_16
* restrict base
;
223 const GFC_LOGICAL_1
* restrict mbase
;
233 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
235 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
241 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
243 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
244 #ifdef HAVE_GFC_LOGICAL_16
248 mbase
= GFOR_POINTER_TO_L1 (mbase
, mask_kind
);
250 runtime_error ("Funny sized logical array");
252 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
253 mdelta
= GFC_DESCRIPTOR_STRIDE_BYTES(mask
,dim
);
255 for (n
= 0; n
< dim
; n
++)
257 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
258 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
,n
);
259 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
265 for (n
= dim
; n
< rank
; n
++)
267 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
+ 1);
268 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
, n
+ 1);
269 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
275 if (retarray
->data
== NULL
)
277 size_t alloc_size
, str
;
279 for (n
= 0; n
< rank
; n
++)
284 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
286 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
290 alloc_size
= sizeof (GFC_REAL_16
) * GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1)
293 retarray
->offset
= 0;
294 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
298 /* Make sure we have a zero-sized array. */
299 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
303 retarray
->data
= internal_malloc_size (alloc_size
);
308 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
309 runtime_error ("rank of return array incorrect in MAXVAL intrinsic");
311 if (unlikely (compile_options
.bounds_check
))
313 bounds_ifunction_return ((array_t
*) retarray
, extent
,
314 "return value", "MAXVAL");
315 bounds_equal_extents ((array_t
*) mask
, (array_t
*) array
,
316 "MASK argument", "MAXVAL");
320 for (n
= 0; n
< rank
; n
++)
323 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
328 dest
= retarray
->data
;
333 const GFC_REAL_16
* restrict src
;
334 const GFC_LOGICAL_1
* restrict msrc
;
340 #if defined (GFC_REAL_16_INFINITY)
341 result
= -GFC_REAL_16_INFINITY
;
343 result
= -GFC_REAL_16_HUGE
;
345 #if defined (GFC_REAL_16_QUIET_NAN)
349 *dest
= -GFC_REAL_16_HUGE
;
352 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
355 #if defined (GFC_REAL_16_INFINITY) || defined (GFC_REAL_16_QUIET_NAN)
358 #if defined (GFC_REAL_16_QUIET_NAN)
365 if (unlikely (n
>= len
))
367 #if defined (GFC_REAL_16_QUIET_NAN)
368 result
= non_empty_p
? GFC_REAL_16_QUIET_NAN
: -GFC_REAL_16_HUGE
;
370 result
= -GFC_REAL_16_HUGE
;
373 else for (; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
376 if (*msrc
&& *src
> result
)
382 /* Advance to the next element. */
388 while (count
[n
] == extent
[n
])
390 /* When we get to the end of a dimension, reset it and increment
391 the next dimension. */
393 /* We could precalculate these products, but this is a less
394 frequently used path so probably not worth it. */
395 base
-= sstride
[n
] * extent
[n
];
396 mbase
-= mstride
[n
] * extent
[n
];
397 dest
-= dstride
[n
] * extent
[n
];
401 /* Break out of the look. */
417 extern void smaxval_r16 (gfc_array_r16
* const restrict
,
418 gfc_array_r16
* const restrict
, const index_type
* const restrict
,
420 export_proto(smaxval_r16
);
423 smaxval_r16 (gfc_array_r16
* const restrict retarray
,
424 gfc_array_r16
* const restrict array
,
425 const index_type
* const restrict pdim
,
426 GFC_LOGICAL_4
* mask
)
428 index_type count
[GFC_MAX_DIMENSIONS
];
429 index_type extent
[GFC_MAX_DIMENSIONS
];
430 index_type dstride
[GFC_MAX_DIMENSIONS
];
431 GFC_REAL_16
* restrict dest
;
439 maxval_r16 (retarray
, array
, pdim
);
442 /* Make dim zero based to avoid confusion. */
444 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
446 for (n
= 0; n
< dim
; n
++)
448 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
454 for (n
= dim
; n
< rank
; n
++)
457 GFC_DESCRIPTOR_EXTENT(array
,n
+ 1);
463 if (retarray
->data
== NULL
)
465 size_t alloc_size
, str
;
467 for (n
= 0; n
< rank
; n
++)
472 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
474 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
478 retarray
->offset
= 0;
479 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
481 alloc_size
= sizeof (GFC_REAL_16
) * GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1)
486 /* Make sure we have a zero-sized array. */
487 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
491 retarray
->data
= internal_malloc_size (alloc_size
);
495 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
496 runtime_error ("rank of return array incorrect in"
497 " MAXVAL intrinsic: is %ld, should be %ld",
498 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
501 if (unlikely (compile_options
.bounds_check
))
503 for (n
=0; n
< rank
; n
++)
505 index_type ret_extent
;
507 ret_extent
= GFC_DESCRIPTOR_EXTENT(retarray
,n
);
508 if (extent
[n
] != ret_extent
)
509 runtime_error ("Incorrect extent in return value of"
510 " MAXVAL intrinsic in dimension %ld:"
511 " is %ld, should be %ld", (long int) n
+ 1,
512 (long int) ret_extent
, (long int) extent
[n
]);
517 for (n
= 0; n
< rank
; n
++)
520 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
523 dest
= retarray
->data
;
527 *dest
= -GFC_REAL_16_HUGE
;
531 while (count
[n
] == extent
[n
])
533 /* When we get to the end of a dimension, reset it and increment
534 the next dimension. */
536 /* We could precalculate these products, but this is a less
537 frequently used path so probably not worth it. */
538 dest
-= dstride
[n
] * extent
[n
];