maxval1_s4.c 14 KB

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