ifunction.m4 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. dnl
  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,
  21. `
  22. extern void name`'rtype_qual`_'atype_code (rtype * const restrict,
  23. atype * const restrict, const index_type * const restrict);
  24. export_proto(name`'rtype_qual`_'atype_code);
  25. void
  26. name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
  27. atype * const restrict array,
  28. const index_type * const restrict pdim)
  29. {
  30. index_type count[GFC_MAX_DIMENSIONS];
  31. index_type extent[GFC_MAX_DIMENSIONS];
  32. index_type sstride[GFC_MAX_DIMENSIONS];
  33. index_type dstride[GFC_MAX_DIMENSIONS];
  34. const atype_name * restrict base;
  35. rtype_name * restrict dest;
  36. index_type rank;
  37. index_type n;
  38. index_type len;
  39. index_type delta;
  40. index_type dim;
  41. int continue_loop;
  42. /* Make dim zero based to avoid confusion. */
  43. dim = (*pdim) - 1;
  44. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  45. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  46. if (len < 0)
  47. len = 0;
  48. delta = GFC_DESCRIPTOR_STRIDE(array,dim);
  49. for (n = 0; n < dim; n++)
  50. {
  51. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
  52. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  53. if (extent[n] < 0)
  54. extent[n] = 0;
  55. }
  56. for (n = dim; n < rank; n++)
  57. {
  58. sstride[n] = GFC_DESCRIPTOR_STRIDE(array, n + 1);
  59. extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
  60. if (extent[n] < 0)
  61. extent[n] = 0;
  62. }
  63. if (retarray->base_addr == NULL)
  64. {
  65. size_t alloc_size, str;
  66. for (n = 0; n < rank; n++)
  67. {
  68. if (n == 0)
  69. str = 1;
  70. else
  71. str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
  72. GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
  73. }
  74. retarray->offset = 0;
  75. retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
  76. alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
  77. retarray->base_addr = xmallocarray (alloc_size, sizeof (rtype_name));
  78. if (alloc_size == 0)
  79. {
  80. /* Make sure we have a zero-sized array. */
  81. GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
  82. return;
  83. }
  84. }
  85. else
  86. {
  87. if (rank != GFC_DESCRIPTOR_RANK (retarray))
  88. runtime_error ("rank of return array incorrect in"
  89. " u_name intrinsic: is %ld, should be %ld",
  90. (long int) (GFC_DESCRIPTOR_RANK (retarray)),
  91. (long int) rank);
  92. if (unlikely (compile_options.bounds_check))
  93. bounds_ifunction_return ((array_t *) retarray, extent,
  94. "return value", "u_name");
  95. }
  96. for (n = 0; n < rank; n++)
  97. {
  98. count[n] = 0;
  99. dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
  100. if (extent[n] <= 0)
  101. return;
  102. }
  103. base = array->base_addr;
  104. dest = retarray->base_addr;
  105. continue_loop = 1;
  106. while (continue_loop)
  107. {
  108. const atype_name * restrict src;
  109. rtype_name result;
  110. src = base;
  111. {
  112. ')dnl
  113. define(START_ARRAY_BLOCK,
  114. ` if (len <= 0)
  115. *dest = '$1`;
  116. else
  117. {
  118. for (n = 0; n < len; n++, src += delta)
  119. {
  120. ')dnl
  121. define(FINISH_ARRAY_FUNCTION,
  122. ` }
  123. '$1`
  124. *dest = result;
  125. }
  126. }
  127. /* Advance to the next element. */
  128. count[0]++;
  129. base += sstride[0];
  130. dest += dstride[0];
  131. n = 0;
  132. while (count[n] == extent[n])
  133. {
  134. /* When we get to the end of a dimension, reset it and increment
  135. the next dimension. */
  136. count[n] = 0;
  137. /* We could precalculate these products, but this is a less
  138. frequently used path so probably not worth it. */
  139. base -= sstride[n] * extent[n];
  140. dest -= dstride[n] * extent[n];
  141. n++;
  142. if (n == rank)
  143. {
  144. /* Break out of the look. */
  145. continue_loop = 0;
  146. break;
  147. }
  148. else
  149. {
  150. count[n]++;
  151. base += sstride[n];
  152. dest += dstride[n];
  153. }
  154. }
  155. }
  156. }')dnl
  157. define(START_MASKED_ARRAY_FUNCTION,
  158. `
  159. extern void `m'name`'rtype_qual`_'atype_code (rtype * const restrict,
  160. atype * const restrict, const index_type * const restrict,
  161. gfc_array_l1 * const restrict);
  162. export_proto(`m'name`'rtype_qual`_'atype_code);
  163. void
  164. `m'name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
  165. atype * const restrict array,
  166. const index_type * const restrict pdim,
  167. gfc_array_l1 * const restrict mask)
  168. {
  169. index_type count[GFC_MAX_DIMENSIONS];
  170. index_type extent[GFC_MAX_DIMENSIONS];
  171. index_type sstride[GFC_MAX_DIMENSIONS];
  172. index_type dstride[GFC_MAX_DIMENSIONS];
  173. index_type mstride[GFC_MAX_DIMENSIONS];
  174. rtype_name * restrict dest;
  175. const atype_name * restrict base;
  176. const GFC_LOGICAL_1 * restrict mbase;
  177. int rank;
  178. int dim;
  179. index_type n;
  180. index_type len;
  181. index_type delta;
  182. index_type mdelta;
  183. int mask_kind;
  184. dim = (*pdim) - 1;
  185. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  186. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  187. if (len <= 0)
  188. return;
  189. mbase = mask->base_addr;
  190. mask_kind = GFC_DESCRIPTOR_SIZE (mask);
  191. if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
  192. #ifdef HAVE_GFC_LOGICAL_16
  193. || mask_kind == 16
  194. #endif
  195. )
  196. mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
  197. else
  198. runtime_error ("Funny sized logical array");
  199. delta = GFC_DESCRIPTOR_STRIDE(array,dim);
  200. mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim);
  201. for (n = 0; n < dim; n++)
  202. {
  203. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
  204. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
  205. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  206. if (extent[n] < 0)
  207. extent[n] = 0;
  208. }
  209. for (n = dim; n < rank; n++)
  210. {
  211. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n + 1);
  212. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask, n + 1);
  213. extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
  214. if (extent[n] < 0)
  215. extent[n] = 0;
  216. }
  217. if (retarray->base_addr == NULL)
  218. {
  219. size_t alloc_size, str;
  220. for (n = 0; n < rank; n++)
  221. {
  222. if (n == 0)
  223. str = 1;
  224. else
  225. str= GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
  226. GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
  227. }
  228. alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
  229. retarray->offset = 0;
  230. retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
  231. if (alloc_size == 0)
  232. {
  233. /* Make sure we have a zero-sized array. */
  234. GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
  235. return;
  236. }
  237. else
  238. retarray->base_addr = xmallocarray (alloc_size, sizeof (rtype_name));
  239. }
  240. else
  241. {
  242. if (rank != GFC_DESCRIPTOR_RANK (retarray))
  243. runtime_error ("rank of return array incorrect in u_name intrinsic");
  244. if (unlikely (compile_options.bounds_check))
  245. {
  246. bounds_ifunction_return ((array_t *) retarray, extent,
  247. "return value", "u_name");
  248. bounds_equal_extents ((array_t *) mask, (array_t *) array,
  249. "MASK argument", "u_name");
  250. }
  251. }
  252. for (n = 0; n < rank; n++)
  253. {
  254. count[n] = 0;
  255. dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
  256. if (extent[n] <= 0)
  257. return;
  258. }
  259. dest = retarray->base_addr;
  260. base = array->base_addr;
  261. while (base)
  262. {
  263. const atype_name * restrict src;
  264. const GFC_LOGICAL_1 * restrict msrc;
  265. rtype_name result;
  266. src = base;
  267. msrc = mbase;
  268. {
  269. ')dnl
  270. define(START_MASKED_ARRAY_BLOCK,
  271. ` for (n = 0; n < len; n++, src += delta, msrc += mdelta)
  272. {
  273. ')dnl
  274. define(FINISH_MASKED_ARRAY_FUNCTION,
  275. ` }
  276. *dest = result;
  277. }
  278. /* Advance to the next element. */
  279. count[0]++;
  280. base += sstride[0];
  281. mbase += mstride[0];
  282. dest += dstride[0];
  283. n = 0;
  284. while (count[n] == extent[n])
  285. {
  286. /* When we get to the end of a dimension, reset it and increment
  287. the next dimension. */
  288. count[n] = 0;
  289. /* We could precalculate these products, but this is a less
  290. frequently used path so probably not worth it. */
  291. base -= sstride[n] * extent[n];
  292. mbase -= mstride[n] * extent[n];
  293. dest -= dstride[n] * extent[n];
  294. n++;
  295. if (n == rank)
  296. {
  297. /* Break out of the look. */
  298. base = NULL;
  299. break;
  300. }
  301. else
  302. {
  303. count[n]++;
  304. base += sstride[n];
  305. mbase += mstride[n];
  306. dest += dstride[n];
  307. }
  308. }
  309. }
  310. }')dnl
  311. define(SCALAR_ARRAY_FUNCTION,
  312. `
  313. extern void `s'name`'rtype_qual`_'atype_code (rtype * const restrict,
  314. atype * const restrict, const index_type * const restrict,
  315. GFC_LOGICAL_4 *);
  316. export_proto(`s'name`'rtype_qual`_'atype_code);
  317. void
  318. `s'name`'rtype_qual`_'atype_code (rtype * const restrict retarray,
  319. atype * const restrict array,
  320. const index_type * const restrict pdim,
  321. GFC_LOGICAL_4 * mask)
  322. {
  323. index_type count[GFC_MAX_DIMENSIONS];
  324. index_type extent[GFC_MAX_DIMENSIONS];
  325. index_type dstride[GFC_MAX_DIMENSIONS];
  326. rtype_name * restrict dest;
  327. index_type rank;
  328. index_type n;
  329. index_type dim;
  330. if (*mask)
  331. {
  332. name`'rtype_qual`_'atype_code (retarray, array, pdim);
  333. return;
  334. }
  335. /* Make dim zero based to avoid confusion. */
  336. dim = (*pdim) - 1;
  337. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  338. for (n = 0; n < dim; n++)
  339. {
  340. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  341. if (extent[n] <= 0)
  342. extent[n] = 0;
  343. }
  344. for (n = dim; n < rank; n++)
  345. {
  346. extent[n] =
  347. GFC_DESCRIPTOR_EXTENT(array,n + 1);
  348. if (extent[n] <= 0)
  349. extent[n] = 0;
  350. }
  351. if (retarray->base_addr == NULL)
  352. {
  353. size_t alloc_size, str;
  354. for (n = 0; n < rank; n++)
  355. {
  356. if (n == 0)
  357. str = 1;
  358. else
  359. str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
  360. GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
  361. }
  362. retarray->offset = 0;
  363. retarray->dtype = (array->dtype & ~GFC_DTYPE_RANK_MASK) | rank;
  364. alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
  365. if (alloc_size == 0)
  366. {
  367. /* Make sure we have a zero-sized array. */
  368. GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
  369. return;
  370. }
  371. else
  372. retarray->base_addr = xmallocarray (alloc_size, sizeof (rtype_name));
  373. }
  374. else
  375. {
  376. if (rank != GFC_DESCRIPTOR_RANK (retarray))
  377. runtime_error ("rank of return array incorrect in"
  378. " u_name intrinsic: is %ld, should be %ld",
  379. (long int) (GFC_DESCRIPTOR_RANK (retarray)),
  380. (long int) rank);
  381. if (unlikely (compile_options.bounds_check))
  382. {
  383. for (n=0; n < rank; n++)
  384. {
  385. index_type ret_extent;
  386. ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
  387. if (extent[n] != ret_extent)
  388. runtime_error ("Incorrect extent in return value of"
  389. " u_name intrinsic in dimension %ld:"
  390. " is %ld, should be %ld", (long int) n + 1,
  391. (long int) ret_extent, (long int) extent[n]);
  392. }
  393. }
  394. }
  395. for (n = 0; n < rank; n++)
  396. {
  397. count[n] = 0;
  398. dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
  399. }
  400. dest = retarray->base_addr;
  401. while(1)
  402. {
  403. *dest = '$1`;
  404. count[0]++;
  405. dest += dstride[0];
  406. n = 0;
  407. while (count[n] == extent[n])
  408. {
  409. /* When we get to the end of a dimension, reset it and increment
  410. the next dimension. */
  411. count[n] = 0;
  412. /* We could precalculate these products, but this is a less
  413. frequently used path so probably not worth it. */
  414. dest -= dstride[n] * extent[n];
  415. n++;
  416. if (n == rank)
  417. return;
  418. else
  419. {
  420. count[n]++;
  421. dest += dstride[n];
  422. }
  423. }
  424. }
  425. }')dnl
  426. define(ARRAY_FUNCTION,
  427. `START_ARRAY_FUNCTION
  428. $2
  429. START_ARRAY_BLOCK($1)
  430. $3
  431. FINISH_ARRAY_FUNCTION($4)')dnl
  432. define(MASKED_ARRAY_FUNCTION,
  433. `START_MASKED_ARRAY_FUNCTION
  434. $2
  435. START_MASKED_ARRAY_BLOCK
  436. $3
  437. FINISH_MASKED_ARRAY_FUNCTION')dnl