]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libgfortran/generated/maxloc1_4_i4.c
Update copyright years.
[thirdparty/gcc.git] / libgfortran / generated / maxloc1_4_i4.c
index 5fcb75452209b43562c35d5af638d34546aa0407..b81ad0f9a3b9c4b949008b52f1de8ce86c57e387 100644 (file)
@@ -1,5 +1,5 @@
 /* Implementation of the MAXLOC intrinsic
-   Copyright 2002, 2007, 2009, 2010 Free Software Foundation, Inc.
+   Copyright (C) 2002-2021 Free Software Foundation, Inc.
    Contributed by Paul Brook <paul@nowt.org>
 
 This file is part of the GNU Fortran runtime library (libgfortran).
@@ -24,22 +24,22 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
 <http://www.gnu.org/licenses/>.  */
 
 #include "libgfortran.h"
-#include <stdlib.h>
 #include <assert.h>
-#include <limits.h>
 
 
 #if defined (HAVE_GFC_INTEGER_4) && defined (HAVE_GFC_INTEGER_4)
 
+#define HAVE_BACK_ARG 1
+
 
 extern void maxloc1_4_i4 (gfc_array_i4 * const restrict, 
-       gfc_array_i4 * const restrict, const index_type * const restrict);
+       gfc_array_i4 * const restrict, const index_type * const restrict, GFC_LOGICAL_4 back);
 export_proto(maxloc1_4_i4);
 
 void
 maxloc1_4_i4 (gfc_array_i4 * const restrict retarray, 
        gfc_array_i4 * 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];
@@ -55,8 +55,15 @@ maxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
   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)
@@ -80,7 +87,7 @@ maxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
        extent[n] = 0;
     }
 
-  if (retarray->data == NULL)
+  if (retarray->base_addr == NULL)
     {
       size_t alloc_size, str;
 
@@ -96,11 +103,11 @@ maxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
        }
 
       retarray->offset = 0;
-      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+      retarray->dtype.rank = rank;
 
-      alloc_size = sizeof (GFC_INTEGER_4) * 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_4));
       if (alloc_size == 0)
        {
          /* Make sure we have a zero-sized array.  */
@@ -108,8 +115,6 @@ maxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
          return;
 
        }
-      else
-       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
@@ -132,8 +137,8 @@ maxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
        return;
     }
 
-  base = array->data;
-  dest = retarray->data;
+  base = array->base_addr;
+  dest = retarray->base_addr;
 
   continue_loop = 1;
   while (continue_loop)
@@ -154,10 +159,14 @@ maxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
          *dest = 0;
        else
          {
+#if ! defined HAVE_BACK_ARG
            for (n = 0; n < len; n++, src += delta)
              {
+#endif
 
 #if defined (GFC_INTEGER_4_QUIET_NAN)
+            for (n = 0; n < len; n++, src += delta)
+              {
                if (*src >= maxval)
                  {
                    maxval = *src;
@@ -165,10 +174,12 @@ maxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
                    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_4)n + 1;
@@ -193,9 +204,9 @@ maxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
          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;
            }
@@ -212,14 +223,14 @@ maxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
 
 extern void mmaxloc1_4_i4 (gfc_array_i4 * const restrict, 
        gfc_array_i4 * const restrict, const index_type * const restrict,
-       gfc_array_l1 * const restrict);
+       gfc_array_l1 * const restrict, GFC_LOGICAL_4 back);
 export_proto(mmaxloc1_4_i4);
 
 void
 mmaxloc1_4_i4 (gfc_array_i4 * const restrict retarray, 
        gfc_array_i4 * 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];
@@ -229,22 +240,40 @@ mmaxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
   GFC_INTEGER_4 * restrict dest;
   const GFC_INTEGER_4 * 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_4_i4 (retarray, array, pdim, back);
+#else
+      maxloc1_4_i4 (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;
 
-  mbase = mask->data;
+  mbase = mask->base_addr;
 
   mask_kind = GFC_DESCRIPTOR_SIZE (mask);
 
@@ -280,7 +309,7 @@ mmaxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
        extent[n] = 0;
     }
 
-  if (retarray->data == NULL)
+  if (retarray->base_addr == NULL)
     {
       size_t alloc_size, str;
 
@@ -295,11 +324,10 @@ mmaxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
 
        }
 
-      alloc_size = sizeof (GFC_INTEGER_4) * 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;
 
       if (alloc_size == 0)
        {
@@ -308,7 +336,7 @@ mmaxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
          return;
        }
       else
-       retarray->data = internal_malloc_size (alloc_size);
+       retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_4));
 
     }
   else
