1 dnl Support macro file for intrinsic functions.
2 dnl Contains the generic sections of the array functions.
3 dnl This file is part of the GNU Fortran Runtime Library (libgfortran)
4 dnl Distributed under the GNU GPL with exception. See COPYING for details.
5 define(START_FOREACH_FUNCTION,
7 extern void name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
8 atype * const restrict array);
9 export_proto(name`'rtype_qual`_'atype_code);
12 name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
13 atype * const restrict array)
15 index_type count[GFC_MAX_DIMENSIONS];
16 index_type extent[GFC_MAX_DIMENSIONS];
17 index_type sstride[GFC_MAX_DIMENSIONS];
19 const atype_name *base;
20 rtype_name * restrict dest;
24 rank = GFC_DESCRIPTOR_RANK (array);
26 runtime_error ("Rank of array needs to be > 0");
28 if (retarray->base_addr == NULL)
30 GFC_DIMENSION_SET(retarray->dim[0], 0, rank-1, 1);
31 retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
33 retarray->base_addr = xmallocarray (rank, sizeof (rtype_name));
37 if (unlikely (compile_options.bounds_check))
38 bounds_iforeach_return ((array_t *) retarray, (array_t *) array,
42 dstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
43 dest = retarray->base_addr;
44 for (n = 0; n < rank; n++)
46 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
47 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
51 /* Set the return value. */
52 for (n = 0; n < rank; n++)
53 dest[n * dstride] = 0;
58 base = array->base_addr;
60 /* Initialize the return value. */
61 for (n = 0; n < rank; n++)
62 dest[n * dstride] = 1;
65 define(START_FOREACH_BLOCK,
70 /* Implementation start. */
72 define(FINISH_FOREACH_FUNCTION,
73 ` /* Implementation end. */
74 /* Advance to the next element. */
77 while (++count[0] != extent[0]);
81 /* When we get to the end of a dimension, reset it and increment
82 the next dimension. */
84 /* We could precalculate these products, but this is a less
85 frequently used path so probably not worth it. */
86 base -= sstride[n] * extent[n];
90 /* Break out of the loop. */
100 while (count[n] == extent[n]);
104 define(START_MASKED_FOREACH_FUNCTION,
106 extern void `m'name`'rtype_qual`_'atype_code (rtype * const restrict,
107 atype * const restrict, gfc_array_l1 * const restrict);
108 export_proto(`m'name`'rtype_qual`_'atype_code);
111 `m'name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
112 atype * const restrict array,
113 gfc_array_l1 * const restrict mask)
115 index_type count[GFC_MAX_DIMENSIONS];
116 index_type extent[GFC_MAX_DIMENSIONS];
117 index_type sstride[GFC_MAX_DIMENSIONS];
118 index_type mstride[GFC_MAX_DIMENSIONS];
121 const atype_name *base;
122 GFC_LOGICAL_1 *mbase;
127 rank = GFC_DESCRIPTOR_RANK (array);
129 runtime_error ("Rank of array needs to be > 0");
131 if (retarray->base_addr == NULL)
133 GFC_DIMENSION_SET(retarray->dim[0], 0, rank - 1, 1);
134 retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
135 retarray->offset = 0;
136 retarray->base_addr = xmallocarray (rank, sizeof (rtype_name));
140 if (unlikely (compile_options.bounds_check))
143 bounds_iforeach_return ((array_t *) retarray, (array_t *) array,
145 bounds_equal_extents ((array_t *) mask, (array_t *) array,
146 "MASK argument", "u_name");
150 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
152 mbase = mask->base_addr;
154 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
155 #ifdef HAVE_GFC_LOGICAL_16
159 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
161 runtime_error ("Funny sized logical array");
163 dstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
164 dest = retarray->base_addr;
165 for (n = 0; n < rank; n++)
167 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
168 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
169 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
173 /* Set the return value. */
174 for (n = 0; n < rank; n++)
175 dest[n * dstride] = 0;
180 base = array->base_addr;
182 /* Initialize the return value. */
183 for (n = 0; n < rank; n++)
184 dest[n * dstride] = 0;
187 define(START_MASKED_FOREACH_BLOCK, `START_FOREACH_BLOCK')dnl
188 define(FINISH_MASKED_FOREACH_FUNCTION,
189 ` /* Implementation end. */
190 /* Advance to the next element. */
194 while (++count[0] != extent[0]);
198 /* When we get to the end of a dimension, reset it and increment
199 the next dimension. */
201 /* We could precalculate these products, but this is a less
202 frequently used path so probably not worth it. */
203 base -= sstride[n] * extent[n];
204 mbase -= mstride[n] * extent[n];
208 /* Break out of the loop. */
219 while (count[n] == extent[n]);
223 define(FOREACH_FUNCTION,
224 `START_FOREACH_FUNCTION
228 FINISH_FOREACH_FUNCTION')dnl
229 define(MASKED_FOREACH_FUNCTION,
230 `START_MASKED_FOREACH_FUNCTION
232 START_MASKED_FOREACH_BLOCK
234 FINISH_MASKED_FOREACH_FUNCTION')dnl
235 define(SCALAR_FOREACH_FUNCTION,
237 extern void `s'name`'rtype_qual`_'atype_code (rtype * const restrict,
238 atype * const restrict, GFC_LOGICAL_4 *);
239 export_proto(`s'name`'rtype_qual`_'atype_code);
242 `s'name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
243 atype * const restrict array,
244 GFC_LOGICAL_4 * mask)
253 name`'rtype_qual`_'atype_code (retarray, array);
257 rank = GFC_DESCRIPTOR_RANK (array);
260 runtime_error ("Rank of array needs to be > 0");
262 if (retarray->base_addr == NULL)
264 GFC_DIMENSION_SET(retarray->dim[0], 0, rank-1, 1);
265 retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
266 retarray->offset = 0;
267 retarray->base_addr = xmallocarray (rank, sizeof (rtype_name));
269 else if (unlikely (compile_options.bounds_check))
271 bounds_iforeach_return ((array_t *) retarray, (array_t *) array,
275 dstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
276 dest = retarray->base_addr;
277 for (n = 0; n<rank; n++)
278 dest[n * dstride] = $1 ;