annotate libgfortran/m4/pack.m4 @ 138:fc828634a951

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