OSDN Git Service

2008-05-18 Thomas Koenig <tkoenig@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libgfortran / generated / maxloc0_16_i8.c
index d8a6261..b2098a7 100644 (file)
@@ -1,5 +1,5 @@
 /* Implementation of the MAXLOC intrinsic
-   Copyright 2002 Free Software Foundation, Inc.
+   Copyright 2002, 2007 Free Software Foundation, Inc.
    Contributed by Paul Brook <paul@nowt.org>
 
 This file is part of the GNU Fortran 95 runtime library (libgfortran).
@@ -28,29 +28,29 @@ License along with libgfortran; see the file COPYING.  If not,
 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 Boston, MA 02110-1301, USA.  */
 
-#include "config.h"
+#include "libgfortran.h"
 #include <stdlib.h>
 #include <assert.h>
-#include <float.h>
 #include <limits.h>
-#include "libgfortran.h"
 
 
 #if defined (HAVE_GFC_INTEGER_8) && defined (HAVE_GFC_INTEGER_16)
 
 
-extern void maxloc0_16_i8 (gfc_array_i16 * retarray, gfc_array_i8 *array);
+extern void maxloc0_16_i8 (gfc_array_i16 * const restrict retarray, 
+       gfc_array_i8 * const restrict array);
 export_proto(maxloc0_16_i8);
 
 void
-maxloc0_16_i8 (gfc_array_i16 * retarray, gfc_array_i8 *array)
+maxloc0_16_i8 (gfc_array_i16 * const restrict retarray, 
+       gfc_array_i8 * const restrict array)
 {
   index_type count[GFC_MAX_DIMENSIONS];
   index_type extent[GFC_MAX_DIMENSIONS];
   index_type sstride[GFC_MAX_DIMENSIONS];
   index_type dstride;
-  GFC_INTEGER_8 *base;
-  GFC_INTEGER_16 *dest;
+  const GFC_INTEGER_8 *base;
+  GFC_INTEGER_16 * restrict dest;
   index_type rank;
   index_type n;
 
@@ -69,21 +69,24 @@ maxloc0_16_i8 (gfc_array_i16 * retarray, gfc_array_i8 *array)
     }
   else
     {
-      if (GFC_DESCRIPTOR_RANK (retarray) != 1)
-       runtime_error ("rank of return array does not equal 1");
-
-      if (retarray->dim[0].ubound + 1 - retarray->dim[0].lbound != rank)
-        runtime_error ("dimension of return array incorrect");
-
-      if (retarray->dim[0].stride == 0)
-       retarray->dim[0].stride = 1;
+      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);
+
+         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
+         if (ret_extent != rank)
+           runtime_error ("Incorrect extent in return value of"
+                          " MAXLOC intrnisic: is %ld, should be %ld",
+                          (long int) ret_extent, (long int) rank);
+       }
     }
 
-  /* TODO:  It should be a front end job to correctly set the strides.  */
-
-  if (array->dim[0].stride == 0)
-    array->dim[0].stride = 1;
-
   dstride = retarray->dim[0].stride;
   dest = retarray->data;
   for (n = 0; n < rank; n++)
@@ -104,19 +107,19 @@ maxloc0_16_i8 (gfc_array_i16 * retarray, gfc_array_i8 *array)
 
   /* Initialize the return value.  */
   for (n = 0; n < rank; n++)
