]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/m4/cshift1.m4
re PR fortran/37577 ([meta-bug] change internal array descriptor format for better...
[thirdparty/gcc.git] / libgfortran / m4 / cshift1.m4
1 `/* Implementation of the CSHIFT intrinsic
2 Copyright (C) 2003-2018 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 <string.h>'
28
29 include(iparm.m4)dnl
30
31 `#if defined (HAVE_'atype_name`)
32
33 static void
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)
38 {
39 /* r.* indicates the return array. */
40 index_type rstride[GFC_MAX_DIMENSIONS];
41 index_type rstride0;
42 index_type roffset;
43 char *rptr;
44 char *dest;
45 /* s.* indicates the source array. */
46 index_type sstride[GFC_MAX_DIMENSIONS];
47 index_type sstride0;
48 index_type soffset;
49 const char *sptr;
50 const char *src;
51 /* h.* indicates the shift array. */
52 index_type hstride[GFC_MAX_DIMENSIONS];
53 index_type hstride0;
54 const 'atype_name` *hptr;
55
56 index_type count[GFC_MAX_DIMENSIONS];
57 index_type extent[GFC_MAX_DIMENSIONS];
58 index_type dim;
59 index_type len;
60 index_type n;
61 int which;
62 'atype_name` sh;
63 index_type arraysize;
64 index_type size;
65 index_type 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 GFC_DTYPE_COPY(ret,array);
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 /* See if we should dispatch to a helper function. */
117
118 type_size = GFC_DTYPE_TYPE_SIZE (array);
119
120 switch (type_size)
121 {
122 case GFC_DTYPE_LOGICAL_1:
123 case GFC_DTYPE_INTEGER_1:
124 cshift1_'atype_kind`_i1 ((gfc_array_i1 *)ret, (gfc_array_i1 *) array,
125 h, pwhich);
126 return;
127
128 case GFC_DTYPE_LOGICAL_2:
129 case GFC_DTYPE_INTEGER_2:
130 cshift1_'atype_kind`_i2 ((gfc_array_i2 *)ret, (gfc_array_i2 *) array,
131 h, pwhich);
132 return;
133
134 case GFC_DTYPE_LOGICAL_4:
135 case GFC_DTYPE_INTEGER_4:
136 cshift1_'atype_kind`_i4 ((gfc_array_i4 *)ret, (gfc_array_i4 *) array,
137 h, pwhich);
138 return;
139
140 case GFC_DTYPE_LOGICAL_8:
141 case GFC_DTYPE_INTEGER_8:
142 cshift1_'atype_kind`_i8 ((gfc_array_i8 *)ret, (gfc_array_i8 *) array,
143 h, pwhich);
144 return;
145
146 #if defined (HAVE_INTEGER_16)
147 case GFC_DTYPE_LOGICAL_16:
148 case GFC_DTYPE_INTEGER_16:
149 cshift1_'atype_kind`_i16 ((gfc_array_i16 *)ret, (gfc_array_i16 *) array,
150 h, pwhich);
151 return;
152 #endif
153
154 case GFC_DTYPE_REAL_4:
155 cshift1_'atype_kind`_r4 ((gfc_array_r4 *)ret, (gfc_array_r4 *) array,
156 h, pwhich);
157 return;
158
159 case GFC_DTYPE_REAL_8:
160 cshift1_'atype_kind`_r8 ((gfc_array_r8 *)ret, (gfc_array_r8 *) array,
161 h, pwhich);
162 return;
163
164 #if defined (HAVE_REAL_10)
165 case GFC_DTYPE_REAL_10:
166 cshift1_'atype_kind`_r10 ((gfc_array_r10 *)ret, (gfc_array_r10 *) array,
167 h, pwhich);
168 return;
169 #endif
170
171 #if defined (HAVE_REAL_16)
172 case GFC_DTYPE_REAL_16:
173 cshift1_'atype_kind`_r16 ((gfc_array_r16 *)ret, (gfc_array_r16 *) array,
174 h, pwhich);
175 return;
176 #endif
177
178 case GFC_DTYPE_COMPLEX_4:
179 cshift1_'atype_kind`_c4 ((gfc_array_c4 *)ret, (gfc_array_c4 *) array,
180 h, pwhich);
181 return;
182
183 case GFC_DTYPE_COMPLEX_8:
184 cshift1_'atype_kind`_c8 ((gfc_array_c8 *)ret, (gfc_array_c8 *) array,
185 h, pwhich);
186 return;
187
188 #if defined (HAVE_COMPLEX_10)
189 case GFC_DTYPE_COMPLEX_10:
190 cshift1_'atype_kind`_c10 ((gfc_array_c10 *)ret, (gfc_array_c10 *) array,
191 h, pwhich);
192 return;
193 #endif
194
195 #if defined (HAVE_COMPLEX_16)
196 case GFC_DTYPE_COMPLEX_16:
197 cshift1_'atype_kind`_c16 ((gfc_array_c16 *)ret, (gfc_array_c16 *) array,
198 h, pwhich);
199 return;
200 #endif
201
202 default:
203 break;
204
205 }
206
207 extent[0] = 1;
208 count[0] = 0;
209 n = 0;
210
211 /* Initialized for avoiding compiler warnings. */
212 roffset = size;
213 soffset = size;
214 len = 0;
215
216 for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
217 {
218 if (dim == which)
219 {
220 roffset = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
221 if (roffset == 0)
222 roffset = size;
223 soffset = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
224 if (soffset == 0)
225 soffset = size;
226 len = GFC_DESCRIPTOR_EXTENT(array,dim);
227 }
228 else
229 {
230 count[n] = 0;
231 extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
232 rstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
233 sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
234
235 hstride[n] = GFC_DESCRIPTOR_STRIDE(h,n);
236 n++;
237 }
238 }
239 if (sstride[0] == 0)
240 sstride[0] = size;
241 if (rstride[0] == 0)
242 rstride[0] = size;
243 if (hstride[0] == 0)
244 hstride[0] = 1;
245
246 dim = GFC_DESCRIPTOR_RANK (array);
247 rstride0 = rstride[0];
248 sstride0 = sstride[0];
249 hstride0 = hstride[0];
250 rptr = ret->base_addr;
251 sptr = array->base_addr;
252 hptr = h->base_addr;
253
254 while (rptr)
255 {
256 /* Do the shift for this dimension. */
257 sh = *hptr;
258 /* Normal case should be -len < sh < len; try to
259 avoid the expensive remainder operation if possible. */
260 if (sh < 0)
261 sh += len;
262 if (unlikely (sh >= len || sh < 0))
263 {
264 sh = sh % len;
265 if (sh < 0)
266 sh += len;
267 }
268
269 src = &sptr[sh * soffset];
270 dest = rptr;
271 if (soffset == size && roffset == size)
272 {
273 size_t len1 = sh * size;
274 size_t len2 = (len - sh) * size;
275 memcpy (rptr, sptr + len1, len2);
276 memcpy (rptr + len2, sptr, len1);
277 }
278 else
279 {
280 for (n = 0; n < len - sh; n++)
281 {
282 memcpy (dest, src, size);
283 dest += roffset;
284 src += soffset;
285 }
286 for (src = sptr, n = 0; n < sh; n++)
287 {
288 memcpy (dest, src, size);
289 dest += roffset;
290 src += soffset;
291 }
292 }
293
294 /* Advance to the next section. */
295 rptr += rstride0;
296 sptr += sstride0;
297 hptr += hstride0;
298 count[0]++;
299 n = 0;
300 while (count[n] == extent[n])
301 {
302 /* When we get to the end of a dimension, reset it and increment
303 the next dimension. */
304 count[n] = 0;
305 /* We could precalculate these products, but this is a less
306 frequently used path so probably not worth it. */
307 rptr -= rstride[n] * extent[n];
308 sptr -= sstride[n] * extent[n];
309 hptr -= hstride[n] * extent[n];
310 n++;
311 if (n >= dim - 1)
312 {
313 /* Break out of the loop. */
314 rptr = NULL;
315 break;
316 }
317 else
318 {
319 count[n]++;
320 rptr += rstride[n];
321 sptr += sstride[n];
322 hptr += hstride[n];
323 }
324 }
325 }
326 }
327
328 void cshift1_'atype_kind` (gfc_array_char * const restrict,
329 const gfc_array_char * const restrict,
330 const 'atype` * const restrict,
331 const 'atype_name` * const restrict);
332 export_proto(cshift1_'atype_kind`);
333
334 void
335 cshift1_'atype_kind` (gfc_array_char * const restrict ret,
336 const gfc_array_char * const restrict array,
337 const 'atype` * const restrict h,
338 const 'atype_name` * const restrict pwhich)
339 {
340 cshift1 (ret, array, h, pwhich);
341 }
342
343
344 void cshift1_'atype_kind`_char (gfc_array_char * const restrict ret,
345 GFC_INTEGER_4,
346 const gfc_array_char * const restrict array,
347 const 'atype` * const restrict h,
348 const 'atype_name` * const restrict pwhich,
349 GFC_INTEGER_4);
350 export_proto(cshift1_'atype_kind`_char);
351
352 void
353 cshift1_'atype_kind`_char (gfc_array_char * const restrict ret,
354 GFC_INTEGER_4 ret_length __attribute__((unused)),
355 const gfc_array_char * const restrict array,
356 const 'atype` * const restrict h,
357 const 'atype_name` * const restrict pwhich,
358 GFC_INTEGER_4 array_length __attribute__((unused)))
359 {
360 cshift1 (ret, array, h, pwhich);
361 }
362
363
364 void cshift1_'atype_kind`_char4 (gfc_array_char * const restrict ret,
365 GFC_INTEGER_4,
366 const gfc_array_char * const restrict array,
367 const 'atype` * const restrict h,
368 const 'atype_name` * const restrict pwhich,
369 GFC_INTEGER_4);
370 export_proto(cshift1_'atype_kind`_char4);
371
372 void
373 cshift1_'atype_kind`_char4 (gfc_array_char * const restrict ret,
374 GFC_INTEGER_4 ret_length __attribute__((unused)),
375 const gfc_array_char * const restrict array,
376 const 'atype` * const restrict h,
377 const 'atype_name` * const restrict pwhich,
378 GFC_INTEGER_4 array_length __attribute__((unused)))
379 {
380 cshift1 (ret, array, h, pwhich);
381 }
382
383 #endif'