OSDN Git Service

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