OSDN Git Service

PR testsuite/39807
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / eoshift0.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 Libgfortran 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 eoshift0 (gfc_array_char * ret, const gfc_array_char * array,
36           int shift, const char * pbound, int which, index_type size,
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
52   index_type count[GFC_MAX_DIMENSIONS];
53   index_type extent[GFC_MAX_DIMENSIONS];
54   index_type dim;
55   index_type len;
56   index_type n;
57
58   /* The compiler cannot figure out that these are set, initialize
59      them to avoid warnings.  */
60   len = 0;
61   soffset = 0;
62   roffset = 0;
63
64   if (ret->data == NULL)
65     {
66       int i;
67
68       ret->data = internal_malloc_size (size * size0 ((array_t *)array));
69       ret->offset = 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   else
83     {
84       if (size0 ((array_t *) ret) == 0)
85         return;
86     }
87
88   which = which - 1;
89
90   extent[0] = 1;
91   count[0] = 0;
92   sstride[0] = -1;
93   rstride[0] = -1;
94   n = 0;
95   for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
96     {
97       if (dim == which)
98         {
99           roffset = ret->dim[dim].stride * size;
100           if (roffset == 0)
101             roffset = size;
102           soffset = array->dim[dim].stride * size;
103           if (soffset == 0)
104             soffset = size;
105           len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
106         }
107       else
108         {
109           count[n] = 0;
110           extent[n] = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
111           rstride[n] = ret->dim[dim].stride * size;
112           sstride[n] = array->dim[dim].stride * size;
113           n++;
114         }
115     }
116   if (sstride[0] == 0)
117     sstride[0] = size;
118   if (rstride[0] == 0)
119     rstride[0] = size;
120
121   dim = GFC_DESCRIPTOR_RANK (array);
122   rstride0 = rstride[0];
123   sstride0 = sstride[0];
124   rptr = ret->data;
125   sptr = array->data;
126
127   if ((shift >= 0 ? shift : -shift) > len)
128     {
129       shift = len;
130       len = 0;
131     }
132   else
133     {
134       if (shift > 0)
135         len = len - shift;
136       else
137         len = len + shift;
138     }
139
140   while (rptr)
141     {
142       /* Do the shift for this dimension.  */
143       if (shift > 0)
144         {
145           src = &sptr[shift * soffset];
146           dest = rptr;
147         }
148       else
149         {
150           src = sptr;
151           dest = &rptr[-shift * roffset];
152         }
153       for (n = 0; n < len; n++)
154         {
155           memcpy (dest, src, size);
156           dest += roffset;
157           src += soffset;
158         }
159       if (shift >= 0)
160         {
161           n = shift;
162         }
163       else
164         {
165           dest = rptr;
166           n = -shift;
167         }
168
169       if (pbound)
170         while (n--)
171           {
172             memcpy (dest, pbound, size);
173             dest += roffset;
174           }
175       else
176         while (n--)
177           {
178             index_type i;
179
180             if (filler_len == 1)
181               memset (dest, filler[0], size);
182             else
183               for (i = 0; i < size ; i += filler_len)
184                 memcpy (&dest[i], filler, filler_len);
185
186             dest += roffset;
187           }
188
189       /* Advance to the next section.  */
190       rptr += rstride0;
191       sptr += sstride0;
192       count[0]++;
193       n = 0;
194       while (count[n] == extent[n])
195         {
196           /* When we get to the end of a dimension, reset it and increment
197              the next dimension.  */
198           count[n] = 0;
199           /* We could precalculate these products, but this is a less
200              frequently used path so probably not worth it.  */
201           rptr -= rstride[n] * extent[n];
202           sptr -= sstride[n] * extent[n];
203           n++;
204           if (n >= dim - 1)
205             {
206               /* Break out of the loop.  */
207               rptr = NULL;
208               break;
209             }
210           else
211             {
212               count[n]++;
213               rptr += rstride[n];
214               sptr += sstride[n];
215             }
216         }
217     }
218 }
219
220
221 #define DEFINE_EOSHIFT(N)                                                     \
222   extern void eoshift0_##N (gfc_array_char *, const gfc_array_char *,         \
223                             const GFC_INTEGER_##N *, const char *,            \
224                             const GFC_INTEGER_##N *);                         \
225   export_proto(eoshift0_##N);                                                 \
226                                                                               \
227   void                                                                        \
228   eoshift0_##N (gfc_array_char *ret, const gfc_array_char *array,             \
229                 const GFC_INTEGER_##N *pshift, const char *pbound,            \
230                 const GFC_INTEGER_##N *pdim)                                  \
231   {                                                                           \
232     eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1,                  \
233               GFC_DESCRIPTOR_SIZE (array), "\0", 1);                          \
234   }                                                                           \
235                                                                               \
236   extern void eoshift0_##N##_char (gfc_array_char *, GFC_INTEGER_4,           \
237                                    const gfc_array_char *,                    \
238                                    const GFC_INTEGER_##N *, const char *,     \
239                                    const GFC_INTEGER_##N *, GFC_INTEGER_4,    \
240                                    GFC_INTEGER_4);                            \
241   export_proto(eoshift0_##N##_char);                                          \
242                                                                               \
243   void                                                                        \
244   eoshift0_##N##_char (gfc_array_char *ret,                                   \
245                        GFC_INTEGER_4 ret_length __attribute__((unused)),      \
246                        const gfc_array_char *array,                           \
247                        const GFC_INTEGER_##N *pshift,                         \
248                        const char *pbound,                                    \
249                        const GFC_INTEGER_##N *pdim,                           \
250                        GFC_INTEGER_4 array_length,                            \
251                        GFC_INTEGER_4 bound_length __attribute__((unused)))    \
252   {                                                                           \
253     eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1,                  \
254               array_length, " ", 1);                                          \
255   }                                                                           \
256                                                                               \
257   extern void eoshift0_##N##_char4 (gfc_array_char *, GFC_INTEGER_4,          \
258                                     const gfc_array_char *,                   \
259                                     const GFC_INTEGER_##N *, const char *,    \
260                                     const GFC_INTEGER_##N *, GFC_INTEGER_4,   \
261                                     GFC_INTEGER_4);                           \
262   export_proto(eoshift0_##N##_char4);                                         \
263                                                                               \
264   void                                                                        \
265   eoshift0_##N##_char4 (gfc_array_char *ret,                                  \
266                         GFC_INTEGER_4 ret_length __attribute__((unused)),     \
267                         const gfc_array_char *array,                          \
268                         const GFC_INTEGER_##N *pshift,                        \
269                         const char *pbound,                                   \
270                         const GFC_INTEGER_##N *pdim,                          \
271                         GFC_INTEGER_4 array_length,                           \
272                         GFC_INTEGER_4 bound_length __attribute__((unused)))   \
273   {                                                                           \
274     static const gfc_char4_t space = (unsigned char) ' ';                     \
275     eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1,                  \
276               array_length * sizeof (gfc_char4_t), (const char *) &space,     \
277               sizeof (gfc_char4_t));                                          \
278   }
279
280 DEFINE_EOSHIFT (1);
281 DEFINE_EOSHIFT (2);
282 DEFINE_EOSHIFT (4);
283 DEFINE_EOSHIFT (8);
284 #ifdef HAVE_GFC_INTEGER_16
285 DEFINE_EOSHIFT (16);
286 #endif