1 `/* Helper function for cshift functions.
2 Copyright 2008 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 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
31 #include "libgfortran.h"
38 `#if defined (HAVE_'rtype_name`)
41 cshift0_'rtype_code` ('rtype` *ret, const 'rtype` *array, ssize_t shift,
44 /* r.* indicates the return array. */
45 index_type rstride[GFC_MAX_DIMENSIONS];
50 /* s.* indicates the source array. */
51 index_type sstride[GFC_MAX_DIMENSIONS];
54 const 'rtype_name` *sptr;
56 index_type count[GFC_MAX_DIMENSIONS];
57 index_type extent[GFC_MAX_DIMENSIONS];
69 /* Initialized for avoiding compiler warnings. */
74 for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
78 roffset = ret->dim[dim].stride;
81 soffset = array->dim[dim].stride;
84 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
89 extent[n] = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
90 rstride[n] = ret->dim[dim].stride;
91 sstride[n] = array->dim[dim].stride;
100 dim = GFC_DESCRIPTOR_RANK (array);
101 rstride0 = rstride[0];
102 sstride0 = sstride[0];
106 shift = len == 0 ? 0 : shift % (ssize_t)len;
112 /* Do the shift for this dimension. */
114 /* If elements are contiguous, perform the operation
115 in two block moves. */
116 if (soffset == 1 && roffset == 1)
118 size_t len1 = shift * sizeof ('rtype_name`);
119 size_t len2 = (len - shift) * sizeof ('rtype_name`);
120 memcpy (rptr, sptr + shift, len2);
121 memcpy (rptr + (len - shift), sptr, len1);
125 /* Otherwise, we will have to perform the copy one element at
127 'rtype_name` *dest = rptr;
128 const 'rtype_name` *src = &sptr[shift * soffset];
130 for (n = 0; n < len - shift; n++)
136 for (src = sptr, n = 0; n < shift; n++)
144 /* Advance to the next section. */
149 while (count[n] == extent[n])
151 /* When we get to the end of a dimension, reset it and increment
152 the next dimension. */
154 /* We could precalculate these products, but this is a less
155 frequently used path so probably not worth it. */
156 rptr -= rstride[n] * extent[n];
157 sptr -= sstride[n] * extent[n];
161 /* Break out of the loop. */