OSDN Git Service

Merge remote-tracking branch 'gnu/gcc-4_7-branch' into rework
[pf3gnuchains/gcc-fork.git] / libgfortran / generated / eoshift3_16.c
1 /* Implementation of the EOSHIFT intrinsic
2    Copyright 2002, 2005, 2007, 2009 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 3 of the License, or (at your option) any later version.
11
12 Libgfortran 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 General Public License for more details.
16
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
20
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24 <http://www.gnu.org/licenses/>.  */
25
26 #include "libgfortran.h"
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <string.h>
30
31
32 #if defined (HAVE_GFC_INTEGER_16)
33
34 static void
35 eoshift3 (gfc_array_char * const restrict ret, 
36         const gfc_array_char * const restrict array, 
37         const gfc_array_i16 * const restrict h,
38         const gfc_array_char * const restrict bound, 
39         const GFC_INTEGER_16 * const restrict pwhich,
40         const char * filler, index_type filler_len)
41 {
42   /* r.* indicates the return array.  */
43   index_type rstride[GFC_MAX_DIMENSIONS];
44   index_type rstride0;
45   index_type roffset;
46   char *rptr;
47   char * restrict dest;
48   /* s.* indicates the source array.  */
49   index_type sstride[GFC_MAX_DIMENSIONS];
50   index_type sstride0;
51   index_type soffset;
52   const char *sptr;
53   const char *src;
54   /* h.* indicates the shift array.  */
55   index_type hstride[GFC_MAX_DIMENSIONS];
56   index_type hstride0;
57   const GFC_INTEGER_16 *hptr;
58   /* b.* indicates the bound array.  */
59   index_type bstride[GFC_MAX_DIMENSIONS];
60   index_type bstride0;
61   const char *bptr;
62
63   index_type count[GFC_MAX_DIMENSIONS];
64   index_type extent[GFC_MAX_DIMENSIONS];
65   index_type dim;
66   index_type len;
67   index_type n;
68   index_type size;
69   index_type arraysize;
70   int which;
71   GFC_INTEGER_16 sh;
72   GFC_INTEGER_16 delta;
73
74   /* The compiler cannot figure out that these are set, initialize
75      them to avoid warnings.  */
76   len = 0;
77   soffset = 0;
78   roffset = 0;
79
80   arraysize = size0 ((array_t *) array);
81   size = GFC_DESCRIPTOR_SIZE(array);
82
83   if (pwhich)
84     which = *pwhich - 1;
85   else
86     which = 0;
87
88   if (ret->data == NULL)
89     {
90       int i;
91
92       ret->offset = 0;
93       ret->dtype = array->dtype;
94       for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
95         {
96           index_type ub, str;
97
98           ub = GFC_DESCRIPTOR_EXTENT(array,i) - 1;
99
100           if (i == 0)
101             str = 1;
102           else
103             str = GFC_DESCRIPTOR_EXTENT(ret,i-1)
104               * GFC_DESCRIPTOR_STRIDE(ret,i-1);
105
106           GFC_DIMENSION_SET(ret->dim[i], 0, ub, str);
107
108         }
109       /* internal_malloc_size allocates a single byte for zero size.  */
110       ret->data = internal_malloc_size (size * arraysize);
111
112     }
113   else if (unlikely (compile_options.bounds_check))
114     {
115       bounds_equal_extents ((array_t *) ret, (array_t *) array,
116                                  "return value", "EOSHIFT");
117     }
118
119   if (unlikely (compile_options.bounds_check))
120     {
121       bounds_reduced_extents ((array_t *) h, (array_t *) array, which,
122                               "SHIFT argument", "EOSHIFT");
123     }
124
125   if (arraysize == 0)
126     return;
127
128   extent[0] = 1;
129   count[0] = 0;
130   n = 0;
131   for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
132     {
133       if (dim == which)
134         {
135           roffset = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
136           if (roffset == 0)
137             roffset = size;
138           soffset = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
139           if (soffset == 0)
140             soffset = size;
141           len = GFC_DESCRIPTOR_EXTENT(array,dim);
142         }
143       else
144         {
145           count[n] = 0;
146           extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
147           rstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
148           sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
149
150           hstride[n] = GFC_DESCRIPTOR_STRIDE(h,n);
151           if (bound)
152             bstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(bound,n);
153           else
154             bstride[n] = 0;
155           n++;
156         }
157     }
158   if (sstride[0] == 0)
159     sstride[0] = size;
160   if (rstride[0] == 0)
161     rstride[0] = size;
162   if (hstride[0] == 0)
163     hstride[0] = 1;
164   if (bound && bstride[0] == 0)
165     bstride[0] = size;
166
167   dim = GFC_DESCRIPTOR_RANK (array);
168   rstride0 = rstride[0];
169   sstride0 = sstride[0];
170   hstride0 = hstride[0];
171   bstride0 = bstride[0];
172   rptr = ret->data;
173   sptr = array->data;
174   hptr = h->data;
175   if (bound)
176     bptr = bound->data;
177   else
178     bptr = NULL;
179
180   while (rptr)
181     {
182       /* Do the shift for this dimension.  */
183       sh = *hptr;
184       if (( sh >= 0 ? sh : -sh ) > len)
185         {
186           delta = len;
187           sh = len;
188         }
189       else
190         delta = (sh >= 0) ? sh: -sh;
191
192       if (sh > 0)
193         {
194           src = &sptr[delta * soffset];
195           dest = rptr;
196         }
197       else
198         {
199           src = sptr;
200           dest = &rptr[delta * roffset];
201         }
202       for (n = 0; n < len - delta; n++)
203         {
204           memcpy (dest, src, size);
205           dest += roffset;
206           src += soffset;
207         }
208       if (sh < 0)
209         dest = rptr;
210       n = delta;
211
212       if (bptr)
213         while (n--)
214           {
215             memcpy (dest, bptr, size);
216             dest += roffset;
217           }
218       else
219         while (n--)
220           {
221             index_type i;
222
223             if (filler_len == 1)
224               memset (dest, filler[0], size);
225             else
226               for (i = 0; i < size; i += filler_len)
227                 memcpy (&dest[i], filler, filler_len);
228
229             dest += roffset;
230           }
231
232       /* Advance to the next section.  */
233       rptr += rstride0;
234       sptr += sstride0;
235       hptr += hstride0;
236       bptr += bstride0;
237       count[0]++;
238       n = 0;
239       while (count[n] == extent[n])
240         {
241           /* When we get to the end of a dimension, reset it and increment
242              the next dimension.  */
243           count[n] = 0;
244           /* We could precalculate these products, but this is a less
245              frequently used path so probably not worth it.  */
246           rptr -= rstride[n] * extent[n];
247           sptr -= sstride[n] * extent[n];
248           hptr -= hstride[n] * extent[n];
249           bptr -= bstride[n] * extent[n];
250           n++;
251           if (n >= dim - 1)
252             {
253               /* Break out of the loop.  */
254               rptr = NULL;
255               break;
256             }
257           else
258             {
259               count[n]++;
260               rptr += rstride[n];
261               sptr += sstride[n];
262               hptr += hstride[n];
263               bptr += bstride[n];
264             }
265         }
266     }
267 }
268
269 extern void eoshift3_16 (gfc_array_char * const restrict, 
270         const gfc_array_char * const restrict,
271         const gfc_array_i16 * const restrict, 
272         const gfc_array_char * const restrict,
273         const GFC_INTEGER_16 *);
274 export_proto(eoshift3_16);
275
276 void
277 eoshift3_16 (gfc_array_char * const restrict ret, 
278         const gfc_array_char * const restrict array,
279         const gfc_array_i16 * const restrict h, 
280         const gfc_array_char * const restrict bound,
281         const GFC_INTEGER_16 * const restrict pwhich)
282 {
283   eoshift3 (ret, array, h, bound, pwhich, "\0", 1);
284 }
285
286
287 extern void eoshift3_16_char (gfc_array_char * const restrict, 
288         GFC_INTEGER_4,
289         const gfc_array_char * const restrict,
290         const gfc_array_i16 * const restrict,
291         const gfc_array_char * const restrict,
292         const GFC_INTEGER_16 * const restrict, 
293         GFC_INTEGER_4, GFC_INTEGER_4);
294 export_proto(eoshift3_16_char);
295
296 void
297 eoshift3_16_char (gfc_array_char * const restrict ret,
298         GFC_INTEGER_4 ret_length __attribute__((unused)),
299         const gfc_array_char * const restrict array, 
300         const gfc_array_i16 *  const restrict h,
301         const gfc_array_char * const restrict bound,
302         const GFC_INTEGER_16 * const restrict pwhich,
303         GFC_INTEGER_4 array_length __attribute__((unused)),
304         GFC_INTEGER_4 bound_length __attribute__((unused)))
305 {
306   eoshift3 (ret, array, h, bound, pwhich, " ", 1);
307 }
308
309
310 extern void eoshift3_16_char4 (gfc_array_char * const restrict, 
311         GFC_INTEGER_4,
312         const gfc_array_char * const restrict,
313         const gfc_array_i16 * const restrict,
314         const gfc_array_char * const restrict,
315         const GFC_INTEGER_16 * const restrict, 
316         GFC_INTEGER_4, GFC_INTEGER_4);
317 export_proto(eoshift3_16_char4);
318
319 void
320 eoshift3_16_char4 (gfc_array_char * const restrict ret,
321         GFC_INTEGER_4 ret_length __attribute__((unused)),
322         const gfc_array_char * const restrict array, 
323         const gfc_array_i16 *  const restrict h,
324         const gfc_array_char * const restrict bound,
325         const GFC_INTEGER_16 * const restrict pwhich,
326         GFC_INTEGER_4 array_length __attribute__((unused)),
327         GFC_INTEGER_4 bound_length __attribute__((unused)))
328 {
329   static const gfc_char4_t space = (unsigned char) ' ';
330   eoshift3 (ret, array, h, bound, pwhich,
331             (const char *) &space, sizeof (gfc_char4_t));
332 }
333
334 #endif