Daily bump.
[official-gcc.git] / libgfortran / generated / spread_r8.c
blob98bac832aa81878ffe6979e5c7322ef2af16eb0b
1 /* Special implementation of the SPREAD intrinsic
2 Copyright (C) 2008-2015 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 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"
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <string.h>
33 #if defined (HAVE_GFC_REAL_8)
35 void
36 spread_r8 (gfc_array_r8 *ret, const gfc_array_r8 *source,
37 const index_type along, const index_type pncopies)
39 /* r.* indicates the return array. */
40 index_type rstride[GFC_MAX_DIMENSIONS];
41 index_type rstride0;
42 index_type rdelta = 0;
43 index_type rrank;
44 index_type rs;
45 GFC_REAL_8 *rptr;
46 GFC_REAL_8 * restrict dest;
47 /* s.* indicates the source array. */
48 index_type sstride[GFC_MAX_DIMENSIONS];
49 index_type sstride0;
50 index_type srank;
51 const GFC_REAL_8 *sptr;
53 index_type count[GFC_MAX_DIMENSIONS];
54 index_type extent[GFC_MAX_DIMENSIONS];
55 index_type n;
56 index_type dim;
57 index_type ncopies;
59 srank = GFC_DESCRIPTOR_RANK(source);
61 rrank = srank + 1;
62 if (rrank > GFC_MAX_DIMENSIONS)
63 runtime_error ("return rank too large in spread()");
65 if (along > rrank)
66 runtime_error ("dim outside of rank in spread()");
68 ncopies = pncopies;
70 if (ret->base_addr == NULL)
73 size_t ub, stride;
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;
78 dim = 0;
79 rs = 1;
80 for (n = 0; n < rrank; n++)
82 stride = rs;
83 if (n == along - 1)
85 ub = ncopies - 1;
86 rdelta = rs;
87 rs *= ncopies;
89 else
91 count[dim] = 0;
92 extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
93 sstride[dim] = GFC_DESCRIPTOR_STRIDE(source,dim);
94 rstride[dim] = rs;
96 ub = extent[dim] - 1;
97 rs *= extent[dim];
98 dim++;
100 GFC_DIMENSION_SET(ret->dim[n], 0, ub, stride);
102 ret->offset = 0;
104 /* xmallocarray allocates a single byte for zero size. */
105 ret->base_addr = xmallocarray (rs, sizeof(GFC_REAL_8));
106 if (rs <= 0)
107 return;
109 else
111 int zero_sized;
113 zero_sized = 0;
115 dim = 0;
116 if (GFC_DESCRIPTOR_RANK(ret) != rrank)
117 runtime_error ("rank mismatch in spread()");
119 if (unlikely (compile_options.bounds_check))
121 for (n = 0; n < rrank; n++)
123 index_type ret_extent;
125 ret_extent = GFC_DESCRIPTOR_EXTENT(ret,n);
126 if (n == along - 1)
128 rdelta = GFC_DESCRIPTOR_STRIDE(ret,n);
130 if (ret_extent != ncopies)
131 runtime_error("Incorrect extent in return value of SPREAD"
132 " intrinsic in dimension %ld: is %ld,"
133 " should be %ld", (long int) n+1,
134 (long int) ret_extent, (long int) ncopies);
136 else
138 count[dim] = 0;
139 extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
140 if (ret_extent != extent[dim])
141 runtime_error("Incorrect extent in return value of SPREAD"
142 " intrinsic in dimension %ld: is %ld,"
143 " should be %ld", (long int) n+1,
144 (long int) ret_extent,
145 (long int) extent[dim]);
147 if (extent[dim] <= 0)
148 zero_sized = 1;
149 sstride[dim] = GFC_DESCRIPTOR_STRIDE(source,dim);
150 rstride[dim] = GFC_DESCRIPTOR_STRIDE(ret,n);
151 dim++;
155 else
157 for (n = 0; n < rrank; n++)
159 if (n == along - 1)
161 rdelta = GFC_DESCRIPTOR_STRIDE(ret,n);
163 else
165 count[dim] = 0;
166 extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
167 if (extent[dim] <= 0)
168 zero_sized = 1;
169 sstride[dim] = GFC_DESCRIPTOR_STRIDE(source,dim);
170 rstride[dim] = GFC_DESCRIPTOR_STRIDE(ret,n);
171 dim++;
176 if (zero_sized)
177 return;
179 if (sstride[0] == 0)
180 sstride[0] = 1;
182 sstride0 = sstride[0];
183 rstride0 = rstride[0];
184 rptr = ret->base_addr;
185 sptr = source->base_addr;
187 while (sptr)
189 /* Spread this element. */
190 dest = rptr;
191 for (n = 0; n < ncopies; n++)
193 *dest = *sptr;
194 dest += rdelta;
196 /* Advance to the next element. */
197 sptr += sstride0;
198 rptr += rstride0;
199 count[0]++;
200 n = 0;
201 while (count[n] == extent[n])
203 /* When we get to the end of a dimension, reset it and increment
204 the next dimension. */
205 count[n] = 0;
206 /* We could precalculate these products, but this is a less
207 frequently used path so probably not worth it. */
208 sptr -= sstride[n] * extent[n];
209 rptr -= rstride[n] * extent[n];
210 n++;
211 if (n >= srank)
213 /* Break out of the loop. */
214 sptr = NULL;
215 break;
217 else
219 count[n]++;
220 sptr += sstride[n];
221 rptr += rstride[n];
227 /* This version of spread_internal treats the special case of a scalar
228 source. This is much simpler than the more general case above. */
230 void
231 spread_scalar_r8 (gfc_array_r8 *ret, const GFC_REAL_8 *source,
232 const index_type along, const index_type pncopies)
234 int n;
235 int ncopies = pncopies;
236 GFC_REAL_8 * restrict dest;
237 index_type stride;
239 if (GFC_DESCRIPTOR_RANK (ret) != 1)
240 runtime_error ("incorrect destination rank in spread()");
242 if (along > 1)
243 runtime_error ("dim outside of rank in spread()");
245 if (ret->base_addr == NULL)
247 ret->base_addr = xmallocarray (ncopies, sizeof (GFC_REAL_8));
248 ret->offset = 0;
249 GFC_DIMENSION_SET(ret->dim[0], 0, ncopies - 1, 1);
251 else
253 if (ncopies - 1 > (GFC_DESCRIPTOR_EXTENT(ret,0) - 1)
254 / GFC_DESCRIPTOR_STRIDE(ret,0))
255 runtime_error ("dim too large in spread()");
258 dest = ret->base_addr;
259 stride = GFC_DESCRIPTOR_STRIDE(ret,0);
261 for (n = 0; n < ncopies; n++)
263 *dest = *source;
264 dest += stride;
268 #endif