OSDN Git Service

2008-04-30 Thomas Koenig <tkoenig@gcc.gnu.org>
authortkoenig <tkoenig@138bc75d-0d04-0410-961f-82ee72b054a4>
Wed, 30 Apr 2008 16:56:01 +0000 (16:56 +0000)
committertkoenig <tkoenig@138bc75d-0d04-0410-961f-82ee72b054a4>
Wed, 30 Apr 2008 16:56:01 +0000 (16:56 +0000)
PR libfortran/35993
* ifunction.m4 (SCALAR_ARRAY_FUNCTION):  Use correct
implementation for multi-dimensional return arrays when
the mask is .false.
* generated/maxloc1_16_i1.c: Regenerated.
* generated/maxloc1_16_i16.c: Regenerated.
* generated/maxloc1_16_i2.c: Regenerated.
* generated/maxloc1_16_i4.c: Regenerated.
* generated/maxloc1_16_i8.c: Regenerated.
* generated/maxloc1_16_r10.c: Regenerated.
* generated/maxloc1_16_r16.c: Regenerated.
* generated/maxloc1_16_r4.c: Regenerated.
* generated/maxloc1_16_r8.c: Regenerated.
* generated/maxloc1_4_i1.c: Regenerated.
* generated/maxloc1_4_i16.c: Regenerated.
* generated/maxloc1_4_i2.c: Regenerated.
* generated/maxloc1_4_i4.c: Regenerated.
* generated/maxloc1_4_i8.c: Regenerated.
* generated/maxloc1_4_r10.c: Regenerated.
* generated/maxloc1_4_r16.c: Regenerated.
* generated/maxloc1_4_r4.c: Regenerated.
* generated/maxloc1_4_r8.c: Regenerated.
* generated/maxloc1_8_i1.c: Regenerated.
* generated/maxloc1_8_i16.c: Regenerated.
* generated/maxloc1_8_i2.c: Regenerated.
* generated/maxloc1_8_i4.c: Regenerated.
* generated/maxloc1_8_i8.c: Regenerated.
* generated/maxloc1_8_r10.c: Regenerated.
* generated/maxloc1_8_r16.c: Regenerated.
* generated/maxloc1_8_r4.c: Regenerated.
* generated/maxloc1_8_r8.c: Regenerated.
* generated/maxval_i1.c: Regenerated.
* generated/maxval_i16.c: Regenerated.
* generated/maxval_i2.c: Regenerated.
* generated/maxval_i4.c: Regenerated.
* generated/maxval_i8.c: Regenerated.
* generated/maxval_r10.c: Regenerated.
* generated/maxval_r16.c: Regenerated.
* generated/maxval_r4.c: Regenerated.
* generated/maxval_r8.c: Regenerated.
* generated/minloc1_16_i1.c: Regenerated.
* generated/minloc1_16_i16.c: Regenerated.
* generated/minloc1_16_i2.c: Regenerated.
* generated/minloc1_16_i4.c: Regenerated.
* generated/minloc1_16_i8.c: Regenerated.
* generated/minloc1_16_r10.c: Regenerated.
* generated/minloc1_16_r16.c: Regenerated.
* generated/minloc1_16_r4.c: Regenerated.
* generated/minloc1_16_r8.c: Regenerated.
* generated/minloc1_4_i1.c: Regenerated.
* generated/minloc1_4_i16.c: Regenerated.
* generated/minloc1_4_i2.c: Regenerated.
* generated/minloc1_4_i4.c: Regenerated.
* generated/minloc1_4_i8.c: Regenerated.
* generated/minloc1_4_r10.c: Regenerated.
* generated/minloc1_4_r16.c: Regenerated.
* generated/minloc1_4_r4.c: Regenerated.
* generated/minloc1_4_r8.c: Regenerated.
* generated/minloc1_8_i1.c: Regenerated.
* generated/minloc1_8_i16.c: Regenerated.
* generated/minloc1_8_i2.c: Regenerated.
* generated/minloc1_8_i4.c: Regenerated.
* generated/minloc1_8_i8.c: Regenerated.
* generated/minloc1_8_r10.c: Regenerated.
* generated/minloc1_8_r16.c: Regenerated.
* generated/minloc1_8_r4.c: Regenerated.
* generated/minloc1_8_r8.c: Regenerated.
* generated/minval_i1.c: Regenerated.
* generated/minval_i16.c: Regenerated.
* generated/minval_i2.c: Regenerated.
* generated/minval_i4.c: Regenerated.
* generated/minval_i8.c: Regenerated.
* generated/minval_r10.c: Regenerated.
* generated/minval_r16.c: Regenerated.
* generated/minval_r4.c: Regenerated.
* generated/minval_r8.c: Regenerated.
* generated/product_c10.c: Regenerated.
* generated/product_c16.c: Regenerated.
* generated/product_c4.c: Regenerated.
* generated/product_c8.c: Regenerated.
* generated/product_i1.c: Regenerated.
* generated/product_i16.c: Regenerated.
* generated/product_i2.c: Regenerated.
* generated/product_i4.c: Regenerated.
* generated/product_i8.c: Regenerated.
* generated/product_r10.c: Regenerated.
* generated/product_r16.c: Regenerated.
* generated/product_r4.c: Regenerated.
* generated/product_r8.c: Regenerated.
* generated/sum_c10.c: Regenerated.
* generated/sum_c16.c: Regenerated.
* generated/sum_c4.c: Regenerated.
* generated/sum_c8.c: Regenerated.
* generated/sum_i1.c: Regenerated.
* generated/sum_i16.c: Regenerated.
* generated/sum_i2.c: Regenerated.
* generated/sum_i4.c: Regenerated.
* generated/sum_i8.c: Regenerated.
* generated/sum_r10.c: Regenerated.
* generated/sum_r16.c: Regenerated.
* generated/sum_r4.c: Regenerated.
* generated/sum_r8.c: Regenerated.

