2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libgfortran / m4 / spread.m4
blobb4bdce64316c89fdfb68966740f830487495b5ff
1 `/* Special implementation of the SPREAD intrinsic
2    Copyright 2008 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 2 of the License, or (at your option) any later version.
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file.  (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combine
20 executable.)
22 Ligbfortran is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public
28 License along with libgfortran; see the file COPYING.  If not,
29 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30 Boston, MA 02110-1301, USA.  */
32 #include "libgfortran.h"
33 #include <stdlib.h>
34 #include <assert.h>
35 #include <string.h>'
37 include(iparm.m4)dnl
39 `#if defined (HAVE_'rtype_name`)
41 void
42 spread_'rtype_code` ('rtype` *ret, const 'rtype` *source,
43                  const index_type along, const index_type pncopies)
45   /* r.* indicates the return array.  */
46   index_type rstride[GFC_MAX_DIMENSIONS];
47   index_type rstride0;
48   index_type rdelta = 0;
49   index_type rrank;
50   index_type rs;
51   'rtype_name` *rptr;
52   'rtype_name` * restrict dest;
53   /* s.* indicates the source array.  */
54   index_type sstride[GFC_MAX_DIMENSIONS];
55   index_type sstride0;
56   index_type srank;
57   const 'rtype_name` *sptr;
59   index_type count[GFC_MAX_DIMENSIONS];
60   index_type extent[GFC_MAX_DIMENSIONS];
61   index_type n;
62   index_type dim;
63   index_type ncopies;
65   srank = GFC_DESCRIPTOR_RANK(source);
67   rrank = srank + 1;
68   if (rrank > GFC_MAX_DIMENSIONS)
69     runtime_error ("return rank too large in spread()");
71   if (along > rrank)
72       runtime_error ("dim outside of rank in spread()");
74   ncopies = pncopies;
76   if (ret->data == NULL)
77     {
78       /* The front end has signalled that we need to populate the
79          return array descriptor.  */
80       ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rrank;
81       dim = 0;
82       rs = 1;
83       for (n = 0; n < rrank; n++)
84         {
85           ret->dim[n].stride = rs;
86           ret->dim[n].lbound = 0;
87           if (n == along - 1)
88             {
89               ret->dim[n].ubound = ncopies - 1;
90               rdelta = rs;
91               rs *= ncopies;
92             }
93           else
94             {
95               count[dim] = 0;
96               extent[dim] = source->dim[dim].ubound + 1
97                 - source->dim[dim].lbound;
98               sstride[dim] = source->dim[dim].stride;
99               rstride[dim] = rs;
101               ret->dim[n].ubound = extent[dim]-1;
102               rs *= extent[dim];
103               dim++;
104             }
105         }
106       ret->offset = 0;
107       if (rs > 0)
108         ret->data = internal_malloc_size (rs * sizeof('rtype_name`));
109       else
110         {
111           ret->data = internal_malloc_size (1);
112           return;
113         }
114     }
115   else
116     {
117       int zero_sized;
119       zero_sized = 0;
121       dim = 0;
122       if (GFC_DESCRIPTOR_RANK(ret) != rrank)
123         runtime_error ("rank mismatch in spread()");
125       if (compile_options.bounds_check)
126         {
127           for (n = 0; n < rrank; n++)
128             {
129               index_type ret_extent;
131               ret_extent = ret->dim[n].ubound + 1 - ret->dim[n].lbound;
132               if (n == along - 1)
133                 {
134                   rdelta = ret->dim[n].stride;
136                   if (ret_extent != ncopies)
137                     runtime_error("Incorrect extent in return value of SPREAD"
138                                   " intrinsic in dimension %ld: is %ld,"
139                                   " should be %ld", (long int) n+1,
140                                   (long int) ret_extent, (long int) ncopies);
141                 }
142               else
143                 {
144                   count[dim] = 0;
145                   extent[dim] = source->dim[dim].ubound + 1
146                     - source->dim[dim].lbound;
147                   if (ret_extent != extent[dim])
148                     runtime_error("Incorrect extent in return value of SPREAD"
149                                   " intrinsic in dimension %ld: is %ld,"
150                                   " should be %ld", (long int) n+1,
151                                   (long int) ret_extent,
152                                   (long int) extent[dim]);
153                     
154                   if (extent[dim] <= 0)
155                     zero_sized = 1;
156                   sstride[dim] = source->dim[dim].stride;
157                   rstride[dim] = ret->dim[n].stride;
158                   dim++;
159                 }
160             }
161         }
162       else
163         {
164           for (n = 0; n < rrank; n++)
165             {
166               if (n == along - 1)
167                 {
168                   rdelta = ret->dim[n].stride;
169                 }
170               else
171                 {
172                   count[dim] = 0;
173                   extent[dim] = source->dim[dim].ubound + 1
174                     - source->dim[dim].lbound;
175                   if (extent[dim] <= 0)
176                     zero_sized = 1;
177                   sstride[dim] = source->dim[dim].stride;
178                   rstride[dim] = ret->dim[n].stride;
179                   dim++;
180                 }
181             }
182         }
184       if (zero_sized)
185         return;
187       if (sstride[0] == 0)
188         sstride[0] = 1;
189     }
190   sstride0 = sstride[0];
191   rstride0 = rstride[0];
192   rptr = ret->data;
193   sptr = source->data;
195   while (sptr)
196     {
197       /* Spread this element.  */
198       dest = rptr;
199       for (n = 0; n < ncopies; n++)
200         {
201           *dest = *sptr;
202           dest += rdelta;
203         }
204       /* Advance to the next element.  */
205       sptr += sstride0;
206       rptr += rstride0;
207       count[0]++;
208       n = 0;
209       while (count[n] == extent[n])
210         {
211           /* When we get to the end of a dimension, reset it and increment
212              the next dimension.  */
213           count[n] = 0;
214           /* We could precalculate these products, but this is a less
215              frequently used path so probably not worth it.  */
216           sptr -= sstride[n] * extent[n];
217           rptr -= rstride[n] * extent[n];
218           n++;
219           if (n >= srank)
220             {
221               /* Break out of the loop.  */
222               sptr = NULL;
223               break;
224             }
225           else
226             {
227               count[n]++;
228               sptr += sstride[n];
229               rptr += rstride[n];
230             }
231         }
232     }
235 /* This version of spread_internal treats the special case of a scalar
236    source.  This is much simpler than the more general case above.  */
238 void
239 spread_scalar_'rtype_code` ('rtype` *ret, const 'rtype_name` *source,
240                         const index_type along, const index_type pncopies)
242   int n;
243   int ncopies = pncopies;
244   'rtype_name` * restrict dest;
245   index_type stride;
247   if (GFC_DESCRIPTOR_RANK (ret) != 1)
248     runtime_error ("incorrect destination rank in spread()");
250   if (along > 1)
251     runtime_error ("dim outside of rank in spread()");
253   if (ret->data == NULL)
254     {
255       ret->data = internal_malloc_size (ncopies * sizeof ('rtype_name`));
256       ret->offset = 0;
257       ret->dim[0].stride = 1;
258       ret->dim[0].lbound = 0;
259       ret->dim[0].ubound = ncopies - 1;
260     }
261   else
262     {
263       if (ncopies - 1 > (ret->dim[0].ubound - ret->dim[0].lbound)
264                            / ret->dim[0].stride)
265         runtime_error ("dim too large in spread()");
266     }
268   dest = ret->data;
269   stride = ret->dim[0].stride;
271   for (n = 0; n < ncopies; n++)
272     {
273       *dest = *source;
274       dest += stride;
275     }
278 #endif