OSDN Git Service

2009-06-21 Thomas Koenig <tkoenig@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / eoshift2.c
1 /* Generic implementation of the EOSHIFT intrinsic
2    Copyright 2002, 2005, 2007, 2009 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 3 of the License, or (at your option) any later version.
11
12 Ligbfortran 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 General Public License for more details.
16
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 <http://www.gnu.org/licenses/>.  */
25
26 #include "libgfortran.h"
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <string.h>
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           const char *filler, index_type filler_len)
38 {
39   /* r.* indicates the return array.  */
40   index_type rstride[GFC_MAX_DIMENSIONS];
41   index_type rstride0;
42   index_type roffset;
43   char * restrict rptr;
44   char *dest;
45   /* s.* indicates the source array.  */
46   index_type sstride[GFC_MAX_DIMENSIONS];
47   index_type sstride0;
48   index_type soffset;
49   const char *sptr;
50   const char *src;
51   /* b.* indicates the bound array.  */
52   index_type bstride[GFC_MAX_DIMENSIONS];
53   index_type bstride0;
54   const char *bptr;
55
56   index_type count[GFC_MAX_DIMENSIONS];
57   index_type extent[GFC_MAX_DIMENSIONS];
58   index_type dim;
59   index_type len;
60   index_type n;
61   index_type arraysize;
62   index_type size;
63
64   /* The compiler cannot figure out that these are set, initialize
65      them to avoid warnings.  */
66   len = 0;
67   soffset = 0;
68   roffset = 0;
69
70   size = GFC_DESCRIPTOR_SIZE (array);
71
72   arraysize = size0 ((array_t *) array);
73
74   if (ret->data == NULL)
75     {
76       int i;
77
78       ret->data = internal_malloc_size (size * arraysize);
79       ret->offset = 0;
80       ret->dtype = array->dtype;
81       for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
82         {
83           index_type ub, str;
84
85           ub = GFC_DESCRIPTOR_EXTENT(array,i) - 1;
86
87           if (i == 0)
88             str = 1;
89           else
90             str = GFC_DESCRIPTOR_EXTENT(ret,i-1)
91               * GFC_DESCRIPTOR_STRIDE(ret,i-1);
92
93           GFC_DIMENSION_SET(ret->dim[i], 0, ub, str);
94
95         }
96     }
97   else
98     {
99       if (size0 ((array_t *) ret) == 0)
100         return;
101     }
102
103   if (arraysize == 0 && filler == NULL)
104     return;
105
106   which = which - 1;
107
108   extent[0] = 1;
109   count[0] = 0;
110   sstride[0] = -1;
111   rstride[0] = -1;
112   bstride[0] = -1;
113   n = 0;
114   for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
115     {
116       if (dim == which)
117         {
118           roffset = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
119           if (roffset == 0)
120             roffset = size;
121           soffset = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
122           if (soffset == 0)
123             soffset = size;
124           len = GFC_DESCRIPTOR_EXTENT(array,dim);
125         }
126       else
127         {
128           count[n] = 0;
129           extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
130           rstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
131           sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
132           if (bound)
133             bstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(bound,n);
134           else
135             bstride[n] = 0;
136           n++;
137         }
138     }
139   if (sstride[0] == 0)
140     sstride[0] = size;
141   if (rstride[0] == 0)
142     rstride[0] = size;
143   if (bound && bstride[0] == 0)
144     bstride[0] = size;
145
146   dim = GFC_DESCRIPTOR_RANK (array);
147   rstride0 = rstride[0];
148   sstride0 = sstride[0];
149   bstride0 = bstride[0];
150   rptr = ret->data;
151   sptr = array->data;
152
153   if ((shift >= 0 ? shift : -shift ) > len)
154     {
155       shift = len;
156       len = 0;
157     }
158   else
159     {
160       if (shift > 0)
161         len = len - shift;
162       else
163         len = len + shift;
164     }
165   
166   if (bound)
167     bptr = bound->data;
168   else
169     bptr = NULL;
170
171   while (rptr)
172     {
173       /* Do the shift for this dimension.  */
174       if (shift > 0)
175         {
176           src = &sptr[shift * soffset];
177           dest = rptr;
178         }
179       else
180         {
181           src = sptr;
182           dest = &rptr[-shift * roffset];
183         }
184       for (n = 0; n < len; n++)
185         {
186           memcpy (dest, src, size);
187           dest += roffset;
188           src += soffset;
189         }
190       if (shift >= 0)
191         {
192           n = shift;
193         }
194       else
195         {
196           dest = rptr;
197           n = -shift;
198         }
199
200       if (bptr)
201         while (n--)
202           {
203             memcpy (dest, bptr, size);
204             dest += roffset;
205           }
206       else
207         while (n--)
208           {
209             index_type i;
210
211             if (filler_len == 1)
212               memset (dest, filler[0], size);
213             else
214               for (i = 0; i < size ; i += filler_len)
215                 memcpy (&dest[i], filler, filler_len);
216
217             dest += roffset;
218           }
219
220       /* Advance to the next section.  */
221       rptr += rstride0;
222       sptr += sstride0;
223       bptr += bstride0;
224       count[0]++;
225       n = 0;
226       while (count[n] == extent[n])
227         {
228           /* When we get to the end of a dimension, reset it and increment
229              the next dimension.  */
230           count[n] = 0;
231           /* We could precalculate these products, but this is a less
232              frequently used path so probably not worth it.  */
233           rptr -= rstride[n] * extent[n];
234           sptr -= sstride[n] * extent[n];
235           bptr -= bstride[n] * extent[n];
236           n++;
237           if (n >= dim - 1)
238             {
239               /* Break out of the loop.  */
240               rptr = NULL;
241               break;
242             }
243           else
244             {
245               count[n]++;
246               rptr += rstride[n];
247               sptr += sstride[n];
248               bptr += bstride[n];
249             }
250         }
251     }
252 }
253
254
255 #define DEFINE_EOSHIFT(N)                                                     \
256   extern void eoshift2_##N (gfc_array_char *, const gfc_array_char *,         \
257                             const GFC_INTEGER_##N *, const gfc_array_char *,  \
258                             const GFC_INTEGER_##N *);                         \
259   export_proto(eoshift2_##N);                                                 \
260                                                                               \
261   void                                                                        \
262   eoshift2_##N (gfc_array_char *ret, const gfc_array_char *array,             \
263                 const GFC_INTEGER_##N *pshift, const gfc_array_char *pbound,  \
264                 const GFC_INTEGER_##N *pdim)                                  \
265   {                                                                           \
266     eoshift2 (ret, array, *pshift, pbound, pdim ? *pdim : 1,                  \
267               "\0", 1);                       \
268   }                                                                           \
269                                                                               \
270   extern void eoshift2_##N##_char (gfc_array_char *, GFC_INTEGER_4,           \
271                                    const gfc_array_char *,                    \
272                                    const GFC_INTEGER_##N *,                   \
273                                    const gfc_array_char *,                    \
274                                    const GFC_INTEGER_##N *,                   \
275                                    GFC_INTEGER_4, GFC_INTEGER_4);             \
276   export_proto(eoshift2_##N##_char);                                          \
277                                                                               \
278   void                                                                        \
279   eoshift2_##N##_char (gfc_array_char *ret,                                   \
280                        GFC_INTEGER_4 ret_length __attribute__((unused)),      \
281                        const gfc_array_char *array,                           \
282                        const GFC_INTEGER_##N *pshift,                         \
283                        const gfc_array_char *pbound,                          \
284                        const GFC_INTEGER_##N *pdim,                           \
285                        GFC_INTEGER_4 array_length __attribute__((unused)),    \
286                        GFC_INTEGER_4 bound_length __attribute__((unused)))    \
287   {                                                                           \
288     eoshift2 (ret, array, *pshift, pbound, pdim ? *pdim : 1,                  \
289               " ", 1);                                                        \
290   }                                                                           \
291                                                                               \
292   extern void eoshift2_##N##_char4 (gfc_array_char *, GFC_INTEGER_4,          \
293                                     const gfc_array_char *,                   \
294                                     const GFC_INTEGER_##N *,                  \
295                                     const gfc_array_char *,                   \
296                                     const GFC_INTEGER_##N *,                  \
297                                     GFC_INTEGER_4, GFC_INTEGER_4);            \
298   export_proto(eoshift2_##N##_char4);                                         \
299                                                                               \
300   void                                                                        \
301   eoshift2_##N##_char4 (gfc_array_char *ret,                                  \
302                         GFC_INTEGER_4 ret_length __attribute__((unused)),     \
303                         const gfc_array_char *array,                          \
304                         const GFC_INTEGER_##N *pshift,                        \
305                         const gfc_array_char *pbound,                         \
306                         const GFC_INTEGER_##N *pdim,                          \
307                         GFC_INTEGER_4 array_length __attribute__((unused)),   \
308                         GFC_INTEGER_4 bound_length __attribute__((unused)))   \
309   {                                                                           \
310     static const gfc_char4_t space = (unsigned char) ' ';                     \
311     eoshift2 (ret, array, *pshift, pbound, pdim ? *pdim : 1,                  \
312               (const char *) &space,                                          \
313               sizeof (gfc_char4_t));                                          \
314   }
315
316 DEFINE_EOSHIFT (1);
317 DEFINE_EOSHIFT (2);
318 DEFINE_EOSHIFT (4);
319 DEFINE_EOSHIFT (8);
320 #ifdef HAVE_GFC_INTEGER_16
321 DEFINE_EOSHIFT (16);
322 #endif