1 /* Generic implementation of the CSHIFT intrinsic
2 Copyright (C) 2003-2017 Free Software Foundation, Inc.
3 Contributed by Feng Wang <wf_cs@yahoo.com>
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"
30 cshift0 (gfc_array_char
* ret
, const gfc_array_char
* array
,
31 ptrdiff_t shift
, int which
, index_type size
)
33 /* r.* indicates the return array. */
34 index_type rstride
[GFC_MAX_DIMENSIONS
];
39 /* s.* indicates the source array. */
40 index_type sstride
[GFC_MAX_DIMENSIONS
];
45 index_type count
[GFC_MAX_DIMENSIONS
];
46 index_type extent
[GFC_MAX_DIMENSIONS
];
54 if (which
< 1 || which
> GFC_DESCRIPTOR_RANK (array
))
55 runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
57 arraysize
= size0 ((array_t
*) array
);
59 if (ret
->base_addr
== NULL
)
64 ret
->dtype
= array
->dtype
;
65 for (i
= 0; i
< GFC_DESCRIPTOR_RANK (array
); i
++)
69 ub
= GFC_DESCRIPTOR_EXTENT(array
,i
) - 1;
74 str
= GFC_DESCRIPTOR_EXTENT(ret
,i
-1) *
75 GFC_DESCRIPTOR_STRIDE(ret
,i
-1);
77 GFC_DIMENSION_SET(ret
->dim
[i
], 0, ub
, str
);
80 /* xmallocarray allocates a single byte for zero size. */
81 ret
->base_addr
= xmallocarray (arraysize
, size
);
83 else if (unlikely (compile_options
.bounds_check
))
85 bounds_equal_extents ((array_t
*) ret
, (array_t
*) array
,
86 "return value", "CSHIFT");
92 type_size
= GFC_DTYPE_TYPE_SIZE (array
);
96 case GFC_DTYPE_LOGICAL_1
:
97 case GFC_DTYPE_INTEGER_1
:
98 case GFC_DTYPE_DERIVED_1
:
99 cshift0_i1 ((gfc_array_i1
*)ret
, (gfc_array_i1
*) array
, shift
, which
);
102 case GFC_DTYPE_LOGICAL_2
:
103 case GFC_DTYPE_INTEGER_2
:
104 cshift0_i2 ((gfc_array_i2
*)ret
, (gfc_array_i2
*) array
, shift
, which
);
107 case GFC_DTYPE_LOGICAL_4
:
108 case GFC_DTYPE_INTEGER_4
:
109 cshift0_i4 ((gfc_array_i4
*)ret
, (gfc_array_i4
*) array
, shift
, which
);
112 case GFC_DTYPE_LOGICAL_8
:
113 case GFC_DTYPE_INTEGER_8
:
114 cshift0_i8 ((gfc_array_i8
*)ret
, (gfc_array_i8
*) array
, shift
, which
);
117 #ifdef HAVE_GFC_INTEGER_16
118 case GFC_DTYPE_LOGICAL_16
:
119 case GFC_DTYPE_INTEGER_16
:
120 cshift0_i16 ((gfc_array_i16
*)ret
, (gfc_array_i16
*) array
, shift
,
125 case GFC_DTYPE_REAL_4
:
126 cshift0_r4 ((gfc_array_r4
*)ret
, (gfc_array_r4
*) array
, shift
, which
);
129 case GFC_DTYPE_REAL_8
:
130 cshift0_r8 ((gfc_array_r8
*)ret
, (gfc_array_r8
*) array
, shift
, which
);
133 /* FIXME: This here is a hack, which will have to be removed when
134 the array descriptor is reworked. Currently, we don't store the
135 kind value for the type, but only the size. Because on targets with
136 __float128, we have sizeof(logn double) == sizeof(__float128),
137 we cannot discriminate here and have to fall back to the generic
138 handling (which is suboptimal). */
139 #if !defined(GFC_REAL_16_IS_FLOAT128)
140 # ifdef HAVE_GFC_REAL_10
141 case GFC_DTYPE_REAL_10
:
142 cshift0_r10 ((gfc_array_r10
*)ret
, (gfc_array_r10
*) array
, shift
,
147 # ifdef HAVE_GFC_REAL_16
148 case GFC_DTYPE_REAL_16
:
149 cshift0_r16 ((gfc_array_r16
*)ret
, (gfc_array_r16
*) array
, shift
,
155 case GFC_DTYPE_COMPLEX_4
:
156 cshift0_c4 ((gfc_array_c4
*)ret
, (gfc_array_c4
*) array
, shift
, which
);
159 case GFC_DTYPE_COMPLEX_8
:
160 cshift0_c8 ((gfc_array_c8
*)ret
, (gfc_array_c8
*) array
, shift
, which
);
163 /* FIXME: This here is a hack, which will have to be removed when
164 the array descriptor is reworked. Currently, we don't store the
165 kind value for the type, but only the size. Because on targets with
166 __float128, we have sizeof(logn double) == sizeof(__float128),
167 we cannot discriminate here and have to fall back to the generic
168 handling (which is suboptimal). */
169 #if !defined(GFC_REAL_16_IS_FLOAT128)
170 # ifdef HAVE_GFC_COMPLEX_10
171 case GFC_DTYPE_COMPLEX_10
:
172 cshift0_c10 ((gfc_array_c10
*)ret
, (gfc_array_c10
*) array
, shift
,
177 # ifdef HAVE_GFC_COMPLEX_16
178 case GFC_DTYPE_COMPLEX_16
:
179 cshift0_c16 ((gfc_array_c16
*)ret
, (gfc_array_c16
*) array
, shift
,
191 /* Let's check the actual alignment of the data pointers. If they
192 are suitably aligned, we can safely call the unpack functions. */
194 case sizeof (GFC_INTEGER_1
):
195 cshift0_i1 ((gfc_array_i1
*) ret
, (gfc_array_i1
*) array
, shift
,
199 case sizeof (GFC_INTEGER_2
):
200 if (GFC_UNALIGNED_2(ret
->base_addr
) || GFC_UNALIGNED_2(array
->base_addr
))
204 cshift0_i2 ((gfc_array_i2
*) ret
, (gfc_array_i2
*) array
, shift
,
209 case sizeof (GFC_INTEGER_4
):
210 if (GFC_UNALIGNED_4(ret
->base_addr
) || GFC_UNALIGNED_4(array
->base_addr
))
214 cshift0_i4 ((gfc_array_i4
*)ret
, (gfc_array_i4
*) array
, shift
,
219 case sizeof (GFC_INTEGER_8
):
220 if (GFC_UNALIGNED_8(ret
->base_addr
) || GFC_UNALIGNED_8(array
->base_addr
))
222 /* Let's try to use the complex routines. First, a sanity
223 check that the sizes match; this should be optimized to
225 if (sizeof(GFC_INTEGER_8
) != sizeof(GFC_COMPLEX_4
))
228 if (GFC_UNALIGNED_C4(ret
->base_addr
)
229 || GFC_UNALIGNED_C4(array
->base_addr
))
232 cshift0_c4 ((gfc_array_c4
*) ret
, (gfc_array_c4
*) array
, shift
,
238 cshift0_i8 ((gfc_array_i8
*)ret
, (gfc_array_i8
*) array
, shift
,
243 #ifdef HAVE_GFC_INTEGER_16
244 case sizeof (GFC_INTEGER_16
):
245 if (GFC_UNALIGNED_16(ret
->base_addr
)
246 || GFC_UNALIGNED_16(array
->base_addr
))
248 /* Let's try to use the complex routines. First, a sanity
249 check that the sizes match; this should be optimized to
251 if (sizeof(GFC_INTEGER_16
) != sizeof(GFC_COMPLEX_8
))
254 if (GFC_UNALIGNED_C8(ret
->base_addr
)
255 || GFC_UNALIGNED_C8(array
->base_addr
))
258 cshift0_c8 ((gfc_array_c8
*) ret
, (gfc_array_c8
*) array
, shift
,
264 cshift0_i16 ((gfc_array_i16
*) ret
, (gfc_array_i16
*) array
,
269 case sizeof (GFC_COMPLEX_8
):
271 if (GFC_UNALIGNED_C8(ret
->base_addr
)
272 || GFC_UNALIGNED_C8(array
->base_addr
))
276 cshift0_c8 ((gfc_array_c8
*) ret
, (gfc_array_c8
*) array
, shift
,
294 /* Initialized for avoiding compiler warnings. */
299 for (dim
= 0; dim
< GFC_DESCRIPTOR_RANK (array
); dim
++)
303 roffset
= GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
306 soffset
= GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
309 len
= GFC_DESCRIPTOR_EXTENT(array
,dim
);
314 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,dim
);
315 rstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(ret
,dim
);
316 sstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(array
,dim
);
325 dim
= GFC_DESCRIPTOR_RANK (array
);
326 rstride0
= rstride
[0];
327 sstride0
= sstride
[0];
328 rptr
= ret
->base_addr
;
329 sptr
= array
->base_addr
;
331 shift
= len
== 0 ? 0 : shift
% (ptrdiff_t)len
;
337 /* Do the shift for this dimension. */
339 /* If elements are contiguous, perform the operation
340 in two block moves. */
341 if (soffset
== size
&& roffset
== size
)
343 size_t len1
= shift
* size
;
344 size_t len2
= (len
- shift
) * size
;
345 memcpy (rptr
, sptr
+ len1
, len2
);
346 memcpy (rptr
+ len2
, sptr
, len1
);
350 /* Otherwise, we'll have to perform the copy one element at
353 const char *src
= &sptr
[shift
* soffset
];
355 for (n
= 0; n
< len
- shift
; n
++)
357 memcpy (dest
, src
, size
);
361 for (src
= sptr
, n
= 0; n
< shift
; n
++)
363 memcpy (dest
, src
, size
);
369 /* Advance to the next section. */
374 while (count
[n
] == extent
[n
])
376 /* When we get to the end of a dimension, reset it and increment
377 the next dimension. */
379 /* We could precalculate these products, but this is a less
380 frequently used path so probably not worth it. */
381 rptr
-= rstride
[n
] * extent
[n
];
382 sptr
-= sstride
[n
] * extent
[n
];
386 /* Break out of the loop. */
400 #define DEFINE_CSHIFT(N) \
401 extern void cshift0_##N (gfc_array_char *, const gfc_array_char *, \
402 const GFC_INTEGER_##N *, const GFC_INTEGER_##N *); \
403 export_proto(cshift0_##N); \
406 cshift0_##N (gfc_array_char *ret, const gfc_array_char *array, \
407 const GFC_INTEGER_##N *pshift, const GFC_INTEGER_##N *pdim) \
409 cshift0 (ret, array, *pshift, pdim ? *pdim : 1, \
410 GFC_DESCRIPTOR_SIZE (array)); \
413 extern void cshift0_##N##_char (gfc_array_char *, GFC_INTEGER_4, \
414 const gfc_array_char *, \
415 const GFC_INTEGER_##N *, \
416 const GFC_INTEGER_##N *, GFC_INTEGER_4); \
417 export_proto(cshift0_##N##_char); \
420 cshift0_##N##_char (gfc_array_char *ret, \
421 GFC_INTEGER_4 ret_length __attribute__((unused)), \
422 const gfc_array_char *array, \
423 const GFC_INTEGER_##N *pshift, \
424 const GFC_INTEGER_##N *pdim, \
425 GFC_INTEGER_4 array_length) \
427 cshift0 (ret, array, *pshift, pdim ? *pdim : 1, array_length); \
430 extern void cshift0_##N##_char4 (gfc_array_char *, GFC_INTEGER_4, \
431 const gfc_array_char *, \
432 const GFC_INTEGER_##N *, \
433 const GFC_INTEGER_##N *, GFC_INTEGER_4); \
434 export_proto(cshift0_##N##_char4); \
437 cshift0_##N##_char4 (gfc_array_char *ret, \
438 GFC_INTEGER_4 ret_length __attribute__((unused)), \
439 const gfc_array_char *array, \
440 const GFC_INTEGER_##N *pshift, \
441 const GFC_INTEGER_##N *pdim, \
442 GFC_INTEGER_4 array_length) \
444 cshift0 (ret, array, *pshift, pdim ? *pdim : 1, \
445 array_length * sizeof (gfc_char4_t)); \
452 #ifdef HAVE_GFC_INTEGER_16