OSDN Git Service

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