1 /* Generic implementation of the RESHAPE intrinsic
2 Copyright (C) 2002-2017 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 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 Ligbfortran 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"
29 typedef GFC_ARRAY_DESCRIPTOR(1, index_type
) shape_type
;
30 typedef GFC_ARRAY_DESCRIPTOR(GFC_MAX_DIMENSIONS
, char) parray
;
33 reshape_internal (parray
*ret
, parray
*source
, shape_type
*shape
,
34 parray
*pad
, shape_type
*order
, index_type size
)
36 /* r.* indicates the return array. */
37 index_type rcount
[GFC_MAX_DIMENSIONS
];
38 index_type rextent
[GFC_MAX_DIMENSIONS
];
39 index_type rstride
[GFC_MAX_DIMENSIONS
];
46 /* s.* indicates the source array. */
47 index_type scount
[GFC_MAX_DIMENSIONS
];
48 index_type sextent
[GFC_MAX_DIMENSIONS
];
49 index_type sstride
[GFC_MAX_DIMENSIONS
];
54 /* p.* indicates the pad array. */
55 index_type pcount
[GFC_MAX_DIMENSIONS
];
56 index_type pextent
[GFC_MAX_DIMENSIONS
];
57 index_type pstride
[GFC_MAX_DIMENSIONS
];
65 int sempty
, pempty
, shape_empty
;
66 index_type shape_data
[GFC_MAX_DIMENSIONS
];
68 rdim
= GFC_DESCRIPTOR_EXTENT(shape
,0);
69 if (rdim
!= GFC_DESCRIPTOR_RANK(ret
))
70 runtime_error("rank of return array incorrect in RESHAPE intrinsic");
74 for (n
= 0; n
< rdim
; n
++)
76 shape_data
[n
] = shape
->base_addr
[n
* GFC_DESCRIPTOR_STRIDE(shape
,0)];
77 if (shape_data
[n
] <= 0)
84 if (ret
->base_addr
== NULL
)
86 index_type alloc_size
;
89 for (n
= 0; n
< rdim
; n
++)
93 GFC_DIMENSION_SET(ret
->dim
[n
],0,rex
- 1,rs
);
99 if (unlikely (rs
< 1))
100 alloc_size
= 0; /* xmalloc will allocate 1 byte. */
104 ret
->base_addr
= xmallocarray (alloc_size
, size
);
106 ret
->dtype
= (source
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rdim
;
114 pdim
= GFC_DESCRIPTOR_RANK (pad
);
117 for (n
= 0; n
< pdim
; n
++)
120 pstride
[n
] = GFC_DESCRIPTOR_STRIDE(pad
,n
);
121 pextent
[n
] = GFC_DESCRIPTOR_EXTENT(pad
,n
);
128 if (psize
== pstride
[n
])
133 pptr
= pad
->base_addr
;
143 if (unlikely (compile_options
.bounds_check
))
145 index_type ret_extent
, source_extent
;
148 for (n
= 0; n
< rdim
; n
++)
151 ret_extent
= GFC_DESCRIPTOR_EXTENT(ret
,n
);
152 if (ret_extent
!= shape_data
[n
])
153 runtime_error("Incorrect extent in return value of RESHAPE"
154 " intrinsic in dimension %ld: is %ld,"
155 " should be %ld", (long int) n
+1,
156 (long int) ret_extent
, (long int) shape_data
[n
]);
160 sdim
= GFC_DESCRIPTOR_RANK (source
);
161 for (n
= 0; n
< sdim
; n
++)
164 se
= GFC_DESCRIPTOR_EXTENT(source
,n
);
165 source_extent
*= se
> 0 ? se
: 0;
168 if (rs
> source_extent
&& (!pad
|| pempty
))
169 runtime_error("Incorrect size in SOURCE argument to RESHAPE"
170 " intrinsic: is %ld, should be %ld",
171 (long int) source_extent
, (long int) rs
);
175 int seen
[GFC_MAX_DIMENSIONS
];
178 for (n
= 0; n
< rdim
; n
++)
181 for (n
= 0; n
< rdim
; n
++)
183 v
= order
->base_addr
[n
* GFC_DESCRIPTOR_STRIDE(order
,0)] - 1;
185 if (v
< 0 || v
>= rdim
)
186 runtime_error("Value %ld out of range in ORDER argument"
187 " to RESHAPE intrinsic", (long int) v
+ 1);
190 runtime_error("Duplicate value %ld in ORDER argument to"
191 " RESHAPE intrinsic", (long int) v
+ 1);
199 for (n
= 0; n
< rdim
; n
++)
202 dim
= order
->base_addr
[n
* GFC_DESCRIPTOR_STRIDE(order
,0)] - 1;
207 rstride
[n
] = GFC_DESCRIPTOR_STRIDE(ret
,dim
);
208 rextent
[n
] = GFC_DESCRIPTOR_EXTENT(ret
,dim
);
210 if (rextent
[n
] != shape_data
[dim
])
211 runtime_error ("shape and target do not conform");
213 if (rsize
== rstride
[n
])
221 sdim
= GFC_DESCRIPTOR_RANK (source
);
224 for (n
= 0; n
< sdim
; n
++)
227 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(source
,n
);
228 sextent
[n
] = GFC_DESCRIPTOR_EXTENT(source
,n
);
235 if (ssize
== sstride
[n
])
241 if (rsize
!= 0 && ssize
!= 0 && psize
!= 0)
246 reshape_packed (ret
->base_addr
, rsize
, source
->base_addr
, ssize
,
247 pad
? pad
->base_addr
: NULL
, psize
);
250 rptr
= ret
->base_addr
;
251 src
= sptr
= source
->base_addr
;
252 rstride0
= rstride
[0] * size
;
253 sstride0
= sstride
[0] * size
;
255 if (sempty
&& pempty
)
260 /* Pretend we are using the pad array the first time around, too. */
264 for (dim
= 0; dim
< pdim
; dim
++)
266 scount
[dim
] = pcount
[dim
];
267 sextent
[dim
] = pextent
[dim
];
268 sstride
[dim
] = pstride
[dim
];
269 sstride0
= pstride
[0] * size
;
275 /* Select between the source and pad arrays. */
276 memcpy(rptr
, src
, size
);
277 /* Advance to the next element. */
283 /* Advance to the next destination element. */
285 while (rcount
[n
] == rextent
[n
])
287 /* When we get to the end of a dimension, reset it and increment
288 the next dimension. */
290 /* We could precalculate these products, but this is a less
291 frequently used path so probably not worth it. */
292 rptr
-= rstride
[n
] * rextent
[n
] * size
;
296 /* Break out of the loop. */
303 rptr
+= rstride
[n
] * size
;
307 /* Advance to the next source element. */
309 while (scount
[n
] == sextent
[n
])
311 /* When we get to the end of a dimension, reset it and increment
312 the next dimension. */
314 /* We could precalculate these products, but this is a less
315 frequently used path so probably not worth it. */
316 src
-= sstride
[n
] * sextent
[n
] * size
;
322 /* Switch to the pad array. */
325 for (dim
= 0; dim
< pdim
; dim
++)
327 scount
[dim
] = pcount
[dim
];
328 sextent
[dim
] = pextent
[dim
];
329 sstride
[dim
] = pstride
[dim
];
330 sstride0
= sstride
[0] * size
;
333 /* We now start again from the beginning of the pad array. */
340 src
+= sstride
[n
] * size
;
346 extern void reshape (parray
*, parray
*, shape_type
*, parray
*, shape_type
*);
347 export_proto(reshape
);
350 reshape (parray
*ret
, parray
*source
, shape_type
*shape
, parray
*pad
,
353 reshape_internal (ret
, source
, shape
, pad
, order
,
354 GFC_DESCRIPTOR_SIZE (source
));
358 extern void reshape_char (parray
*, gfc_charlen_type
, parray
*, shape_type
*,
359 parray
*, shape_type
*, gfc_charlen_type
,
361 export_proto(reshape_char
);
364 reshape_char (parray
*ret
, gfc_charlen_type ret_length
__attribute__((unused
)),
365 parray
*source
, shape_type
*shape
, parray
*pad
,
366 shape_type
*order
, gfc_charlen_type source_length
,
367 gfc_charlen_type pad_length
__attribute__((unused
)))
369 reshape_internal (ret
, source
, shape
, pad
, order
, source_length
);
373 extern void reshape_char4 (parray
*, gfc_charlen_type
, parray
*, shape_type
*,
374 parray
*, shape_type
*, gfc_charlen_type
,
376 export_proto(reshape_char4
);
379 reshape_char4 (parray
*ret
, gfc_charlen_type ret_length
__attribute__((unused
)),
380 parray
*source
, shape_type
*shape
, parray
*pad
,
381 shape_type
*order
, gfc_charlen_type source_length
,
382 gfc_charlen_type pad_length
__attribute__((unused
)))
384 reshape_internal (ret
, source
, shape
, pad
, order
,
385 source_length
* sizeof (gfc_char4_t
));