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 Libgfortran 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 eoshift0 (gfc_array_char
* ret
, const gfc_array_char
* array
,
34 int shift
, const char * pbound
, int which
, index_type size
,
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
];
50 index_type count
[GFC_MAX_DIMENSIONS
];
51 index_type extent
[GFC_MAX_DIMENSIONS
];
57 /* The compiler cannot figure out that these are set, initialize
58 them to avoid warnings. */
63 arraysize
= size0 ((array_t
*) array
);
65 if (ret
->base_addr
== NULL
)
70 ret
->dtype
= array
->dtype
;
71 for (i
= 0; i
< GFC_DESCRIPTOR_RANK (array
); i
++)
75 ub
= GFC_DESCRIPTOR_EXTENT(array
,i
) - 1;
80 str
= GFC_DESCRIPTOR_EXTENT(ret
,i
-1)
81 * GFC_DESCRIPTOR_STRIDE(ret
,i
-1);
83 GFC_DIMENSION_SET(ret
->dim
[i
], 0, ub
, str
);
87 /* xmallocarray allocates a single byte for zero size. */
88 ret
->base_addr
= xmallocarray (arraysize
, size
);
90 else if (unlikely (compile_options
.bounds_check
))
92 bounds_equal_extents ((array_t
*) ret
, (array_t
*) array
,
93 "return value", "EOSHIFT");
106 for (dim
= 0; dim
< GFC_DESCRIPTOR_RANK (array
); dim
++)
110 roffset
= GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
113 soffset
= GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
116 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
121 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,dim
);
122 rstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
123 sstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
132 dim
= GFC_DESCRIPTOR_RANK (array
);
133 rstride0
= rstride
[0];
134 sstride0
= sstride
[0];
135 rptr
= ret
->base_addr
;
136 sptr
= array
->base_addr
;
138 if ((shift
>= 0 ? shift
: -shift
) > len
)
153 /* Do the shift for this dimension. */
156 src
= &sptr
[shift
* soffset
];
162 dest
= &rptr
[-shift
* roffset
];
164 for (n
= 0; n
< len
; n
++)
166 memcpy (dest
, src
, size
);
183 memcpy (dest
, pbound
, size
);
192 memset (dest
, filler
[0], size
);
194 for (i
= 0; i
< size
; i
+= filler_len
)
195 memcpy (&dest
[i
], filler
, filler_len
);
200 /* Advance to the next section. */
205 while (count
[n
] == extent
[n
])
207 /* When we get to the end of a dimension, reset it and increment
208 the next dimension. */
210 /* We could precalculate these products, but this is a less
211 frequently used path so probably not worth it. */
212 rptr
-= rstride
[n
] * extent
[n
];
213 sptr
-= sstride
[n
] * extent
[n
];
217 /* Break out of the loop. */
232 #define DEFINE_EOSHIFT(N) \
233 extern void eoshift0_##N (gfc_array_char *, const gfc_array_char *, \
234 const GFC_INTEGER_##N *, const char *, \
235 const GFC_INTEGER_##N *); \
236 export_proto(eoshift0_##N); \
239 eoshift0_##N (gfc_array_char *ret, const gfc_array_char *array, \
240 const GFC_INTEGER_##N *pshift, const char *pbound, \
241 const GFC_INTEGER_##N *pdim) \
243 eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
244 GFC_DESCRIPTOR_SIZE (array), "\0", 1); \
247 extern void eoshift0_##N##_char (gfc_array_char *, GFC_INTEGER_4, \
248 const gfc_array_char *, \
249 const GFC_INTEGER_##N *, const char *, \
250 const GFC_INTEGER_##N *, GFC_INTEGER_4, \
252 export_proto(eoshift0_##N##_char); \
255 eoshift0_##N##_char (gfc_array_char *ret, \
256 GFC_INTEGER_4 ret_length __attribute__((unused)), \
257 const gfc_array_char *array, \
258 const GFC_INTEGER_##N *pshift, \
259 const char *pbound, \
260 const GFC_INTEGER_##N *pdim, \
261 GFC_INTEGER_4 array_length, \
262 GFC_INTEGER_4 bound_length __attribute__((unused))) \
264 eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
265 array_length, " ", 1); \
268 extern void eoshift0_##N##_char4 (gfc_array_char *, GFC_INTEGER_4, \
269 const gfc_array_char *, \
270 const GFC_INTEGER_##N *, const char *, \
271 const GFC_INTEGER_##N *, GFC_INTEGER_4, \
273 export_proto(eoshift0_##N##_char4); \
276 eoshift0_##N##_char4 (gfc_array_char *ret, \
277 GFC_INTEGER_4 ret_length __attribute__((unused)), \
278 const gfc_array_char *array, \
279 const GFC_INTEGER_##N *pshift, \
280 const char *pbound, \
281 const GFC_INTEGER_##N *pdim, \
282 GFC_INTEGER_4 array_length, \
283 GFC_INTEGER_4 bound_length __attribute__((unused))) \
285 static const gfc_char4_t space = (unsigned char) ' '; \
286 eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
287 array_length * sizeof (gfc_char4_t), (const char *) &space, \
288 sizeof (gfc_char4_t)); \
295 #ifdef HAVE_GFC_INTEGER_16