* tree-ssa-pre.c (try_look_through_load): New function.
[official-gcc.git] / libgfortran / m4 / eoshift1.m4
blob0c55c8711b6371a8bd3f10fa0a20cf7571c42d0f
1 `/* Implementation of the EOSHIFT intrinsic
2    Copyright 2002, 2005 Free Software Foundation, Inc.
3    Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
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.
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.)
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.
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., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA.  */
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
38 static const char zeros[16] =
39   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
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);
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)
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;
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;
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;
86   if (pwhich)
87     which = *pwhich - 1;
88   else
89     which = 0;
91   if (!pbound)
92     pbound = zeros;
94   size = GFC_DESCRIPTOR_SIZE (ret);
96   extent[0] = 1;
97   count[0] = 0;
98   size = GFC_DESCRIPTOR_SIZE (array);
100   if (ret->data == NULL)
101     {
102       int i;
104       ret->data = internal_malloc_size (size * size0 ((array_t *)array));
105       ret->offset = 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;
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     }
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;
139           hstride[n] = h->dim[n].stride * size;
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;
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;
158   while (rptr)
159     {
160 `      /* Do the shift for this dimension.  */'
161       sh = *hptr;
162       if (( sh >= 0 ? sh : -sh ) > len)
163         {
164           delta = len;
165           sh = len;
166         }
167       else
168         delta = (sh >= 0) ? sh: -sh;
170       if (sh > 0)
171         {
172           src = &sptr[delta * soffset];
173           dest = rptr;
174         }
175       else
176         {
177           src = sptr;
178           dest = &rptr[delta * roffset];
179         }
180       for (n = 0; n < len - delta; n++)
181         {
182           memcpy (dest, src, size);
183           dest += roffset;
184           src += soffset;
185         }
186       if (sh < 0)
187         dest = rptr;
188       n = delta;
190       while (n--)
191         {
192           memcpy (dest, pbound, size);
193           dest += roffset;
194         }
196       /* Advance to the next section.  */
197       rptr += rstride0;
198       sptr += sstride0;
199       hptr += hstride0;
200       count[0]++;
201       n = 0;
202       while (count[n] == extent[n])
203         {
204           /* When we get to the end of a dimension, reset it and increment
205              the next dimension.  */
206           count[n] = 0;
207           /* We could precalculate these products, but this is a less
208              frequently used path so proabably not worth it.  */
209           rptr -= rstride[n] * extent[n];
210           sptr -= sstride[n] * extent[n];
211           hptr -= hstride[n] * extent[n];
212           n++;
213           if (n >= dim - 1)
214             {
215               /* Break out of the loop.  */
216               rptr = NULL;
217               break;
218             }
219           else
220             {
221               count[n]++;
222               rptr += rstride[n];
223               sptr += sstride[n];
224               hptr += hstride[n];
225             }
226         }
227     }