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;
20 rtype_name * restrict dest;
24 rank = GFC_DESCRIPTOR_RANK (array);
26 runtime_error ("Rank of array needs to be > 0");
28 if (retarray->data == NULL)
30 GFC_DIMENSION_SET(retarray->dim[0], 0, rank-1, 1);
31 retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
33 retarray->data = internal_malloc_size (sizeof (rtype_name) * rank);
37 if (unlikely (compile_options.bounds_check))
40 index_type ret_extent;
42 ret_rank = GFC_DESCRIPTOR_RANK (retarray);
44 runtime_error ("rank of return array in u_name intrinsic"
45 " should be 1, is %ld", (long int) ret_rank);
47 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,0);
48 if (ret_extent != rank)
49 runtime_error ("Incorrect extent in return value of"
50 " u_name intrnisic: is %ld, should be %ld",
51 (long int) ret_extent, (long int) rank);
55 dstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
56 dest = retarray->data;
57 for (n = 0; n < rank; n++)
59 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
60 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
64 /* Set the return value. */
65 for (n = 0; n < rank; n++)
66 dest[n * dstride] = 0;
73 /* Initialize the return value. */
74 for (n = 0; n < rank; n++)
75 dest[n * dstride] = 0;
78 define(START_FOREACH_BLOCK,
82 /* Implementation start. */
84 define(FINISH_FOREACH_FUNCTION,
85 ` /* Implementation end. */
87 /* Advance to the next element. */
91 while (count[n] == extent[n])
93 /* When we get to the end of a dimension, reset it and increment
94 the next dimension. */
96 /* We could precalculate these products, but this is a less
97 frequently used path so probably not worth it. */
98 base -= sstride[n] * extent[n];
102 /* Break out of the loop. */
115 define(START_MASKED_FOREACH_FUNCTION,
117 extern void `m'name`'rtype_qual`_'atype_code (rtype * const restrict,
118 atype * const restrict, gfc_array_l1 * const restrict);
119 export_proto(`m'name`'rtype_qual`_'atype_code);
122 `m'name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
123 atype * const restrict array,
124 gfc_array_l1 * const restrict mask)
126 index_type count[GFC_MAX_DIMENSIONS];
127 index_type extent[GFC_MAX_DIMENSIONS];
128 index_type sstride[GFC_MAX_DIMENSIONS];
129 index_type mstride[GFC_MAX_DIMENSIONS];
132 const atype_name *base;
133 GFC_LOGICAL_1 *mbase;
138 rank = GFC_DESCRIPTOR_RANK (array);
140 runtime_error ("Rank of array needs to be > 0");
142 if (retarray->data == NULL)
144 GFC_DIMENSION_SET(retarray->dim[0], 0, rank - 1, 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 (unlikely (compile_options.bounds_check))
153 int ret_rank, mask_rank;
154 index_type ret_extent;
156 index_type array_extent, mask_extent;
158 ret_rank = GFC_DESCRIPTOR_RANK (retarray);
160 runtime_error ("rank of return array in u_name intrinsic"
161 " should be 1, is %ld", (long int) ret_rank);
163 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,0);
164 if (ret_extent != rank)
165 runtime_error ("Incorrect extent in return value of"
166 " u_name intrnisic: is %ld, should be %ld",
167 (long int) ret_extent, (long int) rank);
169 mask_rank = GFC_DESCRIPTOR_RANK (mask);
170 if (rank != mask_rank)
171 runtime_error ("rank of MASK argument in u_name intrnisic"
172 "should be %ld, is %ld", (long int) rank,
173 (long int) mask_rank);
175 for (n=0; n<rank; n++)
177 array_extent = GFC_DESCRIPTOR_EXTENT(array,n);
178 mask_extent = GFC_DESCRIPTOR_EXTENT(mask,n);
179 if (array_extent != mask_extent)
180 runtime_error ("Incorrect extent in MASK argument of"
181 " u_name intrinsic in dimension %ld:"
182 " is %ld, should be %ld", (long int) n + 1,
183 (long int) mask_extent, (long int) array_extent);
188 mask_kind = GFC_DESCRIPTOR_SIZE (mask);
192 if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
193 #ifdef HAVE_GFC_LOGICAL_16
197 mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
199 runtime_error ("Funny sized logical array");
201 dstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
202 dest = retarray->data;
203 for (n = 0; n < rank; n++)
205 sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
206 mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
207 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
211 /* Set the return value. */
212 for (n = 0; n < rank; n++)
213 dest[n * dstride] = 0;
220 /* Initialize the return value. */
221 for (n = 0; n < rank; n++)
222 dest[n * dstride] = 0;
225 define(START_MASKED_FOREACH_BLOCK, `START_FOREACH_BLOCK')dnl
226 define(FINISH_MASKED_FOREACH_FUNCTION,
227 ` /* Implementation end. */
229 /* Advance to the next element. */
234 while (count[n] == extent[n])
236 /* When we get to the end of a dimension, reset it and increment
237 the next dimension. */
239 /* We could precalculate these products, but this is a less
240 frequently used path so probably not worth it. */
241 base -= sstride[n] * extent[n];
242 mbase -= mstride[n] * extent[n];
246 /* Break out of the loop. */
260 define(FOREACH_FUNCTION,
261 `START_FOREACH_FUNCTION
265 FINISH_FOREACH_FUNCTION')dnl
266 define(MASKED_FOREACH_FUNCTION,
267 `START_MASKED_FOREACH_FUNCTION
269 START_MASKED_FOREACH_BLOCK
271 FINISH_MASKED_FOREACH_FUNCTION')dnl
272 define(SCALAR_FOREACH_FUNCTION,
274 extern void `s'name`'rtype_qual`_'atype_code (rtype * const restrict,
275 atype * const restrict, GFC_LOGICAL_4 *);
276 export_proto(`s'name`'rtype_qual`_'atype_code);
279 `s'name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
280 atype * const restrict array,
281 GFC_LOGICAL_4 * mask)
290 name`'rtype_qual`_'atype_code (retarray, array);
294 rank = GFC_DESCRIPTOR_RANK (array);
297 runtime_error ("Rank of array needs to be > 0");
299 if (retarray->data == NULL)
301 GFC_DIMENSION_SET(retarray->dim[0], 0, rank-1, 1);
302 retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
303 retarray->offset = 0;
304 retarray->data = internal_malloc_size (sizeof (rtype_name) * rank);
308 if (unlikely (compile_options.bounds_check))
311 index_type ret_extent;
313 ret_rank = GFC_DESCRIPTOR_RANK (retarray);
315 runtime_error ("rank of return array in u_name intrinsic"
316 " should be 1, is %ld", (long int) ret_rank);
318 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,0);
319 if (ret_extent != rank)
320 runtime_error ("dimension of return array incorrect");
324 dstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
325 dest = retarray->data;
326 for (n = 0; n<rank; n++)
327 dest[n * dstride] = $1 ;