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 95 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 * retarray, atype *array);
8 export_proto(name`'rtype_qual`_'atype_code);
11 name`'rtype_qual`_'atype_code (rtype * retarray, atype *array)
13 index_type count[GFC_MAX_DIMENSIONS];
14 index_type extent[GFC_MAX_DIMENSIONS];
15 index_type sstride[GFC_MAX_DIMENSIONS];
22 rank = GFC_DESCRIPTOR_RANK (array);
24 runtime_error ("Rank of array needs to be > 0");
26 if (retarray->data == NULL)
28 retarray->dim[0].lbound = 0;
29 retarray->dim[0].ubound = rank-1;
30 retarray->dim[0].stride = 1;
31 retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
33 retarray->data = internal_malloc_size (sizeof (rtype_name) * rank);
37 if (GFC_DESCRIPTOR_RANK (retarray) != 1)
38 runtime_error ("rank of return array does not equal 1");
40 if (retarray->dim[0].ubound + 1 - retarray->dim[0].lbound != rank)
41 runtime_error ("dimension of return array incorrect");
43 if (retarray->dim[0].stride == 0)
44 retarray->dim[0].stride = 1;
47 /* TODO: It should be a front end job to correctly set the strides. */
49 if (array->dim[0].stride == 0)
50 array->dim[0].stride = 1;
52 dstride = retarray->dim[0].stride;
53 dest = retarray->data;
54 for (n = 0; n < rank; n++)
56 sstride[n] = array->dim[n].stride;
57 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
61 /* Set the return value. */
62 for (n = 0; n < rank; n++)
63 dest[n * dstride] = 0;
70 /* Initialize the return value. */
71 for (n = 0; n < rank; n++)
72 dest[n * dstride] = 1;
75 define(START_FOREACH_BLOCK,
79 /* Implementation start. */
81 define(FINISH_FOREACH_FUNCTION,
82 ` /* Implementation end. */
84 /* Advance to the next element. */
88 while (count[n] == extent[n])
90 /* When we get to the end of a dimension, reset it and increment
91 the next dimension. */
93 /* We could precalculate these products, but this is a less
94 frequently used path so proabably not worth it. */
95 base -= sstride[n] * extent[n];
99 /* Break out of the loop. */
112 define(START_MASKED_FOREACH_FUNCTION,
114 extern void `m'name`'rtype_qual`_'atype_code (rtype *, atype *, gfc_array_l4 *);
115 export_proto(`m'name`'rtype_qual`_'atype_code);
118 `m'name`'rtype_qual`_'atype_code (rtype * retarray, atype *array,
121 index_type count[GFC_MAX_DIMENSIONS];
122 index_type extent[GFC_MAX_DIMENSIONS];
123 index_type sstride[GFC_MAX_DIMENSIONS];
124 index_type mstride[GFC_MAX_DIMENSIONS];
128 GFC_LOGICAL_4 *mbase;
132 rank = GFC_DESCRIPTOR_RANK (array);
134 runtime_error ("Rank of array needs to be > 0");
136 if (retarray->data == NULL)
138 retarray->dim[0].lbound = 0;
139 retarray->dim[0].ubound = rank-1;
140 retarray->dim[0].stride = 1;
141 retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
142 retarray->offset = 0;
143 retarray->data = internal_malloc_size (sizeof (rtype_name) * rank);
147 if (GFC_DESCRIPTOR_RANK (retarray) != 1)
148 runtime_error ("rank of return array does not equal 1");
150 if (retarray->dim[0].ubound + 1 - retarray->dim[0].lbound != rank)
151 runtime_error ("dimension of return array incorrect");
153 if (retarray->dim[0].stride == 0)
154 retarray->dim[0].stride = 1;
157 /* TODO: It should be a front end job to correctly set the strides. */
159 if (array->dim[0].stride == 0)
160 array->dim[0].stride = 1;
162 if (mask->dim[0].stride == 0)
163 mask->dim[0].stride = 1;
165 dstride = retarray->dim[0].stride;
166 dest = retarray->data;
167 for (n = 0; n < rank; n++)
169 sstride[n] = array->dim[n].stride;
170 mstride[n] = mask->dim[n].stride;
171 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
175 /* Set the return value. */
176 for (n = 0; n < rank; n++)
177 dest[n * dstride] = 0;
185 if (GFC_DESCRIPTOR_SIZE (mask) != 4)
187 /* This allows the same loop to be used for all logical types. */
188 assert (GFC_DESCRIPTOR_SIZE (mask) == 8);
189 for (n = 0; n < rank; n++)
191 mbase = (GFOR_POINTER_L8_TO_L4 (mbase));
195 /* Initialize the return value. */
196 for (n = 0; n < rank; n++)
197 dest[n * dstride] = 1;
200 define(START_MASKED_FOREACH_BLOCK, `START_FOREACH_BLOCK')dnl
201 define(FINISH_MASKED_FOREACH_FUNCTION,
202 ` /* Implementation end. */
204 /* Advance to the next element. */
209 while (count[n] == extent[n])
211 /* When we get to the end of a dimension, reset it and increment
212 the next dimension. */
214 /* We could precalculate these products, but this is a less
215 frequently used path so proabably not worth it. */
216 base -= sstride[n] * extent[n];
217 mbase -= mstride[n] * extent[n];
221 /* Break out of the loop. */
235 define(FOREACH_FUNCTION,
236 `START_FOREACH_FUNCTION
240 FINISH_FOREACH_FUNCTION')dnl
241 define(MASKED_FOREACH_FUNCTION,
242 `START_MASKED_FOREACH_FUNCTION
244 START_MASKED_FOREACH_BLOCK
246 FINISH_MASKED_FOREACH_FUNCTION')dnl