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 * 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;
24 rank = GFC_DESCRIPTOR_RANK (array);
26 runtime_error ("Rank of array needs to be > 0");
28 if (retarray->data == NULL)
30 retarray->dim[0].lbound = 0;
31 retarray->dim[0].ubound = rank-1;
32 retarray->dim[0].stride = 1;
33 retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
35 retarray->data = internal_malloc_size (sizeof (rtype_name) * rank);
39 if (GFC_DESCRIPTOR_RANK (retarray) != 1)
40 runtime_error ("rank of return array does not equal 1");
42 if (retarray->dim[0].ubound + 1 - retarray->dim[0].lbound != rank)
43 runtime_error ("dimension of return array incorrect");
45 if (retarray->dim[0].stride == 0)
46 retarray->dim[0].stride = 1;
49 /* TODO: It should be a front end job to correctly set the strides. */
51 if (array->dim[0].stride == 0)
52 array->dim[0].stride = 1;
54 dstride = retarray->dim[0].stride;
55 dest = retarray->data;
56 for (n = 0; n < rank; n++)
58 sstride[n] = array->dim[n].stride;
59 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
63 /* Set the return value. */
64 for (n = 0; n < rank; n++)
65 dest[n * dstride] = 0;
72 /* Initialize the return value. */
73 for (n = 0; n < rank; n++)
74 dest[n * dstride] = 0;
77 define(START_FOREACH_BLOCK,
81 /* Implementation start. */
83 define(FINISH_FOREACH_FUNCTION,
84 ` /* Implementation end. */
86 /* Advance to the next element. */
90 while (count[n] == extent[n])
92 /* When we get to the end of a dimension, reset it and increment
93 the next dimension. */
95 /* We could precalculate these products, but this is a less
96 frequently used path so proabably not worth it. */
97 base -= sstride[n] * extent[n];
101 /* Break out of the loop. */
114 define(START_MASKED_FOREACH_FUNCTION,
116 extern void `m'name`'rtype_qual`_'atype_code (rtype * const restrict,
117 atype * const restrict, gfc_array_l4 * const restrict);
118 export_proto(`m'name`'rtype_qual`_'atype_code);
121 `m'name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
122 atype * const restrict array,
123 gfc_array_l4 * const restrict mask)
125 index_type count[GFC_MAX_DIMENSIONS];
126 index_type extent[GFC_MAX_DIMENSIONS];
127 index_type sstride[GFC_MAX_DIMENSIONS];
128 index_type mstride[GFC_MAX_DIMENSIONS];
131 const atype_name *base;
132 GFC_LOGICAL_4 *mbase;
136 rank = GFC_DESCRIPTOR_RANK (array);
138 runtime_error ("Rank of array needs to be > 0");
140 if (retarray->data == NULL)
142 retarray->dim[0].lbound = 0;
143 retarray->dim[0].ubound = rank-1;
144 retarray->dim[0].stride = 1;
145 retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
146 retarray->offset = 0;
147 retarray->data = internal_malloc_size (sizeof (rtype_name) * rank);
151 if (GFC_DESCRIPTOR_RANK (retarray) != 1)
152 runtime_error ("rank of return array does not equal 1");
154 if (retarray->dim[0].ubound + 1 - retarray->dim[0].lbound != rank)
155 runtime_error ("dimension of return array incorrect");
157 if (retarray->dim[0].stride == 0)
158 retarray->dim[0].stride = 1;
161 /* TODO: It should be a front end job to correctly set the strides. */
163 if (array->dim[0].stride == 0)
164 array->dim[0].stride = 1;
166 if (mask->dim[0].stride == 0)
167 mask->dim[0].stride = 1;
169 dstride = retarray->dim[0].stride;
170 dest = retarray->data;
171 for (n = 0; n < rank; n++)
173 sstride[n] = array->dim[n].stride;
174 mstride[n] = mask->dim[n].stride;
175 extent[n] = array->dim[n].ubound + 1 - array->dim[n].lbound;
179 /* Set the return value. */
180 for (n = 0; n < rank; n++)
181 dest[n * dstride] = 0;
189 if (GFC_DESCRIPTOR_SIZE (mask) != 4)
191 /* This allows the same loop to be used for all logical types. */
192 assert (GFC_DESCRIPTOR_SIZE (mask) == 8);
193 for (n = 0; n < rank; n++)
195 mbase = (GFOR_POINTER_L8_TO_L4 (mbase));
199 /* Initialize the return value. */
200 for (n = 0; n < rank; n++)
201 dest[n * dstride] = 0;
204 define(START_MASKED_FOREACH_BLOCK, `START_FOREACH_BLOCK')dnl
205 define(FINISH_MASKED_FOREACH_FUNCTION,
206 ` /* Implementation end. */
208 /* Advance to the next element. */
213 while (count[n] == extent[n])
215 /* When we get to the end of a dimension, reset it and increment
216 the next dimension. */
218 /* We could precalculate these products, but this is a less
219 frequently used path so proabably not worth it. */
220 base -= sstride[n] * extent[n];
221 mbase -= mstride[n] * extent[n];
225 /* Break out of the loop. */
239 define(FOREACH_FUNCTION,
240 `START_FOREACH_FUNCTION
244 FINISH_FOREACH_FUNCTION')dnl
245 define(MASKED_FOREACH_FUNCTION,
246 `START_MASKED_FOREACH_FUNCTION
248 START_MASKED_FOREACH_BLOCK
250 FINISH_MASKED_FOREACH_FUNCTION')dnl