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, GFC_LOGICAL_4);
9 export_proto(name`'rtype_qual`_'atype_code);
12 name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
13 atype * const restrict array, GFC_LOGICAL_4 back)
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.rank = 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,
68 /* Implementation start. */
70 define(FINISH_FOREACH_FUNCTION,
71 ` /* Implementation end. */
72 /* Advance to the next element. */
75 while (++count[0] != extent[0]);
79 /* When we get to the end of a dimension, reset it and increment
80 the next dimension. */
82 /* We could precalculate these products, but this is a less
83 frequently used path so probably not worth it. */
84 base -= sstride[n] * extent[n];
88 /* Break out of the loop. */
98 while (count[n] == extent[n]);
102 define(START_MASKED_FOREACH_FUNCTION,
104 extern void `m'name`'rtype_qual`_'atype_code (rtype * const restrict,
105 atype * const restrict, gfc_array_l1 * const restrict,
107 export_proto(`m'name`'rtype_qual`_'atype_code);
110 `m'name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
111 atype * const restrict array,
112 gfc_array_l1 * const restrict mask, GFC_LOGICAL_4 back)
114 index_type count[GFC_MAX_DIMENSIONS];
115 index_type extent[GFC_MAX_DIMENSIONS];
116 index_type sstride[GFC_MAX_DIMENSIONS];
117 index_type mstride[GFC_MAX_DIMENSIONS];
120 const atype_name *base;
121 GFC_LOGICAL_1 *mbase;
129 name`'rtype_qual`_'atype_code (retarray, array, back);
133 rank = GFC_DESCRIPTOR_RANK (array);
135 runtime_error ("Rank of array needs to be > 0");
137 if (retarray->base_addr == NULL)
139 GFC_DIMENSION_SET(retarray->dim[0], 0, rank - 1, 1);
140 retarray->dtype.rank = 1;
141 retarray->offset = 0;
142 retarray->base_addr = xmallocarray (rank, sizeof (rtype_name));
146 if (unlikely (compile_options.bounds_check))
149 bounds_iforeach_return ((array_t *) retarray, (array_t *) array,
151 bounds_equal_extents ((array_t *) mask, (array_t *) array,
152 "MASK argument", "u_name");
156 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
158 mbase = mask->base_addr;
160 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
161 #ifdef HAVE_GFC_LOGICAL_16
165 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
167 runtime_error ("Funny sized logical array");
169 dstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
170 dest = retarray->base_addr;
171 for (n = 0; n < rank; n++)
173 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
174 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
175 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
179 /* Set the return value. */
180 for (n = 0; n < rank; n++)
181 dest[n * dstride] = 0;
186 base = array->base_addr;
188 /* Initialize the return value. */
189 for (n = 0; n < rank; n++)
190 dest[n * dstride] = 0;
193 define(START_MASKED_FOREACH_BLOCK, `START_FOREACH_BLOCK')dnl
194 define(FINISH_MASKED_FOREACH_FUNCTION,
195 ` /* Implementation end. */
196 /* Advance to the next element. */
200 while (++count[0] != extent[0]);
204 /* When we get to the end of a dimension, reset it and increment
205 the next dimension. */
207 /* We could precalculate these products, but this is a less
208 frequently used path so probably not worth it. */
209 base -= sstride[n] * extent[n];
210 mbase -= mstride[n] * extent[n];
214 /* Break out of the loop. */
225 while (count[n] == extent[n]);
229 define(FOREACH_FUNCTION,
230 `START_FOREACH_FUNCTION
234 FINISH_FOREACH_FUNCTION')dnl
235 define(MASKED_FOREACH_FUNCTION,
236 `START_MASKED_FOREACH_FUNCTION
238 START_MASKED_FOREACH_BLOCK
240 FINISH_MASKED_FOREACH_FUNCTION')dnl
241 define(SCALAR_FOREACH_FUNCTION,
243 extern void `s'name`'rtype_qual`_'atype_code (rtype * const restrict,
244 atype * const restrict, GFC_LOGICAL_4 *, GFC_LOGICAL_4);
245 export_proto(`s'name`'rtype_qual`_'atype_code);
248 `s'name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
249 atype * const restrict array,
250 GFC_LOGICAL_4 * mask, GFC_LOGICAL_4 back)
257 if (mask == NULL || *mask)
259 name`'rtype_qual`_'atype_code (retarray, array, back);
263 rank = GFC_DESCRIPTOR_RANK (array);
266 runtime_error ("Rank of array needs to be > 0");
268 if (retarray->base_addr == NULL)
270 GFC_DIMENSION_SET(retarray->dim[0], 0, rank-1, 1);
271 retarray->dtype.rank = 1;
272 retarray->offset = 0;
273 retarray->base_addr = xmallocarray (rank, sizeof (rtype_name));
275 else if (unlikely (compile_options.bounds_check))
277 bounds_iforeach_return ((array_t *) retarray, (array_t *) array,
281 dstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
282 dest = retarray->base_addr;
283 for (n = 0; n<rank; n++)
284 dest[n * dstride] = $1 ;