2008-04-30  Thomas Koenig  <tkoenig@gcc.gnu.org>

PR libfortran/35993
* gfortran.dg/intrinsic_product_1.f90:  New test case.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@134830 138bc75d-0d04-0410-961f-82ee72b054a4

102 files changed:
gcc/testsuite/ChangeLog
gcc/testsuite/gfortran.dg/intrinsic_product_1.f90 [new file with mode: 0644]
libgfortran/ChangeLog
libgfortran/generated/maxloc1_16_i1.c
libgfortran/generated/maxloc1_16_i16.c
libgfortran/generated/maxloc1_16_i2.c
libgfortran/generated/maxloc1_16_i4.c
libgfortran/generated/maxloc1_16_i8.c
libgfortran/generated/maxloc1_16_r10.c
libgfortran/generated/maxloc1_16_r16.c
libgfortran/generated/maxloc1_16_r4.c
libgfortran/generated/maxloc1_16_r8.c
libgfortran/generated/maxloc1_4_i1.c
libgfortran/generated/maxloc1_4_i16.c
libgfortran/generated/maxloc1_4_i2.c
libgfortran/generated/maxloc1_4_i4.c
libgfortran/generated/maxloc1_4_i8.c
libgfortran/generated/maxloc1_4_r10.c
libgfortran/generated/maxloc1_4_r16.c
libgfortran/generated/maxloc1_4_r4.c
libgfortran/generated/maxloc1_4_r8.c
libgfortran/generated/maxloc1_8_i1.c
libgfortran/generated/maxloc1_8_i16.c
libgfortran/generated/maxloc1_8_i2.c
libgfortran/generated/maxloc1_8_i4.c
libgfortran/generated/maxloc1_8_i8.c
libgfortran/generated/maxloc1_8_r10.c
libgfortran/generated/maxloc1_8_r16.c
libgfortran/generated/maxloc1_8_r4.c
libgfortran/generated/maxloc1_8_r8.c
libgfortran/generated/maxval_i1.c
libgfortran/generated/maxval_i16.c
libgfortran/generated/maxval_i2.c
libgfortran/generated/maxval_i4.c
libgfortran/generated/maxval_i8.c
libgfortran/generated/maxval_r10.c
libgfortran/generated/maxval_r16.c
libgfortran/generated/maxval_r4.c
libgfortran/generated/maxval_r8.c
libgfortran/generated/minloc1_16_i1.c
libgfortran/generated/minloc1_16_i16.c
libgfortran/generated/minloc1_16_i2.c
libgfortran/generated/minloc1_16_i4.c
libgfortran/generated/minloc1_16_i8.c
libgfortran/generated/minloc1_16_r10.c
libgfortran/generated/minloc1_16_r16.c
libgfortran/generated/minloc1_16_r4.c
libgfortran/generated/minloc1_16_r8.c
libgfortran/generated/minloc1_4_i1.c
libgfortran/generated/minloc1_4_i16.c
libgfortran/generated/minloc1_4_i2.c
libgfortran/generated/minloc1_4_i4.c
libgfortran/generated/minloc1_4_i8.c
libgfortran/generated/minloc1_4_r10.c
libgfortran/generated/minloc1_4_r16.c
libgfortran/generated/minloc1_4_r4.c
libgfortran/generated/minloc1_4_r8.c
libgfortran/generated/minloc1_8_i1.c
libgfortran/generated/minloc1_8_i16.c
libgfortran/generated/minloc1_8_i2.c
libgfortran/generated/minloc1_8_i4.c
libgfortran/generated/minloc1_8_i8.c
libgfortran/generated/minloc1_8_r10.c
libgfortran/generated/minloc1_8_r16.c
libgfortran/generated/minloc1_8_r4.c
libgfortran/generated/minloc1_8_r8.c
libgfortran/generated/minval_i1.c
libgfortran/generated/minval_i16.c
libgfortran/generated/minval_i2.c
libgfortran/generated/minval_i4.c
libgfortran/generated/minval_i8.c
libgfortran/generated/minval_r10.c
libgfortran/generated/minval_r16.c
libgfortran/generated/minval_r4.c
libgfortran/generated/minval_r8.c
libgfortran/generated/product_c10.c
libgfortran/generated/product_c16.c
libgfortran/generated/product_c4.c
libgfortran/generated/product_c8.c
libgfortran/generated/product_i1.c
libgfortran/generated/product_i16.c
libgfortran/generated/product_i2.c
libgfortran/generated/product_i4.c
libgfortran/generated/product_i8.c
libgfortran/generated/product_r10.c
libgfortran/generated/product_r16.c
libgfortran/generated/product_r4.c
libgfortran/generated/product_r8.c
libgfortran/generated/sum_c10.c
libgfortran/generated/sum_c16.c
libgfortran/generated/sum_c4.c
libgfortran/generated/sum_c8.c
libgfortran/generated/sum_i1.c
libgfortran/generated/sum_i16.c
libgfortran/generated/sum_i2.c
libgfortran/generated/sum_i4.c
libgfortran/generated/sum_i8.c
libgfortran/generated/sum_r10.c
libgfortran/generated/sum_r16.c
libgfortran/generated/sum_r4.c
libgfortran/generated/sum_r8.c
libgfortran/m4/ifunction.m4

