1 /* Implementation of the MAXVAL intrinsic
2 Copyright (C) 2002-2017 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_INTEGER_2) && defined (HAVE_GFC_INTEGER_2)
32 extern void maxval_i2 (gfc_array_i2
* const restrict
,
33 gfc_array_i2
* const restrict
, const index_type
* const restrict
);
34 export_proto(maxval_i2
);
37 maxval_i2 (gfc_array_i2
* const restrict retarray
,
38 gfc_array_i2
* 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_INTEGER_2
* restrict base
;
46 GFC_INTEGER_2
* restrict dest
;
54 /* Make dim zero based to avoid confusion. */
55 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
58 if (unlikely (dim
< 0 || dim
> rank
))
60 runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
61 "is %ld, should be between 1 and %ld",
62 (long int) dim
+ 1, (long int) rank
+ 1);
65 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
68 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
70 for (n
= 0; n
< dim
; n
++)
72 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
73 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
78 for (n
= dim
; n
< rank
; n
++)
80 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
, n
+ 1);
81 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
87 if (retarray
->base_addr
== NULL
)
89 size_t alloc_size
, str
;
91 for (n
= 0; n
< rank
; n
++)
96 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
98 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
102 retarray
->offset
= 0;
103 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
105 alloc_size
= GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1) * extent
[rank
-1];
107 retarray
->base_addr
= xmallocarray (alloc_size
, sizeof (GFC_INTEGER_2
));
110 /* Make sure we have a zero-sized array. */
111 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
118 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
119 runtime_error ("rank of return array incorrect in"
120 " MAXVAL intrinsic: is %ld, should be %ld",
121 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
124 if (unlikely (compile_options
.bounds_check
))
125 bounds_ifunction_return ((array_t
*) retarray
, extent
,
126 "return value", "MAXVAL");
129 for (n
= 0; n
< rank
; n
++)
132 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
137 base
= array
->base_addr
;
138 dest
= retarray
->base_addr
;
141 while (continue_loop
)
143 const GFC_INTEGER_2
* restrict src
;
144 GFC_INTEGER_2 result
;
148 #if defined (GFC_INTEGER_2_INFINITY)
149 result
= -GFC_INTEGER_2_INFINITY
;
151 result
= (-GFC_INTEGER_2_HUGE
-1);
154 *dest
= (-GFC_INTEGER_2_HUGE
-1);
157 for (n
= 0; n
< len
; n
++, src
+= delta
)
160 #if defined (GFC_INTEGER_2_QUIET_NAN)
164 if (unlikely (n
>= len
))
165 result
= GFC_INTEGER_2_QUIET_NAN
;
166 else for (; n
< len
; n
++, src
+= delta
)
176 /* Advance to the next element. */
181 while (count
[n
] == extent
[n
])
183 /* When we get to the end of a dimension, reset it and increment
184 the next dimension. */
186 /* We could precalculate these products, but this is a less
187 frequently used path so probably not worth it. */
188 base
-= sstride
[n
] * extent
[n
];
189 dest
-= dstride
[n
] * extent
[n
];
193 /* Break out of the loop. */
208 extern void mmaxval_i2 (gfc_array_i2
* const restrict
,
209 gfc_array_i2
* const restrict
, const index_type
* const restrict
,
210 gfc_array_l1
* const restrict
);
211 export_proto(mmaxval_i2
);
214 mmaxval_i2 (gfc_array_i2
* const restrict retarray
,
215 gfc_array_i2
* const restrict array
,
216 const index_type
* const restrict pdim
,
217 gfc_array_l1
* const restrict mask
)
219 index_type count
[GFC_MAX_DIMENSIONS
];
220 index_type extent
[GFC_MAX_DIMENSIONS
];
221 index_type sstride
[GFC_MAX_DIMENSIONS
];
222 index_type dstride
[GFC_MAX_DIMENSIONS
];
223 index_type mstride
[GFC_MAX_DIMENSIONS
];
224 GFC_INTEGER_2
* restrict dest
;
225 const GFC_INTEGER_2
* restrict base
;
226 const GFC_LOGICAL_1
* restrict mbase
;
236 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
239 if (unlikely (dim
< 0 || dim
> rank
))
241 runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
242 "is %ld, should be between 1 and %ld",
243 (long int) dim
+ 1, (long int) rank
+ 1);
246 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
250 mbase
= mask
->base_addr
;
252 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
254 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
255 #ifdef HAVE_GFC_LOGICAL_16
259 mbase
= GFOR_POINTER_TO_L1 (mbase
, mask_kind
);
261 runtime_error ("Funny sized logical array");
263 delta
= GFC_DESCRIPTOR_STRIDE(array
,dim
);
264 mdelta
= GFC_DESCRIPTOR_STRIDE_BYTES(mask
,dim
);
266 for (n
= 0; n
< dim
; n
++)
268 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
);
269 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
,n
);
270 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
276 for (n
= dim
; n
< rank
; n
++)
278 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(array
,n
+ 1);
279 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
, n
+ 1);
280 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
, n
+ 1);
286 if (retarray
->base_addr
== NULL
)
288 size_t alloc_size
, str
;
290 for (n
= 0; n
< rank
; n
++)
295 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
297 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
301 alloc_size
= GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1) * extent
[rank
-1];
303 retarray
->offset
= 0;
304 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
308 /* Make sure we have a zero-sized array. */
309 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
313 retarray
->base_addr
= xmallocarray (alloc_size
, sizeof (GFC_INTEGER_2
));
318 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
319 runtime_error ("rank of return array incorrect in MAXVAL intrinsic");
321 if (unlikely (compile_options
.bounds_check
))
323 bounds_ifunction_return ((array_t
*) retarray
, extent
,
324 "return value", "MAXVAL");
325 bounds_equal_extents ((array_t
*) mask
, (array_t
*) array
,
326 "MASK argument", "MAXVAL");
330 for (n
= 0; n
< rank
; n
++)
333 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
338 dest
= retarray
->base_addr
;
339 base
= array
->base_addr
;
343 const GFC_INTEGER_2
* restrict src
;
344 const GFC_LOGICAL_1
* restrict msrc
;
345 GFC_INTEGER_2 result
;
350 #if defined (GFC_INTEGER_2_INFINITY)
351 result
= -GFC_INTEGER_2_INFINITY
;
353 result
= (-GFC_INTEGER_2_HUGE
-1);
355 #if defined (GFC_INTEGER_2_QUIET_NAN)
358 for (n
= 0; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
361 #if defined (GFC_INTEGER_2_INFINITY) || defined (GFC_INTEGER_2_QUIET_NAN)
364 #if defined (GFC_INTEGER_2_QUIET_NAN)
371 if (unlikely (n
>= len
))
373 #if defined (GFC_INTEGER_2_QUIET_NAN)
374 result
= non_empty_p
? GFC_INTEGER_2_QUIET_NAN
: (-GFC_INTEGER_2_HUGE
-1);
376 result
= (-GFC_INTEGER_2_HUGE
-1);
379 else for (; n
< len
; n
++, src
+= delta
, msrc
+= mdelta
)
382 if (*msrc
&& *src
> result
)
387 /* Advance to the next element. */
393 while (count
[n
] == extent
[n
])
395 /* When we get to the end of a dimension, reset it and increment
396 the next dimension. */
398 /* We could precalculate these products, but this is a less
399 frequently used path so probably not worth it. */
400 base
-= sstride
[n
] * extent
[n
];
401 mbase
-= mstride
[n
] * extent
[n
];
402 dest
-= dstride
[n
] * extent
[n
];
406 /* Break out of the loop. */
422 extern void smaxval_i2 (gfc_array_i2
* const restrict
,
423 gfc_array_i2
* const restrict
, const index_type
* const restrict
,
425 export_proto(smaxval_i2
);
428 smaxval_i2 (gfc_array_i2
* const restrict retarray
,
429 gfc_array_i2
* const restrict array
,
430 const index_type
* const restrict pdim
,
431 GFC_LOGICAL_4
* mask
)
433 index_type count
[GFC_MAX_DIMENSIONS
];
434 index_type extent
[GFC_MAX_DIMENSIONS
];
435 index_type dstride
[GFC_MAX_DIMENSIONS
];
436 GFC_INTEGER_2
* restrict dest
;
444 maxval_i2 (retarray
, array
, pdim
);
447 /* Make dim zero based to avoid confusion. */
449 rank
= GFC_DESCRIPTOR_RANK (array
) - 1;
451 if (unlikely (dim
< 0 || dim
> rank
))
453 runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
454 "is %ld, should be between 1 and %ld",
455 (long int) dim
+ 1, (long int) rank
+ 1);
458 for (n
= 0; n
< dim
; n
++)
460 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
466 for (n
= dim
; n
< rank
; n
++)
469 GFC_DESCRIPTOR_EXTENT(array
,n
+ 1);
475 if (retarray
->base_addr
== NULL
)
477 size_t alloc_size
, str
;
479 for (n
= 0; n
< rank
; n
++)
484 str
= GFC_DESCRIPTOR_STRIDE(retarray
,n
-1) * extent
[n
-1];
486 GFC_DIMENSION_SET(retarray
->dim
[n
], 0, extent
[n
] - 1, str
);
490 retarray
->offset
= 0;
491 retarray
->dtype
= (array
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rank
;
493 alloc_size
= GFC_DESCRIPTOR_STRIDE(retarray
,rank
-1) * extent
[rank
-1];
497 /* Make sure we have a zero-sized array. */
498 GFC_DIMENSION_SET(retarray
->dim
[0], 0, -1, 1);
502 retarray
->base_addr
= xmallocarray (alloc_size
, sizeof (GFC_INTEGER_2
));
506 if (rank
!= GFC_DESCRIPTOR_RANK (retarray
))
507 runtime_error ("rank of return array incorrect in"
508 " MAXVAL intrinsic: is %ld, should be %ld",
509 (long int) (GFC_DESCRIPTOR_RANK (retarray
)),
512 if (unlikely (compile_options
.bounds_check
))
514 for (n
=0; n
< rank
; n
++)
516 index_type ret_extent
;
518 ret_extent
= GFC_DESCRIPTOR_EXTENT(retarray
,n
);
519 if (extent
[n
] != ret_extent
)
520 runtime_error ("Incorrect extent in return value of"
521 " MAXVAL intrinsic in dimension %ld:"
522 " is %ld, should be %ld", (long int) n
+ 1,
523 (long int) ret_extent
, (long int) extent
[n
]);
528 for (n
= 0; n
< rank
; n
++)
531 dstride
[n
] = GFC_DESCRIPTOR_STRIDE(retarray
,n
);
534 dest
= retarray
->base_addr
;
538 *dest
= (-GFC_INTEGER_2_HUGE
-1);
542 while (count
[n
] == extent
[n
])
544 /* When we get to the end of a dimension, reset it and increment
545 the next dimension. */
547 /* We could precalculate these products, but this is a less
548 frequently used path so probably not worth it. */
549 dest
-= dstride
[n
] * extent
[n
];