1 /* Generic helper function for repacking arrays.
2 Copyright (C) 2003-2013 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"
31 extern void internal_unpack (gfc_array_char
*, const void *);
32 export_proto(internal_unpack
);
35 internal_unpack (gfc_array_char
* d
, const void * s
)
37 index_type count
[GFC_MAX_DIMENSIONS
];
38 index_type extent
[GFC_MAX_DIMENSIONS
];
39 index_type stride
[GFC_MAX_DIMENSIONS
];
50 /* This check may be redundant, but do it anyway. */
54 type_size
= GFC_DTYPE_TYPE_SIZE (d
);
57 case GFC_DTYPE_INTEGER_1
:
58 case GFC_DTYPE_LOGICAL_1
:
59 case GFC_DTYPE_DERIVED_1
:
60 internal_unpack_1 ((gfc_array_i1
*) d
, (const GFC_INTEGER_1
*) s
);
63 case GFC_DTYPE_INTEGER_2
:
64 case GFC_DTYPE_LOGICAL_2
:
65 internal_unpack_2 ((gfc_array_i2
*) d
, (const GFC_INTEGER_2
*) s
);
68 case GFC_DTYPE_INTEGER_4
:
69 case GFC_DTYPE_LOGICAL_4
:
70 internal_unpack_4 ((gfc_array_i4
*) d
, (const GFC_INTEGER_4
*) s
);
73 case GFC_DTYPE_INTEGER_8
:
74 case GFC_DTYPE_LOGICAL_8
:
75 internal_unpack_8 ((gfc_array_i8
*) d
, (const GFC_INTEGER_8
*) s
);
78 #if defined (HAVE_GFC_INTEGER_16)
79 case GFC_DTYPE_INTEGER_16
:
80 case GFC_DTYPE_LOGICAL_16
:
81 internal_unpack_16 ((gfc_array_i16
*) d
, (const GFC_INTEGER_16
*) s
);
85 case GFC_DTYPE_REAL_4
:
86 internal_unpack_r4 ((gfc_array_r4
*) d
, (const GFC_REAL_4
*) s
);
89 case GFC_DTYPE_REAL_8
:
90 internal_unpack_r8 ((gfc_array_r8
*) d
, (const GFC_REAL_8
*) s
);
93 /* FIXME: This here is a hack, which will have to be removed when
94 the array descriptor is reworked. Currently, we don't store the
95 kind value for the type, but only the size. Because on targets with
96 __float128, we have sizeof(logn double) == sizeof(__float128),
97 we cannot discriminate here and have to fall back to the generic
98 handling (which is suboptimal). */
99 #if !defined(GFC_REAL_16_IS_FLOAT128)
100 # if defined(HAVE_GFC_REAL_10)
101 case GFC_DTYPE_REAL_10
:
102 internal_unpack_r10 ((gfc_array_r10
*) d
, (const GFC_REAL_10
*) s
);
106 # if defined(HAVE_GFC_REAL_16)
107 case GFC_DTYPE_REAL_16
:
108 internal_unpack_r16 ((gfc_array_r16
*) d
, (const GFC_REAL_16
*) s
);
113 case GFC_DTYPE_COMPLEX_4
:
114 internal_unpack_c4 ((gfc_array_c4
*)d
, (const GFC_COMPLEX_4
*)s
);
117 case GFC_DTYPE_COMPLEX_8
:
118 internal_unpack_c8 ((gfc_array_c8
*)d
, (const GFC_COMPLEX_8
*)s
);
121 /* FIXME: This here is a hack, which will have to be removed when
122 the array descriptor is reworked. Currently, we don't store the
123 kind value for the type, but only the size. Because on targets with
124 __float128, we have sizeof(logn double) == sizeof(__float128),
125 we cannot discriminate here and have to fall back to the generic
126 handling (which is suboptimal). */
127 #if !defined(GFC_REAL_16_IS_FLOAT128)
128 # if defined(HAVE_GFC_COMPLEX_10)
129 case GFC_DTYPE_COMPLEX_10
:
130 internal_unpack_c10 ((gfc_array_c10
*) d
, (const GFC_COMPLEX_10
*) s
);
134 # if defined(HAVE_GFC_COMPLEX_16)
135 case GFC_DTYPE_COMPLEX_16
:
136 internal_unpack_c16 ((gfc_array_c16
*) d
, (const GFC_COMPLEX_16
*) s
);
141 case GFC_DTYPE_DERIVED_2
:
142 if (GFC_UNALIGNED_2(d
->base_addr
) || GFC_UNALIGNED_2(s
))
146 internal_unpack_2 ((gfc_array_i2
*) d
, (const GFC_INTEGER_2
*) s
);
149 case GFC_DTYPE_DERIVED_4
:
150 if (GFC_UNALIGNED_4(d
->base_addr
) || GFC_UNALIGNED_4(s
))
154 internal_unpack_4 ((gfc_array_i4
*) d
, (const GFC_INTEGER_4
*) s
);
158 case GFC_DTYPE_DERIVED_8
:
159 if (GFC_UNALIGNED_8(d
->base_addr
) || GFC_UNALIGNED_8(s
))
163 internal_unpack_8 ((gfc_array_i8
*) d
, (const GFC_INTEGER_8
*) s
);
167 #ifdef HAVE_GFC_INTEGER_16
168 case GFC_DTYPE_DERIVED_16
:
169 if (GFC_UNALIGNED_16(d
->base_addr
) || GFC_UNALIGNED_16(s
))
173 internal_unpack_16 ((gfc_array_i16
*) d
, (const GFC_INTEGER_16
*) s
);
182 size
= GFC_DESCRIPTOR_SIZE (d
);
184 dim
= GFC_DESCRIPTOR_RANK (d
);
186 for (n
= 0; n
< dim
; n
++)
189 stride
[n
] = GFC_DESCRIPTOR_STRIDE(d
,n
);
190 extent
[n
] = GFC_DESCRIPTOR_EXTENT(d
,n
);
194 if (dsize
== stride
[n
])
204 memcpy (dest
, src
, dsize
* size
);
208 stride0
= stride
[0] * size
;
213 memcpy (dest
, src
, size
);
214 /* Advance to the next element. */
218 /* Advance to the next source element. */
220 while (count
[n
] == extent
[n
])
222 /* When we get to the end of a dimension, reset it and increment
223 the next dimension. */
225 /* We could precalculate these products, but this is a less
226 frequently used path so probably not worth it. */
227 dest
-= stride
[n
] * extent
[n
] * size
;
237 dest
+= stride
[n
] * size
;