1 /* Implementation of the RESHAPE intrinsic
2 Copyright (C) 2002-2016 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 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"
29 #if defined (HAVE_GFC_REAL_10)
31 typedef GFC_ARRAY_DESCRIPTOR(1, index_type
) shape_type
;
34 extern void reshape_r10 (gfc_array_r10
* const restrict
,
35 gfc_array_r10
* const restrict
,
36 shape_type
* const restrict
,
37 gfc_array_r10
* const restrict
,
38 shape_type
* const restrict
);
39 export_proto(reshape_r10
);
42 reshape_r10 (gfc_array_r10
* const restrict ret
,
43 gfc_array_r10
* const restrict source
,
44 shape_type
* const restrict shape
,
45 gfc_array_r10
* const restrict pad
,
46 shape_type
* const restrict order
)
48 /* r.* indicates the return array. */
49 index_type rcount
[GFC_MAX_DIMENSIONS
];
50 index_type rextent
[GFC_MAX_DIMENSIONS
];
51 index_type rstride
[GFC_MAX_DIMENSIONS
];
58 /* s.* indicates the source array. */
59 index_type scount
[GFC_MAX_DIMENSIONS
];
60 index_type sextent
[GFC_MAX_DIMENSIONS
];
61 index_type sstride
[GFC_MAX_DIMENSIONS
];
65 const GFC_REAL_10
*sptr
;
66 /* p.* indicates the pad array. */
67 index_type pcount
[GFC_MAX_DIMENSIONS
];
68 index_type pextent
[GFC_MAX_DIMENSIONS
];
69 index_type pstride
[GFC_MAX_DIMENSIONS
];
72 const GFC_REAL_10
*pptr
;
74 const GFC_REAL_10
*src
;
77 int sempty
, pempty
, shape_empty
;
78 index_type shape_data
[GFC_MAX_DIMENSIONS
];
80 rdim
= GFC_DESCRIPTOR_EXTENT(shape
,0);
81 if (rdim
!= GFC_DESCRIPTOR_RANK(ret
))
82 runtime_error("rank of return array incorrect in RESHAPE intrinsic");
86 for (n
= 0; n
< rdim
; n
++)
88 shape_data
[n
] = shape
->base_addr
[n
* GFC_DESCRIPTOR_STRIDE(shape
,0)];
89 if (shape_data
[n
] <= 0)
96 if (ret
->base_addr
== NULL
)
98 index_type alloc_size
;
101 for (n
= 0; n
< rdim
; n
++)
105 GFC_DIMENSION_SET(ret
->dim
[n
], 0, rex
- 1, rs
);
111 if (unlikely (rs
< 1))
116 ret
->base_addr
= xmallocarray (alloc_size
, sizeof (GFC_REAL_10
));
117 ret
->dtype
= (source
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rdim
;
125 pdim
= GFC_DESCRIPTOR_RANK (pad
);
128 for (n
= 0; n
< pdim
; n
++)
131 pstride
[n
] = GFC_DESCRIPTOR_STRIDE(pad
,n
);
132 pextent
[n
] = GFC_DESCRIPTOR_EXTENT(pad
,n
);
139 if (psize
== pstride
[n
])
144 pptr
= pad
->base_addr
;
154 if (unlikely (compile_options
.bounds_check
))
156 index_type ret_extent
, source_extent
;
159 for (n
= 0; n
< rdim
; n
++)
162 ret_extent
= GFC_DESCRIPTOR_EXTENT(ret
,n
);
163 if (ret_extent
!= shape_data
[n
])
164 runtime_error("Incorrect extent in return value of RESHAPE"
165 " intrinsic in dimension %ld: is %ld,"
166 " should be %ld", (long int) n
+1,
167 (long int) ret_extent
, (long int) shape_data
[n
]);
171 sdim
= GFC_DESCRIPTOR_RANK (source
);
172 for (n
= 0; n
< sdim
; n
++)
175 se
= GFC_DESCRIPTOR_EXTENT(source
,n
);
176 source_extent
*= se
> 0 ? se
: 0;
179 if (rs
> source_extent
&& (!pad
|| pempty
))
180 runtime_error("Incorrect size in SOURCE argument to RESHAPE"
181 " intrinsic: is %ld, should be %ld",
182 (long int) source_extent
, (long int) rs
);
186 int seen
[GFC_MAX_DIMENSIONS
];
189 for (n
= 0; n
< rdim
; n
++)
192 for (n
= 0; n
< rdim
; n
++)
194 v
= order
->base_addr
[n
* GFC_DESCRIPTOR_STRIDE(order
,0)] - 1;
196 if (v
< 0 || v
>= rdim
)
197 runtime_error("Value %ld out of range in ORDER argument"
198 " to RESHAPE intrinsic", (long int) v
+ 1);
201 runtime_error("Duplicate value %ld in ORDER argument to"
202 " RESHAPE intrinsic", (long int) v
+ 1);
210 for (n
= 0; n
< rdim
; n
++)
213 dim
= order
->base_addr
[n
* GFC_DESCRIPTOR_STRIDE(order
,0)] - 1;
218 rstride
[n
] = GFC_DESCRIPTOR_STRIDE(ret
,dim
);
219 rextent
[n
] = GFC_DESCRIPTOR_EXTENT(ret
,dim
);
223 if (rextent
[n
] != shape_data
[dim
])
224 runtime_error ("shape and target do not conform");
226 if (rsize
== rstride
[n
])
234 sdim
= GFC_DESCRIPTOR_RANK (source
);
237 for (n
= 0; n
< sdim
; n
++)
240 sstride
[n
] = GFC_DESCRIPTOR_STRIDE(source
,n
);
241 sextent
[n
] = GFC_DESCRIPTOR_EXTENT(source
,n
);
248 if (ssize
== sstride
[n
])
254 if (rsize
!= 0 && ssize
!= 0 && psize
!= 0)
256 rsize
*= sizeof (GFC_REAL_10
);
257 ssize
*= sizeof (GFC_REAL_10
);
258 psize
*= sizeof (GFC_REAL_10
);
259 reshape_packed ((char *)ret
->base_addr
, rsize
, (char *)source
->base_addr
,
260 ssize
, pad
? (char *)pad
->base_addr
: NULL
, psize
);
263 rptr
= ret
->base_addr
;
264 src
= sptr
= source
->base_addr
;
265 rstride0
= rstride
[0];
266 sstride0
= sstride
[0];
268 if (sempty
&& pempty
)
273 /* Pretend we are using the pad array the first time around, too. */
277 for (dim
= 0; dim
< pdim
; dim
++)
279 scount
[dim
] = pcount
[dim
];
280 sextent
[dim
] = pextent
[dim
];
281 sstride
[dim
] = pstride
[dim
];
282 sstride0
= pstride
[0];
288 /* Select between the source and pad arrays. */
290 /* Advance to the next element. */
296 /* Advance to the next destination element. */
298 while (rcount
[n
] == rextent
[n
])
300 /* When we get to the end of a dimension, reset it and increment
301 the next dimension. */
303 /* We could precalculate these products, but this is a less
304 frequently used path so probably not worth it. */
305 rptr
-= rstride
[n
] * rextent
[n
];
309 /* Break out of the loop. */
319 /* Advance to the next source element. */
321 while (scount
[n
] == sextent
[n
])
323 /* When we get to the end of a dimension, reset it and increment
324 the next dimension. */
326 /* We could precalculate these products, but this is a less
327 frequently used path so probably not worth it. */
328 src
-= sstride
[n
] * sextent
[n
];
334 /* Switch to the pad array. */
337 for (dim
= 0; dim
< pdim
; dim
++)
339 scount
[dim
] = pcount
[dim
];
340 sextent
[dim
] = pextent
[dim
];
341 sstride
[dim
] = pstride
[dim
];
342 sstride0
= sstride
[0];
345 /* We now start again from the beginning of the pad array. */