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
];
58 /* The compiler cannot figure out that these are set, initialize
59 them to avoid warnings. */
64 arraysize
= size0 ((array_t
*) array
);
66 if (ret
->base_addr
== NULL
)
71 ret
->dtype
= array
->dtype
;
72 for (i
= 0; i
< GFC_DESCRIPTOR_RANK (array
); i
++)
76 ub
= GFC_DESCRIPTOR_EXTENT(array
,i
) - 1;
81 str
= GFC_DESCRIPTOR_EXTENT(ret
,i
-1)
82 * GFC_DESCRIPTOR_STRIDE(ret
,i
-1);
84 GFC_DIMENSION_SET(ret
->dim
[i
], 0, ub
, str
);
88 /* xmallocarray allocates a single byte for zero size. */
89 ret
->base_addr
= xmallocarray (arraysize
, size
);
91 else if (unlikely (compile_options
.bounds_check
))
93 bounds_equal_extents ((array_t
*) ret
, (array_t
*) array
,
94 "return value", "EOSHIFT");
109 /* Test if both ret and array are contiguous. */
114 dim
= GFC_DESCRIPTOR_RANK (array
);
115 for (n
= 0; n
< dim
; n
++)
118 rs
= GFC_DESCRIPTOR_STRIDE (ret
, n
);
124 as
= GFC_DESCRIPTOR_STRIDE (array
, n
);
130 r_ex
*= GFC_DESCRIPTOR_EXTENT (ret
, n
);
131 a_ex
*= GFC_DESCRIPTOR_EXTENT (array
, n
);
141 /* For contiguous arrays, use the relationship that
143 dimension(n1,n2,n3) :: a, b
146 can be dealt with as if
148 dimension(n1*n2*n3) :: an, bn
149 bn = eoshift(a,sh*n1*n2,1)
151 so a block move can be used for dim>1. */
152 len
= GFC_DESCRIPTOR_STRIDE(array
, which
)
153 * GFC_DESCRIPTOR_EXTENT(array
, which
);
154 shift
*= GFC_DESCRIPTOR_STRIDE(array
, which
);
157 for (dim
= which
+ 1; dim
< GFC_DESCRIPTOR_RANK (array
); dim
++)
160 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,dim
);
161 rstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
162 sstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
166 dim
= GFC_DESCRIPTOR_RANK (array
) - which
;
170 for (dim
= 0; dim
< GFC_DESCRIPTOR_RANK (array
); dim
++)
174 roffset
= GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
177 soffset
= GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
180 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
185 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,dim
);
186 rstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
187 sstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
191 dim
= GFC_DESCRIPTOR_RANK (array
);
194 if ((shift
>= 0 ? shift
: -shift
) > len
)
207 rstride0
= rstride
[0];
208 sstride0
= sstride
[0];
209 rptr
= ret
->base_addr
;
210 sptr
= array
->base_addr
;
214 /* Do the shift for this dimension. */
217 src
= &sptr
[shift
* soffset
];
223 dest
= &rptr
[-shift
* roffset
];
225 /* If the elements are contiguous, perform a single block move. */
227 if (soffset
== size
&& roffset
== size
)
229 size_t chunk
= size
* len
;
230 memcpy (dest
, src
, chunk
);
235 for (n
= 0; n
< len
; n
++)
237 memcpy (dest
, src
, size
);
255 memcpy (dest
, pbound
, size
);
264 memset (dest
, filler
[0], size
);
266 for (i
= 0; i
< size
; i
+= filler_len
)
267 memcpy (&dest
[i
], filler
, filler_len
);
272 /* Advance to the next section. */
277 while (count
[n
] == extent
[n
])
279 /* When we get to the end of a dimension, reset it and increment
280 the next dimension. */
282 /* We could precalculate these products, but this is a less
283 frequently used path so probably not worth it. */
284 rptr
-= rstride
[n
] * extent
[n
];
285 sptr
-= sstride
[n
] * extent
[n
];
289 /* Break out of the loop. */
304 #define DEFINE_EOSHIFT(N) \
305 extern void eoshift0_##N (gfc_array_char *, const gfc_array_char *, \
306 const GFC_INTEGER_##N *, const char *, \
307 const GFC_INTEGER_##N *); \
308 export_proto(eoshift0_##N); \
311 eoshift0_##N (gfc_array_char *ret, const gfc_array_char *array, \
312 const GFC_INTEGER_##N *pshift, const char *pbound, \
313 const GFC_INTEGER_##N *pdim) \
315 eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
316 GFC_DESCRIPTOR_SIZE (array), "\0", 1); \
319 extern void eoshift0_##N##_char (gfc_array_char *, GFC_INTEGER_4, \
320 const gfc_array_char *, \
321 const GFC_INTEGER_##N *, const char *, \
322 const GFC_INTEGER_##N *, GFC_INTEGER_4, \
324 export_proto(eoshift0_##N##_char); \
327 eoshift0_##N##_char (gfc_array_char *ret, \
328 GFC_INTEGER_4 ret_length __attribute__((unused)), \
329 const gfc_array_char *array, \
330 const GFC_INTEGER_##N *pshift, \
331 const char *pbound, \
332 const GFC_INTEGER_##N *pdim, \
333 GFC_INTEGER_4 array_length, \
334 GFC_INTEGER_4 bound_length __attribute__((unused))) \
336 eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
337 array_length, " ", 1); \
340 extern void eoshift0_##N##_char4 (gfc_array_char *, GFC_INTEGER_4, \
341 const gfc_array_char *, \
342 const GFC_INTEGER_##N *, const char *, \
343 const GFC_INTEGER_##N *, GFC_INTEGER_4, \
345 export_proto(eoshift0_##N##_char4); \
348 eoshift0_##N##_char4 (gfc_array_char *ret, \
349 GFC_INTEGER_4 ret_length __attribute__((unused)), \
350 const gfc_array_char *array, \
351 const GFC_INTEGER_##N *pshift, \
352 const char *pbound, \
353 const GFC_INTEGER_##N *pdim, \
354 GFC_INTEGER_4 array_length, \
355 GFC_INTEGER_4 bound_length __attribute__((unused))) \
357 static const gfc_char4_t space = (unsigned char) ' '; \
358 eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
359 array_length * sizeof (gfc_char4_t), (const char *) &space, \
360 sizeof (gfc_char4_t)); \
367 #ifdef HAVE_GFC_INTEGER_16