| 1 |
/* Generic implementation of the reduce intrinsic |
| 2 |
Copyright (C) 2002-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Paul Thomas <[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 |
Ligbfortran is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WarrayANTY; without even the implied warrayanty 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 |
#include <stdio.h> |
| 29 |
|
| 30 |
typedef GFC_FULL_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, char) parray; |
| 31 |
|
| 32 |
extern void reduce (parray *, parray *, |
| 33 |
void (*operation) (void *, void *, void *), |
| 34 |
GFC_INTEGER_4 *, gfc_array_l4 *, void *, void *); |
| 35 |
export_proto (reduce); |
| 36 |
|
| 37 |
void |
| 38 |
reduce (parray *ret, |
| 39 |
parray *array, |
| 40 |
void (*operation) (void *, void *, void *), |
| 41 |
GFC_INTEGER_4 *dim, |
| 42 |
gfc_array_l4 *mask, |
| 43 |
void *identity, |
| 44 |
void *ordered __attribute__ ((unused))) |
| 45 |
{ |
| 46 |
GFC_LOGICAL_4 maskR = 0; |
| 47 |
void *array_ptr; |
| 48 |
void *buffer_ptr; |
| 49 |
void *zero; |
| 50 |
void *buffer; |
| 51 |
void *res; |
| 52 |
index_type ext0, ext1, ext2; |
| 53 |
index_type str0, str1, str2; |
| 54 |
index_type idx0, idx1, idx2; |
| 55 |
index_type dimen, dimen_m1, ldx, ext, str; |
| 56 |
bool started; |
| 57 |
bool masked = false; |
| 58 |
bool dim_present = dim != NULL; |
| 59 |
bool mask_present = mask != NULL; |
| 60 |
bool identity_present = identity != NULL; |
| 61 |
bool scalar_result; |
| 62 |
int i, j; |
| 63 |
int array_rank = (int)GFC_DESCRIPTOR_RANK (array); |
| 64 |
size_t elem_len = GFC_DESCRIPTOR_SIZE (array); |
| 65 |
|
| 66 |
/* The standard mandates that OPERATION is a pure scalar function such that in |
| 67 |
the reduction below: |
| 68 |
|
| 69 |
*buffer_ptr = OPERATION (*buffer_ptr, array(idx1, idx2, idx3)) |
| 70 |
|
| 71 |
To make this type agnostic, the front end builds a wrapper, that puts the |
| 72 |
assignment within a subroutine and transforms it into a pointer operation: |
| 73 |
|
| 74 |
operation (buffer_ptr, &array(idx1, idx2, idx3), buffer_ptr) |
| 75 |
|
| 76 |
The wrapper also detects the presence or not of the second argument. If it |
| 77 |
is not present, the wrapper effects *third_arg = *first_arg. |
| 78 |
|
| 79 |
The only information needed about the type of array is its element size. In |
| 80 |
both modes, the wrapper takes care of allocatable components correctly, |
| 81 |
which is why the second mode is used to fill the result elements. */ |
| 82 |
|
| 83 |
if (dim_present) |
| 84 |
{ |
| 85 |
if ((*dim < 1) || (*dim > (GFC_INTEGER_4)array_rank)) |
| 86 |
runtime_error ("Mismatch between DIM and the rank of ARRAY in the " |
| 87 |
"REDUCE intrinsic (%d/%d)", (int)*dim, array_rank); |
| 88 |
dimen = (index_type) *dim; |
| 89 |
} |
| 90 |
else |
| 91 |
dimen = 1; |
| 92 |
dimen_m1 = dimen -1; |
| 93 |
|
| 94 |
/* Set up the shape and strides for the reduction. This is made relatively |
| 95 |
painless by the use of pointer arithmetic throughout (except for MASK, |
| 96 |
whose type is known. */ |
| 97 |
ext0 = ext1 = ext2 = 1; |
| 98 |
str0 = str1 = str2 = 1; |
| 99 |
|
| 100 |
scalar_result = (!dim_present && array_rank > 1) || array_rank == 1; |
| 101 |
|
| 102 |
j = 0; |
| 103 |
for (i = 0; i < array_rank; i++) |
| 104 |
{ |
| 105 |
/* Obtain the shape of the reshaped ARRAY. */ |
| 106 |
ext = GFC_DESCRIPTOR_EXTENT (array,i); |
| 107 |
str = GFC_DESCRIPTOR_STRIDE (array,i); |
| 108 |
|
| 109 |
if (masked && (ext != GFC_DESCRIPTOR_EXTENT (mask, i))) |
| 110 |
{ |
| 111 |
int mext = (int)GFC_DESCRIPTOR_EXTENT (mask, i); |
| 112 |
runtime_error ("shape mismatch between ARRAY and MASK in the REDUCE " |
| 113 |
"intrinsic (%zd/%d)", ext, mext); |
| 114 |
} |
| 115 |
|
| 116 |
if (scalar_result) |
| 117 |
{ |
| 118 |
ext1 *= ext; |
| 119 |
continue; |
| 120 |
} |
| 121 |
else if (i < (int)dimen_m1) |
| 122 |
ext0 *= ext; |
| 123 |
else if (i == (int)dimen_m1) |
| 124 |
ext1 = ext; |
| 125 |
else |
| 126 |
ext2 *= ext; |
| 127 |
|
| 128 |
/* The dimensions of the return array. */ |
| 129 |
if (i != (int)dimen_m1) |
| 130 |
{ |
| 131 |
str = GFC_DESCRIPTOR_STRIDE (array, j); |
| 132 |
GFC_DIMENSION_SET (ret->dim[j], 0, ext - 1, str); |
| 133 |
j++; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if (!scalar_result) |
| 138 |
{ |
| 139 |
str1 = GFC_DESCRIPTOR_STRIDE (array, dimen_m1); |
| 140 |
if (dimen < array_rank) |
| 141 |
str2 = GFC_DESCRIPTOR_STRIDE (array, dimen); |
| 142 |
else |
| 143 |
str2 = 1; |
| 144 |
} |
| 145 |
|
| 146 |
/* Allocate the result data, the result buffer and zero. */ |
| 147 |
if (ret->base_addr == NULL) |
| 148 |
ret->base_addr = calloc ((size_t)(ext0 * ext2), elem_len); |
| 149 |
buffer = calloc (1, elem_len); |
| 150 |
zero = calloc (1, elem_len); |
| 151 |
|
| 152 |
/* Now loop over the first and third dimensions of the reshaped ARRAY. */ |
| 153 |
for (idx0 = 0; idx0 < ext0; idx0++) |
| 154 |
{ |
| 155 |
for (idx2 = 0; idx2 < ext2; idx2++) |
| 156 |
{ |
| 157 |
ldx = idx0 * str0 + idx2 * str2; |
| 158 |
if (mask_present) |
| 159 |
maskR = mask->base_addr[ldx]; |
| 160 |
|
| 161 |
started = (mask_present && maskR) || !mask_present; |
| 162 |
|
| 163 |
buffer_ptr = array->base_addr |
| 164 |
+ (size_t)((idx0 * str0 + idx2 * str2) * elem_len); |
| 165 |
|
| 166 |
/* Start the iteration over the second dimension of ARRAY. */ |
| 167 |
for (idx1 = 1; idx1 < ext1; idx1++) |
| 168 |
{ |
| 169 |
/* If masked, cycle until after first element that is not masked |
| 170 |
out. Then set 'started' and cycle so that this becomes the |
| 171 |
first element in the reduction. */ |
| 172 |
ldx = idx0 * str0 + idx1 * str1 + idx2 * str2; |
| 173 |
if (mask_present) |
| 174 |
maskR = mask->base_addr[ldx]; |
| 175 |
|
| 176 |
array_ptr = array->base_addr |
| 177 |
+ (size_t)((idx0 * str0 + idx1 * str1 |
| 178 |
+ idx2 * str2) * elem_len); |
| 179 |
if (!started) |
| 180 |
{ |
| 181 |
if (mask_present && maskR) |
| 182 |
started = true; |
| 183 |
buffer_ptr = array_ptr; |
| 184 |
continue; |
| 185 |
} |
| 186 |
|
| 187 |
/* Call the operation, if not masked out, with the previous |
| 188 |
element or the buffer and current element as arguments. The |
| 189 |
result is stored in the buffer and the buffer_ptr set to |
| 190 |
point to buffer, instead of the previous array element. */ |
| 191 |
if ((mask_present && maskR) || !mask_present) |
| 192 |
{ |
| 193 |
operation (buffer_ptr, array_ptr, buffer); |
| 194 |
buffer_ptr = buffer; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
/* Now the result of the iteration is transferred to the returned |
| 199 |
result. If this result element is empty emit an error or, if |
| 200 |
available, set to identity. Note that str1 is paired with idx2 |
| 201 |
here because the result skips a dimension. */ |
| 202 |
res = ret->base_addr + (size_t)((idx0 * str0 + idx2 * str1) * elem_len); |
| 203 |
if (started) |
| 204 |
{ |
| 205 |
operation (buffer_ptr, NULL, res); |
| 206 |
operation (zero, NULL, buffer); |
| 207 |
} |
| 208 |
else |
| 209 |
{ |
| 210 |
if (!identity_present) |
| 211 |
runtime_error ("Empty column and no IDENTITY in REDUCE " |
| 212 |
"intrinsic"); |
| 213 |
else |
| 214 |
operation (identity, NULL, res); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
free (zero); |
| 219 |
free (buffer); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
extern void * reduce_scalar (parray *, |
| 224 |
void (*operation) (void *, void *, void *), |
| 225 |
GFC_INTEGER_4 *, gfc_array_l4 *, void *, void *); |
| 226 |
export_proto (reduce_scalar); |
| 227 |
|
| 228 |
void * |
| 229 |
reduce_scalar (parray *array, |
| 230 |
void (*operation) (void *, void *, void *), |
| 231 |
GFC_INTEGER_4 *dim, |
| 232 |
gfc_array_l4 *mask, |
| 233 |
void *identity, |
| 234 |
void *ordered) |
| 235 |
{ |
| 236 |
parray ret; |
| 237 |
ret.base_addr = NULL; |
| 238 |
ret.dtype.rank = 0; |
| 239 |
reduce (&ret, array, operation, dim, mask, identity, ordered); |
| 240 |
return (void *)ret.base_addr; |
| 241 |
} |
| 242 |
|
| 243 |
extern void reduce_c (parray *, gfc_charlen_type, parray *, |
| 244 |
void (*operation) (void *, void *, void *), |
| 245 |
GFC_INTEGER_4 *, gfc_array_l4 *, void *, void *, |
| 246 |
gfc_charlen_type, gfc_charlen_type); |
| 247 |
export_proto (reduce_c); |
| 248 |
|
| 249 |
void |
| 250 |
reduce_c (parray *ret, |
| 251 |
gfc_charlen_type ret_strlen __attribute__ ((unused)), |
| 252 |
parray *array, |
| 253 |
void (*operation) (void *, void *, void *), |
| 254 |
GFC_INTEGER_4 *dim, |
| 255 |
gfc_array_l4 *mask, |
| 256 |
void *identity, |
| 257 |
void *ordered, |
| 258 |
gfc_charlen_type array_strlen __attribute__ ((unused)), |
| 259 |
gfc_charlen_type identity_strlen __attribute__ ((unused))) |
| 260 |
{ |
| 261 |
/* The frontend constraints make string length checking redundant. Also, the |
| 262 |
scalar symbol is flagged to be allocatable in trans-intrinsic.cc so that |
| 263 |
gfc_conv_procedure_call does the necessary allocation/deallocation. */ |
| 264 |
reduce (ret, array, operation, dim, mask, identity, ordered); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
extern void reduce_scalar_c (void *, gfc_charlen_type, parray *, |
| 269 |
void (*operation) (void *, void *, void *), |
| 270 |
GFC_INTEGER_4 *, gfc_array_l4 *, void *, void *, |
| 271 |
gfc_charlen_type, gfc_charlen_type); |
| 272 |
export_proto (reduce_scalar_c); |
| 273 |
|
| 274 |
|
| 275 |
void |
| 276 |
reduce_scalar_c (void *res, |
| 277 |
gfc_charlen_type res_strlen __attribute__ ((unused)), |
| 278 |
parray *array, |
| 279 |
void (*operation) (void *, void *, void *), |
| 280 |
GFC_INTEGER_4 *dim, |
| 281 |
gfc_array_l4 *mask, |
| 282 |
void *identity, |
| 283 |
void *ordered, |
| 284 |
gfc_charlen_type array_strlen __attribute__ ((unused)), |
| 285 |
gfc_charlen_type identity_strlen __attribute__ ((unused))) |
| 286 |
{ |
| 287 |
parray ret; |
| 288 |
ret.base_addr = NULL; |
| 289 |
ret.dtype.rank = 0; |
| 290 |
/* The frontend constraints make string length checking redundant. */ |
| 291 |
reduce (&ret, array, operation, dim, mask, identity, ordered); |
| 292 |
if (res) |
| 293 |
{ |
| 294 |
memcpy (res, ret.base_addr, GFC_DESCRIPTOR_SIZE (array)); |
| 295 |
if (ret.base_addr) free (ret.base_addr); |
| 296 |
} |
| 297 |
else |
| 298 |
res = ret.base_addr; |
| 299 |
} |