1 /* Generic helper function for repacking arrays.
2 Copyright (C) 2003-2025 Free Software Foundation, Inc.
3 Contributed by Paul Brook <paul@nowt.org>
5 This file is part of the GNU Fortran 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 3 of the License, or (at your option) any later version.
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
29 extern void internal_unpack (gfc_array_char
*, const void *);
30 export_proto(internal_unpack
);
33 internal_unpack (gfc_array_char
* d
, const void * s
)
35 index_type count
[GFC_MAX_DIMENSIONS
];
36 index_type extent
[GFC_MAX_DIMENSIONS
];
37 index_type stride
[GFC_MAX_DIMENSIONS
];
47 /* This check may be redundant, but do it anyway. */
51 type_size
= GFC_DTYPE_TYPE_SIZE (d
);
54 case GFC_DTYPE_INTEGER_1
:
55 case GFC_DTYPE_LOGICAL_1
:
56 internal_unpack_1 ((gfc_array_i1
*) d
, (const GFC_INTEGER_1
*) s
);
59 case GFC_DTYPE_INTEGER_2
:
60 case GFC_DTYPE_LOGICAL_2
:
61 internal_unpack_2 ((gfc_array_i2
*) d
, (const GFC_INTEGER_2
*) s
);
64 case GFC_DTYPE_INTEGER_4
:
65 case GFC_DTYPE_LOGICAL_4
:
66 internal_unpack_4 ((gfc_array_i4
*) d
, (const GFC_INTEGER_4
*) s
);
69 case GFC_DTYPE_INTEGER_8
:
70 case GFC_DTYPE_LOGICAL_8
:
71 internal_unpack_8 ((gfc_array_i8
*) d
, (const GFC_INTEGER_8
*) s
);
74 #if defined (HAVE_GFC_INTEGER_16)
75 case GFC_DTYPE_INTEGER_16
:
76 case GFC_DTYPE_LOGICAL_16
:
77 internal_unpack_16 ((gfc_array_i16
*) d
, (const GFC_INTEGER_16
*) s
);
81 case GFC_DTYPE_REAL_4
:
82 internal_unpack_r4 ((gfc_array_r4
*) d
, (const GFC_REAL_4
*) s
);
85 case GFC_DTYPE_REAL_8
:
86 internal_unpack_r8 ((gfc_array_r8
*) d
, (const GFC_REAL_8
*) s
);
89 /* FIXME: This here is a hack, which will have to be removed when
90 the array descriptor is reworked. Currently, we don't store the
91 kind value for the type, but only the size. Because on targets with
92 _Float128, we have sizeof(long double) == sizeof(_Float128),
93 we cannot discriminate here and have to fall back to the generic
94 handling (which is suboptimal). */
95 #if !defined(GFC_REAL_16_IS_FLOAT128)
96 # if defined(HAVE_GFC_REAL_10)
97 case GFC_DTYPE_REAL_10
:
98 internal_unpack_r10 ((gfc_array_r10
*) d
, (const GFC_REAL_10
*) s
);
102 # if defined(HAVE_GFC_REAL_16)
103 case GFC_DTYPE_REAL_16
:
104 internal_unpack_r16 ((gfc_array_r16
*) d
, (const GFC_REAL_16
*) s
);
109 case GFC_DTYPE_COMPLEX_4
:
110 internal_unpack_c4 ((gfc_array_c4
*)d
, (const GFC_COMPLEX_4
*)s
);
113 case GFC_DTYPE_COMPLEX_8
:
114 internal_unpack_c8 ((gfc_array_c8
*)d
, (const GFC_COMPLEX_8
*)s
);
117 /* FIXME: This here is a hack, which will have to be removed when
118 the array descriptor is reworked. Currently, we don't store the
119 kind value for the type, but only the size. Because on targets with
120 _Float128, we have sizeof(long double) == sizeof(_Float128),
121 we cannot discriminate here and have to fall back to the generic
122 handling (which is suboptimal). */
123 #if !defined(GFC_REAL_16_IS_FLOAT128)
124 # if defined(HAVE_GFC_COMPLEX_10)
125 case GFC_DTYPE_COMPLEX_10
:
126 internal_unpack_c10 ((gfc_array_c10
*) d
, (const GFC_COMPLEX_10
*) s
);
130 # if defined(HAVE_GFC_COMPLEX_16)
131 case GFC_DTYPE_COMPLEX_16
:
132 internal_unpack_c16 ((gfc_array_c16
*) d
, (const GFC_COMPLEX_16
*) s
);
141 switch (GFC_DESCRIPTOR_SIZE(d
))
144 internal_unpack_1 ((gfc_array_i1
*) d
, (const GFC_INTEGER_1
*) s
);
148 if (GFC_UNALIGNED_2(d
->base_addr
) || GFC_UNALIGNED_2(s
))
152 internal_unpack_2 ((gfc_array_i2
*) d
, (const GFC_INTEGER_2
*) s
);
157 if (GFC_UNALIGNED_4(d
->base_addr
) || GFC_UNALIGNED_4(s
))
161 internal_unpack_4 ((gfc_array_i4
*) d
, (const GFC_INTEGER_4
*) s
);
166 if (GFC_UNALIGNED_8(d
->base_addr
) || GFC_UNALIGNED_8(s
))
170 internal_unpack_8 ((gfc_array_i8
*) d
, (const GFC_INTEGER_8
*) s
);
174 #ifdef HAVE_GFC_INTEGER_16
176 if (GFC_UNALIGNED_16(d
->base_addr
) || GFC_UNALIGNED_16(s
))
180 internal_unpack_16 ((gfc_array_i16
*) d
, (const GFC_INTEGER_16
*) s
);
188 size
= GFC_DESCRIPTOR_SIZE (d
);
190 dim
= GFC_DESCRIPTOR_RANK (d
);
192 for (index_type n
= 0; n
< dim
; n
++)
195 stride
[n
] = GFC_DESCRIPTOR_STRIDE(d
,n
);
196 extent
[n
] = GFC_DESCRIPTOR_EXTENT(d
,n
);
200 if (dsize
== stride
[n
])
210 memcpy (dest
, src
, dsize
* size
);
214 stride0
= stride
[0] * size
;
219 memcpy (dest
, src
, size
);
220 /* Advance to the next element. */
224 /* Advance to the next source element. */
226 while (count
[n
] == extent
[n
])
228 /* When we get to the end of a dimension, reset it and increment
229 the next dimension. */
231 /* We could precalculate these products, but this is a less
232 frequently used path so probably not worth it. */
233 dest
-= stride
[n
] * extent
[n
] * size
;
243 dest
+= stride
[n
] * size
;