OSDN Git Service

00a50d5db7c6fe00de717da3f0d3e2f545f5e40a
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / cshift0.c
1 /* Generic implementation of the CSHIFT intrinsic
2    Copyright 2003, 2005, 2006, 2007, 2010 Free Software Foundation, Inc.
3    Contributed by Feng Wang <wf_cs@yahoo.com>
4
5 This file is part of the GNU Fortran 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 static void
32 cshift0 (gfc_array_char * ret, const gfc_array_char * array,
33          ptrdiff_t shift, int which, index_type size)
34 {
35   /* r.* indicates the return array.  */
36   index_type rstride[GFC_MAX_DIMENSIONS];
37   index_type rstride0;
38   index_type roffset;
39   char *rptr;
40
41   /* s.* indicates the source array.  */
42   index_type sstride[GFC_MAX_DIMENSIONS];
43   index_type sstride0;
44   index_type soffset;
45   const char *sptr;
46
47   index_type count[GFC_MAX_DIMENSIONS];
48   index_type extent[GFC_MAX_DIMENSIONS];
49   index_type dim;
50   index_type len;
51   index_type n;
52   index_type arraysize;
53
54   index_type type_size;
55
56   if (which < 1 || which > GFC_DESCRIPTOR_RANK (array))
57     runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
58
59   arraysize = size0 ((array_t *) array);
60
61   if (ret->data == NULL)
62     {
63       int i;
64
65       ret->offset = 0;
66       ret->dtype = array->dtype;
67       for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
68         {
69           index_type ub, str;
70
71           ub = GFC_DESCRIPTOR_EXTENT(array,i) - 1;
72
73           if (i == 0)
74             str = 1;
75           else
76             str = GFC_DESCRIPTOR_EXTENT(ret,i-1) *
77               GFC_DESCRIPTOR_STRIDE(ret,i-1);
78
79           GFC_DIMENSION_SET(ret->dim[i], 0, ub, str);
80         }
81
82       if (arraysize > 0)
83         ret->data = internal_malloc_size (size * arraysize);
84       else
85         ret->data = internal_malloc_size (1);
86     }
87   else if (unlikely (compile_options.bounds_check))
88     {
89       bounds_equal_extents ((array_t *) ret, (array_t *) array,
90                                  "return value", "CSHIFT");
91     }
92
93   if (arraysize == 0)
94     return;
95
96   type_size = GFC_DTYPE_TYPE_SIZE (array);
97
98   switch(type_size)
99     {
100     case GFC_DTYPE_LOGICAL_1:
101     case GFC_DTYPE_INTEGER_1:
102     case GFC_DTYPE_DERIVED_1:
103       cshift0_i1 ((gfc_array_i1 *)ret, (gfc_array_i1 *) array, shift, which);
104       return;
105
106     case GFC_DTYPE_LOGICAL_2:
107     case GFC_DTYPE_INTEGER_2:
108       cshift0_i2 ((gfc_array_i2 *)ret, (gfc_array_i2 *) array, shift, which);
109       return;
110
111     case GFC_DTYPE_LOGICAL_4:
112     case GFC_DTYPE_INTEGER_4:
113       cshift0_i4 ((gfc_array_i4 *)ret, (gfc_array_i4 *) array, shift, which);
114       return;
115
116     case GFC_DTYPE_LOGICAL_8:
117     case GFC_DTYPE_INTEGER_8:
118       cshift0_i8 ((gfc_array_i8 *)ret, (gfc_array_i8 *) array, shift, which);
119       return;
120
121 #ifdef HAVE_GFC_INTEGER_16
122     case GFC_DTYPE_LOGICAL_16:
123     case GFC_DTYPE_INTEGER_16:
124       cshift0_i16 ((gfc_array_i16 *)ret, (gfc_array_i16 *) array, shift,
125                    which);
126       return;
127 #endif
128
129     case GFC_DTYPE_REAL_4:
130       cshift0_r4 ((gfc_array_r4 *)ret, (gfc_array_r4 *) array, shift, which);
131       return;
132
133     case GFC_DTYPE_REAL_8:
134       cshift0_r8 ((gfc_array_r8 *)ret, (gfc_array_r8 *) array, shift, which);
135       return;
136
137 /* FIXME: This here is a hack, which will have to be removed when
138    the array descriptor is reworked.  Currently, we don't store the
139    kind value for the type, but only the size.  Because on targets with
140    __float128, we have sizeof(logn double) == sizeof(__float128),
141    we cannot discriminate here and have to fall back to the generic
142    handling (which is suboptimal).  */
143 #if !defined(GFC_REAL_16_IS_FLOAT128)
144 # ifdef HAVE_GFC_REAL_10
145     case GFC_DTYPE_REAL_10:
146       cshift0_r10 ((gfc_array_r10 *)ret, (gfc_array_r10 *) array, shift,
147                    which);
148       return;
149 # endif
150
151 # ifdef HAVE_GFC_REAL_16
152     case GFC_DTYPE_REAL_16:
153       cshift0_r16 ((gfc_array_r16 *)ret, (gfc_array_r16 *) array, shift,
154                    which);
155       return;
156 # endif
157 #endif
158
159     case GFC_DTYPE_COMPLEX_4:
160       cshift0_c4 ((gfc_array_c4 *)ret, (gfc_array_c4 *) array, shift, which);
161       return;
162
163     case GFC_DTYPE_COMPLEX_8:
164       cshift0_c8 ((gfc_array_c8 *)ret, (gfc_array_c8 *) array, shift, which);
165       return;
166
167 /* FIXME: This here is a hack, which will have to be removed when
168    the array descriptor is reworked.  Currently, we don't store the
169    kind value for the type, but only the size.  Because on targets with
170    __float128, we have sizeof(logn double) == sizeof(__float128),
171    we cannot discriminate here and have to fall back to the generic
172    handling (which is suboptimal).  */
173 #if !defined(GFC_REAL_16_IS_FLOAT128)
174 # ifdef HAVE_GFC_COMPLEX_10
175     case GFC_DTYPE_COMPLEX_10:
176       cshift0_c10 ((gfc_array_c10 *)ret, (gfc_array_c10 *) array, shift,
177                    which);
178       return;
179 # endif
180
181 # ifdef HAVE_GFC_COMPLEX_16
182     case GFC_DTYPE_COMPLEX_16:
183       cshift0_c16 ((gfc_array_c16 *)ret, (gfc_array_c16 *) array, shift,
184                    which);
185       return;
186 # endif
187 #endif
188
189     default:
190       break;
191     }
192
193   switch (size)
194     {
195       /* Let's check the actual alignment of the data pointers.  If they
196          are suitably aligned, we can safely call the unpack functions.  */
197
198     case sizeof (GFC_INTEGER_1):
199       cshift0_i1 ((gfc_array_i1 *) ret, (gfc_array_i1 *) array, shift,
200                   which);
201       break;
202
203     case sizeof (GFC_INTEGER_2):
204       if (GFC_UNALIGNED_2(ret->data) || GFC_UNALIGNED_2(array->data))
205         break;
206       else
207         {
208           cshift0_i2 ((gfc_array_i2 *) ret, (gfc_array_i2 *) array, shift,
209                       which);
210           return;
211         }
212
213     case sizeof (GFC_INTEGER_4):
214       if (GFC_UNALIGNED_4(ret->data) || GFC_UNALIGNED_4(array->data))
215         break;
216       else
217         {
218           cshift0_i4 ((gfc_array_i4 *)ret, (gfc_array_i4 *) array, shift,
219                       which);
220           return;
221         }
222
223     case sizeof (GFC_INTEGER_8):
224       if (GFC_UNALIGNED_8(ret->data) || GFC_UNALIGNED_8(array->data))
225         {
226           /* Let's try to use the complex routines.  First, a sanity
227              check that the sizes match; this should be optimized to
228              a no-op.  */
229           if (sizeof(GFC_INTEGER_8) != sizeof(GFC_COMPLEX_4))
230             break;
231
232           if (GFC_UNALIGNED_C4(ret->data) || GFC_UNALIGNED_C4(array->data))
233             break;
234
235           cshift0_c4 ((gfc_array_c4 *) ret, (gfc_array_c4 *) array, shift,
236                       which);
237           return;
238         }
239       else
240         {
241           cshift0_i8 ((gfc_array_i8 *)ret, (gfc_array_i8 *) array, shift,
242                       which);
243           return;
244         }
245
246 #ifdef HAVE_GFC_INTEGER_16
247     case sizeof (GFC_INTEGER_16):
248       if (GFC_UNALIGNED_16(ret->data) || GFC_UNALIGNED_16(array->data))
249         {
250           /* Let's try to use the complex routines.  First, a sanity
251              check that the sizes match; this should be optimized to
252              a no-op.  */
253           if (sizeof(GFC_INTEGER_16) != sizeof(GFC_COMPLEX_8))
254             break;
255
256           if (GFC_UNALIGNED_C8(ret->data) || GFC_UNALIGNED_C8(array->data))
257             break;
258
259           cshift0_c8 ((gfc_array_c8 *) ret, (gfc_array_c8 *) array, shift,
260                       which);
261           return;
262         }
263       else
264         {
265           cshift0_i16 ((gfc_array_i16 *) ret, (gfc_array_i16 *) array,
266                        shift, which);
267           return;
268         }
269 #else
270     case sizeof (GFC_COMPLEX_8):
271
272       if (GFC_UNALIGNED_C8(ret->data) || GFC_UNALIGNED_C8(array->data))
273         break;
274       else
275         {
276           cshift0_c8 ((gfc_array_c8 *) ret, (gfc_array_c8 *) array, shift,
277                       which);
278           return;
279         }
280 #endif
281
282     default:
283       break;
284     }
285
286
287   which = which - 1;
288   sstride[0] = 0;
289   rstride[0] = 0;
290
291   extent[0] = 1;
292   count[0] = 0;
293   n = 0;
294   /* Initialized for avoiding compiler warnings.  */
295   roffset = size;
296   soffset = size;
297   len = 0;
298
299   for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
300     {
301       if (dim == which)
302         {
303           roffset = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
304           if (roffset == 0)
305             roffset = size;
306           soffset = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
307           if (soffset == 0)
308             soffset = size;
309           len = GFC_DESCRIPTOR_EXTENT(array,dim);
310         }
311       else
312         {
313           count[n] = 0;
314           extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
315           rstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
316           sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
317           n++;
318         }
319     }
320   if (sstride[0] == 0)
321     sstride[0] = size;
322   if (rstride[0] == 0)
323     rstride[0] = size;
324
325   dim = GFC_DESCRIPTOR_RANK (array);
326   rstride0 = rstride[0];
327   sstride0 = sstride[0];
328   rptr = ret->data;
329   sptr = array->data;
330
331   shift = len == 0 ? 0 : shift % (ptrdiff_t)len;
332   if (shift < 0)
333     shift += len;
334
335   while (rptr)
336     {
337       /* Do the shift for this dimension.  */
338
339       /* If elements are contiguous, perform the operation
340          in two block moves.  */
341       if (soffset == size && roffset == size)
342         {
343           size_t len1 = shift * size;
344           size_t len2 = (len - shift) * size;
345           memcpy (rptr, sptr + len1, len2);
346           memcpy (rptr + len2, sptr, len1);
347         }
348       else
349         {
350           /* Otherwise, we'll have to perform the copy one element at
351              a time.  */
352           char *dest = rptr;
353           const char *src = &sptr[shift * soffset];
354
355           for (n = 0; n < len - shift; n++)
356             {
357               memcpy (dest, src, size);
358               dest += roffset;
359               src += soffset;
360             }
361           for (src = sptr, n = 0; n < shift; n++)
362             {
363               memcpy (dest, src, size);
364               dest += roffset;
365               src += soffset;
366             }
367         }
368
369       /* Advance to the next section.  */
370       rptr += rstride0;
371       sptr += sstride0;
372       count[0]++;
373       n = 0;
374       while (count[n] == extent[n])
375         {
376           /* When we get to the end of a dimension, reset it and increment
377              the next dimension.  */
378           count[n] = 0;
379           /* We could precalculate these products, but this is a less
380              frequently used path so probably not worth it.  */
381           rptr -= rstride[n] * extent[n];
382           sptr -= sstride[n] * extent[n];
383           n++;
384           if (n >= dim - 1)
385             {
386               /* Break out of the loop.  */
387               rptr = NULL;
388               break;
389             }
390           else
391             {
392               count[n]++;
393               rptr += rstride[n];
394               sptr += sstride[n];
395             }
396         }
397     }
398 }
399
400 #define DEFINE_CSHIFT(N)                                                      \
401   extern void cshift0_##N (gfc_array_char *, const gfc_array_char *,          \
402                            const GFC_INTEGER_##N *, const GFC_INTEGER_##N *); \
403   export_proto(cshift0_##N);                                                  \
404                                                                               \
405   void                                                                        \
406   cshift0_##N (gfc_array_char *ret, const gfc_array_char *array,              \
407                const GFC_INTEGER_##N *pshift, const GFC_INTEGER_##N *pdim)    \
408   {                                                                           \
409     cshift0 (ret, array, *pshift, pdim ? *pdim : 1,                           \
410              GFC_DESCRIPTOR_SIZE (array));                                    \
411   }                                                                           \
412                                                                               \
413   extern void cshift0_##N##_char (gfc_array_char *, GFC_INTEGER_4,            \
414                                   const gfc_array_char *,                     \
415                                   const GFC_INTEGER_##N *,                    \
416                                   const GFC_INTEGER_##N *, GFC_INTEGER_4);    \
417   export_proto(cshift0_##N##_char);                                           \
418                                                                               \
419   void                                                                        \
420   cshift0_##N##_char (gfc_array_char *ret,                                    \
421                       GFC_INTEGER_4 ret_length __attribute__((unused)),       \
422                       const gfc_array_char *array,                            \
423                       const GFC_INTEGER_##N *pshift,                          \
424                       const GFC_INTEGER_##N *pdim,                            \
425                       GFC_INTEGER_4 array_length)                             \
426   {                                                                           \
427     cshift0 (ret, array, *pshift, pdim ? *pdim : 1, array_length);            \
428   }                                                                           \
429                                                                               \
430   extern void cshift0_##N##_char4 (gfc_array_char *, GFC_INTEGER_4,           \
431                                    const gfc_array_char *,                    \
432                                    const GFC_INTEGER_##N *,                   \
433                                    const GFC_INTEGER_##N *, GFC_INTEGER_4);   \
434   export_proto(cshift0_##N##_char4);                                          \
435                                                                               \
436   void                                                                        \
437   cshift0_##N##_char4 (gfc_array_char *ret,                                   \
438                        GFC_INTEGER_4 ret_length __attribute__((unused)),      \
439                        const gfc_array_char *array,                           \
440                        const GFC_INTEGER_##N *pshift,                         \
441                        const GFC_INTEGER_##N *pdim,                           \
442                        GFC_INTEGER_4 array_length)                            \
443   {                                                                           \
444     cshift0 (ret, array, *pshift, pdim ? *pdim : 1,                           \
445              array_length * sizeof (gfc_char4_t));                            \
446   }
447
448 DEFINE_CSHIFT (1);
449 DEFINE_CSHIFT (2);
450 DEFINE_CSHIFT (4);
451 DEFINE_CSHIFT (8);
452 #ifdef HAVE_GFC_INTEGER_16
453 DEFINE_CSHIFT (16);
454 #endif