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;
46 if (array->dim[0].stride == 0)
47 array->dim[0].stride = 1;
49 dstride = retarray->dim[0].stride;
50 dest = retarray->data;
51 for (n = 0; n < rank; n++)
53 sstride[n] = array->dim[n].stride;
54 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
58 /* Set the return value. */
59 for (n = 0; n < rank; n++)
60 dest[n * dstride] = 0;
67 /* Initialize the return value. */
68 for (n = 0; n < rank; n++)
69 dest[n * dstride] = 1;
72 define(START_FOREACH_BLOCK,
76 /* Implementation start. */
78 define(FINISH_FOREACH_FUNCTION,
79 ` /* Implementation end. */
81 /* Advance to the next element. */
85 while (count[n] == extent[n])
87 /* When we get to the end of a dimension, reset it and increment
88 the next dimension. */
90 /* We could precalculate these products, but this is a less
91 frequently used path so proabably not worth it. */
92 base -= sstride[n] * extent[n];
96 /* Break out of the loop. */
109 define(START_MASKED_FOREACH_FUNCTION,
111 extern void `m'name`'rtype_qual`_'atype_code (rtype *, atype *, gfc_array_l4 *);
112 export_proto(`m'name`'rtype_qual`_'atype_code);
115 `m'name`'rtype_qual`_'atype_code (rtype * retarray, atype *array,
118 index_type count[GFC_MAX_DIMENSIONS];
119 index_type extent[GFC_MAX_DIMENSIONS];
120 index_type sstride[GFC_MAX_DIMENSIONS];
121 index_type mstride[GFC_MAX_DIMENSIONS];
125 GFC_LOGICAL_4 *mbase;
129 rank = GFC_DESCRIPTOR_RANK (array);
131 runtime_error ("Rank of array needs to be > 0");
133 if (retarray->data == NULL)
135 retarray->dim[0].lbound = 0;
136 retarray->dim[0].ubound = rank-1;
137 retarray->dim[0].stride = 1;
138 retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
140 retarray->data = internal_malloc_size (sizeof (rtype_name) * rank);
144 if (GFC_DESCRIPTOR_RANK (retarray) != 1)
145 runtime_error ("rank of return array does not equal 1");
147 if (retarray->dim[0].ubound + 1 - retarray->dim[0].lbound != rank)
148 runtime_error ("dimension of return array incorrect");
150 if (retarray->dim[0].stride == 0)
151 retarray->dim[0].stride = 1;
154 if (array->dim[0].stride == 0)
155 array->dim[0].stride = 1;
157 dstride = retarray->dim[0].stride;
158 dest = retarray->data;
159 for (n = 0; n < rank; n++)
161 sstride[n] = array->dim[n].stride;
162 mstride[n] = mask->dim[n].stride;
163 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
167 /* Set the return value. */
168 for (n = 0; n < rank; n++)
169 dest[n * dstride] = 0;
177 if (GFC_DESCRIPTOR_SIZE (mask) != 4)
179 /* This allows the same loop to be used for all logical types. */
180 assert (GFC_DESCRIPTOR_SIZE (mask) == 8);
181 for (n = 0; n < rank; n++)
183 mbase = (GFOR_POINTER_L8_TO_L4 (mbase));
187 /* Initialize the return value. */
188 for (n = 0; n < rank; n++)
189 dest[n * dstride] = 1;
192 define(START_MASKED_FOREACH_BLOCK, `START_FOREACH_BLOCK')dnl
193 define(FINISH_MASKED_FOREACH_FUNCTION,
194 ` /* Implementation end. */
196 /* Advance to the next element. */
201 while (count[n] == extent[n])
203 /* When we get to the end of a dimension, reset it and increment
204 the next dimension. */
206 /* We could precalculate these products, but this is a less
207 frequently used path so proabably not worth it. */
208 base -= sstride[n] * extent[n];
209 mbase -= mstride[n] * extent[n];
213 /* Break out of the loop. */
227 define(FOREACH_FUNCTION,
228 `START_FOREACH_FUNCTION
232 FINISH_FOREACH_FUNCTION')dnl
233 define(MASKED_FOREACH_FUNCTION,
234 `START_MASKED_FOREACH_FUNCTION
236 START_MASKED_FOREACH_BLOCK
238 FINISH_MASKED_FOREACH_FUNCTION')dnl