OSDN Git Service

* runtime/select.c: Moved content to select_inc.c. Include it.
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / reshape_generic.c
index 97860b6..ad82351 100644 (file)
@@ -1,5 +1,5 @@
 /* Generic implementation of the RESHAPE intrinsic
-   Copyright 2002, 2006 Free Software Foundation, Inc.
+   Copyright 2002, 2006, 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,18 +28,14 @@ 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 <string.h>
 #include <assert.h>
-#include "libgfortran.h"
 
 typedef GFC_ARRAY_DESCRIPTOR(1, index_type) shape_type;
 typedef GFC_ARRAY_DESCRIPTOR(GFC_MAX_DIMENSIONS, char) parray;
 
-/* The shape parameter is ignored. We can currently deduce the shape from the
-   return array.  */
-
 static void
 reshape_internal (parray *ret, parray *source, shape_type *shape,
                  parray *pad, shape_type *order, index_type size)
@@ -73,15 +69,33 @@ reshape_internal (parray *ret, parray *source, shape_type *shape,
   const char *src;
   int n;
   int dim;
+  int sempty, pempty, shape_empty;
+  index_type shape_data[GFC_MAX_DIMENSIONS];
+
+  rdim = shape->dim[0].ubound - shape->dim[0].lbound + 1;
+  if (rdim != GFC_DESCRIPTOR_RANK(ret))
+    runtime_error("rank of return array incorrect in RESHAPE intrinsic");
+
+  shape_empty = 0;
+
+  for (n = 0; n < rdim; n++)
+    {
+      shape_data[n] = shape->data[n * shape->dim[0].stride];
+      if (shape_data[n] <= 0)
+       {
+         shape_data[n] = 0;
+         shape_empty = 1;
+       }
+    }
 
   if (ret->data == NULL)
     {
       rdim = shape->dim[0].ubound - shape->dim[0].lbound + 1;
       rs = 1;
-      for (n=0; n < rdim; n++)
+      for (n = 0; n < rdim; n++)
        {
          ret->dim[n].lbound = 0;
-         rex = shape->data[n * shape->dim[0].stride];
+         rex = shape_data[n];
          ret->dim[n].ubound =  rex - 1;
          ret->dim[n].stride = rs;
          rs *= rex;
@@ -90,10 +104,9 @@ reshape_internal (parray *ret, parray *source, shape_type *shape,
       ret->data = internal_malloc_size ( rs * size );
       ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim;
     }
-  else
-    {
-      rdim = GFC_DESCRIPTOR_RANK (ret);
-    }
+
+  if (shape_empty)
+    return;
 
   rsize = 1;
   for (n = 0; n < rdim; n++)
@@ -107,7 +120,7 @@ reshape_internal (parray *ret, parray *source, shape_type *shape,
       rstride[n] = ret->dim[dim].stride;
       rextent[n] = ret->dim[dim].ubound + 1 - ret->dim[dim].lbound;
 
-      if (rextent[n] != shape->data[dim * shape->dim[0].stride])
+      if (rextent[n] != shape_data[dim])
         runtime_error ("shape and target do not conform");
 
       if (rsize == rstride[n])
@@ -120,13 +133,17 @@ reshape_internal (parray *ret, parray *source, shape_type *shape,
 
   sdim = GFC_DESCRIPTOR_RANK (source);
   ssize = 1;
+  sempty = 0;
   for (n = 0; n < sdim; n++)
     {
       scount[n] = 0;
       sstride[n] = source->dim[n].stride;
       sextent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound;
       if (sextent[n] <= 0)
-        abort ();
+       {
+         sempty = 1;
+         sextent[n] = 0;
+       }
 
       if (ssize == sstride[n])
         ssize *= sextent[n];
@@ -138,13 +155,18 @@ reshape_internal (parray *ret, parray *source, shape_type *shape,
     {
       pdim = GFC_DESCRIPTOR_RANK (pad);
       psize = 1;
+      pempty = 0;
       for (n = 0; n < pdim; n++)
         {
           pcount[n] = 0;
           pstride[n] = pad->dim[n].stride;
           pextent[n] = pad->dim[n].ubound + 1 - pad->dim[n].lbound;
           if (pextent[n] <= 0)
-            abort ();
+           {
+             pempty = 1;
+              pextent[n] = 0;
+           }
+
           if (psize == pstride[n])
             psize *= pextent[n];
           else
@@ -156,6 +178,7 @@ reshape_internal (parray *ret, parray *source, shape_type *shape,
     {
       pdim = 0;
       psize = 1;
+      pempty = 1;
       pptr = NULL;
     }
 
@@ -173,6 +196,24 @@ reshape_internal (parray *ret, parray *source, shape_type *shape,
   rstride0 = rstride[0] * size;
   sstride0 = sstride[0] * size;
 
+  if (sempty && pempty)
+    abort ();
+
+  if (sempty)
+    {
+      /* Switch immediately to the pad array.  */
+      src = pptr;
+      sptr = NULL;
+      sdim = pdim;
+      for (dim = 0; dim < pdim; dim++)
+       {
+         scount[dim] = pcount[dim];
+         sextent[dim] = pextent[dim];
+         sstride[dim] = pstride[dim];
+         sstride0 = sstride[0] * size;
+       }
+    }
+
   while (rptr)
     {
       /* Select between the source and pad arrays.  */
@@ -182,6 +223,7 @@ reshape_internal (parray *ret, parray *source, shape_type *shape,
       src += sstride0;
       rcount[0]++;
       scount[0]++;
+
       /* Advance to the next destination element.  */
       n = 0;
       while (rcount[n] == rextent[n])
@@ -204,7 +246,8 @@ reshape_internal (parray *ret, parray *source, shape_type *shape,
               rcount[n]++;
               rptr += rstride[n] * size;
             }
-        }
+       }
+
       /* Advance to the next source element.  */
       n = 0;
       while (scount[n] == sextent[n])
@@ -238,7 +281,7 @@ reshape_internal (parray *ret, parray *source, shape_type *shape,
           else
             {
               scount[n]++;
-              sptr += sstride[n] * size;
+              src += sstride[n] * size;
             }
         }
     }
@@ -255,16 +298,33 @@ reshape (parray *ret, parray *source, shape_type *shape, parray *pad,
                    GFC_DESCRIPTOR_SIZE (source));
 }
 
-extern void reshape_char (parray *, GFC_INTEGER_4, parray *, shape_type *,
-                         parray *, shape_type *, GFC_INTEGER_4,
-                         GFC_INTEGER_4);
+
+extern void reshape_char (parray *, gfc_charlen_type, parray *, shape_type *,
+                         parray *, shape_type *, gfc_charlen_type,
+                         gfc_charlen_type);
 export_proto(reshape_char);
 
 void
-reshape_char (parray *ret, GFC_INTEGER_4 ret_length __attribute__((unused)),
+reshape_char (parray *ret, gfc_charlen_type ret_length __attribute__((unused)),
              parray *source, shape_type *shape, parray *pad,
-             shape_type *order, GFC_INTEGER_4 source_length,
-             GFC_INTEGER_4 pad_length __attribute__((unused)))
+             shape_type *order, gfc_charlen_type source_length,
+             gfc_charlen_type pad_length __attribute__((unused)))
 {
   reshape_internal (ret, source, shape, pad, order, source_length);
 }
+
+
+extern void reshape_char4 (parray *, gfc_charlen_type, parray *, shape_type *,
+                          parray *, shape_type *, gfc_charlen_type,
+                          gfc_charlen_type);
+export_proto(reshape_char4);
+
+void
+reshape_char4 (parray *ret, gfc_charlen_type ret_length __attribute__((unused)),
+              parray *source, shape_type *shape, parray *pad,
+              shape_type *order, gfc_charlen_type source_length,
+              gfc_charlen_type pad_length __attribute__((unused)))
+{
+  reshape_internal (ret, source, shape, pad, order,
+                   source_length * sizeof (gfc_char4_t));
+}