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 `__reshape_'rtype_kind (rtype * ret, rtype * source, shape_type * shape,
35 rtype * pad, shape_type * order)
37 /* r.* indicates the return array. */
38 index_type rcount[GFC_MAX_DIMENSIONS - 1];
39 index_type rextent[GFC_MAX_DIMENSIONS - 1];
40 index_type rstride[GFC_MAX_DIMENSIONS - 1];
45 /* s.* indicates the source array. */
46 index_type scount[GFC_MAX_DIMENSIONS - 1];
47 index_type sextent[GFC_MAX_DIMENSIONS - 1];
48 index_type sstride[GFC_MAX_DIMENSIONS - 1];
52 const rtype_name *sptr;
53 /* p.* indicates the pad array. */
54 index_type pcount[GFC_MAX_DIMENSIONS - 1];
55 index_type pextent[GFC_MAX_DIMENSIONS - 1];
56 index_type pstride[GFC_MAX_DIMENSIONS - 1];
59 const rtype_name *pptr;
61 const rtype_name *src;
65 if (ret->dim[0].stride == 0)
66 ret->dim[0].stride = 1;
67 if (source->dim[0].stride == 0)
68 source->dim[0].stride = 1;
69 if (shape->dim[0].stride == 0)
70 shape->dim[0].stride = 1;
71 if (pad && pad->dim[0].stride == 0)
72 pad->dim[0].stride = 1;
73 if (order && order->dim[0].stride == 0)
74 order->dim[0].stride = 1;
76 rdim = GFC_DESCRIPTOR_RANK (ret);
78 for (n = 0; n < rdim; n++)
81 dim = order->data[n * order->dim[0].stride] - 1;
86 rstride[n] = ret->dim[dim].stride;
87 rextent[n] = ret->dim[dim].ubound + 1 - ret->dim[dim].lbound;
89 if (rextent[n] != shape->data[dim * shape->dim[0].stride])
90 runtime_error ("shape and target do not conform");
92 if (rsize == rstride[n])
96 if (rextent[dim] <= 0)
100 sdim = GFC_DESCRIPTOR_RANK (source);
102 for (n = 0; n < sdim; n++)
105 sstride[n] = source->dim[n].stride;
106 sextent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound;
110 if (ssize == sstride[n])
118 if (pad->dim[0].stride == 0)
119 pad->dim[0].stride = 1;
120 pdim = GFC_DESCRIPTOR_RANK (pad);
122 for (n = 0; n < pdim; n++)
125 pstride[n] = pad->dim[n].stride;
126 pextent[n] = pad->dim[n].ubound + 1 - pad->dim[n].lbound;
129 if (psize == pstride[n])
143 if (rsize != 0 && ssize != 0 && psize != 0)
148 reshape_packed ((char *)ret->data, rsize, (char *)source->data,
149 ssize, pad ? (char *)pad->data : NULL, psize);
153 src = sptr = source->data;
154 rstride0 = rstride[0];
155 sstride0 = sstride[0];
159 /* Select between the source and pad arrays. */
161 /* Advance to the next element. */
166 /* Advance to the next destination element. */
168 while (rcount[n] == rextent[n])
170 /* When we get to the end of a dimension, reset it and increment
171 the next dimension. */
173 /* We could precalculate these products, but this is a less
174 frequently used path so proabably not worth it. */
175 rptr -= rstride[n] * rextent[n];
179 /* Break out of the loop. */
189 /* Advance to the next source element. */
191 while (scount[n] == sextent[n])
193 /* When we get to the end of a dimension, reset it and increment
194 the next dimension. */
196 /* We could precalculate these products, but this is a less
197 frequently used path so proabably not worth it. */
198 src -= sstride[n] * sextent[n];
204 /* Switch to the pad array. */
207 for (dim = 0; dim < pdim; dim++)
209 scount[dim] = pcount[dim];
210 sextent[dim] = pextent[dim];
211 sstride[dim] = pstride[dim];
212 sstride0 = sstride[0];
215 /* We now start again from the beginning of the pad array. */