1 /* Generic implementation of the EOSHIFT intrinsic
2 Copyright (C) 2002-2017 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
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"
29 /* TODO: make this work for large shifts when
30 sizeof(int) < sizeof (index_type). */
33 eoshift2 (gfc_array_char
*ret
, const gfc_array_char
*array
,
34 int shift
, const gfc_array_char
*bound
, int which
,
35 const char *filler
, index_type filler_len
)
37 /* r.* indicates the return array. */
38 index_type rstride
[GFC_MAX_DIMENSIONS
];
43 /* s.* indicates the source array. */
44 index_type sstride
[GFC_MAX_DIMENSIONS
];
49 /* b.* indicates the bound array. */
50 index_type bstride
[GFC_MAX_DIMENSIONS
];
54 index_type count
[GFC_MAX_DIMENSIONS
];
55 index_type extent
[GFC_MAX_DIMENSIONS
];
62 /* The compiler cannot figure out that these are set, initialize
63 them to avoid warnings. */
68 size
= GFC_DESCRIPTOR_SIZE (array
);
70 arraysize
= size0 ((array_t
*) array
);
72 if (ret
->base_addr
== NULL
)
77 ret
->dtype
= array
->dtype
;
79 /* xmallocarray allocates a single byte for zero size. */
80 ret
->base_addr
= xmallocarray (arraysize
, size
);
82 for (i
= 0; i
< GFC_DESCRIPTOR_RANK (array
); i
++)
86 ub
= GFC_DESCRIPTOR_EXTENT(array
,i
) - 1;
91 str
= GFC_DESCRIPTOR_EXTENT(ret
,i
-1)
92 * GFC_DESCRIPTOR_STRIDE(ret
,i
-1);
94 GFC_DIMENSION_SET(ret
->dim
[i
], 0, ub
, str
);
97 else if (unlikely (compile_options
.bounds_check
))
99 bounds_equal_extents ((array_t
*) ret
, (array_t
*) array
,
100 "return value", "EOSHIFT");
114 for (dim
= 0; dim
< GFC_DESCRIPTOR_RANK (array
); dim
++)
118 roffset
= GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
121 soffset
= GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
124 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
129 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,dim
);
130 rstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
131 sstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
133 bstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(bound
,n
);
143 if (bound
&& bstride
[0] == 0)
146 dim
= GFC_DESCRIPTOR_RANK (array
);
147 rstride0
= rstride
[0];
148 sstride0
= sstride
[0];
149 bstride0
= bstride
[0];
150 rptr
= ret
->base_addr
;
151 sptr
= array
->base_addr
;
153 if ((shift
>= 0 ? shift
: -shift
) > len
)
167 bptr
= bound
->base_addr
;
173 /* Do the shift for this dimension. */
176 src
= &sptr
[shift
* soffset
];
182 dest
= &rptr
[-shift
* roffset
];
184 for (n
= 0; n
< len
; n
++)
186 memcpy (dest
, src
, size
);
203 memcpy (dest
, bptr
, size
);
212 memset (dest
, filler
[0], size
);
214 for (i
= 0; i
< size
; i
+= filler_len
)
215 memcpy (&dest
[i
], filler
, filler_len
);
220 /* Advance to the next section. */
226 while (count
[n
] == extent
[n
])
228 /* When we get to the end of a dimension, reset it and increment
229 the next dimension. */
231 /* We could precalculate these products, but this is a less
232 frequently used path so probably not worth it. */
233 rptr
-= rstride
[n
] * extent
[n
];
234 sptr
-= sstride
[n
] * extent
[n
];
235 bptr
-= bstride
[n
] * extent
[n
];
239 /* Break out of the loop. */
255 #define DEFINE_EOSHIFT(N) \
256 extern void eoshift2_##N (gfc_array_char *, const gfc_array_char *, \
257 const GFC_INTEGER_##N *, const gfc_array_char *, \
258 const GFC_INTEGER_##N *); \
259 export_proto(eoshift2_##N); \
262 eoshift2_##N (gfc_array_char *ret, const gfc_array_char *array, \
263 const GFC_INTEGER_##N *pshift, const gfc_array_char *pbound, \
264 const GFC_INTEGER_##N *pdim) \
266 eoshift2 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
270 extern void eoshift2_##N##_char (gfc_array_char *, GFC_INTEGER_4, \
271 const gfc_array_char *, \
272 const GFC_INTEGER_##N *, \
273 const gfc_array_char *, \
274 const GFC_INTEGER_##N *, \
275 GFC_INTEGER_4, GFC_INTEGER_4); \
276 export_proto(eoshift2_##N##_char); \
279 eoshift2_##N##_char (gfc_array_char *ret, \
280 GFC_INTEGER_4 ret_length __attribute__((unused)), \
281 const gfc_array_char *array, \
282 const GFC_INTEGER_##N *pshift, \
283 const gfc_array_char *pbound, \
284 const GFC_INTEGER_##N *pdim, \
285 GFC_INTEGER_4 array_length __attribute__((unused)), \
286 GFC_INTEGER_4 bound_length __attribute__((unused))) \
288 eoshift2 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
292 extern void eoshift2_##N##_char4 (gfc_array_char *, GFC_INTEGER_4, \
293 const gfc_array_char *, \
294 const GFC_INTEGER_##N *, \
295 const gfc_array_char *, \
296 const GFC_INTEGER_##N *, \
297 GFC_INTEGER_4, GFC_INTEGER_4); \
298 export_proto(eoshift2_##N##_char4); \
301 eoshift2_##N##_char4 (gfc_array_char *ret, \
302 GFC_INTEGER_4 ret_length __attribute__((unused)), \
303 const gfc_array_char *array, \
304 const GFC_INTEGER_##N *pshift, \
305 const gfc_array_char *pbound, \
306 const GFC_INTEGER_##N *pdim, \
307 GFC_INTEGER_4 array_length __attribute__((unused)), \
308 GFC_INTEGER_4 bound_length __attribute__((unused))) \
310 static const gfc_char4_t space = (unsigned char) ' '; \
311 eoshift2 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
312 (const char *) &space, \
313 sizeof (gfc_char4_t)); \
320 #ifdef HAVE_GFC_INTEGER_16