1 /* Generic implementation of the SPREAD intrinsic
2 Copyright (C) 2002-2018 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"
30 spread_internal (gfc_array_char
*ret
, const gfc_array_char
*source
,
31 const index_type
*along
, const index_type
*pncopies
)
33 /* r.* indicates the return array. */
34 index_type rstride
[GFC_MAX_DIMENSIONS
];
36 index_type rdelta
= 0;
41 /* s.* indicates the source array. */
42 index_type sstride
[GFC_MAX_DIMENSIONS
];
47 index_type count
[GFC_MAX_DIMENSIONS
];
48 index_type extent
[GFC_MAX_DIMENSIONS
];
54 size
= GFC_DESCRIPTOR_SIZE(source
);
56 srank
= GFC_DESCRIPTOR_RANK(source
);
59 if (rrank
> GFC_MAX_DIMENSIONS
)
60 runtime_error ("return rank too large in spread()");
63 runtime_error ("dim outside of rank in spread()");
67 if (ret
->base_addr
== NULL
)
69 /* The front end has signalled that we need to populate the
70 return array descriptor. */
74 GFC_DTYPE_COPY_SETRANK(ret
,source
,rrank
);
77 for (n
= 0; n
< rrank
; n
++)
89 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
90 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE_BYTES(source
,dim
);
91 rstride
[dim
] = rs
* size
;
98 GFC_DIMENSION_SET(ret
->dim
[n
], 0, ub
, stride
);
101 ret
->base_addr
= xmallocarray (rs
, size
);
113 if (GFC_DESCRIPTOR_RANK(ret
) != rrank
)
114 runtime_error ("rank mismatch in spread()");
116 if (compile_options
.bounds_check
)
118 for (n
= 0; n
< rrank
; n
++)
120 index_type ret_extent
;
122 ret_extent
= GFC_DESCRIPTOR_EXTENT(ret
,n
);
125 rdelta
= GFC_DESCRIPTOR_STRIDE_BYTES(ret
,n
);
127 if (ret_extent
!= ncopies
)
128 runtime_error("Incorrect extent in return value of SPREAD"
129 " intrinsic in dimension %ld: is %ld,"
130 " should be %ld", (long int) n
+1,
131 (long int) ret_extent
, (long int) ncopies
);
136 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
137 if (ret_extent
!= extent
[dim
])
138 runtime_error("Incorrect extent in return value of SPREAD"
139 " intrinsic in dimension %ld: is %ld,"
140 " should be %ld", (long int) n
+1,
141 (long int) ret_extent
,
142 (long int) extent
[dim
]);
144 if (extent
[dim
] <= 0)
146 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE_BYTES(source
,dim
);
147 rstride
[dim
] = GFC_DESCRIPTOR_STRIDE_BYTES(ret
,n
);
154 for (n
= 0; n
< rrank
; n
++)
158 rdelta
= GFC_DESCRIPTOR_STRIDE_BYTES(ret
,n
);
163 extent
[dim
] = GFC_DESCRIPTOR_EXTENT(source
,dim
);
164 if (extent
[dim
] <= 0)
166 sstride
[dim
] = GFC_DESCRIPTOR_STRIDE_BYTES(source
,dim
);
167 rstride
[dim
] = GFC_DESCRIPTOR_STRIDE_BYTES(ret
,n
);
179 sstride0
= sstride
[0];
180 rstride0
= rstride
[0];
181 rptr
= ret
->base_addr
;
182 sptr
= source
->base_addr
;
186 /* Spread this element. */
188 for (n
= 0; n
< ncopies
; n
++)
190 memcpy (dest
, sptr
, size
);
193 /* Advance to the next element. */
198 while (count
[n
] == extent
[n
])
200 /* When we get to the end of a dimension, reset it and increment
201 the next dimension. */
203 /* We could precalculate these products, but this is a less
204 frequently used path so probably not worth it. */
205 sptr
-= sstride
[n
] * extent
[n
];
206 rptr
-= rstride
[n
] * extent
[n
];
210 /* Break out of the loop. */
224 /* This version of spread_internal treats the special case of a scalar
225 source. This is much simpler than the more general case above. */
228 spread_internal_scalar (gfc_array_char
*ret
, const char *source
,
229 const index_type
*along
, const index_type
*pncopies
)
232 int ncopies
= *pncopies
;
236 size
= GFC_DESCRIPTOR_SIZE(ret
);
238 if (GFC_DESCRIPTOR_RANK (ret
) != 1)
239 runtime_error ("incorrect destination rank in spread()");
242 runtime_error ("dim outside of rank in spread()");
244 if (ret
->base_addr
== NULL
)
246 ret
->base_addr
= xmallocarray (ncopies
, size
);
248 GFC_DIMENSION_SET(ret
->dim
[0], 0, ncopies
- 1, 1);
252 if (ncopies
- 1 > (GFC_DESCRIPTOR_EXTENT(ret
,0) - 1)
253 / GFC_DESCRIPTOR_STRIDE(ret
,0))
254 runtime_error ("dim too large in spread()");
257 for (n
= 0; n
< ncopies
; n
++)
259 dest
= (char*)(ret
->base_addr
+ n
* GFC_DESCRIPTOR_STRIDE_BYTES(ret
,0));
260 memcpy (dest
, source
, size
);
264 extern void spread (gfc_array_char
*, const gfc_array_char
*,
265 const index_type
*, const index_type
*);
266 export_proto(spread
);
269 spread (gfc_array_char
*ret
, const gfc_array_char
*source
,
270 const index_type
*along
, const index_type
*pncopies
)
272 index_type type_size
;
274 type_size
= GFC_DTYPE_TYPE_SIZE(ret
);
277 case GFC_DTYPE_LOGICAL_1
:
278 case GFC_DTYPE_INTEGER_1
:
279 spread_i1 ((gfc_array_i1
*) ret
, (gfc_array_i1
*) source
,
283 case GFC_DTYPE_LOGICAL_2
:
284 case GFC_DTYPE_INTEGER_2
:
285 spread_i2 ((gfc_array_i2
*) ret
, (gfc_array_i2
*) source
,
289 case GFC_DTYPE_LOGICAL_4
:
290 case GFC_DTYPE_INTEGER_4
:
291 spread_i4 ((gfc_array_i4
*) ret
, (gfc_array_i4
*) source
,
295 case GFC_DTYPE_LOGICAL_8
:
296 case GFC_DTYPE_INTEGER_8
:
297 spread_i8 ((gfc_array_i8
*) ret
, (gfc_array_i8
*) source
,
301 #ifdef HAVE_GFC_INTEGER_16
302 case GFC_DTYPE_LOGICAL_16
:
303 case GFC_DTYPE_INTEGER_16
:
304 spread_i16 ((gfc_array_i16
*) ret
, (gfc_array_i16
*) source
,
309 case GFC_DTYPE_REAL_4
:
310 spread_r4 ((gfc_array_r4
*) ret
, (gfc_array_r4
*) source
,
314 case GFC_DTYPE_REAL_8
:
315 spread_r8 ((gfc_array_r8
*) ret
, (gfc_array_r8
*) source
,
319 /* FIXME: This here is a hack, which will have to be removed when
320 the array descriptor is reworked. Currently, we don't store the
321 kind value for the type, but only the size. Because on targets with
322 __float128, we have sizeof(logn double) == sizeof(__float128),
323 we cannot discriminate here and have to fall back to the generic
324 handling (which is suboptimal). */
325 #if !defined(GFC_REAL_16_IS_FLOAT128)
326 # ifdef GFC_HAVE_REAL_10
327 case GFC_DTYPE_REAL_10
:
328 spread_r10 ((gfc_array_r10
*) ret
, (gfc_array_r10
*) source
,
333 # ifdef GFC_HAVE_REAL_16
334 case GFC_DTYPE_REAL_16
:
335 spread_r16 ((gfc_array_r16
*) ret
, (gfc_array_r16
*) source
,
341 case GFC_DTYPE_COMPLEX_4
:
342 spread_c4 ((gfc_array_c4
*) ret
, (gfc_array_c4
*) source
,
346 case GFC_DTYPE_COMPLEX_8
:
347 spread_c8 ((gfc_array_c8
*) ret
, (gfc_array_c8
*) source
,
351 /* FIXME: This here is a hack, which will have to be removed when
352 the array descriptor is reworked. Currently, we don't store the
353 kind value for the type, but only the size. Because on targets with
354 __float128, we have sizeof(logn double) == sizeof(__float128),
355 we cannot discriminate here and have to fall back to the generic
356 handling (which is suboptimal). */
357 #if !defined(GFC_REAL_16_IS_FLOAT128)
358 # ifdef GFC_HAVE_COMPLEX_10
359 case GFC_DTYPE_COMPLEX_10
:
360 spread_c10 ((gfc_array_c10
*) ret
, (gfc_array_c10
*) source
,
365 # ifdef GFC_HAVE_COMPLEX_16
366 case GFC_DTYPE_COMPLEX_16
:
367 spread_c16 ((gfc_array_c16
*) ret
, (gfc_array_c16
*) source
,
375 switch (GFC_DESCRIPTOR_SIZE (ret
))
378 spread_i1 ((gfc_array_i1
*) ret
, (gfc_array_i1
*) source
,
383 if (GFC_UNALIGNED_2(ret
->base_addr
) || GFC_UNALIGNED_2(source
->base_addr
))
387 spread_i2 ((gfc_array_i2
*) ret
, (gfc_array_i2
*) source
,
393 if (GFC_UNALIGNED_4(ret
->base_addr
) || GFC_UNALIGNED_4(source
->base_addr
))
397 spread_i4 ((gfc_array_i4
*) ret
, (gfc_array_i4
*) source
,
403 if (GFC_UNALIGNED_8(ret
->base_addr
) || GFC_UNALIGNED_8(source
->base_addr
))
407 spread_i8 ((gfc_array_i8
*) ret
, (gfc_array_i8
*) source
,
411 #ifdef HAVE_GFC_INTEGER_16
413 if (GFC_UNALIGNED_16(ret
->base_addr
)
414 || GFC_UNALIGNED_16(source
->base_addr
))
418 spread_i16 ((gfc_array_i16
*) ret
, (gfc_array_i16
*) source
,
426 spread_internal (ret
, source
, along
, pncopies
);
430 extern void spread_char (gfc_array_char
*, GFC_INTEGER_4
,
431 const gfc_array_char
*, const index_type
*,
432 const index_type
*, GFC_INTEGER_4
);
433 export_proto(spread_char
);
436 spread_char (gfc_array_char
*ret
,
437 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
438 const gfc_array_char
*source
, const index_type
*along
,
439 const index_type
*pncopies
,
440 GFC_INTEGER_4 source_length
__attribute__((unused
)))
442 spread_internal (ret
, source
, along
, pncopies
);
446 extern void spread_char4 (gfc_array_char
*, GFC_INTEGER_4
,
447 const gfc_array_char
*, const index_type
*,
448 const index_type
*, GFC_INTEGER_4
);
449 export_proto(spread_char4
);
452 spread_char4 (gfc_array_char
*ret
,
453 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
454 const gfc_array_char
*source
, const index_type
*along
,
455 const index_type
*pncopies
,
456 GFC_INTEGER_4 source_length
__attribute__((unused
)))
458 spread_internal (ret
, source
, along
, pncopies
);
462 /* The following are the prototypes for the versions of spread with a
465 extern void spread_scalar (gfc_array_char
*, const char *,
466 const index_type
*, const index_type
*);
467 export_proto(spread_scalar
);
470 spread_scalar (gfc_array_char
*ret
, const char *source
,
471 const index_type
*along
, const index_type
*pncopies
)
473 index_type type_size
;
475 if (GFC_DTYPE_IS_UNSET(ret
))
476 runtime_error ("return array missing descriptor in spread()");
478 type_size
= GFC_DTYPE_TYPE_SIZE(ret
);
481 case GFC_DTYPE_LOGICAL_1
:
482 case GFC_DTYPE_INTEGER_1
:
483 spread_scalar_i1 ((gfc_array_i1
*) ret
, (GFC_INTEGER_1
*) source
,
487 case GFC_DTYPE_LOGICAL_2
:
488 case GFC_DTYPE_INTEGER_2
:
489 spread_scalar_i2 ((gfc_array_i2
*) ret
, (GFC_INTEGER_2
*) source
,
493 case GFC_DTYPE_LOGICAL_4
:
494 case GFC_DTYPE_INTEGER_4
:
495 spread_scalar_i4 ((gfc_array_i4
*) ret
, (GFC_INTEGER_4
*) source
,
499 case GFC_DTYPE_LOGICAL_8
:
500 case GFC_DTYPE_INTEGER_8
:
501 spread_scalar_i8 ((gfc_array_i8
*) ret
, (GFC_INTEGER_8
*) source
,
505 #ifdef HAVE_GFC_INTEGER_16
506 case GFC_DTYPE_LOGICAL_16
:
507 case GFC_DTYPE_INTEGER_16
:
508 spread_scalar_i16 ((gfc_array_i16
*) ret
, (GFC_INTEGER_16
*) source
,
513 case GFC_DTYPE_REAL_4
:
514 spread_scalar_r4 ((gfc_array_r4
*) ret
, (GFC_REAL_4
*) source
,
518 case GFC_DTYPE_REAL_8
:
519 spread_scalar_r8 ((gfc_array_r8
*) ret
, (GFC_REAL_8
*) source
,
523 /* FIXME: This here is a hack, which will have to be removed when
524 the array descriptor is reworked. Currently, we don't store the
525 kind value for the type, but only the size. Because on targets with
526 __float128, we have sizeof(logn double) == sizeof(__float128),
527 we cannot discriminate here and have to fall back to the generic
528 handling (which is suboptimal). */
529 #if !defined(GFC_REAL_16_IS_FLOAT128)
530 # ifdef HAVE_GFC_REAL_10
531 case GFC_DTYPE_REAL_10
:
532 spread_scalar_r10 ((gfc_array_r10
*) ret
, (GFC_REAL_10
*) source
,
537 # ifdef HAVE_GFC_REAL_16
538 case GFC_DTYPE_REAL_16
:
539 spread_scalar_r16 ((gfc_array_r16
*) ret
, (GFC_REAL_16
*) source
,
545 case GFC_DTYPE_COMPLEX_4
:
546 spread_scalar_c4 ((gfc_array_c4
*) ret
, (GFC_COMPLEX_4
*) source
,
550 case GFC_DTYPE_COMPLEX_8
:
551 spread_scalar_c8 ((gfc_array_c8
*) ret
, (GFC_COMPLEX_8
*) source
,
555 /* FIXME: This here is a hack, which will have to be removed when
556 the array descriptor is reworked. Currently, we don't store the
557 kind value for the type, but only the size. Because on targets with
558 __float128, we have sizeof(logn double) == sizeof(__float128),
559 we cannot discriminate here and have to fall back to the generic
560 handling (which is suboptimal). */
561 #if !defined(GFC_REAL_16_IS_FLOAT128)
562 # ifdef HAVE_GFC_COMPLEX_10
563 case GFC_DTYPE_COMPLEX_10
:
564 spread_scalar_c10 ((gfc_array_c10
*) ret
, (GFC_COMPLEX_10
*) source
,
569 # ifdef HAVE_GFC_COMPLEX_16
570 case GFC_DTYPE_COMPLEX_16
:
571 spread_scalar_c16 ((gfc_array_c16
*) ret
, (GFC_COMPLEX_16
*) source
,
579 switch (GFC_DESCRIPTOR_SIZE(ret
))
582 spread_scalar_i1 ((gfc_array_i1
*) ret
, (GFC_INTEGER_1
*) source
,
587 if (GFC_UNALIGNED_2(ret
->base_addr
) || GFC_UNALIGNED_2(source
))
591 spread_scalar_i2 ((gfc_array_i2
*) ret
, (GFC_INTEGER_2
*) source
,
597 if (GFC_UNALIGNED_4(ret
->base_addr
) || GFC_UNALIGNED_4(source
))
601 spread_scalar_i4 ((gfc_array_i4
*) ret
, (GFC_INTEGER_4
*) source
,
607 if (GFC_UNALIGNED_8(ret
->base_addr
) || GFC_UNALIGNED_8(source
))
611 spread_scalar_i8 ((gfc_array_i8
*) ret
, (GFC_INTEGER_8
*) source
,
615 #ifdef HAVE_GFC_INTEGER_16
617 if (GFC_UNALIGNED_16(ret
->base_addr
) || GFC_UNALIGNED_16(source
))
621 spread_scalar_i16 ((gfc_array_i16
*) ret
, (GFC_INTEGER_16
*) source
,
630 spread_internal_scalar (ret
, source
, along
, pncopies
);
634 extern void spread_char_scalar (gfc_array_char
*, GFC_INTEGER_4
,
635 const char *, const index_type
*,
636 const index_type
*, GFC_INTEGER_4
);
637 export_proto(spread_char_scalar
);
640 spread_char_scalar (gfc_array_char
*ret
,
641 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
642 const char *source
, const index_type
*along
,
643 const index_type
*pncopies
,
644 GFC_INTEGER_4 source_length
__attribute__((unused
)))
646 if (GFC_DTYPE_IS_UNSET(ret
))
647 runtime_error ("return array missing descriptor in spread()");
648 spread_internal_scalar (ret
, source
, along
, pncopies
);
652 extern void spread_char4_scalar (gfc_array_char
*, GFC_INTEGER_4
,
653 const char *, const index_type
*,
654 const index_type
*, GFC_INTEGER_4
);
655 export_proto(spread_char4_scalar
);
658 spread_char4_scalar (gfc_array_char
*ret
,
659 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
660 const char *source
, const index_type
*along
,
661 const index_type
*pncopies
,
662 GFC_INTEGER_4 source_length
__attribute__((unused
)))
664 if (GFC_DTYPE_IS_UNSET(ret
))
665 runtime_error ("return array missing descriptor in spread()");
666 spread_internal_scalar (ret
, source
, along
, pncopies
);