1 /* Generic implementation of the CSHIFT intrinsic
2 Copyright 2003 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., 59 Temple Place - Suite 330,
29 Boston, MA 02111-1307, 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)
78 cshift0 (gfc_array_char
* ret
, const gfc_array_char
* array
,
79 ssize_t shift
, int which
)
81 /* r.* indicates the return array. */
82 index_type rstride
[GFC_MAX_DIMENSIONS
- 1];
87 /* s.* indicates the source array. */
88 index_type sstride
[GFC_MAX_DIMENSIONS
- 1];
93 index_type count
[GFC_MAX_DIMENSIONS
- 1];
94 index_type extent
[GFC_MAX_DIMENSIONS
- 1];
100 if (which
< 1 || which
> GFC_DESCRIPTOR_RANK (array
))
101 runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
103 size
= GFC_DESCRIPTOR_SIZE (ret
);
109 size
= GFC_DESCRIPTOR_SIZE (array
);
112 /* Initialized for avoiding compiler warnings. */
117 if (ret
->data
== NULL
)
121 ret
->data
= internal_malloc_size (size
* size0 ((array_t
*)array
));
123 ret
->dtype
= array
->dtype
;
124 for (i
= 0; i
< GFC_DESCRIPTOR_RANK (array
); i
++)
126 ret
->dim
[i
].lbound
= 0;
127 ret
->dim
[i
].ubound
= array
->dim
[i
].ubound
- array
->dim
[i
].lbound
;
130 ret
->dim
[i
].stride
= 1;
132 ret
->dim
[i
].stride
= (ret
->dim
[i
-1].ubound
+ 1) * ret
->dim
[i
-1].stride
;
136 for (dim
= 0; dim
< GFC_DESCRIPTOR_RANK (array
); dim
++)
140 roffset
= ret
->dim
[dim
].stride
* size
;
143 soffset
= array
->dim
[dim
].stride
* size
;
146 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
151 extent
[n
] = array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
152 rstride
[n
] = ret
->dim
[dim
].stride
* size
;
153 sstride
[n
] = array
->dim
[dim
].stride
* size
;
162 dim
= GFC_DESCRIPTOR_RANK (array
);
163 rstride0
= rstride
[0];
164 sstride0
= sstride
[0];
168 shift
= shift
% (ssize_t
)len
;
174 /* Do the shift for this dimension. */
176 /* If elements are contiguous, perform the operation
177 in two block moves. */
178 if (soffset
== size
&& roffset
== size
)
180 size_t len1
= shift
* size
;
181 size_t len2
= (len
- shift
) * size
;
182 memcpy (rptr
, sptr
+ len1
, len2
);
183 memcpy (rptr
+ len2
, sptr
, len1
);
187 /* Otherwise, we'll have to perform the copy one element at
188 a time. We can speed this up a tad for common cases of
189 fundamental types. */
190 if (size
== sizeof(int))
191 copy_loop_int (rptr
, sptr
, roffset
, soffset
, len
, shift
);
192 else if (size
== sizeof(long))
193 copy_loop_long (rptr
, sptr
, roffset
, soffset
, len
, shift
);
194 else if (size
== sizeof(double))
195 copy_loop_double (rptr
, sptr
, roffset
, soffset
, len
, shift
);
196 else if (size
== sizeof(long double))
197 copy_loop_ldouble (rptr
, sptr
, roffset
, soffset
, len
, shift
);
201 const char *src
= &sptr
[shift
* soffset
];
203 for (n
= 0; n
< len
- shift
; n
++)
205 memcpy (dest
, src
, size
);
209 for (src
= sptr
, n
= 0; n
< shift
; n
++)
211 memcpy (dest
, src
, size
);
218 /* Advance to the next section. */
223 while (count
[n
] == extent
[n
])
225 /* When we get to the end of a dimension, reset it and increment
226 the next dimension. */
228 /* We could precalculate these products, but this is a less
229 frequently used path so proabably not worth it. */
230 rptr
-= rstride
[n
] * extent
[n
];
231 sptr
-= sstride
[n
] * extent
[n
];
235 /* Break out of the loop. */
250 extern void cshift0_1 (gfc_array_char
*, const gfc_array_char
*,
251 const GFC_INTEGER_1
*, const GFC_INTEGER_1
*);
252 export_proto(cshift0_1
);
255 cshift0_1 (gfc_array_char
*ret
, const gfc_array_char
*array
,
256 const GFC_INTEGER_1
*pshift
, const GFC_INTEGER_1
*pdim
)
258 cshift0 (ret
, array
, *pshift
, pdim
? *pdim
: 1);
262 extern void cshift0_2 (gfc_array_char
*, const gfc_array_char
*,
263 const GFC_INTEGER_2
*, const GFC_INTEGER_2
*);
264 export_proto(cshift0_2
);
267 cshift0_2 (gfc_array_char
*ret
, const gfc_array_char
*array
,
268 const GFC_INTEGER_2
*pshift
, const GFC_INTEGER_2
*pdim
)
270 cshift0 (ret
, array
, *pshift
, pdim
? *pdim
: 1);
274 extern void cshift0_4 (gfc_array_char
*, const gfc_array_char
*,
275 const GFC_INTEGER_4
*, const GFC_INTEGER_4
*);
276 export_proto(cshift0_4
);
279 cshift0_4 (gfc_array_char
*ret
, const gfc_array_char
*array
,
280 const GFC_INTEGER_4
*pshift
, const GFC_INTEGER_4
*pdim
)
282 cshift0 (ret
, array
, *pshift
, pdim
? *pdim
: 1);
286 extern void cshift0_8 (gfc_array_char
*, const gfc_array_char
*,
287 const GFC_INTEGER_8
*, const GFC_INTEGER_8
*);
288 export_proto(cshift0_8
);
291 cshift0_8 (gfc_array_char
*ret
, const gfc_array_char
*array
,
292 const GFC_INTEGER_8
*pshift
, const GFC_INTEGER_8
*pdim
)
294 cshift0 (ret
, array
, *pshift
, pdim
? *pdim
: 1);