OSDN Git Service

2005-08-17 Kelley Cook <kcook@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libgfortran / generated / reshape_i8.c
1 /* Implementation of the RESHAPE
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 (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 Libgfortran 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 "libgfortran.h"
35
36 typedef GFC_ARRAY_DESCRIPTOR(1, index_type) shape_type;
37
38 /* The shape parameter is ignored. We can currently deduce the shape from the
39    return array.  */
40
41 extern void reshape_8 (gfc_array_i8 *, gfc_array_i8 *, shape_type *,
42                                     gfc_array_i8 *, shape_type *);
43 export_proto(reshape_8);
44
45 void
46 reshape_8 (gfc_array_i8 * ret, gfc_array_i8 * source, shape_type * shape,
47                       gfc_array_i8 * pad, shape_type * order)
48 {
49   /* r.* indicates the return array.  */
50   index_type rcount[GFC_MAX_DIMENSIONS];
51   index_type rextent[GFC_MAX_DIMENSIONS];
52   index_type rstride[GFC_MAX_DIMENSIONS];
53   index_type rstride0;
54   index_type rdim;
55   index_type rsize;
56   index_type rs;
57   index_type rex;
58   GFC_INTEGER_8 *rptr;
59   /* s.* indicates the source array.  */
60   index_type scount[GFC_MAX_DIMENSIONS];
61   index_type sextent[GFC_MAX_DIMENSIONS];
62   index_type sstride[GFC_MAX_DIMENSIONS];
63   index_type sstride0;
64   index_type sdim;
65   index_type ssize;
66   const GFC_INTEGER_8 *sptr;
67   /* p.* indicates the pad array.  */
68   index_type pcount[GFC_MAX_DIMENSIONS];
69   index_type pextent[GFC_MAX_DIMENSIONS];
70   index_type pstride[GFC_MAX_DIMENSIONS];
71   index_type pdim;
72   index_type psize;
73   const GFC_INTEGER_8 *pptr;
74
75   const GFC_INTEGER_8 *src;
76   int n;
77   int dim;
78
79   if (source->dim[0].stride == 0)
80     source->dim[0].stride = 1;
81   if (shape->dim[0].stride == 0)
82     shape->dim[0].stride = 1;
83   if (pad && pad->dim[0].stride == 0)
84     pad->dim[0].stride = 1;
85   if (order && order->dim[0].stride == 0)
86     order->dim[0].stride = 1;
87
88   if (ret->data == NULL)
89     {
90       rdim = shape->dim[0].ubound - shape->dim[0].lbound + 1;
91       rs = 1;
92       for (n=0; n < rdim; n++)
93         {
94           ret->dim[n].lbound = 0;
95           rex = shape->data[n * shape->dim[0].stride];
96           ret->dim[n].ubound =  rex - 1;
97           ret->dim[n].stride = rs;
98           rs *= rex;
99         }
100       ret->offset = 0;
101       ret->data = internal_malloc_size ( rs * sizeof (GFC_INTEGER_8));
102       ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim;
103     }
104   else
105     {
106       rdim = GFC_DESCRIPTOR_RANK (ret);
107       if (ret->dim[0].stride == 0)
108         ret->dim[0].stride = 1;
109     }
110
111   rsize = 1;
112   for (n = 0; n < rdim; n++)
113     {
114       if (order)
115         dim = order->data[n * order->dim[0].stride] - 1;
116       else
117         dim = n;
118
119       rcount[n] = 0;
120       rstride[n] = ret->dim[dim].stride;
121       rextent[n] = ret->dim[dim].ubound + 1 - ret->dim[dim].lbound;
122
123       if (rextent[n] != shape->data[dim * shape->dim[0].stride])
124         runtime_error ("shape and target do not conform");
125
126       if (rsize == rstride[n])
127         rsize *= rextent[n];
128       else
129         rsize = 0;
130       if (rextent[n] <= 0)
131         return;
132     }
133
134   sdim = GFC_DESCRIPTOR_RANK (source);
135   ssize = 1;
136   for (n = 0; n < sdim; n++)
137     {
138       scount[n] = 0;
139       sstride[n] = source->dim[n].stride;
140       sextent[n] = source->dim[n].ubound + 1 - source->dim[n].lbound;
141       if (sextent[n] <= 0)
142         abort ();
143
144       if (ssize == sstride[n])
145         ssize *= sextent[n];
146       else
147         ssize = 0;
148     }
149
150   if (pad)
151     {
152       pdim = GFC_DESCRIPTOR_RANK (pad);
153       psize = 1;
154       for (n = 0; n < pdim; n++)
155         {
156           pcount[n] = 0;
157           pstride[n] = pad->dim[n].stride;
158           pextent[n] = pad->dim[n].ubound + 1 - pad->dim[n].lbound;
159           if (pextent[n] <= 0)
160             abort ();
161           if (psize == pstride[n])
162             psize *= pextent[n];
163           else
164             psize = 0;
165         }
166       pptr = pad->data;
167     }
168   else
169     {
170       pdim = 0;
171       psize = 1;
172       pptr = NULL;
173     }
174
175   if (rsize != 0 && ssize != 0 && psize != 0)
176     {
177       rsize *= sizeof (GFC_INTEGER_8);
178       ssize *= sizeof (GFC_INTEGER_8);
179       psize *= sizeof (GFC_INTEGER_8);
180       reshape_packed ((char *)ret->data, rsize, (char *)source->data,
181                       ssize, pad ? (char *)pad->data : NULL, psize);
182       return;
183     }
184   rptr = ret->data;
185   src = sptr = source->data;
186   rstride0 = rstride[0];
187   sstride0 = sstride[0];
188
189   while (rptr)
190     {
191       /* Select between the source and pad arrays.  */
192       *rptr = *src;
193       /* Advance to the next element.  */
194       rptr += rstride0;
195       src += sstride0;
196       rcount[0]++;
197       scount[0]++;
198       /* Advance to the next destination element.  */
199       n = 0;
200       while (rcount[n] == rextent[n])
201         {
202           /* When we get to the end of a dimension, reset it and increment
203              the next dimension.  */
204           rcount[n] = 0;
205           /* We could precalculate these products, but this is a less
206              frequently used path so proabably not worth it.  */
207           rptr -= rstride[n] * rextent[n];
208           n++;
209           if (n == rdim)
210             {
211               /* Break out of the loop.  */
212               rptr = NULL;
213               break;
214             }
215           else
216             {
217               rcount[n]++;
218               rptr += rstride[n];
219             }
220         }
221       /* Advance to the next source element.  */
222       n = 0;
223       while (scount[n] == sextent[n])
224         {
225           /* When we get to the end of a dimension, reset it and increment
226              the next dimension.  */
227           scount[n] = 0;
228           /* We could precalculate these products, but this is a less
229              frequently used path so proabably not worth it.  */
230           src -= sstride[n] * sextent[n];
231           n++;
232           if (n == sdim)
233             {
234               if (sptr && pad)
235                 {
236                   /* Switch to the pad array.  */
237                   sptr = NULL;
238                   sdim = pdim;
239                   for (dim = 0; dim < pdim; dim++)
240                     {
241                       scount[dim] = pcount[dim];
242                       sextent[dim] = pextent[dim];
243                       sstride[dim] = pstride[dim];
244                       sstride0 = sstride[0];
245                     }
246                 }
247               /* We now start again from the beginning of the pad array.  */
248               src = pptr;
249               break;
250             }
251           else
252             {
253               scount[n]++;
254               src += sstride[n];
255             }
256         }
257     }
258 }