1 /* Generic implementation of the EOSHIFT intrinsic
2 Copyright (C) 2002-2018 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"
31 eoshift0 (gfc_array_char
* ret
, const gfc_array_char
* array
,
32 index_type shift
, const char * pbound
, int which
, index_type size
,
33 const char *filler
, index_type filler_len
)
35 /* r.* indicates the return array. */
36 index_type rstride
[GFC_MAX_DIMENSIONS
];
41 /* s.* indicates the source array. */
42 index_type sstride
[GFC_MAX_DIMENSIONS
];
48 index_type count
[GFC_MAX_DIMENSIONS
];
49 index_type extent
[GFC_MAX_DIMENSIONS
];
56 /* The compiler cannot figure out that these are set, initialize
57 them to avoid warnings. */
62 arraysize
= size0 ((array_t
*) array
);
64 if (ret
->base_addr
== NULL
)
69 GFC_DTYPE_COPY(ret
,array
);
70 for (i
= 0; i
< GFC_DESCRIPTOR_RANK (array
); i
++)
74 ub
= GFC_DESCRIPTOR_EXTENT(array
,i
) - 1;
79 str
= GFC_DESCRIPTOR_EXTENT(ret
,i
-1)
80 * GFC_DESCRIPTOR_STRIDE(ret
,i
-1);
82 GFC_DIMENSION_SET(ret
->dim
[i
], 0, ub
, str
);
86 /* xmallocarray allocates a single byte for zero size. */
87 ret
->base_addr
= xmallocarray (arraysize
, size
);
89 else if (unlikely (compile_options
.bounds_check
))
91 bounds_equal_extents ((array_t
*) ret
, (array_t
*) array
,
92 "return value", "EOSHIFT");
107 /* Test if both ret and array are contiguous. */
108 index_type r_ex
, a_ex
;
112 dim
= GFC_DESCRIPTOR_RANK (array
);
113 for (n
= 0; n
< dim
; n
++)
116 rs
= GFC_DESCRIPTOR_STRIDE (ret
, n
);
122 as
= GFC_DESCRIPTOR_STRIDE (array
, n
);
128 r_ex
*= GFC_DESCRIPTOR_EXTENT (ret
, n
);
129 a_ex
*= GFC_DESCRIPTOR_EXTENT (array
, n
);
139 /* For contiguous arrays, use the relationship that
141 dimension(n1,n2,n3) :: a, b
144 can be dealt with as if
146 dimension(n1*n2*n3) :: an, bn
147 bn = eoshift(a,sh*n1*n2,1)
149 so a block move can be used for dim>1. */
150 len
= GFC_DESCRIPTOR_STRIDE(array
, which
)
151 * GFC_DESCRIPTOR_EXTENT(array
, which
);
152 shift
*= GFC_DESCRIPTOR_STRIDE(array
, which
);
155 for (dim
= which
+ 1; dim
< GFC_DESCRIPTOR_RANK (array
); dim
++)
158 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,dim
);
159 rstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
160 sstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
164 dim
= GFC_DESCRIPTOR_RANK (array
) - which
;
168 for (dim
= 0; dim
< GFC_DESCRIPTOR_RANK (array
); dim
++)
172 roffset
= GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
175 soffset
= GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
178 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
183 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,dim
);
184 rstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
185 sstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
189 dim
= GFC_DESCRIPTOR_RANK (array
);
192 if ((shift
>= 0 ? shift
: -shift
) > len
)
205 rstride0
= rstride
[0];
206 sstride0
= sstride
[0];
207 rptr
= ret
->base_addr
;
208 sptr
= array
->base_addr
;
212 /* Do the shift for this dimension. */
215 src
= &sptr
[shift
* soffset
];
221 dest
= &rptr
[-shift
* roffset
];
223 /* If the elements are contiguous, perform a single block move. */
225 if (soffset
== size
&& roffset
== size
)
227 size_t chunk
= size
* len
;
228 memcpy (dest
, src
, chunk
);
233 for (n
= 0; n
< len
; n
++)
235 memcpy (dest
, src
, size
);
253 memcpy (dest
, pbound
, size
);
262 memset (dest
, filler
[0], size
);
264 for (i
= 0; i
< size
; i
+= filler_len
)
265 memcpy (&dest
[i
], filler
, filler_len
);
270 /* Advance to the next section. */
275 while (count
[n
] == extent
[n
])
277 /* When we get to the end of a dimension, reset it and increment
278 the next dimension. */
280 /* We could precalculate these products, but this is a less
281 frequently used path so probably not worth it. */
282 rptr
-= rstride
[n
] * extent
[n
];
283 sptr
-= sstride
[n
] * extent
[n
];
287 /* Break out of the loop. */
302 #define DEFINE_EOSHIFT(N) \
303 extern void eoshift0_##N (gfc_array_char *, const gfc_array_char *, \
304 const GFC_INTEGER_##N *, const char *, \
305 const GFC_INTEGER_##N *); \
306 export_proto(eoshift0_##N); \
309 eoshift0_##N (gfc_array_char *ret, const gfc_array_char *array, \
310 const GFC_INTEGER_##N *pshift, const char *pbound, \
311 const GFC_INTEGER_##N *pdim) \
313 eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
314 GFC_DESCRIPTOR_SIZE (array), "\0", 1); \
317 extern void eoshift0_##N##_char (gfc_array_char *, GFC_INTEGER_4, \
318 const gfc_array_char *, \
319 const GFC_INTEGER_##N *, const char *, \
320 const GFC_INTEGER_##N *, GFC_INTEGER_4, \
322 export_proto(eoshift0_##N##_char); \
325 eoshift0_##N##_char (gfc_array_char *ret, \
326 GFC_INTEGER_4 ret_length __attribute__((unused)), \
327 const gfc_array_char *array, \
328 const GFC_INTEGER_##N *pshift, \
329 const char *pbound, \
330 const GFC_INTEGER_##N *pdim, \
331 GFC_INTEGER_4 array_length, \
332 GFC_INTEGER_4 bound_length __attribute__((unused))) \
334 eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
335 array_length, " ", 1); \
338 extern void eoshift0_##N##_char4 (gfc_array_char *, GFC_INTEGER_4, \
339 const gfc_array_char *, \
340 const GFC_INTEGER_##N *, const char *, \
341 const GFC_INTEGER_##N *, GFC_INTEGER_4, \
343 export_proto(eoshift0_##N##_char4); \
346 eoshift0_##N##_char4 (gfc_array_char *ret, \
347 GFC_INTEGER_4 ret_length __attribute__((unused)), \
348 const gfc_array_char *array, \
349 const GFC_INTEGER_##N *pshift, \
350 const char *pbound, \
351 const GFC_INTEGER_##N *pdim, \
352 GFC_INTEGER_4 array_length, \
353 GFC_INTEGER_4 bound_length __attribute__((unused))) \
355 static const gfc_char4_t space = (unsigned char) ' '; \
356 eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
357 array_length * sizeof (gfc_char4_t), (const char *) &space, \
358 sizeof (gfc_char4_t)); \
365 #ifdef HAVE_GFC_INTEGER_16