1 /* Generic implementation of the CSHIFT intrinsic
2 Copyright 2003, 2005, 2006, 2007 Free Software Foundation, Inc.
3 Contributed by Feng Wang <wf_cs@yahoo.com>
5 This file is part of the GNU Fortran 95 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 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
31 #include "libgfortran.h"
37 cshift0 (gfc_array_char * ret, const gfc_array_char * array,
38 ssize_t shift, int which, index_type size)
40 /* r.* indicates the return array. */
41 index_type rstride[GFC_MAX_DIMENSIONS];
46 /* s.* indicates the source array. */
47 index_type sstride[GFC_MAX_DIMENSIONS];
52 index_type count[GFC_MAX_DIMENSIONS];
53 index_type extent[GFC_MAX_DIMENSIONS];
61 if (which < 1 || which > GFC_DESCRIPTOR_RANK (array))
62 runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
64 arraysize = size0 ((array_t *) array);
66 if (ret->data == 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 ret->data = internal_malloc_size (size * arraysize);
91 ret->data = internal_malloc_size (1);
98 type_size = GFC_DTYPE_TYPE_SIZE (array);
102 case GFC_DTYPE_LOGICAL_1:
103 case GFC_DTYPE_INTEGER_1:
104 case GFC_DTYPE_DERIVED_1:
105 cshift0_i1 ((gfc_array_i1 *)ret, (gfc_array_i1 *) array, shift, which);
108 case GFC_DTYPE_LOGICAL_2:
109 case GFC_DTYPE_INTEGER_2:
110 cshift0_i2 ((gfc_array_i2 *)ret, (gfc_array_i2 *) array, shift, which);
113 case GFC_DTYPE_LOGICAL_4:
114 case GFC_DTYPE_INTEGER_4:
115 cshift0_i4 ((gfc_array_i4 *)ret, (gfc_array_i4 *) array, shift, which);
118 case GFC_DTYPE_LOGICAL_8:
119 case GFC_DTYPE_INTEGER_8:
120 cshift0_i8 ((gfc_array_i8 *)ret, (gfc_array_i8 *) array, shift, which);
123 #ifdef HAVE_GFC_INTEGER_16
124 case GFC_DTYPE_LOGICAL_16:
125 case GFC_DTYPE_INTEGER_16:
126 cshift0_i16 ((gfc_array_i16 *)ret, (gfc_array_i16 *) array, shift,
131 case GFC_DTYPE_REAL_4:
132 cshift0_r4 ((gfc_array_r4 *)ret, (gfc_array_r4 *) array, shift, which);
135 case GFC_DTYPE_REAL_8:
136 cshift0_r8 ((gfc_array_r8 *)ret, (gfc_array_r8 *) array, shift, which);
139 #ifdef HAVE_GFC_REAL_10
140 case GFC_DTYPE_REAL_10:
141 cshift0_r10 ((gfc_array_r10 *)ret, (gfc_array_r10 *) array, shift,
146 #ifdef HAVE_GFC_REAL_16
147 case GFC_DTYPE_REAL_16:
148 cshift0_r16 ((gfc_array_r16 *)ret, (gfc_array_r16 *) array, shift,
153 case GFC_DTYPE_COMPLEX_4:
154 cshift0_c4 ((gfc_array_c4 *)ret, (gfc_array_c4 *) array, shift, which);
157 case GFC_DTYPE_COMPLEX_8:
158 cshift0_c8 ((gfc_array_c8 *)ret, (gfc_array_c8 *) array, shift, which);
161 #ifdef HAVE_GFC_COMPLEX_10
162 case GFC_DTYPE_COMPLEX_10:
163 cshift0_c10 ((gfc_array_c10 *)ret, (gfc_array_c10 *) array, shift,
168 #ifdef HAVE_GFC_COMPLEX_16
169 case GFC_DTYPE_COMPLEX_16:
170 cshift0_c16 ((gfc_array_c16 *)ret, (gfc_array_c16 *) array, shift,
181 /* Let's check the actual alignment of the data pointers. If they
182 are suitably aligned, we can safely call the unpack functions. */
184 case sizeof (GFC_INTEGER_1):
185 cshift0_i1 ((gfc_array_i1 *) ret, (gfc_array_i1 *) array, shift,
189 case sizeof (GFC_INTEGER_2):
190 if (GFC_UNALIGNED_2(ret->data) || GFC_UNALIGNED_2(array->data))
194 cshift0_i2 ((gfc_array_i2 *) ret, (gfc_array_i2 *) array, shift,
199 case sizeof (GFC_INTEGER_4):
200 if (GFC_UNALIGNED_4(ret->data) || GFC_UNALIGNED_4(array->data))
204 cshift0_i4 ((gfc_array_i4 *)ret, (gfc_array_i4 *) array, shift,
209 case sizeof (GFC_INTEGER_8):
210 if (GFC_UNALIGNED_8(ret->data) || GFC_UNALIGNED_8(array->data))
212 /* Let's try to use the complex routines. First, a sanity
213 check that the sizes match; this should be optimized to
215 if (sizeof(GFC_INTEGER_8) != sizeof(GFC_COMPLEX_4))
218 if (GFC_UNALIGNED_C4(ret->data) || GFC_UNALIGNED_C4(array->data))
221 cshift0_c4 ((gfc_array_c4 *) ret, (gfc_array_c4 *) array, shift,
227 cshift0_i8 ((gfc_array_i8 *)ret, (gfc_array_i8 *) array, shift,
232 #ifdef HAVE_GFC_INTEGER_16
233 case sizeof (GFC_INTEGER_16):
234 if (GFC_UNALIGNED_16(ret->data) || GFC_UNALIGNED_16(array->data))
236 /* Let's try to use the complex routines. First, a sanity
237 check that the sizes match; this should be optimized to
239 if (sizeof(GFC_INTEGER_16) != sizeof(GFC_COMPLEX_8))
242 if (GFC_UNALIGNED_C8(ret->data) || GFC_UNALIGNED_C8(array->data))
245 cshift0_c8 ((gfc_array_c8 *) ret, (gfc_array_c8 *) array, shift,
251 cshift0_i16 ((gfc_array_i16 *) ret, (gfc_array_i16 *) array,
256 case sizeof (GFC_COMPLEX_8):
258 if (GFC_UNALIGNED_C8(ret->data) || GFC_UNALIGNED_C8(array->data))
262 cshift0_c8 ((gfc_array_c8 *) ret, (gfc_array_c8 *) array, shift,
280 /* Initialized for avoiding compiler warnings. */
285 for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
289 roffset = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
292 soffset = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
295 len = GFC_DESCRIPTOR_EXTENT(array,dim);
300 extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
301 rstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
302 sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
311 dim = GFC_DESCRIPTOR_RANK (array);
312 rstride0 = rstride[0];
313 sstride0 = sstride[0];
317 shift = len == 0 ? 0 : shift % (ssize_t)len;
323 /* Do the shift for this dimension. */
325 /* If elements are contiguous, perform the operation
326 in two block moves. */
327 if (soffset == size && roffset == size)
329 size_t len1 = shift * size;
330 size_t len2 = (len - shift) * size;
331 memcpy (rptr, sptr + len1, len2);
332 memcpy (rptr + len2, sptr, len1);
336 /* Otherwise, we'll have to perform the copy one element at
339 const char *src = &sptr[shift * soffset];
341 for (n = 0; n < len - shift; n++)
343 memcpy (dest, src, size);
347 for (src = sptr, n = 0; n < shift; n++)
349 memcpy (dest, src, size);
355 /* Advance to the next section. */
360 while (count[n] == extent[n])
362 /* When we get to the end of a dimension, reset it and increment
363 the next dimension. */
365 /* We could precalculate these products, but this is a less
366 frequently used path so probably not worth it. */
367 rptr -= rstride[n] * extent[n];
368 sptr -= sstride[n] * extent[n];
372 /* Break out of the loop. */
386 #define DEFINE_CSHIFT(N) \
387 extern void cshift0_##N (gfc_array_char *, const gfc_array_char *, \
388 const GFC_INTEGER_##N *, const GFC_INTEGER_##N *); \
389 export_proto(cshift0_##N); \
392 cshift0_##N (gfc_array_char *ret, const gfc_array_char *array, \
393 const GFC_INTEGER_##N *pshift, const GFC_INTEGER_##N *pdim) \
395 cshift0 (ret, array, *pshift, pdim ? *pdim : 1, \
396 GFC_DESCRIPTOR_SIZE (array)); \
399 extern void cshift0_##N##_char (gfc_array_char *, GFC_INTEGER_4, \
400 const gfc_array_char *, \
401 const GFC_INTEGER_##N *, \
402 const GFC_INTEGER_##N *, GFC_INTEGER_4); \
403 export_proto(cshift0_##N##_char); \
406 cshift0_##N##_char (gfc_array_char *ret, \
407 GFC_INTEGER_4 ret_length __attribute__((unused)), \
408 const gfc_array_char *array, \
409 const GFC_INTEGER_##N *pshift, \
410 const GFC_INTEGER_##N *pdim, \
411 GFC_INTEGER_4 array_length) \
413 cshift0 (ret, array, *pshift, pdim ? *pdim : 1, array_length); \
416 extern void cshift0_##N##_char4 (gfc_array_char *, GFC_INTEGER_4, \
417 const gfc_array_char *, \
418 const GFC_INTEGER_##N *, \
419 const GFC_INTEGER_##N *, GFC_INTEGER_4); \
420 export_proto(cshift0_##N##_char4); \
423 cshift0_##N##_char4 (gfc_array_char *ret, \
424 GFC_INTEGER_4 ret_length __attribute__((unused)), \
425 const gfc_array_char *array, \
426 const GFC_INTEGER_##N *pshift, \
427 const GFC_INTEGER_##N *pdim, \
428 GFC_INTEGER_4 array_length) \
430 cshift0 (ret, array, *pshift, pdim ? *pdim : 1, \
431 array_length * sizeof (gfc_char4_t)); \
438 #ifdef HAVE_GFC_INTEGER_16