iforeach.m4 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 Runtime Library (libgfortran)
  4. dnl Distributed under the GNU GPL with exception. See COPYING for details.
  5. define(START_FOREACH_FUNCTION,
  6. `
  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);
  10. void
  11. name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
  12. atype * const restrict array)
  13. {
  14. index_type count[GFC_MAX_DIMENSIONS];
  15. index_type extent[GFC_MAX_DIMENSIONS];
  16. index_type sstride[GFC_MAX_DIMENSIONS];
  17. index_type dstride;
  18. const atype_name *base;
  19. rtype_name * restrict dest;
  20. index_type rank;
  21. index_type n;
  22. rank = GFC_DESCRIPTOR_RANK (array);
  23. if (rank <= 0)
  24. runtime_error ("Rank of array needs to be > 0");
  25. if (retarray->base_addr == NULL)
  26. {
  27. GFC_DIMENSION_SET(retarray->dim[0], 0, rank-1, 1);
  28. retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
  29. retarray->offset = 0;
  30. retarray->base_addr = xmallocarray (rank, sizeof (rtype_name));
  31. }
  32. else
  33. {
  34. if (unlikely (compile_options.bounds_check))
  35. bounds_iforeach_return ((array_t *) retarray, (array_t *) array,
  36. "u_name");
  37. }
  38. dstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
  39. dest = retarray->base_addr;
  40. for (n = 0; n < rank; n++)
  41. {
  42. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
  43. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  44. count[n] = 0;
  45. if (extent[n] <= 0)
  46. {
  47. /* Set the return value. */
  48. for (n = 0; n < rank; n++)
  49. dest[n * dstride] = 0;
  50. return;
  51. }
  52. }
  53. base = array->base_addr;
  54. /* Initialize the return value. */
  55. for (n = 0; n < rank; n++)
  56. dest[n * dstride] = 1;
  57. {
  58. ')dnl
  59. define(START_FOREACH_BLOCK,
  60. ` while (base)
  61. {
  62. do
  63. {
  64. /* Implementation start. */
  65. ')dnl
  66. define(FINISH_FOREACH_FUNCTION,
  67. ` /* Implementation end. */
  68. /* Advance to the next element. */
  69. base += sstride[0];
  70. }
  71. while (++count[0] != extent[0]);
  72. n = 0;
  73. do
  74. {
  75. /* When we get to the end of a dimension, reset it and increment
  76. the next dimension. */
  77. count[n] = 0;
  78. /* We could precalculate these products, but this is a less
  79. frequently used path so probably not worth it. */
  80. base -= sstride[n] * extent[n];
  81. n++;
  82. if (n == rank)
  83. {
  84. /* Break out of the loop. */
  85. base = NULL;
  86. break;
  87. }
  88. else
  89. {
  90. count[n]++;
  91. base += sstride[n];
  92. }
  93. }
  94. while (count[n] == extent[n]);
  95. }
  96. }
  97. }')dnl
  98. define(START_MASKED_FOREACH_FUNCTION,
  99. `
  100. extern void `m'name`'rtype_qual`_'atype_code (rtype * const restrict,
  101. atype * const restrict, gfc_array_l1 * const restrict);
  102. export_proto(`m'name`'rtype_qual`_'atype_code);
  103. void
  104. `m'name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
  105. atype * const restrict array,
  106. gfc_array_l1 * const restrict mask)
  107. {
  108. index_type count[GFC_MAX_DIMENSIONS];
  109. index_type extent[GFC_MAX_DIMENSIONS];
  110. index_type sstride[GFC_MAX_DIMENSIONS];
  111. index_type mstride[GFC_MAX_DIMENSIONS];
  112. index_type dstride;
  113. rtype_name *dest;
  114. const atype_name *base;
  115. GFC_LOGICAL_1 *mbase;
  116. int rank;
  117. index_type n;
  118. int mask_kind;
  119. rank = GFC_DESCRIPTOR_RANK (array);
  120. if (rank <= 0)
  121. runtime_error ("Rank of array needs to be > 0");
  122. if (retarray->base_addr == NULL)
  123. {
  124. GFC_DIMENSION_SET(retarray->dim[0], 0, rank - 1, 1);
  125. retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
  126. retarray->offset = 0;
  127. retarray->base_addr = xmallocarray (rank, sizeof (rtype_name));
  128. }
  129. else
  130. {
  131. if (unlikely (compile_options.bounds_check))
  132. {
  133. bounds_iforeach_return ((array_t *) retarray, (array_t *) array,
  134. "u_name");
  135. bounds_equal_extents ((array_t *) mask, (array_t *) array,
  136. "MASK argument", "u_name");
  137. }
  138. }
  139. mask_kind = GFC_DESCRIPTOR_SIZE (mask);
  140. mbase = mask->base_addr;
  141. if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
  142. #ifdef HAVE_GFC_LOGICAL_16
  143. || mask_kind == 16
  144. #endif
  145. )
  146. mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
  147. else
  148. runtime_error ("Funny sized logical array");
  149. dstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
  150. dest = retarray->base_addr;
  151. for (n = 0; n < rank; n++)
  152. {
  153. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
  154. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
  155. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  156. count[n] = 0;
  157. if (extent[n] <= 0)
  158. {
  159. /* Set the return value. */
  160. for (n = 0; n < rank; n++)
  161. dest[n * dstride] = 0;
  162. return;
  163. }
  164. }
  165. base = array->base_addr;
  166. /* Initialize the return value. */
  167. for (n = 0; n < rank; n++)
  168. dest[n * dstride] = 0;
  169. {
  170. ')dnl
  171. define(START_MASKED_FOREACH_BLOCK, `START_FOREACH_BLOCK')dnl
  172. define(FINISH_MASKED_FOREACH_FUNCTION,
  173. ` /* Implementation end. */
  174. /* Advance to the next element. */
  175. base += sstride[0];
  176. mbase += mstride[0];
  177. }
  178. while (++count[0] != extent[0]);
  179. n = 0;
  180. do
  181. {
  182. /* When we get to the end of a dimension, reset it and increment
  183. the next dimension. */
  184. count[n] = 0;
  185. /* We could precalculate these products, but this is a less
  186. frequently used path so probably not worth it. */
  187. base -= sstride[n] * extent[n];
  188. mbase -= mstride[n] * extent[n];
  189. n++;
  190. if (n == rank)
  191. {
  192. /* Break out of the loop. */
  193. base = NULL;
  194. break;
  195. }
  196. else
  197. {
  198. count[n]++;
  199. base += sstride[n];
  200. mbase += mstride[n];
  201. }
  202. }
  203. while (count[n] == extent[n]);
  204. }
  205. }
  206. }')dnl
  207. define(FOREACH_FUNCTION,
  208. `START_FOREACH_FUNCTION
  209. $1
  210. START_FOREACH_BLOCK
  211. $2
  212. FINISH_FOREACH_FUNCTION')dnl
  213. define(MASKED_FOREACH_FUNCTION,
  214. `START_MASKED_FOREACH_FUNCTION
  215. $1
  216. START_MASKED_FOREACH_BLOCK
  217. $2
  218. FINISH_MASKED_FOREACH_FUNCTION')dnl
  219. define(SCALAR_FOREACH_FUNCTION,
  220. `
  221. extern void `s'name`'rtype_qual`_'atype_code (rtype * const restrict,
  222. atype * const restrict, GFC_LOGICAL_4 *);
  223. export_proto(`s'name`'rtype_qual`_'atype_code);
  224. void
  225. `s'name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
  226. atype * const restrict array,
  227. GFC_LOGICAL_4 * mask)
  228. {
  229. index_type rank;
  230. index_type dstride;
  231. index_type n;
  232. rtype_name *dest;
  233. if (*mask)
  234. {
  235. name`'rtype_qual`_'atype_code (retarray, array);
  236. return;
  237. }
  238. rank = GFC_DESCRIPTOR_RANK (array);
  239. if (rank <= 0)
  240. runtime_error ("Rank of array needs to be > 0");
  241. if (retarray->base_addr == NULL)
  242. {
  243. GFC_DIMENSION_SET(retarray->dim[0], 0, rank-1, 1);
  244. retarray->dtype = (retarray->dtype & ~GFC_DTYPE_RANK_MASK) | 1;
  245. retarray->offset = 0;
  246. retarray->base_addr = xmallocarray (rank, sizeof (rtype_name));
  247. }
  248. else if (unlikely (compile_options.bounds_check))
  249. {
  250. bounds_iforeach_return ((array_t *) retarray, (array_t *) array,
  251. "u_name");
  252. }
  253. dstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
  254. dest = retarray->base_addr;
  255. for (n = 0; n<rank; n++)
  256. dest[n * dstride] = $1 ;
  257. }')dnl