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. */
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
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];
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];
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];
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
);
82 for (n
= 0; n
< rdim
; n
++)
85 dim
= order
->data
[n
* order
->dim
[0].stride
] - 1;
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
])
100 if (rextent
[dim
] <= 0)
104 sdim
= GFC_DESCRIPTOR_RANK (source
);
106 for (n
= 0; n
< sdim
; n
++)
109 sstride
[n
] = source
->dim
[n
].stride
;
110 sextent
[n
] = source
->dim
[n
].ubound
+ 1 - source
->dim
[n
].lbound
;
114 if (rsize
== sstride
[n
])
122 if (pad
->dim
[0].stride
== 0)
123 pad
->dim
[0].stride
= 1;
124 pdim
= GFC_DESCRIPTOR_RANK (pad
);
126 for (n
= 0; n
< pdim
; n
++)
129 pstride
[n
] = pad
->dim
[n
].stride
;
130 pextent
[n
] = pad
->dim
[n
].ubound
+ 1 - pad
->dim
[n
].lbound
;
133 if (psize
== pstride
[n
])
147 if (rsize
!= 0 && ssize
!= 0 && psize
!= 0)
152 reshape_packed (ret
->data
, rsize
, source
->data
, ssize
,
153 pad
? pad
->data
: NULL
, psize
);
157 src
= sptr
= source
->data
;
158 rstride0
= rstride
[0] * size
;
159 sstride0
= sstride
[0] * size
;
163 /* Select between the source and pad arrays. */
164 memcpy(rptr
, src
, size
);
165 /* Advance to the next element. */
170 /* Advance to the next destination element. */
172 while (rcount
[n
] == rextent
[n
])
174 /* When we get to the end of a dimension, reset it and increment
175 the next dimension. */
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
;
183 /* Break out of the loop. */
190 rptr
+= rstride
[n
] * size
;
193 /* Advance to the next source element. */
195 while (scount
[n
] == sextent
[n
])
197 /* When we get to the end of a dimension, reset it and increment
198 the next dimension. */
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
;
208 /* Switch to the pad array. */
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. */
226 sptr
+= sstride
[n
] * size
;