annotate libgfortran/runtime/in_pack_generic.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Generic helper function for repacking arrays.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2003-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Contributed by Paul Brook <paul@nowt.org>
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 This file is part of the GNU Fortran runtime library (libgfortran).
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 Libgfortran is free software; you can redistribute it and/or
kono
parents:
diff changeset
8 modify it under the terms of the GNU General Public
kono
parents:
diff changeset
9 License as published by the Free Software Foundation; either
kono
parents:
diff changeset
10 version 3 of the License, or (at your option) any later version.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 Libgfortran is distributed in the hope that it will be useful,
kono
parents:
diff changeset
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
kono
parents:
diff changeset
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
kono
parents:
diff changeset
15 GNU General Public License for more details.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 Under Section 7 of GPL version 3, you are granted additional
kono
parents:
diff changeset
18 permissions described in the GCC Runtime Library Exception, version
kono
parents:
diff changeset
19 3.1, as published by the Free Software Foundation.
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 You should have received a copy of the GNU General Public License and
kono
parents:
diff changeset
22 a copy of the GCC Runtime Library Exception along with this program;
kono
parents:
diff changeset
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
kono
parents:
diff changeset
24 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 #include "libgfortran.h"
kono
parents:
diff changeset
27 #include <string.h>
kono
parents:
diff changeset
28
kono
parents:
diff changeset
29 extern void *internal_pack (gfc_array_char *);
kono
parents:
diff changeset
30 export_proto(internal_pack);
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 void *
kono
parents:
diff changeset
33 internal_pack (gfc_array_char * source)
kono
parents:
diff changeset
34 {
kono
parents:
diff changeset
35 index_type count[GFC_MAX_DIMENSIONS];
kono
parents:
diff changeset
36 index_type extent[GFC_MAX_DIMENSIONS];
kono
parents:
diff changeset
37 index_type stride[GFC_MAX_DIMENSIONS];
kono
parents:
diff changeset
38 index_type stride0;
kono
parents:
diff changeset
39 index_type dim;
kono
parents:
diff changeset
40 index_type ssize;
kono
parents:
diff changeset
41 const char *src;
kono
parents:
diff changeset
42 char *dest;
kono
parents:
diff changeset
43 void *destptr;
kono
parents:
diff changeset
44 int packed;
kono
parents:
diff changeset
45 index_type size;
kono
parents:
diff changeset
46 index_type type_size;
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 if (source->base_addr == NULL)
kono
parents:
diff changeset
49 return NULL;
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 type_size = GFC_DTYPE_TYPE_SIZE(source);
kono
parents:
diff changeset
52 size = GFC_DESCRIPTOR_SIZE (source);
kono
parents:
diff changeset
53 switch (type_size)
kono
parents:
diff changeset
54 {
kono
parents:
diff changeset
55 case GFC_DTYPE_INTEGER_1:
kono
parents:
diff changeset
56 case GFC_DTYPE_LOGICAL_1:
kono
parents:
diff changeset
57 return internal_pack_1 ((gfc_array_i1 *) source);
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 case GFC_DTYPE_INTEGER_2:
kono
parents:
diff changeset
60 case GFC_DTYPE_LOGICAL_2:
kono
parents:
diff changeset
61 return internal_pack_2 ((gfc_array_i2 *) source);
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 case GFC_DTYPE_INTEGER_4:
kono
parents:
diff changeset
64 case GFC_DTYPE_LOGICAL_4:
kono
parents:
diff changeset
65 return internal_pack_4 ((gfc_array_i4 *) source);
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 case GFC_DTYPE_INTEGER_8:
kono
parents:
diff changeset
68 case GFC_DTYPE_LOGICAL_8:
kono
parents:
diff changeset
69 return internal_pack_8 ((gfc_array_i8 *) source);
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 #if defined(HAVE_GFC_INTEGER_16)
kono
parents:
diff changeset
72 case GFC_DTYPE_INTEGER_16:
kono
parents:
diff changeset
73 case GFC_DTYPE_LOGICAL_16:
kono
parents:
diff changeset
74 return internal_pack_16 ((gfc_array_i16 *) source);
kono
parents:
diff changeset
75 #endif
kono
parents:
diff changeset
76 case GFC_DTYPE_REAL_4:
kono
parents:
diff changeset
77 return internal_pack_r4 ((gfc_array_r4 *) source);
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 case GFC_DTYPE_REAL_8:
kono
parents:
diff changeset
80 return internal_pack_r8 ((gfc_array_r8 *) source);
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 /* FIXME: This here is a hack, which will have to be removed when
kono
parents:
diff changeset
83 the array descriptor is reworked. Currently, we don't store the
kono
parents:
diff changeset
84 kind value for the type, but only the size. Because on targets with
kono
parents:
diff changeset
85 __float128, we have sizeof(logn double) == sizeof(__float128),
kono
parents:
diff changeset
86 we cannot discriminate here and have to fall back to the generic
kono
parents:
diff changeset
87 handling (which is suboptimal). */
kono
parents:
diff changeset
88 #if !defined(GFC_REAL_16_IS_FLOAT128)
kono
parents:
diff changeset
89 # if defined (HAVE_GFC_REAL_10)
kono
parents:
diff changeset
90 case GFC_DTYPE_REAL_10:
kono
parents:
diff changeset
91 return internal_pack_r10 ((gfc_array_r10 *) source);
kono
parents:
diff changeset
92 # endif
kono
parents:
diff changeset
93
kono
parents:
diff changeset
94 # if defined (HAVE_GFC_REAL_16)
kono
parents:
diff changeset
95 case GFC_DTYPE_REAL_16:
kono
parents:
diff changeset
96 return internal_pack_r16 ((gfc_array_r16 *) source);
kono
parents:
diff changeset
97 # endif
kono
parents:
diff changeset
98 #endif
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 case GFC_DTYPE_COMPLEX_4:
kono
parents:
diff changeset
101 return internal_pack_c4 ((gfc_array_c4 *) source);
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 case GFC_DTYPE_COMPLEX_8:
kono
parents:
diff changeset
104 return internal_pack_c8 ((gfc_array_c8 *) source);
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 /* FIXME: This here is a hack, which will have to be removed when
kono
parents:
diff changeset
107 the array descriptor is reworked. Currently, we don't store the
kono
parents:
diff changeset
108 kind value for the type, but only the size. Because on targets with
kono
parents:
diff changeset
109 __float128, we have sizeof(logn double) == sizeof(__float128),
kono
parents:
diff changeset
110 we cannot discriminate here and have to fall back to the generic
kono
parents:
diff changeset
111 handling (which is suboptimal). */
kono
parents:
diff changeset
112 #if !defined(GFC_REAL_16_IS_FLOAT128)
kono
parents:
diff changeset
113 # if defined (HAVE_GFC_COMPLEX_10)
kono
parents:
diff changeset
114 case GFC_DTYPE_COMPLEX_10:
kono
parents:
diff changeset
115 return internal_pack_c10 ((gfc_array_c10 *) source);
kono
parents:
diff changeset
116 # endif
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 # if defined (HAVE_GFC_COMPLEX_16)
kono
parents:
diff changeset
119 case GFC_DTYPE_COMPLEX_16:
kono
parents:
diff changeset
120 return internal_pack_c16 ((gfc_array_c16 *) source);
kono
parents:
diff changeset
121 # endif
kono
parents:
diff changeset
122 #endif
kono
parents:
diff changeset
123
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
124 default:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
125 break;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
126 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
127
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
128 switch(GFC_DESCRIPTOR_SIZE (source))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
129 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
130 case 1:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
131 return internal_pack_1 ((gfc_array_i1 *) source);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
132
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
133 case 2:
111
kono
parents:
diff changeset
134 if (GFC_UNALIGNED_2(source->base_addr))
kono
parents:
diff changeset
135 break;
kono
parents:
diff changeset
136 else
kono
parents:
diff changeset
137 return internal_pack_2 ((gfc_array_i2 *) source);
kono
parents:
diff changeset
138
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
139 case 4:
111
kono
parents:
diff changeset
140 if (GFC_UNALIGNED_4(source->base_addr))
kono
parents:
diff changeset
141 break;
kono
parents:
diff changeset
142 else
kono
parents:
diff changeset
143 return internal_pack_4 ((gfc_array_i4 *) source);
kono
parents:
diff changeset
144
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
145 case 8:
111
kono
parents:
diff changeset
146 if (GFC_UNALIGNED_8(source->base_addr))
kono
parents:
diff changeset
147 break;
kono
parents:
diff changeset
148 else
kono
parents:
diff changeset
149 return internal_pack_8 ((gfc_array_i8 *) source);
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 #ifdef HAVE_GFC_INTEGER_16
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
152 case 16:
111
kono
parents:
diff changeset
153 if (GFC_UNALIGNED_16(source->base_addr))
kono
parents:
diff changeset
154 break;
kono
parents:
diff changeset
155 else
kono
parents:
diff changeset
156 return internal_pack_16 ((gfc_array_i16 *) source);
kono
parents:
diff changeset
157 #endif
kono
parents:
diff changeset
158 default:
kono
parents:
diff changeset
159 break;
kono
parents:
diff changeset
160 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
161
111
kono
parents:
diff changeset
162 dim = GFC_DESCRIPTOR_RANK (source);
kono
parents:
diff changeset
163 ssize = 1;
kono
parents:
diff changeset
164 packed = 1;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
165 for (index_type n = 0; n < dim; n++)
111
kono
parents:
diff changeset
166 {
kono
parents:
diff changeset
167 count[n] = 0;
kono
parents:
diff changeset
168 stride[n] = GFC_DESCRIPTOR_STRIDE(source,n);
kono
parents:
diff changeset
169 extent[n] = GFC_DESCRIPTOR_EXTENT(source,n);
kono
parents:
diff changeset
170 if (extent[n] <= 0)
kono
parents:
diff changeset
171 {
kono
parents:
diff changeset
172 /* Do nothing. */
kono
parents:
diff changeset
173 packed = 1;
kono
parents:
diff changeset
174 break;
kono
parents:
diff changeset
175 }
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 if (ssize != stride[n])
kono
parents:
diff changeset
178 packed = 0;
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 ssize *= extent[n];
kono
parents:
diff changeset
181 }
kono
parents:
diff changeset
182
kono
parents:
diff changeset
183 if (packed)
kono
parents:
diff changeset
184 return source->base_addr;
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 /* Allocate storage for the destination. */
kono
parents:
diff changeset
187 destptr = xmallocarray (ssize, size);
kono
parents:
diff changeset
188 dest = (char *)destptr;
kono
parents:
diff changeset
189 src = source->base_addr;
kono
parents:
diff changeset
190 stride0 = stride[0] * size;
kono
parents:
diff changeset
191
kono
parents:
diff changeset
192 while (src)
kono
parents:
diff changeset
193 {
kono
parents:
diff changeset
194 /* Copy the data. */
kono
parents:
diff changeset
195 memcpy(dest, src, size);
kono
parents:
diff changeset
196 /* Advance to the next element. */
kono
parents:
diff changeset
197 dest += size;
kono
parents:
diff changeset
198 src += stride0;
kono
parents:
diff changeset
199 count[0]++;
kono
parents:
diff changeset
200 /* Advance to the next source element. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
201 index_type n = 0;
111
kono
parents:
diff changeset
202 while (count[n] == extent[n])
kono
parents:
diff changeset
203 {
kono
parents:
diff changeset
204 /* When we get to the end of a dimension, reset it and increment
kono
parents:
diff changeset
205 the next dimension. */
kono
parents:
diff changeset
206 count[n] = 0;
kono
parents:
diff changeset
207 /* We could precalculate these products, but this is a less
kono
parents:
diff changeset
208 frequently used path so probably not worth it. */
kono
parents:
diff changeset
209 src -= stride[n] * extent[n] * size;
kono
parents:
diff changeset
210 n++;
kono
parents:
diff changeset
211 if (n == dim)
kono
parents:
diff changeset
212 {
kono
parents:
diff changeset
213 src = NULL;
kono
parents:
diff changeset
214 break;
kono
parents:
diff changeset
215 }
kono
parents:
diff changeset
216 else
kono
parents:
diff changeset
217 {
kono
parents:
diff changeset
218 count[n]++;
kono
parents:
diff changeset
219 src += stride[n] * size;
kono
parents:
diff changeset
220 }
kono
parents:
diff changeset
221 }
kono
parents:
diff changeset
222 }
kono
parents:
diff changeset
223 return destptr;
kono
parents:
diff changeset
224 }