2004-07-23 Matthias Klose <doko@debian.org>
[official-gcc.git] / libgfortran / generated / eoshift3_8.c
blobfcb161df39a6e8f80ed74bb2cb8cce39463c90a3
1 /* Implementation of the EOSHIFT intrinsic
2 Copyright 2002 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 runtime library (libgfor).
7 Libgfor 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. */
22 #include "config.h"
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <string.h>
26 #include "libgfortran.h"
28 static const char zeros[16] =
29 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
31 void
32 __eoshift3_8 (gfc_array_char * ret, gfc_array_char * array,
33 gfc_array_i8 * h, const gfc_array_char * bound, GFC_INTEGER_8 * pwhich)
35 /* r.* indicates the return array. */
36 index_type rstride[GFC_MAX_DIMENSIONS - 1];
37 index_type rstride0;
38 index_type roffset;
39 char *rptr;
40 char *dest;
41 /* s.* indicates the source array. */
42 index_type sstride[GFC_MAX_DIMENSIONS - 1];
43 index_type sstride0;
44 index_type soffset;
45 const char *sptr;
46 const char *src;
47 /* h.* indicates the shift array. */
48 index_type hstride[GFC_MAX_DIMENSIONS - 1];
49 index_type hstride0;
50 const GFC_INTEGER_8 *hptr;
51 /* b.* indicates the bound array. */
52 index_type bstride[GFC_MAX_DIMENSIONS - 1];
53 index_type bstride0;
54 const char *bptr;
56 index_type count[GFC_MAX_DIMENSIONS - 1];
57 index_type extent[GFC_MAX_DIMENSIONS - 1];
58 index_type dim;
59 index_type size;
60 index_type len;
61 index_type n;
62 int which;
63 GFC_INTEGER_8 sh;
64 GFC_INTEGER_8 delta;
66 if (pwhich)
67 which = *pwhich - 1;
68 else
69 which = 0;
71 size = GFC_DESCRIPTOR_SIZE (ret);
73 extent[0] = 1;
74 count[0] = 0;
75 size = GFC_DESCRIPTOR_SIZE (array);
76 n = 0;
77 for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
79 if (dim == which)
81 roffset = ret->dim[dim].stride * size;
82 if (roffset == 0)
83 roffset = size;
84 soffset = array->dim[dim].stride * size;
85 if (soffset == 0)
86 soffset = size;
87 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
89 else
91 count[n] = 0;
92 extent[n] = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
93 rstride[n] = ret->dim[dim].stride * size;
94 sstride[n] = array->dim[dim].stride * size;
96 hstride[n] = h->dim[n].stride;
97 if (bound)
98 bstride[n] = bound->dim[n].stride;
99 else
100 bstride[n] = 0;
101 n++;
104 if (sstride[0] == 0)
105 sstride[0] = size;
106 if (rstride[0] == 0)
107 rstride[0] = size;
108 if (hstride[0] == 0)
109 hstride[0] = 1;
110 if (bound && bstride[0] == 0)
111 bstride[0] = size;
113 dim = GFC_DESCRIPTOR_RANK (array);
114 rstride0 = rstride[0];
115 sstride0 = sstride[0];
116 hstride0 = hstride[0];
117 bstride0 = bstride[0];
118 rptr = ret->data;
119 sptr = array->data;
120 hptr = h->data;
121 if (bound)
122 bptr = bound->data;
123 else
124 bptr = zeros;
126 while (rptr)
128 /* Do the shift for this dimension. */
129 sh = *hptr;
130 delta = (sh >= 0) ? sh: -sh;
131 if (sh > 0)
133 src = &sptr[delta * soffset];
134 dest = rptr;
136 else
138 src = sptr;
139 dest = &rptr[delta * roffset];
141 for (n = 0; n < len - delta; n++)
143 memcpy (dest, src, size);
144 dest += roffset;
145 src += soffset;
147 if (sh < 0)
148 dest = rptr;
149 n = delta;
151 while (n--)
153 memcpy (dest, bptr, size);
154 dest += roffset;
157 /* Advance to the next section. */
158 rptr += rstride0;
159 sptr += sstride0;
160 hptr += hstride0;
161 bptr += bstride0;
162 count[0]++;
163 n = 0;
164 while (count[n] == extent[n])
166 /* When we get to the end of a dimension, reset it and increment
167 the next dimension. */
168 count[n] = 0;
169 /* We could precalculate these products, but this is a less
170 frequently used path so proabably not worth it. */
171 rptr -= rstride[n] * extent[n];
172 sptr -= sstride[n] * extent[n];
173 hptr -= hstride[n] * extent[n];
174 bptr -= bstride[n] * extent[n];
175 n++;
176 if (n >= dim - 1)
178 /* Break out of the loop. */
179 rptr = NULL;
180 break;
182 else
184 count[n]++;
185 rptr += rstride[n];
186 sptr += sstride[n];
187 hptr += hstride[n];
188 bptr += bstride[n];