1 `/* Specific implementation of the UNPACK intrinsic
2 Copyright 2008 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 95 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 2 of the License, or (at your option) any later version.
13 In addition to the permissions in the GNU General Public License, the
14 Free Software Foundation gives you unlimited permission to link the
15 compiled version of this file into combinations with other programs,
16 and to distribute those combinations without any restriction coming
17 from the use of this file. (The General Public License restrictions
18 do apply in other respects; for example, they cover modification of
19 the file, and distribution when not linked into a combine
22 Ligbfortran is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public
28 License along with libgfortran; see the file COPYING. If not,
29 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30 Boston, MA 02110-1301, USA. */
32 #include "libgfortran.h"
39 `#if defined (HAVE_'rtype_name`)
42 unpack0_'rtype_code` ('rtype` *ret, const 'rtype` *vector,
43 const gfc_array_l1 *mask, const 'rtype_name` *fptr)
45 /* r.* indicates the return array. */
46 index_type rstride[GFC_MAX_DIMENSIONS];
49 'rtype_name` * restrict rptr;
50 /* v.* indicates the vector array. */
53 /* Value for field, this is constant. */
54 const 'rtype_name` fval = *fptr;
55 /* m.* indicates the mask array. */
56 index_type mstride[GFC_MAX_DIMENSIONS];
58 const GFC_LOGICAL_1 *mptr;
60 index_type count[GFC_MAX_DIMENSIONS];
61 index_type extent[GFC_MAX_DIMENSIONS];
72 /* Use the same loop for all logical types, by using GFC_LOGICAL_1
73 and using shifting to address size and endian issues. */
75 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
77 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
78 #ifdef HAVE_GFC_LOGICAL_16
83 /* Do not convert a NULL pointer as we use test for NULL below. */
85 mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
88 runtime_error ("Funny sized logical array");
90 if (ret->data == NULL)
92 /* The front end has signalled that we need to populate the
93 return array descriptor. */
94 dim = GFC_DESCRIPTOR_RANK (mask);
96 for (n = 0; n < dim; n++)
99 ret->dim[n].stride = rs;
100 ret->dim[n].lbound = 0;
101 ret->dim[n].ubound = mask->dim[n].ubound - mask->dim[n].lbound;
102 extent[n] = ret->dim[n].ubound + 1;
103 empty = empty || extent[n] <= 0;
104 rstride[n] = ret->dim[n].stride;
105 mstride[n] = mask->dim[n].stride * mask_kind;
109 ret->data = internal_malloc_size (rs * sizeof ('rtype_name`));
113 dim = GFC_DESCRIPTOR_RANK (ret);
114 for (n = 0; n < dim; n++)
117 extent[n] = ret->dim[n].ubound + 1 - ret->dim[n].lbound;
118 empty = empty || extent[n] <= 0;
119 rstride[n] = ret->dim[n].stride;
120 mstride[n] = mask->dim[n].stride * mask_kind;
132 vstride0 = vector->dim[0].stride;
135 rstride0 = rstride[0];
136 mstride0 = mstride[0];
153 /* Advance to the next element. */
158 while (count[n] == extent[n])
160 /* When we get to the end of a dimension, reset it and increment
161 the next dimension. */
163 /* We could precalculate these products, but this is a less
164 frequently used path so probably not worth it. */
165 rptr -= rstride[n] * extent[n];
166 mptr -= mstride[n] * extent[n];
170 /* Break out of the loop. */
185 unpack1_'rtype_code` ('rtype` *ret, const 'rtype` *vector,
186 const gfc_array_l1 *mask, const 'rtype` *field)
188 /* r.* indicates the return array. */
189 index_type rstride[GFC_MAX_DIMENSIONS];
192 'rtype_name` * restrict rptr;
193 /* v.* indicates the vector array. */
196 /* f.* indicates the field array. */
197 index_type fstride[GFC_MAX_DIMENSIONS];
199 const 'rtype_name` *fptr;
200 /* m.* indicates the mask array. */
201 index_type mstride[GFC_MAX_DIMENSIONS];
203 const GFC_LOGICAL_1 *mptr;
205 index_type count[GFC_MAX_DIMENSIONS];
206 index_type extent[GFC_MAX_DIMENSIONS];
217 /* Use the same loop for all logical types, by using GFC_LOGICAL_1
218 and using shifting to address size and endian issues. */
220 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
222 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
223 #ifdef HAVE_GFC_LOGICAL_16
228 /* Do not convert a NULL pointer as we use test for NULL below. */
230 mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
233 runtime_error ("Funny sized logical array");
235 if (ret->data == NULL)
237 /* The front end has signalled that we need to populate the
238 return array descriptor. */
239 dim = GFC_DESCRIPTOR_RANK (mask);
241 for (n = 0; n < dim; n++)
244 ret->dim[n].stride = rs;
245 ret->dim[n].lbound = 0;
246 ret->dim[n].ubound = mask->dim[n].ubound - mask->dim[n].lbound;
247 extent[n] = ret->dim[n].ubound + 1;
248 empty = empty || extent[n] <= 0;
249 rstride[n] = ret->dim[n].stride;
250 fstride[n] = field->dim[n].stride;
251 mstride[n] = mask->dim[n].stride * mask_kind;
255 ret->data = internal_malloc_size (rs * sizeof ('rtype_name`));
259 dim = GFC_DESCRIPTOR_RANK (ret);
260 for (n = 0; n < dim; n++)
263 extent[n] = ret->dim[n].ubound + 1 - ret->dim[n].lbound;
264 empty = empty || extent[n] <= 0;
265 rstride[n] = ret->dim[n].stride;
266 fstride[n] = field->dim[n].stride;
267 mstride[n] = mask->dim[n].stride * mask_kind;
281 vstride0 = vector->dim[0].stride;
284 rstride0 = rstride[0];
285 fstride0 = fstride[0];
286 mstride0 = mstride[0];
304 /* Advance to the next element. */
310 while (count[n] == extent[n])
312 /* When we get to the end of a dimension, reset it and increment
313 the next dimension. */
315 /* We could precalculate these products, but this is a less
316 frequently used path so probably not worth it. */
317 rptr -= rstride[n] * extent[n];
318 fptr -= fstride[n] * extent[n];
319 mptr -= mstride[n] * extent[n];
323 /* Break out of the loop. */