1 /* Generic implementation of the SPREAD intrinsic
2 Copyright 2002, 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 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
->data
== 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
->data
= internal_malloc_size (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];
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
->data
== NULL
)
248 ret
->data
= internal_malloc_size (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
->data
+ 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
->data
) || GFC_UNALIGNED_2(source
->data
))
381 spread_i2 ((gfc_array_i2
*) ret
, (gfc_array_i2
*) source
,
386 case GFC_DTYPE_DERIVED_4
:
387 if (GFC_UNALIGNED_4(ret
->data
) || GFC_UNALIGNED_4(source
->data
))
391 spread_i4 ((gfc_array_i4
*) ret
, (gfc_array_i4
*) source
,
396 case GFC_DTYPE_DERIVED_8
:
397 if (GFC_UNALIGNED_8(ret
->data
) || GFC_UNALIGNED_8(source
->data
))
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
->data
) || GFC_UNALIGNED_16(source
->data
))
412 spread_i16 ((gfc_array_i16
*) ret
, (gfc_array_i16
*) source
,
419 spread_internal (ret
, source
, along
, pncopies
);
423 extern void spread_char (gfc_array_char
*, GFC_INTEGER_4
,
424 const gfc_array_char
*, const index_type
*,
425 const index_type
*, GFC_INTEGER_4
);
426 export_proto(spread_char
);
429 spread_char (gfc_array_char
*ret
,
430 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
431 const gfc_array_char
*source
, const index_type
*along
,
432 const index_type
*pncopies
,
433 GFC_INTEGER_4 source_length
__attribute__((unused
)))
435 spread_internal (ret
, source
, along
, pncopies
);
439 extern void spread_char4 (gfc_array_char
*, GFC_INTEGER_4
,
440 const gfc_array_char
*, const index_type
*,
441 const index_type
*, GFC_INTEGER_4
);
442 export_proto(spread_char4
);
445 spread_char4 (gfc_array_char
*ret
,
446 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
447 const gfc_array_char
*source
, const index_type
*along
,
448 const index_type
*pncopies
,
449 GFC_INTEGER_4 source_length
__attribute__((unused
)))
451 spread_internal (ret
, source
, along
, pncopies
);
455 /* The following are the prototypes for the versions of spread with a
458 extern void spread_scalar (gfc_array_char
*, const char *,
459 const index_type
*, const index_type
*);
460 export_proto(spread_scalar
);
463 spread_scalar (gfc_array_char
*ret
, const char *source
,
464 const index_type
*along
, const index_type
*pncopies
)
466 index_type type_size
;
469 runtime_error ("return array missing descriptor in spread()");
471 type_size
= GFC_DTYPE_TYPE_SIZE(ret
);
474 case GFC_DTYPE_DERIVED_1
:
475 case GFC_DTYPE_LOGICAL_1
:
476 case GFC_DTYPE_INTEGER_1
:
477 spread_scalar_i1 ((gfc_array_i1
*) ret
, (GFC_INTEGER_1
*) source
,
481 case GFC_DTYPE_LOGICAL_2
:
482 case GFC_DTYPE_INTEGER_2
:
483 spread_scalar_i2 ((gfc_array_i2
*) ret
, (GFC_INTEGER_2
*) source
,
487 case GFC_DTYPE_LOGICAL_4
:
488 case GFC_DTYPE_INTEGER_4
:
489 spread_scalar_i4 ((gfc_array_i4
*) ret
, (GFC_INTEGER_4
*) source
,
493 case GFC_DTYPE_LOGICAL_8
:
494 case GFC_DTYPE_INTEGER_8
:
495 spread_scalar_i8 ((gfc_array_i8
*) ret
, (GFC_INTEGER_8
*) source
,
499 #ifdef HAVE_GFC_INTEGER_16
500 case GFC_DTYPE_LOGICAL_16
:
501 case GFC_DTYPE_INTEGER_16
:
502 spread_scalar_i16 ((gfc_array_i16
*) ret
, (GFC_INTEGER_16
*) source
,
507 case GFC_DTYPE_REAL_4
:
508 spread_scalar_r4 ((gfc_array_r4
*) ret
, (GFC_REAL_4
*) source
,
512 case GFC_DTYPE_REAL_8
:
513 spread_scalar_r8 ((gfc_array_r8
*) ret
, (GFC_REAL_8
*) source
,
517 /* FIXME: This here is a hack, which will have to be removed when
518 the array descriptor is reworked. Currently, we don't store the
519 kind value for the type, but only the size. Because on targets with
520 __float128, we have sizeof(logn double) == sizeof(__float128),
521 we cannot discriminate here and have to fall back to the generic
522 handling (which is suboptimal). */
523 #if !defined(GFC_REAL_16_IS_FLOAT128)
524 # ifdef HAVE_GFC_REAL_10
525 case GFC_DTYPE_REAL_10
:
526 spread_scalar_r10 ((gfc_array_r10
*) ret
, (GFC_REAL_10
*) source
,
531 # ifdef HAVE_GFC_REAL_16
532 case GFC_DTYPE_REAL_16
:
533 spread_scalar_r16 ((gfc_array_r16
*) ret
, (GFC_REAL_16
*) source
,
539 case GFC_DTYPE_COMPLEX_4
:
540 spread_scalar_c4 ((gfc_array_c4
*) ret
, (GFC_COMPLEX_4
*) source
,
544 case GFC_DTYPE_COMPLEX_8
:
545 spread_scalar_c8 ((gfc_array_c8
*) ret
, (GFC_COMPLEX_8
*) source
,
549 /* FIXME: This here is a hack, which will have to be removed when
550 the array descriptor is reworked. Currently, we don't store the
551 kind value for the type, but only the size. Because on targets with
552 __float128, we have sizeof(logn double) == sizeof(__float128),
553 we cannot discriminate here and have to fall back to the generic
554 handling (which is suboptimal). */
555 #if !defined(GFC_REAL_16_IS_FLOAT128)
556 # ifdef HAVE_GFC_COMPLEX_10
557 case GFC_DTYPE_COMPLEX_10
:
558 spread_scalar_c10 ((gfc_array_c10
*) ret
, (GFC_COMPLEX_10
*) source
,
563 # ifdef HAVE_GFC_COMPLEX_16
564 case GFC_DTYPE_COMPLEX_16
:
565 spread_scalar_c16 ((gfc_array_c16
*) ret
, (GFC_COMPLEX_16
*) source
,
571 case GFC_DTYPE_DERIVED_2
:
572 if (GFC_UNALIGNED_2(ret
->data
) || GFC_UNALIGNED_2(source
))
576 spread_scalar_i2 ((gfc_array_i2
*) ret
, (GFC_INTEGER_2
*) source
,
581 case GFC_DTYPE_DERIVED_4
:
582 if (GFC_UNALIGNED_4(ret
->data
) || GFC_UNALIGNED_4(source
))
586 spread_scalar_i4 ((gfc_array_i4
*) ret
, (GFC_INTEGER_4
*) source
,
591 case GFC_DTYPE_DERIVED_8
:
592 if (GFC_UNALIGNED_8(ret
->data
) || GFC_UNALIGNED_8(source
))
596 spread_scalar_i8 ((gfc_array_i8
*) ret
, (GFC_INTEGER_8
*) source
,
600 #ifdef HAVE_GFC_INTEGER_16
601 case GFC_DTYPE_DERIVED_16
:
602 if (GFC_UNALIGNED_16(ret
->data
) || GFC_UNALIGNED_16(source
))
606 spread_scalar_i16 ((gfc_array_i16
*) ret
, (GFC_INTEGER_16
*) source
,
613 spread_internal_scalar (ret
, source
, along
, pncopies
);
617 extern void spread_char_scalar (gfc_array_char
*, GFC_INTEGER_4
,
618 const char *, const index_type
*,
619 const index_type
*, GFC_INTEGER_4
);
620 export_proto(spread_char_scalar
);
623 spread_char_scalar (gfc_array_char
*ret
,
624 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
625 const char *source
, const index_type
*along
,
626 const index_type
*pncopies
,
627 GFC_INTEGER_4 source_length
__attribute__((unused
)))
630 runtime_error ("return array missing descriptor in spread()");
631 spread_internal_scalar (ret
, source
, along
, pncopies
);
635 extern void spread_char4_scalar (gfc_array_char
*, GFC_INTEGER_4
,
636 const char *, const index_type
*,
637 const index_type
*, GFC_INTEGER_4
);
638 export_proto(spread_char4_scalar
);
641 spread_char4_scalar (gfc_array_char
*ret
,
642 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
643 const char *source
, const index_type
*along
,
644 const index_type
*pncopies
,
645 GFC_INTEGER_4 source_length
__attribute__((unused
)))
648 runtime_error ("return array missing descriptor in spread()");
649 spread_internal_scalar (ret
, source
, along
, pncopies
);