index e16eaff..53978cc 100644 (file)
@@ -1,3 +1,8 @@
+2008-04-30  Thomas Koenig  <tkoenig@gcc.gnu.org>
+
+       PR libfortran/35993
+       * gfortran.dg/intrinsic_product_1.f90:  New test case.
+
 2008-04-30  Richard Guenther  <rguenther@suse.de>
 
        PR tree-optimization/14847
diff --git a/gcc/testsuite/gfortran.dg/intrinsic_product_1.f90 b/gcc/testsuite/gfortran.dg/intrinsic_product_1.f90
new file mode 100644 (file)
index 0000000..34d34fe
--- /dev/null
@@ -0,0 +1,29 @@
+! { dg-do run }
+! PR 35993 - some intrinsics with mask = .false. didn't set
+! the whole return array for multi-dimensional arrays.
+! Test case adapted from Dick Hendrickson.
+
+      program try
+
+      call       ga3019(  1,  2,  3,  4)
+      end program
+
+      SUBROUTINE GA3019(nf1,nf2,nf3,nf4)
+      INTEGER IDA(NF2,NF3)
+      INTEGER IDA1(NF2,NF4,NF3)
+
+      ida1 = 3
+
+      ida = -3
+      IDA(NF1:NF2,NF1:NF3) = PRODUCT(IDA1,NF2, NF1 .LT. 0)  !fails
+      if (any(ida /= 1)) call abort
+
+      ida = -3
+      IDA(NF1:NF2,NF1:NF3) = PRODUCT(IDA1,NF2, .false. )    !fails
+      if (any(ida /= 1)) call abort
+
+      ida = -3
+      IDA(NF1:NF2,NF1:NF3) = PRODUCT(IDA1,NF2, ida1 .eq. 137 )    !works
+      if (any(ida /= 1)) call abort
+
+      END SUBROUTINE
index df959f9..0ee6848 100644 (file)
@@ -1,3 +1,108 @@
+2008-04-30  Thomas Koenig  <tkoenig@gcc.gnu.org>
+
+       PR libfortran/35993
+       * ifunction.m4 (SCALAR_ARRAY_FUNCTION):  Use correct
+       implementation for multi-dimensional return arrays when
+       the mask is .false.
+       * generated/maxloc1_16_i1.c: Regenerated.
+       * generated/maxloc1_16_i16.c: Regenerated.
+       * generated/maxloc1_16_i2.c: Regenerated.
+       * generated/maxloc1_16_i4.c: Regenerated.
+       * generated/maxloc1_16_i8.c: Regenerated.
+       * generated/maxloc1_16_r10.c: Regenerated.
+       * generated/maxloc1_16_r16.c: Regenerated.
+       * generated/maxloc1_16_r4.c: Regenerated.
+       * generated/maxloc1_16_r8.c: Regenerated.
+       * generated/maxloc1_4_i1.c: Regenerated.
+       * generated/maxloc1_4_i16.c: Regenerated.
+       * generated/maxloc1_4_i2.c: Regenerated.
+       * generated/maxloc1_4_i4.c: Regenerated.
+       * generated/maxloc1_4_i8.c: Regenerated.
+       * generated/maxloc1_4_r10.c: Regenerated.
+       * generated/maxloc1_4_r16.c: Regenerated.
+       * generated/maxloc1_4_r4.c: Regenerated.
+       * generated/maxloc1_4_r8.c: Regenerated.
+       * generated/maxloc1_8_i1.c: Regenerated.
+       * generated/maxloc1_8_i16.c: Regenerated.
+       * generated/maxloc1_8_i2.c: Regenerated.
+       * generated/maxloc1_8_i4.c: Regenerated.
+       * generated/maxloc1_8_i8.c: Regenerated.
+       * generated/maxloc1_8_r10.c: Regenerated.
+       * generated/maxloc1_8_r16.c: Regenerated.
+       * generated/maxloc1_8_r4.c: Regenerated.
+       * generated/maxloc1_8_r8.c: Regenerated.
+       * generated/maxval_i1.c: Regenerated.
+       * generated/maxval_i16.c: Regenerated.
+       * generated/maxval_i2.c: Regenerated.
+       * generated/maxval_i4.c: Regenerated.
+       * generated/maxval_i8.c: Regenerated.
+       * generated/maxval_r10.c: Regenerated.
+       * generated/maxval_r16.c: Regenerated.
+       * generated/maxval_r4.c: Regenerated.
+       * generated/maxval_r8.c: Regenerated.
+       * generated/minloc1_16_i1.c: Regenerated.
+       * generated/minloc1_16_i16.c: Regenerated.
+       * generated/minloc1_16_i2.c: Regenerated.
+       * generated/minloc1_16_i4.c: Regenerated.
+       * generated/minloc1_16_i8.c: Regenerated.
+       * generated/minloc1_16_r10.c: Regenerated.
+       * generated/minloc1_16_r16.c: Regenerated.
+       * generated/minloc1_16_r4.c: Regenerated.
+       * generated/minloc1_16_r8.c: Regenerated.
+       * generated/minloc1_4_i1.c: Regenerated.
+       * generated/minloc1_4_i16.c: Regenerated.
+       * generated/minloc1_4_i2.c: Regenerated.
+       * generated/minloc1_4_i4.c: Regenerated.
+       * generated/minloc1_4_i8.c: Regenerated.
+       * generated/minloc1_4_r10.c: Regenerated.
+       * generated/minloc1_4_r16.c: Regenerated.
+       * generated/minloc1_4_r4.c: Regenerated.
+       * generated/minloc1_4_r8.c: Regenerated.
+       * generated/minloc1_8_i1.c: Regenerated.
+       * generated/minloc1_8_i16.c: Regenerated.
+       * generated/minloc1_8_i2.c: Regenerated.
+       * generated/minloc1_8_i4.c: Regenerated.
+       * generated/minloc1_8_i8.c: Regenerated.
+       * generated/minloc1_8_r10.c: Regenerated.
+       * generated/minloc1_8_r16.c: Regenerated.
+       * generated/minloc1_8_r4.c: Regenerated.
+       * generated/minloc1_8_r8.c: Regenerated.
+       * generated/minval_i1.c: Regenerated.
+       * generated/minval_i16.c: Regenerated.
+       * generated/minval_i2.c: Regenerated.
+       * generated/minval_i4.c: Regenerated.
+       * generated/minval_i8.c: Regenerated.
+       * generated/minval_r10.c: Regenerated.
+       * generated/minval_r16.c: Regenerated.
+       * generated/minval_r4.c: Regenerated.
+       * generated/minval_r8.c: Regenerated.
+       * generated/product_c10.c: Regenerated.
+       * generated/product_c16.c: Regenerated.
+       * generated/product_c4.c: Regenerated.
+       * generated/product_c8.c: Regenerated.
+       * generated/product_i1.c: Regenerated.
+       * generated/product_i16.c: Regenerated.
+       * generated/product_i2.c: Regenerated.
+       * generated/product_i4.c: Regenerated.
+       * generated/product_i8.c: Regenerated.
+       * generated/product_r10.c: Regenerated.
+       * generated/product_r16.c: Regenerated.
+       * generated/product_r4.c: Regenerated.
+       * generated/product_r8.c: Regenerated.
+       * generated/sum_c10.c: Regenerated.
+       * generated/sum_c16.c: Regenerated.
+       * generated/sum_c4.c: Regenerated.
+       * generated/sum_c8.c: Regenerated.
+       * generated/sum_i1.c: Regenerated.
+       * generated/sum_i16.c: Regenerated.
+       * generated/sum_i2.c: Regenerated.
+       * generated/sum_i4.c: Regenerated.
+       * generated/sum_i8.c: Regenerated.
+       * generated/sum_r10.c: Regenerated.
+       * generated/sum_r16.c: Regenerated.
+       * generated/sum_r4.c: Regenerated.
+       * generated/sum_r8.c: Regenerated.
+
 2008-04-25  Thomas Koenig  <tkoenig@gcc.gnu.org>
 
        PR libfortran/35960
