1 /* Generic implementation of the SPREAD intrinsic
2 Copyright 2002, 2005, 2006, 2007 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 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Ligbfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
31 #include "libgfortran.h"
37 spread_internal (gfc_array_char
*ret
, const gfc_array_char
*source
,
38 const index_type
*along
, const index_type
*pncopies
,
41 /* r.* indicates the return array. */
42 index_type rstride
[GFC_MAX_DIMENSIONS
];
44 index_type rdelta
= 0;
49 /* s.* indicates the source array. */
50 index_type sstride
[GFC_MAX_DIMENSIONS
];
55 index_type count
[GFC_MAX_DIMENSIONS
];
56 index_type extent
[GFC_MAX_DIMENSIONS
];
61 srank
= GFC_DESCRIPTOR_RANK(source
);
64 if (rrank
> GFC_MAX_DIMENSIONS
)
65 runtime_error ("return rank too large in spread()");
68 runtime_error ("dim outside of rank in spread()");
72 if (ret
->data
== NULL
)
74 /* The front end has signalled that we need to populate the
75 return array descriptor. */
76 ret
->dtype
= (source
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rrank
;
79 for (n
= 0; n
< rrank
; n
++)
81 ret
->dim
[n
].stride
= rs
;
82 ret
->dim
[n
].lbound
= 0;
85 ret
->dim
[n
].ubound
= ncopies
- 1;
92 extent
[dim
] = source
->dim
[dim
].ubound
+ 1
93 - source
->dim
[dim
].lbound
;
94 sstride
[dim
] = source
->dim
[dim
].stride
* size
;
95 rstride
[dim
] = rs
* size
;
97 ret
->dim
[n
].ubound
= extent
[dim
]-1;
104 ret
->data
= internal_malloc_size (rs
* size
);
107 ret
->data
= internal_malloc_size (1);
118 if (GFC_DESCRIPTOR_RANK(ret
) != rrank
)
119 runtime_error ("rank mismatch in spread()");
121 if (compile_options
.bounds_check
)
123 for (n
= 0; n
< rrank
; n
++)
125 index_type ret_extent
;
127 ret_extent
= ret
->dim
[n
].ubound
+ 1 - ret
->dim
[n
].lbound
;
130 rdelta
= ret
->dim
[n
].stride
* size
;
132 if (ret_extent
!= ncopies
)
133 runtime_error("Incorrect extent in return value of SPREAD"
134 " intrinsic in dimension %ld: is %ld,"
135 " should be %ld", (long int) n
+1,
136 (long int) ret_extent
, (long int) ncopies
);
141 extent
[dim
] = source
->dim
[dim
].ubound
+ 1
142 - source
->dim
[dim
].lbound
;
143 if (ret_extent
!= extent
[dim
])
144 runtime_error("Incorrect extent in return value of SPREAD"
145 " intrinsic in dimension %ld: is %ld,"
146 " should be %ld", (long int) n
+1,
147 (long int) ret_extent
,
148 (long int) extent
[dim
]);
150 if (extent
[dim
] <= 0)
152 sstride
[dim
] = source
->dim
[dim
].stride
* size
;
153 rstride
[dim
] = ret
->dim
[n
].stride
* size
;
160 for (n
= 0; n
< rrank
; n
++)
164 rdelta
= ret
->dim
[n
].stride
* size
;
169 extent
[dim
] = source
->dim
[dim
].ubound
+ 1
170 - source
->dim
[dim
].lbound
;
171 if (extent
[dim
] <= 0)
173 sstride
[dim
] = source
->dim
[dim
].stride
* size
;
174 rstride
[dim
] = ret
->dim
[n
].stride
* size
;
186 sstride0
= sstride
[0];
187 rstride0
= rstride
[0];
193 /* Spread this element. */
195 for (n
= 0; n
< ncopies
; n
++)
197 memcpy (dest
, sptr
, size
);
200 /* Advance to the next element. */
205 while (count
[n
] == extent
[n
])
207 /* When we get to the end of a dimension, reset it and increment
208 the next dimension. */
210 /* We could precalculate these products, but this is a less
211 frequently used path so probably not worth it. */
212 sptr
-= sstride
[n
] * extent
[n
];
213 rptr
-= rstride
[n
] * extent
[n
];
217 /* Break out of the loop. */
231 /* This version of spread_internal treats the special case of a scalar
232 source. This is much simpler than the more general case above. */
235 spread_internal_scalar (gfc_array_char
*ret
, const char *source
,
236 const index_type
*along
, const index_type
*pncopies
,
240 int ncopies
= *pncopies
;
243 if (GFC_DESCRIPTOR_RANK (ret
) != 1)
244 runtime_error ("incorrect destination rank in spread()");
247 runtime_error ("dim outside of rank in spread()");
249 if (ret
->data
== NULL
)
251 ret
->data
= internal_malloc_size (ncopies
* size
);
253 ret
->dim
[0].stride
= 1;
254 ret
->dim
[0].lbound
= 0;
255 ret
->dim
[0].ubound
= ncopies
- 1;
259 if (ncopies
- 1 > (ret
->dim
[0].ubound
- ret
->dim
[0].lbound
)
260 / ret
->dim
[0].stride
)
261 runtime_error ("dim too large in spread()");
264 for (n
= 0; n
< ncopies
; n
++)
266 dest
= (char*)(ret
->data
+ n
*size
*ret
->dim
[0].stride
);
267 memcpy (dest
, source
, size
);
271 extern void spread (gfc_array_char
*, const gfc_array_char
*,
272 const index_type
*, const index_type
*);
273 export_proto(spread
);
276 spread (gfc_array_char
*ret
, const gfc_array_char
*source
,
277 const index_type
*along
, const index_type
*pncopies
)
279 index_type type_size
;
281 type_size
= GFC_DTYPE_TYPE_SIZE(ret
);
284 case GFC_DTYPE_DERIVED_1
:
285 case GFC_DTYPE_LOGICAL_1
:
286 case GFC_DTYPE_INTEGER_1
:
287 spread_i1 ((gfc_array_i1
*) ret
, (gfc_array_i1
*) source
,
291 case GFC_DTYPE_LOGICAL_2
:
292 case GFC_DTYPE_INTEGER_2
:
293 spread_i2 ((gfc_array_i2
*) ret
, (gfc_array_i2
*) source
,
297 case GFC_DTYPE_LOGICAL_4
:
298 case GFC_DTYPE_INTEGER_4
:
299 spread_i4 ((gfc_array_i4
*) ret
, (gfc_array_i4
*) source
,
303 case GFC_DTYPE_LOGICAL_8
:
304 case GFC_DTYPE_INTEGER_8
:
305 spread_i8 ((gfc_array_i8
*) ret
, (gfc_array_i8
*) source
,
309 #ifdef HAVE_GFC_INTEGER_16
310 case GFC_DTYPE_LOGICAL_16
:
311 case GFC_DTYPE_INTEGER_16
:
312 spread_i16 ((gfc_array_i16
*) ret
, (gfc_array_i16
*) source
,
317 case GFC_DTYPE_REAL_4
:
318 spread_r4 ((gfc_array_r4
*) ret
, (gfc_array_r4
*) source
,
322 case GFC_DTYPE_REAL_8
:
323 spread_r8 ((gfc_array_r8
*) ret
, (gfc_array_r8
*) source
,
327 #ifdef GFC_HAVE_REAL_10
328 case GFC_DTYPE_REAL_10
:
329 spread_r10 ((gfc_array_r10
*) ret
, (gfc_array_r10
*) source
,
334 #ifdef GFC_HAVE_REAL_16
335 case GFC_DTYPE_REAL_16
:
336 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 #ifdef GFC_HAVE_COMPLEX_10
352 case GFC_DTYPE_COMPLEX_10
:
353 spread_c10 ((gfc_array_c10
*) ret
, (gfc_array_c10
*) source
,
358 #ifdef GFC_HAVE_COMPLEX_16
359 case GFC_DTYPE_COMPLEX_16
:
360 spread_c16 ((gfc_array_c16
*) ret
, (gfc_array_c16
*) source
,
365 case GFC_DTYPE_DERIVED_2
:
366 if (GFC_UNALIGNED_2(ret
->data
) || GFC_UNALIGNED_2(source
->data
))
370 spread_i2 ((gfc_array_i2
*) ret
, (gfc_array_i2
*) source
,
375 case GFC_DTYPE_DERIVED_4
:
376 if (GFC_UNALIGNED_4(ret
->data
) || GFC_UNALIGNED_4(source
->data
))
380 spread_i4 ((gfc_array_i4
*) ret
, (gfc_array_i4
*) source
,
385 case GFC_DTYPE_DERIVED_8
:
386 if (GFC_UNALIGNED_8(ret
->data
) || GFC_UNALIGNED_8(source
->data
))
390 spread_i8 ((gfc_array_i8
*) ret
, (gfc_array_i8
*) source
,
395 #ifdef HAVE_GFC_INTEGER_16
396 case GFC_DTYPE_DERIVED_16
:
397 if (GFC_UNALIGNED_16(ret
->data
) || GFC_UNALIGNED_16(source
->data
))
401 spread_i16 ((gfc_array_i16
*) ret
, (gfc_array_i16
*) source
,
408 spread_internal (ret
, source
, along
, pncopies
, GFC_DESCRIPTOR_SIZE (source
));
412 extern void spread_char (gfc_array_char
*, GFC_INTEGER_4
,
413 const gfc_array_char
*, const index_type
*,
414 const index_type
*, GFC_INTEGER_4
);
415 export_proto(spread_char
);
418 spread_char (gfc_array_char
*ret
,
419 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
420 const gfc_array_char
*source
, const index_type
*along
,
421 const index_type
*pncopies
, GFC_INTEGER_4 source_length
)
423 spread_internal (ret
, source
, along
, pncopies
, source_length
);
427 extern void spread_char4 (gfc_array_char
*, GFC_INTEGER_4
,
428 const gfc_array_char
*, const index_type
*,
429 const index_type
*, GFC_INTEGER_4
);
430 export_proto(spread_char4
);
433 spread_char4 (gfc_array_char
*ret
,
434 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
435 const gfc_array_char
*source
, const index_type
*along
,
436 const index_type
*pncopies
, GFC_INTEGER_4 source_length
)
438 spread_internal (ret
, source
, along
, pncopies
,
439 source_length
* sizeof (gfc_char4_t
));
443 /* The following are the prototypes for the versions of spread with a
446 extern void spread_scalar (gfc_array_char
*, const char *,
447 const index_type
*, const index_type
*);
448 export_proto(spread_scalar
);
451 spread_scalar (gfc_array_char
*ret
, const char *source
,
452 const index_type
*along
, const index_type
*pncopies
)
454 index_type type_size
;
457 runtime_error ("return array missing descriptor in spread()");
459 type_size
= GFC_DTYPE_TYPE_SIZE(ret
);
462 case GFC_DTYPE_DERIVED_1
:
463 case GFC_DTYPE_LOGICAL_1
:
464 case GFC_DTYPE_INTEGER_1
:
465 spread_scalar_i1 ((gfc_array_i1
*) ret
, (GFC_INTEGER_1
*) source
,
469 case GFC_DTYPE_LOGICAL_2
:
470 case GFC_DTYPE_INTEGER_2
:
471 spread_scalar_i2 ((gfc_array_i2
*) ret
, (GFC_INTEGER_2
*) source
,
475 case GFC_DTYPE_LOGICAL_4
:
476 case GFC_DTYPE_INTEGER_4
:
477 spread_scalar_i4 ((gfc_array_i4
*) ret
, (GFC_INTEGER_4
*) source
,
481 case GFC_DTYPE_LOGICAL_8
:
482 case GFC_DTYPE_INTEGER_8
:
483 spread_scalar_i8 ((gfc_array_i8
*) ret
, (GFC_INTEGER_8
*) source
,
487 #ifdef HAVE_GFC_INTEGER_16
488 case GFC_DTYPE_LOGICAL_16
:
489 case GFC_DTYPE_INTEGER_16
:
490 spread_scalar_i16 ((gfc_array_i16
*) ret
, (GFC_INTEGER_16
*) source
,
495 case GFC_DTYPE_REAL_4
:
496 spread_scalar_r4 ((gfc_array_r4
*) ret
, (GFC_REAL_4
*) source
,
500 case GFC_DTYPE_REAL_8
:
501 spread_scalar_r8 ((gfc_array_r8
*) ret
, (GFC_REAL_8
*) source
,
505 #ifdef HAVE_GFC_REAL_10
506 case GFC_DTYPE_REAL_10
:
507 spread_scalar_r10 ((gfc_array_r10
*) ret
, (GFC_REAL_10
*) source
,
512 #ifdef HAVE_GFC_REAL_16
513 case GFC_DTYPE_REAL_16
:
514 spread_scalar_r16 ((gfc_array_r16
*) ret
, (GFC_REAL_16
*) source
,
519 case GFC_DTYPE_COMPLEX_4
:
520 spread_scalar_c4 ((gfc_array_c4
*) ret
, (GFC_COMPLEX_4
*) source
,
524 case GFC_DTYPE_COMPLEX_8
:
525 spread_scalar_c8 ((gfc_array_c8
*) ret
, (GFC_COMPLEX_8
*) source
,
529 #ifdef HAVE_GFC_COMPLEX_10
530 case GFC_DTYPE_COMPLEX_10
:
531 spread_scalar_c10 ((gfc_array_c10
*) ret
, (GFC_COMPLEX_10
*) source
,
536 #ifdef HAVE_GFC_COMPLEX_16
537 case GFC_DTYPE_COMPLEX_16
:
538 spread_scalar_c16 ((gfc_array_c16
*) ret
, (GFC_COMPLEX_16
*) source
,
543 case GFC_DTYPE_DERIVED_2
:
544 if (GFC_UNALIGNED_2(ret
->data
) || GFC_UNALIGNED_2(source
))
548 spread_scalar_i2 ((gfc_array_i2
*) ret
, (GFC_INTEGER_2
*) source
,
553 case GFC_DTYPE_DERIVED_4
:
554 if (GFC_UNALIGNED_4(ret
->data
) || GFC_UNALIGNED_4(source
))
558 spread_scalar_i4 ((gfc_array_i4
*) ret
, (GFC_INTEGER_4
*) source
,
563 case GFC_DTYPE_DERIVED_8
:
564 if (GFC_UNALIGNED_8(ret
->data
) || GFC_UNALIGNED_8(source
))
568 spread_scalar_i8 ((gfc_array_i8
*) ret
, (GFC_INTEGER_8
*) source
,
572 #ifdef HAVE_GFC_INTEGER_16
573 case GFC_DTYPE_DERIVED_16
:
574 if (GFC_UNALIGNED_16(ret
->data
) || GFC_UNALIGNED_16(source
))
578 spread_scalar_i16 ((gfc_array_i16
*) ret
, (GFC_INTEGER_16
*) source
,
585 spread_internal_scalar (ret
, source
, along
, pncopies
, GFC_DESCRIPTOR_SIZE (ret
));
589 extern void spread_char_scalar (gfc_array_char
*, GFC_INTEGER_4
,
590 const char *, const index_type
*,
591 const index_type
*, GFC_INTEGER_4
);
592 export_proto(spread_char_scalar
);
595 spread_char_scalar (gfc_array_char
*ret
,
596 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
597 const char *source
, const index_type
*along
,
598 const index_type
*pncopies
, GFC_INTEGER_4 source_length
)
601 runtime_error ("return array missing descriptor in spread()");
602 spread_internal_scalar (ret
, source
, along
, pncopies
, source_length
);
606 extern void spread_char4_scalar (gfc_array_char
*, GFC_INTEGER_4
,
607 const char *, const index_type
*,
608 const index_type
*, GFC_INTEGER_4
);
609 export_proto(spread_char4_scalar
);
612 spread_char4_scalar (gfc_array_char
*ret
,
613 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
614 const char *source
, const index_type
*along
,
615 const index_type
*pncopies
, GFC_INTEGER_4 source_length
)
618 runtime_error ("return array missing descriptor in spread()");
619 spread_internal_scalar (ret
, source
, along
, pncopies
,
620 source_length
* sizeof (gfc_char4_t
));