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>
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 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.
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.
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.
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/>. */
26 #include "libgfortran.h"
31 /* TODO: make this work for large shifts when
32 sizeof(int) < sizeof (index_type). */
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)
39 /* r.* indicates the return array. */
40 index_type rstride[GFC_MAX_DIMENSIONS];
45 /* s.* indicates the source array. */
46 index_type sstride[GFC_MAX_DIMENSIONS];
52 index_type count[GFC_MAX_DIMENSIONS];
53 index_type extent[GFC_MAX_DIMENSIONS];
58 /* The compiler cannot figure out that these are set, initialize
59 them to avoid warnings. */
64 if (ret->data == NULL)
68 ret->data = internal_malloc_size (size * size0 ((array_t *)array));
70 ret->dtype = array->dtype;
71 for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
73 ret->dim[i].lbound = 0;
74 ret->dim[i].ubound = array->dim[i].ubound - array->dim[i].lbound;
77 ret->dim[i].stride = 1;
79 ret->dim[i].stride = (ret->dim[i-1].ubound + 1) * ret->dim[i-1].stride;
84 if (size0 ((array_t *) ret) == 0)
95 for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
99 roffset = ret->dim[dim].stride * size;
102 soffset = array->dim[dim].stride * size;
105 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
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;
121 dim = GFC_DESCRIPTOR_RANK (array);
122 rstride0 = rstride[0];
123 sstride0 = sstride[0];
127 if ((shift >= 0 ? shift : -shift) > len)
142 /* Do the shift for this dimension. */
145 src = &sptr[shift * soffset];
151 dest = &rptr[-shift * roffset];
153 for (n = 0; n < len; n++)
155 memcpy (dest, src, size);
172 memcpy (dest, pbound, size);
181 memset (dest, filler[0], size);
183 for (i = 0; i < size ; i += filler_len)
184 memcpy (&dest[i], filler, filler_len);
189 /* Advance to the next section. */
194 while (count[n] == extent[n])
196 /* When we get to the end of a dimension, reset it and increment
197 the next dimension. */
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];
206 /* Break out of the loop. */
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); \
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) \
232 eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
233 GFC_DESCRIPTOR_SIZE (array), "\0", 1); \
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, \
241 export_proto(eoshift0_##N##_char); \
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))) \
253 eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
254 array_length, " ", 1); \
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, \
262 export_proto(eoshift0_##N##_char4); \
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))) \
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)); \
284 #ifdef HAVE_GFC_INTEGER_16