1 /* Generic implementation of the SPREAD intrinsic
2 Copyright (C) 2002-2013 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"
32 spread_internal (gfc_array_char
*ret
, const gfc_array_char
*source
,
33 const index_type
*along
, const index_type
*pncopies
)
35 /* r.* indicates the return array. */
36 index_type rstride
[GFC_MAX_DIMENSIONS
];
38 index_type rdelta
= 0;
43 /* s.* indicates the source array. */
44 index_type sstride
[GFC_MAX_DIMENSIONS
];
49 index_type count
[GFC_MAX_DIMENSIONS
];
50 index_type extent
[GFC_MAX_DIMENSIONS
];
56 size
= GFC_DESCRIPTOR_SIZE(source
);
58 srank
= GFC_DESCRIPTOR_RANK(source
);
61 if (rrank
> GFC_MAX_DIMENSIONS
)
62 runtime_error ("return rank too large in spread()");
65 runtime_error ("dim outside of rank in spread()");
69 if (ret
->base_addr
== NULL
)
71 /* The front end has signalled that we need to populate the
72 return array descriptor. */
76 ret
->dtype
= (source
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rrank
;
79 for (n
= 0; n
< rrank
; n
++)
91 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
92 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE_BYTES(source
,dim
);
93 rstride
[dim
] = rs
* size
;
100 GFC_DIMENSION_SET(ret
->dim
[n
], 0, ub
, stride
);
103 ret
->base_addr
= xmalloc (rs
* size
);
115 if (GFC_DESCRIPTOR_RANK(ret
) != rrank
)
116 runtime_error ("rank mismatch in spread()");
118 if (compile_options
.bounds_check
)
120 for (n
= 0; n
< rrank
; n
++)
122 index_type ret_extent
;
124 ret_extent
= GFC_DESCRIPTOR_EXTENT(ret
,n
);
127 rdelta
= GFC_DESCRIPTOR_STRIDE_BYTES(ret
,n
);
129 if (ret_extent
!= ncopies
)
130 runtime_error("Incorrect extent in return value of SPREAD"
131 " intrinsic in dimension %ld: is %ld,"
132 " should be %ld", (long int) n
+1,
133 (long int) ret_extent
, (long int) ncopies
);
138 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
139 if (ret_extent
!= extent
[dim
])
140 runtime_error("Incorrect extent in return value of SPREAD"
141 " intrinsic in dimension %ld: is %ld,"
142 " should be %ld", (long int) n
+1,
143 (long int) ret_extent
,
144 (long int) extent
[dim
]);
146 if (extent
[dim
] <= 0)
148 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE_BYTES(source
,dim
);
149 rstride
[dim
] = GFC_DESCRIPTOR_STRIDE_BYTES(ret
,n
);
156 for (n
= 0; n
< rrank
; n
++)
160 rdelta
= GFC_DESCRIPTOR_STRIDE_BYTES(ret
,n
);
165 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
166 if (extent
[dim
] <= 0)
168 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE_BYTES(source
,dim
);
169 rstride
[dim
] = GFC_DESCRIPTOR_STRIDE_BYTES(ret
,n
);
181 sstride0
= sstride
[0];
182 rstride0
= rstride
[0];
183 rptr
= ret
->base_addr
;
184 sptr
= source
->base_addr
;
188 /* Spread this element. */
190 for (n
= 0; n
< ncopies
; n
++)
192 memcpy (dest
, sptr
, size
);
195 /* Advance to the next element. */
200 while (count
[n
] == extent
[n
])
202 /* When we get to the end of a dimension, reset it and increment
203 the next dimension. */
205 /* We could precalculate these products, but this is a less
206 frequently used path so probably not worth it. */
207 sptr
-= sstride
[n
] * extent
[n
];
208 rptr
-= rstride
[n
] * extent
[n
];
212 /* Break out of the loop. */
226 /* This version of spread_internal treats the special case of a scalar
227 source. This is much simpler than the more general case above. */
230 spread_internal_scalar (gfc_array_char
*ret
, const char *source
,
231 const index_type
*along
, const index_type
*pncopies
)
234 int ncopies
= *pncopies
;
238 size
= GFC_DESCRIPTOR_SIZE(ret
);
240 if (GFC_DESCRIPTOR_RANK (ret
) != 1)
241 runtime_error ("incorrect destination rank in spread()");
244 runtime_error ("dim outside of rank in spread()");
246 if (ret
->base_addr
== NULL
)
248 ret
->base_addr
= xmalloc (ncopies
* size
);
250 GFC_DIMENSION_SET(ret
->dim
[0], 0, ncopies
- 1, 1);
254 if (ncopies
- 1 > (GFC_DESCRIPTOR_EXTENT(ret
,0) - 1)
255 / GFC_DESCRIPTOR_STRIDE(ret
,0))
256 runtime_error ("dim too large in spread()");
259 for (n
= 0; n
< ncopies
; n
++)
261 dest
= (char*)(ret
->base_addr
+ n
* GFC_DESCRIPTOR_STRIDE_BYTES(ret
,0));
262 memcpy (dest
, source
, size
);
266 extern void spread (gfc_array_char
*, const gfc_array_char
*,
267 const index_type
*, const index_type
*);
268 export_proto(spread
);
271 spread (gfc_array_char
*ret
, const gfc_array_char
*source
,
272 const index_type
*along
, const index_type
*pncopies
)
274 index_type type_size
;
276 type_size
= GFC_DTYPE_TYPE_SIZE(ret
);
279 case GFC_DTYPE_DERIVED_1
:
280 case GFC_DTYPE_LOGICAL_1
:
281 case GFC_DTYPE_INTEGER_1
:
282 spread_i1 ((gfc_array_i1
*) ret
, (gfc_array_i1
*) source
,
286 case GFC_DTYPE_LOGICAL_2
:
287 case GFC_DTYPE_INTEGER_2
:
288 spread_i2 ((gfc_array_i2
*) ret
, (gfc_array_i2
*) source
,
292 case GFC_DTYPE_LOGICAL_4
:
293 case GFC_DTYPE_INTEGER_4
:
294 spread_i4 ((gfc_array_i4
*) ret
, (gfc_array_i4
*) source
,
298 case GFC_DTYPE_LOGICAL_8
:
299 case GFC_DTYPE_INTEGER_8
:
300 spread_i8 ((gfc_array_i8
*) ret
, (gfc_array_i8
*) source
,
304 #ifdef HAVE_GFC_INTEGER_16
305 case GFC_DTYPE_LOGICAL_16
:
306 case GFC_DTYPE_INTEGER_16
:
307 spread_i16 ((gfc_array_i16
*) ret
, (gfc_array_i16
*) source
,
312 case GFC_DTYPE_REAL_4
:
313 spread_r4 ((gfc_array_r4
*) ret
, (gfc_array_r4
*) source
,
317 case GFC_DTYPE_REAL_8
:
318 spread_r8 ((gfc_array_r8
*) ret
, (gfc_array_r8
*) source
,
322 /* FIXME: This here is a hack, which will have to be removed when
323 the array descriptor is reworked. Currently, we don't store the
324 kind value for the type, but only the size. Because on targets with
325 __float128, we have sizeof(logn double) == sizeof(__float128),
326 we cannot discriminate here and have to fall back to the generic
327 handling (which is suboptimal). */
328 #if !defined(GFC_REAL_16_IS_FLOAT128)
329 # ifdef GFC_HAVE_REAL_10
330 case GFC_DTYPE_REAL_10
:
331 spread_r10 ((gfc_array_r10
*) ret
, (gfc_array_r10
*) source
,
336 # ifdef GFC_HAVE_REAL_16
337 case GFC_DTYPE_REAL_16
:
338 spread_r16 ((gfc_array_r16
*) ret
, (gfc_array_r16
*) source
,
344 case GFC_DTYPE_COMPLEX_4
:
345 spread_c4 ((gfc_array_c4
*) ret
, (gfc_array_c4
*) source
,
349 case GFC_DTYPE_COMPLEX_8
:
350 spread_c8 ((gfc_array_c8
*) ret
, (gfc_array_c8
*) source
,
354 /* FIXME: This here is a hack, which will have to be removed when
355 the array descriptor is reworked. Currently, we don't store the
356 kind value for the type, but only the size. Because on targets with
357 __float128, we have sizeof(logn double) == sizeof(__float128),
358 we cannot discriminate here and have to fall back to the generic
359 handling (which is suboptimal). */
360 #if !defined(GFC_REAL_16_IS_FLOAT128)
361 # ifdef GFC_HAVE_COMPLEX_10
362 case GFC_DTYPE_COMPLEX_10
:
363 spread_c10 ((gfc_array_c10
*) ret
, (gfc_array_c10
*) source
,
368 # ifdef GFC_HAVE_COMPLEX_16
369 case GFC_DTYPE_COMPLEX_16
:
370 spread_c16 ((gfc_array_c16
*) ret
, (gfc_array_c16
*) source
,
376 case GFC_DTYPE_DERIVED_2
:
377 if (GFC_UNALIGNED_2(ret
->base_addr
) || GFC_UNALIGNED_2(source
->base_addr
))
381 spread_i2 ((gfc_array_i2
*) ret
, (gfc_array_i2
*) source
,
386 case GFC_DTYPE_DERIVED_4
:
387 if (GFC_UNALIGNED_4(ret
->base_addr
) || GFC_UNALIGNED_4(source
->base_addr
))
391 spread_i4 ((gfc_array_i4
*) ret
, (gfc_array_i4
*) source
,
396 case GFC_DTYPE_DERIVED_8
:
397 if (GFC_UNALIGNED_8(ret
->base_addr
) || GFC_UNALIGNED_8(source
->base_addr
))
401 spread_i8 ((gfc_array_i8
*) ret
, (gfc_array_i8
*) source
,
406 #ifdef HAVE_GFC_INTEGER_16
407 case GFC_DTYPE_DERIVED_16
:
408 if (GFC_UNALIGNED_16(ret
->base_addr
)
409 || GFC_UNALIGNED_16(source
->base_addr
))
413 spread_i16 ((gfc_array_i16
*) ret
, (gfc_array_i16
*) source
,
420 spread_internal (ret
, source
, along
, pncopies
);
424 extern void spread_char (gfc_array_char
*, GFC_INTEGER_4
,
425 const gfc_array_char
*, const index_type
*,
426 const index_type
*, GFC_INTEGER_4
);
427 export_proto(spread_char
);
430 spread_char (gfc_array_char
*ret
,
431 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
432 const gfc_array_char
*source
, const index_type
*along
,
433 const index_type
*pncopies
,
434 GFC_INTEGER_4 source_length
__attribute__((unused
)))
436 spread_internal (ret
, source
, along
, pncopies
);
440 extern void spread_char4 (gfc_array_char
*, GFC_INTEGER_4
,
441 const gfc_array_char
*, const index_type
*,
442 const index_type
*, GFC_INTEGER_4
);
443 export_proto(spread_char4
);
446 spread_char4 (gfc_array_char
*ret
,
447 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
448 const gfc_array_char
*source
, const index_type
*along
,
449 const index_type
*pncopies
,
450 GFC_INTEGER_4 source_length
__attribute__((unused
)))
452 spread_internal (ret
, source
, along
, pncopies
);
456 /* The following are the prototypes for the versions of spread with a
459 extern void spread_scalar (gfc_array_char
*, const char *,
460 const index_type
*, const index_type
*);
461 export_proto(spread_scalar
);
464 spread_scalar (gfc_array_char
*ret
, const char *source
,
465 const index_type
*along
, const index_type
*pncopies
)
467 index_type type_size
;
470 runtime_error ("return array missing descriptor in spread()");
472 type_size
= GFC_DTYPE_TYPE_SIZE(ret
);
475 case GFC_DTYPE_DERIVED_1
:
476 case GFC_DTYPE_LOGICAL_1
:
477 case GFC_DTYPE_INTEGER_1
:
478 spread_scalar_i1 ((gfc_array_i1
*) ret
, (GFC_INTEGER_1
*) source
,
482 case GFC_DTYPE_LOGICAL_2
:
483 case GFC_DTYPE_INTEGER_2
:
484 spread_scalar_i2 ((gfc_array_i2
*) ret
, (GFC_INTEGER_2
*) source
,
488 case GFC_DTYPE_LOGICAL_4
:
489 case GFC_DTYPE_INTEGER_4
:
490 spread_scalar_i4 ((gfc_array_i4
*) ret
, (GFC_INTEGER_4
*) source
,
494 case GFC_DTYPE_LOGICAL_8
:
495 case GFC_DTYPE_INTEGER_8
:
496 spread_scalar_i8 ((gfc_array_i8
*) ret
, (GFC_INTEGER_8
*) source
,
500 #ifdef HAVE_GFC_INTEGER_16
501 case GFC_DTYPE_LOGICAL_16
:
502 case GFC_DTYPE_INTEGER_16
:
503 spread_scalar_i16 ((gfc_array_i16
*) ret
, (GFC_INTEGER_16
*) source
,
508 case GFC_DTYPE_REAL_4
:
509 spread_scalar_r4 ((gfc_array_r4
*) ret
, (GFC_REAL_4
*) source
,
513 case GFC_DTYPE_REAL_8
:
514 spread_scalar_r8 ((gfc_array_r8
*) ret
, (GFC_REAL_8
*) source
,
518 /* FIXME: This here is a hack, which will have to be removed when
519 the array descriptor is reworked. Currently, we don't store the
520 kind value for the type, but only the size. Because on targets with
521 __float128, we have sizeof(logn double) == sizeof(__float128),
522 we cannot discriminate here and have to fall back to the generic
523 handling (which is suboptimal). */
524 #if !defined(GFC_REAL_16_IS_FLOAT128)
525 # ifdef HAVE_GFC_REAL_10
526 case GFC_DTYPE_REAL_10
:
527 spread_scalar_r10 ((gfc_array_r10
*) ret
, (GFC_REAL_10
*) source
,
532 # ifdef HAVE_GFC_REAL_16
533 case GFC_DTYPE_REAL_16
:
534 spread_scalar_r16 ((gfc_array_r16
*) ret
, (GFC_REAL_16
*) source
,
540 case GFC_DTYPE_COMPLEX_4
:
541 spread_scalar_c4 ((gfc_array_c4
*) ret
, (GFC_COMPLEX_4
*) source
,
545 case GFC_DTYPE_COMPLEX_8
:
546 spread_scalar_c8 ((gfc_array_c8
*) ret
, (GFC_COMPLEX_8
*) source
,
550 /* FIXME: This here is a hack, which will have to be removed when
551 the array descriptor is reworked. Currently, we don't store the
552 kind value for the type, but only the size. Because on targets with
553 __float128, we have sizeof(logn double) == sizeof(__float128),
554 we cannot discriminate here and have to fall back to the generic
555 handling (which is suboptimal). */
556 #if !defined(GFC_REAL_16_IS_FLOAT128)
557 # ifdef HAVE_GFC_COMPLEX_10
558 case GFC_DTYPE_COMPLEX_10
:
559 spread_scalar_c10 ((gfc_array_c10
*) ret
, (GFC_COMPLEX_10
*) source
,
564 # ifdef HAVE_GFC_COMPLEX_16
565 case GFC_DTYPE_COMPLEX_16
:
566 spread_scalar_c16 ((gfc_array_c16
*) ret
, (GFC_COMPLEX_16
*) source
,
572 case GFC_DTYPE_DERIVED_2
:
573 if (GFC_UNALIGNED_2(ret
->base_addr
) || GFC_UNALIGNED_2(source
))
577 spread_scalar_i2 ((gfc_array_i2
*) ret
, (GFC_INTEGER_2
*) source
,
582 case GFC_DTYPE_DERIVED_4
:
583 if (GFC_UNALIGNED_4(ret
->base_addr
) || GFC_UNALIGNED_4(source
))
587 spread_scalar_i4 ((gfc_array_i4
*) ret
, (GFC_INTEGER_4
*) source
,
592 case GFC_DTYPE_DERIVED_8
:
593 if (GFC_UNALIGNED_8(ret
->base_addr
) || GFC_UNALIGNED_8(source
))
597 spread_scalar_i8 ((gfc_array_i8
*) ret
, (GFC_INTEGER_8
*) source
,
601 #ifdef HAVE_GFC_INTEGER_16
602 case GFC_DTYPE_DERIVED_16
:
603 if (GFC_UNALIGNED_16(ret
->base_addr
) || GFC_UNALIGNED_16(source
))
607 spread_scalar_i16 ((gfc_array_i16
*) ret
, (GFC_INTEGER_16
*) source
,
614 spread_internal_scalar (ret
, source
, along
, pncopies
);
618 extern void spread_char_scalar (gfc_array_char
*, GFC_INTEGER_4
,
619 const char *, const index_type
*,
620 const index_type
*, GFC_INTEGER_4
);
621 export_proto(spread_char_scalar
);
624 spread_char_scalar (gfc_array_char
*ret
,
625 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
626 const char *source
, const index_type
*along
,
627 const index_type
*pncopies
,
628 GFC_INTEGER_4 source_length
__attribute__((unused
)))
631 runtime_error ("return array missing descriptor in spread()");
632 spread_internal_scalar (ret
, source
, along
, pncopies
);
636 extern void spread_char4_scalar (gfc_array_char
*, GFC_INTEGER_4
,
637 const char *, const index_type
*,
638 const index_type
*, GFC_INTEGER_4
);
639 export_proto(spread_char4_scalar
);
642 spread_char4_scalar (gfc_array_char
*ret
,
643 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
644 const char *source
, const index_type
*along
,
645 const index_type
*pncopies
,
646 GFC_INTEGER_4 source_length
__attribute__((unused
)))
649 runtime_error ("return array missing descriptor in spread()");
650 spread_internal_scalar (ret
, source
, along
, pncopies
);