annotate libgfortran/generated/pack_i8.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 /* Specific implementation of the PACK intrinsic
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2002-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 Ligbfortran 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
kono
parents:
diff changeset
30 #if defined (HAVE_GFC_INTEGER_8)
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 /* PACK is specified as follows:
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 13.14.80 PACK (ARRAY, MASK, [VECTOR])
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 Description: Pack an array into an array of rank one under the
kono
parents:
diff changeset
37 control of a mask.
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 Class: Transformational function.
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 Arguments:
kono
parents:
diff changeset
42 ARRAY may be of any type. It shall not be scalar.
kono
parents:
diff changeset
43 MASK shall be of type LOGICAL. It shall be conformable with ARRAY.
kono
parents:
diff changeset
44 VECTOR (optional) shall be of the same type and type parameters
kono
parents:
diff changeset
45 as ARRAY. VECTOR shall have at least as many elements as
kono
parents:
diff changeset
46 there are true elements in MASK. If MASK is a scalar
kono
parents:
diff changeset
47 with the value true, VECTOR shall have at least as many
kono
parents:
diff changeset
48 elements as there are in ARRAY.
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 Result Characteristics: The result is an array of rank one with the
kono
parents:
diff changeset
51 same type and type parameters as ARRAY. If VECTOR is present, the
kono
parents:
diff changeset
52 result size is that of VECTOR; otherwise, the result size is the
kono
parents:
diff changeset
53 number /t/ of true elements in MASK unless MASK is scalar with the
kono
parents:
diff changeset
54 value true, in which case the result size is the size of ARRAY.
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 Result Value: Element /i/ of the result is the element of ARRAY
kono
parents:
diff changeset
57 that corresponds to the /i/th true element of MASK, taking elements
kono
parents:
diff changeset
58 in array element order, for /i/ = 1, 2, ..., /t/. If VECTOR is
kono
parents:
diff changeset
59 present and has size /n/ > /t/, element /i/ of the result has the
kono
parents:
diff changeset
60 value VECTOR(/i/), for /i/ = /t/ + 1, ..., /n/.
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 Examples: The nonzero elements of an array M with the value
kono
parents:
diff changeset
63 | 0 0 0 |
kono
parents:
diff changeset
64 | 9 0 0 | may be "gathered" by the function PACK. The result of
kono
parents:
diff changeset
65 | 0 0 7 |
kono
parents:
diff changeset
66 PACK (M, MASK = M.NE.0) is [9,7] and the result of PACK (M, M.NE.0,
kono
parents:
diff changeset
67 VECTOR = (/ 2,4,6,8,10,12 /)) is [9,7,6,8,10,12].
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 There are two variants of the PACK intrinsic: one, where MASK is
kono
parents:
diff changeset
70 array valued, and the other one where MASK is scalar. */
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 void
kono
parents:
diff changeset
73 pack_i8 (gfc_array_i8 *ret, const gfc_array_i8 *array,
kono
parents:
diff changeset
74 const gfc_array_l1 *mask, const gfc_array_i8 *vector)
kono
parents:
diff changeset
75 {
kono
parents:
diff changeset
76 /* r.* indicates the return array. */
kono
parents:
diff changeset
77 index_type rstride0;
kono
parents:
diff changeset
78 GFC_INTEGER_8 * restrict rptr;
kono
parents:
diff changeset
79 /* s.* indicates the source array. */
kono
parents:
diff changeset
80 index_type sstride[GFC_MAX_DIMENSIONS];
kono
parents:
diff changeset
81 index_type sstride0;
kono
parents:
diff changeset
82 const GFC_INTEGER_8 *sptr;
kono
parents:
diff changeset
83 /* m.* indicates the mask array. */
kono
parents:
diff changeset
84 index_type mstride[GFC_MAX_DIMENSIONS];
kono
parents:
diff changeset
85 index_type mstride0;
kono
parents:
diff changeset
86 const GFC_LOGICAL_1 *mptr;
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 index_type count[GFC_MAX_DIMENSIONS];
kono
parents:
diff changeset
89 index_type extent[GFC_MAX_DIMENSIONS];
kono
parents:
diff changeset
90 int zero_sized;
kono
parents:
diff changeset
91 index_type n;
kono
parents:
diff changeset
92 index_type dim;
kono
parents:
diff changeset
93 index_type nelem;
kono
parents:
diff changeset
94 index_type total;
kono
parents:
diff changeset
95 int mask_kind;
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 dim = GFC_DESCRIPTOR_RANK (array);
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 mptr = mask->base_addr;
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 /* Use the same loop for all logical types, by using GFC_LOGICAL_1
kono
parents:
diff changeset
102 and using shifting to address size and endian issues. */
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
kono
parents:
diff changeset
107 #ifdef HAVE_GFC_LOGICAL_16
kono
parents:
diff changeset
108 || mask_kind == 16
kono
parents:
diff changeset
109 #endif
kono
parents:
diff changeset
110 )
kono
parents:
diff changeset
111 {
kono
parents:
diff changeset
112 /* Do not convert a NULL pointer as we use test for NULL below. */
kono
parents:
diff changeset
113 if (mptr)
kono
parents:
diff changeset
114 mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
kono
parents:
diff changeset
115 }
kono
parents:
diff changeset
116 else
kono
parents:
diff changeset
117 runtime_error ("Funny sized logical array");
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 zero_sized = 0;
kono
parents:
diff changeset
120 for (n = 0; n < dim; n++)
kono
parents:
diff changeset
121 {
kono
parents:
diff changeset
122 count[n] = 0;
kono
parents:
diff changeset
123 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
kono
parents:
diff changeset
124 if (extent[n] <= 0)
kono
parents:
diff changeset
125 zero_sized = 1;
kono
parents:
diff changeset
126 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
kono
parents:
diff changeset
127 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
kono
parents:
diff changeset
128 }
kono
parents:
diff changeset
129 if (sstride[0] == 0)
kono
parents:
diff changeset
130 sstride[0] = 1;
kono
parents:
diff changeset
131 if (mstride[0] == 0)
kono
parents:
diff changeset
132 mstride[0] = mask_kind;
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 if (zero_sized)
kono
parents:
diff changeset
135 sptr = NULL;
kono
parents:
diff changeset
136 else
kono
parents:
diff changeset
137 sptr = array->base_addr;
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 if (ret->base_addr == NULL || unlikely (compile_options.bounds_check))
kono
parents:
diff changeset
140 {
kono
parents:
diff changeset
141 /* Count the elements, either for allocating memory or
kono
parents:
diff changeset
142 for bounds checking. */
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 if (vector != NULL)
kono
parents:
diff changeset
145 {
kono
parents:
diff changeset
146 /* The return array will have as many
kono
parents:
diff changeset
147 elements as there are in VECTOR. */
kono
parents:
diff changeset
148 total = GFC_DESCRIPTOR_EXTENT(vector,0);
kono
parents:
diff changeset
149 if (total < 0)
kono
parents:
diff changeset
150 {
kono
parents:
diff changeset
151 total = 0;
kono
parents:
diff changeset
152 vector = NULL;
kono
parents:
diff changeset
153 }
kono
parents:
diff changeset
154 }
kono
parents:
diff changeset
155 else
kono
parents:
diff changeset
156 {
kono
parents:
diff changeset
157 /* We have to count the true elements in MASK. */
kono
parents:
diff changeset
158 total = count_0 (mask);
kono
parents:
diff changeset
159 }
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 if (ret->base_addr == NULL)
kono
parents:
diff changeset
162 {
kono
parents:
diff changeset
163 /* Setup the array descriptor. */
kono
parents:
diff changeset
164 GFC_DIMENSION_SET(ret->dim[0], 0, total-1, 1);
kono
parents:
diff changeset
165
kono
parents:
diff changeset
166 ret->offset = 0;
kono
parents:
diff changeset
167
kono
parents:
diff changeset
168 /* xmallocarray allocates a single byte for zero size. */
kono
parents:
diff changeset
169 ret->base_addr = xmallocarray (total, sizeof (GFC_INTEGER_8));
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 if (total == 0)
kono
parents:
diff changeset
172 return;
kono
parents:
diff changeset
173 }
kono
parents:
diff changeset
174 else
kono
parents:
diff changeset
175 {
kono
parents:
diff changeset
176 /* We come here because of range checking. */
kono
parents:
diff changeset
177 index_type ret_extent;
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 ret_extent = GFC_DESCRIPTOR_EXTENT(ret,0);
kono
parents:
diff changeset
180 if (total != ret_extent)
kono
parents:
diff changeset
181 runtime_error ("Incorrect extent in return value of PACK intrinsic;"
kono
parents:
diff changeset
182 " is %ld, should be %ld", (long int) total,
kono
parents:
diff changeset
183 (long int) ret_extent);
kono
parents:
diff changeset
184 }
kono
parents:
diff changeset
185 }
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 rstride0 = GFC_DESCRIPTOR_STRIDE(ret,0);
kono
parents:
diff changeset
188 if (rstride0 == 0)
kono
parents:
diff changeset
189 rstride0 = 1;
kono
parents:
diff changeset
190 sstride0 = sstride[0];
kono
parents:
diff changeset
191 mstride0 = mstride[0];
kono
parents:
diff changeset
192 rptr = ret->base_addr;
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 while (sptr && mptr)
kono
parents:
diff changeset
195 {
kono
parents:
diff changeset
196 /* Test this element. */
kono
parents:
diff changeset
197 if (*mptr)
kono
parents:
diff changeset
198 {
kono
parents:
diff changeset
199 /* Add it. */
kono
parents:
diff changeset
200 *rptr = *sptr;
kono
parents:
diff changeset
201 rptr += rstride0;
kono
parents:
diff changeset
202 }
kono
parents:
diff changeset
203 /* Advance to the next element. */
kono
parents:
diff changeset
204 sptr += sstride0;
kono
parents:
diff changeset
205 mptr += mstride0;
kono
parents:
diff changeset
206 count[0]++;
kono
parents:
diff changeset
207 n = 0;
kono
parents:
diff changeset
208 while (count[n] == extent[n])
kono
parents:
diff changeset
209 {
kono
parents:
diff changeset
210 /* When we get to the end of a dimension, reset it and increment
kono
parents:
diff changeset
211 the next dimension. */
kono
parents:
diff changeset
212 count[n] = 0;
kono
parents:
diff changeset
213 /* We could precalculate these products, but this is a less
kono
parents:
diff changeset
214 frequently used path so probably not worth it. */
kono
parents:
diff changeset
215 sptr -= sstride[n] * extent[n];
kono
parents:
diff changeset
216 mptr -= mstride[n] * extent[n];
kono
parents:
diff changeset
217 n++;
kono
parents:
diff changeset
218 if (n >= dim)
kono
parents:
diff changeset
219 {
kono
parents:
diff changeset
220 /* Break out of the loop. */
kono
parents:
diff changeset
221 sptr = NULL;
kono
parents:
diff changeset
222 break;
kono
parents:
diff changeset
223 }
kono
parents:
diff changeset
224 else
kono
parents:
diff changeset
225 {
kono
parents:
diff changeset
226 count[n]++;
kono
parents:
diff changeset
227 sptr += sstride[n];
kono
parents:
diff changeset
228 mptr += mstride[n];
kono
parents:
diff changeset
229 }
kono
parents:
diff changeset
230 }
kono
parents:
diff changeset
231 }
kono
parents:
diff changeset
232
kono
parents:
diff changeset
233 /* Add any remaining elements from VECTOR. */
kono
parents:
diff changeset
234 if (vector)
kono
parents:
diff changeset
235 {
kono
parents:
diff changeset
236 n = GFC_DESCRIPTOR_EXTENT(vector,0);
kono
parents:
diff changeset
237 nelem = ((rptr - ret->base_addr) / rstride0);
kono
parents:
diff changeset
238 if (n > nelem)
kono
parents:
diff changeset
239 {
kono
parents:
diff changeset
240 sstride0 = GFC_DESCRIPTOR_STRIDE(vector,0);
kono
parents:
diff changeset
241 if (sstride0 == 0)
kono
parents:
diff changeset
242 sstride0 = 1;
kono
parents:
diff changeset
243
kono
parents:
diff changeset
244 sptr = vector->base_addr + sstride0 * nelem;
kono
parents:
diff changeset
245 n -= nelem;
kono
parents:
diff changeset
246 while (n--)
kono
parents:
diff changeset
247 {
kono
parents:
diff changeset
248 *rptr = *sptr;
kono
parents:
diff changeset
249 rptr += rstride0;
kono
parents:
diff changeset
250 sptr += sstride0;
kono
parents:
diff changeset
251 }
kono
parents:
diff changeset
252 }
kono
parents:
diff changeset
253 }
kono
parents:
diff changeset
254 }
kono
parents:
diff changeset
255
kono
parents:
diff changeset
256 #endif
kono
parents:
diff changeset
257