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 Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 Ligbfor 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 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "libgfortran.h"
29 /* "Templatized" helper function for the inner shift loop. */
31 #define DEF_COPY_LOOP(NAME, TYPE) \
33 copy_loop_##NAME (void *xdest, const void *xsrc, \
34 size_t roff, size_t soff, \
35 index_type len, index_type shift) \
41 roff /= sizeof (TYPE); \
42 soff /= sizeof (TYPE); \
45 src += shift * soff; \
46 for (i = 0; i < len - shift; ++i) \
54 for (i = 0; i < shift; ++i) \
62 DEF_COPY_LOOP(int, int)
63 DEF_COPY_LOOP(long, long)
64 DEF_COPY_LOOP(double, double)
65 DEF_COPY_LOOP(ldouble
, long double)
69 __cshift0 (gfc_array_char
* ret
, const gfc_array_char
* array
,
70 ssize_t shift
, int which
)
72 /* r.* indicates the return array. */
73 index_type rstride
[GFC_MAX_DIMENSIONS
- 1];
78 /* s.* indicates the source array. */
79 index_type sstride
[GFC_MAX_DIMENSIONS
- 1];
84 index_type count
[GFC_MAX_DIMENSIONS
- 1];
85 index_type extent
[GFC_MAX_DIMENSIONS
- 1];
91 if (which
< 1 || which
> GFC_DESCRIPTOR_RANK (array
))
92 runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
94 size
= GFC_DESCRIPTOR_SIZE (ret
);
100 size
= GFC_DESCRIPTOR_SIZE (array
);
103 /* Initialized for avoiding compiler warnings. */
108 if (ret
->data
== NULL
)
112 ret
->data
= internal_malloc (size
* size0 ((array_t
*)array
));
114 ret
->dtype
= array
->dtype
;
115 for (i
= 0; i
< GFC_DESCRIPTOR_RANK (array
); i
++)
117 ret
->dim
[i
].lbound
= 0;
118 ret
->dim
[i
].ubound
= array
->dim
[i
].ubound
- array
->dim
[i
].lbound
;
121 ret
->dim
[i
].stride
= 1;
123 ret
->dim
[i
].stride
= (ret
->dim
[i
-1].ubound
+ 1) * ret
->dim
[i
-1].stride
;
127 for (dim
= 0; dim
< GFC_DESCRIPTOR_RANK (array
); dim
++)
131 roffset
= ret
->dim
[dim
].stride
* size
;
134 soffset
= array
->dim
[dim
].stride
* size
;
137 len
= array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
142 extent
[n
] = array
->dim
[dim
].ubound
+ 1 - array
->dim
[dim
].lbound
;
143 rstride
[n
] = ret
->dim
[dim
].stride
* size
;
144 sstride
[n
] = array
->dim
[dim
].stride
* size
;
153 dim
= GFC_DESCRIPTOR_RANK (array
);
154 rstride0
= rstride
[0];
155 sstride0
= sstride
[0];
159 shift
= shift
% (ssize_t
)len
;
165 /* Do the shift for this dimension. */
167 /* If elements are contiguous, perform the operation
168 in two block moves. */
169 if (soffset
== size
&& roffset
== size
)
171 size_t len1
= shift
* size
;
172 size_t len2
= (len
- shift
) * size
;
173 memcpy (rptr
, sptr
+ len1
, len2
);
174 memcpy (rptr
+ len2
, sptr
, len1
);
178 /* Otherwise, we'll have to perform the copy one element at
179 a time. We can speed this up a tad for common cases of
180 fundamental types. */
181 if (size
== sizeof(int))
182 copy_loop_int (rptr
, sptr
, roffset
, soffset
, len
, shift
);
183 else if (size
== sizeof(long))
184 copy_loop_long (rptr
, sptr
, roffset
, soffset
, len
, shift
);
185 else if (size
== sizeof(double))
186 copy_loop_double (rptr
, sptr
, roffset
, soffset
, len
, shift
);
187 else if (size
== sizeof(long double))
188 copy_loop_ldouble (rptr
, sptr
, roffset
, soffset
, len
, shift
);
192 const char *src
= &sptr
[shift
* soffset
];
194 for (n
= 0; n
< len
- shift
; n
++)
196 memcpy (dest
, src
, size
);
200 for (src
= sptr
, n
= 0; n
< shift
; n
++)
202 memcpy (dest
, src
, size
);
209 /* Advance to the next section. */
214 while (count
[n
] == extent
[n
])
216 /* When we get to the end of a dimension, reset it and increment
217 the next dimension. */
219 /* We could precalculate these products, but this is a less
220 frequently used path so proabably not worth it. */
221 rptr
-= rstride
[n
] * extent
[n
];
222 sptr
-= sstride
[n
] * extent
[n
];
226 /* Break out of the loop. */
242 __cshift0_4 (gfc_array_char
* ret
, const gfc_array_char
* array
,
243 const GFC_INTEGER_4
* pshift
, const GFC_INTEGER_4
* pdim
)
245 __cshift0 (ret
, array
, *pshift
, pdim
? *pdim
: 1);
250 __cshift0_8 (gfc_array_char
* ret
, const gfc_array_char
* array
,
251 const GFC_INTEGER_8
* pshift
, const GFC_INTEGER_8
* pdim
)
253 __cshift0 (ret
, array
, *pshift
, pdim
? *pdim
: 1);