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.
6 dnl Pass the implementation for a single section as the parameter to
7 dnl {MASK_}ARRAY_FUNCTION.
8 dnl The variables base, delta, and len describe the input section.
9 dnl For masked section the mask is described by mbase and mdelta.
10 dnl These should not be modified. The result should be stored in *dest.
11 dnl The names count, extent, sstride, dstride, base, dest, rank, dim
12 dnl retarray, array, pdim and mstride should not be used.
13 dnl The variable n is declared as index_type and may be used.
14 dnl Other variable declarations may be placed at the start of the code,
15 dnl The types of the array parameter and the return value are
16 dnl atype_name and rtype_name respectively.
17 dnl Execution should be allowed to continue to the end of the block.
18 dnl You should not return or break from the inner loop of the implementation.
19 dnl Care should also be taken to avoid using the names defined in iparm.m4
20 define(START_ARRAY_FUNCTION,
22 extern void name`'rtype_qual`_'atype_code (rtype * const restrict,
23 gfc_array_l1 * const restrict, const index_type * const restrict);
24 export_proto(name`'rtype_qual`_'atype_code);
27 name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
28 gfc_array_l1 * const restrict array,
29 const index_type * const restrict pdim)
31 index_type count[GFC_MAX_DIMENSIONS];
32 index_type extent[GFC_MAX_DIMENSIONS];
33 index_type sstride[GFC_MAX_DIMENSIONS];
34 index_type dstride[GFC_MAX_DIMENSIONS];
35 const GFC_LOGICAL_1 * restrict base;
36 rtype_name * restrict dest;
45 /* Make dim zero based to avoid confusion. */
47 rank = GFC_DESCRIPTOR_RANK (array) - 1;
49 src_kind = GFC_DESCRIPTOR_SIZE (array);
51 len = GFC_DESCRIPTOR_EXTENT(array,dim);
55 delta = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
57 for (n = 0; n < dim; n++)
59 sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,n);
60 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
65 for (n = dim; n < rank; n++)
67 sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,n + 1);
68 extent[n] = GFC_DESCRIPTOR_EXTENT(array,n + 1);
74 if (retarray->data == NULL)
76 size_t alloc_size, str;
78 for (n = 0; n < rank; n++)
83 str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
85 GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
90 retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
92 alloc_size = sizeof (rtype_name) * GFC_DESCRIPTOR_STRIDE(retarray,rank-1)
97 /* Make sure we have a zero-sized array. */
98 GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
102 retarray->data = internal_malloc_size (alloc_size);
106 if (rank != GFC_DESCRIPTOR_RANK (retarray))
107 runtime_error ("rank of return array incorrect in"
108 " u_name intrinsic: is %ld, should be %ld",
109 (long int) GFC_DESCRIPTOR_RANK (retarray),
112 if (unlikely (compile_options.bounds_check))
114 for (n=0; n < rank; n++)
116 index_type ret_extent;
118 ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
119 if (extent[n] != ret_extent)
120 runtime_error ("Incorrect extent in return value of"
121 " u_name intrinsic in dimension %d:"
122 " is %ld, should be %ld", (int) n + 1,
123 (long int) ret_extent, (long int) extent[n]);
128 for (n = 0; n < rank; n++)
131 dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
138 if (src_kind == 1 || src_kind == 2 || src_kind == 4 || src_kind == 8
139 #ifdef HAVE_GFC_LOGICAL_16
145 base = GFOR_POINTER_TO_L1 (base, src_kind);
148 internal_error (NULL, "Funny sized logical array in u_name intrinsic");
150 dest = retarray->data;
153 while (continue_loop)
155 const GFC_LOGICAL_1 * restrict src;
160 define(START_ARRAY_BLOCK,
165 for (n = 0; n < len; n++, src += delta)
168 define(FINISH_ARRAY_FUNCTION,
173 /* Advance to the next element. */
178 while (count[n] == extent[n])
180 /* When we get to the end of a dimension, reset it and increment
181 the next dimension. */
183 /* We could precalculate these products, but this is a less
184 frequently used path so probably not worth it. */
185 base -= sstride[n] * extent[n];
186 dest -= dstride[n] * extent[n];
190 /* Break out of the look. */
203 define(ARRAY_FUNCTION,
204 `START_ARRAY_FUNCTION
206 START_ARRAY_BLOCK($1)
208 FINISH_ARRAY_FUNCTION')dnl