| 1 |
/* Class specific 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_pack_class (gfc_class_array_t *, gfc_class_array_t *, const size_t, |
| 31 |
const int); |
| 32 |
export_proto (internal_pack_class); |
| 33 |
|
| 34 |
/* attr is a bitfield. The bits in use are: |
| 35 |
0 - _len is present. |
| 36 |
*/ |
| 37 |
void |
| 38 |
internal_pack_class (gfc_class_array_t *dest_class, |
| 39 |
gfc_class_array_t *source_class, const size_t size_class, |
| 40 |
const int attr) |
| 41 |
{ |
| 42 |
#define BIT_TEST(mask, bit) (((mask) & (1U << (bit))) == (1U << (bit))) |
| 43 |
|
| 44 |
index_type count[GFC_MAX_DIMENSIONS]; |
| 45 |
index_type extent[GFC_MAX_DIMENSIONS]; |
| 46 |
index_type stride[GFC_MAX_DIMENSIONS]; |
| 47 |
index_type stride0; |
| 48 |
index_type dim; |
| 49 |
index_type ssize; |
| 50 |
index_type dest_stride; |
| 51 |
index_type n; |
| 52 |
const void *src; |
| 53 |
void *dest; |
| 54 |
int packed; |
| 55 |
index_type size; |
| 56 |
gfc_array_void *source_arr; |
| 57 |
gfc_array_void *dest_arr; |
| 58 |
size_t dest_offset; |
| 59 |
bool len_present = BIT_TEST (attr, 0); |
| 60 |
gfc_vtype_generic_t *vtab; |
| 61 |
void (*copyfn) (const void *, void *); |
| 62 |
|
| 63 |
/* Always make sure the dest is initialized. */ |
| 64 |
memcpy (dest_class, source_class, size_class); |
| 65 |
if (source_class->_data.base_addr == NULL) |
| 66 |
return; |
| 67 |
|
| 68 |
source_arr = (gfc_array_void *) &(source_class->_data); |
| 69 |
size = GFC_DESCRIPTOR_SIZE (source_arr); |
| 70 |
dim = GFC_DESCRIPTOR_RANK (source_arr); |
| 71 |
ssize = 1; |
| 72 |
packed = 1; |
| 73 |
for (n = 0; n < dim; n++) |
| 74 |
{ |
| 75 |
count[n] = 0; |
| 76 |
stride[n] = GFC_DESCRIPTOR_STRIDE (source_arr, n); |
| 77 |
extent[n] = GFC_DESCRIPTOR_EXTENT (source_arr, n); |
| 78 |
if (extent[n] <= 0) |
| 79 |
{ |
| 80 |
/* Do nothing. */ |
| 81 |
packed = 1; |
| 82 |
break; |
| 83 |
} |
| 84 |
|
| 85 |
if (ssize != stride[n]) |
| 86 |
packed = 0; |
| 87 |
|
| 88 |
ssize *= extent[n]; |
| 89 |
} |
| 90 |
|
| 91 |
/* When the data is packed already, nothing needs to be done and unpack, will |
| 92 |
quit immediately, because _data is identical and nothing needs to be done. |
| 93 |
*/ |
| 94 |
if (packed) |
| 95 |
return; |
| 96 |
|
| 97 |
/* Allocate storage for the destination. */ |
| 98 |
dest_arr = (gfc_array_void *) &dest_class->_data; |
| 99 |
dest_stride = 1; |
| 100 |
dest_offset = 0; |
| 101 |
for (n = 0; n < dim; ++n) |
| 102 |
{ |
| 103 |
GFC_DESCRIPTOR_LBOUND (dest_arr, n) = 1; |
| 104 |
GFC_DESCRIPTOR_UBOUND (dest_arr, n) = extent[n]; |
| 105 |
GFC_DESCRIPTOR_STRIDE (dest_arr, n) = dest_stride; |
| 106 |
dest_offset -= dest_stride * 1 /* GFC_DESCRIPTOR_LBOUND (dest_arr, n) */; |
| 107 |
dest_stride *= GFC_DESCRIPTOR_EXTENT (dest_arr, n); |
| 108 |
} |
| 109 |
dest_arr->offset = dest_offset; |
| 110 |
dest_arr->base_addr = xmallocarray (ssize, size); |
| 111 |
dest = (void *) dest_arr->base_addr; |
| 112 |
src = source_arr->base_addr; |
| 113 |
stride0 = stride[0] * size; |
| 114 |
/* Can not use the dimension here, because the class may be allocated for |
| 115 |
a higher dimensional array, but only a smaller amount is present. */ |
| 116 |
vtab = *(gfc_vtype_generic_t **) (((void *) source_class) + size_class |
| 117 |
- (len_present ? sizeof (size_t) : 0) |
| 118 |
- sizeof (void *)); /* _vptr */ |
| 119 |
copyfn = vtab->_copy; |
| 120 |
|
| 121 |
while (src) |
| 122 |
{ |
| 123 |
/* Copy the data. */ |
| 124 |
copyfn (src, dest); |
| 125 |
/* Advance to the next element. */ |
| 126 |
dest += size; |
| 127 |
src += stride0; |
| 128 |
count[0]++; |
| 129 |
/* Advance to the next source element. */ |
| 130 |
n = 0; |
| 131 |
while (count[n] == extent[n]) |
| 132 |
{ |
| 133 |
/* When we get to the end of a dimension, reset it and increment |
| 134 |
the next dimension. */ |
| 135 |
count[n] = 0; |
| 136 |
/* We could precalculate these products, but this is a less |
| 137 |
frequently used path so probably not worth it. */ |
| 138 |
src -= stride[n] * extent[n] * size; |
| 139 |
n++; |
| 140 |
if (n == dim) |
| 141 |
{ |
| 142 |
src = NULL; |
| 143 |
break; |
| 144 |
} |
| 145 |
else |
| 146 |
{ |
| 147 |
count[n]++; |
| 148 |
src += stride[n] * size; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |