1 /* Generic implementation of the UNPACK intrinsic
2 Copyright 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 95 runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 In addition to the permissions in the GNU General Public License, the
13 Free Software Foundation gives you unlimited permission to link the
14 compiled version of this file into combinations with other programs,
15 and to distribute those combinations without any restriction coming
16 from the use of this file. (The General Public License restrictions
17 do apply in other respects; for example, they cover modification of
18 the file, and distribution when not linked into a combine
21 Ligbfortran is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public
27 License along with libgfortran; see the file COPYING. If not,
28 write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 Boston, MA 02110-1301, USA. */
35 #include "libgfortran.h"
38 unpack_internal (gfc_array_char *ret, const gfc_array_char *vector,
39 const gfc_array_l1 *mask, const gfc_array_char *field,
40 index_type size, index_type fsize)
42 /* r.* indicates the return array. */
43 index_type rstride[GFC_MAX_DIMENSIONS];
47 /* v.* indicates the vector array. */
50 /* f.* indicates the field array. */
51 index_type fstride[GFC_MAX_DIMENSIONS];
54 /* m.* indicates the mask array. */
55 index_type mstride[GFC_MAX_DIMENSIONS];
57 const GFC_LOGICAL_1 *mptr;
59 index_type count[GFC_MAX_DIMENSIONS];
60 index_type extent[GFC_MAX_DIMENSIONS];
71 /* Use the same loop for all logical types, by using GFC_LOGICAL_1
72 and using shifting to address size and endian issues. */
74 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
76 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
77 #ifdef HAVE_GFC_LOGICAL_16
82 /* Don't convert a NULL pointer as we use test for NULL below. */
84 mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
87 runtime_error ("Funny sized logical array");
89 if (ret->data == NULL)
91 /* The front end has signalled that we need to populate the
92 return array descriptor. */
93 dim = GFC_DESCRIPTOR_RANK (mask);
95 for (n = 0; n < dim; n++)
98 ret->dim[n].stride = rs;
99 ret->dim[n].lbound = 0;
100 ret->dim[n].ubound = mask->dim[n].ubound - mask->dim[n].lbound;
101 extent[n] = ret->dim[n].ubound + 1;
102 empty = empty || extent[n] <= 0;
103 rstride[n] = ret->dim[n].stride * size;
104 fstride[n] = field->dim[n].stride * fsize;
105 mstride[n] = mask->dim[n].stride * mask_kind;
109 ret->data = internal_malloc_size (rs * size);
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 * size;
120 fstride[n] = field->dim[n].stride * fsize;
121 mstride[n] = mask->dim[n].stride * mask_kind;
135 vstride0 = vector->dim[0].stride * size;
138 rstride0 = rstride[0];
139 fstride0 = fstride[0];
140 mstride0 = mstride[0];
150 memcpy (rptr, vptr, size);
156 memcpy (rptr, fptr, size);
158 /* Advance to the next element. */
164 while (count[n] == extent[n])
166 /* When we get to the end of a dimension, reset it and increment
167 the next dimension. */
169 /* We could precalculate these products, but this is a less
170 frequently used path so probably not worth it. */
171 rptr -= rstride[n] * extent[n];
172 fptr -= fstride[n] * extent[n];
173 mptr -= mstride[n] * extent[n];
177 /* Break out of the loop. */
192 extern void unpack1 (gfc_array_char *, const gfc_array_char *,
193 const gfc_array_l4 *, const gfc_array_char *);
194 export_proto(unpack1);
197 unpack1 (gfc_array_char *ret, const gfc_array_char *vector,
198 const gfc_array_l4 *mask, const gfc_array_char *field)
200 unpack_internal (ret, vector, mask, field,
201 GFC_DESCRIPTOR_SIZE (vector),
202 GFC_DESCRIPTOR_SIZE (field));
205 extern void unpack1_char (gfc_array_char *, GFC_INTEGER_4,
206 const gfc_array_char *, const gfc_array_l4 *,
207 const gfc_array_char *, GFC_INTEGER_4,
209 export_proto(unpack1_char);
212 unpack1_char (gfc_array_char *ret,
213 GFC_INTEGER_4 ret_length __attribute__((unused)),
214 const gfc_array_char *vector, const gfc_array_l4 *mask,
215 const gfc_array_char *field, GFC_INTEGER_4 vector_length,
216 GFC_INTEGER_4 field_length)
218 unpack_internal (ret, vector, mask, field, vector_length, field_length);
221 extern void unpack0 (gfc_array_char *, const gfc_array_char *,
222 const gfc_array_l4 *, char *);
223 export_proto(unpack0);
226 unpack0 (gfc_array_char *ret, const gfc_array_char *vector,
227 const gfc_array_l4 *mask, char *field)
231 memset (&tmp, 0, sizeof (tmp));
234 unpack_internal (ret, vector, mask, &tmp, GFC_DESCRIPTOR_SIZE (vector), 0);
237 extern void unpack0_char (gfc_array_char *, GFC_INTEGER_4,
238 const gfc_array_char *, const gfc_array_l4 *,
239 char *, GFC_INTEGER_4, GFC_INTEGER_4);
240 export_proto(unpack0_char);
243 unpack0_char (gfc_array_char *ret,
244 GFC_INTEGER_4 ret_length __attribute__((unused)),
245 const gfc_array_char *vector, const gfc_array_l4 *mask,
246 char *field, GFC_INTEGER_4 vector_length,
247 GFC_INTEGER_4 field_length __attribute__((unused)))
251 memset (&tmp, 0, sizeof (tmp));
254 unpack_internal (ret, vector, mask, &tmp, vector_length, 0);