OSDN Git Service

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