@@ -333,8 +361,8 @@ mmaxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
        return;
     }
 
-  dest = retarray->data;
-  base = array->data;
+  dest = retarray->base_addr;
+  base = array->base_addr;
 
   while (base)
     {
@@ -355,12 +383,8 @@ mmaxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
        GFC_INTEGER_4 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)
                  {
@@ -381,16 +405,25 @@ mmaxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
              result = result2;
            else
 #endif
-           for (; n < len; n++, src += delta, msrc += mdelta)
-             {
-               if (*msrc && *src > maxval)
-                 {
-                   maxval = *src;
-                   result = (GFC_INTEGER_4)n + 1;
-                 }
-             }
-           *dest = result;
+           if (back)
+             for (; n < len; n++, src += delta, msrc += mdelta)
+               {
+                 if (*msrc && unlikely (*src >= maxval))
+                   {
+                     maxval = *src;
+                     result = (GFC_INTEGER_4)n + 1;
+                   }
+               }
+           else
+             for (; n < len; n++, src += delta, msrc += mdelta)
+               {
+                 if (*msrc && unlikely (*src > maxval))
+                   {
+                     maxval = *src;
+                     result = (GFC_INTEGER_4)n + 1;
+                   }
          }
+       *dest = result;
       }
       /* Advance to the next element.  */
       count[0]++;
@@ -409,9 +442,9 @@ mmaxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
          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;
            }
@@ -429,14 +462,14 @@ mmaxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
 
 extern void smaxloc1_4_i4 (gfc_array_i4 * const restrict, 
        gfc_array_i4 * const restrict, const index_type * const restrict,
-       GFC_LOGICAL_4 *);
+       GFC_LOGICAL_4 *, GFC_LOGICAL_4 back);
 export_proto(smaxloc1_4_i4);
 
 void
 smaxloc1_4_i4 (gfc_array_i4 * const restrict retarray, 
        gfc_array_i4 * 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];
@@ -447,15 +480,26 @@ smaxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
   index_type dim;
 
 
-  if (*mask)
+  if (mask == NULL || *mask)
     {
+#ifdef HAVE_BACK_ARG
+      maxloc1_4_i4 (retarray, array, pdim, back);
+#else
       maxloc1_4_i4 (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);
@@ -473,7 +517,7 @@ smaxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
        extent[n] = 0;
     }
 
-  if (retarray->data == NULL)
+  if (retarray->base_addr == NULL)
     {
       size_t alloc_size, str;
 
@@ -489,10 +533,9 @@ smaxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
        }
 
       retarray->offset = 0;
-      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+      retarray->dtype.rank = rank;
 
-      alloc_size = sizeof (GFC_INTEGER_4) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
-                  * extent[rank-1];
+      alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
 
       if (alloc_size == 0)
        {
@@ -501,7 +544,7 @@ smaxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
          return;
        }
       else
-       retarray->data = internal_malloc_size (alloc_size);
+       retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_4));
     }
   else
     {
@@ -533,7 +576,7 @@ smaxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
       dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
     }
 
-  dest = retarray->data;
+  dest = retarray->base_addr;
 
   while(1)
     {
@@ -550,7 +593,7 @@ smaxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
             frequently used path so probably not worth it.  */
          dest -= dstride[n] * extent[n];
          n++;
-         if (n == rank)
+         if (n >= rank)
            return;
          else
            {