OSDN Git Service

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