PR libfortran/18966
[official-gcc.git] / libgfortran / intrinsics / cshift0.c
blobf712629d5df96f53be46d35c5181c8a2932a0c89
1 /* Generic implementation of the CSHIFT intrinsic
2 Copyright 2003 Free Software Foundation, Inc.
3 Contributed by Feng Wang <wf_cs@yahoo.com>
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
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.
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.
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. */
22 #include "config.h"
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <string.h>
26 #include "libgfortran.h"
29 /* "Templatized" helper function for the inner shift loop. */
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; \
41 roff /= sizeof (TYPE); \
42 soff /= sizeof (TYPE); \
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 } \
53 src = xsrc; \
54 for (i = 0; i < shift; ++i) \
55 { \
56 *dest = *src; \
57 dest += roff; \
58 src += soff; \
59 } \
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)
68 static void
69 cshift0 (gfc_array_char * ret, const gfc_array_char * array,
70 ssize_t shift, int which)
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;
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;
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;
91 if (which < 1 || which > GFC_DESCRIPTOR_RANK (array))
92 runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
94 size = GFC_DESCRIPTOR_SIZE (ret);
96 which = which - 1;
98 extent[0] = 1;
99 count[0] = 0;
100 size = GFC_DESCRIPTOR_SIZE (array);
101 n = 0;
103 /* Initialized for avoiding compiler warnings. */
104 roffset = size;
105 soffset = size;
106 len = 0;
108 if (ret->data == NULL)
110 int i;
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++)
117 ret->dim[i].lbound = 0;
118 ret->dim[i].ubound = array->dim[i].ubound - array->dim[i].lbound;
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;
127 for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
129 if (dim == which)
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;
139 else
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++;
148 if (sstride[0] == 0)
149 sstride[0] = size;
150 if (rstride[0] == 0)
151 rstride[0] = size;
153 dim = GFC_DESCRIPTOR_RANK (array);
154 rstride0 = rstride[0];
155 sstride0 = sstride[0];
156 rptr = ret->data;
157 sptr = array->data;
159 shift = shift % (ssize_t)len;
160 if (shift < 0)
161 shift += len;
163 while (rptr)
165 /* Do the shift for this dimension. */
167 /* If elements are contiguous, perform the operation
168 in two block moves. */
169 if (soffset == size && roffset == size)
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);
176 else
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
191 char *dest = rptr;
192 const char *src = &sptr[shift * soffset];
194 for (n = 0; n < len - shift; n++)
196 memcpy (dest, src, size);
197 dest += roffset;
198 src += soffset;
200 for (src = sptr, n = 0; n < shift; n++)
202 memcpy (dest, src, size);
203 dest += roffset;
204 src += soffset;
209 /* Advance to the next section. */
210 rptr += rstride0;
211 sptr += sstride0;
212 count[0]++;
213 n = 0;
214 while (count[n] == extent[n])
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)
226 /* Break out of the loop. */
227 rptr = NULL;
228 break;
230 else
232 count[n]++;
233 rptr += rstride[n];
234 sptr += sstride[n];
241 extern void cshift0_1 (gfc_array_char *, const gfc_array_char *,
242 const GFC_INTEGER_1 *, const GFC_INTEGER_1 *);
243 export_proto(cshift0_1);
245 void
246 cshift0_1 (gfc_array_char *ret, const gfc_array_char *array,
247 const GFC_INTEGER_1 *pshift, const GFC_INTEGER_1 *pdim)
249 cshift0 (ret, array, *pshift, pdim ? *pdim : 1);
253 extern void cshift0_2 (gfc_array_char *, const gfc_array_char *,
254 const GFC_INTEGER_2 *, const GFC_INTEGER_2 *);
255 export_proto(cshift0_2);
257 void
258 cshift0_2 (gfc_array_char *ret, const gfc_array_char *array,
259 const GFC_INTEGER_2 *pshift, const GFC_INTEGER_2 *pdim)
261 cshift0 (ret, array, *pshift, pdim ? *pdim : 1);
265 extern void cshift0_4 (gfc_array_char *, const gfc_array_char *,
266 const GFC_INTEGER_4 *, const GFC_INTEGER_4 *);
267 export_proto(cshift0_4);
269 void
270 cshift0_4 (gfc_array_char *ret, const gfc_array_char *array,
271 const GFC_INTEGER_4 *pshift, const GFC_INTEGER_4 *pdim)
273 cshift0 (ret, array, *pshift, pdim ? *pdim : 1);
277 extern void cshift0_8 (gfc_array_char *, const gfc_array_char *,
278 const GFC_INTEGER_8 *, const GFC_INTEGER_8 *);
279 export_proto(cshift0_8);
281 void
282 cshift0_8 (gfc_array_char *ret, const gfc_array_char *array,
283 const GFC_INTEGER_8 *pshift, const GFC_INTEGER_8 *pdim)
285 cshift0 (ret, array, *pshift, pdim ? *pdim : 1);