1 /* Generic implementation of the SPREAD intrinsic
2 Copyright 2002, 2005, 2006 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. */
35 #include "libgfortran.h"
38 spread_internal (gfc_array_char
*ret
, const gfc_array_char
*source
,
39 const index_type
*along
, const index_type
*pncopies
,
42 /* r.* indicates the return array. */
43 index_type rstride
[GFC_MAX_DIMENSIONS
];
45 index_type rdelta
= 0;
50 /* s.* indicates the source array. */
51 index_type sstride
[GFC_MAX_DIMENSIONS
];
56 index_type count
[GFC_MAX_DIMENSIONS
];
57 index_type extent
[GFC_MAX_DIMENSIONS
];
62 srank
= GFC_DESCRIPTOR_RANK(source
);
65 if (rrank
> GFC_MAX_DIMENSIONS
)
66 runtime_error ("return rank too large in spread()");
69 runtime_error ("dim outside of rank in spread()");
73 if (ret
->data
== NULL
)
75 /* The front end has signalled that we need to populate the
76 return array descriptor. */
77 ret
->dtype
= (source
->dtype
& ~GFC_DTYPE_RANK_MASK
) | rrank
;
80 for (n
= 0; n
< rrank
; n
++)
82 ret
->dim
[n
].stride
= rs
;
83 ret
->dim
[n
].lbound
= 0;
86 ret
->dim
[n
].ubound
= ncopies
- 1;
93 extent
[dim
] = source
->dim
[dim
].ubound
+ 1
94 - source
->dim
[dim
].lbound
;
95 sstride
[dim
] = source
->dim
[dim
].stride
* size
;
96 rstride
[dim
] = rs
* size
;
98 ret
->dim
[n
].ubound
= extent
[dim
]-1;
104 ret
->data
= internal_malloc_size (rs
* size
);
109 if (GFC_DESCRIPTOR_RANK(ret
) != rrank
)
110 runtime_error ("rank mismatch in spread()");
112 for (n
= 0; n
< rrank
; n
++)
116 rdelta
= ret
->dim
[n
].stride
* size
;
121 extent
[dim
] = source
->dim
[dim
].ubound
+ 1
122 - source
->dim
[dim
].lbound
;
123 sstride
[dim
] = source
->dim
[dim
].stride
* size
;
124 rstride
[dim
] = ret
->dim
[n
].stride
* size
;
131 sstride0
= sstride
[0];
132 rstride0
= rstride
[0];
138 /* Spread this element. */
140 for (n
= 0; n
< ncopies
; n
++)
142 memcpy (dest
, sptr
, size
);
145 /* Advance to the next element. */
150 while (count
[n
] == extent
[n
])
152 /* When we get to the end of a dimension, reset it and increment
153 the next dimension. */
155 /* We could precalculate these products, but this is a less
156 frequently used path so probably not worth it. */
157 sptr
-= sstride
[n
] * extent
[n
];
158 rptr
-= rstride
[n
] * extent
[n
];
162 /* Break out of the loop. */
176 /* This version of spread_internal treats the special case of a scalar
177 source. This is much simpler than the more general case above. */
180 spread_internal_scalar (gfc_array_char
*ret
, const char *source
,
181 const index_type
*along
, const index_type
*pncopies
,
185 int ncopies
= *pncopies
;
188 if (GFC_DESCRIPTOR_RANK (ret
) != 1)
189 runtime_error ("incorrect destination rank in spread()");
192 runtime_error ("dim outside of rank in spread()");
194 if (ret
->data
== NULL
)
196 ret
->data
= internal_malloc_size (ncopies
* size
);
198 ret
->dim
[0].stride
= 1;
199 ret
->dim
[0].lbound
= 0;
200 ret
->dim
[0].ubound
= ncopies
- 1;
204 if (ncopies
- 1 > (ret
->dim
[0].ubound
- ret
->dim
[0].lbound
)
205 / ret
->dim
[0].stride
)
206 runtime_error ("dim too large in spread()");
209 for (n
= 0; n
< ncopies
; n
++)
211 dest
= (char*)(ret
->data
+ n
*size
*ret
->dim
[0].stride
);
212 memcpy (dest
, source
, size
);
216 extern void spread (gfc_array_char
*, const gfc_array_char
*,
217 const index_type
*, const index_type
*);
218 export_proto(spread
);
221 spread (gfc_array_char
*ret
, const gfc_array_char
*source
,
222 const index_type
*along
, const index_type
*pncopies
)
224 spread_internal (ret
, source
, along
, pncopies
, GFC_DESCRIPTOR_SIZE (source
));
227 extern void spread_char (gfc_array_char
*, GFC_INTEGER_4
,
228 const gfc_array_char
*, const index_type
*,
229 const index_type
*, GFC_INTEGER_4
);
230 export_proto(spread_char
);
233 spread_char (gfc_array_char
*ret
,
234 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
235 const gfc_array_char
*source
, const index_type
*along
,
236 const index_type
*pncopies
, GFC_INTEGER_4 source_length
)
238 spread_internal (ret
, source
, along
, pncopies
, source_length
);
241 /* The following are the prototypes for the versions of spread with a
244 extern void spread_scalar (gfc_array_char
*, const char *,
245 const index_type
*, const index_type
*);
246 export_proto(spread_scalar
);
249 spread_scalar (gfc_array_char
*ret
, const char *source
,
250 const index_type
*along
, const index_type
*pncopies
)
253 runtime_error ("return array missing descriptor in spread()");
254 spread_internal_scalar (ret
, source
, along
, pncopies
, GFC_DESCRIPTOR_SIZE (ret
));
258 extern void spread_char_scalar (gfc_array_char
*, GFC_INTEGER_4
,
259 const char *, const index_type
*,
260 const index_type
*, GFC_INTEGER_4
);
261 export_proto(spread_char_scalar
);
264 spread_char_scalar (gfc_array_char
*ret
,
265 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
266 const char *source
, const index_type
*along
,
267 const index_type
*pncopies
, GFC_INTEGER_4 source_length
)
270 runtime_error ("return array missing descriptor in spread()");
271 spread_internal_scalar (ret
, source
, along
, pncopies
, source_length
);