OSDN Git Service

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