1 `/* Implementation of the CSHIFT intrinsic
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
3 Contributed by Feng Wang <wf_cs@yahoo.com>
5 This file is part of the GNU Fortran 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 Ligbfortran 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 `#if defined (HAVE_'atype_name`)
34 cshift1 (gfc_array_char * const restrict ret,
35 const gfc_array_char * const restrict array,
36 const 'atype` * const restrict h,
37 const 'atype_name` * const restrict pwhich)
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];
51 /* h.* indicates the shift array. */
52 index_type hstride[GFC_MAX_DIMENSIONS];
54 const 'atype_name` *hptr;
56 index_type count[GFC_MAX_DIMENSIONS];
57 index_type extent[GFC_MAX_DIMENSIONS];
71 if (which < 0 || (which + 1) > GFC_DESCRIPTOR_RANK (array))
72 runtime_error ("Argument ''`DIM''` is out of range in call to ''`CSHIFT''`");
74 size = GFC_DESCRIPTOR_SIZE(array);
76 arraysize = size0 ((array_t *)array);
78 if (ret->base_addr == NULL)
82 ret->base_addr = xmallocarray (arraysize, size);
84 ret->dtype = array->dtype;
85 for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
89 ub = GFC_DESCRIPTOR_EXTENT(array,i) - 1;
94 str = GFC_DESCRIPTOR_EXTENT(ret,i-1) *
95 GFC_DESCRIPTOR_STRIDE(ret,i-1);
97 GFC_DIMENSION_SET(ret->dim[i], 0, ub, str);
100 else if (unlikely (compile_options.bounds_check))
102 bounds_equal_extents ((array_t *) ret, (array_t *) array,
103 "return value", "CSHIFT");
106 if (unlikely (compile_options.bounds_check))
108 bounds_reduced_extents ((array_t *) h, (array_t *) array, which,
109 "SHIFT argument", "CSHIFT");
119 /* Initialized for avoiding compiler warnings. */
124 for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
128 roffset = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
131 soffset = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
134 len = GFC_DESCRIPTOR_EXTENT(array,dim);
139 extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
140 rstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
141 sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
143 hstride[n] = GFC_DESCRIPTOR_STRIDE(h,n);
154 dim = GFC_DESCRIPTOR_RANK (array);
155 rstride0 = rstride[0];
156 sstride0 = sstride[0];
157 hstride0 = hstride[0];
158 rptr = ret->base_addr;
159 sptr = array->base_addr;
164 /* Do the shift for this dimension. */
166 sh = (div (sh, len)).rem;
170 src = &sptr[sh * soffset];
173 for (n = 0; n < len; n++)
175 memcpy (dest, src, size);
177 if (n == len - sh - 1)
183 /* Advance to the next section. */
189 while (count[n] == extent[n])
191 /* When we get to the end of a dimension, reset it and increment
192 the next dimension. */
194 /* We could precalculate these products, but this is a less
195 frequently used path so probably not worth it. */
196 rptr -= rstride[n] * extent[n];
197 sptr -= sstride[n] * extent[n];
198 hptr -= hstride[n] * extent[n];
202 /* Break out of the loop. */
217 void cshift1_'atype_kind` (gfc_array_char * const restrict,
218 const gfc_array_char * const restrict,
219 const 'atype` * const restrict,
220 const 'atype_name` * const restrict);
221 export_proto(cshift1_'atype_kind`);
224 cshift1_'atype_kind` (gfc_array_char * const restrict ret,
225 const gfc_array_char * const restrict array,
226 const 'atype` * const restrict h,
227 const 'atype_name` * const restrict pwhich)
229 cshift1 (ret, array, h, pwhich);
233 void cshift1_'atype_kind`_char (gfc_array_char * const restrict ret,
235 const gfc_array_char * const restrict array,
236 const 'atype` * const restrict h,
237 const 'atype_name` * const restrict pwhich,
239 export_proto(cshift1_'atype_kind`_char);
242 cshift1_'atype_kind`_char (gfc_array_char * const restrict ret,
243 GFC_INTEGER_4 ret_length __attribute__((unused)),
244 const gfc_array_char * const restrict array,
245 const 'atype` * const restrict h,
246 const 'atype_name` * const restrict pwhich,
247 GFC_INTEGER_4 array_length __attribute__((unused)))
249 cshift1 (ret, array, h, pwhich);
253 void cshift1_'atype_kind`_char4 (gfc_array_char * const restrict ret,
255 const gfc_array_char * const restrict array,
256 const 'atype` * const restrict h,
257 const 'atype_name` * const restrict pwhich,
259 export_proto(cshift1_'atype_kind`_char4);
262 cshift1_'atype_kind`_char4 (gfc_array_char * const restrict ret,
263 GFC_INTEGER_4 ret_length __attribute__((unused)),
264 const gfc_array_char * const restrict array,
265 const 'atype` * const restrict h,
266 const 'atype_name` * const restrict pwhich,
267 GFC_INTEGER_4 array_length __attribute__((unused)))
269 cshift1 (ret, array, h, pwhich);