OSDN Git Service

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