OSDN Git Service

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