Convert diagnostics to use quoting flag q 4/n
[official-gcc.git] / libgfortran / intrinsics / reshape_generic.c
blobca6f6aacd0063d7c9788e73e3c3051a0ab0f1bac
1 /* Generic implementation of the RESHAPE intrinsic
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. */
22 #include "config.h"
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
26 #include "libgfortran.h"
28 typedef GFC_ARRAY_DESCRIPTOR(1, index_type) shape_type;
29 typedef GFC_ARRAY_DESCRIPTOR(GFC_MAX_DIMENSIONS, char) parray;
32 /* The shape parameter is ignored. We can currently deduce the shape from the
33 return array. */
35 void
36 __reshape (parray * ret, parray * source, shape_type * shape,
37 parray * pad, shape_type * order)
39 /* r.* indicates the return array. */
40 index_type rcount[GFC_MAX_DIMENSIONS - 1];
41 index_type rextent[GFC_MAX_DIMENSIONS - 1];
42 index_type rstride[GFC_MAX_DIMENSIONS - 1];
43 index_type rstride0;
44 index_type rdim;
45 index_type rsize;
46 char *rptr;
47 /* s.* indicates the source array. */
48 index_type scount[GFC_MAX_DIMENSIONS - 1];
49 index_type sextent[GFC_MAX_DIMENSIONS - 1];
50 index_type sstride[GFC_MAX_DIMENSIONS - 1];
51 index_type sstride0;
52 index_type sdim;
53 index_type ssize;
54 const char *sptr;
55 /* p.* indicates the pad array. */
56 index_type pcount[GFC_MAX_DIMENSIONS - 1];
57 index_type pextent[GFC_MAX_DIMENSIONS - 1];
58 index_type pstride[GFC_MAX_DIMENSIONS - 1];
59 index_type pdim;
60 index_type psize;
61 const char *pptr;
63 const char *src;
64 int n;
65 int dim;
66 int size;
68 size = GFC_DESCRIPTOR_SIZE (ret);
69 if (ret->dim[0].stride == 0)
70 ret->dim[0].stride = 1;
71 if (source->dim[0].stride == 0)
72 source->dim[0].stride = 1;
73 if (shape->dim[0].stride == 0)
74 shape->dim[0].stride = 1;
75 if (pad && pad->dim[0].stride == 0)
76 pad->dim[0].stride = 1;
77 if (order && order->dim[0].stride == 0)
78 order->dim[0].stride = 1;
80 rdim = GFC_DESCRIPTOR_RANK (ret);
81 rsize = 1;
82 for (n = 0; n < rdim; n++)
84 if (order)
85 dim = order->data[n * order->dim[0].stride] - 1;
86 else
87 dim = n;
89 rcount[n] = 0;
90 rstride[n] = ret->dim[dim].stride;
91 rextent[n] = ret->dim[dim].ubound + 1 - ret->dim[dim].lbound;
93 if (rextent[n] != shape->data[dim * shape->dim[0].stride])
94 runtime_error ("shape and target do not conform");
96 if (rsize == rstride[n])
97 rsize *= rextent[n];
98 else
99 rsize = 0;
100 if (rextent[dim] <= 0)
101 return;
104 sdim = GFC_DESCRIPTOR_RANK (source);
105 ssize = 1;
106 for (n = 0; n < sdim; n++)
108 scount[n] = 0;
109 sstride[n] = source->dim[n].stride;
110 sextent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound;
111 if (sextent[n] <= 0)
112 abort ();
114 if (rsize == sstride[n])
115 ssize *= sextent[n];
116 else
117 ssize = 0;
120 if (pad)
122 if (pad->dim[0].stride == 0)
123 pad->dim[0].stride = 1;
124 pdim = GFC_DESCRIPTOR_RANK (pad);
125 psize = 1;
126 for (n = 0; n < pdim; n++)
128 pcount[n] = 0;
129 pstride[n] = pad->dim[n].stride;
130 pextent[n] = pad->dim[n].ubound + 1 - pad->dim[n].lbound;
131 if (pextent[n] <= 0)
132 abort ();
133 if (psize == pstride[n])
134 psize *= pextent[n];
135 else
136 rsize = 0;
138 pptr = pad->data;
140 else
142 pdim = 0;
143 psize = 1;
144 pptr = NULL;
147 if (rsize != 0 && ssize != 0 && psize != 0)
149 rsize *= size;
150 ssize *= size;
151 psize *= size;
152 reshape_packed (ret->data, rsize, source->data, ssize,
153 pad ? pad->data : NULL, psize);
154 return;
156 rptr = ret->data;
157 src = sptr = source->data;
158 rstride0 = rstride[0] * size;
159 sstride0 = sstride[0] * size;
161 while (rptr)
163 /* Select between the source and pad arrays. */
164 memcpy(rptr, src, size);
165 /* Advance to the next element. */
166 rptr += rstride0;
167 src += sstride0;
168 rcount[0]++;
169 scount[0]++;
170 /* Advance to the next destination element. */
171 n = 0;
172 while (rcount[n] == rextent[n])
174 /* When we get to the end of a dimension, reset it and increment
175 the next dimension. */
176 rcount[n] = 0;
177 /* We could precalculate these products, but this is a less
178 frequently used path so proabably not worth it. */
179 rptr -= rstride[n] * rextent[n] * size;
180 n++;
181 if (n == rdim)
183 /* Break out of the loop. */
184 rptr = NULL;
185 break;
187 else
189 rcount[n]++;
190 rptr += rstride[n] * size;
193 /* Advance to the next source element. */
194 n = 0;
195 while (scount[n] == sextent[n])
197 /* When we get to the end of a dimension, reset it and increment
198 the next dimension. */
199 scount[n] = 0;
200 /* We could precalculate these products, but this is a less
201 frequently used path so proabably not worth it. */
202 src -= sstride[n] * sextent[n] * size;
203 n++;
204 if (n == sdim)
206 if (sptr && pad)
208 /* Switch to the pad array. */
209 sptr = NULL;
210 sdim = pdim;
211 for (dim = 0; dim < pdim; dim++)
213 scount[dim] = pcount[dim];
214 sextent[dim] = pextent[dim];
215 sstride[dim] = pstride[dim];
216 sstride0 = sstride[0] * size;
219 /* We now start again from the beginning of the pad array. */
220 src = pptr;
221 break;
223 else
225 scount[n]++;
226 sptr += sstride[n] * size;