1 `/* Implementation of the RESHAPE intrinsic
2 Copyright 2002, 2006, 2007, 2009 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 3 of the License, or (at your option) any later version.
12 Libgfortran 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 General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
32 `#if defined (HAVE_'rtype_name`)
34 typedef GFC_ARRAY_DESCRIPTOR(1, 'index_type`) 'shape_type`;'
36 dnl For integer routines, only the kind (ie size) is used to name the
37 dnl function. The same function will be used for integer and logical
38 dnl arrays of the same kind.
40 `extern void reshape_'rtype_ccode` ('rtype` * const restrict,
41 'rtype` * const restrict,
42 'shape_type` * const restrict,
43 'rtype` * const restrict,
44 'shape_type` * const restrict);
45 export_proto(reshape_'rtype_ccode`);
48 reshape_'rtype_ccode` ('rtype` * const restrict ret,
49 'rtype` * const restrict source,
50 'shape_type` * const restrict shape,
51 'rtype` * const restrict pad,
52 'shape_type` * const restrict order)
54 /* r.* indicates the return array. */
55 index_type rcount[GFC_MAX_DIMENSIONS];
56 index_type rextent[GFC_MAX_DIMENSIONS];
57 index_type rstride[GFC_MAX_DIMENSIONS];
64 /* s.* indicates the source array. */
65 index_type scount[GFC_MAX_DIMENSIONS];
66 index_type sextent[GFC_MAX_DIMENSIONS];
67 index_type sstride[GFC_MAX_DIMENSIONS];
71 const 'rtype_name` *sptr;
72 /* p.* indicates the pad array. */
73 index_type pcount[GFC_MAX_DIMENSIONS];
74 index_type pextent[GFC_MAX_DIMENSIONS];
75 index_type pstride[GFC_MAX_DIMENSIONS];
78 const 'rtype_name` *pptr;
80 const 'rtype_name` *src;
83 int sempty, pempty, shape_empty;
84 index_type shape_data[GFC_MAX_DIMENSIONS];
86 rdim = GFC_DESCRIPTOR_EXTENT(shape,0);
87 if (rdim != GFC_DESCRIPTOR_RANK(ret))
88 runtime_error("rank of return array incorrect in RESHAPE intrinsic");
92 for (n = 0; n < rdim; n++)
94 shape_data[n] = shape->data[n * GFC_DESCRIPTOR_STRIDE(shape,0)];
95 if (shape_data[n] <= 0)
102 if (ret->data == NULL)
104 index_type alloc_size;
107 for (n = 0; n < rdim; n++)
111 GFC_DIMENSION_SET(ret->dim[n], 0, rex - 1, rs);
117 if (unlikely (rs < 1))
120 alloc_size = rs * sizeof ('rtype_name`);
122 ret->data = internal_malloc_size (alloc_size);
123 ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim;
131 pdim = GFC_DESCRIPTOR_RANK (pad);
134 for (n = 0; n < pdim; n++)
137 pstride[n] = GFC_DESCRIPTOR_STRIDE(pad,n);
138 pextent[n] = GFC_DESCRIPTOR_EXTENT(pad,n);
145 if (psize == pstride[n])
160 if (unlikely (compile_options.bounds_check))
162 index_type ret_extent, source_extent;
165 for (n = 0; n < rdim; n++)
168 ret_extent = GFC_DESCRIPTOR_EXTENT(ret,n);
169 if (ret_extent != shape_data[n])
170 runtime_error("Incorrect extent in return value of RESHAPE"
171 " intrinsic in dimension %ld: is %ld,"
172 " should be %ld", (long int) n+1,
173 (long int) ret_extent, (long int) shape_data[n]);
177 sdim = GFC_DESCRIPTOR_RANK (source);
178 for (n = 0; n < sdim; n++)
181 se = GFC_DESCRIPTOR_EXTENT(source,n);
182 source_extent *= se > 0 ? se : 0;
185 if (rs > source_extent && (!pad || pempty))
186 runtime_error("Incorrect size in SOURCE argument to RESHAPE"
187 " intrinsic: is %ld, should be %ld",
188 (long int) source_extent, (long int) rs);
192 int seen[GFC_MAX_DIMENSIONS];
195 for (n = 0; n < rdim; n++)
198 for (n = 0; n < rdim; n++)
200 v = order->data[n * GFC_DESCRIPTOR_STRIDE(order,0)] - 1;
202 if (v < 0 || v >= rdim)
203 runtime_error("Value %ld out of range in ORDER argument"
204 " to RESHAPE intrinsic", (long int) v + 1);
207 runtime_error("Duplicate value %ld in ORDER argument to"
208 " RESHAPE intrinsic", (long int) v + 1);
216 for (n = 0; n < rdim; n++)
219 dim = order->data[n * GFC_DESCRIPTOR_STRIDE(order,0)] - 1;
224 rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,dim);
225 rextent[n] = GFC_DESCRIPTOR_EXTENT(ret,dim);
229 if (rextent[n] != shape_data[dim])
230 runtime_error ("shape and target do not conform");
232 if (rsize == rstride[n])
240 sdim = GFC_DESCRIPTOR_RANK (source);
243 for (n = 0; n < sdim; n++)
246 sstride[n] = GFC_DESCRIPTOR_STRIDE(source,n);
247 sextent[n] = GFC_DESCRIPTOR_EXTENT(source,n);
254 if (ssize == sstride[n])
260 if (rsize != 0 && ssize != 0 && psize != 0)
262 rsize *= sizeof ('rtype_name`);
263 ssize *= sizeof ('rtype_name`);
264 psize *= sizeof ('rtype_name`);
265 reshape_packed ((char *)ret->data, rsize, (char *)source->data,
266 ssize, pad ? (char *)pad->data : NULL, psize);
270 src = sptr = source->data;
271 rstride0 = rstride[0];
272 sstride0 = sstride[0];
274 if (sempty && pempty)
279 /* Pretend we are using the pad array the first time around, too. */
283 for (dim = 0; dim < pdim; dim++)
285 scount[dim] = pcount[dim];
286 sextent[dim] = pextent[dim];
287 sstride[dim] = pstride[dim];
288 sstride0 = pstride[0];
294 /* Select between the source and pad arrays. */
296 /* Advance to the next element. */
302 /* Advance to the next destination element. */
304 while (rcount[n] == rextent[n])
306 /* When we get to the end of a dimension, reset it and increment
307 the next dimension. */
309 /* We could precalculate these products, but this is a less
310 frequently used path so probably not worth it. */
311 rptr -= rstride[n] * rextent[n];
315 /* Break out of the loop. */
325 /* Advance to the next source element. */
327 while (scount[n] == sextent[n])
329 /* When we get to the end of a dimension, reset it and increment
330 the next dimension. */
332 /* We could precalculate these products, but this is a less
333 frequently used path so probably not worth it. */
334 src -= sstride[n] * sextent[n];
340 /* Switch to the pad array. */
343 for (dim = 0; dim < pdim; dim++)
345 scount[dim] = pcount[dim];
346 sextent[dim] = pextent[dim];
347 sstride[dim] = pstride[dim];
348 sstride0 = sstride[0];
351 /* We now start again from the beginning of the pad array. */