OSDN Git Service

* trans-array.c (gfc_trans_array_constructor): Loop over the parents.
[pf3gnuchains/gcc-fork.git] / libgfortran / runtime / in_unpack_generic.c
1 /* Generic helper function for repacking arrays.
2    Copyright 2003, 2004, 2005, 2007, 2009, 2010
3    Free Software Foundation, Inc.
4    Contributed by Paul Brook <paul@nowt.org>
5
6 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7
8 Libgfortran is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public
10 License as published by the Free Software Foundation; either
11 version 3 of the License, or (at your option) any later version.
12
13 Libgfortran is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
25 <http://www.gnu.org/licenses/>.  */
26
27 #include "libgfortran.h"
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <string.h>
31
32 extern void internal_unpack (gfc_array_char *, const void *);
33 export_proto(internal_unpack);
34
35 void
36 internal_unpack (gfc_array_char * d, const void * s)
37 {
38   index_type count[GFC_MAX_DIMENSIONS];
39   index_type extent[GFC_MAX_DIMENSIONS];
40   index_type stride[GFC_MAX_DIMENSIONS];
41   index_type stride0;
42   index_type dim;
43   index_type dsize;
44   char *dest;
45   const char *src;
46   int n;
47   int size;
48   int type_size;
49
50   dest = d->data;
51   /* This check may be redundant, but do it anyway.  */
52   if (s == dest || !s)
53     return;
54
55   type_size = GFC_DTYPE_TYPE_SIZE (d);
56   switch (type_size)
57     {
58     case GFC_DTYPE_INTEGER_1:
59     case GFC_DTYPE_LOGICAL_1:
60     case GFC_DTYPE_DERIVED_1:
61       internal_unpack_1 ((gfc_array_i1 *) d, (const GFC_INTEGER_1 *) s);
62       return;
63
64     case GFC_DTYPE_INTEGER_2:
65     case GFC_DTYPE_LOGICAL_2:
66       internal_unpack_2 ((gfc_array_i2 *) d, (const GFC_INTEGER_2 *) s);
67       return;
68
69     case GFC_DTYPE_INTEGER_4:
70     case GFC_DTYPE_LOGICAL_4:
71       internal_unpack_4 ((gfc_array_i4 *) d, (const GFC_INTEGER_4 *) s);
72       return;
73
74     case GFC_DTYPE_INTEGER_8:
75     case GFC_DTYPE_LOGICAL_8:
76       internal_unpack_8 ((gfc_array_i8 *) d, (const GFC_INTEGER_8 *) s);
77       return;
78
79 #if defined (HAVE_GFC_INTEGER_16)
80     case GFC_DTYPE_INTEGER_16:
81     case GFC_DTYPE_LOGICAL_16:
82       internal_unpack_16 ((gfc_array_i16 *) d, (const GFC_INTEGER_16 *) s);
83       return;
84 #endif
85
86     case GFC_DTYPE_REAL_4:
87       internal_unpack_r4 ((gfc_array_r4 *) d, (const GFC_REAL_4 *) s);
88       return;
89
90     case GFC_DTYPE_REAL_8:
91       internal_unpack_r8 ((gfc_array_r8 *) d, (const GFC_REAL_8 *) s);
92       return;
93
94 /* FIXME: This here is a hack, which will have to be removed when
95    the array descriptor is reworked.  Currently, we don't store the
96    kind value for the type, but only the size.  Because on targets with
97    __float128, we have sizeof(logn double) == sizeof(__float128),
98    we cannot discriminate here and have to fall back to the generic
99    handling (which is suboptimal).  */
100 #if !defined(GFC_REAL_16_IS_FLOAT128)
101 # if defined(HAVE_GFC_REAL_10)
102     case GFC_DTYPE_REAL_10:
103       internal_unpack_r10 ((gfc_array_r10 *) d, (const GFC_REAL_10 *) s);
104       return;
105 # endif
106
107 # if defined(HAVE_GFC_REAL_16)
108     case GFC_DTYPE_REAL_16:
109       internal_unpack_r16 ((gfc_array_r16 *) d, (const GFC_REAL_16 *) s);
110       return;
111 # endif
112 #endif
113
114     case GFC_DTYPE_COMPLEX_4:
115       internal_unpack_c4 ((gfc_array_c4 *)d, (const GFC_COMPLEX_4 *)s);
116       return;
117
118     case GFC_DTYPE_COMPLEX_8:
119       internal_unpack_c8 ((gfc_array_c8 *)d, (const GFC_COMPLEX_8 *)s);
120       return;
121
122 /* FIXME: This here is a hack, which will have to be removed when
123    the array descriptor is reworked.  Currently, we don't store the
124    kind value for the type, but only the size.  Because on targets with
125    __float128, we have sizeof(logn double) == sizeof(__float128),
126    we cannot discriminate here and have to fall back to the generic
127    handling (which is suboptimal).  */
128 #if !defined(GFC_REAL_16_IS_FLOAT128)
129 # if defined(HAVE_GFC_COMPLEX_10)
130     case GFC_DTYPE_COMPLEX_10:
131       internal_unpack_c10 ((gfc_array_c10 *) d, (const GFC_COMPLEX_10 *) s);
132       return;
133 # endif
134
135 # if defined(HAVE_GFC_COMPLEX_16)
136     case GFC_DTYPE_COMPLEX_16:
137       internal_unpack_c16 ((gfc_array_c16 *) d, (const GFC_COMPLEX_16 *) s);
138       return;
139 # endif
140 #endif
141
142     case GFC_DTYPE_DERIVED_2:
143       if (GFC_UNALIGNED_2(d->data) || GFC_UNALIGNED_2(s))
144         break;
145       else
146         {
147           internal_unpack_2 ((gfc_array_i2 *) d, (const GFC_INTEGER_2 *) s);
148           return;
149         }
150     case GFC_DTYPE_DERIVED_4:
151       if (GFC_UNALIGNED_4(d->data) || GFC_UNALIGNED_4(s))
152         break;
153       else
154         {
155           internal_unpack_4 ((gfc_array_i4 *) d, (const GFC_INTEGER_4 *) s);
156           return;
157         }
158
159     case GFC_DTYPE_DERIVED_8:
160       if (GFC_UNALIGNED_8(d->data) || GFC_UNALIGNED_8(s))
161         break;
162       else
163         {
164           internal_unpack_8 ((gfc_array_i8 *) d, (const GFC_INTEGER_8 *) s);
165           return;
166         }
167
168 #ifdef HAVE_GFC_INTEGER_16
169     case GFC_DTYPE_DERIVED_16:
170       if (GFC_UNALIGNED_16(d->data) || GFC_UNALIGNED_16(s))
171         break;
172       else
173         {
174           internal_unpack_16 ((gfc_array_i16 *) d, (const GFC_INTEGER_16 *) s);
175           return;
176         }
177 #endif
178
179     default:
180       break;
181     }
182
183   size = GFC_DESCRIPTOR_SIZE (d);
184
185   dim = GFC_DESCRIPTOR_RANK (d);
186   dsize = 1;
187   for (n = 0; n < dim; n++)
188     {
189       count[n] = 0;
190       stride[n] = GFC_DESCRIPTOR_STRIDE(d,n);
191       extent[n] = GFC_DESCRIPTOR_EXTENT(d,n);
192       if (extent[n] <= 0)
193         return;
194
195       if (dsize == stride[n])
196         dsize *= extent[n];
197       else
198         dsize = 0;
199     }
200
201   src = s;
202
203   if (dsize != 0)
204     {
205       memcpy (dest, src, dsize * size);
206       return;
207     }
208
209   stride0 = stride[0] * size;
210
211   while (dest)
212     {
213       /* Copy the data.  */
214       memcpy (dest, src, size);
215       /* Advance to the next element.  */
216       src += size;
217       dest += stride0;
218       count[0]++;
219       /* Advance to the next source element.  */
220       n = 0;
221       while (count[n] == extent[n])
222         {
223           /* When we get to the end of a dimension, reset it and increment
224              the next dimension.  */
225           count[n] = 0;
226           /* We could precalculate these products, but this is a less
227              frequently used path so probably not worth it.  */
228           dest -= stride[n] * extent[n] * size;
229           n++;
230           if (n == dim)
231             {
232               dest = NULL;
233               break;
234             }
235           else
236             {
237               count[n]++;
238               dest += stride[n] * size;
239             }
240         }
241     }
242 }