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