OSDN Git Service

2004-07-23 Matthias Klose <doko@debian.org>
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / cshift0.c
1 /* Generic implementation of the CSHIFT intrinsic
2    Copyright 2003 Free Software Foundation, Inc.
3    Contributed by Feng Wang <wf_cs@yahoo.com>
4
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
6
7 Libgfortran 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 /* TODO: make this work for large shifts when
29    sizeof(int) < sizeof (index_type).  */
30
31 static void
32 __cshift0 (const gfc_array_char * ret, const gfc_array_char * array,
33     int shift, int which)
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
48   index_type count[GFC_MAX_DIMENSIONS - 1];
49   index_type extent[GFC_MAX_DIMENSIONS - 1];
50   index_type dim;
51   index_type size;
52   index_type len;
53   index_type n;
54
55   if (which < 1 || which > GFC_DESCRIPTOR_RANK (array))
56     runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
57
58   size = GFC_DESCRIPTOR_SIZE (ret);
59
60   which = which - 1;
61
62   extent[0] = 1;
63   count[0] = 0;
64   size = GFC_DESCRIPTOR_SIZE (array);
65   n = 0;
66
67 /* Initialized for avoiding compiler warnings.  */
68   roffset = size;
69   soffset = size;
70   len = 0;
71
72   for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
73     {
74       if (dim == which)
75         {
76           roffset = ret->dim[dim].stride * size;
77           if (roffset == 0)
78             roffset = size;
79           soffset = array->dim[dim].stride * size;
80           if (soffset == 0)
81             soffset = size;
82           len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
83         }
84       else
85         {
86           count[n] = 0;
87           extent[n] = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
88           rstride[n] = ret->dim[dim].stride * size;
89           sstride[n] = array->dim[dim].stride * size;
90           n++;
91         }
92     }
93   if (sstride[0] == 0)
94     sstride[0] = size;
95   if (rstride[0] == 0)
96     rstride[0] = size;
97
98   dim = GFC_DESCRIPTOR_RANK (array);
99   rstride0 = rstride[0];
100   sstride0 = sstride[0];
101   rptr = ret->data;
102   sptr = array->data;
103
104   shift = (div (shift, len)).rem;
105   if (shift < 0)
106     shift += len;
107
108   while (rptr)
109     {
110       /* Do the shift for this dimension.  */
111       src = &sptr[shift * soffset];
112       dest = rptr;
113       for (n = 0; n < len; n++)
114         {
115           memcpy (dest, src, size);
116           dest += roffset;
117           if (n == len - shift - 1)
118             src = sptr;
119           else
120             src += soffset;
121         }
122
123       /* Advance to the next section.  */
124       rptr += rstride0;
125       sptr += sstride0;
126       count[0]++;
127       n = 0;
128       while (count[n] == extent[n])
129         {
130           /* When we get to the end of a dimension, reset it and increment
131              the next dimension.  */
132           count[n] = 0;
133           /* We could precalculate these products, but this is a less
134              frequently used path so proabably not worth it.  */
135           rptr -= rstride[n] * extent[n];
136           sptr -= sstride[n] * extent[n];
137           n++;
138           if (n >= dim - 1)
139             {
140               /* Break out of the loop.  */
141               rptr = NULL;
142               break;
143             }
144           else
145             {
146               count[n]++;
147               rptr += rstride[n];
148               sptr += sstride[n];
149             }
150         }
151     }
152 }
153
154
155 void
156 __cshift0_4 (const gfc_array_char * ret, const gfc_array_char * array,
157     const GFC_INTEGER_4 * pshift, const GFC_INTEGER_4 * pdim)
158 {
159   __cshift0 (ret, array, *pshift, pdim ? *pdim : 1);
160 }
161
162
163 void
164 __cshift0_8 (const gfc_array_char * ret, const gfc_array_char * array,
165     const GFC_INTEGER_8 * pshift, const GFC_INTEGER_8 * pdim)
166 {
167   __cshift0 (ret, array, *pshift, pdim ? *pdim : 1);
168 }
169