1 /* Generic implementation of the UNPACK intrinsic
2 Copyright 2002, 2003, 2004, 2005, 2007 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. */
31 #include "libgfortran.h"
37 unpack_internal (gfc_array_char
*ret
, const gfc_array_char
*vector
,
38 const gfc_array_l1
*mask
, const gfc_array_char
*field
,
39 index_type size
, index_type fsize
)
41 /* r.* indicates the return array. */
42 index_type rstride
[GFC_MAX_DIMENSIONS
];
46 /* v.* indicates the vector array. */
49 /* f.* indicates the field array. */
50 index_type fstride
[GFC_MAX_DIMENSIONS
];
53 /* m.* indicates the mask array. */
54 index_type mstride
[GFC_MAX_DIMENSIONS
];
56 const GFC_LOGICAL_1
*mptr
;
58 index_type count
[GFC_MAX_DIMENSIONS
];
59 index_type extent
[GFC_MAX_DIMENSIONS
];
70 /* Use the same loop for all logical types, by using GFC_LOGICAL_1
71 and using shifting to address size and endian issues. */
73 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
75 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
76 #ifdef HAVE_GFC_LOGICAL_16
81 /* Don't convert a NULL pointer as we use test for NULL below. */
83 mptr
= GFOR_POINTER_TO_L1 (mptr
, mask_kind
);
86 runtime_error ("Funny sized logical array");
88 if (ret
->data
== NULL
)
90 /* The front end has signalled that we need to populate the
91 return array descriptor. */
92 dim
= GFC_DESCRIPTOR_RANK (mask
);
94 for (n
= 0; n
< dim
; n
++)
97 ret
->dim
[n
].stride
= rs
;
98 ret
->dim
[n
].lbound
= 0;
99 ret
->dim
[n
].ubound
= mask
->dim
[n
].ubound
- mask
->dim
[n
].lbound
;
100 extent
[n
] = ret
->dim
[n
].ubound
+ 1;
101 empty
= empty
|| extent
[n
] <= 0;
102 rstride
[n
] = ret
->dim
[n
].stride
* size
;
103 fstride
[n
] = field
->dim
[n
].stride
* fsize
;
104 mstride
[n
] = mask
->dim
[n
].stride
* mask_kind
;
108 ret
->data
= internal_malloc_size (rs
* size
);
112 dim
= GFC_DESCRIPTOR_RANK (ret
);
113 for (n
= 0; n
< dim
; n
++)
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
* size
;
119 fstride
[n
] = field
->dim
[n
].stride
* fsize
;
120 mstride
[n
] = mask
->dim
[n
].stride
* mask_kind
;
134 vstride0
= vector
->dim
[0].stride
* size
;
137 rstride0
= rstride
[0];
138 fstride0
= fstride
[0];
139 mstride0
= mstride
[0];
149 memcpy (rptr
, vptr
, size
);
155 memcpy (rptr
, fptr
, size
);
157 /* Advance to the next element. */
163 while (count
[n
] == extent
[n
])
165 /* When we get to the end of a dimension, reset it and increment
166 the next dimension. */
168 /* We could precalculate these products, but this is a less
169 frequently used path so probably not worth it. */
170 rptr
-= rstride
[n
] * extent
[n
];
171 fptr
-= fstride
[n
] * extent
[n
];
172 mptr
-= mstride
[n
] * extent
[n
];
176 /* Break out of the loop. */
191 extern void unpack1 (gfc_array_char
*, const gfc_array_char
*,
192 const gfc_array_l1
*, const gfc_array_char
*);
193 export_proto(unpack1
);
196 unpack1 (gfc_array_char
*ret
, const gfc_array_char
*vector
,
197 const gfc_array_l1
*mask
, const gfc_array_char
*field
)
202 type
= GFC_DESCRIPTOR_TYPE (vector
);
203 size
= GFC_DESCRIPTOR_SIZE (vector
);
207 case GFC_DTYPE_INTEGER
:
208 case GFC_DTYPE_LOGICAL
:
211 case sizeof (GFC_INTEGER_1
):
212 unpack1_i1 ((gfc_array_i1
*) ret
, (gfc_array_i1
*) vector
,
213 mask
, (gfc_array_i1
*) field
);
216 case sizeof (GFC_INTEGER_2
):
217 unpack1_i2 ((gfc_array_i2
*) ret
, (gfc_array_i2
*) vector
,
218 mask
, (gfc_array_i2
*) field
);
221 case sizeof (GFC_INTEGER_4
):
222 unpack1_i4 ((gfc_array_i4
*) ret
, (gfc_array_i4
*) vector
,
223 mask
, (gfc_array_i4
*) field
);
226 case sizeof (GFC_INTEGER_8
):
227 unpack1_i8 ((gfc_array_i8
*) ret
, (gfc_array_i8
*) vector
,
228 mask
, (gfc_array_i8
*) field
);
231 #ifdef HAVE_GFC_INTEGER_16
232 case sizeof (GFC_INTEGER_16
):
233 unpack1_i16 ((gfc_array_i16
*) ret
, (gfc_array_i16
*) vector
,
234 mask
, (gfc_array_i16
*) field
);
241 case sizeof (GFC_REAL_4
):
242 unpack1_r4 ((gfc_array_r4
*) ret
, (gfc_array_r4
*) vector
,
243 mask
, (gfc_array_r4
*) field
);
246 case sizeof (GFC_REAL_8
):
247 unpack1_r8 ((gfc_array_r8
*) ret
, (gfc_array_r8
*) vector
,
248 mask
, (gfc_array_r8
*) field
);
251 #ifdef HAVE_GFC_REAL_10
252 case sizeof (GFC_REAL_10
):
253 unpack1_r10 ((gfc_array_r10
*) ret
, (gfc_array_r10
*) vector
,
254 mask
, (gfc_array_r10
*) field
);
258 #ifdef HAVE_GFC_REAL_16
259 case sizeof (GFC_REAL_16
):
260 unpack1_r16 ((gfc_array_r16
*) ret
, (gfc_array_r16
*) vector
,
261 mask
, (gfc_array_r16
*) field
);
266 case GFC_DTYPE_COMPLEX
:
269 case sizeof (GFC_COMPLEX_4
):
270 unpack1_c4 ((gfc_array_c4
*) ret
, (gfc_array_c4
*) vector
,
271 mask
, (gfc_array_c4
*) field
);
274 case sizeof (GFC_COMPLEX_8
):
275 unpack1_c8 ((gfc_array_c8
*) ret
, (gfc_array_c8
*) vector
,
276 mask
, (gfc_array_c8
*) field
);
279 #ifdef HAVE_GFC_COMPLEX_10
280 case sizeof (GFC_COMPLEX_10
):
281 unpack1_c10 ((gfc_array_c10
*) ret
, (gfc_array_c10
*) vector
,
282 mask
, (gfc_array_c10
*) field
);
286 #ifdef HAVE_GFC_COMPLEX_16
287 case sizeof (GFC_COMPLEX_16
):
288 unpack1_c16 ((gfc_array_c16
*) ret
, (gfc_array_c16
*) vector
,
289 mask
, (gfc_array_c16
*) field
);
295 unpack_internal (ret
, vector
, mask
, field
, size
,
296 GFC_DESCRIPTOR_SIZE (field
));
299 extern void unpack1_char (gfc_array_char
*, GFC_INTEGER_4
,
300 const gfc_array_char
*, const gfc_array_l1
*,
301 const gfc_array_char
*, GFC_INTEGER_4
,
303 export_proto(unpack1_char
);
306 unpack1_char (gfc_array_char
*ret
,
307 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
308 const gfc_array_char
*vector
, const gfc_array_l1
*mask
,
309 const gfc_array_char
*field
, GFC_INTEGER_4 vector_length
,
310 GFC_INTEGER_4 field_length
)
312 unpack_internal (ret
, vector
, mask
, field
, vector_length
, field_length
);
315 extern void unpack0 (gfc_array_char
*, const gfc_array_char
*,
316 const gfc_array_l1
*, char *);
317 export_proto(unpack0
);
320 unpack0 (gfc_array_char
*ret
, const gfc_array_char
*vector
,
321 const gfc_array_l1
*mask
, char *field
)
328 type
= GFC_DESCRIPTOR_TYPE (vector
);
329 size
= GFC_DESCRIPTOR_SIZE (vector
);
333 case GFC_DTYPE_INTEGER
:
334 case GFC_DTYPE_LOGICAL
:
337 case sizeof (GFC_INTEGER_1
):
338 unpack0_i1 ((gfc_array_i1
*) ret
, (gfc_array_i1
*) vector
,
339 mask
, (GFC_INTEGER_1
*) field
);
342 case sizeof (GFC_INTEGER_2
):
343 unpack0_i2 ((gfc_array_i2
*) ret
, (gfc_array_i2
*) vector
,
344 mask
, (GFC_INTEGER_2
*) field
);
347 case sizeof (GFC_INTEGER_4
):
348 unpack0_i4 ((gfc_array_i4
*) ret
, (gfc_array_i4
*) vector
,
349 mask
, (GFC_INTEGER_4
*) field
);
352 case sizeof (GFC_INTEGER_8
):
353 unpack0_i8 ((gfc_array_i8
*) ret
, (gfc_array_i8
*) vector
,
354 mask
, (GFC_INTEGER_8
*) field
);
357 #ifdef HAVE_GFC_INTEGER_16
358 case sizeof (GFC_INTEGER_16
):
359 unpack0_i16 ((gfc_array_i16
*) ret
, (gfc_array_i16
*) vector
,
360 mask
, (GFC_INTEGER_16
*) field
);
368 case sizeof (GFC_REAL_4
):
369 unpack0_r4 ((gfc_array_r4
*) ret
, (gfc_array_r4
*) vector
,
370 mask
, (GFC_REAL_4
*) field
);
373 case sizeof (GFC_REAL_8
):
374 unpack0_r8 ((gfc_array_r8
*) ret
, (gfc_array_r8
*) vector
,
375 mask
, (GFC_REAL_8
*) field
);
378 #ifdef HAVE_GFC_REAL_10
379 case sizeof (GFC_REAL_10
):
380 unpack0_r10 ((gfc_array_r10
*) ret
, (gfc_array_r10
*) vector
,
381 mask
, (GFC_REAL_10
*) field
);
385 #ifdef HAVE_GFC_REAL_16
386 case sizeof (GFC_REAL_16
):
387 unpack0_r16 ((gfc_array_r16
*) ret
, (gfc_array_r16
*) vector
,
388 mask
, (GFC_REAL_16
*) field
);
393 case GFC_DTYPE_COMPLEX
:
396 case sizeof (GFC_COMPLEX_4
):
397 unpack0_c4 ((gfc_array_c4
*) ret
, (gfc_array_c4
*) vector
,
398 mask
, (GFC_COMPLEX_4
*) field
);
401 case sizeof (GFC_COMPLEX_8
):
402 unpack0_c8 ((gfc_array_c8
*) ret
, (gfc_array_c8
*) vector
,
403 mask
, (GFC_COMPLEX_8
*) field
);
406 #ifdef HAVE_GFC_COMPLEX_10
407 case sizeof (GFC_COMPLEX_10
):
408 unpack0_c10 ((gfc_array_c10
*) ret
, (gfc_array_c10
*) vector
,
409 mask
, (GFC_COMPLEX_10
*) field
);
413 #ifdef HAVE_GFC_COMPLEX_16
414 case sizeof (GFC_COMPLEX_16
):
415 unpack0_c16 ((gfc_array_c16
*) ret
, (gfc_array_c16
*) vector
,
416 mask
, (GFC_COMPLEX_16
*) field
);
421 memset (&tmp
, 0, sizeof (tmp
));
424 unpack_internal (ret
, vector
, mask
, &tmp
, GFC_DESCRIPTOR_SIZE (vector
), 0);
427 extern void unpack0_char (gfc_array_char
*, GFC_INTEGER_4
,
428 const gfc_array_char
*, const gfc_array_l1
*,
429 char *, GFC_INTEGER_4
, GFC_INTEGER_4
);
430 export_proto(unpack0_char
);
433 unpack0_char (gfc_array_char
*ret
,
434 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
435 const gfc_array_char
*vector
, const gfc_array_l1
*mask
,
436 char *field
, GFC_INTEGER_4 vector_length
,
437 GFC_INTEGER_4 field_length
__attribute__((unused
)))
441 memset (&tmp
, 0, sizeof (tmp
));
444 unpack_internal (ret
, vector
, mask
, &tmp
, vector_length
, 0);