minloc1_16_i16.c 14 KB

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