1 /* Implementation of the CSHIFT intrinsic
2    Copyright (C) 2003-2016 Free Software Foundation, Inc.
3    Contributed by Feng Wang <wf_cs@yahoo.com>
4 
5 This file is part of the GNU Fortran runtime library (libgfortran).
6 
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.
11 
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.
16 
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.
20 
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/>.  */
25 
26 #include "libgfortran.h"
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <string.h>
30 
31 
32 #if defined (HAVE_GFC_INTEGER_4)
33 
34 static void
cshift1(gfc_array_char * const restrict ret,const gfc_array_char * const restrict array,const gfc_array_i4 * const restrict h,const GFC_INTEGER_4 * const restrict pwhich)35 cshift1 (gfc_array_char * const restrict ret,
36 	const gfc_array_char * const restrict array,
37 	const gfc_array_i4 * const restrict h,
38 	const GFC_INTEGER_4 * const restrict pwhich)
39 {
40   /* r.* indicates the return array.  */
41   index_type rstride[GFC_MAX_DIMENSIONS];
42   index_type rstride0;
43   index_type roffset;
44   char *rptr;
45   char *dest;
46   /* s.* indicates the source array.  */
47   index_type sstride[GFC_MAX_DIMENSIONS];
48   index_type sstride0;
49   index_type soffset;
50   const char *sptr;
51   const char *src;
52   /* h.* indicates the shift array.  */
53   index_type hstride[GFC_MAX_DIMENSIONS];
54   index_type hstride0;
55   const GFC_INTEGER_4 *hptr;
56 
57   index_type count[GFC_MAX_DIMENSIONS];
58   index_type extent[GFC_MAX_DIMENSIONS];
59   index_type dim;
60   index_type len;
61   index_type n;
62   int which;
63   GFC_INTEGER_4 sh;
64   index_type arraysize;
65   index_type size;
66 
67   if (pwhich)
68     which = *pwhich - 1;
69   else
70     which = 0;
71 
72   if (which < 0 || (which + 1) > GFC_DESCRIPTOR_RANK (array))
73     runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
74 
75   size = GFC_DESCRIPTOR_SIZE(array);
76 
77   arraysize = size0 ((array_t *)array);
78 
79   if (ret->base_addr == NULL)
80     {
81       int i;
82 
83       ret->base_addr = xmallocarray (arraysize, size);
84       ret->offset = 0;
85       ret->dtype = array->dtype;
86       for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
87         {
88 	  index_type ub, str;
89 
90           ub = GFC_DESCRIPTOR_EXTENT(array,i) - 1;
91 
92           if (i == 0)
93             str = 1;
94           else
95 	    str = GFC_DESCRIPTOR_EXTENT(ret,i-1) *
96 	      GFC_DESCRIPTOR_STRIDE(ret,i-1);
97 
98 	  GFC_DIMENSION_SET(ret->dim[i], 0, ub, str);
99         }
100     }
101   else if (unlikely (compile_options.bounds_check))
102     {
103       bounds_equal_extents ((array_t *) ret, (array_t *) array,
104 				 "return value", "CSHIFT");
105     }
106 
107   if (unlikely (compile_options.bounds_check))
108     {
109       bounds_reduced_extents ((array_t *) h, (array_t *) array, which,
110       			      "SHIFT argument", "CSHIFT");
111     }
112 
113   if (arraysize == 0)
114     return;
115 
116   extent[0] = 1;
117   count[0] = 0;
118   n = 0;
119 
120   /* Initialized for avoiding compiler warnings.  */
121   roffset = size;
122   soffset = size;
123   len = 0;
124 
125   for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
126     {
127       if (dim == which)
128         {
129           roffset = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
130           if (roffset == 0)
131             roffset = size;
132           soffset = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
133           if (soffset == 0)
134             soffset = size;
135           len = GFC_DESCRIPTOR_EXTENT(array,dim);
136         }
137       else
138         {
139           count[n] = 0;
140           extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
141           rstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
142           sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
143 
144           hstride[n] = GFC_DESCRIPTOR_STRIDE(h,n);
145           n++;
146         }
147     }
148   if (sstride[0] == 0)
149     sstride[0] = size;
150   if (rstride[0] == 0)
151     rstride[0] = size;
152   if (hstride[0] == 0)
153     hstride[0] = 1;
154 
155   dim = GFC_DESCRIPTOR_RANK (array);
156   rstride0 = rstride[0];
157   sstride0 = sstride[0];
158   hstride0 = hstride[0];
159   rptr = ret->base_addr;
160   sptr = array->base_addr;
161   hptr = h->base_addr;
162 
163   while (rptr)
164     {
165       /* Do the shift for this dimension.  */
166       sh = *hptr;
167       sh = (div (sh, len)).rem;
168       if (sh < 0)
169         sh += len;
170 
171       src = &sptr[sh * soffset];
172       dest = rptr;
173 
174       for (n = 0; n < len; n++)
175         {
176           memcpy (dest, src, size);
177           dest += roffset;
178           if (n == len - sh - 1)
179             src = sptr;
180           else
181             src += soffset;
182         }
183 
184       /* Advance to the next section.  */
185       rptr += rstride0;
186       sptr += sstride0;
187       hptr += hstride0;
188       count[0]++;
189       n = 0;
190       while (count[n] == extent[n])
191         {
192           /* When we get to the end of a dimension, reset it and increment
193              the next dimension.  */
194           count[n] = 0;
195           /* We could precalculate these products, but this is a less
196              frequently used path so probably not worth it.  */
197           rptr -= rstride[n] * extent[n];
198           sptr -= sstride[n] * extent[n];
199 	  hptr -= hstride[n] * extent[n];
200           n++;
201           if (n >= dim - 1)
202             {
203               /* Break out of the loop.  */
204               rptr = NULL;
205               break;
206             }
207           else
208             {
209               count[n]++;
210               rptr += rstride[n];
211               sptr += sstride[n];
212 	      hptr += hstride[n];
213             }
214         }
215     }
216 }
217 
218 void cshift1_4 (gfc_array_char * const restrict,
219 	const gfc_array_char * const restrict,
220 	const gfc_array_i4 * const restrict,
221 	const GFC_INTEGER_4 * const restrict);
222 export_proto(cshift1_4);
223 
224 void
cshift1_4(gfc_array_char * const restrict ret,const gfc_array_char * const restrict array,const gfc_array_i4 * const restrict h,const GFC_INTEGER_4 * const restrict pwhich)225 cshift1_4 (gfc_array_char * const restrict ret,
226 	const gfc_array_char * const restrict array,
227 	const gfc_array_i4 * const restrict h,
228 	const GFC_INTEGER_4 * const restrict pwhich)
229 {
230   cshift1 (ret, array, h, pwhich);
231 }
232 
233 
234 void cshift1_4_char (gfc_array_char * const restrict ret,
235 	GFC_INTEGER_4,
236 	const gfc_array_char * const restrict array,
237 	const gfc_array_i4 * const restrict h,
238 	const GFC_INTEGER_4 * const restrict pwhich,
239 	GFC_INTEGER_4);
240 export_proto(cshift1_4_char);
241 
242 void
cshift1_4_char(gfc_array_char * const restrict ret,GFC_INTEGER_4 ret_length,const gfc_array_char * const restrict array,const gfc_array_i4 * const restrict h,const GFC_INTEGER_4 * const restrict pwhich,GFC_INTEGER_4 array_length)243 cshift1_4_char (gfc_array_char * const restrict ret,
244 	GFC_INTEGER_4 ret_length __attribute__((unused)),
245 	const gfc_array_char * const restrict array,
246 	const gfc_array_i4 * const restrict h,
247 	const GFC_INTEGER_4 * const restrict pwhich,
248 	GFC_INTEGER_4 array_length __attribute__((unused)))
249 {
250   cshift1 (ret, array, h, pwhich);
251 }
252 
253 
254 void cshift1_4_char4 (gfc_array_char * const restrict ret,
255 	GFC_INTEGER_4,
256 	const gfc_array_char * const restrict array,
257 	const gfc_array_i4 * const restrict h,
258 	const GFC_INTEGER_4 * const restrict pwhich,
259 	GFC_INTEGER_4);
260 export_proto(cshift1_4_char4);
261 
262 void
cshift1_4_char4(gfc_array_char * const restrict ret,GFC_INTEGER_4 ret_length,const gfc_array_char * const restrict array,const gfc_array_i4 * const restrict h,const GFC_INTEGER_4 * const restrict pwhich,GFC_INTEGER_4 array_length)263 cshift1_4_char4 (gfc_array_char * const restrict ret,
264 	GFC_INTEGER_4 ret_length __attribute__((unused)),
265 	const gfc_array_char * const restrict array,
266 	const gfc_array_i4 * const restrict h,
267 	const GFC_INTEGER_4 * const restrict pwhich,
268 	GFC_INTEGER_4 array_length __attribute__((unused)))
269 {
270   cshift1 (ret, array, h, pwhich);
271 }
272 
273 #endif
274