]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/intrinsics/spread_generic.c
minloc1.m4: Update copyright year and ajust headers order.
[thirdparty/gcc.git] / libgfortran / intrinsics / spread_generic.c
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>
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
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.
11
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
19 executable.)
20
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.
25
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. */
30
31 #include "libgfortran.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <string.h>
35
36 static void
37 spread_internal (gfc_array_char *ret, const gfc_array_char *source,
38 const index_type *along, const index_type *pncopies,
39 index_type size)
40 {
41 /* r.* indicates the return array. */
42 index_type rstride[GFC_MAX_DIMENSIONS];
43 index_type rstride0;
44 index_type rdelta = 0;
45 index_type rrank;
46 index_type rs;
47 char *rptr;
48 char *dest;
49 /* s.* indicates the source array. */
50 index_type sstride[GFC_MAX_DIMENSIONS];
51 index_type sstride0;
52 index_type srank;
53 const char *sptr;
54
55 index_type count[GFC_MAX_DIMENSIONS];
56 index_type extent[GFC_MAX_DIMENSIONS];
57 index_type n;
58 index_type dim;
59 index_type ncopies;
60
61 srank = GFC_DESCRIPTOR_RANK(source);
62
63 rrank = srank + 1;
64 if (rrank > GFC_MAX_DIMENSIONS)
65 runtime_error ("return rank too large in spread()");
66
67 if (*along > rrank)
68 runtime_error ("dim outside of rank in spread()");
69
70 ncopies = *pncopies;
71
72 if (ret->data == NULL)
73 {
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;
77 dim = 0;
78 rs = 1;
79 for (n = 0; n < rrank; n++)
80 {
81 ret->dim[n].stride = rs;
82 ret->dim[n].lbound = 0;
83 if (n == *along - 1)
84 {
85 ret->dim[n].ubound = ncopies - 1;
86 rdelta = rs * size;
87 rs *= ncopies;
88 }
89 else
90 {
91 count[dim] = 0;
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;
96
97 ret->dim[n].ubound = extent[dim]-1;
98 rs *= extent[dim];
99 dim++;
100 }
101 }
102 ret->offset = 0;
103 if (rs > 0)
104 ret->data = internal_malloc_size (rs * size);
105 else
106 {
107 ret->data = internal_malloc_size (1);
108 return;
109 }
110 }
111 else
112 {
113 dim = 0;
114 if (GFC_DESCRIPTOR_RANK(ret) != rrank)
115 runtime_error ("rank mismatch in spread()");
116
117 for (n = 0; n < rrank; n++)
118 {
119 if (n == *along - 1)
120 {
121 rdelta = ret->dim[n].stride * size;
122 }
123 else
124 {
125 count[dim] = 0;
126 extent[dim] = source->dim[dim].ubound + 1
127 - source->dim[dim].lbound;
128 sstride[dim] = source->dim[dim].stride * size;
129 rstride[dim] = ret->dim[n].stride * size;
130 dim++;
131 }
132 }
133 if (sstride[0] == 0)
134 sstride[0] = size;
135 }
136 sstride0 = sstride[0];
137 rstride0 = rstride[0];
138 rptr = ret->data;
139 sptr = source->data;
140
141 while (sptr)
142 {
143 /* Spread this element. */
144 dest = rptr;
145 for (n = 0; n < ncopies; n++)
146 {
147 memcpy (dest, sptr, size);
148 dest += rdelta;
149 }
150 /* Advance to the next element. */
151 sptr += sstride0;
152 rptr += rstride0;
153 count[0]++;
154 n = 0;
155 while (count[n] == extent[n])
156 {
157 /* When we get to the end of a dimension, reset it and increment
158 the next dimension. */
159 count[n] = 0;
160 /* We could precalculate these products, but this is a less
161 frequently used path so probably not worth it. */
162 sptr -= sstride[n] * extent[n];
163 rptr -= rstride[n] * extent[n];
164 n++;
165 if (n >= srank)
166 {
167 /* Break out of the loop. */
168 sptr = NULL;
169 break;
170 }
171 else
172 {
173 count[n]++;
174 sptr += sstride[n];
175 rptr += rstride[n];
176 }
177 }
178 }
179 }
180
181 /* This version of spread_internal treats the special case of a scalar
182 source. This is much simpler than the more general case above. */
183
184 static void
185 spread_internal_scalar (gfc_array_char *ret, const char *source,
186 const index_type *along, const index_type *pncopies,
187 index_type size)
188 {
189 int n;
190 int ncopies = *pncopies;
191 char * dest;
192
193 if (GFC_DESCRIPTOR_RANK (ret) != 1)
194 runtime_error ("incorrect destination rank in spread()");
195
196 if (*along > 1)
197 runtime_error ("dim outside of rank in spread()");
198
199 if (ret->data == NULL)
200 {
201 ret->data = internal_malloc_size (ncopies * size);
202 ret->offset = 0;
203 ret->dim[0].stride = 1;
204 ret->dim[0].lbound = 0;
205 ret->dim[0].ubound = ncopies - 1;
206 }
207 else
208 {
209 if (ncopies - 1 > (ret->dim[0].ubound - ret->dim[0].lbound)
210 / ret->dim[0].stride)
211 runtime_error ("dim too large in spread()");
212 }
213
214 for (n = 0; n < ncopies; n++)
215 {
216 dest = (char*)(ret->data + n*size*ret->dim[0].stride);
217 memcpy (dest , source, size);
218 }
219 }
220
221 extern void spread (gfc_array_char *, const gfc_array_char *,
222 const index_type *, const index_type *);
223 export_proto(spread);
224
225 void
226 spread (gfc_array_char *ret, const gfc_array_char *source,
227 const index_type *along, const index_type *pncopies)
228 {
229 spread_internal (ret, source, along, pncopies, GFC_DESCRIPTOR_SIZE (source));
230 }
231
232 extern void spread_char (gfc_array_char *, GFC_INTEGER_4,
233 const gfc_array_char *, const index_type *,
234 const index_type *, GFC_INTEGER_4);
235 export_proto(spread_char);
236
237 void
238 spread_char (gfc_array_char *ret,
239 GFC_INTEGER_4 ret_length __attribute__((unused)),
240 const gfc_array_char *source, const index_type *along,
241 const index_type *pncopies, GFC_INTEGER_4 source_length)
242 {
243 spread_internal (ret, source, along, pncopies, source_length);
244 }
245
246 /* The following are the prototypes for the versions of spread with a
247 scalar source. */
248
249 extern void spread_scalar (gfc_array_char *, const char *,
250 const index_type *, const index_type *);
251 export_proto(spread_scalar);
252
253 void
254 spread_scalar (gfc_array_char *ret, const char *source,
255 const index_type *along, const index_type *pncopies)
256 {
257 if (!ret->dtype)
258 runtime_error ("return array missing descriptor in spread()");
259 spread_internal_scalar (ret, source, along, pncopies, GFC_DESCRIPTOR_SIZE (ret));
260 }
261
262
263 extern void spread_char_scalar (gfc_array_char *, GFC_INTEGER_4,
264 const char *, const index_type *,
265 const index_type *, GFC_INTEGER_4);
266 export_proto(spread_char_scalar);
267
268 void
269 spread_char_scalar (gfc_array_char *ret,
270 GFC_INTEGER_4 ret_length __attribute__((unused)),
271 const char *source, const index_type *along,
272 const index_type *pncopies, GFC_INTEGER_4 source_length)
273 {
274 if (!ret->dtype)
275 runtime_error ("return array missing descriptor in spread()");
276 spread_internal_scalar (ret, source, along, pncopies, source_length);
277 }
278