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 (libgfor).
7 Libgfor is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 Ligbfor is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
25 #include "libgfortran.h"
27 typedef GFC_ARRAY_DESCRIPTOR(1, index_type) shape_type;
29 /* The shape parameter is ignored. We can currently deduce the shape from the
32 extern void reshape_4 (gfc_array_i4 *, gfc_array_i4 *, shape_type *,
33 gfc_array_i4 *, shape_type *);
34 export_proto(reshape_4);
37 reshape_4 (gfc_array_i4 * ret, gfc_array_i4 * source, shape_type * shape,
38 gfc_array_i4 * pad, shape_type * order)
40 /* r.* indicates the return array. */
41 index_type rcount[GFC_MAX_DIMENSIONS - 1];
42 index_type rextent[GFC_MAX_DIMENSIONS - 1];
43 index_type rstride[GFC_MAX_DIMENSIONS - 1];
48 /* s.* indicates the source array. */
49 index_type scount[GFC_MAX_DIMENSIONS - 1];
50 index_type sextent[GFC_MAX_DIMENSIONS - 1];
51 index_type sstride[GFC_MAX_DIMENSIONS - 1];
55 const GFC_INTEGER_4 *sptr;
56 /* p.* indicates the pad array. */
57 index_type pcount[GFC_MAX_DIMENSIONS - 1];
58 index_type pextent[GFC_MAX_DIMENSIONS - 1];
59 index_type pstride[GFC_MAX_DIMENSIONS - 1];
62 const GFC_INTEGER_4 *pptr;
64 const GFC_INTEGER_4 *src;
68 if (ret->dim[0].stride == 0)
69 ret->dim[0].stride = 1;
70 if (source->dim[0].stride == 0)
71 source->dim[0].stride = 1;
72 if (shape->dim[0].stride == 0)
73 shape->dim[0].stride = 1;
74 if (pad && pad->dim[0].stride == 0)
75 pad->dim[0].stride = 1;
76 if (order && order->dim[0].stride == 0)
77 order->dim[0].stride = 1;
79 rdim = GFC_DESCRIPTOR_RANK (ret);
81 for (n = 0; n < rdim; n++)
84 dim = order->data[n * order->dim[0].stride] - 1;
89 rstride[n] = ret->dim[dim].stride;
90 rextent[n] = ret->dim[dim].ubound + 1 - ret->dim[dim].lbound;
92 if (rextent[n] != shape->data[dim * shape->dim[0].stride])
93 runtime_error ("shape and target do not conform");
95 if (rsize == rstride[n])
99 if (rextent[dim] <= 0)
103 sdim = GFC_DESCRIPTOR_RANK (source);
105 for (n = 0; n < sdim; n++)
108 sstride[n] = source->dim[n].stride;
109 sextent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound;
113 if (ssize == sstride[n])
121 if (pad->dim[0].stride == 0)
122 pad->dim[0].stride = 1;
123 pdim = GFC_DESCRIPTOR_RANK (pad);
125 for (n = 0; n < pdim; n++)
128 pstride[n] = pad->dim[n].stride;
129 pextent[n] = pad->dim[n].ubound + 1 - pad->dim[n].lbound;
132 if (psize == pstride[n])
146 if (rsize != 0 && ssize != 0 && psize != 0)
151 reshape_packed ((char *)ret->data, rsize, (char *)source->data,
152 ssize, pad ? (char *)pad->data : NULL, psize);
156 src = sptr = source->data;
157 rstride0 = rstride[0];
158 sstride0 = sstride[0];
162 /* Select between the source and pad arrays. */
164 /* Advance to the next element. */
169 /* Advance to the next destination element. */
171 while (rcount[n] == rextent[n])
173 /* When we get to the end of a dimension, reset it and increment
174 the next dimension. */
176 /* We could precalculate these products, but this is a less
177 frequently used path so proabably not worth it. */
178 rptr -= rstride[n] * rextent[n];
182 /* Break out of the loop. */
192 /* Advance to the next source element. */
194 while (scount[n] == sextent[n])
196 /* When we get to the end of a dimension, reset it and increment
197 the next dimension. */
199 /* We could precalculate these products, but this is a less
200 frequently used path so proabably not worth it. */
201 src -= sstride[n] * sextent[n];
207 /* Switch to the pad array. */
210 for (dim = 0; dim < pdim; dim++)
212 scount[dim] = pcount[dim];
213 sextent[dim] = pextent[dim];
214 sstride[dim] = pstride[dim];
215 sstride0 = sstride[0];
218 /* We now start again from the beginning of the pad array. */