1 /* Generic implementation of the PACK intrinsic
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2009, 2010, 2012
3 Free Software Foundation, Inc.
4 Contributed 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"
32 /* PACK is specified as follows:
34 13.14.80 PACK (ARRAY, MASK, [VECTOR])
36 Description: Pack an array into an array of rank one under the
39 Class: Transformational function.
42 ARRAY may be of any type. It shall not be scalar.
43 MASK shall be of type LOGICAL. It shall be conformable with ARRAY.
44 VECTOR (optional) shall be of the same type and type parameters
45 as ARRAY. VECTOR shall have at least as many elements as
46 there are true elements in MASK. If MASK is a scalar
47 with the value true, VECTOR shall have at least as many
48 elements as there are in ARRAY.
50 Result Characteristics: The result is an array of rank one with the
51 same type and type parameters as ARRAY. If VECTOR is present, the
52 result size is that of VECTOR; otherwise, the result size is the
53 number /t/ of true elements in MASK unless MASK is scalar with the
54 value true, in which case the result size is the size of ARRAY.
56 Result Value: Element /i/ of the result is the element of ARRAY
57 that corresponds to the /i/th true element of MASK, taking elements
58 in array element order, for /i/ = 1, 2, ..., /t/. If VECTOR is
59 present and has size /n/ > /t/, element /i/ of the result has the
60 value VECTOR(/i/), for /i/ = /t/ + 1, ..., /n/.
62 Examples: The nonzero elements of an array M with the value
64 | 9 0 0 | may be "gathered" by the function PACK. The result of
66 PACK (M, MASK = M.NE.0) is [9,7] and the result of PACK (M, M.NE.0,
67 VECTOR = (/ 2,4,6,8,10,12 /)) is [9,7,6,8,10,12].
69 There are two variants of the PACK intrinsic: one, where MASK is
70 array valued, and the other one where MASK is scalar. */
73 pack_internal (gfc_array_char
*ret
, const gfc_array_char
*array
,
74 const gfc_array_l1
*mask
, const gfc_array_char
*vector
,
77 /* r.* indicates the return array. */
80 /* s.* indicates the source array. */
81 index_type sstride
[GFC_MAX_DIMENSIONS
];
84 /* m.* indicates the mask array. */
85 index_type mstride
[GFC_MAX_DIMENSIONS
];
87 const GFC_LOGICAL_1
*mptr
;
89 index_type count
[GFC_MAX_DIMENSIONS
];
90 index_type extent
[GFC_MAX_DIMENSIONS
];
97 dim
= GFC_DESCRIPTOR_RANK (array
);
99 sptr
= array
->base_addr
;
100 mptr
= mask
->base_addr
;
102 /* Use the same loop for all logical types, by using GFC_LOGICAL_1
103 and using shifting to address size and endian issues. */
105 mask_kind
= GFC_DESCRIPTOR_SIZE (mask
);
107 if (mask_kind
== 1 || mask_kind
== 2 || mask_kind
== 4 || mask_kind
== 8
108 #ifdef HAVE_GFC_LOGICAL_16
113 /* Don't convert a NULL pointer as we use test for NULL below. */
115 mptr
= GFOR_POINTER_TO_L1 (mptr
, mask_kind
);
118 runtime_error ("Funny sized logical array");
120 for (n
= 0; n
< dim
; n
++)
123 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
124 sstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(array
,n
);
125 mstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(mask
,n
);
130 mstride
[0] = mask_kind
;
132 if (ret
->base_addr
== NULL
|| unlikely (compile_options
.bounds_check
))
134 /* Count the elements, either for allocating memory or
135 for bounds checking. */
139 /* The return array will have as many
140 elements as there are in VECTOR. */
141 total
= GFC_DESCRIPTOR_EXTENT(vector
,0);
145 /* We have to count the true elements in MASK. */
147 total
= count_0 (mask
);
150 if (ret
->base_addr
== NULL
)
152 /* Setup the array descriptor. */
153 GFC_DIMENSION_SET(ret
->dim
[0], 0, total
-1, 1);
156 /* xmalloc allocates a single byte for zero size. */
157 ret
->base_addr
= xmalloc (size
* total
);
160 return; /* In this case, nothing remains to be done. */
164 /* We come here because of range checking. */
165 index_type ret_extent
;
167 ret_extent
= GFC_DESCRIPTOR_EXTENT(ret
,0);
168 if (total
!= ret_extent
)
169 runtime_error ("Incorrect extent in return value of PACK intrinsic;"
170 " is %ld, should be %ld", (long int) total
,
171 (long int) ret_extent
);
175 rstride0
= GFC_DESCRIPTOR_STRIDE_BYTES(ret
,0);
178 sstride0
= sstride
[0];
179 mstride0
= mstride
[0];
180 rptr
= ret
->base_addr
;
184 /* Test this element. */
188 memcpy (rptr
, sptr
, size
);
191 /* Advance to the next element. */
196 while (count
[n
] == extent
[n
])
198 /* When we get to the end of a dimension, reset it and increment
199 the next dimension. */
201 /* We could precalculate these products, but this is a less
202 frequently used path so probably not worth it. */
203 sptr
-= sstride
[n
] * extent
[n
];
204 mptr
-= mstride
[n
] * extent
[n
];
208 /* Break out of the loop. */
221 /* Add any remaining elements from VECTOR. */
224 n
= GFC_DESCRIPTOR_EXTENT(vector
,0);
225 nelem
= ((rptr
- ret
->base_addr
) / rstride0
);
228 sstride0
= GFC_DESCRIPTOR_STRIDE_BYTES(vector
,0);
232 sptr
= vector
->base_addr
+ sstride0
* nelem
;
236 memcpy (rptr
, sptr
, size
);
244 extern void pack (gfc_array_char
*, const gfc_array_char
*,
245 const gfc_array_l1
*, const gfc_array_char
*);
249 pack (gfc_array_char
*ret
, const gfc_array_char
*array
,
250 const gfc_array_l1
*mask
, const gfc_array_char
*vector
)
252 index_type type_size
;
255 type_size
= GFC_DTYPE_TYPE_SIZE(array
);
259 case GFC_DTYPE_LOGICAL_1
:
260 case GFC_DTYPE_INTEGER_1
:
261 case GFC_DTYPE_DERIVED_1
:
262 pack_i1 ((gfc_array_i1
*) ret
, (gfc_array_i1
*) array
,
263 (gfc_array_l1
*) mask
, (gfc_array_i1
*) vector
);
266 case GFC_DTYPE_LOGICAL_2
:
267 case GFC_DTYPE_INTEGER_2
:
268 pack_i2 ((gfc_array_i2
*) ret
, (gfc_array_i2
*) array
,
269 (gfc_array_l1
*) mask
, (gfc_array_i2
*) vector
);
272 case GFC_DTYPE_LOGICAL_4
:
273 case GFC_DTYPE_INTEGER_4
:
274 pack_i4 ((gfc_array_i4
*) ret
, (gfc_array_i4
*) array
,
275 (gfc_array_l1
*) mask
, (gfc_array_i4
*) vector
);
278 case GFC_DTYPE_LOGICAL_8
:
279 case GFC_DTYPE_INTEGER_8
:
280 pack_i8 ((gfc_array_i8
*) ret
, (gfc_array_i8
*) array
,
281 (gfc_array_l1
*) mask
, (gfc_array_i8
*) vector
);
284 #ifdef HAVE_GFC_INTEGER_16
285 case GFC_DTYPE_LOGICAL_16
:
286 case GFC_DTYPE_INTEGER_16
:
287 pack_i16 ((gfc_array_i16
*) ret
, (gfc_array_i16
*) array
,
288 (gfc_array_l1
*) mask
, (gfc_array_i16
*) vector
);
292 case GFC_DTYPE_REAL_4
:
293 pack_r4 ((gfc_array_r4
*) ret
, (gfc_array_r4
*) array
,
294 (gfc_array_l1
*) mask
, (gfc_array_r4
*) vector
);
297 case GFC_DTYPE_REAL_8
:
298 pack_r8 ((gfc_array_r8
*) ret
, (gfc_array_r8
*) array
,
299 (gfc_array_l1
*) mask
, (gfc_array_r8
*) vector
);
302 /* FIXME: This here is a hack, which will have to be removed when
303 the array descriptor is reworked. Currently, we don't store the
304 kind value for the type, but only the size. Because on targets with
305 __float128, we have sizeof(logn double) == sizeof(__float128),
306 we cannot discriminate here and have to fall back to the generic
307 handling (which is suboptimal). */
308 #if !defined(GFC_REAL_16_IS_FLOAT128)
309 # ifdef HAVE_GFC_REAL_10
310 case GFC_DTYPE_REAL_10
:
311 pack_r10 ((gfc_array_r10
*) ret
, (gfc_array_r10
*) array
,
312 (gfc_array_l1
*) mask
, (gfc_array_r10
*) vector
);
316 # ifdef HAVE_GFC_REAL_16
317 case GFC_DTYPE_REAL_16
:
318 pack_r16 ((gfc_array_r16
*) ret
, (gfc_array_r16
*) array
,
319 (gfc_array_l1
*) mask
, (gfc_array_r16
*) vector
);
324 case GFC_DTYPE_COMPLEX_4
:
325 pack_c4 ((gfc_array_c4
*) ret
, (gfc_array_c4
*) array
,
326 (gfc_array_l1
*) mask
, (gfc_array_c4
*) vector
);
329 case GFC_DTYPE_COMPLEX_8
:
330 pack_c8 ((gfc_array_c8
*) ret
, (gfc_array_c8
*) array
,
331 (gfc_array_l1
*) mask
, (gfc_array_c8
*) vector
);
334 /* FIXME: This here is a hack, which will have to be removed when
335 the array descriptor is reworked. Currently, we don't store the
336 kind value for the type, but only the size. Because on targets with
337 __float128, we have sizeof(logn double) == sizeof(__float128),
338 we cannot discriminate here and have to fall back to the generic
339 handling (which is suboptimal). */
340 #if !defined(GFC_REAL_16_IS_FLOAT128)
341 # ifdef HAVE_GFC_COMPLEX_10
342 case GFC_DTYPE_COMPLEX_10
:
343 pack_c10 ((gfc_array_c10
*) ret
, (gfc_array_c10
*) array
,
344 (gfc_array_l1
*) mask
, (gfc_array_c10
*) vector
);
348 # ifdef HAVE_GFC_COMPLEX_16
349 case GFC_DTYPE_COMPLEX_16
:
350 pack_c16 ((gfc_array_c16
*) ret
, (gfc_array_c16
*) array
,
351 (gfc_array_l1
*) mask
, (gfc_array_c16
*) vector
);
356 /* For derived types, let's check the actual alignment of the
357 data pointers. If they are aligned, we can safely call
358 the unpack functions. */
360 case GFC_DTYPE_DERIVED_2
:
361 if (GFC_UNALIGNED_2(ret
->base_addr
) || GFC_UNALIGNED_2(array
->base_addr
)
362 || (vector
&& GFC_UNALIGNED_2(vector
->base_addr
)))
366 pack_i2 ((gfc_array_i2
*) ret
, (gfc_array_i2
*) array
,
367 (gfc_array_l1
*) mask
, (gfc_array_i2
*) vector
);
371 case GFC_DTYPE_DERIVED_4
:
372 if (GFC_UNALIGNED_4(ret
->base_addr
) || GFC_UNALIGNED_4(array
->base_addr
)
373 || (vector
&& GFC_UNALIGNED_4(vector
->base_addr
)))
377 pack_i4 ((gfc_array_i4
*) ret
, (gfc_array_i4
*) array
,
378 (gfc_array_l1
*) mask
, (gfc_array_i4
*) vector
);
382 case GFC_DTYPE_DERIVED_8
:
383 if (GFC_UNALIGNED_8(ret
->base_addr
) || GFC_UNALIGNED_8(array
->base_addr
)
384 || (vector
&& GFC_UNALIGNED_8(vector
->base_addr
)))
388 pack_i8 ((gfc_array_i8
*) ret
, (gfc_array_i8
*) array
,
389 (gfc_array_l1
*) mask
, (gfc_array_i8
*) vector
);
393 #ifdef HAVE_GFC_INTEGER_16
394 case GFC_DTYPE_DERIVED_16
:
395 if (GFC_UNALIGNED_16(ret
->base_addr
) || GFC_UNALIGNED_16(array
->base_addr
)
396 || (vector
&& GFC_UNALIGNED_16(vector
->base_addr
)))
400 pack_i16 ((gfc_array_i16
*) ret
, (gfc_array_i16
*) array
,
401 (gfc_array_l1
*) mask
, (gfc_array_i16
*) vector
);
408 size
= GFC_DESCRIPTOR_SIZE (array
);
409 pack_internal (ret
, array
, mask
, vector
, size
);
413 extern void pack_char (gfc_array_char
*, GFC_INTEGER_4
, const gfc_array_char
*,
414 const gfc_array_l1
*, const gfc_array_char
*,
415 GFC_INTEGER_4
, GFC_INTEGER_4
);
416 export_proto(pack_char
);
419 pack_char (gfc_array_char
*ret
,
420 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
421 const gfc_array_char
*array
, const gfc_array_l1
*mask
,
422 const gfc_array_char
*vector
, GFC_INTEGER_4 array_length
,
423 GFC_INTEGER_4 vector_length
__attribute__((unused
)))
425 pack_internal (ret
, array
, mask
, vector
, array_length
);
429 extern void pack_char4 (gfc_array_char
*, GFC_INTEGER_4
, const gfc_array_char
*,
430 const gfc_array_l1
*, const gfc_array_char
*,
431 GFC_INTEGER_4
, GFC_INTEGER_4
);
432 export_proto(pack_char4
);
435 pack_char4 (gfc_array_char
*ret
,
436 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
437 const gfc_array_char
*array
, const gfc_array_l1
*mask
,
438 const gfc_array_char
*vector
, GFC_INTEGER_4 array_length
,
439 GFC_INTEGER_4 vector_length
__attribute__((unused
)))
441 pack_internal (ret
, array
, mask
, vector
, array_length
* sizeof (gfc_char4_t
));
446 pack_s_internal (gfc_array_char
*ret
, const gfc_array_char
*array
,
447 const GFC_LOGICAL_4
*mask
, const gfc_array_char
*vector
,
450 /* r.* indicates the return array. */
453 /* s.* indicates the source array. */
454 index_type sstride
[GFC_MAX_DIMENSIONS
];
458 index_type count
[GFC_MAX_DIMENSIONS
];
459 index_type extent
[GFC_MAX_DIMENSIONS
];
466 dim
= GFC_DESCRIPTOR_RANK (array
);
468 for (n
= 0; n
< dim
; n
++)
471 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
475 sstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(array
,n
);
481 sstride0
= sstride
[0];
484 sptr
= array
->base_addr
;
488 if (ret
->base_addr
== NULL
)
490 /* Allocate the memory for the result. */
494 /* The return array will have as many elements as there are
496 total
= GFC_DESCRIPTOR_EXTENT(vector
,0);
507 /* The result array will have as many elements as the input
510 for (n
= 1; n
< dim
; n
++)
514 /* The result array will be empty. */
518 /* Setup the array descriptor. */
519 GFC_DIMENSION_SET(ret
->dim
[0],0,total
-1,1);
523 ret
->base_addr
= xmalloc (size
* total
);
529 rstride0
= GFC_DESCRIPTOR_STRIDE_BYTES(ret
,0);
532 rptr
= ret
->base_addr
;
534 /* The remaining possibilities are now:
535 If MASK is .TRUE., we have to copy the source array into the
536 result array. We then have to fill it up with elements from VECTOR.
537 If MASK is .FALSE., we have to copy VECTOR into the result
538 array. If VECTOR were not present we would have already returned. */
540 if (*mask
&& ssize
!= 0)
544 /* Add this element. */
545 memcpy (rptr
, sptr
, size
);
548 /* Advance to the next element. */
552 while (count
[n
] == extent
[n
])
554 /* When we get to the end of a dimension, reset it and
555 increment the next dimension. */
557 /* We could precalculate these products, but this is a
558 less frequently used path so probably not worth it. */
559 sptr
-= sstride
[n
] * extent
[n
];
563 /* Break out of the loop. */
576 /* Add any remaining elements from VECTOR. */
579 n
= GFC_DESCRIPTOR_EXTENT(vector
,0);
580 nelem
= ((rptr
- ret
->base_addr
) / rstride0
);
583 sstride0
= GFC_DESCRIPTOR_STRIDE_BYTES(vector
,0);
587 sptr
= vector
->base_addr
+ sstride0
* nelem
;
591 memcpy (rptr
, sptr
, size
);
599 extern void pack_s (gfc_array_char
*ret
, const gfc_array_char
*array
,
600 const GFC_LOGICAL_4
*, const gfc_array_char
*);
601 export_proto(pack_s
);
604 pack_s (gfc_array_char
*ret
, const gfc_array_char
*array
,
605 const GFC_LOGICAL_4
*mask
, const gfc_array_char
*vector
)
607 pack_s_internal (ret
, array
, mask
, vector
, GFC_DESCRIPTOR_SIZE (array
));
611 extern void pack_s_char (gfc_array_char
*ret
, GFC_INTEGER_4
,
612 const gfc_array_char
*array
, const GFC_LOGICAL_4
*,
613 const gfc_array_char
*, GFC_INTEGER_4
,
615 export_proto(pack_s_char
);
618 pack_s_char (gfc_array_char
*ret
,
619 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
620 const gfc_array_char
*array
, const GFC_LOGICAL_4
*mask
,
621 const gfc_array_char
*vector
, GFC_INTEGER_4 array_length
,
622 GFC_INTEGER_4 vector_length
__attribute__((unused
)))
624 pack_s_internal (ret
, array
, mask
, vector
, array_length
);
628 extern void pack_s_char4 (gfc_array_char
*ret
, GFC_INTEGER_4
,
629 const gfc_array_char
*array
, const GFC_LOGICAL_4
*,
630 const gfc_array_char
*, GFC_INTEGER_4
,
632 export_proto(pack_s_char4
);
635 pack_s_char4 (gfc_array_char
*ret
,
636 GFC_INTEGER_4 ret_length
__attribute__((unused
)),
637 const gfc_array_char
*array
, const GFC_LOGICAL_4
*mask
,
638 const gfc_array_char
*vector
, GFC_INTEGER_4 array_length
,
639 GFC_INTEGER_4 vector_length
__attribute__((unused
)))
641 pack_s_internal (ret
, array
, mask
, vector
,
642 array_length
* sizeof (gfc_char4_t
));