OSDN Git Service

PR libfortran/19308
[pf3gnuchains/gcc-fork.git] / libgfortran / generated / eoshift1_16.c
1 /* Implementation of the EOSHIFT intrinsic
2    Copyright 2002, 2005 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 (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 "config.h"
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <string.h>
35 #include "libgfortran.h"
36
37 #if defined (HAVE_GFC_INTEGER_16)
38
39 static void
40 eoshift1 (gfc_array_char *ret, const gfc_array_char *array, const gfc_array_i16 *h,
41           const char *pbound, const GFC_INTEGER_16 *pwhich, index_type size,
42           char filler)
43 {
44   /* r.* indicates the return array.  */
45   index_type rstride[GFC_MAX_DIMENSIONS];
46   index_type rstride0;
47   index_type roffset;
48   char *rptr;
49   char *dest;
50   /* s.* indicates the source array.  */
51   index_type sstride[GFC_MAX_DIMENSIONS];
52   index_type sstride0;
53   index_type soffset;
54   const char *sptr;
55   const char *src;
56   /* h.* indicates the shift array.  */
57   index_type hstride[GFC_MAX_DIMENSIONS];
58   index_type hstride0;
59   const GFC_INTEGER_16 *hptr;
60
61   index_type count[GFC_MAX_DIMENSIONS];
62   index_type extent[GFC_MAX_DIMENSIONS];
63   index_type dim;
64   index_type len;
65   index_type n;
66   int which;
67   GFC_INTEGER_16 sh;
68   GFC_INTEGER_16 delta;
69
70   /* The compiler cannot figure out that these are set, initialize
71      them to avoid warnings.  */
72   len = 0;
73   soffset = 0;
74   roffset = 0;
75
76   if (pwhich)
77     which = *pwhich - 1;
78   else
79     which = 0;
80
81   extent[0] = 1;
82   count[0] = 0;
83
84   if (ret->data == NULL)
85     {
86       int i;
87
88       ret->data = internal_malloc_size (size * size0 ((array_t *)array));
89       ret->offset = 0;
90       ret->dtype = array->dtype;
91       for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
92         {
93           ret->dim[i].lbound = 0;
94           ret->dim[i].ubound = array->dim[i].ubound - array->dim[i].lbound;
95
96           if (i == 0)
97             ret->dim[i].stride = 1;
98           else
99             ret->dim[i].stride = (ret->dim[i-1].ubound + 1) * ret->dim[i-1].stride;
100         }
101     }
102
103   n = 0;
104   for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
105     {
106       if (dim == which)
107         {
108           roffset = ret->dim[dim].stride * size;
109           if (roffset == 0)
110             roffset = size;
111           soffset = array->dim[dim].stride * size;
112           if (soffset == 0)
113             soffset = size;
114           len = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
115         }
116       else
117         {
118           count[n] = 0;
119           extent[n] = array->dim[dim].ubound + 1 - array->dim[dim].lbound;
120           rstride[n] = ret->dim[dim].stride * size;
121           sstride[n] = array->dim[dim].stride * size;
122
123           hstride[n] = h->dim[n].stride;
124           n++;
125         }
126     }
127   if (sstride[0] == 0)
128     sstride[0] = size;
129   if (rstride[0] == 0)
130     rstride[0] = size;
131   if (hstride[0] == 0)
132     hstride[0] = 1;
133
134   dim = GFC_DESCRIPTOR_RANK (array);
135   rstride0 = rstride[0];
136   sstride0 = sstride[0];
137   hstride0 = hstride[0];
138   rptr = ret->data;
139   sptr = array->data;
140   hptr = h->data;
141
142   while (rptr)
143     {
144       /* Do the shift for this dimension.  */
145       sh = *hptr;
146       if (( sh >= 0 ? sh : -sh ) > len)
147         {
148           delta = len;
149           sh = len;
150         }
151       else
152         delta = (sh >= 0) ? sh: -sh;
153
154       if (sh > 0)
155         {
156           src = &sptr[delta * soffset];
157           dest = rptr;
158         }
159       else
160         {
161           src = sptr;
162           dest = &rptr[delta * roffset];
163         }
164       for (n = 0; n < len - delta; n++)
165         {
166           memcpy (dest, src, size);
167           dest += roffset;
168           src += soffset;
169         }
170       if (sh < 0)
171         dest = rptr;
172       n = delta;
173
174       if (pbound)
175         while (n--)
176           {
177             memcpy (dest, pbound, size);
178             dest += roffset;
179           }
180       else
181         while (n--)
182           {
183             memset (dest, filler, size);
184             dest += roffset;
185           }
186
187       /* Advance to the next section.  */
188       rptr += rstride0;
189       sptr += sstride0;
190       hptr += hstride0;
191       count[0]++;
192       n = 0;
193       while (count[n] == extent[n])
194         {
195           /* When we get to the end of a dimension, reset it and increment
196              the next dimension.  */
197           count[n] = 0;
198           /* We could precalculate these products, but this is a less
199              frequently used path so proabably not worth it.  */
200           rptr -= rstride[n] * extent[n];
201           sptr -= sstride[n] * extent[n];
202           hptr -= hstride[n] * extent[n];
203           n++;
204           if (n >= dim - 1)
205             {
206               /* Break out of the loop.  */
207               rptr = NULL;
208               break;
209             }
210           else
211             {
212               count[n]++;
213               rptr += rstride[n];
214               sptr += sstride[n];
215               hptr += hstride[n];
216             }
217         }
218     }
219 }
220
221 void eoshift1_16 (gfc_array_char *, const gfc_array_char *,
222                             const gfc_array_i16 *, const char *, const GFC_INTEGER_16 *);
223 export_proto(eoshift1_16);
224
225 void
226 eoshift1_16 (gfc_array_char *ret, const gfc_array_char *array,
227                        const gfc_array_i16 *h, const char *pbound,
228                        const GFC_INTEGER_16 *pwhich)
229 {
230   eoshift1 (ret, array, h, pbound, pwhich, GFC_DESCRIPTOR_SIZE (array), 0);
231 }
232
233 void eoshift1_16_char (gfc_array_char *, GFC_INTEGER_4,
234                                    const gfc_array_char *, const gfc_array_i16 *,
235                                    const char *, const GFC_INTEGER_16 *,
236                                    GFC_INTEGER_4, GFC_INTEGER_4);
237 export_proto(eoshift1_16_char);
238
239 void
240 eoshift1_16_char (gfc_array_char *ret,
241                               GFC_INTEGER_4 ret_length __attribute__((unused)),
242                               const gfc_array_char *array, const gfc_array_i16 *h,
243                               const char *pbound, const GFC_INTEGER_16 *pwhich,
244                               GFC_INTEGER_4 array_length,
245                               GFC_INTEGER_4 bound_length
246                                 __attribute__((unused)))
247 {
248   eoshift1 (ret, array, h, pbound, pwhich, array_length, ' ');
249 }
250
251 #endif