OSDN Git Service

* intrinsics/cshift0.c, intrinsics/eoshift0.c, intrinsics/eoshift2.c,
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / cshift0.c
1 /* Generic implementation of the CSHIFT intrinsic
2    Copyright 2003 Free Software Foundation, Inc.
3    Contributed by Feng Wang <wf_cs@yahoo.com>
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 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
29 /* "Templatized" helper function for the inner shift loop.  */
30
31 #define DEF_COPY_LOOP(NAME, TYPE)                                       \
32 static inline void                                                      \
33 copy_loop_##NAME (void *xdest, const void *xsrc,                        \
34                   size_t roff, size_t soff,                             \
35                   index_type len, index_type shift)                     \
36 {                                                                       \
37   TYPE *dest = xdest;                                                   \
38   const TYPE *src;                                                      \
39   index_type i;                                                         \
40                                                                         \
41   roff /= sizeof (TYPE);                                                \
42   soff /= sizeof (TYPE);                                                \
43                                                                         \
44   src = xsrc;                                                           \
45   src += shift * soff;                                                  \
46   for (i = 0; i < len - shift; ++i)                                     \
47     {                                                                   \
48       *dest = *src;                                                     \
49       dest += roff;                                                     \
50       src += soff;                                                      \
51     }                                                                   \
52                                                                         \
53   src = xsrc;                                                           \
54   for (i = 0; i < shift; ++i)                                           \
55     {                                                                   \
56       *dest = *src;                                                     \
57       dest += roff;                                                     \
58       src += soff;                                                      \
59     }                                                                   \
60 }
61
62 DEF_COPY_LOOP(int, int)
63 DEF_COPY_LOOP(long, long)
64 DEF_COPY_LOOP(double, double)
65 DEF_COPY_LOOP(ldouble, long double)
66
67
68 static void
69 __cshift0 (gfc_array_char * ret, const gfc_array_char * array,
70            ssize_t shift, int which)
71 {
72   /* r.* indicates the return array.  */
73   index_type rstride[GFC_MAX_DIMENSIONS - 1];
74   index_type rstride0;
75   index_type roffset;
76   char *rptr;
77
78   /* s.* indicates the source array.  */
79   index_type sstride[GFC_MAX_DIMENSIONS - 1];
80   index_type sstride0;
81   index_type soffset;
82   const char *sptr;
83
84   index_type count[GFC_MAX_DIMENSIONS - 1];
85   index_type extent[GFC_MAX_DIMENSIONS - 1];
86   index_type dim;
87   index_type size;
88   index_type len;
89   index_type n;
90
91   if (which < 1 || which > GFC_DESCRIPTOR_RANK (array))
92     runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
93
94   size = GFC_DESCRIPTOR_SIZE (ret);
95
96   which = which - 1;
97
98   extent[0] = 1;
99   count[0] = 0;
100   size = GFC_DESCRIPTOR_SIZE (array);
101   n = 0;
102
103   /* Initialized for avoiding compiler warnings.  */
104   roffset = size;
105   soffset = size;
106   len = 0;
107
108   if (ret->data == NULL)
109     {
110       int i;
111
112       ret->data = internal_malloc_size (size * size0 ((array_t *)array));
113       ret->base = 0;
114       ret->dtype = array->dtype;
115       for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
116         {
117           ret->dim[i].lbound = 0;
118           ret->dim[i].ubound = array->dim[i].ubound - array->dim[i].lbound;
119
120           if (i == 0)
121             ret->dim[i].stride = 1;
122           else
123             ret->dim[i].stride = (ret->dim[i-1].ubound + 1) * ret->dim[i-1].stride;
124         }
125     }
126
127   for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
128     {
129       if (dim == which)
130         {
131           roffset = ret->dim[dim].stride * size;
132           if (roffset == 0)
133             roffset = size;
134           soffset = array->dim[dim].stride * size;
135           if (soffset == 0)
136             soffset = size;
137           len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
138         }
139       else
140         {
141           count[n] = 0;
142           extent[n] = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
143           rstride[n] = ret->dim[dim].stride * size;
144           sstride[n] = array->dim[dim].stride * size;
145           n++;
146         }
147     }
148   if (sstride[0] == 0)
149     sstride[0] = size;
150   if (rstride[0] == 0)
151     rstride[0] = size;
152
153   dim = GFC_DESCRIPTOR_RANK (array);
154   rstride0 = rstride[0];
155   sstride0 = sstride[0];
156   rptr = ret->data;
157   sptr = array->data;
158
159   shift = shift % (ssize_t)len;
160   if (shift < 0)
161     shift += len;
162
163   while (rptr)
164     {
165       /* Do the shift for this dimension.  */
166
167       /* If elements are contiguous, perform the operation
168          in two block moves.  */
169       if (soffset == size && roffset == size)
170         {
171           size_t len1 = shift * size;
172           size_t len2 = (len - shift) * size;
173           memcpy (rptr, sptr + len1, len2);
174           memcpy (rptr + len2, sptr, len1);
175         }
176       else
177         {
178           /* Otherwise, we'll have to perform the copy one element at
179              a time.  We can speed this up a tad for common cases of 
180              fundamental types.  */
181           if (size == sizeof(int))
182             copy_loop_int (rptr, sptr, roffset, soffset, len, shift);
183           else if (size == sizeof(long))
184             copy_loop_long (rptr, sptr, roffset, soffset, len, shift);
185           else if (size == sizeof(double))
186             copy_loop_double (rptr, sptr, roffset, soffset, len, shift);
187           else if (size == sizeof(long double))
188             copy_loop_ldouble (rptr, sptr, roffset, soffset, len, shift);
189           else
190             {
191               char *dest = rptr;
192               const char *src = &sptr[shift * soffset];
193
194               for (n = 0; n < len - shift; n++)
195                 {
196                   memcpy (dest, src, size);
197                   dest += roffset;
198                   src += soffset;
199                 }
200               for (src = sptr, n = 0; n < shift; n++)
201                 {
202                   memcpy (dest, src, size);
203                   dest += roffset;
204                   src += soffset;
205                 }
206             }
207         }
208
209       /* Advance to the next section.  */
210       rptr += rstride0;
211       sptr += sstride0;
212       count[0]++;
213       n = 0;
214       while (count[n] == extent[n])
215         {
216           /* When we get to the end of a dimension, reset it and increment
217              the next dimension.  */
218           count[n] = 0;
219           /* We could precalculate these products, but this is a less
220              frequently used path so proabably not worth it.  */
221           rptr -= rstride[n] * extent[n];
222           sptr -= sstride[n] * extent[n];
223           n++;
224           if (n >= dim - 1)
225             {
226               /* Break out of the loop.  */
227               rptr = NULL;
228               break;
229             }
230           else
231             {
232               count[n]++;
233               rptr += rstride[n];
234               sptr += sstride[n];
235             }
236         }
237     }
238 }
239
240
241 void
242 __cshift0_4 (gfc_array_char * ret, const gfc_array_char * array,
243     const GFC_INTEGER_4 * pshift, const GFC_INTEGER_4 * pdim)
244 {
245   __cshift0 (ret, array, *pshift, pdim ? *pdim : 1);
246 }
247
248
249 void
250 __cshift0_8 (gfc_array_char * ret, const gfc_array_char * array,
251     const GFC_INTEGER_8 * pshift, const GFC_INTEGER_8 * pdim)
252 {
253   __cshift0 (ret, array, *pshift, pdim ? *pdim : 1);
254 }