Merge reload-branch up to revision 101000
[official-gcc.git] / libgfortran / generated / reshape_i4.c
blob465ca24381bf639a341f73a67ed2bf0d0f4924e0
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
19 executable.)
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. */
31 #include "config.h"
32 #include <stdlib.h>
33 #include <assert.h>
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
39 return array. */
41 extern void reshape_4 (gfc_array_i4 *, gfc_array_i4 *, shape_type *,
42 gfc_array_i4 *, shape_type *);
43 export_proto(reshape_4);
45 void
46 reshape_4 (gfc_array_i4 * ret, gfc_array_i4 * source, shape_type * shape,
47 gfc_array_i4 * 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];
53 index_type rstride0;
54 index_type rdim;
55 index_type rsize;
56 index_type rs;
57 index_type rex;
58 GFC_INTEGER_4 *rptr;
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];
63 index_type sstride0;
64 index_type sdim;
65 index_type ssize;
66 const GFC_INTEGER_4 *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];
71 index_type pdim;
72 index_type psize;
73 const GFC_INTEGER_4 *pptr;
75 const GFC_INTEGER_4 *src;
76 int n;
77 int dim;
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;
91 rs = 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;
98 rs *= rex;
100 ret->base = 0;
101 ret->data = internal_malloc_size ( rs * sizeof (GFC_INTEGER_4));
102 ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim;
104 else
106 rdim = GFC_DESCRIPTOR_RANK (ret);
107 if (ret->dim[0].stride == 0)
108 ret->dim[0].stride = 1;
111 rsize = 1;
112 for (n = 0; n < rdim; n++)
114 if (order)
115 dim = order->data[n * order->dim[0].stride] - 1;
116 else
117 dim = n;
119 rcount[n] = 0;
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])
127 rsize *= rextent[n];
128 else
129 rsize = 0;
130 if (rextent[n] <= 0)
131 return;
134 sdim = GFC_DESCRIPTOR_RANK (source);
135 ssize = 1;
136 for (n = 0; n < sdim; n++)
138 scount[n] = 0;
139 sstride[n] = source->dim[n].stride;
140 sextent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound;
141 if (sextent[n] <= 0)
142 abort ();
144 if (ssize == sstride[n])
145 ssize *= sextent[n];
146 else
147 ssize = 0;
150 if (pad)
152 pdim = GFC_DESCRIPTOR_RANK (pad);
153 psize = 1;
154 for (n = 0; n < pdim; n++)
156 pcount[n] = 0;
157 pstride[n] = pad->dim[n].stride;
158 pextent[n] = pad->dim[n].ubound + 1 - pad->dim[n].lbound;
159 if (pextent[n] <= 0)
160 abort ();
161 if (psize == pstride[n])
162 psize *= pextent[n];
163 else
164 psize = 0;
166 pptr = pad->data;
168 else
170 pdim = 0;
171 psize = 1;
172 pptr = NULL;
175 if (rsize != 0 && ssize != 0 && psize != 0)
177 rsize *= sizeof (GFC_INTEGER_4);
178 ssize *= sizeof (GFC_INTEGER_4);
179 psize *= sizeof (GFC_INTEGER_4);
180 reshape_packed ((char *)ret->data, rsize, (char *)source->data,
181 ssize, pad ? (char *)pad->data : NULL, psize);
182 return;
184 rptr = ret->data;
185 src = sptr = source->data;
186 rstride0 = rstride[0];
187 sstride0 = sstride[0];
189 while (rptr)
191 /* Select between the source and pad arrays. */
192 *rptr = *src;
193 /* Advance to the next element. */
194 rptr += rstride0;
195 src += sstride0;
196 rcount[0]++;
197 scount[0]++;
198 /* Advance to the next destination element. */
199 n = 0;
200 while (rcount[n] == rextent[n])
202 /* When we get to the end of a dimension, reset it and increment
203 the next dimension. */
204 rcount[n] = 0;
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];
208 n++;
209 if (n == rdim)
211 /* Break out of the loop. */
212 rptr = NULL;
213 break;
215 else
217 rcount[n]++;
218 rptr += rstride[n];
221 /* Advance to the next source element. */
222 n = 0;
223 while (scount[n] == sextent[n])
225 /* When we get to the end of a dimension, reset it and increment
226 the next dimension. */
227 scount[n] = 0;
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];
231 n++;
232 if (n == sdim)
234 if (sptr && pad)
236 /* Switch to the pad array. */
237 sptr = NULL;
238 sdim = pdim;
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. */
248 src = pptr;
249 break;
251 else
253 scount[n]++;
254 src += sstride[n];