1 `/* Special implementation of the SPREAD intrinsic
2 Copyright 2008, 2009 Free Software Foundation, Inc.
3 Contributed by Thomas Koenig <tkoenig@gcc.gnu.org>, based on
4 spread_generic.c written by Paul Brook <paul@nowt.org>
6 This file is part of the GNU Fortran 95 runtime library (libgfortran).
8 Libgfortran is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public
10 License as published by the Free Software Foundation; either
11 version 3 of the License, or (at your option) any later version.
13 Ligbfortran is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 #include "libgfortran.h"
34 `#if defined (HAVE_'rtype_name`)
37 spread_'rtype_code` ('rtype` *ret, const 'rtype` *source,
38 const index_type along, const index_type pncopies)
40 /* r.* indicates the return array. */
41 index_type rstride[GFC_MAX_DIMENSIONS];
43 index_type rdelta = 0;
47 'rtype_name` * restrict dest;
48 /* s.* indicates the source array. */
49 index_type sstride[GFC_MAX_DIMENSIONS];
52 const 'rtype_name` *sptr;
54 index_type count[GFC_MAX_DIMENSIONS];
55 index_type extent[GFC_MAX_DIMENSIONS];
60 srank = GFC_DESCRIPTOR_RANK(source);
63 if (rrank > GFC_MAX_DIMENSIONS)
64 runtime_error ("return rank too large in spread()");
67 runtime_error ("dim outside of rank in spread()");
71 if (ret->data == NULL)
76 /* The front end has signalled that we need to populate the
77 return array descriptor. */
78 ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rrank;
81 for (n = 0; n < rrank; n++)
93 extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
94 sstride[dim] = GFC_DESCRIPTOR_STRIDE(source,dim);
101 GFC_DIMENSION_SET(ret->dim[n], 0, ub, stride);
105 ret->data = internal_malloc_size (rs * sizeof('rtype_name`));
108 ret->data = internal_malloc_size (1);
119 if (GFC_DESCRIPTOR_RANK(ret) != rrank)
120 runtime_error ("rank mismatch in spread()");
122 if (unlikely (compile_options.bounds_check))
124 for (n = 0; n < rrank; n++)
126 index_type ret_extent;
128 ret_extent = GFC_DESCRIPTOR_EXTENT(ret,n);
131 rdelta = GFC_DESCRIPTOR_STRIDE(ret,n);
133 if (ret_extent != ncopies)
134 runtime_error("Incorrect extent in return value of SPREAD"
135 " intrinsic in dimension %ld: is %ld,"
136 " should be %ld", (long int) n+1,
137 (long int) ret_extent, (long int) ncopies);
142 extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
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] = GFC_DESCRIPTOR_STRIDE(source,dim);
153 rstride[dim] = GFC_DESCRIPTOR_STRIDE(ret,n);
160 for (n = 0; n < rrank; n++)
164 rdelta = GFC_DESCRIPTOR_STRIDE(ret,n);
169 extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
170 if (extent[dim] <= 0)
172 sstride[dim] = GFC_DESCRIPTOR_STRIDE(source,dim);
173 rstride[dim] = GFC_DESCRIPTOR_STRIDE(ret,n);
185 sstride0 = sstride[0];
186 rstride0 = rstride[0];
192 /* Spread this element. */
194 for (n = 0; n < ncopies; n++)
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_scalar_'rtype_code` ('rtype` *ret, const 'rtype_name` *source,
235 const index_type along, const index_type pncopies)
238 int ncopies = pncopies;
239 'rtype_name` * restrict dest;
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 * sizeof ('rtype_name`));
252 GFC_DIMENSION_SET(ret->dim[0], 0, ncopies - 1, 1);
256 if (ncopies - 1 > (GFC_DESCRIPTOR_EXTENT(ret,0) - 1)
257 / GFC_DESCRIPTOR_STRIDE(ret,0))
258 runtime_error ("dim too large in spread()");
262 stride = GFC_DESCRIPTOR_STRIDE(ret,0);
264 for (n = 0; n < ncopies; n++)