1 /* Generic implementation of the SPREAD intrinsic
2 Copyright 2002, 2005, 2006, 2007, 2009, 2010, 2012
3 Free Software Foundation, Inc.
4 Contributed by Paul Brook <paul@nowt.org>
6 This file is part of the GNU Fortran runtime library (libgfortran).
8 Libgfortran is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public
10 License as published by the Free Software Foundation; either
11 version 3 of the License, or (at your option) any later version.
13 Ligbfortran is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 #include "libgfortran.h"
33 spread_internal (gfc_array_char
*ret
, const gfc_array_char
*source
,
34 const index_type
*along
, const index_type
*pncopies
)
36 /* r.* indicates the return array. */
37 index_type rstride
[GFC_MAX_DIMENSIONS
];
39 index_type rdelta
= 0;
44 /* s.* indicates the source array. */
45 index_type sstride
[GFC_MAX_DIMENSIONS
];
50 index_type count
[GFC_MAX_DIMENSIONS
];
51 index_type extent
[GFC_MAX_DIMENSIONS
];
57 size
= GFC_DESCRIPTOR_SIZE(source
);
59 srank
= GFC_DESCRIPTOR_RANK(source
);
62 if (rrank
> GFC_MAX_DIMENSIONS
)
63 runtime_error ("return rank too large in spread()");
66 runtime_error ("dim outside of rank in spread()");
70 if (ret
->base_addr
== NULL
)
72 /* The front end has signalled that we need to populate the
73 return array descriptor. */
77 ret
->dtype
= (source
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rrank
;
80 for (n
= 0; n
< rrank
; n
++)
92 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
93 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE_BYTES(source
,dim
);
94 rstride
[dim
] = rs
* size
;
101 GFC_DIMENSION_SET(ret
->dim
[n
], 0, ub
, stride
);
104 ret
->base_addr
= xmalloc (rs
* size
);
116 if (GFC_DESCRIPTOR_RANK(ret
) != rrank
)
117 runtime_error ("rank mismatch in spread()");
119 if (compile_options
.bounds_check
)
121 for (n
= 0; n
< rrank
; n
++)
123 index_type ret_extent
;
125 ret_extent
= GFC_DESCRIPTOR_EXTENT(ret
,n
);
128 rdelta
= GFC_DESCRIPTOR_STRIDE_BYTES(ret
,n
);
130 if (ret_extent
!= ncopies
)
131 runtime_error("Incorrect extent in return value of SPREAD"
132 " intrinsic in dimension %ld: is %ld,"
133 " should be %ld", (long int) n
+1,
134 (long int) ret_extent
, (long int) ncopies
);
139 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
140 if (ret_extent
!= extent
[dim
])
141 runtime_error("Incorrect extent in return value of SPREAD"
142 " intrinsic in dimension %ld: is %ld,"
143 " should be %ld", (long int) n
+1,
144 (long int) ret_extent
,
145 (long int) extent
[dim
]);
147 if (extent
[dim
] <= 0)
149 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE_BYTES(source
,dim
);
150 rstride
[dim
] = GFC_DESCRIPTOR_STRIDE_BYTES(ret
,n
);
157 for (n
= 0; n
< rrank
; n
++)
161 rdelta
= GFC_DESCRIPTOR_STRIDE_BYTES(ret
,n
);
166 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
167 if (extent
[dim
] <= 0)
169 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE_BYTES(source
,dim
);
170 rstride
[dim
] = GFC_DESCRIPTOR_STRIDE_BYTES(ret
,n
);
182 sstride0
= sstride
[0];
183 rstride0
= rstride
[0];
184 rptr
= ret
->base_addr
;
185 sptr
= source
->base_addr
;
189 /* Spread this element. */
191 for (n
= 0; n
< ncopies
; n
++)
193 memcpy (dest
, sptr
, size
);
196 /* Advance to the next element. */
201 while (count
[n
] == extent
[n
])
203 /* When we get to the end of a dimension, reset it and increment
204 the next dimension. */
206 /* We could precalculate these products, but this is a less
207 frequently used path so probably not worth it. */
208 sptr
-= sstride
[n
] * extent
[n
];
209 rptr
-= rstride
[n
] * extent
[n
];
213 /* Break out of the loop. */
227 /* This version of spread_internal treats the special case of a scalar
228 source. This is much simpler than the more general case above. */
231 spread_internal_scalar (gfc_array_char
*ret
, const char *source
,
232 const index_type
*along
, const index_type
*pncopies
)
235 int ncopies
= *pncopies
;
239 size
= GFC_DESCRIPTOR_SIZE(ret
);
241 if (GFC_DESCRIPTOR_RANK (ret
) != 1)
242 runtime_error ("incorrect destination rank in spread()");
245 runtime_error ("dim outside of rank in spread()");
247 if (ret
->base_addr
== NULL
)
249 ret
->base_addr
= xmalloc (ncopies
* size
);
251 GFC_DIMENSION_SET(ret
->dim
[0], 0, ncopies
- 1, 1);
255 if (ncopies
- 1 > (GFC_DESCRIPTOR_EXTENT(ret
,0) - 1)
256 / GFC_DESCRIPTOR_STRIDE(ret
,0))
257 runtime_error ("dim too large in spread()");
260 for (n
= 0; n
< ncopies
; n
++)
262 dest
= (char*)(ret
->base_addr
+ n
* GFC_DESCRIPTOR_STRIDE_BYTES(ret
,0));
263 memcpy (dest
, source
, size
);
267 extern void spread (gfc_array_char
*, const gfc_array_char
*,
268 const index_type
*, const index_type
*);
269 export_proto(spread
);
272 spread (gfc_array_char
*ret
, const gfc_array_char
*source
,
273 const index_type
*along
, const index_type
*pncopies
)
275 index_type type_size
;
277 type_size
= GFC_DTYPE_TYPE_SIZE(ret
);
280 case GFC_DTYPE_DERIVED_1
:
281 case GFC_DTYPE_LOGICAL_1
:
282 case GFC_DTYPE_INTEGER_1
:
283 spread_i1 ((gfc_array_i1
*) ret
, (gfc_array_i1
*) source
,
287 case GFC_DTYPE_LOGICAL_2
:
288 case GFC_DTYPE_INTEGER_2
:
289 spread_i2 ((gfc_array_i2
*) ret
, (gfc_array_i2
*) source
,
293 case GFC_DTYPE_LOGICAL_4
:
294 case GFC_DTYPE_INTEGER_4
:
295 spread_i4 ((gfc_array_i4
*) ret
, (gfc_array_i4
*) source
,
299 case GFC_DTYPE_LOGICAL_8
:
300 case GFC_DTYPE_INTEGER_8
:
301 spread_i8 ((gfc_array_i8
*) ret
, (gfc_array_i8
*) source
,
305 #ifdef HAVE_GFC_INTEGER_16
306 case GFC_DTYPE_LOGICAL_16
:
307 case GFC_DTYPE_INTEGER_16
:
308 spread_i16 ((gfc_array_i16
*) ret
, (gfc_array_i16
*) source
,
313 case GFC_DTYPE_REAL_4
:
314 spread_r4 ((gfc_array_r4
*) ret
, (gfc_array_r4
*) source
,
318 case GFC_DTYPE_REAL_8
:
319 spread_r8 ((gfc_array_r8
*) ret
, (gfc_array_r8
*) source
,
323 /* FIXME: This here is a hack, which will have to be removed when
324 the array descriptor is reworked. Currently, we don't store the
325 kind value for the type, but only the size. Because on targets with
326 __float128, we have sizeof(logn double) == sizeof(__float128),
327 we cannot discriminate here and have to fall back to the generic
328 handling (which is suboptimal). */
329 #if !defined(GFC_REAL_16_IS_FLOAT128)
330 # ifdef GFC_HAVE_REAL_10
331 case GFC_DTYPE_REAL_10
:
332 spread_r10 ((gfc_array_r10
*) ret
, (gfc_array_r10
*) source
,
337 # ifdef GFC_HAVE_REAL_16
338 case GFC_DTYPE_REAL_16
:
339 spread_r16 ((gfc_array_r16
*) ret
, (gfc_array_r16
*) source
,
345 case GFC_DTYPE_COMPLEX_4
:
346 spread_c4 ((gfc_array_c4
*) ret
, (gfc_array_c4
*) source
,
350 case GFC_DTYPE_COMPLEX_8
:
351 spread_c8 ((gfc_array_c8
*) ret
, (gfc_array_c8
*) source
,
355 /* FIXME: This here is a hack, which will have to be removed when
356 the array descriptor is reworked. Currently, we don't store the
357 kind value for the type, but only the size. Because on targets with
358 __float128, we have sizeof(logn double) == sizeof(__float128),
359 we cannot discriminate here and have to fall back to the generic
360 handling (which is suboptimal). */
361 #if !defined(GFC_REAL_16_IS_FLOAT128)
362 # ifdef GFC_HAVE_COMPLEX_10
363 case GFC_DTYPE_COMPLEX_10
:
364 spread_c10 ((gfc_array_c10
*) ret
, (gfc_array_c10
*) source
,
369 # ifdef GFC_HAVE_COMPLEX_16
370 case GFC_DTYPE_COMPLEX_16
:
371 spread_c16 ((gfc_array_c16
*) ret
, (gfc_array_c16
*) source
,
377 case GFC_DTYPE_DERIVED_2
:
378 if (GFC_UNALIGNED_2(ret
->base_addr
) || GFC_UNALIGNED_2(source
->base_addr
))
382 spread_i2 ((gfc_array_i2
*) ret
, (gfc_array_i2
*) source
,
387 case GFC_DTYPE_DERIVED_4
:
388 if (GFC_UNALIGNED_4(ret
->base_addr
) || GFC_UNALIGNED_4(source
->base_addr
))
392 spread_i4 ((gfc_array_i4
*) ret
, (gfc_array_i4
*) source
,
397 case GFC_DTYPE_DERIVED_8
:
398 if (GFC_UNALIGNED_8(ret
->base_addr
) || GFC_UNALIGNED_8(source
->base_addr
))
402 spread_i8 ((gfc_array_i8
*) ret
, (gfc_array_i8
*) source
,
407 #ifdef HAVE_GFC_INTEGER_16
408 case GFC_DTYPE_DERIVED_16
:
409 if (GFC_UNALIGNED_16(ret
->base_addr
)
410 || GFC_UNALIGNED_16(source
->base_addr
))
414 spread_i16 ((gfc_array_i16
*) ret
, (gfc_array_i16
*) source
,
421 spread_internal (ret
, source
, along
, pncopies
);
425 extern void spread_char (gfc_array_char
*, GFC_INTEGER_4
,
426 const gfc_array_char
*, const index_type
*,
427 const index_type
*, GFC_INTEGER_4
);
428 export_proto(spread_char
);
431 spread_char (gfc_array_char
*ret
,
432 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
433 const gfc_array_char
*source
, const index_type
*along
,
434 const index_type
*pncopies
,
435 GFC_INTEGER_4 source_length
__attribute__((unused
)))
437 spread_internal (ret
, source
, along
, pncopies
);
441 extern void spread_char4 (gfc_array_char
*, GFC_INTEGER_4
,
442 const gfc_array_char
*, const index_type
*,
443 const index_type
*, GFC_INTEGER_4
);
444 export_proto(spread_char4
);
447 spread_char4 (gfc_array_char
*ret
,
448 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
449 const gfc_array_char
*source
, const index_type
*along
,
450 const index_type
*pncopies
,
451 GFC_INTEGER_4 source_length
__attribute__((unused
)))
453 spread_internal (ret
, source
, along
, pncopies
);
457 /* The following are the prototypes for the versions of spread with a
460 extern void spread_scalar (gfc_array_char
*, const char *,
461 const index_type
*, const index_type
*);
462 export_proto(spread_scalar
);
465 spread_scalar (gfc_array_char
*ret
, const char *source
,
466 const index_type
*along
, const index_type
*pncopies
)
468 index_type type_size
;
471 runtime_error ("return array missing descriptor in spread()");
473 type_size
= GFC_DTYPE_TYPE_SIZE(ret
);
476 case GFC_DTYPE_DERIVED_1
:
477 case GFC_DTYPE_LOGICAL_1
:
478 case GFC_DTYPE_INTEGER_1
:
479 spread_scalar_i1 ((gfc_array_i1
*) ret
, (GFC_INTEGER_1
*) source
,
483 case GFC_DTYPE_LOGICAL_2
:
484 case GFC_DTYPE_INTEGER_2
:
485 spread_scalar_i2 ((gfc_array_i2
*) ret
, (GFC_INTEGER_2
*) source
,
489 case GFC_DTYPE_LOGICAL_4
:
490 case GFC_DTYPE_INTEGER_4
:
491 spread_scalar_i4 ((gfc_array_i4
*) ret
, (GFC_INTEGER_4
*) source
,
495 case GFC_DTYPE_LOGICAL_8
:
496 case GFC_DTYPE_INTEGER_8
:
497 spread_scalar_i8 ((gfc_array_i8
*) ret
, (GFC_INTEGER_8
*) source
,
501 #ifdef HAVE_GFC_INTEGER_16
502 case GFC_DTYPE_LOGICAL_16
:
503 case GFC_DTYPE_INTEGER_16
:
504 spread_scalar_i16 ((gfc_array_i16
*) ret
, (GFC_INTEGER_16
*) source
,
509 case GFC_DTYPE_REAL_4
:
510 spread_scalar_r4 ((gfc_array_r4
*) ret
, (GFC_REAL_4
*) source
,
514 case GFC_DTYPE_REAL_8
:
515 spread_scalar_r8 ((gfc_array_r8
*) ret
, (GFC_REAL_8
*) source
,
519 /* FIXME: This here is a hack, which will have to be removed when
520 the array descriptor is reworked. Currently, we don't store the
521 kind value for the type, but only the size. Because on targets with
522 __float128, we have sizeof(logn double) == sizeof(__float128),
523 we cannot discriminate here and have to fall back to the generic
524 handling (which is suboptimal). */
525 #if !defined(GFC_REAL_16_IS_FLOAT128)
526 # ifdef HAVE_GFC_REAL_10
527 case GFC_DTYPE_REAL_10
:
528 spread_scalar_r10 ((gfc_array_r10
*) ret
, (GFC_REAL_10
*) source
,
533 # ifdef HAVE_GFC_REAL_16
534 case GFC_DTYPE_REAL_16
:
535 spread_scalar_r16 ((gfc_array_r16
*) ret
, (GFC_REAL_16
*) source
,
541 case GFC_DTYPE_COMPLEX_4
:
542 spread_scalar_c4 ((gfc_array_c4
*) ret
, (GFC_COMPLEX_4
*) source
,
546 case GFC_DTYPE_COMPLEX_8
:
547 spread_scalar_c8 ((gfc_array_c8
*) ret
, (GFC_COMPLEX_8
*) source
,
551 /* FIXME: This here is a hack, which will have to be removed when
552 the array descriptor is reworked. Currently, we don't store the
553 kind value for the type, but only the size. Because on targets with
554 __float128, we have sizeof(logn double) == sizeof(__float128),
555 we cannot discriminate here and have to fall back to the generic
556 handling (which is suboptimal). */
557 #if !defined(GFC_REAL_16_IS_FLOAT128)
558 # ifdef HAVE_GFC_COMPLEX_10
559 case GFC_DTYPE_COMPLEX_10
:
560 spread_scalar_c10 ((gfc_array_c10
*) ret
, (GFC_COMPLEX_10
*) source
,
565 # ifdef HAVE_GFC_COMPLEX_16
566 case GFC_DTYPE_COMPLEX_16
:
567 spread_scalar_c16 ((gfc_array_c16
*) ret
, (GFC_COMPLEX_16
*) source
,
573 case GFC_DTYPE_DERIVED_2
:
574 if (GFC_UNALIGNED_2(ret
->base_addr
) || GFC_UNALIGNED_2(source
))
578 spread_scalar_i2 ((gfc_array_i2
*) ret
, (GFC_INTEGER_2
*) source
,
583 case GFC_DTYPE_DERIVED_4
:
584 if (GFC_UNALIGNED_4(ret
->base_addr
) || GFC_UNALIGNED_4(source
))
588 spread_scalar_i4 ((gfc_array_i4
*) ret
, (GFC_INTEGER_4
*) source
,
593 case GFC_DTYPE_DERIVED_8
:
594 if (GFC_UNALIGNED_8(ret
->base_addr
) || GFC_UNALIGNED_8(source
))
598 spread_scalar_i8 ((gfc_array_i8
*) ret
, (GFC_INTEGER_8
*) source
,
602 #ifdef HAVE_GFC_INTEGER_16
603 case GFC_DTYPE_DERIVED_16
:
604 if (GFC_UNALIGNED_16(ret
->base_addr
) || GFC_UNALIGNED_16(source
))
608 spread_scalar_i16 ((gfc_array_i16
*) ret
, (GFC_INTEGER_16
*) source
,
615 spread_internal_scalar (ret
, source
, along
, pncopies
);
619 extern void spread_char_scalar (gfc_array_char
*, GFC_INTEGER_4
,
620 const char *, const index_type
*,
621 const index_type
*, GFC_INTEGER_4
);
622 export_proto(spread_char_scalar
);
625 spread_char_scalar (gfc_array_char
*ret
,
626 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
627 const char *source
, const index_type
*along
,
628 const index_type
*pncopies
,
629 GFC_INTEGER_4 source_length
__attribute__((unused
)))
632 runtime_error ("return array missing descriptor in spread()");
633 spread_internal_scalar (ret
, source
, along
, pncopies
);
637 extern void spread_char4_scalar (gfc_array_char
*, GFC_INTEGER_4
,
638 const char *, const index_type
*,
639 const index_type
*, GFC_INTEGER_4
);
640 export_proto(spread_char4_scalar
);
643 spread_char4_scalar (gfc_array_char
*ret
,
644 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
645 const char *source
, const index_type
*along
,
646 const index_type
*pncopies
,
647 GFC_INTEGER_4 source_length
__attribute__((unused
)))
650 runtime_error ("return array missing descriptor in spread()");
651 spread_internal_scalar (ret
, source
, along
, pncopies
);