Debian Codegraph @

libgfortran/runtime/in_unpack_class.c

raw
1 /* Class helper function for repacking arrays.
2 Copyright (C) 2003-2025 Free Software Foundation, Inc.
3 Contributed by Paul Brook <[email protected]>
4
5 This file is part of the GNU Fortran runtime library (libgfortran).
6
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.
11
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.
16
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.
20
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/>. */
25
26 #include "libgfortran.h"
27 #include <string.h>
28
29 extern void
30 internal_unpack_class (gfc_class_array_t *, gfc_class_array_t *, const size_t,
31 const int);
32 export_proto (internal_unpack_class);
33
34 void
35 internal_unpack_class (gfc_class_array_t *dest_class,
36 gfc_class_array_t *source_class, const size_t size_class,
37 const int attr)
38 {
39 #define BIT_TEST(mask, bit) (((mask) & (1U << (bit))) == (1U << (bit)))
40
41 index_type count[GFC_MAX_DIMENSIONS];
42 index_type extent[GFC_MAX_DIMENSIONS];
43 index_type stride[GFC_MAX_DIMENSIONS];
44 index_type stride0;
45 index_type dim;
46 index_type dsize;
47 void *dest;
48 const void *src;
49 index_type size;
50 const gfc_array_void *src_arr;
51 gfc_array_void *dest_arr;
52 bool len_present = BIT_TEST (attr, 0);
53 gfc_vtype_generic_t *vtab;
54 void (*copyfn) (const void *, void *);
55
56 /* This check may be redundant, but do it anyway. */
57 if (!source_class || !dest_class || !source_class->_data.base_addr
58 || !dest_class->_data.base_addr)
59 return;
60
61 dest_arr = (gfc_array_void *) &(dest_class->_data);
62 dest = dest_arr->base_addr;
63 size = GFC_DESCRIPTOR_SIZE (dest_arr);
64 dim = GFC_DESCRIPTOR_RANK (dest_arr);
65 dsize = 1;
66 for (index_type n = 0; n < dim; n++)
67 {
68 count[n] = 0;
69 stride[n] = GFC_DESCRIPTOR_STRIDE (dest_arr, n);
70 extent[n] = GFC_DESCRIPTOR_EXTENT (dest_arr, n);
71 if (extent[n] <= 0)
72 return;
73
74 if (dsize == stride[n])
75 dsize *= extent[n];
76 else
77 dsize = 0;
78 }
79
80 src_arr = (gfc_array_void *) &source_class->_data;
81 src = src_arr->base_addr;
82
83 vtab = *(gfc_vtype_generic_t **) (((void *) source_class) + size_class
84 - (len_present ? sizeof (size_t) : 0)
85 - sizeof (void *)); /* _vptr */
86 copyfn = vtab->_copy;
87
88 if (dsize != 0)
89 {
90 for (index_type n = 0; n < dsize; ++n)
91 {
92 copyfn (src, dest);
93 src += size;
94 dest += size;
95 }
96 free (src_arr->base_addr);
97 return;
98 }
99
100 stride0 = stride[0] * size;
101
102 while (dest)
103 {
104 /* Copy the data. */
105 copyfn (src, dest);
106 /* Advance to the next element. */
107 src += size;
108 dest += stride0;
109 count[0]++;
110 /* Advance to the next source element. */
111 index_type n = 0;
112 while (count[n] == extent[n])
113 {
114 /* When we get to the end of a dimension, reset it and increment
115 the next dimension. */
116 count[n] = 0;
117 /* We could precalculate these products, but this is a less
118 frequently used path so probably not worth it. */
119 dest -= stride[n] * extent[n] * size;
120 n++;
121 if (n == dim)
122 {
123 dest = NULL;
124 break;
125 }
126 else
127 {
128 count[n]++;
129 dest += stride[n] * size;
130 }
131 }
132 }
133 free (src_arr->base_addr);
134 }