Merge from trunk: 215733-215743
[official-gcc.git] / main / libgfortran / generated / unpack_i8.c
blobd91780413eb67229a176ea7d82089226c6ba4c81
1 /* Specific implementation of the UNPACK intrinsic
2 Copyright (C) 2008-2014 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"
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <string.h>
33 #if defined (HAVE_GFC_INTEGER_8)
35 void
36 unpack0_i8 (gfc_array_i8 *ret, const gfc_array_i8 *vector,
37 const gfc_array_l1 *mask, const GFC_INTEGER_8 *fptr)
39 /* r.* indicates the return array. */
40 index_type rstride[GFC_MAX_DIMENSIONS];
41 index_type rstride0;
42 index_type rs;
43 GFC_INTEGER_8 * restrict rptr;
44 /* v.* indicates the vector array. */
45 index_type vstride0;
46 GFC_INTEGER_8 *vptr;
47 /* Value for field, this is constant. */
48 const GFC_INTEGER_8 fval = *fptr;
49 /* m.* indicates the mask array. */
50 index_type mstride[GFC_MAX_DIMENSIONS];
51 index_type mstride0;
52 const GFC_LOGICAL_1 *mptr;
54 index_type count[GFC_MAX_DIMENSIONS];
55 index_type extent[GFC_MAX_DIMENSIONS];
56 index_type n;
57 index_type dim;
59 int empty;
60 int mask_kind;
62 empty = 0;
64 mptr = mask->base_addr;
66 /* Use the same loop for all logical types, by using GFC_LOGICAL_1
67 and using shifting to address size and endian issues. */
69 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
71 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
72 #ifdef HAVE_GFC_LOGICAL_16
73 || mask_kind == 16
74 #endif
77 /* Do not convert a NULL pointer as we use test for NULL below. */
78 if (mptr)
79 mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
81 else
82 runtime_error ("Funny sized logical array");
84 if (ret->base_addr == NULL)
86 /* The front end has signalled that we need to populate the
87 return array descriptor. */
88 dim = GFC_DESCRIPTOR_RANK (mask);
89 rs = 1;
90 for (n = 0; n < dim; n++)
92 count[n] = 0;
93 GFC_DIMENSION_SET(ret->dim[n], 0,
94 GFC_DESCRIPTOR_EXTENT(mask,n) - 1, rs);
95 extent[n] = GFC_DESCRIPTOR_EXTENT(ret,n);
96 empty = empty || extent[n] <= 0;
97 rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,n);
98 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
99 rs *= extent[n];
101 ret->offset = 0;
102 ret->base_addr = xmallocarray (rs, sizeof (GFC_INTEGER_8));
104 else
106 dim = GFC_DESCRIPTOR_RANK (ret);
107 /* Initialize to avoid -Wmaybe-uninitialized complaints. */
108 rstride[0] = 1;
109 for (n = 0; n < dim; n++)
111 count[n] = 0;
112 extent[n] = GFC_DESCRIPTOR_EXTENT(ret,n);
113 empty = empty || extent[n] <= 0;
114 rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,n);
115 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
117 if (rstride[0] == 0)
118 rstride[0] = 1;
121 if (empty)
122 return;
124 if (mstride[0] == 0)
125 mstride[0] = 1;
127 vstride0 = GFC_DESCRIPTOR_STRIDE(vector,0);
128 if (vstride0 == 0)
129 vstride0 = 1;
130 rstride0 = rstride[0];
131 mstride0 = mstride[0];
132 rptr = ret->base_addr;
133 vptr = vector->base_addr;
135 while (rptr)
137 if (*mptr)
139 /* From vector. */
140 *rptr = *vptr;
141 vptr += vstride0;
143 else
145 /* From field. */
146 *rptr = fval;
148 /* Advance to the next element. */
149 rptr += rstride0;
150 mptr += mstride0;
151 count[0]++;
152 n = 0;
153 while (count[n] == extent[n])
155 /* When we get to the end of a dimension, reset it and increment
156 the next dimension. */
157 count[n] = 0;
158 /* We could precalculate these products, but this is a less
159 frequently used path so probably not worth it. */
160 rptr -= rstride[n] * extent[n];
161 mptr -= mstride[n] * extent[n];
162 n++;
163 if (n >= dim)
165 /* Break out of the loop. */
166 rptr = NULL;
167 break;
169 else
171 count[n]++;
172 rptr += rstride[n];
173 mptr += mstride[n];
179 void
180 unpack1_i8 (gfc_array_i8 *ret, const gfc_array_i8 *vector,
181 const gfc_array_l1 *mask, const gfc_array_i8 *field)
183 /* r.* indicates the return array. */
184 index_type rstride[GFC_MAX_DIMENSIONS];
185 index_type rstride0;
186 index_type rs;
187 GFC_INTEGER_8 * restrict rptr;
188 /* v.* indicates the vector array. */
189 index_type vstride0;
190 GFC_INTEGER_8 *vptr;
191 /* f.* indicates the field array. */
192 index_type fstride[GFC_MAX_DIMENSIONS];
193 index_type fstride0;
194 const GFC_INTEGER_8 *fptr;
195 /* m.* indicates the mask array. */
196 index_type mstride[GFC_MAX_DIMENSIONS];
197 index_type mstride0;
198 const GFC_LOGICAL_1 *mptr;
200 index_type count[GFC_MAX_DIMENSIONS];
201 index_type extent[GFC_MAX_DIMENSIONS];
202 index_type n;
203 index_type dim;
205 int empty;
206 int mask_kind;
208 empty = 0;
210 mptr = mask->base_addr;
212 /* Use the same loop for all logical types, by using GFC_LOGICAL_1
213 and using shifting to address size and endian issues. */
215 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
217 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
218 #ifdef HAVE_GFC_LOGICAL_16
219 || mask_kind == 16
220 #endif
223 /* Do not convert a NULL pointer as we use test for NULL below. */
224 if (mptr)
225 mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
227 else
228 runtime_error ("Funny sized logical array");
230 if (ret->base_addr == NULL)
232 /* The front end has signalled that we need to populate the
233 return array descriptor. */
234 dim = GFC_DESCRIPTOR_RANK (mask);
235 rs = 1;
236 for (n = 0; n < dim; n++)
238 count[n] = 0;
239 GFC_DIMENSION_SET(ret->dim[n], 0,
240 GFC_DESCRIPTOR_EXTENT(mask,n) - 1, rs);
241 extent[n] = GFC_DESCRIPTOR_EXTENT(ret,n);
242 empty = empty || extent[n] <= 0;
243 rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,n);
244 fstride[n] = GFC_DESCRIPTOR_STRIDE(field,n);
245 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
246 rs *= extent[n];
248 ret->offset = 0;
249 ret->base_addr = xmallocarray (rs, sizeof (GFC_INTEGER_8));
251 else
253 dim = GFC_DESCRIPTOR_RANK (ret);
254 /* Initialize to avoid -Wmaybe-uninitialized complaints. */
255 rstride[0] = 1;
256 for (n = 0; n < dim; n++)
258 count[n] = 0;
259 extent[n] = GFC_DESCRIPTOR_EXTENT(ret,n);
260 empty = empty || extent[n] <= 0;
261 rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,n);
262 fstride[n] = GFC_DESCRIPTOR_STRIDE(field,n);
263 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
265 if (rstride[0] == 0)
266 rstride[0] = 1;
269 if (empty)
270 return;
272 if (fstride[0] == 0)
273 fstride[0] = 1;
274 if (mstride[0] == 0)
275 mstride[0] = 1;
277 vstride0 = GFC_DESCRIPTOR_STRIDE(vector,0);
278 if (vstride0 == 0)
279 vstride0 = 1;
280 rstride0 = rstride[0];
281 fstride0 = fstride[0];
282 mstride0 = mstride[0];
283 rptr = ret->base_addr;
284 fptr = field->base_addr;
285 vptr = vector->base_addr;
287 while (rptr)
289 if (*mptr)
291 /* From vector. */
292 *rptr = *vptr;
293 vptr += vstride0;
295 else
297 /* From field. */
298 *rptr = *fptr;
300 /* Advance to the next element. */
301 rptr += rstride0;
302 fptr += fstride0;
303 mptr += mstride0;
304 count[0]++;
305 n = 0;
306 while (count[n] == extent[n])
308 /* When we get to the end of a dimension, reset it and increment
309 the next dimension. */
310 count[n] = 0;
311 /* We could precalculate these products, but this is a less
312 frequently used path so probably not worth it. */
313 rptr -= rstride[n] * extent[n];
314 fptr -= fstride[n] * extent[n];
315 mptr -= mstride[n] * extent[n];
316 n++;
317 if (n >= dim)
319 /* Break out of the loop. */
320 rptr = NULL;
321 break;
323 else
325 count[n]++;
326 rptr += rstride[n];
327 fptr += fstride[n];
328 mptr += mstride[n];
334 #endif