-    dest[n * dstride] = 1;
+    dest[n * dstride] = 0;
   {
 
   GFC_INTEGER_8 maxval;
 
-  maxval = -GFC_INTEGER_8_HUGE;
+  maxval = (-GFC_INTEGER_8_HUGE-1);
 
   while (base)
     {
       {
         /* Implementation start.  */
 
-  if (*base > maxval)
+  if (*base > maxval || !dest[0])
     {
       maxval = *base;
       for (n = 0; n < rank; n++)
@@ -134,7 +137,7 @@ maxloc0_16_i8 (gfc_array_i16 * retarray, gfc_array_i8 *array)
              the next dimension.  */
           count[n] = 0;
           /* We could precalculate these products, but this is a less
-             frequently used path so proabably not worth it.  */
+             frequently used path so probably not worth it.  */
           base -= sstride[n] * extent[n];
           n++;
           if (n == rank)
@@ -154,12 +157,14 @@ maxloc0_16_i8 (gfc_array_i16 * retarray, gfc_array_i8 *array)
 }
 
 
-extern void mmaxloc0_16_i8 (gfc_array_i16 *, gfc_array_i8 *, gfc_array_l4 *);
+extern void mmaxloc0_16_i8 (gfc_array_i16 * const restrict, 
+       gfc_array_i8 * const restrict, gfc_array_l1 * const restrict);
 export_proto(mmaxloc0_16_i8);
 
 void
-mmaxloc0_16_i8 (gfc_array_i16 * retarray, gfc_array_i8 *array,
-                                 gfc_array_l4 * mask)
+mmaxloc0_16_i8 (gfc_array_i16 * const restrict retarray, 
+       gfc_array_i8 * const restrict array,
+       gfc_array_l1 * const restrict mask)
 {
   index_type count[GFC_MAX_DIMENSIONS];
   index_type extent[GFC_MAX_DIMENSIONS];
@@ -167,10 +172,11 @@ mmaxloc0_16_i8 (gfc_array_i16 * retarray, gfc_array_i8 *array,
   index_type mstride[GFC_MAX_DIMENSIONS];
   index_type dstride;
   GFC_INTEGER_16 *dest;
-  GFC_INTEGER_8 *base;
-  GFC_LOGICAL_4 *mbase;
+  const GFC_INTEGER_8 *base;
+  GFC_LOGICAL_1 *mbase;
   int rank;
   index_type n;
+  int mask_kind;
 
   rank = GFC_DESCRIPTOR_RANK (array);
   if (rank <= 0)
@@ -187,30 +193,62 @@ mmaxloc0_16_i8 (gfc_array_i16 * retarray, gfc_array_i8 *array,
     }
   else
     {
-      if (GFC_DESCRIPTOR_RANK (retarray) != 1)
-       runtime_error ("rank of return array does not equal 1");
-
-      if (retarray->dim[0].ubound + 1 - retarray->dim[0].lbound != rank)
-        runtime_error ("dimension of return array incorrect");
-
-      if (retarray->dim[0].stride == 0)
-       retarray->dim[0].stride = 1;
+      if (compile_options.bounds_check)
+       {
+         int ret_rank, mask_rank;
+         index_type ret_extent;
+         int n;
+         index_type array_extent, mask_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);
+
+         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
+         if (ret_extent != rank)
+           runtime_error ("Incorrect extent in return value of"
+                          " MAXLOC intrnisic: is %ld, should be %ld",
+                          (long int) ret_extent, (long int) rank);
+       
+         mask_rank = GFC_DESCRIPTOR_RANK (mask);
+         if (rank != mask_rank)
+           runtime_error ("rank of MASK argument in MAXLOC intrnisic"
+                          "should be %ld, is %ld", (long int) rank,
+                          (long int) mask_rank);
+
+         for (n=0; n<rank; n++)
+           {
+             array_extent = array->dim[n].ubound + 1 - array->dim[n].lbound;
+             mask_extent = mask->dim[n].ubound + 1 - mask->dim[n].lbound;
+             if (array_extent != mask_extent)
+               runtime_error ("Incorrect extent in MASK argument of"
+                              " MAXLOC intrinsic in dimension %ld:"
+                              " is %ld, should be %ld", (long int) n + 1,
+                              (long int) mask_extent, (long int) array_extent);
+           }
+       }
     }
 
-  /* TODO:  It should be a front end job to correctly set the strides.  */
+  mask_kind = GFC_DESCRIPTOR_SIZE (mask);
 
-  if (array->dim[0].stride == 0)
-    array->dim[0].stride = 1;
+  mbase = mask->data;
 
-  if (mask->dim[0].stride == 0)
-    mask->dim[0].stride = 1;
+  if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
+#ifdef HAVE_GFC_LOGICAL_16
+      || mask_kind == 16
+#endif
+      )
+    mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
+  else
+    runtime_error ("Funny sized logical array");
 
   dstride = retarray->dim[0].stride;
   dest = retarray->data;
   for (n = 0; n < rank; n++)
     {
       sstride[n] = array->dim[n].stride;
-      mstride[n] = mask->dim[n].stride;
+      mstride[n] = mask->dim[n].stride * mask_kind;
       extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
       count[n] = 0;
       if (extent[n] <= 0)
@@ -223,33 +261,22 @@ mmaxloc0_16_i8 (gfc_array_i16 * retarray, gfc_array_i8 *array,
     }
 
   base = array->data;
-  mbase = mask->data;
-
-  if (GFC_DESCRIPTOR_SIZE (mask) != 4)
-    {
-      /* This allows the same loop to be used for all logical types.  */
-      assert (GFC_DESCRIPTOR_SIZE (mask) == 8);
-      for (n = 0; n < rank; n++)
-        mstride[n] <<= 1;
-      mbase = (GFOR_POINTER_L8_TO_L4 (mbase));
-    }
-
 
   /* Initialize the return value.  */
   for (n = 0; n < rank; n++)
-    dest[n * dstride] = 1;
+    dest[n * dstride] = 0;
   {
 
   GFC_INTEGER_8 maxval;
 
-  maxval = -GFC_INTEGER_8_HUGE;
+  maxval = (-GFC_INTEGER_8_HUGE-1);
 
   while (base)
     {
       {
         /* Implementation start.  */
 
-  if (*mbase && *base > maxval)
+  if (*mbase && (*base > maxval || !dest[0]))
     {
       maxval = *base;
       for (n = 0; n < rank; n++)
@@ -268,7 +295,7 @@ mmaxloc0_16_i8 (gfc_array_i16 * retarray, gfc_array_i8 *array,
              the next dimension.  */
           count[n] = 0;
           /* We could precalculate these products, but this is a less
-             frequently used path so proabably not worth it.  */
+             frequently used path so probably not worth it.  */
           base -= sstride[n] * extent[n];
           mbase -= mstride[n] * extent[n];
           n++;
@@ -289,4 +316,62 @@ mmaxloc0_16_i8 (gfc_array_i16 * retarray, gfc_array_i8 *array,
   }
 }
 
+
+extern void smaxloc0_16_i8 (gfc_array_i16 * const restrict, 
+       gfc_array_i8 * const restrict, GFC_LOGICAL_4 *);
+export_proto(smaxloc0_16_i8);
+
+void
+smaxloc0_16_i8 (gfc_array_i16 * const restrict retarray, 
+       gfc_array_i8 * const restrict array,
+       GFC_LOGICAL_4 * mask)
+{
+  index_type rank;
+  index_type dstride;
+  index_type n;
+  GFC_INTEGER_16 *dest;
+
+  if (*mask)
+    {
+      maxloc0_16_i8 (retarray, array);
+      return;
+    }
+
+  rank = GFC_DESCRIPTOR_RANK (array);
+
+  if (rank <= 0)
+    runtime_error ("Rank of array needs to be > 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;
+      retarray->offset = 0;
+      retarray->data = internal_malloc_size (sizeof (GFC_INTEGER_16) * rank);
+    }
+  else
+    {
+      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);
+
+         ret_extent = retarray->dim[0].ubound + 1 - retarray->dim[0].lbound;
+           if (ret_extent != rank)
+             runtime_error ("dimension of return array incorrect");
+       }
+    }
+
+  dstride = retarray->dim[0].stride;
+  dest = retarray->data;
+  for (n = 0; n<rank; n++)
+    dest[n * dstride] = 0 ;
+}
 #endif