]>
Commit | Line | Data |
---|---|---|
827aef63 | 1 | `/* Implementation of the RESHAPE intrinsic |
f1717362 | 2 | Copyright (C) 2002-2016 Free Software Foundation, Inc. |
4ee9c684 | 3 | Contributed by Paul Brook <paul@nowt.org> |
4 | ||
553877d9 | 5 | This file is part of the GNU Fortran runtime library (libgfortran). |
4ee9c684 | 6 | |
b417ea8c | 7 | Libgfortran is free software; you can redistribute it and/or |
8 | modify it under the terms of the GNU General Public | |
4ee9c684 | 9 | License as published by the Free Software Foundation; either |
6bc9506f | 10 | version 3 of the License, or (at your option) any later version. |
b417ea8c | 11 | |
12 | Libgfortran is distributed in the hope that it will be useful, | |
4ee9c684 | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
b417ea8c | 15 | GNU General Public License for more details. |
4ee9c684 | 16 | |
6bc9506f | 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. | |
20 | ||
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/>. */ | |
4ee9c684 | 25 | |
41f2d5e8 | 26 | #include "libgfortran.h" |
4ee9c684 | 27 | #include <stdlib.h> |
41f2d5e8 | 28 | #include <assert.h>' |
29 | ||
cdafa1f6 | 30 | include(iparm.m4)dnl |
4ee9c684 | 31 | |
0a6b5f6b | 32 | `#if defined (HAVE_'rtype_name`) |
920e54ef | 33 | |
0a6b5f6b | 34 | typedef GFC_ARRAY_DESCRIPTOR(1, 'index_type`) 'shape_type`;' |
4ee9c684 | 35 | |
e7382835 | 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. | |
7b6cb5bd | 39 | |
0a6b5f6b | 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`); | |
7b6cb5bd | 46 | |
4ee9c684 | 47 | void |
0a6b5f6b | 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) | |
4ee9c684 | 53 | { |
54 | /* r.* indicates the return array. */ | |
2be2fbdf | 55 | index_type rcount[GFC_MAX_DIMENSIONS]; |
56 | index_type rextent[GFC_MAX_DIMENSIONS]; | |
57 | index_type rstride[GFC_MAX_DIMENSIONS]; | |
4ee9c684 | 58 | index_type rstride0; |
59 | index_type rdim; | |
60 | index_type rsize; | |
93c57576 | 61 | index_type rs; |
62 | index_type rex; | |
0a6b5f6b | 63 | 'rtype_name` *rptr; |
4ee9c684 | 64 | /* s.* indicates the source array. */ |
2be2fbdf | 65 | index_type scount[GFC_MAX_DIMENSIONS]; |
66 | index_type sextent[GFC_MAX_DIMENSIONS]; | |
67 | index_type sstride[GFC_MAX_DIMENSIONS]; | |
4ee9c684 | 68 | index_type sstride0; |
69 | index_type sdim; | |
70 | index_type ssize; | |
0a6b5f6b | 71 | const 'rtype_name` *sptr; |
4ee9c684 | 72 | /* p.* indicates the pad array. */ |
2be2fbdf | 73 | index_type pcount[GFC_MAX_DIMENSIONS]; |
74 | index_type pextent[GFC_MAX_DIMENSIONS]; | |
75 | index_type pstride[GFC_MAX_DIMENSIONS]; | |
4ee9c684 | 76 | index_type pdim; |
77 | index_type psize; | |
0a6b5f6b | 78 | const 'rtype_name` *pptr; |
4ee9c684 | 79 | |
0a6b5f6b | 80 | const 'rtype_name` *src; |
4ee9c684 | 81 | int n; |
82 | int dim; | |
c808637e | 83 | int sempty, pempty, shape_empty; |
84 | index_type shape_data[GFC_MAX_DIMENSIONS]; | |
85 | ||
827aef63 | 86 | rdim = GFC_DESCRIPTOR_EXTENT(shape,0); |
c808637e | 87 | if (rdim != GFC_DESCRIPTOR_RANK(ret)) |
88 | runtime_error("rank of return array incorrect in RESHAPE intrinsic"); | |
89 | ||
90 | shape_empty = 0; | |
91 | ||
92 | for (n = 0; n < rdim; n++) | |
93 | { | |
553877d9 | 94 | shape_data[n] = shape->base_addr[n * GFC_DESCRIPTOR_STRIDE(shape,0)]; |
c808637e | 95 | if (shape_data[n] <= 0) |
96 | { | |
97 | shape_data[n] = 0; | |
98 | shape_empty = 1; | |
99 | } | |
100 | } | |
4ee9c684 | 101 | |
553877d9 | 102 | if (ret->base_addr == NULL) |
93c57576 | 103 | { |
568408e3 | 104 | index_type alloc_size; |
105 | ||
93c57576 | 106 | rs = 1; |
e7382835 | 107 | for (n = 0; n < rdim; n++) |
93c57576 | 108 | { |
c808637e | 109 | rex = shape_data[n]; |
827aef63 | 110 | |
111 | GFC_DIMENSION_SET(ret->dim[n], 0, rex - 1, rs); | |
112 | ||
93c57576 | 113 | rs *= rex; |
114 | } | |
93830de1 | 115 | ret->offset = 0; |
568408e3 | 116 | |
117 | if (unlikely (rs < 1)) | |
af1e9051 | 118 | alloc_size = 0; |
568408e3 | 119 | else |
af1e9051 | 120 | alloc_size = rs; |
568408e3 | 121 | |
af1e9051 | 122 | ret->base_addr = xmallocarray (alloc_size, sizeof ('rtype_name`)); |
93c57576 | 123 | ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim; |
124 | } | |
c808637e | 125 | |
126 | if (shape_empty) | |
127 | return; | |
93c57576 | 128 | |
49ff3ac0 | 129 | if (pad) |
130 | { | |
131 | pdim = GFC_DESCRIPTOR_RANK (pad); | |
132 | psize = 1; | |
133 | pempty = 0; | |
134 | for (n = 0; n < pdim; n++) | |
135 | { | |
136 | pcount[n] = 0; | |
827aef63 | 137 | pstride[n] = GFC_DESCRIPTOR_STRIDE(pad,n); |
138 | pextent[n] = GFC_DESCRIPTOR_EXTENT(pad,n); | |
49ff3ac0 | 139 | if (pextent[n] <= 0) |
140 | { | |
141 | pempty = 1; | |
142 | pextent[n] = 0; | |
143 | } | |
144 | ||
145 | if (psize == pstride[n]) | |
146 | psize *= pextent[n]; | |
147 | else | |
148 | psize = 0; | |
149 | } | |
553877d9 | 150 | pptr = pad->base_addr; |
49ff3ac0 | 151 | } |
152 | else | |
153 | { | |
154 | pdim = 0; | |
155 | psize = 1; | |
156 | pempty = 1; | |
157 | pptr = NULL; | |
158 | } | |
159 | ||
d5fc2c84 | 160 | if (unlikely (compile_options.bounds_check)) |
161 | { | |
80726d88 | 162 | index_type ret_extent, source_extent; |
163 | ||
164 | rs = 1; | |
165 | for (n = 0; n < rdim; n++) | |
166 | { | |
167 | rs *= shape_data[n]; | |
827aef63 | 168 | ret_extent = GFC_DESCRIPTOR_EXTENT(ret,n); |
80726d88 | 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]); | |
174 | } | |
175 | ||
aeef0389 | 176 | source_extent = 1; |
177 | sdim = GFC_DESCRIPTOR_RANK (source); | |
178 | for (n = 0; n < sdim; n++) | |
179 | { | |
180 | index_type se; | |
827aef63 | 181 | se = GFC_DESCRIPTOR_EXTENT(source,n); |
aeef0389 | 182 | source_extent *= se > 0 ? se : 0; |
183 | } | |
80726d88 | 184 | |
49ff3ac0 | 185 | if (rs > source_extent && (!pad || pempty)) |
80726d88 | 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); | |
189 | ||
d5fc2c84 | 190 | if (order) |
191 | { | |
192 | int seen[GFC_MAX_DIMENSIONS]; | |
193 | index_type v; | |
194 | ||
195 | for (n = 0; n < rdim; n++) | |
196 | seen[n] = 0; | |
197 | ||
198 | for (n = 0; n < rdim; n++) | |
199 | { | |
553877d9 | 200 | v = order->base_addr[n * GFC_DESCRIPTOR_STRIDE(order,0)] - 1; |
d5fc2c84 | 201 | |
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); | |
205 | ||
206 | if (seen[v] != 0) | |
207 | runtime_error("Duplicate value %ld in ORDER argument to" | |
208 | " RESHAPE intrinsic", (long int) v + 1); | |
209 | ||
210 | seen[v] = 1; | |
211 | } | |
212 | } | |
213 | } | |
214 | ||
4ee9c684 | 215 | rsize = 1; |
216 | for (n = 0; n < rdim; n++) | |
217 | { | |
218 | if (order) | |
553877d9 | 219 | dim = order->base_addr[n * GFC_DESCRIPTOR_STRIDE(order,0)] - 1; |
4ee9c684 | 220 | else |
221 | dim = n; | |
222 | ||
223 | rcount[n] = 0; | |
827aef63 | 224 | rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,dim); |
225 | rextent[n] = GFC_DESCRIPTOR_EXTENT(ret,dim); | |
c808637e | 226 | if (rextent[n] < 0) |
2526e2bf | 227 | rextent[n] = 0; |
4ee9c684 | 228 | |
c808637e | 229 | if (rextent[n] != shape_data[dim]) |
4ee9c684 | 230 | runtime_error ("shape and target do not conform"); |
231 | ||
232 | if (rsize == rstride[n]) | |
233 | rsize *= rextent[n]; | |
234 | else | |
235 | rsize = 0; | |
93c57576 | 236 | if (rextent[n] <= 0) |
4ee9c684 | 237 | return; |
238 | } | |
239 | ||
240 | sdim = GFC_DESCRIPTOR_RANK (source); | |
241 | ssize = 1; | |
e7382835 | 242 | sempty = 0; |
4ee9c684 | 243 | for (n = 0; n < sdim; n++) |
244 | { | |
245 | scount[n] = 0; | |
827aef63 | 246 | sstride[n] = GFC_DESCRIPTOR_STRIDE(source,n); |
247 | sextent[n] = GFC_DESCRIPTOR_EXTENT(source,n); | |
4ee9c684 | 248 | if (sextent[n] <= 0) |
e7382835 | 249 | { |
250 | sempty = 1; | |
251 | sextent[n] = 0; | |
252 | } | |
4ee9c684 | 253 | |
254 | if (ssize == sstride[n]) | |
255 | ssize *= sextent[n]; | |
256 | else | |
257 | ssize = 0; | |
258 | } | |
259 | ||
4ee9c684 | 260 | if (rsize != 0 && ssize != 0 && psize != 0) |
261 | { | |
0a6b5f6b | 262 | rsize *= sizeof ('rtype_name`); |
263 | ssize *= sizeof ('rtype_name`); | |
264 | psize *= sizeof ('rtype_name`); | |
553877d9 | 265 | reshape_packed ((char *)ret->base_addr, rsize, (char *)source->base_addr, |
266 | ssize, pad ? (char *)pad->base_addr : NULL, psize); | |
4ee9c684 | 267 | return; |
268 | } | |
553877d9 | 269 | rptr = ret->base_addr; |
270 | src = sptr = source->base_addr; | |
4ee9c684 | 271 | rstride0 = rstride[0]; |
272 | sstride0 = sstride[0]; | |
273 | ||
e7382835 | 274 | if (sempty && pempty) |
275 | abort (); | |
276 | ||
277 | if (sempty) | |
278 | { | |
d0b5b6ea | 279 | /* Pretend we are using the pad array the first time around, too. */ |
e7382835 | 280 | src = pptr; |
d0b5b6ea | 281 | sptr = pptr; |
e7382835 | 282 | sdim = pdim; |
283 | for (dim = 0; dim < pdim; dim++) | |
284 | { | |
285 | scount[dim] = pcount[dim]; | |
286 | sextent[dim] = pextent[dim]; | |
287 | sstride[dim] = pstride[dim]; | |
d0b5b6ea | 288 | sstride0 = pstride[0]; |
e7382835 | 289 | } |
290 | } | |
291 | ||
4ee9c684 | 292 | while (rptr) |
293 | { | |
294 | /* Select between the source and pad arrays. */ | |
295 | *rptr = *src; | |
296 | /* Advance to the next element. */ | |
297 | rptr += rstride0; | |
298 | src += sstride0; | |
299 | rcount[0]++; | |
300 | scount[0]++; | |
e7382835 | 301 | |
4ee9c684 | 302 | /* Advance to the next destination element. */ |
303 | n = 0; | |
304 | while (rcount[n] == rextent[n]) | |
305 | { | |
306 | /* When we get to the end of a dimension, reset it and increment | |
307 | the next dimension. */ | |
308 | rcount[n] = 0; | |
309 | /* We could precalculate these products, but this is a less | |
a2ffc2c4 | 310 | frequently used path so probably not worth it. */ |
4ee9c684 | 311 | rptr -= rstride[n] * rextent[n]; |
312 | n++; | |
313 | if (n == rdim) | |
314 | { | |
315 | /* Break out of the loop. */ | |
316 | rptr = NULL; | |
317 | break; | |
318 | } | |
319 | else | |
320 | { | |
321 | rcount[n]++; | |
322 | rptr += rstride[n]; | |
323 | } | |
324 | } | |
325 | /* Advance to the next source element. */ | |
326 | n = 0; | |
327 | while (scount[n] == sextent[n]) | |
328 | { | |
329 | /* When we get to the end of a dimension, reset it and increment | |
330 | the next dimension. */ | |
331 | scount[n] = 0; | |
332 | /* We could precalculate these products, but this is a less | |
a2ffc2c4 | 333 | frequently used path so probably not worth it. */ |
4ee9c684 | 334 | src -= sstride[n] * sextent[n]; |
335 | n++; | |
336 | if (n == sdim) | |
337 | { | |
338 | if (sptr && pad) | |
339 | { | |
340 | /* Switch to the pad array. */ | |
341 | sptr = NULL; | |
342 | sdim = pdim; | |
343 | for (dim = 0; dim < pdim; dim++) | |
344 | { | |
345 | scount[dim] = pcount[dim]; | |
346 | sextent[dim] = pextent[dim]; | |
347 | sstride[dim] = pstride[dim]; | |
348 | sstride0 = sstride[0]; | |
349 | } | |
350 | } | |
351 | /* We now start again from the beginning of the pad array. */ | |
352 | src = pptr; | |
353 | break; | |
354 | } | |
355 | else | |
356 | { | |
357 | scount[n]++; | |
358 | src += sstride[n]; | |
359 | } | |
360 | } | |
361 | } | |
362 | } | |
920e54ef | 363 | |
0a6b5f6b | 364 | #endif' |