1181254a7Smrg /* Generic helper function for repacking arrays.
2*b1e83836Smrg Copyright (C) 2003-2022 Free Software Foundation, Inc.
3181254a7Smrg Contributed by Paul Brook <paul@nowt.org>
4181254a7Smrg
5181254a7Smrg This file is part of the GNU Fortran runtime library (libgfortran).
6181254a7Smrg
7181254a7Smrg Libgfortran is free software; you can redistribute it and/or
8181254a7Smrg modify it under the terms of the GNU General Public
9181254a7Smrg License as published by the Free Software Foundation; either
10181254a7Smrg version 3 of the License, or (at your option) any later version.
11181254a7Smrg
12181254a7Smrg Libgfortran is distributed in the hope that it will be useful,
13181254a7Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
14181254a7Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15181254a7Smrg GNU General Public License for more details.
16181254a7Smrg
17181254a7Smrg Under Section 7 of GPL version 3, you are granted additional
18181254a7Smrg permissions described in the GCC Runtime Library Exception, version
19181254a7Smrg 3.1, as published by the Free Software Foundation.
20181254a7Smrg
21181254a7Smrg You should have received a copy of the GNU General Public License and
22181254a7Smrg a copy of the GCC Runtime Library Exception along with this program;
23181254a7Smrg see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24181254a7Smrg <http://www.gnu.org/licenses/>. */
25181254a7Smrg
26181254a7Smrg #include "libgfortran.h"
27181254a7Smrg #include <string.h>
28181254a7Smrg
29181254a7Smrg extern void internal_unpack (gfc_array_char *, const void *);
30181254a7Smrg export_proto(internal_unpack);
31181254a7Smrg
32181254a7Smrg void
internal_unpack(gfc_array_char * d,const void * s)33181254a7Smrg internal_unpack (gfc_array_char * d, const void * s)
34181254a7Smrg {
35181254a7Smrg index_type count[GFC_MAX_DIMENSIONS];
36181254a7Smrg index_type extent[GFC_MAX_DIMENSIONS];
37181254a7Smrg index_type stride[GFC_MAX_DIMENSIONS];
38181254a7Smrg index_type stride0;
39181254a7Smrg index_type dim;
40181254a7Smrg index_type dsize;
41181254a7Smrg char *dest;
42181254a7Smrg const char *src;
43181254a7Smrg index_type size;
44181254a7Smrg int type_size;
45181254a7Smrg
46181254a7Smrg dest = d->base_addr;
47181254a7Smrg /* This check may be redundant, but do it anyway. */
48181254a7Smrg if (s == dest || !s)
49181254a7Smrg return;
50181254a7Smrg
51181254a7Smrg type_size = GFC_DTYPE_TYPE_SIZE (d);
52181254a7Smrg switch (type_size)
53181254a7Smrg {
54181254a7Smrg case GFC_DTYPE_INTEGER_1:
55181254a7Smrg case GFC_DTYPE_LOGICAL_1:
56181254a7Smrg internal_unpack_1 ((gfc_array_i1 *) d, (const GFC_INTEGER_1 *) s);
57181254a7Smrg return;
58181254a7Smrg
59181254a7Smrg case GFC_DTYPE_INTEGER_2:
60181254a7Smrg case GFC_DTYPE_LOGICAL_2:
61181254a7Smrg internal_unpack_2 ((gfc_array_i2 *) d, (const GFC_INTEGER_2 *) s);
62181254a7Smrg return;
63181254a7Smrg
64181254a7Smrg case GFC_DTYPE_INTEGER_4:
65181254a7Smrg case GFC_DTYPE_LOGICAL_4:
66181254a7Smrg internal_unpack_4 ((gfc_array_i4 *) d, (const GFC_INTEGER_4 *) s);
67181254a7Smrg return;
68181254a7Smrg
69181254a7Smrg case GFC_DTYPE_INTEGER_8:
70181254a7Smrg case GFC_DTYPE_LOGICAL_8:
71181254a7Smrg internal_unpack_8 ((gfc_array_i8 *) d, (const GFC_INTEGER_8 *) s);
72181254a7Smrg return;
73181254a7Smrg
74181254a7Smrg #if defined (HAVE_GFC_INTEGER_16)
75181254a7Smrg case GFC_DTYPE_INTEGER_16:
76181254a7Smrg case GFC_DTYPE_LOGICAL_16:
77181254a7Smrg internal_unpack_16 ((gfc_array_i16 *) d, (const GFC_INTEGER_16 *) s);
78181254a7Smrg return;
79181254a7Smrg #endif
80181254a7Smrg
81181254a7Smrg case GFC_DTYPE_REAL_4:
82181254a7Smrg internal_unpack_r4 ((gfc_array_r4 *) d, (const GFC_REAL_4 *) s);
83181254a7Smrg return;
84181254a7Smrg
85181254a7Smrg case GFC_DTYPE_REAL_8:
86181254a7Smrg internal_unpack_r8 ((gfc_array_r8 *) d, (const GFC_REAL_8 *) s);
87181254a7Smrg return;
88181254a7Smrg
89181254a7Smrg /* FIXME: This here is a hack, which will have to be removed when
90181254a7Smrg the array descriptor is reworked. Currently, we don't store the
91181254a7Smrg kind value for the type, but only the size. Because on targets with
92181254a7Smrg __float128, we have sizeof(logn double) == sizeof(__float128),
93181254a7Smrg we cannot discriminate here and have to fall back to the generic
94181254a7Smrg handling (which is suboptimal). */
95181254a7Smrg #if !defined(GFC_REAL_16_IS_FLOAT128)
96181254a7Smrg # if defined(HAVE_GFC_REAL_10)
97181254a7Smrg case GFC_DTYPE_REAL_10:
98181254a7Smrg internal_unpack_r10 ((gfc_array_r10 *) d, (const GFC_REAL_10 *) s);
99181254a7Smrg return;
100181254a7Smrg # endif
101181254a7Smrg
102181254a7Smrg # if defined(HAVE_GFC_REAL_16)
103181254a7Smrg case GFC_DTYPE_REAL_16:
104181254a7Smrg internal_unpack_r16 ((gfc_array_r16 *) d, (const GFC_REAL_16 *) s);
105181254a7Smrg return;
106181254a7Smrg # endif
107181254a7Smrg #endif
108181254a7Smrg
109181254a7Smrg case GFC_DTYPE_COMPLEX_4:
110181254a7Smrg internal_unpack_c4 ((gfc_array_c4 *)d, (const GFC_COMPLEX_4 *)s);
111181254a7Smrg return;
112181254a7Smrg
113181254a7Smrg case GFC_DTYPE_COMPLEX_8:
114181254a7Smrg internal_unpack_c8 ((gfc_array_c8 *)d, (const GFC_COMPLEX_8 *)s);
115181254a7Smrg return;
116181254a7Smrg
117181254a7Smrg /* FIXME: This here is a hack, which will have to be removed when
118181254a7Smrg the array descriptor is reworked. Currently, we don't store the
119181254a7Smrg kind value for the type, but only the size. Because on targets with
120181254a7Smrg __float128, we have sizeof(logn double) == sizeof(__float128),
121181254a7Smrg we cannot discriminate here and have to fall back to the generic
122181254a7Smrg handling (which is suboptimal). */
123181254a7Smrg #if !defined(GFC_REAL_16_IS_FLOAT128)
124181254a7Smrg # if defined(HAVE_GFC_COMPLEX_10)
125181254a7Smrg case GFC_DTYPE_COMPLEX_10:
126181254a7Smrg internal_unpack_c10 ((gfc_array_c10 *) d, (const GFC_COMPLEX_10 *) s);
127181254a7Smrg return;
128181254a7Smrg # endif
129181254a7Smrg
130181254a7Smrg # if defined(HAVE_GFC_COMPLEX_16)
131181254a7Smrg case GFC_DTYPE_COMPLEX_16:
132181254a7Smrg internal_unpack_c16 ((gfc_array_c16 *) d, (const GFC_COMPLEX_16 *) s);
133181254a7Smrg return;
134181254a7Smrg # endif
135181254a7Smrg #endif
136181254a7Smrg
137181254a7Smrg default:
138181254a7Smrg break;
139181254a7Smrg }
140181254a7Smrg
141181254a7Smrg switch (GFC_DESCRIPTOR_SIZE(d))
142181254a7Smrg {
143181254a7Smrg case 1:
144181254a7Smrg internal_unpack_1 ((gfc_array_i1 *) d, (const GFC_INTEGER_1 *) s);
145181254a7Smrg return;
146181254a7Smrg
147181254a7Smrg case 2:
148181254a7Smrg if (GFC_UNALIGNED_2(d->base_addr) || GFC_UNALIGNED_2(s))
149181254a7Smrg break;
150181254a7Smrg else
151181254a7Smrg {
152181254a7Smrg internal_unpack_2 ((gfc_array_i2 *) d, (const GFC_INTEGER_2 *) s);
153181254a7Smrg return;
154181254a7Smrg }
155181254a7Smrg
156181254a7Smrg case 4:
157181254a7Smrg if (GFC_UNALIGNED_4(d->base_addr) || GFC_UNALIGNED_4(s))
158181254a7Smrg break;
159181254a7Smrg else
160181254a7Smrg {
161181254a7Smrg internal_unpack_4 ((gfc_array_i4 *) d, (const GFC_INTEGER_4 *) s);
162181254a7Smrg return;
163181254a7Smrg }
164181254a7Smrg
165181254a7Smrg case 8:
166181254a7Smrg if (GFC_UNALIGNED_8(d->base_addr) || GFC_UNALIGNED_8(s))
167181254a7Smrg break;
168181254a7Smrg else
169181254a7Smrg {
170181254a7Smrg internal_unpack_8 ((gfc_array_i8 *) d, (const GFC_INTEGER_8 *) s);
171181254a7Smrg return;
172181254a7Smrg }
173181254a7Smrg
174181254a7Smrg #ifdef HAVE_GFC_INTEGER_16
175181254a7Smrg case 16:
176181254a7Smrg if (GFC_UNALIGNED_16(d->base_addr) || GFC_UNALIGNED_16(s))
177181254a7Smrg break;
178181254a7Smrg else
179181254a7Smrg {
180181254a7Smrg internal_unpack_16 ((gfc_array_i16 *) d, (const GFC_INTEGER_16 *) s);
181181254a7Smrg return;
182181254a7Smrg }
183181254a7Smrg #endif
184181254a7Smrg default:
185181254a7Smrg break;
186181254a7Smrg }
187181254a7Smrg
188181254a7Smrg size = GFC_DESCRIPTOR_SIZE (d);
189181254a7Smrg
190181254a7Smrg dim = GFC_DESCRIPTOR_RANK (d);
191181254a7Smrg dsize = 1;
192181254a7Smrg for (index_type n = 0; n < dim; n++)
193181254a7Smrg {
194181254a7Smrg count[n] = 0;
195181254a7Smrg stride[n] = GFC_DESCRIPTOR_STRIDE(d,n);
196181254a7Smrg extent[n] = GFC_DESCRIPTOR_EXTENT(d,n);
197181254a7Smrg if (extent[n] <= 0)
198181254a7Smrg return;
199181254a7Smrg
200181254a7Smrg if (dsize == stride[n])
201181254a7Smrg dsize *= extent[n];
202181254a7Smrg else
203181254a7Smrg dsize = 0;
204181254a7Smrg }
205181254a7Smrg
206181254a7Smrg src = s;
207181254a7Smrg
208181254a7Smrg if (dsize != 0)
209181254a7Smrg {
210181254a7Smrg memcpy (dest, src, dsize * size);
211181254a7Smrg return;
212181254a7Smrg }
213181254a7Smrg
214181254a7Smrg stride0 = stride[0] * size;
215181254a7Smrg
216181254a7Smrg while (dest)
217181254a7Smrg {
218181254a7Smrg /* Copy the data. */
219181254a7Smrg memcpy (dest, src, size);
220181254a7Smrg /* Advance to the next element. */
221181254a7Smrg src += size;
222181254a7Smrg dest += stride0;
223181254a7Smrg count[0]++;
224181254a7Smrg /* Advance to the next source element. */
225181254a7Smrg index_type n = 0;
226181254a7Smrg while (count[n] == extent[n])
227181254a7Smrg {
228181254a7Smrg /* When we get to the end of a dimension, reset it and increment
229181254a7Smrg the next dimension. */
230181254a7Smrg count[n] = 0;
231181254a7Smrg /* We could precalculate these products, but this is a less
232181254a7Smrg frequently used path so probably not worth it. */
233181254a7Smrg dest -= stride[n] * extent[n] * size;
234181254a7Smrg n++;
235181254a7Smrg if (n == dim)
236181254a7Smrg {
237181254a7Smrg dest = NULL;
238181254a7Smrg break;
239181254a7Smrg }
240181254a7Smrg else
241181254a7Smrg {
242181254a7Smrg count[n]++;
243181254a7Smrg dest += stride[n] * size;
244181254a7Smrg }
245181254a7Smrg }
246181254a7Smrg }
247181254a7Smrg }
248