index 47e6723..6e4153c 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_16_i1 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_16_i1 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 2c8a06c..b4fd55d 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_16_i16 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_16_i16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index d7b1ca5..483b903 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_16_i2 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_16_i2 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 394c016..69d35d7 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_16_i4 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_16_i4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 5cff65d..54d016c 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_16_i8 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_16_i8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 32af8cd..10ea868 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_16_r10 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_16_r10 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index d695ad8..54b2aeb 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_16_r16 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_16_r16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 05dfbe3..f9712e6 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_16_r4 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_16_r4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index a060e06..1e83628 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_16_r8 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_16_r8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 2244456..271ef38 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_4_i1 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_4_i1 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index d0f260c..627e360 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_4_i16 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_4_i16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 5415eba..cf42ddf 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_4_i2 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_4_i2 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 291b919..a26180f 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_4_i4 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_4_i4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 97a904d..40f2c36 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_4_i8 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_4_i8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 07ccb24..7549702 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_4_r10 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_4_r10 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 5ecfffd..3ed0d96 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_4_r16 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_4_r16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index f859cc7..24ecf9a 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_4_r4 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_4_r4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 5d67342..9a695da 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_4_r8 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_4_r8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index f9ea707..489ed11 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_8_i1 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_8_i1 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 478a8bc..9f8c7ad 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_8_i16 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_8_i16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 972767f..28e0163 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_8_i2 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_8_i2 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index e3b566d..82038dc 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_8_i4 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_8_i4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index e30e104..c7301ea 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_8_i8 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_8_i8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 01e30f6..d83600d 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_8_r10 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_8_r10 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index fbe72d1..6d45297 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_8_r16 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_8_r16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 3985d68..470d73c 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_8_r4 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_8_r4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 6e7745b..7003594 100644 (file)
@@ -428,51 +428,131 @@ smaxloc1_8_r8 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxloc1_8_r8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 901f4e7..9468e4a 100644 (file)
@@ -417,51 +417,131 @@ smaxval_i1 (gfc_array_i1 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_1 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_1 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxval_i1 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_1) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_1) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = (-GFC_INTEGER_1_HUGE-1) ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = (-GFC_INTEGER_1_HUGE-1);
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index c082e85..de2cac8 100644 (file)
@@ -417,51 +417,131 @@ smaxval_i16 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxval_i16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = (-GFC_INTEGER_16_HUGE-1) ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = (-GFC_INTEGER_16_HUGE-1);
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 87865e1..818fc29 100644 (file)
@@ -417,51 +417,131 @@ smaxval_i2 (gfc_array_i2 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_2 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_2 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxval_i2 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_2) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_2) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = (-GFC_INTEGER_2_HUGE-1) ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = (-GFC_INTEGER_2_HUGE-1);
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 3fa4a10..e5289f1 100644 (file)
@@ -417,51 +417,131 @@ smaxval_i4 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxval_i4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = (-GFC_INTEGER_4_HUGE-1) ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = (-GFC_INTEGER_4_HUGE-1);
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 8b2106d..d05737d 100644 (file)
@@ -417,51 +417,131 @@ smaxval_i8 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxval_i8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = (-GFC_INTEGER_8_HUGE-1) ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = (-GFC_INTEGER_8_HUGE-1);
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index a076190..921ad48 100644 (file)
@@ -417,51 +417,131 @@ smaxval_r10 (gfc_array_r10 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_REAL_10 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_REAL_10 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxval_r10 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_REAL_10) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_REAL_10) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = -GFC_REAL_10_HUGE ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = -GFC_REAL_10_HUGE;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 1e36379..15bdd7e 100644 (file)
@@ -417,51 +417,131 @@ smaxval_r16 (gfc_array_r16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_REAL_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_REAL_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxval_r16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_REAL_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_REAL_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = -GFC_REAL_16_HUGE ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = -GFC_REAL_16_HUGE;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 222a4e3..f3470e8 100644 (file)
@@ -417,51 +417,131 @@ smaxval_r4 (gfc_array_r4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_REAL_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_REAL_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxval_r4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_REAL_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_REAL_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = -GFC_REAL_4_HUGE ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = -GFC_REAL_4_HUGE;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 163ec5a..b5d7f3c 100644 (file)
@@ -417,51 +417,131 @@ smaxval_r8 (gfc_array_r8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_REAL_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_REAL_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       maxval_r8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_REAL_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_REAL_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MAXVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MAXVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MAXVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = -GFC_REAL_8_HUGE ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = -GFC_REAL_8_HUGE;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index f4abfa8..8a20d7b 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_16_i1 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_16_i1 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 40b86ea..f0c5b84 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_16_i16 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_16_i16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index f7057b2..234f364 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_16_i2 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_16_i2 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 3cf6f0d..2b55cff 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_16_i4 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_16_i4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index a083868..2e2e894 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_16_i8 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_16_i8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 20b1c57..dcf291f 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_16_r10 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_16_r10 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 40fcbae..eb49613 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_16_r16 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_16_r16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 76e7efa..dd39b5d 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_16_r4 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_16_r4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 97ca866..e31410f 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_16_r8 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_16_r8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 330c0d9..5111c7d 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_4_i1 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_4_i1 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index a142adb..db89832 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_4_i16 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_4_i16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index d7a9280..3e1448d 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_4_i2 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_4_i2 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index c6b12e8..b66c23e 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_4_i4 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_4_i4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index bac4eb5..1eb3c4c 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_4_i8 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_4_i8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 0579519..a224fd3 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_4_r10 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_4_r10 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index d74d26d..404be41 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_4_r16 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_4_r16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 050ed5c..4d9844c 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_4_r4 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_4_r4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 483cd19..fc9b0ae 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_4_r8 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_4_r8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 1fc81d1..b0fbe0d 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_8_i1 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_8_i1 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index ecbabc7..4c4a76c 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_8_i16 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_8_i16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 2c03443..f18cd2a 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_8_i2 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_8_i2 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 9454237..758249e 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_8_i4 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_8_i4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index df80145..245522e 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_8_i8 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_8_i8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 364bf5c..ae8035e 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_8_r10 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_8_r10 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index b8ad095..af4e2ea 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_8_r16 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_8_r16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index e9df66c..3f2b2e1 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_8_r4 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_8_r4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 7d2cfff..5e00272 100644 (file)
@@ -428,51 +428,131 @@ sminloc1_8_r8 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minloc1_8_r8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINLOC intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINLOC intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 1789ec9..3815d44 100644 (file)
@@ -417,51 +417,131 @@ sminval_i1 (gfc_array_i1 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_1 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_1 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minval_i1 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_1) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_1) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = GFC_INTEGER_1_HUGE ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = GFC_INTEGER_1_HUGE;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 2916256..b8b99d3 100644 (file)
@@ -417,51 +417,131 @@ sminval_i16 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minval_i16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = GFC_INTEGER_16_HUGE ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = GFC_INTEGER_16_HUGE;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 73bf18b..1e83efd 100644 (file)
@@ -417,51 +417,131 @@ sminval_i2 (gfc_array_i2 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_2 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_2 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minval_i2 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_2) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_2) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = GFC_INTEGER_2_HUGE ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = GFC_INTEGER_2_HUGE;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 8d6e52a..a40fc2c 100644 (file)
@@ -417,51 +417,131 @@ sminval_i4 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minval_i4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = GFC_INTEGER_4_HUGE ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = GFC_INTEGER_4_HUGE;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 22cf462..af37f43 100644 (file)
@@ -417,51 +417,131 @@ sminval_i8 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minval_i8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = GFC_INTEGER_8_HUGE ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = GFC_INTEGER_8_HUGE;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index f4d467c..829c630 100644 (file)
@@ -417,51 +417,131 @@ sminval_r10 (gfc_array_r10 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_REAL_10 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_REAL_10 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minval_r10 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_REAL_10) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_REAL_10) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = GFC_REAL_10_HUGE ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = GFC_REAL_10_HUGE;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 7ba19c9..c3797c7 100644 (file)
@@ -417,51 +417,131 @@ sminval_r16 (gfc_array_r16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_REAL_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_REAL_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minval_r16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_REAL_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_REAL_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = GFC_REAL_16_HUGE ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = GFC_REAL_16_HUGE;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 3b29f2f..c8c8fd5 100644 (file)
@@ -417,51 +417,131 @@ sminval_r4 (gfc_array_r4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_REAL_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_REAL_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minval_r4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_REAL_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_REAL_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = GFC_REAL_4_HUGE ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = GFC_REAL_4_HUGE;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index adca8b2..e404b17 100644 (file)
@@ -417,51 +417,131 @@ sminval_r8 (gfc_array_r8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_REAL_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_REAL_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       minval_r8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_REAL_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_REAL_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " MINVAL intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in MINVAL intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " MINVAL intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = GFC_REAL_8_HUGE ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = GFC_REAL_8_HUGE;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index def678a..0366544 100644 (file)
@@ -416,51 +416,131 @@ sproduct_c10 (gfc_array_c10 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_COMPLEX_10 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_COMPLEX_10 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       product_c10 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_COMPLEX_10) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_COMPLEX_10) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " PRODUCT intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in PRODUCT intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " PRODUCT intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 1 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 1;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index d8750ae..c4612cf 100644 (file)
@@ -416,51 +416,131 @@ sproduct_c16 (gfc_array_c16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_COMPLEX_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_COMPLEX_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       product_c16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_COMPLEX_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_COMPLEX_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " PRODUCT intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in PRODUCT intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " PRODUCT intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 1 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 1;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 7cac33f..53c7f80 100644 (file)
@@ -416,51 +416,131 @@ sproduct_c4 (gfc_array_c4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_COMPLEX_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_COMPLEX_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       product_c4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_COMPLEX_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_COMPLEX_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " PRODUCT intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in PRODUCT intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " PRODUCT intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 1 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 1;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index e4f0f6b..812fb3b 100644 (file)
@@ -416,51 +416,131 @@ sproduct_c8 (gfc_array_c8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_COMPLEX_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_COMPLEX_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       product_c8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_COMPLEX_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_COMPLEX_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " PRODUCT intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in PRODUCT intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " PRODUCT intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 1 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 1;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 5a428ca..72b894f 100644 (file)
@@ -416,51 +416,131 @@ sproduct_i1 (gfc_array_i1 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_1 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_1 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       product_i1 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_1) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_1) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " PRODUCT intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in PRODUCT intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " PRODUCT intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 1 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 1;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index a1593a4..e042931 100644 (file)
@@ -416,51 +416,131 @@ sproduct_i16 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       product_i16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " PRODUCT intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in PRODUCT intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " PRODUCT intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 1 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 1;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 16793f8..af3da57 100644 (file)
@@ -416,51 +416,131 @@ sproduct_i2 (gfc_array_i2 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_2 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_2 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       product_i2 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_2) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_2) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " PRODUCT intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in PRODUCT intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " PRODUCT intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 1 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 1;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index cbace91..75028e5 100644 (file)
@@ -416,51 +416,131 @@ sproduct_i4 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       product_i4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " PRODUCT intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in PRODUCT intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " PRODUCT intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 1 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 1;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index f1fc567..1fc446c 100644 (file)
@@ -416,51 +416,131 @@ sproduct_i8 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       product_i8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " PRODUCT intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in PRODUCT intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " PRODUCT intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 1 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 1;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 4b7c580..7956b30 100644 (file)
@@ -416,51 +416,131 @@ sproduct_r10 (gfc_array_r10 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_REAL_10 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_REAL_10 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       product_r10 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_REAL_10) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_REAL_10) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " PRODUCT intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in PRODUCT intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " PRODUCT intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 1 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 1;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index b18155b..9cfd8df 100644 (file)
@@ -416,51 +416,131 @@ sproduct_r16 (gfc_array_r16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_REAL_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_REAL_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       product_r16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_REAL_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_REAL_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " PRODUCT intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in PRODUCT intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " PRODUCT intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 1 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 1;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 754cac2..276d91a 100644 (file)
@@ -416,51 +416,131 @@ sproduct_r4 (gfc_array_r4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_REAL_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_REAL_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       product_r4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_REAL_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_REAL_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " PRODUCT intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in PRODUCT intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " PRODUCT intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 1 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 1;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 5f68856..2c07381 100644 (file)
@@ -416,51 +416,131 @@ sproduct_r8 (gfc_array_r8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_REAL_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_REAL_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       product_r8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_REAL_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_REAL_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " PRODUCT intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in PRODUCT intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " PRODUCT intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 1 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 1;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index e495a0b..6c22289 100644 (file)
@@ -416,51 +416,131 @@ ssum_c10 (gfc_array_c10 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_COMPLEX_10 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_COMPLEX_10 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       sum_c10 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_COMPLEX_10) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_COMPLEX_10) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " SUM intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in SUM intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " SUM intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index c73083a..203c483 100644 (file)
@@ -416,51 +416,131 @@ ssum_c16 (gfc_array_c16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_COMPLEX_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_COMPLEX_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       sum_c16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_COMPLEX_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_COMPLEX_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " SUM intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in SUM intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " SUM intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 6f32327..4a91bb2 100644 (file)
@@ -416,51 +416,131 @@ ssum_c4 (gfc_array_c4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_COMPLEX_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_COMPLEX_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       sum_c4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_COMPLEX_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_COMPLEX_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " SUM intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in SUM intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " SUM intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 80db110..f877d09 100644 (file)
@@ -416,51 +416,131 @@ ssum_c8 (gfc_array_c8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_COMPLEX_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_COMPLEX_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       sum_c8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_COMPLEX_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_COMPLEX_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " SUM intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in SUM intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " SUM intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index c652712..de46086 100644 (file)
@@ -416,51 +416,131 @@ ssum_i1 (gfc_array_i1 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_1 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_1 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       sum_i1 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_1) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_1) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " SUM intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in SUM intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " SUM intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 43a29a2..b7fc6e1 100644 (file)
@@ -416,51 +416,131 @@ ssum_i16 (gfc_array_i16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       sum_i16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " SUM intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in SUM intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " SUM intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 6c6fcc1..f7b85d7 100644 (file)
@@ -416,51 +416,131 @@ ssum_i2 (gfc_array_i2 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_2 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_2 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       sum_i2 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_2) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_2) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " SUM intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in SUM intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " SUM intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index e28d2c9..04849b5 100644 (file)
@@ -416,51 +416,131 @@ ssum_i4 (gfc_array_i4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       sum_i4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " SUM intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in SUM intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " SUM intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 6e824f1..bf65615 100644 (file)
@@ -416,51 +416,131 @@ ssum_i8 (gfc_array_i8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_INTEGER_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_INTEGER_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       sum_i8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_INTEGER_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " SUM intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in SUM intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " SUM intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 1ebd1ed..deefd30 100644 (file)
@@ -416,51 +416,131 @@ ssum_r10 (gfc_array_r10 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_REAL_10 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_REAL_10 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       sum_r10 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_REAL_10) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_REAL_10) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " SUM intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in SUM intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " SUM intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 0038983..ee365f7 100644 (file)
@@ -416,51 +416,131 @@ ssum_r16 (gfc_array_r16 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_REAL_16 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_REAL_16 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       sum_r16 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_REAL_16) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_REAL_16) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " SUM intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in SUM intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " SUM intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 1f058dc..07f4339 100644 (file)
@@ -416,51 +416,131 @@ ssum_r4 (gfc_array_r4 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_REAL_4 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_REAL_4 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       sum_r4 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_REAL_4) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_REAL_4) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " SUM intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in SUM intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " SUM intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 82a03bc..f83c683 100644 (file)
@@ -416,51 +416,131 @@ ssum_r8 (gfc_array_r8 * const restrict retarray,
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  GFC_REAL_8 * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  GFC_REAL_8 *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       sum_r8 (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (GFC_REAL_8) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (GFC_REAL_8) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " SUM intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in SUM intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " SUM intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = 0 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = 0;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }
 
 #endif
index 9769e4d..5ab2952 100644 (file)
@@ -398,51 +398,131 @@ void
        const index_type * const restrict pdim, 
        GFC_LOGICAL_4 * mask)
 {
+  index_type count[GFC_MAX_DIMENSIONS];
+  index_type extent[GFC_MAX_DIMENSIONS];
+  index_type sstride[GFC_MAX_DIMENSIONS];
+  index_type dstride[GFC_MAX_DIMENSIONS];
+  rtype_name * restrict dest;
   index_type rank;
   index_type n;
-  index_type dstride;
-  rtype_name *dest;
+  index_type dim;
+
 
   if (*mask)
     {
       name`'rtype_qual`_'atype_code (retarray, array, pdim);
       return;
     }
-    rank = GFC_DESCRIPTOR_RANK (array);
-  if (rank <= 0)
-    runtime_error ("Rank of array needs to be > 0");
+  /* Make dim zero based to avoid confusion.  */
+  dim = (*pdim) - 1;
+  rank = GFC_DESCRIPTOR_RANK (array) - 1;
+
+  for (n = 0; n < dim; n++)
+    {
+      sstride[n] = array->dim[n].stride;
+      extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
+
+      if (extent[n] <= 0)
+       extent[n] = 0;
+    }
+
+  for (n = dim; n < rank; n++)
+    {
+      sstride[n] = array->dim[n + 1].stride;
+      extent[n] =
+        array->dim[n + 1].ubound + 1 - array->dim[n + 1].lbound;
+
+      if (extent[n] <= 0)
+        extent[n] = 0;
+    }
 
   if (retarray->data == NULL)
     {
-      retarray->dim[0].lbound = 0;
-      retarray->dim[0].ubound = rank-1;
-      retarray->dim[0].stride = 1;
-      retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
+      size_t alloc_size;
+
+      for (n = 0; n < rank; n++)
+        {
+          retarray->dim[n].lbound = 0;
+          retarray->dim[n].ubound = extent[n]-1;
+          if (n == 0)
+            retarray->dim[n].stride = 1;
+          else
+            retarray->dim[n].stride = retarray->dim[n-1].stride * extent[n-1];
+        }
+
       retarray->offset = 0;
-      retarray->data = internal_malloc_size (sizeof (rtype_name) * rank);
+      retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
+
+      alloc_size = sizeof (rtype_name) * retarray->dim[rank-1].stride
+                  * extent[rank-1];
+
+      if (alloc_size == 0)
+       {
+         /* Make sure we have a zero-sized array.  */
+         retarray->dim[0].lbound = 0;
+         retarray->dim[0].ubound = -1;
+         return;
+       }
+      else
+       retarray->data = internal_malloc_size (alloc_size);
     }
   else
     {
+      if (rank != GFC_DESCRIPTOR_RANK (retarray))
+       runtime_error ("rank of return array incorrect in"
+                      " u_name intrinsic: is %ld, should be %ld",
+                      (long int) (GFC_DESCRIPTOR_RANK (retarray)),
+                      (long int) rank);
+
       if (compile_options.bounds_check)
        {
-         int ret_rank;
-         index_type ret_extent;
-
-         ret_rank = GFC_DESCRIPTOR_RANK (retarray);
-         if (ret_rank != 1)
-           runtime_error ("rank of return array in u_name intrinsic"
-                          " should be 1, is %ld", (long int) ret_rank);
+         for (n=0; n < rank; n++)
+           {
+             index_type ret_extent;
 
-         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
-           if (ret_extent != rank)
-             runtime_error ("dimension of return array incorrect");
+             ret_extent = retarray->dim[n].ubound + 1
+               - retarray->dim[n].lbound;
+             if (extent[n] != ret_extent)
+               runtime_error ("Incorrect extent in return value of"
+                              " u_name intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) ret_extent, (long int) extent[n]);
+           }
        }
     }
-    dstride = retarray->dim[0].stride;
-    dest = retarray->data;
 
-    for (n = 0; n < rank; n++)
-      dest[n * dstride] = $1 ;
+  for (n = 0; n < rank; n++)
+    {
+      count[n] = 0;
+      dstride[n] = retarray->dim[n].stride;
+    }
+
+  dest = retarray->data;
+
+  while(1)
+    {
+      *dest = '$1`;
+      count[0]++;
+      dest += dstride[0];
+      n = 0;
+      while (count[n] == extent[n])
+        {
+         /* When we get to the end of a dimension, reset it and increment
+             the next dimension.  */
+          count[n] = 0;
+          /* We could precalculate these products, but this is a less
+             frequently used path so probably not worth it.  */
+          dest -= dstride[n] * extent[n];
+          n++;
+          if (n == rank)
+           return;
+          else
+            {
+              count[n]++;
+              dest += dstride[n];
+            }
+       }
+    }
 }')dnl
 define(ARRAY_FUNCTION,
 `START_ARRAY_FUNCTION