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 %d: is %ld,"
135 " should be %ld", n+1, (long int) ret_extent,
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 %d: is %ld,"
146 " should be %ld", n+1, (long int) ret_extent,
147 (long int) extent[dim]);
149 if (extent[dim] <= 0)
151 sstride[dim] = source->dim[dim].stride * size;
152 rstride[dim] = ret->dim[n].stride * size;
159 for (n = 0; n < rrank; n++)
163 rdelta = ret->dim[n].stride * size;
168 extent[dim] = source->dim[dim].ubound + 1
169 - source->dim[dim].lbound;
170 if (extent[dim] <= 0)
172 sstride[dim] = source->dim[dim].stride * size;
173 rstride[dim] = ret->dim[n].stride * size;
185 sstride0 = sstride[0];
186 rstride0 = rstride[0];
192 /* Spread this element. */
194 for (n = 0; n < ncopies; n++)
196 memcpy (dest, sptr, size);
199 /* Advance to the next element. */
204 while (count[n] == extent[n])
206 /* When we get to the end of a dimension, reset it and increment
207 the next dimension. */
209 /* We could precalculate these products, but this is a less
210 frequently used path so probably not worth it. */
211 sptr -= sstride[n] * extent[n];
212 rptr -= rstride[n] * extent[n];
216 /* Break out of the loop. */
230 /* This version of spread_internal treats the special case of a scalar
231 source. This is much simpler than the more general case above. */
234 spread_internal_scalar (gfc_array_char *ret, const char *source,
235 const index_type *along, const index_type *pncopies,
239 int ncopies = *pncopies;
242 if (GFC_DESCRIPTOR_RANK (ret) != 1)
243 runtime_error ("incorrect destination rank in spread()");
246 runtime_error ("dim outside of rank in spread()");
248 if (ret->data == NULL)
250 ret->data = internal_malloc_size (ncopies * size);
252 ret->dim[0].stride = 1;
253 ret->dim[0].lbound = 0;
254 ret->dim[0].ubound = ncopies - 1;
258 if (ncopies - 1 > (ret->dim[0].ubound - ret->dim[0].lbound)
259 / ret->dim[0].stride)
260 runtime_error ("dim too large in spread()");
263 for (n = 0; n < ncopies; n++)
265 dest = (char*)(ret->data + n*size*ret->dim[0].stride);
266 memcpy (dest , source, size);
270 extern void spread (gfc_array_char *, const gfc_array_char *,
271 const index_type *, const index_type *);
272 export_proto(spread);
275 spread (gfc_array_char *ret, const gfc_array_char *source,
276 const index_type *along, const index_type *pncopies)
278 spread_internal (ret, source, along, pncopies, GFC_DESCRIPTOR_SIZE (source));
281 extern void spread_char (gfc_array_char *, GFC_INTEGER_4,
282 const gfc_array_char *, const index_type *,
283 const index_type *, GFC_INTEGER_4);
284 export_proto(spread_char);
287 spread_char (gfc_array_char *ret,
288 GFC_INTEGER_4 ret_length __attribute__((unused)),
289 const gfc_array_char *source, const index_type *along,
290 const index_type *pncopies, GFC_INTEGER_4 source_length)
292 spread_internal (ret, source, along, pncopies, source_length);
295 /* The following are the prototypes for the versions of spread with a
298 extern void spread_scalar (gfc_array_char *, const char *,
299 const index_type *, const index_type *);
300 export_proto(spread_scalar);
303 spread_scalar (gfc_array_char *ret, const char *source,
304 const index_type *along, const index_type *pncopies)
307 runtime_error ("return array missing descriptor in spread()");
308 spread_internal_scalar (ret, source, along, pncopies, GFC_DESCRIPTOR_SIZE (ret));
312 extern void spread_char_scalar (gfc_array_char *, GFC_INTEGER_4,
313 const char *, const index_type *,
314 const index_type *, GFC_INTEGER_4);
315 export_proto(spread_char_scalar);
318 spread_char_scalar (gfc_array_char *ret,
319 GFC_INTEGER_4 ret_length __attribute__((unused)),
320 const char *source, const index_type *along,
321 const index_type *pncopies, GFC_INTEGER_4 source_length)
324 runtime_error ("return array missing descriptor in spread()");
325 spread_internal_scalar (ret, source, along, pncopies, source_length);