OSDN Git Service

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