ifindloc1.m4 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. `/* Implementation of the FINDLOC intrinsic
  2. Copyright (C) 2018-2022 Free Software Foundation, Inc.
  3. Contributed by Thomas König <tk@tkoenig.net>
  4. This file is part of the GNU Fortran 95 runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public
  7. License as published by the Free Software Foundation; either
  8. version 3 of the License, or (at your option) any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "libgfortran.h"
  21. #include <assert.h>
  22. #if defined (HAVE_'atype_name`)
  23. 'header1`
  24. {
  25. index_type count[GFC_MAX_DIMENSIONS];
  26. index_type extent[GFC_MAX_DIMENSIONS];
  27. index_type sstride[GFC_MAX_DIMENSIONS];
  28. index_type dstride[GFC_MAX_DIMENSIONS];
  29. const 'atype_name`'` * restrict base;
  30. index_type * restrict dest;
  31. index_type rank;
  32. index_type n;
  33. index_type len;
  34. index_type delta;
  35. index_type dim;
  36. int continue_loop;
  37. /* Make dim zero based to avoid confusion. */
  38. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  39. dim = (*pdim) - 1;
  40. if (unlikely (dim < 0 || dim > rank))
  41. {
  42. runtime_error ("Dim argument incorrect in FINDLOC intrinsic: "
  43. "is %ld, should be between 1 and %ld",
  44. (long int) dim + 1, (long int) rank + 1);
  45. }
  46. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  47. if (len < 0)
  48. len = 0;
  49. delta = GFC_DESCRIPTOR_STRIDE(array,dim);
  50. for (n = 0; n < dim; n++)
  51. {
  52. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
  53. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  54. if (extent[n] < 0)
  55. extent[n] = 0;
  56. }
  57. for (n = dim; n < rank; n++)
  58. {
  59. sstride[n] = GFC_DESCRIPTOR_STRIDE(array, n + 1);
  60. extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
  61. if (extent[n] < 0)
  62. extent[n] = 0;
  63. }
  64. if (retarray->base_addr == NULL)
  65. {
  66. size_t alloc_size, str;
  67. for (n = 0; n < rank; n++)
  68. {
  69. if (n == 0)
  70. str = 1;
  71. else
  72. str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
  73. GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
  74. }
  75. retarray->offset = 0;
  76. retarray->dtype.rank = rank;
  77. alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
  78. retarray->base_addr = xmallocarray (alloc_size, sizeof (index_type));
  79. if (alloc_size == 0)
  80. {
  81. /* Make sure we have a zero-sized array. */
  82. GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
  83. return;
  84. }
  85. }
  86. else
  87. {
  88. if (rank != GFC_DESCRIPTOR_RANK (retarray))
  89. runtime_error ("rank of return array incorrect in"
  90. " FINDLOC intrinsic: is %ld, should be %ld",
  91. (long int) (GFC_DESCRIPTOR_RANK (retarray)),
  92. (long int) rank);
  93. if (unlikely (compile_options.bounds_check))
  94. bounds_ifunction_return ((array_t *) retarray, extent,
  95. "return value", "FINDLOC");
  96. }
  97. for (n = 0; n < rank; n++)
  98. {
  99. count[n] = 0;
  100. dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
  101. if (extent[n] <= 0)
  102. return;
  103. }
  104. dest = retarray->base_addr;
  105. continue_loop = 1;
  106. base = array->base_addr;
  107. while (continue_loop)
  108. {
  109. const 'atype_name`'` * restrict src;
  110. index_type result;
  111. result = 0;
  112. if (back)
  113. {
  114. src = base + (len - 1) * delta * 'base_mult`;
  115. for (n = len; n > 0; n--, src -= delta * 'base_mult`)
  116. {
  117. if ('comparison`'`)
  118. {
  119. result = n;
  120. break;
  121. }
  122. }
  123. }
  124. else
  125. {
  126. src = base;
  127. for (n = 1; n <= len; n++, src += delta * 'base_mult`)
  128. {
  129. if ('comparison`'`)
  130. {
  131. result = n;
  132. break;
  133. }
  134. }
  135. }
  136. *dest = result;
  137. count[0]++;
  138. base += sstride[0] * 'base_mult`;
  139. dest += dstride[0];
  140. n = 0;
  141. while (count[n] == extent[n])
  142. {
  143. count[n] = 0;
  144. base -= sstride[n] * extent[n] * 'base_mult`;
  145. dest -= dstride[n] * extent[n];
  146. n++;
  147. if (n >= rank)
  148. {
  149. continue_loop = 0;
  150. break;
  151. }
  152. else
  153. {
  154. count[n]++;
  155. base += sstride[n] * 'base_mult`;
  156. dest += dstride[n];
  157. }
  158. }
  159. }
  160. }
  161. 'header2`'`
  162. {
  163. index_type count[GFC_MAX_DIMENSIONS];
  164. index_type extent[GFC_MAX_DIMENSIONS];
  165. index_type sstride[GFC_MAX_DIMENSIONS];
  166. index_type mstride[GFC_MAX_DIMENSIONS];
  167. index_type dstride[GFC_MAX_DIMENSIONS];
  168. const 'atype_name`'` * restrict base;
  169. const GFC_LOGICAL_1 * restrict mbase;
  170. index_type * restrict dest;
  171. index_type rank;
  172. index_type n;
  173. index_type len;
  174. index_type delta;
  175. index_type mdelta;
  176. index_type dim;
  177. int mask_kind;
  178. int continue_loop;
  179. /* Make dim zero based to avoid confusion. */
  180. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  181. dim = (*pdim) - 1;
  182. if (unlikely (dim < 0 || dim > rank))
  183. {
  184. runtime_error ("Dim argument incorrect in FINDLOC intrinsic: "
  185. "is %ld, should be between 1 and %ld",
  186. (long int) dim + 1, (long int) rank + 1);
  187. }
  188. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  189. if (len < 0)
  190. len = 0;
  191. delta = GFC_DESCRIPTOR_STRIDE(array,dim);
  192. mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim);
  193. mbase = mask->base_addr;
  194. mask_kind = GFC_DESCRIPTOR_SIZE (mask);
  195. if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
  196. #ifdef HAVE_GFC_LOGICAL_16
  197. || mask_kind == 16
  198. #endif
  199. )
  200. mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
  201. else
  202. internal_error (NULL, "Funny sized logical array");
  203. for (n = 0; n < dim; n++)
  204. {
  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);
  208. if (extent[n] < 0)
  209. extent[n] = 0;
  210. }
  211. for (n = dim; n < rank; n++)
  212. {
  213. sstride[n] = GFC_DESCRIPTOR_STRIDE(array, n + 1);
  214. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask, n + 1);
  215. extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
  216. if (extent[n] < 0)
  217. extent[n] = 0;
  218. }
  219. if (retarray->base_addr == NULL)
  220. {
  221. size_t alloc_size, str;
  222. for (n = 0; n < rank; n++)
  223. {
  224. if (n == 0)
  225. str = 1;
  226. else
  227. str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
  228. GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
  229. }
  230. retarray->offset = 0;
  231. retarray->dtype.rank = rank;
  232. alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
  233. retarray->base_addr = xmallocarray (alloc_size, sizeof (index_type));
  234. if (alloc_size == 0)
  235. {
  236. /* Make sure we have a zero-sized array. */
  237. GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
  238. return;
  239. }
  240. }
  241. else
  242. {
  243. if (rank != GFC_DESCRIPTOR_RANK (retarray))
  244. runtime_error ("rank of return array incorrect in"
  245. " FINDLOC intrinsic: is %ld, should be %ld",
  246. (long int) (GFC_DESCRIPTOR_RANK (retarray)),
  247. (long int) rank);
  248. if (unlikely (compile_options.bounds_check))
  249. bounds_ifunction_return ((array_t *) retarray, extent,
  250. "return value", "FINDLOC");
  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. continue_loop = 1;
  261. base = array->base_addr;
  262. while (continue_loop)
  263. {
  264. const 'atype_name`'` * restrict src;
  265. const GFC_LOGICAL_1 * restrict msrc;
  266. index_type result;
  267. result = 0;
  268. if (back)
  269. {
  270. src = base + (len - 1) * delta * 'base_mult`;
  271. msrc = mbase + (len - 1) * mdelta;
  272. for (n = len; n > 0; n--, src -= delta * 'base_mult`, msrc -= mdelta)
  273. {
  274. if (*msrc && 'comparison`'`)
  275. {
  276. result = n;
  277. break;
  278. }
  279. }
  280. }
  281. else
  282. {
  283. src = base;
  284. msrc = mbase;
  285. for (n = 1; n <= len; n++, src += delta * 'base_mult`, msrc += mdelta)
  286. {
  287. if (*msrc && 'comparison`'`)
  288. {
  289. result = n;
  290. break;
  291. }
  292. }
  293. }
  294. *dest = result;
  295. count[0]++;
  296. base += sstride[0] * 'base_mult`;
  297. mbase += mstride[0];
  298. dest += dstride[0];
  299. n = 0;
  300. while (count[n] == extent[n])
  301. {
  302. count[n] = 0;
  303. base -= sstride[n] * extent[n] * 'base_mult`;
  304. mbase -= mstride[n] * extent[n];
  305. dest -= dstride[n] * extent[n];
  306. n++;
  307. if (n >= rank)
  308. {
  309. continue_loop = 0;
  310. break;
  311. }
  312. else
  313. {
  314. count[n]++;
  315. base += sstride[n] * 'base_mult`;
  316. dest += dstride[n];
  317. }
  318. }
  319. }
  320. }
  321. 'header3`'`
  322. {
  323. index_type count[GFC_MAX_DIMENSIONS];
  324. index_type extent[GFC_MAX_DIMENSIONS];
  325. index_type dstride[GFC_MAX_DIMENSIONS];
  326. index_type * restrict dest;
  327. index_type rank;
  328. index_type n;
  329. index_type len;
  330. index_type dim;
  331. bool continue_loop;
  332. if (mask == NULL || *mask)
  333. {
  334. findloc1_'atype_code`'` (retarray, array, value, pdim, back'len_arg`'`);
  335. return;
  336. }
  337. /* Make dim zero based to avoid confusion. */
  338. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  339. dim = (*pdim) - 1;
  340. if (unlikely (dim < 0 || dim > rank))
  341. {
  342. runtime_error ("Dim argument incorrect in FINDLOC intrinsic: "
  343. "is %ld, should be between 1 and %ld",
  344. (long int) dim + 1, (long int) rank + 1);
  345. }
  346. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  347. if (len < 0)
  348. len = 0;
  349. for (n = 0; n < dim; n++)
  350. {
  351. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  352. if (extent[n] <= 0)
  353. extent[n] = 0;
  354. }
  355. for (n = dim; n < rank; n++)
  356. {
  357. extent[n] =
  358. GFC_DESCRIPTOR_EXTENT(array,n + 1);
  359. if (extent[n] <= 0)
  360. extent[n] = 0;
  361. }
  362. if (retarray->base_addr == NULL)
  363. {
  364. size_t alloc_size, str;
  365. for (n = 0; n < rank; n++)
  366. {
  367. if (n == 0)
  368. str = 1;
  369. else
  370. str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
  371. GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
  372. }
  373. retarray->offset = 0;
  374. retarray->dtype.rank = rank;
  375. alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
  376. retarray->base_addr = xmallocarray (alloc_size, sizeof (index_type));
  377. if (alloc_size == 0)
  378. {
  379. /* Make sure we have a zero-sized array. */
  380. GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
  381. return;
  382. }
  383. }
  384. else
  385. {
  386. if (rank != GFC_DESCRIPTOR_RANK (retarray))
  387. runtime_error ("rank of return array incorrect in"
  388. " FINDLOC intrinsic: is %ld, should be %ld",
  389. (long int) (GFC_DESCRIPTOR_RANK (retarray)),
  390. (long int) rank);
  391. if (unlikely (compile_options.bounds_check))
  392. bounds_ifunction_return ((array_t *) retarray, extent,
  393. "return value", "FINDLOC");
  394. }
  395. for (n = 0; n < rank; n++)
  396. {
  397. count[n] = 0;
  398. dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
  399. if (extent[n] <= 0)
  400. return;
  401. }
  402. dest = retarray->base_addr;
  403. continue_loop = 1;
  404. while (continue_loop)
  405. {
  406. *dest = 0;
  407. count[0]++;
  408. dest += dstride[0];
  409. n = 0;
  410. while (count[n] == extent[n])
  411. {
  412. count[n] = 0;
  413. dest -= dstride[n] * extent[n];
  414. n++;
  415. if (n >= rank)
  416. {
  417. continue_loop = 0;
  418. break;
  419. }
  420. else
  421. {
  422. count[n]++;
  423. dest += dstride[n];
  424. }
  425. }
  426. }
  427. }
  428. #endif'