iall_i16.c 13 KB

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