/* Implementation of the MAXLOC intrinsic
- Copyright 2002, 2007, 2009, 2010 Free Software Foundation, Inc.
+ Copyright (C) 2002-2024 Free Software Foundation, Inc.
Contributed by Paul Brook <paul@nowt.org>
This file is part of the GNU Fortran runtime library (libgfortran).
<http://www.gnu.org/licenses/>. */
#include "libgfortran.h"
-#include <stdlib.h>
#include <assert.h>
-#include <limits.h>
#if defined (HAVE_GFC_INTEGER_16) && defined (HAVE_GFC_INTEGER_8)
+#define HAVE_BACK_ARG 1
+
extern void maxloc1_8_i16 (gfc_array_i8 * const restrict,
- gfc_array_i16 * const restrict, const index_type * const restrict);
+ gfc_array_i16 * const restrict, const index_type * const restrict, GFC_LOGICAL_4 back);
export_proto(maxloc1_8_i16);
void
maxloc1_8_i16 (gfc_array_i8 * const restrict retarray,
gfc_array_i16 * const restrict array,
- const index_type * const restrict pdim)
+ const index_type * const restrict pdim, GFC_LOGICAL_4 back)
{
index_type count[GFC_MAX_DIMENSIONS];
index_type extent[GFC_MAX_DIMENSIONS];
int continue_loop;
/* Make dim zero based to avoid confusion. */
- dim = (*pdim) - 1;
rank = GFC_DESCRIPTOR_RANK (array) - 1;
+ dim = (*pdim) - 1;
+
+ if (unlikely (dim < 0 || dim > rank))
+ {
+ runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ "is %ld, should be between 1 and %ld",
+ (long int) dim + 1, (long int) rank + 1);
+ }
len = GFC_DESCRIPTOR_EXTENT(array,dim);
if (len < 0)
extent[n] = 0;
}
- if (retarray->data == NULL)
+ if (retarray->base_addr == NULL)
{
size_t alloc_size, str;
}
retarray->offset = 0;
- retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+ retarray->dtype.rank = rank;
- alloc_size = sizeof (GFC_INTEGER_8) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
- * extent[rank-1];
+ alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
+ retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_8));
if (alloc_size == 0)
- {
- /* Make sure we have a zero-sized array. */
- GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
- return;
-
- }
- else
- retarray->data = internal_malloc_size (alloc_size);
+ return;
}
else
{
return;
}
- base = array->data;
- dest = retarray->data;
+ base = array->base_addr;
+ dest = retarray->base_addr;
continue_loop = 1;
while (continue_loop)
*dest = 0;
else
{
+#if ! defined HAVE_BACK_ARG
for (n = 0; n < len; n++, src += delta)
{
+#endif
#if defined (GFC_INTEGER_16_QUIET_NAN)
+ for (n = 0; n < len; n++, src += delta)
+ {
if (*src >= maxval)
{
maxval = *src;
break;
}
}
+#else
+ n = 0;
+#endif
for (; n < len; n++, src += delta)
{
-#endif
- if (*src > maxval)
+ if (back ? *src >= maxval : *src > maxval)
{
maxval = *src;
result = (GFC_INTEGER_8)n + 1;
base -= sstride[n] * extent[n];
dest -= dstride[n] * extent[n];
n++;
- if (n == rank)
+ if (n >= rank)
{
- /* Break out of the look. */
+ /* Break out of the loop. */
continue_loop = 0;
break;
}
extern void mmaxloc1_8_i16 (gfc_array_i8 * const restrict,
gfc_array_i16 * const restrict, const index_type * const restrict,
- gfc_array_l1 * const restrict);
+ gfc_array_l1 * const restrict, GFC_LOGICAL_4 back);
export_proto(mmaxloc1_8_i16);
void
mmaxloc1_8_i16 (gfc_array_i8 * const restrict retarray,
gfc_array_i16 * const restrict array,
const index_type * const restrict pdim,
- gfc_array_l1 * const restrict mask)
+ gfc_array_l1 * const restrict mask, GFC_LOGICAL_4 back)
{
index_type count[GFC_MAX_DIMENSIONS];
index_type extent[GFC_MAX_DIMENSIONS];
GFC_INTEGER_8 * restrict dest;
const GFC_INTEGER_16 * restrict base;
const GFC_LOGICAL_1 * restrict mbase;
- int rank;
- int dim;
+ index_type rank;
+ index_type dim;
index_type n;
index_type len;
index_type delta;
index_type mdelta;
int mask_kind;
+ if (mask == NULL)
+ {
+#ifdef HAVE_BACK_ARG
+ maxloc1_8_i16 (retarray, array, pdim, back);
+#else
+ maxloc1_8_i16 (retarray, array, pdim);
+#endif
+ return;
+ }
+
dim = (*pdim) - 1;
rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+ if (unlikely (dim < 0 || dim > rank))
+ {
+ runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ "is %ld, should be between 1 and %ld",
+ (long int) dim + 1, (long int) rank + 1);
+ }
+
len = GFC_DESCRIPTOR_EXTENT(array,dim);
- if (len <= 0)
- return;
+ if (len < 0)
+ len = 0;
- mbase = mask->data;
+ mbase = mask->base_addr;
mask_kind = GFC_DESCRIPTOR_SIZE (mask);
extent[n] = 0;
}
- if (retarray->data == NULL)
+ if (retarray->base_addr == NULL)
{
size_t alloc_size, str;
}
- alloc_size = sizeof (GFC_INTEGER_8) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
- * extent[rank-1];
+ alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
retarray->offset = 0;
- retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+ retarray->dtype.rank = rank;
+ retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_8));
if (alloc_size == 0)
- {
- /* Make sure we have a zero-sized array. */
- GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
- return;
- }
- else
- retarray->data = internal_malloc_size (alloc_size);
-
+ return;
}
else
{
return;
}
- dest = retarray->data;
- base = array->data;
+ dest = retarray->base_addr;
+ base = array->base_addr;
while (base)
{
GFC_INTEGER_8 result2 = 0;
#endif
result = 0;
- if (len <= 0)
- *dest = 0;
- else
+ for (n = 0; n < len; n++, src += delta, msrc += mdelta)
{
- for (n = 0; n < len; n++, src += delta, msrc += mdelta)
- {
if (*msrc)
{
result = result2;
else
#endif
- for (; n < len; n++, src += delta, msrc += mdelta)
- {
- if (*msrc && *src > maxval)
- {
- maxval = *src;
- result = (GFC_INTEGER_8)n + 1;
- }
- }
- *dest = result;
+ if (back)
+ for (; n < len; n++, src += delta, msrc += mdelta)
+ {
+ if (*msrc && unlikely (*src >= maxval))
+ {
+ maxval = *src;
+ result = (GFC_INTEGER_8)n + 1;
+ }
+ }
+ else
+ for (; n < len; n++, src += delta, msrc += mdelta)
+ {
+ if (*msrc && unlikely (*src > maxval))
+ {
+ maxval = *src;
+ result = (GFC_INTEGER_8)n + 1;
+ }
}
+ *dest = result;
}
/* Advance to the next element. */
count[0]++;
mbase -= mstride[n] * extent[n];
dest -= dstride[n] * extent[n];
n++;
- if (n == rank)
+ if (n >= rank)
{
- /* Break out of the look. */
+ /* Break out of the loop. */
base = NULL;
break;
}
extern void smaxloc1_8_i16 (gfc_array_i8 * const restrict,
gfc_array_i16 * const restrict, const index_type * const restrict,
- GFC_LOGICAL_4 *);
+ GFC_LOGICAL_4 *, GFC_LOGICAL_4 back);
export_proto(smaxloc1_8_i16);
void
smaxloc1_8_i16 (gfc_array_i8 * const restrict retarray,
gfc_array_i16 * const restrict array,
const index_type * const restrict pdim,
- GFC_LOGICAL_4 * mask)
+ GFC_LOGICAL_4 * mask, GFC_LOGICAL_4 back)
{
index_type count[GFC_MAX_DIMENSIONS];
index_type extent[GFC_MAX_DIMENSIONS];
index_type dim;
- if (*mask)
+ if (mask == NULL || *mask)
{
+#ifdef HAVE_BACK_ARG
+ maxloc1_8_i16 (retarray, array, pdim, back);
+#else
maxloc1_8_i16 (retarray, array, pdim);
+#endif
return;
}
/* Make dim zero based to avoid confusion. */
dim = (*pdim) - 1;
rank = GFC_DESCRIPTOR_RANK (array) - 1;
+ if (unlikely (dim < 0 || dim > rank))
+ {
+ runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
+ "is %ld, should be between 1 and %ld",
+ (long int) dim + 1, (long int) rank + 1);
+ }
+
for (n = 0; n < dim; n++)
{
extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
extent[n] = 0;
}
- if (retarray->data == NULL)
+ if (retarray->base_addr == NULL)
{
size_t alloc_size, str;
}
retarray->offset = 0;
- retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+ retarray->dtype.rank = rank;
- alloc_size = sizeof (GFC_INTEGER_8) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
- * extent[rank-1];
+ alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
+ retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_8));
if (alloc_size == 0)
- {
- /* Make sure we have a zero-sized array. */
- GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
- return;
- }
- else
- retarray->data = internal_malloc_size (alloc_size);
+ return;
}
else
{
dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
}
- dest = retarray->data;
+ dest = retarray->base_addr;
while(1)
{
frequently used path so probably not worth it. */
dest -= dstride[n] * extent[n];
n++;
- if (n == rank)
+ if (n >= rank)
return;
else
{