1 `/* Specific implementation of the UNPACK intrinsic
2 Copyright (C) 2008-2016 Free Software Foundation, Inc.
3 Contributed by Thomas Koenig <tkoenig@gcc.gnu.org>, based on
4 unpack_generic.c 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"
34 `#if defined (HAVE_'rtype_name`)
37 unpack0_'rtype_code` ('rtype` *ret, const 'rtype` *vector,
38 const gfc_array_l1 *mask, const 'rtype_name` *fptr)
40 /* r.* indicates the return array. */
41 index_type rstride[GFC_MAX_DIMENSIONS];
44 'rtype_name` * restrict rptr;
45 /* v.* indicates the vector array. */
48 /* Value for field, this is constant. */
49 const 'rtype_name` fval = *fptr;
50 /* m.* indicates the mask array. */
51 index_type mstride[GFC_MAX_DIMENSIONS];
53 const GFC_LOGICAL_1 *mptr;
55 index_type count[GFC_MAX_DIMENSIONS];
56 index_type extent[GFC_MAX_DIMENSIONS];
65 mptr = mask->base_addr;
67 /* Use the same loop for all logical types, by using GFC_LOGICAL_1
68 and using shifting to address size and endian issues. */
70 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
72 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
73 #ifdef HAVE_GFC_LOGICAL_16
78 /* Do not convert a NULL pointer as we use test for NULL below. */
80 mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
83 runtime_error ("Funny sized logical array");
85 if (ret->base_addr == NULL)
87 /* The front end has signalled that we need to populate the
88 return array descriptor. */
89 dim = GFC_DESCRIPTOR_RANK (mask);
91 for (n = 0; n < dim; n++)
94 GFC_DIMENSION_SET(ret->dim[n], 0,
95 GFC_DESCRIPTOR_EXTENT(mask,n) - 1, rs);
96 extent[n] = GFC_DESCRIPTOR_EXTENT(ret,n);
97 empty = empty || extent[n] <= 0;
98 rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,n);
99 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
103 ret->base_addr = xmallocarray (rs, sizeof ('rtype_name`));
107 dim = GFC_DESCRIPTOR_RANK (ret);
108 /* Initialize to avoid -Wmaybe-uninitialized complaints. */
110 for (n = 0; n < dim; n++)
113 extent[n] = GFC_DESCRIPTOR_EXTENT(ret,n);
114 empty = empty || extent[n] <= 0;
115 rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,n);
116 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
128 vstride0 = GFC_DESCRIPTOR_STRIDE(vector,0);
131 rstride0 = rstride[0];
132 mstride0 = mstride[0];
133 rptr = ret->base_addr;
134 vptr = vector->base_addr;
149 /* Advance to the next element. */
154 while (count[n] == extent[n])
156 /* When we get to the end of a dimension, reset it and increment
157 the next dimension. */
159 /* We could precalculate these products, but this is a less
160 frequently used path so probably not worth it. */
161 rptr -= rstride[n] * extent[n];
162 mptr -= mstride[n] * extent[n];
166 /* Break out of the loop. */
181 unpack1_'rtype_code` ('rtype` *ret, const 'rtype` *vector,
182 const gfc_array_l1 *mask, const 'rtype` *field)
184 /* r.* indicates the return array. */
185 index_type rstride[GFC_MAX_DIMENSIONS];
188 'rtype_name` * restrict rptr;
189 /* v.* indicates the vector array. */
192 /* f.* indicates the field array. */
193 index_type fstride[GFC_MAX_DIMENSIONS];
195 const 'rtype_name` *fptr;
196 /* m.* indicates the mask array. */
197 index_type mstride[GFC_MAX_DIMENSIONS];
199 const GFC_LOGICAL_1 *mptr;
201 index_type count[GFC_MAX_DIMENSIONS];
202 index_type extent[GFC_MAX_DIMENSIONS];
211 mptr = mask->base_addr;
213 /* Use the same loop for all logical types, by using GFC_LOGICAL_1
214 and using shifting to address size and endian issues. */
216 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
218 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
219 #ifdef HAVE_GFC_LOGICAL_16
224 /* Do not convert a NULL pointer as we use test for NULL below. */
226 mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
229 runtime_error ("Funny sized logical array");
231 if (ret->base_addr == NULL)
233 /* The front end has signalled that we need to populate the
234 return array descriptor. */
235 dim = GFC_DESCRIPTOR_RANK (mask);
237 for (n = 0; n < dim; n++)
240 GFC_DIMENSION_SET(ret->dim[n], 0,
241 GFC_DESCRIPTOR_EXTENT(mask,n) - 1, rs);
242 extent[n] = GFC_DESCRIPTOR_EXTENT(ret,n);
243 empty = empty || extent[n] <= 0;
244 rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,n);
245 fstride[n] = GFC_DESCRIPTOR_STRIDE(field,n);
246 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
250 ret->base_addr = xmallocarray (rs, sizeof ('rtype_name`));
254 dim = GFC_DESCRIPTOR_RANK (ret);
255 /* Initialize to avoid -Wmaybe-uninitialized complaints. */
257 for (n = 0; n < dim; n++)
260 extent[n] = GFC_DESCRIPTOR_EXTENT(ret,n);
261 empty = empty || extent[n] <= 0;
262 rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,n);
263 fstride[n] = GFC_DESCRIPTOR_STRIDE(field,n);
264 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
278 vstride0 = GFC_DESCRIPTOR_STRIDE(vector,0);
281 rstride0 = rstride[0];
282 fstride0 = fstride[0];
283 mstride0 = mstride[0];
284 rptr = ret->base_addr;
285 fptr = field->base_addr;
286 vptr = vector->base_addr;
301 /* Advance to the next element. */
307 while (count[n] == extent[n])
309 /* When we get to the end of a dimension, reset it and increment
310 the next dimension. */
312 /* We could precalculate these products, but this is a less
313 frequently used path so probably not worth it. */
314 rptr -= rstride[n] * extent[n];
315 fptr -= fstride[n] * extent[n];
316 mptr -= mstride[n] * extent[n];
320 /* Break out of the loop. */