]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/intrinsics/reshape_generic.c
re PR libfortran/38234 (In Reshape, SOURCE can be bigger than result)
[thirdparty/gcc.git] / libgfortran / intrinsics / reshape_generic.c
1 /* Generic implementation of the RESHAPE intrinsic
2 Copyright 2002, 2006, 2007 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
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.
11
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.)
20
21 Ligbfortran 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.
25
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., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
30
31 #include "libgfortran.h"
32 #include <stdlib.h>
33 #include <string.h>
34 #include <assert.h>
35
36 typedef GFC_ARRAY_DESCRIPTOR(1, index_type) shape_type;
37 typedef GFC_ARRAY_DESCRIPTOR(GFC_MAX_DIMENSIONS, char) parray;
38
39 static void
40 reshape_internal (parray *ret, parray *source, shape_type *shape,
41 parray *pad, shape_type *order, index_type size)
42 {
43 /* r.* indicates the return array. */
44 index_type rcount[GFC_MAX_DIMENSIONS];
45 index_type rextent[GFC_MAX_DIMENSIONS];
46 index_type rstride[GFC_MAX_DIMENSIONS];
47 index_type rstride0;
48 index_type rdim;
49 index_type rsize;
50 index_type rs;
51 index_type rex;
52 char * restrict rptr;
53 /* s.* indicates the source array. */
54 index_type scount[GFC_MAX_DIMENSIONS];
55 index_type sextent[GFC_MAX_DIMENSIONS];
56 index_type sstride[GFC_MAX_DIMENSIONS];
57 index_type sstride0;
58 index_type sdim;
59 index_type ssize;
60 const char *sptr;
61 /* p.* indicates the pad array. */
62 index_type pcount[GFC_MAX_DIMENSIONS];
63 index_type pextent[GFC_MAX_DIMENSIONS];
64 index_type pstride[GFC_MAX_DIMENSIONS];
65 index_type pdim;
66 index_type psize;
67 const char *pptr;
68
69 const char *src;
70 int n;
71 int dim;
72 int sempty, pempty, shape_empty;
73 index_type shape_data[GFC_MAX_DIMENSIONS];
74
75 rdim = shape->dim[0].ubound - shape->dim[0].lbound + 1;
76 if (rdim != GFC_DESCRIPTOR_RANK(ret))
77 runtime_error("rank of return array incorrect in RESHAPE intrinsic");
78
79 shape_empty = 0;
80
81 for (n = 0; n < rdim; n++)
82 {
83 shape_data[n] = shape->data[n * shape->dim[0].stride];
84 if (shape_data[n] <= 0)
85 {
86 shape_data[n] = 0;
87 shape_empty = 1;
88 }
89 }
90
91 if (ret->data == NULL)
92 {
93 rdim = shape->dim[0].ubound - shape->dim[0].lbound + 1;
94 rs = 1;
95 for (n = 0; n < rdim; n++)
96 {
97 ret->dim[n].lbound = 0;
98 rex = shape_data[n];
99 ret->dim[n].ubound = rex - 1;
100 ret->dim[n].stride = rs;
101 rs *= rex;
102 }
103 ret->offset = 0;
104 ret->data = internal_malloc_size ( rs * size );
105 ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim;
106 }
107
108 if (shape_empty)
109 return;
110
111 if (pad)
112 {
113 pdim = GFC_DESCRIPTOR_RANK (pad);
114 psize = 1;
115 pempty = 0;
116 for (n = 0; n < pdim; n++)
117 {
118 pcount[n] = 0;
119 pstride[n] = pad->dim[n].stride;
120 pextent[n] = pad->dim[n].ubound + 1 - pad->dim[n].lbound;
121 if (pextent[n] <= 0)
122 {
123 pempty = 1;
124 pextent[n] = 0;
125 }
126
127 if (psize == pstride[n])
128 psize *= pextent[n];
129 else
130 psize = 0;
131 }
132 pptr = pad->data;
133 }
134 else
135 {
136 pdim = 0;
137 psize = 1;
138 pempty = 1;
139 pptr = NULL;
140 }
141
142 if (unlikely (compile_options.bounds_check))
143 {
144 index_type ret_extent, source_extent;
145
146 rs = 1;
147 for (n = 0; n < rdim; n++)
148 {
149 rs *= shape_data[n];
150 ret_extent = ret->dim[n].ubound + 1 - ret->dim[n].lbound;
151 if (ret_extent != shape_data[n])
152 runtime_error("Incorrect extent in return value of RESHAPE"
153 " intrinsic in dimension %ld: is %ld,"
154 " should be %ld", (long int) n+1,
155 (long int) ret_extent, (long int) shape_data[n]);
156 }
157
158 source_extent = 1;
159 sdim = GFC_DESCRIPTOR_RANK (source);
160 for (n = 0; n < sdim; n++)
161 {
162 index_type se;
163 se = source->dim[n].ubound + 1 - source->dim[0].lbound;
164 source_extent *= se > 0 ? se : 0;
165 }
166
167 if (rs > source_extent && (!pad || pempty))
168 runtime_error("Incorrect size in SOURCE argument to RESHAPE"
169 " intrinsic: is %ld, should be %ld",
170 (long int) source_extent, (long int) rs);
171
172 if (order)
173 {
174 int seen[GFC_MAX_DIMENSIONS];
175 index_type v;
176
177 for (n = 0; n < rdim; n++)
178 seen[n] = 0;
179
180 for (n = 0; n < rdim; n++)
181 {
182 v = order->data[n * order->dim[0].stride] - 1;
183
184 if (v < 0 || v >= rdim)
185 runtime_error("Value %ld out of range in ORDER argument"
186 " to RESHAPE intrinsic", (long int) v + 1);
187
188 if (seen[v] != 0)
189 runtime_error("Duplicate value %ld in ORDER argument to"
190 " RESHAPE intrinsic", (long int) v + 1);
191
192 seen[v] = 1;
193 }
194 }
195 }
196
197 rsize = 1;
198 for (n = 0; n < rdim; n++)
199 {
200 if (order)
201 dim = order->data[n * order->dim[0].stride] - 1;
202 else
203 dim = n;
204
205 rcount[n] = 0;
206 rstride[n] = ret->dim[dim].stride;
207 rextent[n] = ret->dim[dim].ubound + 1 - ret->dim[dim].lbound;
208
209 if (rextent[n] != shape_data[dim])
210 runtime_error ("shape and target do not conform");
211
212 if (rsize == rstride[n])
213 rsize *= rextent[n];
214 else
215 rsize = 0;
216 if (rextent[n] <= 0)
217 return;
218 }
219
220 sdim = GFC_DESCRIPTOR_RANK (source);
221 ssize = 1;
222 sempty = 0;
223 for (n = 0; n < sdim; n++)
224 {
225 scount[n] = 0;
226 sstride[n] = source->dim[n].stride;
227 sextent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound;
228 if (sextent[n] <= 0)
229 {
230 sempty = 1;
231 sextent[n] = 0;
232 }
233
234 if (ssize == sstride[n])
235 ssize *= sextent[n];
236 else
237 ssize = 0;
238 }
239
240 if (rsize != 0 && ssize != 0 && psize != 0)
241 {
242 rsize *= size;
243 ssize *= size;
244 psize *= size;
245 reshape_packed (ret->data, rsize, source->data, ssize,
246 pad ? pad->data : NULL, psize);
247 return;
248 }
249 rptr = ret->data;
250 src = sptr = source->data;
251 rstride0 = rstride[0] * size;
252 sstride0 = sstride[0] * size;
253
254 if (sempty && pempty)
255 abort ();
256
257 if (sempty)
258 {
259 /* Pretend we are using the pad array the first time around, too. */
260 src = pptr;
261 sptr = pptr;
262 sdim = pdim;
263 for (dim = 0; dim < pdim; dim++)
264 {
265 scount[dim] = pcount[dim];
266 sextent[dim] = pextent[dim];
267 sstride[dim] = pstride[dim];
268 sstride0 = pstride[0] * size;
269 }
270 }
271
272 while (rptr)
273 {
274 /* Select between the source and pad arrays. */
275 memcpy(rptr, src, size);
276 /* Advance to the next element. */
277 rptr += rstride0;
278 src += sstride0;
279 rcount[0]++;
280 scount[0]++;
281
282 /* Advance to the next destination element. */
283 n = 0;
284 while (rcount[n] == rextent[n])
285 {
286 /* When we get to the end of a dimension, reset it and increment
287 the next dimension. */
288 rcount[n] = 0;
289 /* We could precalculate these products, but this is a less
290 frequently used path so probably not worth it. */
291 rptr -= rstride[n] * rextent[n] * size;
292 n++;
293 if (n == rdim)
294 {
295 /* Break out of the loop. */
296 rptr = NULL;
297 break;
298 }
299 else
300 {
301 rcount[n]++;
302 rptr += rstride[n] * size;
303 }
304 }
305
306 /* Advance to the next source element. */
307 n = 0;
308 while (scount[n] == sextent[n])
309 {
310 /* When we get to the end of a dimension, reset it and increment
311 the next dimension. */
312 scount[n] = 0;
313 /* We could precalculate these products, but this is a less
314 frequently used path so probably not worth it. */
315 src -= sstride[n] * sextent[n] * size;
316 n++;
317 if (n == sdim)
318 {
319 if (sptr && pad)
320 {
321 /* Switch to the pad array. */
322 sptr = NULL;
323 sdim = pdim;
324 for (dim = 0; dim < pdim; dim++)
325 {
326 scount[dim] = pcount[dim];
327 sextent[dim] = pextent[dim];
328 sstride[dim] = pstride[dim];
329 sstride0 = sstride[0] * size;
330 }
331 }
332 /* We now start again from the beginning of the pad array. */
333 src = pptr;
334 break;
335 }
336 else
337 {
338 scount[n]++;
339 src += sstride[n] * size;
340 }
341 }
342 }
343 }
344
345 extern void reshape (parray *, parray *, shape_type *, parray *, shape_type *);
346 export_proto(reshape);
347
348 void
349 reshape (parray *ret, parray *source, shape_type *shape, parray *pad,
350 shape_type *order)
351 {
352 reshape_internal (ret, source, shape, pad, order,
353 GFC_DESCRIPTOR_SIZE (source));
354 }
355
356
357 extern void reshape_char (parray *, gfc_charlen_type, parray *, shape_type *,
358 parray *, shape_type *, gfc_charlen_type,
359 gfc_charlen_type);
360 export_proto(reshape_char);
361
362 void
363 reshape_char (parray *ret, gfc_charlen_type ret_length __attribute__((unused)),
364 parray *source, shape_type *shape, parray *pad,
365 shape_type *order, gfc_charlen_type source_length,
366 gfc_charlen_type pad_length __attribute__((unused)))
367 {
368 reshape_internal (ret, source, shape, pad, order, source_length);
369 }
370
371
372 extern void reshape_char4 (parray *, gfc_charlen_type, parray *, shape_type *,
373 parray *, shape_type *, gfc_charlen_type,
374 gfc_charlen_type);
375 export_proto(reshape_char4);
376
377 void
378 reshape_char4 (parray *ret, gfc_charlen_type ret_length __attribute__((unused)),
379 parray *source, shape_type *shape, parray *pad,
380 shape_type *order, gfc_charlen_type source_length,
381 gfc_charlen_type pad_length __attribute__((unused)))
382 {
383 reshape_internal (ret, source, shape, pad, order,
384 source_length * sizeof (gfc_char4_t));
385 }