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"'
28 typedef GFC_ARRAY_DESCRIPTOR(1, index_type) shape_type;
30 /* The shape parameter is ignored. We can currently deduce the shape from the
32 dnl Only the kind (ie size) is used to name the function.
34 extern void reshape_`'rtype_kind (rtype *, rtype *, shape_type *,
35 rtype *, shape_type *);
36 export_proto(reshape_`'rtype_kind);
39 reshape_`'rtype_kind (rtype * ret, rtype * source, shape_type * shape,
40 rtype * pad, shape_type * order)
42 /* r.* indicates the return array. */
43 index_type rcount[GFC_MAX_DIMENSIONS - 1];
44 index_type rextent[GFC_MAX_DIMENSIONS - 1];
45 index_type rstride[GFC_MAX_DIMENSIONS - 1];
50 /* s.* indicates the source array. */
51 index_type scount[GFC_MAX_DIMENSIONS - 1];
52 index_type sextent[GFC_MAX_DIMENSIONS - 1];
53 index_type sstride[GFC_MAX_DIMENSIONS - 1];
57 const rtype_name *sptr;
58 /* p.* indicates the pad array. */
59 index_type pcount[GFC_MAX_DIMENSIONS - 1];
60 index_type pextent[GFC_MAX_DIMENSIONS - 1];
61 index_type pstride[GFC_MAX_DIMENSIONS - 1];
64 const rtype_name *pptr;
66 const rtype_name *src;
70 if (ret->dim[0].stride == 0)
71 ret->dim[0].stride = 1;
72 if (source->dim[0].stride == 0)
73 source->dim[0].stride = 1;
74 if (shape->dim[0].stride == 0)
75 shape->dim[0].stride = 1;
76 if (pad && pad->dim[0].stride == 0)
77 pad->dim[0].stride = 1;
78 if (order && order->dim[0].stride == 0)
79 order->dim[0].stride = 1;
81 rdim = GFC_DESCRIPTOR_RANK (ret);
83 for (n = 0; n < rdim; n++)
86 dim = order->data[n * order->dim[0].stride] - 1;
91 rstride[n] = ret->dim[dim].stride;
92 rextent[n] = ret->dim[dim].ubound + 1 - ret->dim[dim].lbound;
94 if (rextent[n] != shape->data[dim * shape->dim[0].stride])
95 runtime_error ("shape and target do not conform");
97 if (rsize == rstride[n])
101 if (rextent[dim] <= 0)
105 sdim = GFC_DESCRIPTOR_RANK (source);
107 for (n = 0; n < sdim; n++)
110 sstride[n] = source->dim[n].stride;
111 sextent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound;
115 if (ssize == sstride[n])
123 if (pad->dim[0].stride == 0)
124 pad->dim[0].stride = 1;
125 pdim = GFC_DESCRIPTOR_RANK (pad);
127 for (n = 0; n < pdim; n++)
130 pstride[n] = pad->dim[n].stride;
131 pextent[n] = pad->dim[n].ubound + 1 - pad->dim[n].lbound;
134 if (psize == pstride[n])
148 if (rsize != 0 && ssize != 0 && psize != 0)
153 reshape_packed ((char *)ret->data, rsize, (char *)source->data,
154 ssize, pad ? (char *)pad->data : NULL, psize);
158 src = sptr = source->data;
159 rstride0 = rstride[0];
160 sstride0 = sstride[0];
164 /* Select between the source and pad arrays. */
166 /* Advance to the next element. */
171 /* Advance to the next destination element. */
173 while (rcount[n] == rextent[n])
175 /* When we get to the end of a dimension, reset it and increment
176 the next dimension. */
178 /* We could precalculate these products, but this is a less
179 frequently used path so proabably not worth it. */
180 rptr -= rstride[n] * rextent[n];
184 /* Break out of the loop. */
194 /* Advance to the next source element. */
196 while (scount[n] == sextent[n])
198 /* When we get to the end of a dimension, reset it and increment
199 the next dimension. */
201 /* We could precalculate these products, but this is a less
202 frequently used path so proabably not worth it. */
203 src -= sstride[n] * sextent[n];
209 /* Switch to the pad array. */
212 for (dim = 0; dim < pdim; dim++)
214 scount[dim] = pcount[dim];
215 sextent[dim] = pextent[dim];
216 sstride[dim] = pstride[dim];
217 sstride0 = sstride[0];
220 /* We now start again from the beginning of the pad array. */