Merge tree-ssa-20020619-branch into mainline.
[gcc.git] / libgfortran / generated / eoshift1_8.c
1 /* Implementation of the EOSHIFT intrinsic
2 Copyright 2002 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 (libgfor).
6
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.
11
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.
16
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. */
21
22 #include "config.h"
23 #include <stdlib.h>
24 #include <assert.h>
25 #include <string.h>
26 #include "libgfortran.h"
27
28 static const char zeros[16] =
29 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
30
31 void
32 __eoshift1_8 (const gfc_array_char * ret, const gfc_array_char * array,
33 const gfc_array_i8 * h, const char * pbound, const GFC_INTEGER_8 * pwhich)
34 {
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
52 index_type count[GFC_MAX_DIMENSIONS - 1];
53 index_type extent[GFC_MAX_DIMENSIONS - 1];
54 index_type dim;
55 index_type size;
56 index_type len;
57 index_type n;
58 int which;
59 GFC_INTEGER_8 sh;
60 GFC_INTEGER_8 delta;
61
62 if (pwhich)
63 which = *pwhich - 1;
64 else
65 which = 0;
66
67 if (!pbound)
68 pbound = zeros;
69
70 size = GFC_DESCRIPTOR_SIZE (ret);
71
72 extent[0] = 1;
73 count[0] = 0;
74 size = GFC_DESCRIPTOR_SIZE (array);
75 n = 0;
76 for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
77 {
78 if (dim == which)
79 {
80 roffset = ret->dim[dim].stride * size;
81 if (roffset == 0)
82 roffset = size;
83 soffset = array->dim[dim].stride * size;
84 if (soffset == 0)
85 soffset = size;
86 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
87 }
88 else
89 {
90 count[n] = 0;
91 extent[n] = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
92 rstride[n] = ret->dim[dim].stride * size;
93 sstride[n] = array->dim[dim].stride * size;
94
95 hstride[n] = h->dim[n].stride;
96 n++;
97 }
98 }
99 if (sstride[0] == 0)
100 sstride[0] = size;
101 if (rstride[0] == 0)
102 rstride[0] = size;
103 if (hstride[0] == 0)
104 hstride[0] = 1;
105
106 dim = GFC_DESCRIPTOR_RANK (array);
107 rstride0 = rstride[0];
108 sstride0 = sstride[0];
109 hstride0 = hstride[0];
110 rptr = ret->data;
111 sptr = array->data;
112 hptr = h->data;
113
114 while (rptr)
115 {
116 /* Do the shift for this dimension. */
117 sh = *hptr;
118 delta = (sh >= 0) ? sh: -sh;
119 if (sh > 0)
120 {
121 src = &sptr[delta * soffset];
122 dest = rptr;
123 }
124 else
125 {
126 src = sptr;
127 dest = &rptr[delta * roffset];
128 }
129 for (n = 0; n < len - delta; n++)
130 {
131 memcpy (dest, src, size);
132 dest += roffset;
133 src += soffset;
134 }
135 if (sh < 0)
136 dest = rptr;
137 n = delta;
138
139 while (n--)
140 {
141 memcpy (dest, pbound, size);
142 dest += roffset;
143 }
144
145 /* Advance to the next section. */
146 rptr += rstride0;
147 sptr += sstride0;
148 hptr += hstride0;
149 count[0]++;
150 n = 0;
151 while (count[n] == extent[n])
152 {
153 /* When we get to the end of a dimension, reset it and increment
154 the next dimension. */
155 count[n] = 0;
156 /* We could precalculate these products, but this is a less
157 frequently used path so proabably not worth it. */
158 rptr -= rstride[n] * extent[n];
159 sptr -= sstride[n] * extent[n];
160 hptr -= hstride[n] * extent[n];
161 n++;
162 if (n >= dim - 1)
163 {
164 /* Break out of the loop. */
165 rptr = NULL;
166 break;
167 }
168 else
169 {
170 count[n]++;
171 rptr += rstride[n];
172 sptr += sstride[n];
173 hptr += hstride[n];
174 }
175 }
176 }
177 }
178