OSDN Git Service

ca6f6aacd0063d7c9788e73e3c3051a0ab0f1bac
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / reshape_generic.c
1 /* Generic implementation of the RESHAPE intrinsic
2    Copyright 2002 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 (libgfor).
6
7 Libgfor is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 Ligbfor is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB.  If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
26 #include "libgfortran.h"
27
28 typedef GFC_ARRAY_DESCRIPTOR(1, index_type) shape_type;
29 typedef GFC_ARRAY_DESCRIPTOR(GFC_MAX_DIMENSIONS, char) parray;
30
31
32 /* The shape parameter is ignored. We can currently deduce the shape from the
33    return array.  */
34
35 void
36 __reshape (parray * ret, parray * source, shape_type * shape,
37            parray * pad, shape_type * order)
38 {
39   /* r.* indicates the return array.  */
40   index_type rcount[GFC_MAX_DIMENSIONS - 1];
41   index_type rextent[GFC_MAX_DIMENSIONS - 1];
42   index_type rstride[GFC_MAX_DIMENSIONS - 1];
43   index_type rstride0;
44   index_type rdim;
45   index_type rsize;
46   char *rptr;
47   /* s.* indicates the source array.  */
48   index_type scount[GFC_MAX_DIMENSIONS - 1];
49   index_type sextent[GFC_MAX_DIMENSIONS - 1];
50   index_type sstride[GFC_MAX_DIMENSIONS - 1];
51   index_type sstride0;
52   index_type sdim;
53   index_type ssize;
54   const char *sptr;
55   /* p.* indicates the pad array.  */
56   index_type pcount[GFC_MAX_DIMENSIONS - 1];
57   index_type pextent[GFC_MAX_DIMENSIONS - 1];
58   index_type pstride[GFC_MAX_DIMENSIONS - 1];
59   index_type pdim;
60   index_type psize;
61   const char *pptr;
62
63   const char *src;
64   int n;
65   int dim;
66   int size;
67
68   size = GFC_DESCRIPTOR_SIZE (ret);
69   if (ret->dim[0].stride == 0)
70     ret->dim[0].stride = 1;
71   if (source->dim[0].stride == 0)
72     source->dim[0].stride = 1;
73   if (shape->dim[0].stride == 0)
74     shape->dim[0].stride = 1;
75   if (pad && pad->dim[0].stride == 0)
76     pad->dim[0].stride = 1;
77   if (order && order->dim[0].stride == 0)
78     order->dim[0].stride = 1;
79
80   rdim = GFC_DESCRIPTOR_RANK (ret);
81   rsize = 1;
82   for (n = 0; n < rdim; n++)
83     {
84       if (order)
85         dim = order->data[n * order->dim[0].stride] - 1;
86       else
87         dim = n;
88
89       rcount[n] = 0;
90       rstride[n] = ret->dim[dim].stride;
91       rextent[n] = ret->dim[dim].ubound + 1 - ret->dim[dim].lbound;
92
93       if (rextent[n] != shape->data[dim * shape->dim[0].stride])
94         runtime_error ("shape and target do not conform");
95
96       if (rsize == rstride[n])
97         rsize *= rextent[n];
98       else
99         rsize = 0;
100       if (rextent[dim] <= 0)
101         return;
102     }
103
104   sdim = GFC_DESCRIPTOR_RANK (source);
105   ssize = 1;
106   for (n = 0; n < sdim; n++)
107     {
108       scount[n] = 0;
109       sstride[n] = source->dim[n].stride;
110       sextent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound;
111       if (sextent[n] <= 0)
112         abort ();
113
114       if (rsize == sstride[n])
115         ssize *= sextent[n];
116       else
117         ssize = 0;
118     }
119
120   if (pad)
121     {
122       if (pad->dim[0].stride == 0)
123         pad->dim[0].stride = 1;
124       pdim = GFC_DESCRIPTOR_RANK (pad);
125       psize = 1;
126       for (n = 0; n < pdim; n++)
127         {
128           pcount[n] = 0;
129           pstride[n] = pad->dim[n].stride;
130           pextent[n] = pad->dim[n].ubound + 1 - pad->dim[n].lbound;
131           if (pextent[n] <= 0)
132             abort ();
133           if (psize == pstride[n])
134             psize *= pextent[n];
135           else
136             rsize = 0;
137         }
138       pptr = pad->data;
139     }
140   else
141     {
142       pdim = 0;
143       psize = 1;
144       pptr = NULL;
145     }
146
147   if (rsize != 0 && ssize != 0 && psize != 0)
148     {
149       rsize *= size;
150       ssize *= size;
151       psize *= size;
152       reshape_packed (ret->data, rsize, source->data, ssize,
153                       pad ? pad->data : NULL, psize);
154       return;
155     }
156   rptr = ret->data;
157   src = sptr = source->data;
158   rstride0 = rstride[0] * size;
159   sstride0 = sstride[0] * size;
160
161   while (rptr)
162     {
163       /* Select between the source and pad arrays.  */
164       memcpy(rptr, src, size);
165       /* Advance to the next element.  */
166       rptr += rstride0;
167       src += sstride0;
168       rcount[0]++;
169       scount[0]++;
170       /* Advance to the next destination element.  */
171       n = 0;
172       while (rcount[n] == rextent[n])
173         {
174           /* When we get to the end of a dimension, reset it and increment
175              the next dimension.  */
176           rcount[n] = 0;
177           /* We could precalculate these products, but this is a less
178              frequently used path so proabably not worth it.  */
179           rptr -= rstride[n] * rextent[n] * size;
180           n++;
181           if (n == rdim)
182             {
183               /* Break out of the loop.  */
184               rptr = NULL;
185               break;
186             }
187           else
188             {
189               rcount[n]++;
190               rptr += rstride[n] * size;
191             }
192         }
193       /* Advance to the next source element.  */
194       n = 0;
195       while (scount[n] == sextent[n])
196         {
197           /* When we get to the end of a dimension, reset it and increment
198              the next dimension.  */
199           scount[n] = 0;
200           /* We could precalculate these products, but this is a less
201              frequently used path so proabably not worth it.  */
202           src -= sstride[n] * sextent[n] * size;
203           n++;
204           if (n == sdim)
205             {
206               if (sptr && pad)
207                 {
208                   /* Switch to the pad array.  */
209                   sptr = NULL;
210                   sdim = pdim;
211                   for (dim = 0; dim < pdim; dim++)
212                     {
213                       scount[dim] = pcount[dim];
214                       sextent[dim] = pextent[dim];
215                       sstride[dim] = pstride[dim];
216                       sstride0 = sstride[0] * size;
217                     }
218                 }
219               /* We now start again from the beginning of the pad array.  */
220               src = pptr;
221               break;
222             }
223           else
224             {
225               scount[n]++;
226               sptr += sstride[n] * size;
227             }
228         }
229     }
230 }
231