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., 59 Temple Place - Suite 330,
29 Boston, MA 02111-1307, USA. */
34 #include "libgfortran.h"'
37 typedef GFC_ARRAY_DESCRIPTOR(1, index_type) shape_type;
39 /* The shape parameter is ignored. We can currently deduce the shape from the
41 dnl Only the kind (ie size) is used to name the function.
43 extern void reshape_`'rtype_kind (rtype *, rtype *, shape_type *,
44 rtype *, shape_type *);
45 export_proto(reshape_`'rtype_kind);
48 reshape_`'rtype_kind (rtype * ret, rtype * source, shape_type * shape,
49 rtype * pad, shape_type * order)
51 /* r.* indicates the return array. */
52 index_type rcount[GFC_MAX_DIMENSIONS];
53 index_type rextent[GFC_MAX_DIMENSIONS];
54 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 rtype_name *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 rtype_name *pptr;
75 const rtype_name *src;
79 if (ret->dim[0].stride == 0)
80 ret->dim[0].stride = 1;
81 if (source->dim[0].stride == 0)
82 source->dim[0].stride = 1;
83 if (shape->dim[0].stride == 0)
84 shape->dim[0].stride = 1;
85 if (pad && pad->dim[0].stride == 0)
86 pad->dim[0].stride = 1;
87 if (order && order->dim[0].stride == 0)
88 order->dim[0].stride = 1;
90 rdim = GFC_DESCRIPTOR_RANK (ret);
92 for (n = 0; n < rdim; n++)
95 dim = order->data[n * order->dim[0].stride] - 1;
100 rstride[n] = ret->dim[dim].stride;
101 rextent[n] = ret->dim[dim].ubound + 1 - ret->dim[dim].lbound;
103 if (rextent[n] != shape->data[dim * shape->dim[0].stride])
104 runtime_error ("shape and target do not conform");
106 if (rsize == rstride[n])
110 if (rextent[dim] <= 0)
114 sdim = GFC_DESCRIPTOR_RANK (source);
116 for (n = 0; n < sdim; n++)
119 sstride[n] = source->dim[n].stride;
120 sextent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound;
124 if (ssize == sstride[n])
132 if (pad->dim[0].stride == 0)
133 pad->dim[0].stride = 1;
134 pdim = GFC_DESCRIPTOR_RANK (pad);
136 for (n = 0; n < pdim; n++)
139 pstride[n] = pad->dim[n].stride;
140 pextent[n] = pad->dim[n].ubound + 1 - pad->dim[n].lbound;
143 if (psize == pstride[n])
157 if (rsize != 0 && ssize != 0 && psize != 0)
162 reshape_packed ((char *)ret->data, rsize, (char *)source->data,
163 ssize, pad ? (char *)pad->data : NULL, psize);
167 src = sptr = source->data;
168 rstride0 = rstride[0];
169 sstride0 = sstride[0];
173 /* Select between the source and pad arrays. */
175 /* Advance to the next element. */
180 /* Advance to the next destination element. */
182 while (rcount[n] == rextent[n])
184 /* When we get to the end of a dimension, reset it and increment
185 the next dimension. */
187 /* We could precalculate these products, but this is a less
188 frequently used path so proabably not worth it. */
189 rptr -= rstride[n] * rextent[n];
193 /* Break out of the loop. */
203 /* Advance to the next source element. */
205 while (scount[n] == sextent[n])
207 /* When we get to the end of a dimension, reset it and increment
208 the next dimension. */
210 /* We could precalculate these products, but this is a less
211 frequently used path so proabably not worth it. */
212 src -= sstride[n] * sextent[n];
218 /* Switch to the pad array. */
221 for (dim = 0; dim < pdim; dim++)
223 scount[dim] = pcount[dim];
224 sextent[dim] = pextent[dim];
225 sstride[dim] = pstride[dim];
226 sstride0 = sstride[0];
229 /* We now start again from the beginning of the pad array. */