OSDN Git Service

2004-05-21 Frank Ch. Eigler <fche@redhat.com>
[pf3gnuchains/gcc-fork.git] / libgfortran / generated / cshift1_4.c
1 /* 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 void
29 __cshift1_4 (const gfc_array_char * ret, const gfc_array_char * array,
30     const gfc_array_i4 * h, const GFC_INTEGER_4 * pwhich)
31 {
32   /* r.* indicates the return array.  */
33   index_type rstride[GFC_MAX_DIMENSIONS - 1];
34   index_type rstride0;
35   index_type roffset;
36   char *rptr;
37   char *dest;
38   /* s.* indicates the source array.  */
39   index_type sstride[GFC_MAX_DIMENSIONS - 1];
40   index_type sstride0;
41   index_type soffset;
42   const char *sptr;
43   const char *src;
44   /* h.* indicates the shift array.  */
45   index_type hstride[GFC_MAX_DIMENSIONS - 1];
46   index_type hstride0;
47   const GFC_INTEGER_4 *hptr;
48
49   index_type count[GFC_MAX_DIMENSIONS - 1];
50   index_type extent[GFC_MAX_DIMENSIONS - 1];
51   index_type dim;
52   index_type size;
53   index_type len;
54   index_type n;
55   int which;
56   GFC_INTEGER_4 sh;
57
58   if (pwhich)
59     which = *pwhich - 1;
60   else
61     which = 0;
62
63   if (which < 0 || (which + 1) > GFC_DESCRIPTOR_RANK (array))
64     runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
65
66   size = GFC_DESCRIPTOR_SIZE (ret);
67
68   extent[0] = 1;
69   count[0] = 0;
70   size = GFC_DESCRIPTOR_SIZE (array);
71   n = 0;
72
73 /* Initialized for avoiding compiler warnings.  */
74   roffset = size;
75   soffset = size;
76   len = 0;
77
78   for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
79     {
80       if (dim == which)
81         {
82           roffset = ret->dim[dim].stride * size;
83           if (roffset == 0)
84             roffset = size;
85           soffset = array->dim[dim].stride * size;
86           if (soffset == 0)
87             soffset = size;
88           len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
89         }
90       else
91         {
92           count[n] = 0;
93           extent[n] = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
94           rstride[n] = ret->dim[dim].stride * size;
95           sstride[n] = array->dim[dim].stride * size;
96
97           hstride[n] = h->dim[n].stride;
98           n++;
99         }
100     }
101   if (sstride[0] == 0)
102     sstride[0] = size;
103   if (rstride[0] == 0)
104     rstride[0] = size;
105   if (hstride[0] == 0)
106     hstride[0] = 1;
107
108   dim = GFC_DESCRIPTOR_RANK (array);
109   rstride0 = rstride[0];
110   sstride0 = sstride[0];
111   hstride0 = hstride[0];
112   rptr = ret->data;
113   sptr = array->data;
114   hptr = h->data;
115
116   while (rptr)
117     {
118       /* Do the shift for this dimension.  */
119       sh = *hptr;
120       sh = (div (sh, len)).rem;
121       if (sh < 0)
122         sh += len;
123
124       src = &sptr[sh * soffset];
125       dest = rptr;
126
127       for (n = 0; n < len; n++)
128         {
129           memcpy (dest, src, size);
130           dest += roffset;
131           if (n == len - sh - 1)
132             src = sptr;
133           else
134             src += soffset;
135         }
136
137       /* Advance to the next section.  */
138       rptr += rstride0;
139       sptr += sstride0;
140       hptr += hstride0;
141       count[0]++;
142       n = 0;
143       while (count[n] == extent[n])
144         {
145           /* When we get to the end of a dimension, reset it and increment
146              the next dimension.  */
147           count[n] = 0;
148           /* We could precalculate these products, but this is a less
149              frequently used path so proabably not worth it.  */
150           rptr -= rstride[n] * extent[n];
151           sptr -= sstride[n] * extent[n];
152           hptr -= hstride[n] * extent[n];
153           n++;
154           if (n >= dim - 1)
155             {
156               /* Break out of the loop.  */
157               rptr = NULL;
158               break;
159             }
160           else
161             {
162               count[n]++;
163               rptr += rstride[n];
164               sptr += sstride[n];
165               hptr += hstride[n];
166             }
167         }
168     }
169 }
170