1 `/* 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. */
26 #include "libgfortran.h"'
28 define(htype_kind, regexp(file, `_\([0-9]+\)\.', `\1'))dnl
29 define(htype_code,`i'rtype_name)dnl
30 define(htype,get_arraytype(i,htype_kind))dnl
31 define(htype_name, get_typename(i,htype_kind))dnl
34 `__cshift1_'htype_kind (const gfc_array_char * ret, const gfc_array_char * array,
35 const htype * h, const htype_name * pwhich)
37 /* r.* indicates the return array. */
38 index_type rstride[GFC_MAX_DIMENSIONS - 1];
43 /* s.* indicates the source array. */
44 index_type sstride[GFC_MAX_DIMENSIONS - 1];
49 ` /* h.* indicates the shift array. */'
50 index_type hstride[GFC_MAX_DIMENSIONS - 1];
52 const htype_name *hptr;
54 index_type count[GFC_MAX_DIMENSIONS - 1];
55 index_type extent[GFC_MAX_DIMENSIONS - 1];
68 if (which < 0 || (which + 1) > GFC_DESCRIPTOR_RANK (array))
69 runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
71 size = GFC_DESCRIPTOR_SIZE (ret);
75 size = GFC_DESCRIPTOR_SIZE (array);
78 `/* Initialized for avoiding compiler warnings. */'
83 for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
87 roffset = ret->dim[dim].stride * size;
90 soffset = array->dim[dim].stride * size;
93 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
98 extent[n] = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
99 rstride[n] = ret->dim[dim].stride * size;
100 sstride[n] = array->dim[dim].stride * size;
102 hstride[n] = h->dim[n].stride;
113 dim = GFC_DESCRIPTOR_RANK (array);
114 rstride0 = rstride[0];
115 sstride0 = sstride[0];
116 hstride0 = hstride[0];
123 ` /* Do the shift for this dimension. */'
125 sh = (div (sh, len)).rem;
129 src = &sptr[sh * soffset];
132 for (n = 0; n < len; n++)
134 memcpy (dest, src, size);
136 if (n == len - sh - 1)
142 /* Advance to the next section. */
148 while (count[n] == extent[n])
150 /* When we get to the end of a dimension, reset it and increment
151 the next dimension. */
153 /* We could precalculate these products, but this is a less
154 frequently used path so proabably not worth it. */
155 rptr -= rstride[n] * extent[n];
156 sptr -= sstride[n] * extent[n];
157 hptr -= hstride[n] * extent[n];
161 /* Break out of the loop. */