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
++)
74 ret
->dim
[i
].lbound
= 0;
75 ret
->dim
[i
].ubound
= array
->dim
[i
].ubound
- array
->dim
[i
].lbound
;
78 ret
->dim
[i
].stride
= 1;
80 ret
->dim
[i
].stride
= (ret
->dim
[i
-1].ubound
+ 1)
81 * ret
->dim
[i
-1].stride
;
85 ret
->data
= internal_malloc_size (size
* arraysize
);
88 ret
->data
= internal_malloc_size (1);
95 type_size
= GFC_DTYPE_TYPE_SIZE (array
);
99 case GFC_DTYPE_LOGICAL_1
:
100 case GFC_DTYPE_INTEGER_1
:
101 case GFC_DTYPE_DERIVED_1
:
102 cshift0_i1 ((gfc_array_i1
*)ret
, (gfc_array_i1
*) array
, shift
, which
);
105 case GFC_DTYPE_LOGICAL_2
:
106 case GFC_DTYPE_INTEGER_2
:
107 cshift0_i2 ((gfc_array_i2
*)ret
, (gfc_array_i2
*) array
, shift
, which
);
110 case GFC_DTYPE_LOGICAL_4
:
111 case GFC_DTYPE_INTEGER_4
:
112 cshift0_i4 ((gfc_array_i4
*)ret
, (gfc_array_i4
*) array
, shift
, which
);
115 case GFC_DTYPE_LOGICAL_8
:
116 case GFC_DTYPE_INTEGER_8
:
117 cshift0_i8 ((gfc_array_i8
*)ret
, (gfc_array_i8
*) array
, shift
, which
);
120 #ifdef HAVE_GFC_INTEGER_16
121 case GFC_DTYPE_LOGICAL_16
:
122 case GFC_DTYPE_INTEGER_16
:
123 cshift0_i16 ((gfc_array_i16
*)ret
, (gfc_array_i16
*) array
, shift
,
128 case GFC_DTYPE_REAL_4
:
129 cshift0_r4 ((gfc_array_r4
*)ret
, (gfc_array_r4
*) array
, shift
, which
);
132 case GFC_DTYPE_REAL_8
:
133 cshift0_r8 ((gfc_array_r8
*)ret
, (gfc_array_r8
*) array
, shift
, which
);
136 #ifdef HAVE_GFC_REAL_10
137 case GFC_DTYPE_REAL_10
:
138 cshift0_r10 ((gfc_array_r10
*)ret
, (gfc_array_r10
*) array
, shift
,
143 #ifdef HAVE_GFC_REAL_16
144 case GFC_DTYPE_REAL_16
:
145 cshift0_r16 ((gfc_array_r16
*)ret
, (gfc_array_r16
*) array
, shift
,
150 case GFC_DTYPE_COMPLEX_4
:
151 cshift0_c4 ((gfc_array_c4
*)ret
, (gfc_array_c4
*) array
, shift
, which
);
154 case GFC_DTYPE_COMPLEX_8
:
155 cshift0_c8 ((gfc_array_c8
*)ret
, (gfc_array_c8
*) array
, shift
, which
);
158 #ifdef HAVE_GFC_COMPLEX_10
159 case GFC_DTYPE_COMPLEX_10
:
160 cshift0_c10 ((gfc_array_c10
*)ret
, (gfc_array_c10
*) array
, shift
,
165 #ifdef HAVE_GFC_COMPLEX_16
166 case GFC_DTYPE_COMPLEX_16
:
167 cshift0_c16 ((gfc_array_c16
*)ret
, (gfc_array_c16
*) array
, shift
,
178 /* Let's check the actual alignment of the data pointers. If they
179 are suitably aligned, we can safely call the unpack functions. */
181 case sizeof (GFC_INTEGER_1
):
182 cshift0_i1 ((gfc_array_i1
*) ret
, (gfc_array_i1
*) array
, shift
,
186 case sizeof (GFC_INTEGER_2
):
187 if (GFC_UNALIGNED_2(ret
->data
) || GFC_UNALIGNED_2(array
->data
))
191 cshift0_i2 ((gfc_array_i2
*) ret
, (gfc_array_i2
*) array
, shift
,
196 case sizeof (GFC_INTEGER_4
):
197 if (GFC_UNALIGNED_4(ret
->data
) || GFC_UNALIGNED_4(array
->data
))
201 cshift0_i4 ((gfc_array_i4
*)ret
, (gfc_array_i4
*) array
, shift
,
206 case sizeof (GFC_INTEGER_8
):
207 if (GFC_UNALIGNED_8(ret
->data
) || GFC_UNALIGNED_8(array
->data
))
209 /* Let's try to use the complex routines. First, a sanity
210 check that the sizes match; this should be optimized to
212 if (sizeof(GFC_INTEGER_8
) != sizeof(GFC_COMPLEX_4
))
215 if (GFC_UNALIGNED_C4(ret
->data
) || GFC_UNALIGNED_C4(array
->data
))
218 cshift0_c4 ((gfc_array_c4
*) ret
, (gfc_array_c4
*) array
, shift
,
224 cshift0_i8 ((gfc_array_i8
*)ret
, (gfc_array_i8
*) array
, shift
,
229 #ifdef HAVE_GFC_INTEGER_16
230 case sizeof (GFC_INTEGER_16
):
231 if (GFC_UNALIGNED_16(ret
->data
) || GFC_UNALIGNED_16(array
->data
))
233 /* Let's try to use the complex routines. First, a sanity
234 check that the sizes match; this should be optimized to
236 if (sizeof(GFC_INTEGER_16
) != sizeof(GFC_COMPLEX_8
))
239 if (GFC_UNALIGNED_C8(ret
->data
) || GFC_UNALIGNED_C8(array
->data
))
242 cshift0_c8 ((gfc_array_c8
*) ret
, (gfc_array_c8
*) array
, shift
,
248 cshift0_i16 ((gfc_array_i16
*) ret
, (gfc_array_i16
*) array
,
253 case sizeof (GFC_COMPLEX_8
):
255 if (GFC_UNALIGNED_C8(ret
->data
) || GFC_UNALIGNED_C8(array
->data
))
259 cshift0_c8 ((gfc_array_c8
*) ret
, (gfc_array_c8
*) array
, shift
,
277 /* Initialized for avoiding compiler warnings. */
282 for (dim
= 0; dim
< GFC_DESCRIPTOR_RANK (array
); dim
++)
286 roffset
= ret
->dim
[dim
].stride
* size
;
289 soffset
= array
->dim
[dim
].stride
* size
;
292 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
297 extent
[n
] = array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
298 rstride
[n
] = ret
->dim
[dim
].stride
* size
;
299 sstride
[n
] = array
->dim
[dim
].stride
* size
;
308 dim
= GFC_DESCRIPTOR_RANK (array
);
309 rstride0
= rstride
[0];
310 sstride0
= sstride
[0];
314 shift
= len
== 0 ? 0 : shift
% (ssize_t
)len
;
320 /* Do the shift for this dimension. */
322 /* If elements are contiguous, perform the operation
323 in two block moves. */
324 if (soffset
== size
&& roffset
== size
)
326 size_t len1
= shift
* size
;
327 size_t len2
= (len
- shift
) * size
;
328 memcpy (rptr
, sptr
+ len1
, len2
);
329 memcpy (rptr
+ len2
, sptr
, len1
);
333 /* Otherwise, we'll have to perform the copy one element at
336 const char *src
= &sptr
[shift
* soffset
];
338 for (n
= 0; n
< len
- shift
; n
++)
340 memcpy (dest
, src
, size
);
344 for (src
= sptr
, n
= 0; n
< shift
; n
++)
346 memcpy (dest
, src
, size
);
352 /* Advance to the next section. */
357 while (count
[n
] == extent
[n
])
359 /* When we get to the end of a dimension, reset it and increment
360 the next dimension. */
362 /* We could precalculate these products, but this is a less
363 frequently used path so probably not worth it. */
364 rptr
-= rstride
[n
] * extent
[n
];
365 sptr
-= sstride
[n
] * extent
[n
];
369 /* Break out of the loop. */
383 #define DEFINE_CSHIFT(N) \
384 extern void cshift0_##N (gfc_array_char *, const gfc_array_char *, \
385 const GFC_INTEGER_##N *, const GFC_INTEGER_##N *); \
386 export_proto(cshift0_##N); \
389 cshift0_##N (gfc_array_char *ret, const gfc_array_char *array, \
390 const GFC_INTEGER_##N *pshift, const GFC_INTEGER_##N *pdim) \
392 cshift0 (ret, array, *pshift, pdim ? *pdim : 1, \
393 GFC_DESCRIPTOR_SIZE (array)); \
396 extern void cshift0_##N##_char (gfc_array_char *, GFC_INTEGER_4, \
397 const gfc_array_char *, \
398 const GFC_INTEGER_##N *, \
399 const GFC_INTEGER_##N *, GFC_INTEGER_4); \
400 export_proto(cshift0_##N##_char); \
403 cshift0_##N##_char (gfc_array_char *ret, \
404 GFC_INTEGER_4 ret_length __attribute__((unused)), \
405 const gfc_array_char *array, \
406 const GFC_INTEGER_##N *pshift, \
407 const GFC_INTEGER_##N *pdim, \
408 GFC_INTEGER_4 array_length) \
410 cshift0 (ret, array, *pshift, pdim ? *pdim : 1, array_length); \
413 extern void cshift0_##N##_char4 (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##_char4); \
420 cshift0_##N##_char4 (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, \
428 array_length * sizeof (gfc_char4_t)); \
435 #ifdef HAVE_GFC_INTEGER_16