OSDN Git Service

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