c++: add DECL_IMPLICIT_TEMPLATE_PARM_P macro
[official-gcc.git] / libgfortran / m4 / spread.m4
blobedb46ef1b853b46e38145c23cc0d0eb81c3e1c77
1 `/* Special implementation of the SPREAD intrinsic
2    Copyright (C) 2008-2023 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 <string.h>'
30 include(iparm.m4)dnl
32 `#if defined (HAVE_'rtype_name`)
34 void
35 spread_'rtype_code` ('rtype` *ret, const 'rtype` *source,
36                  const index_type along, const index_type pncopies)
38   /* r.* indicates the return array.  */
39   index_type rstride[GFC_MAX_DIMENSIONS];
40   index_type rstride0;
41   index_type rdelta = 0;
42   index_type rrank;
43   index_type rs;
44   'rtype_name` *rptr;
45   'rtype_name` * restrict dest;
46   /* s.* indicates the source array.  */
47   index_type sstride[GFC_MAX_DIMENSIONS];
48   index_type sstride0;
49   index_type srank;
50   const 'rtype_name` *sptr;
52   index_type count[GFC_MAX_DIMENSIONS];
53   index_type extent[GFC_MAX_DIMENSIONS];
54   index_type n;
55   index_type dim;
56   index_type ncopies;
58   srank = GFC_DESCRIPTOR_RANK(source);
60   sstride[0] = 0; /* Avoid warnings if not initialized.  */
61   
62   rrank = srank + 1;
63   if (rrank > GFC_MAX_DIMENSIONS)
64     runtime_error ("return rank too large in spread()");
66   if (along > rrank)
67       runtime_error ("dim outside of rank in spread()");
69   ncopies = pncopies;
71   if (ret->base_addr == NULL)
72     {
74       size_t ub, stride;
76       /* The front end has signalled that we need to populate the
77          return array descriptor.  */
78       ret->dtype.rank = rrank;
80       dim = 0;
81       rs = 1;
82       for (n = 0; n < rrank; n++)
83         {
84           stride = rs;
85           if (n == along - 1)
86             {
87               ub = ncopies - 1;
88               rdelta = rs;
89               rs *= ncopies;
90             }
91           else
92             {
93               count[dim] = 0;
94               extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
95               sstride[dim] = GFC_DESCRIPTOR_STRIDE(source,dim);
96               rstride[dim] = rs;
98               ub = extent[dim] - 1;
99               rs *= extent[dim];
100               dim++;
101             }
102           GFC_DIMENSION_SET(ret->dim[n], 0, ub, stride);
103         }
104       ret->offset = 0;
106       /* xmallocarray allocates a single byte for zero size.  */
107       ret->base_addr = xmallocarray (rs, sizeof('rtype_name`));
108       if (rs <= 0)
109         return;
110     }
111   else
112     {
113       int zero_sized;
115       zero_sized = 0;
117       dim = 0;
118       if (GFC_DESCRIPTOR_RANK(ret) != rrank)
119         runtime_error ("rank mismatch in spread()");
121       if (unlikely (compile_options.bounds_check))
122         {
123           for (n = 0; n < rrank; n++)
124             {
125               index_type ret_extent;
127               ret_extent = GFC_DESCRIPTOR_EXTENT(ret,n);
128               if (n == along - 1)
129                 {
130                   rdelta = GFC_DESCRIPTOR_STRIDE(ret,n);
132                   if (ret_extent != ncopies)
133                     runtime_error("Incorrect extent in return value of SPREAD"
134                                   " intrinsic in dimension %ld: is %ld,"
135                                   " should be %ld", (long int) n+1,
136                                   (long int) ret_extent, (long int) ncopies);
137                 }
138               else
139                 {
140                   count[dim] = 0;
141                   extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
142                   if (ret_extent != extent[dim])
143                     runtime_error("Incorrect extent in return value of SPREAD"
144                                   " intrinsic in dimension %ld: is %ld,"
145                                   " should be %ld", (long int) n+1,
146                                   (long int) ret_extent,
147                                   (long int) extent[dim]);
148                     
149                   if (extent[dim] <= 0)
150                     zero_sized = 1;
151                   sstride[dim] = GFC_DESCRIPTOR_STRIDE(source,dim);
152                   rstride[dim] = GFC_DESCRIPTOR_STRIDE(ret,n);
153                   dim++;
154                 }
155             }
156         }
157       else
158         {
159           for (n = 0; n < rrank; n++)
160             {
161               if (n == along - 1)
162                 {
163                   rdelta = GFC_DESCRIPTOR_STRIDE(ret,n);
164                 }
165               else
166                 {
167                   count[dim] = 0;
168                   extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
169                   if (extent[dim] <= 0)
170                     zero_sized = 1;
171                   sstride[dim] = GFC_DESCRIPTOR_STRIDE(source,dim);
172                   rstride[dim] = GFC_DESCRIPTOR_STRIDE(ret,n);
173                   dim++;
174                 }
175             }
176         }
178       if (zero_sized)
179         return;
181       if (sstride[0] == 0)
182         sstride[0] = 1;
183     }
184   sstride0 = sstride[0];
185   rstride0 = rstride[0];
186   rptr = ret->base_addr;
187   sptr = source->base_addr;
189   while (sptr)
190     {
191       /* Spread this element.  */
192       dest = rptr;
193       for (n = 0; n < ncopies; n++)
194         {
195           *dest = *sptr;
196           dest += rdelta;
197         }
198       /* Advance to the next element.  */
199       sptr += sstride0;
200       rptr += rstride0;
201       count[0]++;
202       n = 0;
203       while (count[n] == extent[n])
204         {
205           /* When we get to the end of a dimension, reset it and increment
206              the next dimension.  */
207           count[n] = 0;
208           /* We could precalculate these products, but this is a less
209              frequently used path so probably not worth it.  */
210           sptr -= sstride[n] * extent[n];
211           rptr -= rstride[n] * extent[n];
212           n++;
213           if (n >= srank)
214             {
215               /* Break out of the loop.  */
216               sptr = NULL;
217               break;
218             }
219           else
220             {
221               count[n]++;
222               sptr += sstride[n];
223               rptr += rstride[n];
224             }
225         }
226     }
229 /* This version of spread_internal treats the special case of a scalar
230    source.  This is much simpler than the more general case above.  */
232 void
233 spread_scalar_'rtype_code` ('rtype` *ret, const 'rtype_name` *source,
234                         const index_type along, const index_type ncopies)
236   'rtype_name` * 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)
246     {
247       ret->base_addr = xmallocarray (ncopies, sizeof ('rtype_name`));
248       ret->offset = 0;
249       GFC_DIMENSION_SET(ret->dim[0], 0, ncopies - 1, 1);
250     }
251   else
252     {
253       if (ncopies - 1 > (GFC_DESCRIPTOR_EXTENT(ret,0) - 1)
254                            / GFC_DESCRIPTOR_STRIDE(ret,0))
255         runtime_error ("dim too large in spread()");
256     }
258   dest = ret->base_addr;
259   stride = GFC_DESCRIPTOR_STRIDE(ret,0);
261   for (index_type n = 0; n < ncopies; n++)
262     {
263       *dest = *source;
264       dest += stride;
265     }
268 #endif