1 /* Generic implementation of the CSHIFT intrinsic
2 Copyright 2003, 2005, 2006 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. */
35 #include "libgfortran.h"
38 /* "Templatized" helper function for the inner shift loop. */
40 #define DEF_COPY_LOOP(NAME, TYPE) \
42 copy_loop_##NAME (void *xdest, const void *xsrc, \
43 size_t roff, size_t soff, \
44 index_type len, index_type shift) \
50 roff /= sizeof (TYPE); \
51 soff /= sizeof (TYPE); \
54 src += shift * soff; \
55 for (i = 0; i < len - shift; ++i) \
63 for (i = 0; i < shift; ++i) \
71 DEF_COPY_LOOP(int, int)
72 DEF_COPY_LOOP(long, long)
73 DEF_COPY_LOOP(double, double)
74 DEF_COPY_LOOP(ldouble
, long double)
75 DEF_COPY_LOOP(cfloat
, _Complex
float)
76 DEF_COPY_LOOP(cdouble
, _Complex
double)
80 cshift0 (gfc_array_char
* ret
, const gfc_array_char
* array
,
81 ssize_t shift
, int which
, index_type size
)
83 /* r.* indicates the return array. */
84 index_type rstride
[GFC_MAX_DIMENSIONS
];
89 /* s.* indicates the source array. */
90 index_type sstride
[GFC_MAX_DIMENSIONS
];
95 index_type count
[GFC_MAX_DIMENSIONS
];
96 index_type extent
[GFC_MAX_DIMENSIONS
];
102 if (which
< 1 || which
> GFC_DESCRIPTOR_RANK (array
))
103 runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
113 /* The values assigned here must match the cases in the inner loop. */
115 switch (GFC_DESCRIPTOR_TYPE (array
))
117 case GFC_DTYPE_LOGICAL
:
118 case GFC_DTYPE_INTEGER
:
120 if (size
== sizeof (int))
122 else if (size
== sizeof (long))
124 else if (size
== sizeof (double))
126 else if (size
== sizeof (long double))
130 case GFC_DTYPE_COMPLEX
:
131 if (size
== sizeof (_Complex
float))
133 else if (size
== sizeof (_Complex
double))
141 /* Initialized for avoiding compiler warnings. */
146 if (ret
->data
== NULL
)
149 index_type arraysize
= size0 ((array_t
*)array
);
152 ret
->dtype
= array
->dtype
;
153 for (i
= 0; i
< GFC_DESCRIPTOR_RANK (array
); i
++)
155 ret
->dim
[i
].lbound
= 0;
156 ret
->dim
[i
].ubound
= array
->dim
[i
].ubound
- array
->dim
[i
].lbound
;
159 ret
->dim
[i
].stride
= 1;
161 ret
->dim
[i
].stride
= (ret
->dim
[i
-1].ubound
+ 1)
162 * ret
->dim
[i
-1].stride
;
166 ret
->data
= internal_malloc_size (size
* arraysize
);
169 ret
->data
= internal_malloc_size (1);
174 for (dim
= 0; dim
< GFC_DESCRIPTOR_RANK (array
); dim
++)
178 roffset
= ret
->dim
[dim
].stride
* size
;
181 soffset
= array
->dim
[dim
].stride
* size
;
184 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
189 extent
[n
] = array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
190 rstride
[n
] = ret
->dim
[dim
].stride
* size
;
191 sstride
[n
] = array
->dim
[dim
].stride
* size
;
200 dim
= GFC_DESCRIPTOR_RANK (array
);
201 rstride0
= rstride
[0];
202 sstride0
= sstride
[0];
206 shift
= shift
% (ssize_t
)len
;
212 /* Do the shift for this dimension. */
214 /* If elements are contiguous, perform the operation
215 in two block moves. */
216 if (soffset
== size
&& roffset
== size
)
218 size_t len1
= shift
* size
;
219 size_t len2
= (len
- shift
) * size
;
220 memcpy (rptr
, sptr
+ len1
, len2
);
221 memcpy (rptr
+ len2
, sptr
, len1
);
225 /* Otherwise, we'll have to perform the copy one element at
226 a time. We can speed this up a tad for common cases of
227 fundamental types. */
233 const char *src
= &sptr
[shift
* soffset
];
235 for (n
= 0; n
< len
- shift
; n
++)
237 memcpy (dest
, src
, size
);
241 for (src
= sptr
, n
= 0; n
< shift
; n
++)
243 memcpy (dest
, src
, size
);
251 copy_loop_int (rptr
, sptr
, roffset
, soffset
, len
, shift
);
255 copy_loop_long (rptr
, sptr
, roffset
, soffset
, len
, shift
);
259 copy_loop_double (rptr
, sptr
, roffset
, soffset
, len
, shift
);
263 copy_loop_ldouble (rptr
, sptr
, roffset
, soffset
, len
, shift
);
267 copy_loop_cfloat (rptr
, sptr
, roffset
, soffset
, len
, shift
);
271 copy_loop_cdouble (rptr
, sptr
, roffset
, soffset
, len
, shift
);
279 /* Advance to the next section. */
284 while (count
[n
] == extent
[n
])
286 /* When we get to the end of a dimension, reset it and increment
287 the next dimension. */
289 /* We could precalculate these products, but this is a less
290 frequently used path so probably not worth it. */
291 rptr
-= rstride
[n
] * extent
[n
];
292 sptr
-= sstride
[n
] * extent
[n
];
296 /* Break out of the loop. */
310 #define DEFINE_CSHIFT(N) \
311 extern void cshift0_##N (gfc_array_char *, const gfc_array_char *, \
312 const GFC_INTEGER_##N *, const GFC_INTEGER_##N *); \
313 export_proto(cshift0_##N); \
316 cshift0_##N (gfc_array_char *ret, const gfc_array_char *array, \
317 const GFC_INTEGER_##N *pshift, const GFC_INTEGER_##N *pdim) \
319 cshift0 (ret, array, *pshift, pdim ? *pdim : 1, \
320 GFC_DESCRIPTOR_SIZE (array)); \
323 extern void cshift0_##N##_char (gfc_array_char *, GFC_INTEGER_4, \
324 const gfc_array_char *, \
325 const GFC_INTEGER_##N *, \
326 const GFC_INTEGER_##N *, GFC_INTEGER_4); \
327 export_proto(cshift0_##N##_char); \
330 cshift0_##N##_char (gfc_array_char *ret, \
331 GFC_INTEGER_4 ret_length __attribute__((unused)), \
332 const gfc_array_char *array, \
333 const GFC_INTEGER_##N *pshift, \
334 const GFC_INTEGER_##N *pdim, \
335 GFC_INTEGER_4 array_length) \
337 cshift0 (ret, array, *pshift, pdim ? *pdim : 1, array_length); \