re PR libfortran/19280 (Inconsistent licensing of libgfortran)
[gcc.git] / libgfortran / intrinsics / cshift0.c
1 /* Generic implementation of the CSHIFT intrinsic
2 Copyright 2003 Free Software Foundation, Inc.
3 Contributed by Feng Wang <wf_cs@yahoo.com>
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
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.
11
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
19 executable.)
20
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.
25
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. */
30
31 #include "config.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <string.h>
35 #include "libgfortran.h"
36
37
38 /* "Templatized" helper function for the inner shift loop. */
39
40 #define DEF_COPY_LOOP(NAME, TYPE) \
41 static inline void \
42 copy_loop_##NAME (void *xdest, const void *xsrc, \
43 size_t roff, size_t soff, \
44 index_type len, index_type shift) \
45 { \
46 TYPE *dest = xdest; \
47 const TYPE *src; \
48 index_type i; \
49 \
50 roff /= sizeof (TYPE); \
51 soff /= sizeof (TYPE); \
52 \
53 src = xsrc; \
54 src += shift * soff; \
55 for (i = 0; i < len - shift; ++i) \
56 { \
57 *dest = *src; \
58 dest += roff; \
59 src += soff; \
60 } \
61 \
62 src = xsrc; \
63 for (i = 0; i < shift; ++i) \
64 { \
65 *dest = *src; \
66 dest += roff; \
67 src += soff; \
68 } \
69 }
70
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
76
77 static void
78 cshift0 (gfc_array_char * ret, const gfc_array_char * array,
79 ssize_t shift, int which)
80 {
81 /* r.* indicates the return array. */
82 index_type rstride[GFC_MAX_DIMENSIONS - 1];
83 index_type rstride0;
84 index_type roffset;
85 char *rptr;
86
87 /* s.* indicates the source array. */
88 index_type sstride[GFC_MAX_DIMENSIONS - 1];
89 index_type sstride0;
90 index_type soffset;
91 const char *sptr;
92
93 index_type count[GFC_MAX_DIMENSIONS - 1];
94 index_type extent[GFC_MAX_DIMENSIONS - 1];
95 index_type dim;
96 index_type size;
97 index_type len;
98 index_type n;
99
100 if (which < 1 || which > GFC_DESCRIPTOR_RANK (array))
101 runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
102
103 size = GFC_DESCRIPTOR_SIZE (ret);
104
105 which = which - 1;
106
107 extent[0] = 1;
108 count[0] = 0;
109 size = GFC_DESCRIPTOR_SIZE (array);
110 n = 0;
111
112 /* Initialized for avoiding compiler warnings. */
113 roffset = size;
114 soffset = size;
115 len = 0;
116
117 if (ret->data == NULL)
118 {
119 int i;
120
121 ret->data = internal_malloc_size (size * size0 ((array_t *)array));
122 ret->base = 0;
123 ret->dtype = array->dtype;
124 for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
125 {
126 ret->dim[i].lbound = 0;
127 ret->dim[i].ubound = array->dim[i].ubound - array->dim[i].lbound;
128
129 if (i == 0)
130 ret->dim[i].stride = 1;
131 else
132 ret->dim[i].stride = (ret->dim[i-1].ubound + 1) * ret->dim[i-1].stride;
133 }
134 }
135
136 for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
137 {
138 if (dim == which)
139 {
140 roffset = ret->dim[dim].stride * size;
141 if (roffset == 0)
142 roffset = size;
143 soffset = array->dim[dim].stride * size;
144 if (soffset == 0)
145 soffset = size;
146 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
147 }
148 else
149 {
150 count[n] = 0;
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;
154 n++;
155 }
156 }
157 if (sstride[0] == 0)
158 sstride[0] = size;
159 if (rstride[0] == 0)
160 rstride[0] = size;
161
162 dim = GFC_DESCRIPTOR_RANK (array);
163 rstride0 = rstride[0];
164 sstride0 = sstride[0];
165 rptr = ret->data;
166 sptr = array->data;
167
168 shift = shift % (ssize_t)len;
169 if (shift < 0)
170 shift += len;
171
172 while (rptr)
173 {
174 /* Do the shift for this dimension. */
175
176 /* If elements are contiguous, perform the operation
177 in two block moves. */
178 if (soffset == size && roffset == size)
179 {
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);
184 }
185 else
186 {
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);
198 else
199 {
200 char *dest = rptr;
201 const char *src = &sptr[shift * soffset];
202
203 for (n = 0; n < len - shift; n++)
204 {
205 memcpy (dest, src, size);
206 dest += roffset;
207 src += soffset;
208 }
209 for (src = sptr, n = 0; n < shift; n++)
210 {
211 memcpy (dest, src, size);
212 dest += roffset;
213 src += soffset;
214 }
215 }
216 }
217
218 /* Advance to the next section. */
219 rptr += rstride0;
220 sptr += sstride0;
221 count[0]++;
222 n = 0;
223 while (count[n] == extent[n])
224 {
225 /* When we get to the end of a dimension, reset it and increment
226 the next dimension. */
227 count[n] = 0;
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];
232 n++;
233 if (n >= dim - 1)
234 {
235 /* Break out of the loop. */
236 rptr = NULL;
237 break;
238 }
239 else
240 {
241 count[n]++;
242 rptr += rstride[n];
243 sptr += sstride[n];
244 }
245 }
246 }
247 }
248
249
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);
253
254 void
255 cshift0_1 (gfc_array_char *ret, const gfc_array_char *array,
256 const GFC_INTEGER_1 *pshift, const GFC_INTEGER_1 *pdim)
257 {
258 cshift0 (ret, array, *pshift, pdim ? *pdim : 1);
259 }
260
261
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);
265
266 void
267 cshift0_2 (gfc_array_char *ret, const gfc_array_char *array,
268 const GFC_INTEGER_2 *pshift, const GFC_INTEGER_2 *pdim)
269 {
270 cshift0 (ret, array, *pshift, pdim ? *pdim : 1);
271 }
272
273
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);
277
278 void
279 cshift0_4 (gfc_array_char *ret, const gfc_array_char *array,
280 const GFC_INTEGER_4 *pshift, const GFC_INTEGER_4 *pdim)
281 {
282 cshift0 (ret, array, *pshift, pdim ? *pdim : 1);
283 }
284
285
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);
289
290 void
291 cshift0_8 (gfc_array_char *ret, const gfc_array_char *array,
292 const GFC_INTEGER_8 *pshift, const GFC_INTEGER_8 *pdim)
293 {
294 cshift0 (ret, array, *pshift, pdim ? *pdim : 1);
295 }
296