OSDN Git Service

2010-04-07 Iain Sandoe <iains@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / libgfortran / runtime / in_unpack_generic.c
1 /* Generic helper function for repacking arrays.
2    Copyright 2003, 2004, 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 extern void internal_unpack (gfc_array_char *, const void *);
32 export_proto(internal_unpack);
33
34 void
35 internal_unpack (gfc_array_char * d, const void * s)
36 {
37   index_type count[GFC_MAX_DIMENSIONS];
38   index_type extent[GFC_MAX_DIMENSIONS];
39   index_type stride[GFC_MAX_DIMENSIONS];
40   index_type stride0;
41   index_type dim;
42   index_type dsize;
43   char *dest;
44   const char *src;
45   int n;
46   int size;
47   int type_size;
48
49   dest = d->data;
50   /* This check may be redundant, but do it anyway.  */
51   if (s == dest || !s)
52     return;
53
54   type_size = GFC_DTYPE_TYPE_SIZE (d);
55   switch (type_size)
56     {
57     case GFC_DTYPE_INTEGER_1:
58     case GFC_DTYPE_LOGICAL_1:
59     case GFC_DTYPE_DERIVED_1:
60       internal_unpack_1 ((gfc_array_i1 *) d, (const GFC_INTEGER_1 *) s);
61       return;
62
63     case GFC_DTYPE_INTEGER_2:
64     case GFC_DTYPE_LOGICAL_2:
65       internal_unpack_2 ((gfc_array_i2 *) d, (const GFC_INTEGER_2 *) s);
66       return;
67
68     case GFC_DTYPE_INTEGER_4:
69     case GFC_DTYPE_LOGICAL_4:
70       internal_unpack_4 ((gfc_array_i4 *) d, (const GFC_INTEGER_4 *) s);
71       return;
72
73     case GFC_DTYPE_INTEGER_8:
74     case GFC_DTYPE_LOGICAL_8:
75       internal_unpack_8 ((gfc_array_i8 *) d, (const GFC_INTEGER_8 *) s);
76       return;
77
78 #if defined (HAVE_GFC_INTEGER_16)
79     case GFC_DTYPE_INTEGER_16:
80     case GFC_DTYPE_LOGICAL_16:
81       internal_unpack_16 ((gfc_array_i16 *) d, (const GFC_INTEGER_16 *) s);
82       return;
83 #endif
84     case GFC_DTYPE_REAL_4:
85       internal_unpack_r4 ((gfc_array_r4 *) d, (const GFC_REAL_4 *) s);
86       return;
87
88     case GFC_DTYPE_REAL_8:
89       internal_unpack_r8 ((gfc_array_r8 *) d, (const GFC_REAL_8 *) s);
90       return;
91
92 #if defined(HAVE_GFC_REAL_10)
93     case GFC_DTYPE_REAL_10:
94       internal_unpack_r10 ((gfc_array_r10 *) d, (const GFC_REAL_10 *) s);
95       return;
96 #endif
97
98 #if defined(HAVE_GFC_REAL_16)
99     case GFC_DTYPE_REAL_16:
100       internal_unpack_r16 ((gfc_array_r16 *) d, (const GFC_REAL_16 *) s);
101       return;
102 #endif
103     case GFC_DTYPE_COMPLEX_4:
104       internal_unpack_c4 ((gfc_array_c4 *)d, (const GFC_COMPLEX_4 *)s);
105       return;
106
107     case GFC_DTYPE_COMPLEX_8:
108       internal_unpack_c8 ((gfc_array_c8 *)d, (const GFC_COMPLEX_8 *)s);
109       return;
110
111 #if defined(HAVE_GFC_COMPLEX_10)
112     case GFC_DTYPE_COMPLEX_10:
113       internal_unpack_c10 ((gfc_array_c10 *) d, (const GFC_COMPLEX_10 *) s);
114       return;
115 #endif
116
117 #if defined(HAVE_GFC_COMPLEX_16)
118     case GFC_DTYPE_COMPLEX_16:
119       internal_unpack_c16 ((gfc_array_c16 *) d, (const GFC_COMPLEX_16 *) s);
120       return;
121 #endif
122     case GFC_DTYPE_DERIVED_2:
123       if (GFC_UNALIGNED_2(d->data) || GFC_UNALIGNED_2(s))
124         break;
125       else
126         {
127           internal_unpack_2 ((gfc_array_i2 *) d, (const GFC_INTEGER_2 *) s);
128           return;
129         }
130     case GFC_DTYPE_DERIVED_4:
131       if (GFC_UNALIGNED_4(d->data) || GFC_UNALIGNED_4(s))
132         break;
133       else
134         {
135           internal_unpack_4 ((gfc_array_i4 *) d, (const GFC_INTEGER_4 *) s);
136           return;
137         }
138
139     case GFC_DTYPE_DERIVED_8:
140       if (GFC_UNALIGNED_8(d->data) || GFC_UNALIGNED_8(s))
141         break;
142       else
143         {
144           internal_unpack_8 ((gfc_array_i8 *) d, (const GFC_INTEGER_8 *) s);
145           return;
146         }
147
148 #ifdef HAVE_GFC_INTEGER_16
149     case GFC_DTYPE_DERIVED_16:
150       if (GFC_UNALIGNED_16(d->data) || GFC_UNALIGNED_16(s))
151         break;
152       else
153         {
154           internal_unpack_16 ((gfc_array_i16 *) d, (const GFC_INTEGER_16 *) s);
155           return;
156         }
157 #endif
158
159     default:
160       break;
161     }
162
163   size = GFC_DESCRIPTOR_SIZE (d);
164
165   dim = GFC_DESCRIPTOR_RANK (d);
166   dsize = 1;
167   for (n = 0; n < dim; n++)
168     {
169       count[n] = 0;
170       stride[n] = GFC_DESCRIPTOR_STRIDE(d,n);
171       extent[n] = GFC_DESCRIPTOR_EXTENT(d,n);
172       if (extent[n] <= 0)
173         return;
174
175       if (dsize == stride[n])
176         dsize *= extent[n];
177       else
178         dsize = 0;
179     }
180
181   src = s;
182
183   if (dsize != 0)
184     {
185       memcpy (dest, src, dsize * size);
186       return;
187     }
188
189   stride0 = stride[0] * size;
190
191   while (dest)
192     {
193       /* Copy the data.  */
194       memcpy (dest, src, size);
195       /* Advance to the next element.  */
196       src += size;
197       dest += stride0;
198       count[0]++;
199       /* Advance to the next source element.  */
200       n = 0;
201       while (count[n] == extent[n])
202         {
203           /* When we get to the end of a dimension, reset it and increment
204              the next dimension.  */
205           count[n] = 0;
206           /* We could precalculate these products, but this is a less
207              frequently used path so probably not worth it.  */
208           dest -= stride[n] * extent[n] * size;
209           n++;
210           if (n == dim)
211             {
212               dest = NULL;
213               break;
214             }
215           else
216             {
217               count[n]++;
218               dest += stride[n] * size;
219             }
220         }
221     }
222 }