1 `/* Helper function for cshift functions.
2 Copyright 2008, 2009 Free Software Foundation, Inc.
3 Contributed by Thomas Koenig <tkoenig@gcc.gnu.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"
33 `#if defined (HAVE_'rtype_name`)
36 cshift0_'rtype_code` ('rtype` *ret, const 'rtype` *array, ssize_t shift,
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];
49 const 'rtype_name` *sptr;
51 index_type count[GFC_MAX_DIMENSIONS];
52 index_type extent[GFC_MAX_DIMENSIONS];
64 /* Initialized for avoiding compiler warnings. */
69 for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
73 roffset = GFC_DESCRIPTOR_STRIDE(ret,dim);
76 soffset = GFC_DESCRIPTOR_STRIDE(array,dim);
79 len = GFC_DESCRIPTOR_EXTENT(array,dim);
84 extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
85 rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,dim);
86 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,dim);
95 dim = GFC_DESCRIPTOR_RANK (array);
96 rstride0 = rstride[0];
97 sstride0 = sstride[0];
101 shift = len == 0 ? 0 : shift % (ssize_t)len;
107 /* Do the shift for this dimension. */
109 /* If elements are contiguous, perform the operation
110 in two block moves. */
111 if (soffset == 1 && roffset == 1)
113 size_t len1 = shift * sizeof ('rtype_name`);
114 size_t len2 = (len - shift) * sizeof ('rtype_name`);
115 memcpy (rptr, sptr + shift, len2);
116 memcpy (rptr + (len - shift), sptr, len1);
120 /* Otherwise, we will have to perform the copy one element at
122 'rtype_name` *dest = rptr;
123 const 'rtype_name` *src = &sptr[shift * soffset];
125 for (n = 0; n < len - shift; n++)
131 for (src = sptr, n = 0; n < shift; n++)
139 /* Advance to the next section. */
144 while (count[n] == extent[n])
146 /* When we get to the end of a dimension, reset it and increment
147 the next dimension. */
149 /* We could precalculate these products, but this is a less
150 frequently used path so probably not worth it. */
151 rptr -= rstride[n] * extent[n];
152 sptr -= sstride[n] * extent[n];
156 /* Break out of the loop. */