OSDN Git Service

b633169ae519ed02fad841480447788d466b2eec
[pf3gnuchains/gcc-fork.git] / libgfortran / m4 / cshift0.m4
1 `/* Helper function for cshift functions.
2    Copyright 2008 Free Software Foundation, Inc.
3    Contributed by Thomas Koenig <tkoenig@gcc.gnu.org>
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 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.
11
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.)
20
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.
25
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., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA.  */
30
31 #include "libgfortran.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <string.h>'
35
36 include(iparm.m4)dnl
37
38 `#if defined (HAVE_'rtype_name`)
39
40 void
41 cshift0_'rtype_code` ('rtype` *ret, const 'rtype` *array, ssize_t shift,
42                      int which)
43 {
44   /* r.* indicates the return array.  */
45   index_type rstride[GFC_MAX_DIMENSIONS];
46   index_type rstride0;
47   index_type roffset;
48   'rtype_name` *rptr;
49
50   /* s.* indicates the source array.  */
51   index_type sstride[GFC_MAX_DIMENSIONS];
52   index_type sstride0;
53   index_type soffset;
54   const 'rtype_name` *sptr;
55
56   index_type count[GFC_MAX_DIMENSIONS];
57   index_type extent[GFC_MAX_DIMENSIONS];
58   index_type dim;
59   index_type len;
60   index_type n;
61
62   which = which - 1;
63   sstride[0] = 0;
64   rstride[0] = 0;
65
66   extent[0] = 1;
67   count[0] = 0;
68   n = 0;
69   /* Initialized for avoiding compiler warnings.  */
70   roffset = 1;
71   soffset = 1;
72   len = 0;
73
74   for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
75     {
76       if (dim == which)
77         {
78           roffset = ret->dim[dim].stride;
79           if (roffset == 0)
80             roffset = 1;
81           soffset = array->dim[dim].stride;
82           if (soffset == 0)
83             soffset = 1;
84           len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
85         }
86       else
87         {
88           count[n] = 0;
89           extent[n] = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
90           rstride[n] = ret->dim[dim].stride;
91           sstride[n] = array->dim[dim].stride;
92           n++;
93         }
94     }
95   if (sstride[0] == 0)
96     sstride[0] = 1;
97   if (rstride[0] == 0)
98     rstride[0] = 1;
99
100   dim = GFC_DESCRIPTOR_RANK (array);
101   rstride0 = rstride[0];
102   sstride0 = sstride[0];
103   rptr = ret->data;
104   sptr = array->data;
105
106   shift = len == 0 ? 0 : shift % (ssize_t)len;
107   if (shift < 0)
108     shift += len;
109
110   while (rptr)
111     {
112       /* Do the shift for this dimension.  */
113
114       /* If elements are contiguous, perform the operation
115          in two block moves.  */
116       if (soffset == 1 && roffset == 1)
117         {
118           size_t len1 = shift * sizeof ('rtype_name`);
119           size_t len2 = (len - shift) * sizeof ('rtype_name`);
120           memcpy (rptr, sptr + shift, len2);
121           memcpy (rptr + (len - shift), sptr, len1);
122         }
123       else
124         {
125           /* Otherwise, we will have to perform the copy one element at
126              a time.  */
127           'rtype_name` *dest = rptr;
128           const 'rtype_name` *src = &sptr[shift * soffset];
129
130           for (n = 0; n < len - shift; n++)
131             {
132               *dest = *src;
133               dest += roffset;
134               src += soffset;
135             }
136           for (src = sptr, n = 0; n < shift; n++)
137             {
138               *dest = *src;
139               dest += roffset;
140               src += soffset;
141             }
142         }
143
144       /* Advance to the next section.  */
145       rptr += rstride0;
146       sptr += sstride0;
147       count[0]++;
148       n = 0;
149       while (count[n] == extent[n])
150         {
151           /* When we get to the end of a dimension, reset it and increment
152              the next dimension.  */
153           count[n] = 0;
154           /* We could precalculate these products, but this is a less
155              frequently used path so probably not worth it.  */
156           rptr -= rstride[n] * extent[n];
157           sptr -= sstride[n] * extent[n];
158           n++;
159           if (n >= dim - 1)
160             {
161               /* Break out of the loop.  */
162               rptr = NULL;
163               break;
164             }
165           else
166             {
167               count[n]++;
168               rptr += rstride[n];
169               sptr += sstride[n];
170             }
171         }
172     }
173
174   return;
175 }
176
177 #endif'