]> git.ipfire.org Git - thirdparty/gcc.git/blob - libgfortran/m4/pack.m4
libgfortran.h (descriptor_dimension, [...]): Rename _lbound to lower_bound and data...
[thirdparty/gcc.git] / libgfortran / m4 / pack.m4
1 `/* Specific implementation of the PACK intrinsic
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2012
3 Free Software Foundation, Inc.
4 Contributed by Paul Brook <paul@nowt.org>
5
6 This file is part of the GNU Fortran runtime library (libgfortran).
7
8 Libgfortran is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public
10 License as published by the Free Software Foundation; either
11 version 3 of the License, or (at your option) any later version.
12
13 Ligbfortran is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
21
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
26
27 #include "libgfortran.h"
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <string.h>'
31
32 include(iparm.m4)dnl
33
34 `#if defined (HAVE_'rtype_name`)
35
36 /* PACK is specified as follows:
37
38 13.14.80 PACK (ARRAY, MASK, [VECTOR])
39
40 Description: Pack an array into an array of rank one under the
41 control of a mask.
42
43 Class: Transformational function.
44
45 Arguments:
46 ARRAY may be of any type. It shall not be scalar.
47 MASK shall be of type LOGICAL. It shall be conformable with ARRAY.
48 VECTOR (optional) shall be of the same type and type parameters
49 as ARRAY. VECTOR shall have at least as many elements as
50 there are true elements in MASK. If MASK is a scalar
51 with the value true, VECTOR shall have at least as many
52 elements as there are in ARRAY.
53
54 Result Characteristics: The result is an array of rank one with the
55 same type and type parameters as ARRAY. If VECTOR is present, the
56 result size is that of VECTOR; otherwise, the result size is the
57 number /t/ of true elements in MASK unless MASK is scalar with the
58 value true, in which case the result size is the size of ARRAY.
59
60 Result Value: Element /i/ of the result is the element of ARRAY
61 that corresponds to the /i/th true element of MASK, taking elements
62 in array element order, for /i/ = 1, 2, ..., /t/. If VECTOR is
63 present and has size /n/ > /t/, element /i/ of the result has the
64 value VECTOR(/i/), for /i/ = /t/ + 1, ..., /n/.
65
66 Examples: The nonzero elements of an array M with the value
67 | 0 0 0 |
68 | 9 0 0 | may be "gathered" by the function PACK. The result of
69 | 0 0 7 |
70 PACK (M, MASK = M.NE.0) is [9,7] and the result of PACK (M, M.NE.0,
71 VECTOR = (/ 2,4,6,8,10,12 /)) is [9,7,6,8,10,12].
72
73 There are two variants of the PACK intrinsic: one, where MASK is
74 array valued, and the other one where MASK is scalar. */
75
76 void
77 pack_'rtype_code` ('rtype` *ret, const 'rtype` *array,
78 const gfc_array_l1 *mask, const 'rtype` *vector)
79 {
80 /* r.* indicates the return array. */
81 index_type rstride0;
82 'rtype_name` * restrict rptr;
83 /* s.* indicates the source array. */
84 index_type sstride[GFC_MAX_DIMENSIONS];
85 index_type sstride0;
86 const 'rtype_name` *sptr;
87 /* m.* indicates the mask array. */
88 index_type mstride[GFC_MAX_DIMENSIONS];
89 index_type mstride0;
90 const GFC_LOGICAL_1 *mptr;
91
92 index_type count[GFC_MAX_DIMENSIONS];
93 index_type extent[GFC_MAX_DIMENSIONS];
94 int zero_sized;
95 index_type n;
96 index_type dim;
97 index_type nelem;
98 index_type total;
99 int mask_kind;
100
101 dim = GFC_DESCRIPTOR_RANK (array);
102
103 mptr = mask->base_addr;
104
105 /* Use the same loop for all logical types, by using GFC_LOGICAL_1
106 and using shifting to address size and endian issues. */
107
108 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
109
110 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
111 #ifdef HAVE_GFC_LOGICAL_16
112 || mask_kind == 16
113 #endif
114 )
115 {
116 /* Do not convert a NULL pointer as we use test for NULL below. */
117 if (mptr)
118 mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
119 }
120 else
121 runtime_error ("Funny sized logical array");
122
123 zero_sized = 0;
124 for (n = 0; n < dim; n++)
125 {
126 count[n] = 0;
127 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
128 if (extent[n] <= 0)
129 zero_sized = 1;
130 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
131 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
132 }
133 if (sstride[0] == 0)
134 sstride[0] = 1;
135 if (mstride[0] == 0)
136 mstride[0] = mask_kind;
137
138 if (zero_sized)
139 sptr = NULL;
140 else
141 sptr = array->base_addr;
142
143 if (ret->base_addr == NULL || unlikely (compile_options.bounds_check))
144 {
145 /* Count the elements, either for allocating memory or
146 for bounds checking. */
147
148 if (vector != NULL)
149 {
150 /* The return array will have as many
151 elements as there are in VECTOR. */
152 total = GFC_DESCRIPTOR_EXTENT(vector,0);
153 if (total < 0)
154 {
155 total = 0;
156 vector = NULL;
157 }
158 }
159 else
160 {
161 /* We have to count the true elements in MASK. */
162 total = count_0 (mask);
163 }
164
165 if (ret->base_addr == NULL)
166 {
167 /* Setup the array descriptor. */
168 GFC_DIMENSION_SET(ret->dim[0], 0, total-1, 1);
169
170 ret->offset = 0;
171
172 /* internal_malloc_size allocates a single byte for zero size. */
173 ret->base_addr = internal_malloc_size (sizeof ('rtype_name`) * total);
174
175 if (total == 0)
176 return;
177 }
178 else
179 {
180 /* We come here because of range checking. */
181 index_type ret_extent;
182
183 ret_extent = GFC_DESCRIPTOR_EXTENT(ret,0);
184 if (total != ret_extent)
185 runtime_error ("Incorrect extent in return value of PACK intrinsic;"
186 " is %ld, should be %ld", (long int) total,
187 (long int) ret_extent);
188 }
189 }
190
191 rstride0 = GFC_DESCRIPTOR_STRIDE(ret,0);
192 if (rstride0 == 0)
193 rstride0 = 1;
194 sstride0 = sstride[0];
195 mstride0 = mstride[0];
196 rptr = ret->base_addr;
197
198 while (sptr && mptr)
199 {
200 /* Test this element. */
201 if (*mptr)
202 {
203 /* Add it. */
204 *rptr = *sptr;
205 rptr += rstride0;
206 }
207 /* Advance to the next element. */
208 sptr += sstride0;
209 mptr += mstride0;
210 count[0]++;
211 n = 0;
212 while (count[n] == extent[n])
213 {
214 /* When we get to the end of a dimension, reset it and increment
215 the next dimension. */
216 count[n] = 0;
217 /* We could precalculate these products, but this is a less
218 frequently used path so probably not worth it. */
219 sptr -= sstride[n] * extent[n];
220 mptr -= mstride[n] * extent[n];
221 n++;
222 if (n >= dim)
223 {
224 /* Break out of the loop. */
225 sptr = NULL;
226 break;
227 }
228 else
229 {
230 count[n]++;
231 sptr += sstride[n];
232 mptr += mstride[n];
233 }
234 }
235 }
236
237 /* Add any remaining elements from VECTOR. */
238 if (vector)
239 {
240 n = GFC_DESCRIPTOR_EXTENT(vector,0);
241 nelem = ((rptr - ret->base_addr) / rstride0);
242 if (n > nelem)
243 {
244 sstride0 = GFC_DESCRIPTOR_STRIDE(vector,0);
245 if (sstride0 == 0)
246 sstride0 = 1;
247
248 sptr = vector->base_addr + sstride0 * nelem;
249 n -= nelem;
250 while (n--)
251 {
252 *rptr = *sptr;
253 rptr += rstride0;
254 sptr += sstride0;
255 }
256 }
257 }
258 }
259
260 #endif
261 '