OSDN Git Service

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