1 /* Implementation of the RESHAPE
2 Copyright 2002 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
34 #include "libgfortran.h"
36 typedef GFC_ARRAY_DESCRIPTOR(1, index_type) shape_type;
38 /* The shape parameter is ignored. We can currently deduce the shape from the
41 extern void reshape_8 (gfc_array_i8 *, gfc_array_i8 *, shape_type *,
42 gfc_array_i8 *, shape_type *);
43 export_proto(reshape_8);
46 reshape_8 (gfc_array_i8 * ret, gfc_array_i8 * source, shape_type * shape,
47 gfc_array_i8 * pad, shape_type * order)
49 /* r.* indicates the return array. */
50 index_type rcount[GFC_MAX_DIMENSIONS];
51 index_type rextent[GFC_MAX_DIMENSIONS];
52 index_type rstride[GFC_MAX_DIMENSIONS];
59 /* s.* indicates the source array. */
60 index_type scount[GFC_MAX_DIMENSIONS];
61 index_type sextent[GFC_MAX_DIMENSIONS];
62 index_type sstride[GFC_MAX_DIMENSIONS];
66 const GFC_INTEGER_8 *sptr;
67 /* p.* indicates the pad array. */
68 index_type pcount[GFC_MAX_DIMENSIONS];
69 index_type pextent[GFC_MAX_DIMENSIONS];
70 index_type pstride[GFC_MAX_DIMENSIONS];
73 const GFC_INTEGER_8 *pptr;
75 const GFC_INTEGER_8 *src;
79 if (source->dim[0].stride == 0)
80 source->dim[0].stride = 1;
81 if (shape->dim[0].stride == 0)
82 shape->dim[0].stride = 1;
83 if (pad && pad->dim[0].stride == 0)
84 pad->dim[0].stride = 1;
85 if (order && order->dim[0].stride == 0)
86 order->dim[0].stride = 1;
88 if (ret->data == NULL)
90 rdim = shape->dim[0].ubound - shape->dim[0].lbound + 1;
92 for (n=0; n < rdim; n++)
94 ret->dim[n].lbound = 0;
95 rex = shape->data[n * shape->dim[0].stride];
96 ret->dim[n].ubound = rex - 1;
97 ret->dim[n].stride = rs;
101 ret->data = internal_malloc_size ( rs * sizeof (GFC_INTEGER_8));
102 ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim;
106 rdim = GFC_DESCRIPTOR_RANK (ret);
107 if (ret->dim[0].stride == 0)
108 ret->dim[0].stride = 1;
112 for (n = 0; n < rdim; n++)
115 dim = order->data[n * order->dim[0].stride] - 1;
120 rstride[n] = ret->dim[dim].stride;
121 rextent[n] = ret->dim[dim].ubound + 1 - ret->dim[dim].lbound;
123 if (rextent[n] != shape->data[dim * shape->dim[0].stride])
124 runtime_error ("shape and target do not conform");
126 if (rsize == rstride[n])
134 sdim = GFC_DESCRIPTOR_RANK (source);
136 for (n = 0; n < sdim; n++)
139 sstride[n] = source->dim[n].stride;
140 sextent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound;
144 if (ssize == sstride[n])
152 pdim = GFC_DESCRIPTOR_RANK (pad);
154 for (n = 0; n < pdim; n++)
157 pstride[n] = pad->dim[n].stride;
158 pextent[n] = pad->dim[n].ubound + 1 - pad->dim[n].lbound;
161 if (psize == pstride[n])
175 if (rsize != 0 && ssize != 0 && psize != 0)
177 rsize *= sizeof (GFC_INTEGER_8);
178 ssize *= sizeof (GFC_INTEGER_8);
179 psize *= sizeof (GFC_INTEGER_8);
180 reshape_packed ((char *)ret->data, rsize, (char *)source->data,
181 ssize, pad ? (char *)pad->data : NULL, psize);
185 src = sptr = source->data;
186 rstride0 = rstride[0];
187 sstride0 = sstride[0];
191 /* Select between the source and pad arrays. */
193 /* Advance to the next element. */
198 /* Advance to the next destination element. */
200 while (rcount[n] == rextent[n])
202 /* When we get to the end of a dimension, reset it and increment
203 the next dimension. */
205 /* We could precalculate these products, but this is a less
206 frequently used path so proabably not worth it. */
207 rptr -= rstride[n] * rextent[n];
211 /* Break out of the loop. */
221 /* Advance to the next source element. */
223 while (scount[n] == sextent[n])
225 /* When we get to the end of a dimension, reset it and increment
226 the next dimension. */
228 /* We could precalculate these products, but this is a less
229 frequently used path so proabably not worth it. */
230 src -= sstride[n] * sextent[n];
236 /* Switch to the pad array. */
239 for (dim = 0; dim < pdim; dim++)
241 scount[dim] = pcount[dim];
242 sextent[dim] = pextent[dim];
243 sstride[dim] = pstride[dim];
244 sstride0 = sstride[0];
247 /* We now start again from the beginning of the pad array. */