Update concepts branch to revision 131834
[official-gcc.git] / libgfortran / generated / unpack_c4.c
blob3ef95147ab8d7b32dfccac5bc0c1c6313a3072a9
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
20 executable.)
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"
33 #include <stdlib.h>
34 #include <assert.h>
35 #include <string.h>
38 #if defined (HAVE_GFC_COMPLEX_4)
40 void
41 unpack0_c4 (gfc_array_c4 *ret, const gfc_array_c4 *vector,
42 const gfc_array_l1 *mask, const GFC_COMPLEX_4 *fptr)
44 /* r.* indicates the return array. */
45 index_type rstride[GFC_MAX_DIMENSIONS];
46 index_type rstride0;
47 index_type rs;
48 GFC_COMPLEX_4 * restrict rptr;
49 /* v.* indicates the vector array. */
50 index_type vstride0;
51 GFC_COMPLEX_4 *vptr;
52 /* Value for field, this is constant. */
53 const GFC_COMPLEX_4 fval = *fptr;
54 /* m.* indicates the mask array. */
55 index_type mstride[GFC_MAX_DIMENSIONS];
56 index_type mstride0;
57 const GFC_LOGICAL_1 *mptr;
59 index_type count[GFC_MAX_DIMENSIONS];
60 index_type extent[GFC_MAX_DIMENSIONS];
61 index_type n;
62 index_type dim;
64 int empty;
65 int mask_kind;
67 empty = 0;
69 mptr = mask->data;
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
78 || mask_kind == 16
79 #endif
82 /* Do not convert a NULL pointer as we use test for NULL below. */
83 if (mptr)
84 mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
86 else
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);
94 rs = 1;
95 for (n = 0; n < dim; n++)
97 count[n] = 0;
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;
104 mstride[n] = mask->dim[n].stride * mask_kind;
105 rs *= extent[n];
107 ret->offset = 0;
108 ret->data = internal_malloc_size (rs * sizeof (GFC_COMPLEX_4));
110 else
112 dim = GFC_DESCRIPTOR_RANK (ret);
113 for (n = 0; n < dim; n++)
115 count[n] = 0;
116 extent[n] = ret->dim[n].ubound + 1 - ret->dim[n].lbound;
117 empty = empty || extent[n] <= 0;
118 rstride[n] = ret->dim[n].stride;
119 mstride[n] = mask->dim[n].stride * mask_kind;
121 if (rstride[0] == 0)
122 rstride[0] = 1;
125 if (empty)
126 return;
128 if (mstride[0] == 0)
129 mstride[0] = 1;
131 vstride0 = vector->dim[0].stride;
132 if (vstride0 == 0)
133 vstride0 = 1;
134 rstride0 = rstride[0];
135 mstride0 = mstride[0];
136 rptr = ret->data;
137 vptr = vector->data;
139 while (rptr)
141 if (*mptr)
143 /* From vector. */
144 *rptr = *vptr;
145 vptr += vstride0;
147 else
149 /* From field. */
150 *rptr = fval;
152 /* Advance to the next element. */
153 rptr += rstride0;
154 mptr += mstride0;
155 count[0]++;
156 n = 0;
157 while (count[n] == extent[n])
159 /* When we get to the end of a dimension, reset it and increment
160 the next dimension. */
161 count[n] = 0;
162 /* We could precalculate these products, but this is a less
163 frequently used path so probably not worth it. */
164 rptr -= rstride[n] * extent[n];
165 mptr -= mstride[n] * extent[n];
166 n++;
167 if (n >= dim)
169 /* Break out of the loop. */
170 rptr = NULL;
171 break;
173 else
175 count[n]++;
176 rptr += rstride[n];
177 mptr += mstride[n];
183 void
184 unpack1_c4 (gfc_array_c4 *ret, const gfc_array_c4 *vector,
185 const gfc_array_l1 *mask, const gfc_array_c4 *field)
187 /* r.* indicates the return array. */
188 index_type rstride[GFC_MAX_DIMENSIONS];
189 index_type rstride0;
190 index_type rs;
191 GFC_COMPLEX_4 * restrict rptr;
192 /* v.* indicates the vector array. */
193 index_type vstride0;
194 GFC_COMPLEX_4 *vptr;
195 /* f.* indicates the field array. */
196 index_type fstride[GFC_MAX_DIMENSIONS];
197 index_type fstride0;
198 const GFC_COMPLEX_4 *fptr;
199 /* m.* indicates the mask array. */
200 index_type mstride[GFC_MAX_DIMENSIONS];
201 index_type mstride0;
202 const GFC_LOGICAL_1 *mptr;
204 index_type count[GFC_MAX_DIMENSIONS];
205 index_type extent[GFC_MAX_DIMENSIONS];
206 index_type n;
207 index_type dim;
209 int empty;
210 int mask_kind;
212 empty = 0;
214 mptr = mask->data;
216 /* Use the same loop for all logical types, by using GFC_LOGICAL_1
217 and using shifting to address size and endian issues. */
219 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
221 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
222 #ifdef HAVE_GFC_LOGICAL_16
223 || mask_kind == 16
224 #endif
227 /* Do not convert a NULL pointer as we use test for NULL below. */
228 if (mptr)
229 mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
231 else
232 runtime_error ("Funny sized logical array");
234 if (ret->data == NULL)
236 /* The front end has signalled that we need to populate the
237 return array descriptor. */
238 dim = GFC_DESCRIPTOR_RANK (mask);
239 rs = 1;
240 for (n = 0; n < dim; n++)
242 count[n] = 0;
243 ret->dim[n].stride = rs;
244 ret->dim[n].lbound = 0;
245 ret->dim[n].ubound = mask->dim[n].ubound - mask->dim[n].lbound;
246 extent[n] = ret->dim[n].ubound + 1;
247 empty = empty || extent[n] <= 0;
248 rstride[n] = ret->dim[n].stride;
249 fstride[n] = field->dim[n].stride;
250 mstride[n] = mask->dim[n].stride * mask_kind;
251 rs *= extent[n];
253 ret->offset = 0;
254 ret->data = internal_malloc_size (rs * sizeof (GFC_COMPLEX_4));
256 else
258 dim = GFC_DESCRIPTOR_RANK (ret);
259 for (n = 0; n < dim; n++)
261 count[n] = 0;
262 extent[n] = ret->dim[n].ubound + 1 - ret->dim[n].lbound;
263 empty = empty || extent[n] <= 0;
264 rstride[n] = ret->dim[n].stride;
265 fstride[n] = field->dim[n].stride;
266 mstride[n] = mask->dim[n].stride * mask_kind;
268 if (rstride[0] == 0)
269 rstride[0] = 1;
272 if (empty)
273 return;
275 if (fstride[0] == 0)
276 fstride[0] = 1;
277 if (mstride[0] == 0)
278 mstride[0] = 1;
280 vstride0 = vector->dim[0].stride;
281 if (vstride0 == 0)
282 vstride0 = 1;
283 rstride0 = rstride[0];
284 fstride0 = fstride[0];
285 mstride0 = mstride[0];
286 rptr = ret->data;
287 fptr = field->data;
288 vptr = vector->data;
290 while (rptr)
292 if (*mptr)
294 /* From vector. */
295 *rptr = *vptr;
296 vptr += vstride0;
298 else
300 /* From field. */
301 *rptr = *fptr;
303 /* Advance to the next element. */
304 rptr += rstride0;
305 fptr += fstride0;
306 mptr += mstride0;
307 count[0]++;
308 n = 0;
309 while (count[n] == extent[n])
311 /* When we get to the end of a dimension, reset it and increment
312 the next dimension. */
313 count[n] = 0;
314 /* We could precalculate these products, but this is a less
315 frequently used path so probably not worth it. */
316 rptr -= rstride[n] * extent[n];
317 fptr -= fstride[n] * extent[n];
318 mptr -= mstride[n] * extent[n];
319 n++;
320 if (n >= dim)
322 /* Break out of the loop. */
323 rptr = NULL;
324 break;
326 else
328 count[n]++;
329 rptr += rstride[n];
330 fptr += fstride[n];
331 mptr += mstride[n];
337 #endif