acinclude.m4 (LIBGFOR_CHECK_ATTRIBUTE_VISIBILITY): New.
[gcc.git] / libgfortran / m4 / eoshift1.m4
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 include(iparm.m4)dnl
28
29 static const char zeros[16] =
30 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
31
32 extern void __eoshift1_`'atype_kind (const gfc_array_char *,
33 const gfc_array_char *,
34 const atype *, const char *,
35 const atype_name *);
36 export_proto_np(__eoshift1_`'atype_kind);
37
38 void
39 __eoshift1_`'atype_kind (const gfc_array_char *ret,
40 const gfc_array_char *array,
41 const atype *h, const char *pbound,
42 const atype_name *pwhich)
43 {
44 /* r.* indicates the return array. */
45 index_type rstride[GFC_MAX_DIMENSIONS - 1];
46 index_type rstride0;
47 index_type roffset;
48 char *rptr;
49 char *dest;
50 /* s.* indicates the source array. */
51 index_type sstride[GFC_MAX_DIMENSIONS - 1];
52 index_type sstride0;
53 index_type soffset;
54 const char *sptr;
55 const char *src;
56 ` /* h.* indicates the shift array. */'
57 index_type hstride[GFC_MAX_DIMENSIONS - 1];
58 index_type hstride0;
59 const atype_name *hptr;
60
61 index_type count[GFC_MAX_DIMENSIONS - 1];
62 index_type extent[GFC_MAX_DIMENSIONS - 1];
63 index_type dim;
64 index_type size;
65 index_type len;
66 index_type n;
67 int which;
68 atype_name sh;
69 atype_name delta;
70
71 if (pwhich)
72 which = *pwhich - 1;
73 else
74 which = 0;
75
76 if (!pbound)
77 pbound = zeros;
78
79 size = GFC_DESCRIPTOR_SIZE (ret);
80
81 extent[0] = 1;
82 count[0] = 0;
83 size = GFC_DESCRIPTOR_SIZE (array);
84 n = 0;
85 for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
86 {
87 if (dim == which)
88 {
89 roffset = ret->dim[dim].stride * size;
90 if (roffset == 0)
91 roffset = size;
92 soffset = array->dim[dim].stride * size;
93 if (soffset == 0)
94 soffset = size;
95 len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
96 }
97 else
98 {
99 count[n] = 0;
100 extent[n] = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
101 rstride[n] = ret->dim[dim].stride * size;
102 sstride[n] = array->dim[dim].stride * size;
103
104 hstride[n] = h->dim[n].stride;
105 n++;
106 }
107 }
108 if (sstride[0] == 0)
109 sstride[0] = size;
110 if (rstride[0] == 0)
111 rstride[0] = size;
112 if (hstride[0] == 0)
113 hstride[0] = 1;
114
115 dim = GFC_DESCRIPTOR_RANK (array);
116 rstride0 = rstride[0];
117 sstride0 = sstride[0];
118 hstride0 = hstride[0];
119 rptr = ret->data;
120 sptr = array->data;
121 hptr = h->data;
122
123 while (rptr)
124 {
125 ` /* Do the shift for this dimension. */'
126 sh = *hptr;
127 delta = (sh >= 0) ? sh: -sh;
128 if (sh > 0)
129 {
130 src = &sptr[delta * soffset];
131 dest = rptr;
132 }
133 else
134 {
135 src = sptr;
136 dest = &rptr[delta * roffset];
137 }
138 for (n = 0; n < len - delta; n++)
139 {
140 memcpy (dest, src, size);
141 dest += roffset;
142 src += soffset;
143 }
144 if (sh < 0)
145 dest = rptr;
146 n = delta;
147
148 while (n--)
149 {
150 memcpy (dest, pbound, size);
151 dest += roffset;
152 }
153
154 /* Advance to the next section. */
155 rptr += rstride0;
156 sptr += sstride0;
157 hptr += hstride0;
158 count[0]++;
159 n = 0;
160 while (count[n] == extent[n])
161 {
162 /* When we get to the end of a dimension, reset it and increment
163 the next dimension. */
164 count[n] = 0;
165 /* We could precalculate these products, but this is a less
166 frequently used path so proabably not worth it. */
167 rptr -= rstride[n] * extent[n];
168 sptr -= sstride[n] * extent[n];
169 hptr -= hstride[n] * extent[n];
170 n++;
171 if (n >= dim - 1)
172 {
173 /* Break out of the loop. */
174 rptr = NULL;
175 break;
176 }
177 else
178 {
179 count[n]++;
180 rptr += rstride[n];
181 sptr += sstride[n];
182 hptr += hstride[n];
183 }
184 }
185 }
186 }