Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libgfortran / m4 / eoshift1.m4
blob898f778c9e7484632a926c7e74e8882e1234e852
1 `/* Implementation of the EOSHIFT intrinsic
2    Copyright 2002 Free Software Foundation, Inc.
3    Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file.  (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
19 executable.)
21 Libgfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING.  If not,
28 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 Boston, MA 02111-1307, USA.  */
31 #include "config.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <string.h>
35 #include "libgfortran.h"'
36 include(iparm.m4)dnl
38 static const char zeros[16] =
39   {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
41 extern void eoshift1_`'atype_kind (const gfc_array_char *,
42                                      const gfc_array_char *,
43                                      const atype *, const char *,
44                                      const atype_name *);
45 export_proto(eoshift1_`'atype_kind);
47 void
48 eoshift1_`'atype_kind (const gfc_array_char *ret,
49                        const gfc_array_char *array,
50                        const atype *h, const char *pbound,
51                        const atype_name *pwhich)
53   /* r.* indicates the return array.  */
54   index_type rstride[GFC_MAX_DIMENSIONS - 1];
55   index_type rstride0;
56   index_type roffset;
57   char *rptr;
58   char *dest;
59   /* s.* indicates the source array.  */
60   index_type sstride[GFC_MAX_DIMENSIONS - 1];
61   index_type sstride0;
62   index_type soffset;
63   const char *sptr;
64   const char *src;
65 `  /* h.* indicates the shift array.  */'
66   index_type hstride[GFC_MAX_DIMENSIONS - 1];
67   index_type hstride0;
68   const atype_name *hptr;
70   index_type count[GFC_MAX_DIMENSIONS - 1];
71   index_type extent[GFC_MAX_DIMENSIONS - 1];
72   index_type dim;
73   index_type size;
74   index_type len;
75   index_type n;
76   int which;
77   atype_name sh;
78   atype_name delta;
80   if (pwhich)
81     which = *pwhich - 1;
82   else
83     which = 0;
85   if (!pbound)
86     pbound = zeros;
88   size = GFC_DESCRIPTOR_SIZE (ret);
90   extent[0] = 1;
91   count[0] = 0;
92   size = GFC_DESCRIPTOR_SIZE (array);
93   n = 0;
94   for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
95     {
96       if (dim == which)
97         {
98           roffset = ret->dim[dim].stride * size;
99           if (roffset == 0)
100             roffset = size;
101           soffset = array->dim[dim].stride * size;
102           if (soffset == 0)
103             soffset = size;
104           len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
105         }
106       else
107         {
108           count[n] = 0;
109           extent[n] = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
110           rstride[n] = ret->dim[dim].stride * size;
111           sstride[n] = array->dim[dim].stride * size;
113           hstride[n] = h->dim[n].stride;
114           n++;
115         }
116     }
117   if (sstride[0] == 0)
118     sstride[0] = size;
119   if (rstride[0] == 0)
120     rstride[0] = size;
121   if (hstride[0] == 0)
122     hstride[0] = 1;
124   dim = GFC_DESCRIPTOR_RANK (array);
125   rstride0 = rstride[0];
126   sstride0 = sstride[0];
127   hstride0 = hstride[0];
128   rptr = ret->data;
129   sptr = array->data;
130   hptr = h->data;
132   while (rptr)
133     {
134 `      /* Do the shift for this dimension.  */'
135       sh = *hptr;
136       delta = (sh >= 0) ? sh: -sh;
137       if (sh > 0)
138         {
139           src = &sptr[delta * soffset];
140           dest = rptr;
141         }
142       else
143         {
144           src = sptr;
145           dest = &rptr[delta * roffset];
146         }
147       for (n = 0; n < len - delta; n++)
148         {
149           memcpy (dest, src, size);
150           dest += roffset;
151           src += soffset;
152         }
153       if (sh < 0)
154         dest = rptr;
155       n = delta;
157       while (n--)
158         {
159           memcpy (dest, pbound, size);
160           dest += roffset;
161         }
163       /* Advance to the next section.  */
164       rptr += rstride0;
165       sptr += sstride0;
166       hptr += hstride0;
167       count[0]++;
168       n = 0;
169       while (count[n] == extent[n])
170         {
171           /* When we get to the end of a dimension, reset it and increment
172              the next dimension.  */
173           count[n] = 0;
174           /* We could precalculate these products, but this is a less
175              frequently used path so proabably not worth it.  */
176           rptr -= rstride[n] * extent[n];
177           sptr -= sstride[n] * extent[n];
178           hptr -= hstride[n] * extent[n];
179           n++;
180           if (n >= dim - 1)
181             {
182               /* Break out of the loop.  */
183               rptr = NULL;
184               break;
185             }
186           else
187             {
188               count[n]++;
189               rptr += rstride[n];
190               sptr += sstride[n];
191               hptr += hstride[n];
192             }
193         }
194     }