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