OSDN Git Service

* libgfortran.h (GFC_ARRAY_DESCRIPTOR): Replace 'type *base' by
[pf3gnuchains/gcc-fork.git] / libgfortran / m4 / eoshift3.m4
1 `/* Implementation of the EOSHIFT intrinsic
2    Copyright 2002, 2005 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 <string.h>
35 #include "libgfortran.h"'
36 include(iparm.m4)dnl
37
38 static const char zeros[16] =
39   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
40
41 extern void eoshift3_`'atype_kind (gfc_array_char *, gfc_array_char *,
42                                      atype *, const gfc_array_char *,
43                                      atype_name *);
44 export_proto(eoshift3_`'atype_kind);
45
46 void
47 eoshift3_`'atype_kind (gfc_array_char *ret, gfc_array_char *array,
48                        atype *h, const gfc_array_char *bound,
49                        atype_name *pwhich)
50 {
51   /* r.* indicates the return array.  */
52   index_type rstride[GFC_MAX_DIMENSIONS];
53   index_type rstride0;
54   index_type roffset;
55   char *rptr;
56   char *dest;
57   /* s.* indicates the source array.  */
58   index_type sstride[GFC_MAX_DIMENSIONS];
59   index_type sstride0;
60   index_type soffset;
61   const char *sptr;
62   const char *src;
63 `  /* h.* indicates the shift array.  */'
64   index_type hstride[GFC_MAX_DIMENSIONS];
65   index_type hstride0;
66   const atype_name *hptr;
67   /* b.* indicates the bound array.  */
68   index_type bstride[GFC_MAX_DIMENSIONS];
69   index_type bstride0;
70   const char *bptr;
71
72   index_type count[GFC_MAX_DIMENSIONS];
73   index_type extent[GFC_MAX_DIMENSIONS];
74   index_type dim;
75   index_type size;
76   index_type len;
77   index_type n;
78   int which;
79   atype_name sh;
80   atype_name delta;
81
82   /* The compiler cannot figure out that these are set, initialize
83      them to avoid warnings.  */
84   len = 0;
85   soffset = 0;
86   roffset = 0;
87
88   if (pwhich)
89     which = *pwhich - 1;
90   else
91     which = 0;
92
93   size = GFC_DESCRIPTOR_SIZE (ret);
94   if (ret->data == NULL)
95     {
96       int i;
97
98       ret->data = internal_malloc_size (size * size0 ((array_t *)array));
99       ret->offset = 0;
100       ret->dtype = array->dtype;
101       for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
102         {
103           ret->dim[i].lbound = 0;
104           ret->dim[i].ubound = array->dim[i].ubound - array->dim[i].lbound;
105
106           if (i == 0)
107             ret->dim[i].stride = 1;
108           else
109             ret->dim[i].stride = (ret->dim[i-1].ubound + 1) * ret->dim[i-1].stride;
110         }
111     }
112
113
114   extent[0] = 1;
115   count[0] = 0;
116   size = GFC_DESCRIPTOR_SIZE (array);
117   n = 0;
118   for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
119     {
120       if (dim == which)
121         {
122           roffset = ret->dim[dim].stride * size;
123           if (roffset == 0)
124             roffset = size;
125           soffset = array->dim[dim].stride * size;
126           if (soffset == 0)
127             soffset = size;
128           len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
129         }
130       else
131         {
132           count[n] = 0;
133           extent[n] = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
134           rstride[n] = ret->dim[dim].stride * size;
135           sstride[n] = array->dim[dim].stride * size;
136
137           hstride[n] = h->dim[n].stride;
138           if (bound)
139             bstride[n] = bound->dim[n].stride * size;
140           else
141             bstride[n] = 0;
142           n++;
143         }
144     }
145   if (sstride[0] == 0)
146     sstride[0] = size;
147   if (rstride[0] == 0)
148     rstride[0] = size;
149   if (hstride[0] == 0)
150     hstride[0] = 1;
151   if (bound && bstride[0] == 0)
152     bstride[0] = size;
153
154   dim = GFC_DESCRIPTOR_RANK (array);
155   rstride0 = rstride[0];
156   sstride0 = sstride[0];
157   hstride0 = hstride[0];
158   bstride0 = bstride[0];
159   rptr = ret->data;
160   sptr = array->data;
161   hptr = h->data;
162   if (bound)
163     bptr = bound->data;
164   else
165     bptr = zeros;
166
167   while (rptr)
168     {
169 `      /* Do the shift for this dimension.  */'
170       sh = *hptr;
171       if (( sh >= 0 ? sh : -sh ) > len)
172         {
173           delta = len;
174           sh = len;
175         }
176       else
177         delta = (sh >= 0) ? sh: -sh;
178
179       if (sh > 0)
180         {
181           src = &sptr[delta * soffset];
182           dest = rptr;
183         }
184       else
185         {
186           src = sptr;
187           dest = &rptr[delta * roffset];
188         }
189       for (n = 0; n < len - delta; n++)
190         {
191           memcpy (dest, src, size);
192           dest += roffset;
193           src += soffset;
194         }
195       if (sh < 0)
196         dest = rptr;
197       n = delta;
198
199       while (n--)
200         {
201           memcpy (dest, bptr, size);
202           dest += roffset;
203         }
204
205       /* Advance to the next section.  */
206       rptr += rstride0;
207       sptr += sstride0;
208       hptr += hstride0;
209       bptr += bstride0;
210       count[0]++;
211       n = 0;
212       while (count[n] == extent[n])
213         {
214           /* When we get to the end of a dimension, reset it and increment
215              the next dimension.  */
216           count[n] = 0;
217           /* We could precalculate these products, but this is a less
218              frequently used path so proabably not worth it.  */
219           rptr -= rstride[n] * extent[n];
220           sptr -= sstride[n] * extent[n];
221           hptr -= hstride[n] * extent[n];
222           bptr -= bstride[n] * extent[n];
223           n++;
224           if (n >= dim - 1)
225             {
226               /* Break out of the loop.  */
227               rptr = NULL;
228               break;
229             }
230           else
231             {
232               count[n]++;
233               rptr += rstride[n];
234               sptr += sstride[n];
235               hptr += hstride[n];
236               bptr += bstride[n];
237             }
238         }
239     }
240 }