minloc1_8_s1.c 14 KB

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