]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/intrinsics/eoshift0.c
re PR fortran/21594 ([4.0 only] FAIL: gfortran.dg/eoshift.f90 -O0 execution test)
[thirdparty/gcc.git] / libgfortran / intrinsics / eoshift0.c
1 /* Generic implementation of the EOSHIFT intrinsic
2 Copyright 2002, 2005 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
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 static const char zeros[16] =
38 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
39
40 /* TODO: make this work for large shifts when
41 sizeof(int) < sizeof (index_type). */
42
43 static void
44 eoshift0 (gfc_array_char * ret, const gfc_array_char * array,
45 int shift, const char * pbound, int which)
46 {
47 /* r.* indicates the return array. */
48 index_type rstride[GFC_MAX_DIMENSIONS];
49 index_type rstride0;
50 index_type roffset;
51 char *rptr;
52 char *dest;
53 /* s.* indicates the source array. */
54 index_type sstride[GFC_MAX_DIMENSIONS];
55 index_type sstride0;
56 index_type soffset;
57 const char *sptr;
58 const char *src;
59
60 index_type count[GFC_MAX_DIMENSIONS];
61 index_type extent[GFC_MAX_DIMENSIONS];
62 index_type dim;
63 index_type size;
64 index_type len;
65 index_type n;
66
67 /* The compiler cannot figure out that these are set, initialize
68 them to avoid warnings. */
69 len = 0;
70 soffset = 0;
71 roffset = 0;
72
73 if (!pbound)
74 pbound = zeros;
75
76 size = GFC_DESCRIPTOR_SIZE (ret);
77
78 if (ret->data == NULL)
79 {
80 int i;
81
82 ret->data = internal_malloc_size (size * size0 ((array_t *)array));
83 ret->base = 0;
84 ret->dtype = array->dtype;
85 for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
86 {
87 ret->dim[i].lbound = 0;
88 ret->dim[i].ubound = array->dim[i].ubound - array->dim[i].lbound;
89
90 if (i == 0)
91 ret->dim[i].stride = 1;
92 else
93 ret->dim[i].stride = (ret->dim[i-1].ubound + 1) * ret->dim[i-1].stride;
94 }
95 }
96
97 which = which - 1;
98
99 extent[0] = 1;
100 count[0] = 0;
101 size = GFC_DESCRIPTOR_SIZE (array);
102 n = 0;
103 for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
104 {
105 if (dim == which)
106 {
107 roffset = ret->dim[dim].stride * size;
108 if (roffset == 0)
109 roffset = size;
110 soffset = array->dim[dim].stride * size;
111 if (soffset == 0)
112 soffset = size;
113 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
114 }
115 else
116 {
117 count[n] = 0;
118 extent[n] = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
119 rstride[n] = ret->dim[dim].stride * size;
120 sstride[n] = array->dim[dim].stride * size;
121 n++;
122 }
123 }
124 if (sstride[0] == 0)
125 sstride[0] = size;
126 if (rstride[0] == 0)
127 rstride[0] = size;
128
129 dim = GFC_DESCRIPTOR_RANK (array);
130 rstride0 = rstride[0];
131 sstride0 = sstride[0];
132 rptr = ret->data;
133 sptr = array->data;
134
135 if ((shift >= 0 ? shift : -shift) > len)
136 {
137 shift = len;
138 len = 0;
139 }
140 else
141 {
142 if (shift > 0)
143 len = len - shift;
144 else
145 len = len + shift;
146 }
147
148 while (rptr)
149 {
150 /* Do the shift for this dimension. */
151 if (shift > 0)
152 {
153 src = &sptr[shift * soffset];
154 dest = rptr;
155 }
156 else
157 {
158 src = sptr;
159 dest = &rptr[-shift * roffset];
160 }
161 for (n = 0; n < len; n++)
162 {
163 memcpy (dest, src, size);
164 dest += roffset;
165 src += soffset;
166 }
167 if (shift >= 0)
168 {
169 n = shift;
170 }
171 else
172 {
173 dest = rptr;
174 n = -shift;
175 }
176
177 while (n--)
178 {
179 memcpy (dest, pbound, size);
180 dest += roffset;
181 }
182
183 /* Advance to the next section. */
184 rptr += rstride0;
185 sptr += sstride0;
186 count[0]++;
187 n = 0;
188 while (count[n] == extent[n])
189 {
190 /* When we get to the end of a dimension, reset it and increment
191 the next dimension. */
192 count[n] = 0;
193 /* We could precalculate these products, but this is a less
194 frequently used path so proabably not worth it. */
195 rptr -= rstride[n] * extent[n];
196 sptr -= sstride[n] * extent[n];
197 n++;
198 if (n >= dim - 1)
199 {
200 /* Break out of the loop. */
201 rptr = NULL;
202 break;
203 }
204 else
205 {
206 count[n]++;
207 rptr += rstride[n];
208 sptr += sstride[n];
209 }
210 }
211 }
212 }
213
214
215 extern void eoshift0_1 (gfc_array_char *, const gfc_array_char *,
216 const GFC_INTEGER_1 *, const char *,
217 const GFC_INTEGER_1 *);
218 export_proto(eoshift0_1);
219
220 void
221 eoshift0_1 (gfc_array_char *ret, const gfc_array_char *array,
222 const GFC_INTEGER_1 *pshift, const char *pbound,
223 const GFC_INTEGER_1 *pdim)
224 {
225 eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1);
226 }
227
228
229 extern void eoshift0_2 (gfc_array_char *, const gfc_array_char *,
230 const GFC_INTEGER_2 *, const char *,
231 const GFC_INTEGER_2 *);
232 export_proto(eoshift0_2);
233
234 void
235 eoshift0_2 (gfc_array_char *ret, const gfc_array_char *array,
236 const GFC_INTEGER_2 *pshift, const char *pbound,
237 const GFC_INTEGER_2 *pdim)
238 {
239 eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1);
240 }
241
242
243 extern void eoshift0_4 (gfc_array_char *, const gfc_array_char *,
244 const GFC_INTEGER_4 *, const char *,
245 const GFC_INTEGER_4 *);
246 export_proto(eoshift0_4);
247
248 void
249 eoshift0_4 (gfc_array_char *ret, const gfc_array_char *array,
250 const GFC_INTEGER_4 *pshift, const char *pbound,
251 const GFC_INTEGER_4 *pdim)
252 {
253 eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1);
254 }
255
256
257 extern void eoshift0_8 (gfc_array_char *, const gfc_array_char *,
258 const GFC_INTEGER_8 *, const char *,
259 const GFC_INTEGER_8 *);
260 export_proto(eoshift0_8);
261
262 void
263 eoshift0_8 (gfc_array_char *ret, const gfc_array_char *array,
264 const GFC_INTEGER_8 *pshift, const char *pbound,
265 const GFC_INTEGER_8 *pdim)
266 {
267 eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1);
268 }