OSDN Git Service

PR libfortran/18966
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / eoshift2.c
1 /* Generic implementation of the EOSHIFT intrinsic
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 (libgfor).
6
7 Libgfor is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 Ligbfor is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB.  If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <string.h>
26 #include "libgfortran.h"
27
28 static const char zeros[16] =
29   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
30
31 /* TODO: make this work for large shifts when
32    sizeof(int) < sizeof (index_type).  */
33
34 static void
35 eoshift2 (gfc_array_char *ret, const gfc_array_char *array,
36           int shift, const gfc_array_char *bound, int which)
37 {
38   /* r.* indicates the return array.  */
39   index_type rstride[GFC_MAX_DIMENSIONS - 1];
40   index_type rstride0;
41   index_type roffset;
42   char *rptr;
43   char *dest;
44   /* s.* indicates the source array.  */
45   index_type sstride[GFC_MAX_DIMENSIONS - 1];
46   index_type sstride0;
47   index_type soffset;
48   const char *sptr;
49   const char *src;
50   /* b.* indicates the bound array.  */
51   index_type bstride[GFC_MAX_DIMENSIONS - 1];
52   index_type bstride0;
53   const char *bptr;
54
55   index_type count[GFC_MAX_DIMENSIONS - 1];
56   index_type extent[GFC_MAX_DIMENSIONS - 1];
57   index_type dim;
58   index_type size;
59   index_type len;
60   index_type n;
61
62   size = GFC_DESCRIPTOR_SIZE (ret);
63
64   if (ret->data == NULL)
65     {
66       int i;
67
68       ret->data = internal_malloc_size (size * size0 ((array_t *)array));
69       ret->base = 0;
70       ret->dtype = array->dtype;
71       for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
72         {
73           ret->dim[i].lbound = 0;
74           ret->dim[i].ubound = array->dim[i].ubound - array->dim[i].lbound;
75
76           if (i == 0)
77             ret->dim[i].stride = 1;
78           else
79             ret->dim[i].stride = (ret->dim[i-1].ubound + 1) * ret->dim[i-1].stride;
80         }
81     }
82
83   which = which - 1;
84
85   extent[0] = 1;
86   count[0] = 0;
87   size = GFC_DESCRIPTOR_SIZE (array);
88   n = 0;
89   for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
90     {
91       if (dim == which)
92         {
93           roffset = ret->dim[dim].stride * size;
94           if (roffset == 0)
95             roffset = size;
96           soffset = array->dim[dim].stride * size;
97           if (soffset == 0)
98             soffset = size;
99           len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
100         }
101       else
102         {
103           count[n] = 0;
104           extent[n] = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
105           rstride[n] = ret->dim[dim].stride * size;
106           sstride[n] = array->dim[dim].stride * size;
107           if (bound)
108             bstride[n] = bound->dim[n].stride * size;
109           else
110             bstride[n] = 0;
111           n++;
112         }
113     }
114   if (sstride[0] == 0)
115     sstride[0] = size;
116   if (rstride[0] == 0)
117     rstride[0] = size;
118   if (bound && bstride[0] == 0)
119     bstride[0] = size;
120
121   dim = GFC_DESCRIPTOR_RANK (array);
122   rstride0 = rstride[0];
123   sstride0 = sstride[0];
124   bstride0 = bstride[0];
125   rptr = ret->data;
126   sptr = array->data;
127   if (bound)
128     bptr = bound->data;
129   else
130     bptr = zeros;
131
132   if (shift > 0)
133     len = len - shift;
134   else
135     len = len + shift;
136
137   while (rptr)
138     {
139       /* Do the shift for this dimension.  */
140       if (shift > 0)
141         {
142           src = &sptr[shift * soffset];
143           dest = rptr;
144         }
145       else
146         {
147           src = sptr;
148           dest = &rptr[-shift * roffset];
149         }
150       for (n = 0; n < len; n++)
151         {
152           memcpy (dest, src, size);
153           dest += roffset;
154           src += soffset;
155         }
156       if (shift >= 0)
157         {
158           n = shift;
159         }
160       else
161         {
162           dest = rptr;
163           n = -shift;
164         }
165
166       while (n--)
167         {
168           memcpy (dest, bptr, size);
169           dest += roffset;
170         }
171
172       /* Advance to the next section.  */
173       rptr += rstride0;
174       sptr += sstride0;
175       bptr += bstride0;
176       count[0]++;
177       n = 0;
178       while (count[n] == extent[n])
179         {
180           /* When we get to the end of a dimension, reset it and increment
181              the next dimension.  */
182           count[n] = 0;
183           /* We could precalculate these products, but this is a less
184              frequently used path so proabably not worth it.  */
185           rptr -= rstride[n] * extent[n];
186           sptr -= sstride[n] * extent[n];
187           bptr -= bstride[n] * extent[n];
188           n++;
189           if (n >= dim - 1)
190             {
191               /* Break out of the loop.  */
192               rptr = NULL;
193               break;
194             }
195           else
196             {
197               count[n]++;
198               rptr += rstride[n];
199               sptr += sstride[n];
200               bptr += bstride[n];
201             }
202         }
203     }
204 }
205
206
207 extern void eoshift2_1 (gfc_array_char *, const gfc_array_char *,
208                         const GFC_INTEGER_1 *, const gfc_array_char *,
209                         const GFC_INTEGER_1 *);
210 export_proto(eoshift2_1);
211
212 void
213 eoshift2_1 (gfc_array_char *ret, const gfc_array_char *array,
214             const GFC_INTEGER_1 *pshift, const gfc_array_char *bound,
215             const GFC_INTEGER_1 *pdim)
216 {
217   eoshift2 (ret, array, *pshift, bound, pdim ? *pdim : 1);
218 }
219
220
221 extern void eoshift2_2 (gfc_array_char *, const gfc_array_char *,
222                         const GFC_INTEGER_2 *, const gfc_array_char *,
223                         const GFC_INTEGER_2 *);
224 export_proto(eoshift2_2);
225
226 void
227 eoshift2_2 (gfc_array_char *ret, const gfc_array_char *array,
228             const GFC_INTEGER_2 *pshift, const gfc_array_char *bound,
229             const GFC_INTEGER_2 *pdim)
230 {
231   eoshift2 (ret, array, *pshift, bound, pdim ? *pdim : 1);
232 }
233
234
235 extern void eoshift2_4 (gfc_array_char *, const gfc_array_char *,
236                         const GFC_INTEGER_4 *, const gfc_array_char *,
237                         const GFC_INTEGER_4 *);
238 export_proto(eoshift2_4);
239
240 void
241 eoshift2_4 (gfc_array_char *ret, const gfc_array_char *array,
242             const GFC_INTEGER_4 *pshift, const gfc_array_char *bound,
243             const GFC_INTEGER_4 *pdim)
244 {
245   eoshift2 (ret, array, *pshift, bound, pdim ? *pdim : 1);
246 }
247
248
249 extern void eoshift2_8 (gfc_array_char *, const gfc_array_char *,
250                         const GFC_INTEGER_8 *, const gfc_array_char *,
251                         const GFC_INTEGER_8 *);
252 export_proto(eoshift2_8);
253
254 void
255 eoshift2_8 (gfc_array_char *ret, const gfc_array_char *array,
256             const GFC_INTEGER_8 *pshift, const gfc_array_char *bound,
257             const GFC_INTEGER_8 *pdim)
258 {
259   eoshift2 (ret, array, *pshift, bound, pdim ? *pdim : 1);
260 }
261