missing Changelog
[official-gcc.git] / libgfortran / generated / pack_i1.c
blob05b8ae5e35c971873b1b2706b5f47db0b3f29cd9
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>
6 This file is part of the GNU Fortran runtime library (libgfortran).
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.
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.
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.
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/>. */
27 #include "libgfortran.h"
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <string.h>
33 #if defined (HAVE_GFC_INTEGER_1)
35 /* PACK is specified as follows:
37 13.14.80 PACK (ARRAY, MASK, [VECTOR])
39 Description: Pack an array into an array of rank one under the
40 control of a mask.
42 Class: Transformational function.
44 Arguments:
45 ARRAY may be of any type. It shall not be scalar.
46 MASK shall be of type LOGICAL. It shall be conformable with ARRAY.
47 VECTOR (optional) shall be of the same type and type parameters
48 as ARRAY. VECTOR shall have at least as many elements as
49 there are true elements in MASK. If MASK is a scalar
50 with the value true, VECTOR shall have at least as many
51 elements as there are in ARRAY.
53 Result Characteristics: The result is an array of rank one with the
54 same type and type parameters as ARRAY. If VECTOR is present, the
55 result size is that of VECTOR; otherwise, the result size is the
56 number /t/ of true elements in MASK unless MASK is scalar with the
57 value true, in which case the result size is the size of ARRAY.
59 Result Value: Element /i/ of the result is the element of ARRAY
60 that corresponds to the /i/th true element of MASK, taking elements
61 in array element order, for /i/ = 1, 2, ..., /t/. If VECTOR is
62 present and has size /n/ > /t/, element /i/ of the result has the
63 value VECTOR(/i/), for /i/ = /t/ + 1, ..., /n/.
65 Examples: The nonzero elements of an array M with the value
66 | 0 0 0 |
67 | 9 0 0 | may be "gathered" by the function PACK. The result of
68 | 0 0 7 |
69 PACK (M, MASK = M.NE.0) is [9,7] and the result of PACK (M, M.NE.0,
70 VECTOR = (/ 2,4,6,8,10,12 /)) is [9,7,6,8,10,12].
72 There are two variants of the PACK intrinsic: one, where MASK is
73 array valued, and the other one where MASK is scalar. */
75 void
76 pack_i1 (gfc_array_i1 *ret, const gfc_array_i1 *array,
77 const gfc_array_l1 *mask, const gfc_array_i1 *vector)
79 /* r.* indicates the return array. */
80 index_type rstride0;
81 GFC_INTEGER_1 * restrict rptr;
82 /* s.* indicates the source array. */
83 index_type sstride[GFC_MAX_DIMENSIONS];
84 index_type sstride0;
85 const GFC_INTEGER_1 *sptr;
86 /* m.* indicates the mask array. */
87 index_type mstride[GFC_MAX_DIMENSIONS];
88 index_type mstride0;
89 const GFC_LOGICAL_1 *mptr;
91 index_type count[GFC_MAX_DIMENSIONS];
92 index_type extent[GFC_MAX_DIMENSIONS];
93 int zero_sized;
94 index_type n;
95 index_type dim;
96 index_type nelem;
97 index_type total;
98 int mask_kind;
100 dim = GFC_DESCRIPTOR_RANK (array);
102 mptr = mask->base_addr;
104 /* Use the same loop for all logical types, by using GFC_LOGICAL_1
105 and using shifting to address size and endian issues. */
107 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
109 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
110 #ifdef HAVE_GFC_LOGICAL_16
111 || mask_kind == 16
112 #endif
115 /* Do not convert a NULL pointer as we use test for NULL below. */
116 if (mptr)
117 mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
119 else
120 runtime_error ("Funny sized logical array");
122 zero_sized = 0;
123 for (n = 0; n < dim; n++)
125 count[n] = 0;
126 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
127 if (extent[n] <= 0)
128 zero_sized = 1;
129 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
130 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
132 if (sstride[0] == 0)
133 sstride[0] = 1;
134 if (mstride[0] == 0)
135 mstride[0] = mask_kind;
137 if (zero_sized)
138 sptr = NULL;
139 else
140 sptr = array->base_addr;
142 if (ret->base_addr == NULL || unlikely (compile_options.bounds_check))
144 /* Count the elements, either for allocating memory or
145 for bounds checking. */
147 if (vector != NULL)
149 /* The return array will have as many
150 elements as there are in VECTOR. */
151 total = GFC_DESCRIPTOR_EXTENT(vector,0);
152 if (total < 0)
154 total = 0;
155 vector = NULL;
158 else
160 /* We have to count the true elements in MASK. */
161 total = count_0 (mask);
164 if (ret->base_addr == NULL)
166 /* Setup the array descriptor. */
167 GFC_DIMENSION_SET(ret->dim[0], 0, total-1, 1);
169 ret->offset = 0;
171 /* xmalloc allocates a single byte for zero size. */
172 ret->base_addr = xmalloc (sizeof (GFC_INTEGER_1) * total);
174 if (total == 0)
175 return;
177 else
179 /* We come here because of range checking. */
180 index_type ret_extent;
182 ret_extent = GFC_DESCRIPTOR_EXTENT(ret,0);
183 if (total != ret_extent)
184 runtime_error ("Incorrect extent in return value of PACK intrinsic;"
185 " is %ld, should be %ld", (long int) total,
186 (long int) ret_extent);
190 rstride0 = GFC_DESCRIPTOR_STRIDE(ret,0);
191 if (rstride0 == 0)
192 rstride0 = 1;
193 sstride0 = sstride[0];
194 mstride0 = mstride[0];
195 rptr = ret->base_addr;
197 while (sptr && mptr)
199 /* Test this element. */
200 if (*mptr)
202 /* Add it. */
203 *rptr = *sptr;
204 rptr += rstride0;
206 /* Advance to the next element. */
207 sptr += sstride0;
208 mptr += mstride0;
209 count[0]++;
210 n = 0;
211 while (count[n] == extent[n])
213 /* When we get to the end of a dimension, reset it and increment
214 the next dimension. */
215 count[n] = 0;
216 /* We could precalculate these products, but this is a less
217 frequently used path so probably not worth it. */
218 sptr -= sstride[n] * extent[n];
219 mptr -= mstride[n] * extent[n];
220 n++;
221 if (n >= dim)
223 /* Break out of the loop. */
224 sptr = NULL;
225 break;
227 else
229 count[n]++;
230 sptr += sstride[n];
231 mptr += mstride[n];
236 /* Add any remaining elements from VECTOR. */
237 if (vector)
239 n = GFC_DESCRIPTOR_EXTENT(vector,0);
240 nelem = ((rptr - ret->base_addr) / rstride0);
241 if (n > nelem)
243 sstride0 = GFC_DESCRIPTOR_STRIDE(vector,0);
244 if (sstride0 == 0)
245 sstride0 = 1;
247 sptr = vector->base_addr + sstride0 * nelem;
248 n -= nelem;
249 while (n--)
251 *rptr = *sptr;
252 rptr += rstride0;
253 sptr += sstride0;
259 #endif