1 /* Generic implementation of the PACK intrinsic
2 Copyright (C) 2002, 2004 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 runtime library (libgfor).
7 Libgfor is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 Ligbfor 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 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with libgfor; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
26 #include "libgfortran.h"
28 /* PACK is specified as follows:
30 13.14.80 PACK (ARRAY, MASK, [VECTOR])
32 Description: Pack an array into an array of rank one under the
35 Class: Transformational fucntion.
38 ARRAY may be of any type. It shall not be scalar.
39 MASK shall be of type LOGICAL. It shall be conformable with ARRAY.
40 VECTOR (optional) shall be of the same type and type parameters
41 as ARRAY. VECTOR shall have at least as many elements as
42 there are true elements in MASK. If MASK is a scalar
43 with the value true, VECTOR shall have at least as many
44 elements as there are in ARRAY.
46 Result Characteristics: The result is an array of rank one with the
47 same type and type parameters as ARRAY. If VECTOR is present, the
48 result size is that of VECTOR; otherwise, the result size is the
49 number /t/ of true elements in MASK unless MASK is scalar with the
50 value true, in which case the result size is the size of ARRAY.
52 Result Value: Element /i/ of the result is the element of ARRAY
53 that corresponds to the /i/th true element of MASK, taking elements
54 in array element order, for /i/ = 1, 2, ..., /t/. If VECTOR is
55 present and has size /n/ > /t/, element /i/ of the result has the
56 value VECTOR(/i/), for /i/ = /t/ + 1, ..., /n/.
58 Examples: The nonzero elements of an array M with the value
60 | 9 0 0 | may be "gathered" by the function PACK. The result of
62 PACK (M, MASK = M.NE.0) is [9,7] and the result of PACK (M, M.NE.0,
63 VECTOR = (/ 2,4,6,8,10,12 /)) is [9,7,6,8,10,12].
65 There are two variants of the PACK intrinsic: one, where MASK is
66 array valued, and the other one where MASK is scalar. */
68 extern void __pack (gfc_array_char *, const gfc_array_char *,
69 const gfc_array_l4 *, const gfc_array_char *);
70 export_proto_np(__pack);
73 __pack (gfc_array_char * ret, const gfc_array_char * array,
74 const gfc_array_l4 * mask, const gfc_array_char * vector)
76 /* r.* indicates the return array. */
79 /* s.* indicates the source array. */
80 index_type sstride[GFC_MAX_DIMENSIONS];
83 /* m.* indicates the mask array. */
84 index_type mstride[GFC_MAX_DIMENSIONS];
86 const GFC_LOGICAL_4 *mptr;
88 index_type count[GFC_MAX_DIMENSIONS];
89 index_type extent[GFC_MAX_DIMENSIONS];
95 size = GFC_DESCRIPTOR_SIZE (array);
96 dim = GFC_DESCRIPTOR_RANK (array);
97 for (n = 0; n < dim; n++)
100 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
101 sstride[n] = array->dim[n].stride * size;
102 mstride[n] = mask->dim[n].stride;
112 /* Use the same loop for both logical types. */
113 if (GFC_DESCRIPTOR_SIZE (mask) != 4)
115 if (GFC_DESCRIPTOR_SIZE (mask) != 8)
116 runtime_error ("Funny sized logical array");
117 for (n = 0; n < dim; n++)
120 mptr = GFOR_POINTER_L8_TO_L4 (mptr);
123 if (ret->data == NULL)
125 /* Allocate the memory for the result. */
130 /* The return array will have as many
131 elements as there are in VECTOR. */
132 total = vector->dim[0].ubound + 1 - vector->dim[0].lbound;
136 /* We have to count the true elements in MASK. */
138 /* TODO: We could speed up pack easily in the case of only
139 few .TRUE. entries in MASK, by keeping track of where we
140 would be in the source array during the initial traversal
141 of MASK, and caching the pointers to those elements. Then,
142 supposed the number of elements is small enough, we would
143 only have to traverse the list, and copy those elements
144 into the result array. In the case of datatypes which fit
145 in one of the integer types we could also cache the
146 value instead of a pointer to it.
147 This approach might be bad from the point of view of
148 cache behavior in the case where our cache is not big
149 enough to hold all elements that have to be copied. */
151 const GFC_LOGICAL_4 *m = mptr;
157 /* Test this element. */
161 /* Advance to the next element. */
165 while (count[n] == extent[n])
167 /* When we get to the end of a dimension, reset it
168 and increment the next dimension. */
170 /* We could precalculate this product, but this is a
171 less frequently used path so proabably not worth
173 m -= mstride[n] * extent[n];
177 /* Break out of the loop. */
190 /* Setup the array descriptor. */
191 ret->dim[0].lbound = 0;
192 ret->dim[0].ubound = total - 1;
193 ret->dim[0].stride = 1;
195 ret->data = internal_malloc_size (size * total);
199 /* In this case, nothing remains to be done. */
203 rstride0 = ret->dim[0].stride * size;
206 sstride0 = sstride[0];
207 mstride0 = mstride[0];
212 /* Test this element. */
216 memcpy (rptr, sptr, size);
219 /* Advance to the next element. */
224 while (count[n] == extent[n])
226 /* When we get to the end of a dimension, reset it and increment
227 the next dimension. */
229 /* We could precalculate these products, but this is a less
230 frequently used path so proabably not worth it. */
231 sptr -= sstride[n] * extent[n];
232 mptr -= mstride[n] * extent[n];
236 /* Break out of the loop. */
249 /* Add any remaining elements from VECTOR. */
252 n = vector->dim[0].ubound + 1 - vector->dim[0].lbound;
253 nelem = ((rptr - ret->data) / rstride0);
256 sstride0 = vector->dim[0].stride * size;
260 sptr = vector->data + sstride0 * nelem;
264 memcpy (rptr, sptr, size);
272 extern void __pack_s (gfc_array_char *ret, const gfc_array_char *array,
273 const GFC_LOGICAL_4 *, const gfc_array_char *);
274 export_proto_np(__pack_s);
277 __pack_s (gfc_array_char * ret, const gfc_array_char * array,
278 const GFC_LOGICAL_4 * mask, const gfc_array_char * vector)
280 /* r.* indicates the return array. */
283 /* s.* indicates the source array. */
284 index_type sstride[GFC_MAX_DIMENSIONS];
288 index_type count[GFC_MAX_DIMENSIONS];
289 index_type extent[GFC_MAX_DIMENSIONS];
295 size = GFC_DESCRIPTOR_SIZE (array);
296 dim = GFC_DESCRIPTOR_RANK (array);
297 for (n = 0; n < dim; n++)
300 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
301 sstride[n] = array->dim[n].stride * size;
306 sstride0 = sstride[0];
309 if (ret->data == NULL)
311 /* Allocate the memory for the result. */
316 /* The return array will have as many elements as there are
318 total = vector->dim[0].ubound + 1 - vector->dim[0].lbound;
324 /* The result array will have as many elements as the input
327 for (n = 1; n < dim; n++)
332 /* The result array will be empty. */
333 ret->dim[0].lbound = 0;
334 ret->dim[0].ubound = -1;
335 ret->dim[0].stride = 1;
336 ret->data = internal_malloc_size (0);
343 /* Setup the array descriptor. */
344 ret->dim[0].lbound = 0;
345 ret->dim[0].ubound = total - 1;
346 ret->dim[0].stride = 1;
348 ret->data = internal_malloc_size (size * total);
352 rstride0 = ret->dim[0].stride * size;
357 /* The remaining possibilities are now:
358 If MASK is .TRUE., we have to copy the source array into the
359 result array. We then have to fill it up with elements from VECTOR.
360 If MASK is .FALSE., we have to copy VECTOR into the result
361 array. If VECTOR were not present we would have already returned. */
367 /* Add this element. */
368 memcpy (rptr, sptr, size);
371 /* Advance to the next element. */
375 while (count[n] == extent[n])
377 /* When we get to the end of a dimension, reset it and
378 increment the next dimension. */
380 /* We could precalculate these products, but this is a
381 less frequently used path so proabably not worth it. */
382 sptr -= sstride[n] * extent[n];
386 /* Break out of the loop. */
399 /* Add any remaining elements from VECTOR. */
402 n = vector->dim[0].ubound + 1 - vector->dim[0].lbound;
403 nelem = ((rptr - ret->data) / rstride0);
406 sstride0 = vector->dim[0].stride * size;
410 sptr = vector->data + sstride0 * nelem;
414 memcpy (rptr, sptr, size);