maxloc1_8_r10.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /* Implementation of the MAXLOC 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_REAL_10) && defined (HAVE_GFC_INTEGER_8)
  23. #define HAVE_BACK_ARG 1
  24. extern void maxloc1_8_r10 (gfc_array_i8 * const restrict,
  25. gfc_array_r10 * const restrict, const index_type * const restrict, GFC_LOGICAL_4 back);
  26. export_proto(maxloc1_8_r10);
  27. void
  28. maxloc1_8_r10 (gfc_array_i8 * const restrict retarray,
  29. gfc_array_r10 * 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_REAL_10 * restrict base;
  37. GFC_INTEGER_8 * 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 MAXLOC 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_8));
  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. " MAXLOC 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", "MAXLOC");
  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_REAL_10 * restrict src;
  117. GFC_INTEGER_8 result;
  118. src = base;
  119. {
  120. GFC_REAL_10 maxval;
  121. #if defined (GFC_REAL_10_INFINITY)
  122. maxval = -GFC_REAL_10_INFINITY;
  123. #else
  124. maxval = -GFC_REAL_10_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_REAL_10_QUIET_NAN)
  136. for (n = 0; n < len; n++, src += delta)
  137. {
  138. if (*src >= maxval)
  139. {
  140. maxval = *src;
  141. result = (GFC_INTEGER_8)n + 1;
  142. break;
  143. }
  144. }
  145. #else
  146. n = 0;
  147. #endif
  148. for (; n < len; n++, src += delta)
  149. {
  150. if (back ? *src >= maxval : *src > maxval)
  151. {
  152. maxval = *src;
  153. result = (GFC_INTEGER_8)n + 1;
  154. }
  155. }
  156. *dest = result;
  157. }
  158. }
  159. /* Advance to the next element. */
  160. count[0]++;
  161. base += sstride[0];
  162. dest += dstride[0];
  163. n = 0;
  164. while (count[n] == extent[n])
  165. {
  166. /* When we get to the end of a dimension, reset it and increment
  167. the next dimension. */
  168. count[n] = 0;
  169. /* We could precalculate these products, but this is a less
  170. frequently used path so probably not worth it. */
  171. base -= sstride[n] * extent[n];
  172. dest -= dstride[n] * extent[n];
  173. n++;
  174. if (n >= rank)
  175. {
  176. /* Break out of the loop. */
  177. continue_loop = 0;
  178. break;
  179. }
  180. else
  181. {
  182. count[n]++;
  183. base += sstride[n];
  184. dest += dstride[n];
  185. }
  186. }
  187. }
  188. }
  189. extern void mmaxloc1_8_r10 (gfc_array_i8 * const restrict,
  190. gfc_array_r10 * const restrict, const index_type * const restrict,
  191. gfc_array_l1 * const restrict, GFC_LOGICAL_4 back);
  192. export_proto(mmaxloc1_8_r10);
  193. void
  194. mmaxloc1_8_r10 (gfc_array_i8 * const restrict retarray,
  195. gfc_array_r10 * const restrict array,
  196. const index_type * const restrict pdim,
  197. gfc_array_l1 * const restrict mask, GFC_LOGICAL_4 back)
  198. {
  199. index_type count[GFC_MAX_DIMENSIONS];
  200. index_type extent[GFC_MAX_DIMENSIONS];
  201. index_type sstride[GFC_MAX_DIMENSIONS];
  202. index_type dstride[GFC_MAX_DIMENSIONS];
  203. index_type mstride[GFC_MAX_DIMENSIONS];
  204. GFC_INTEGER_8 * restrict dest;
  205. const GFC_REAL_10 * restrict base;
  206. const GFC_LOGICAL_1 * restrict mbase;
  207. index_type rank;
  208. index_type dim;
  209. index_type n;
  210. index_type len;
  211. index_type delta;
  212. index_type mdelta;
  213. int mask_kind;
  214. if (mask == NULL)
  215. {
  216. #ifdef HAVE_BACK_ARG
  217. maxloc1_8_r10 (retarray, array, pdim, back);
  218. #else
  219. maxloc1_8_r10 (retarray, array, pdim);
  220. #endif
  221. return;
  222. }
  223. dim = (*pdim) - 1;
  224. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  225. if (unlikely (dim < 0 || dim > rank))
  226. {
  227. runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
  228. "is %ld, should be between 1 and %ld",
  229. (long int) dim + 1, (long int) rank + 1);
  230. }
  231. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  232. if (len <= 0)
  233. return;
  234. mbase = mask->base_addr;
  235. mask_kind = GFC_DESCRIPTOR_SIZE (mask);
  236. if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
  237. #ifdef HAVE_GFC_LOGICAL_16
  238. || mask_kind == 16
  239. #endif
  240. )
  241. mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
  242. else
  243. runtime_error ("Funny sized logical array");
  244. delta = GFC_DESCRIPTOR_STRIDE(array,dim);
  245. mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim);
  246. for (n = 0; n < dim; n++)
  247. {
  248. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
  249. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
  250. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  251. if (extent[n] < 0)
  252. extent[n] = 0;
  253. }
  254. for (n = dim; n < rank; n++)
  255. {
  256. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n + 1);
  257. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask, n + 1);
  258. extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
  259. if (extent[n] < 0)
  260. extent[n] = 0;
  261. }
  262. if (retarray->base_addr == NULL)
  263. {
  264. size_t alloc_size, str;
  265. for (n = 0; n < rank; n++)
  266. {
  267. if (n == 0)
  268. str = 1;
  269. else
  270. str= GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
  271. GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
  272. }
  273. alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
  274. retarray->offset = 0;
  275. retarray->dtype.rank = rank;
  276. if (alloc_size == 0)
  277. {
  278. /* Make sure we have a zero-sized array. */
  279. GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
  280. return;
  281. }
  282. else
  283. retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_8));
  284. }
  285. else
  286. {
  287. if (rank != GFC_DESCRIPTOR_RANK (retarray))
  288. runtime_error ("rank of return array incorrect in MAXLOC intrinsic");
  289. if (unlikely (compile_options.bounds_check))
  290. {
  291. bounds_ifunction_return ((array_t *) retarray, extent,
  292. "return value", "MAXLOC");
  293. bounds_equal_extents ((array_t *) mask, (array_t *) array,
  294. "MASK argument", "MAXLOC");
  295. }
  296. }
  297. for (n = 0; n < rank; n++)
  298. {
  299. count[n] = 0;
  300. dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
  301. if (extent[n] <= 0)
  302. return;
  303. }
  304. dest = retarray->base_addr;
  305. base = array->base_addr;
  306. while (base)
  307. {
  308. const GFC_REAL_10 * restrict src;
  309. const GFC_LOGICAL_1 * restrict msrc;
  310. GFC_INTEGER_8 result;
  311. src = base;
  312. msrc = mbase;
  313. {
  314. GFC_REAL_10 maxval;
  315. #if defined (GFC_REAL_10_INFINITY)
  316. maxval = -GFC_REAL_10_INFINITY;
  317. #else
  318. maxval = -GFC_REAL_10_HUGE;
  319. #endif
  320. #if defined (GFC_REAL_10_QUIET_NAN)
  321. GFC_INTEGER_8 result2 = 0;
  322. #endif
  323. result = 0;
  324. for (n = 0; n < len; n++, src += delta, msrc += mdelta)
  325. {
  326. if (*msrc)
  327. {
  328. #if defined (GFC_REAL_10_QUIET_NAN)
  329. if (!result2)
  330. result2 = (GFC_INTEGER_8)n + 1;
  331. if (*src >= maxval)
  332. #endif
  333. {
  334. maxval = *src;
  335. result = (GFC_INTEGER_8)n + 1;
  336. break;
  337. }
  338. }
  339. }
  340. #if defined (GFC_REAL_10_QUIET_NAN)
  341. if (unlikely (n >= len))
  342. result = result2;
  343. else
  344. #endif
  345. if (back)
  346. for (; n < len; n++, src += delta, msrc += mdelta)
  347. {
  348. if (*msrc && unlikely (*src >= maxval))
  349. {
  350. maxval = *src;
  351. result = (GFC_INTEGER_8)n + 1;
  352. }
  353. }
  354. else
  355. for (; n < len; n++, src += delta, msrc += mdelta)
  356. {
  357. if (*msrc && unlikely (*src > maxval))
  358. {
  359. maxval = *src;
  360. result = (GFC_INTEGER_8)n + 1;
  361. }
  362. }
  363. *dest = result;
  364. }
  365. /* Advance to the next element. */
  366. count[0]++;
  367. base += sstride[0];
  368. mbase += mstride[0];
  369. dest += dstride[0];
  370. n = 0;
  371. while (count[n] == extent[n])
  372. {
  373. /* When we get to the end of a dimension, reset it and increment
  374. the next dimension. */
  375. count[n] = 0;
  376. /* We could precalculate these products, but this is a less
  377. frequently used path so probably not worth it. */
  378. base -= sstride[n] * extent[n];
  379. mbase -= mstride[n] * extent[n];
  380. dest -= dstride[n] * extent[n];
  381. n++;
  382. if (n >= rank)
  383. {
  384. /* Break out of the loop. */
  385. base = NULL;
  386. break;
  387. }
  388. else
  389. {
  390. count[n]++;
  391. base += sstride[n];
  392. mbase += mstride[n];
  393. dest += dstride[n];
  394. }
  395. }
  396. }
  397. }
  398. extern void smaxloc1_8_r10 (gfc_array_i8 * const restrict,
  399. gfc_array_r10 * const restrict, const index_type * const restrict,
  400. GFC_LOGICAL_4 *, GFC_LOGICAL_4 back);
  401. export_proto(smaxloc1_8_r10);
  402. void
  403. smaxloc1_8_r10 (gfc_array_i8 * const restrict retarray,
  404. gfc_array_r10 * const restrict array,
  405. const index_type * const restrict pdim,
  406. GFC_LOGICAL_4 * mask, GFC_LOGICAL_4 back)
  407. {
  408. index_type count[GFC_MAX_DIMENSIONS];
  409. index_type extent[GFC_MAX_DIMENSIONS];
  410. index_type dstride[GFC_MAX_DIMENSIONS];
  411. GFC_INTEGER_8 * restrict dest;
  412. index_type rank;
  413. index_type n;
  414. index_type dim;
  415. if (mask == NULL || *mask)
  416. {
  417. #ifdef HAVE_BACK_ARG
  418. maxloc1_8_r10 (retarray, array, pdim, back);
  419. #else
  420. maxloc1_8_r10 (retarray, array, pdim);
  421. #endif
  422. return;
  423. }
  424. /* Make dim zero based to avoid confusion. */
  425. dim = (*pdim) - 1;
  426. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  427. if (unlikely (dim < 0 || dim > rank))
  428. {
  429. runtime_error ("Dim argument incorrect in MAXLOC intrinsic: "
  430. "is %ld, should be between 1 and %ld",
  431. (long int) dim + 1, (long int) rank + 1);
  432. }
  433. for (n = 0; n < dim; n++)
  434. {
  435. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  436. if (extent[n] <= 0)
  437. extent[n] = 0;
  438. }
  439. for (n = dim; n < rank; n++)
  440. {
  441. extent[n] =
  442. GFC_DESCRIPTOR_EXTENT(array,n + 1);
  443. if (extent[n] <= 0)
  444. extent[n] = 0;
  445. }
  446. if (retarray->base_addr == NULL)
  447. {
  448. size_t alloc_size, str;
  449. for (n = 0; n < rank; n++)
  450. {
  451. if (n == 0)
  452. str = 1;
  453. else
  454. str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
  455. GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
  456. }
  457. retarray->offset = 0;
  458. retarray->dtype.rank = rank;
  459. alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
  460. if (alloc_size == 0)
  461. {
  462. /* Make sure we have a zero-sized array. */
  463. GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
  464. return;
  465. }
  466. else
  467. retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_8));
  468. }
  469. else
  470. {
  471. if (rank != GFC_DESCRIPTOR_RANK (retarray))
  472. runtime_error ("rank of return array incorrect in"
  473. " MAXLOC intrinsic: is %ld, should be %ld",
  474. (long int) (GFC_DESCRIPTOR_RANK (retarray)),
  475. (long int) rank);
  476. if (unlikely (compile_options.bounds_check))
  477. {
  478. for (n=0; n < rank; n++)
  479. {
  480. index_type ret_extent;
  481. ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
  482. if (extent[n] != ret_extent)
  483. runtime_error ("Incorrect extent in return value of"
  484. " MAXLOC intrinsic in dimension %ld:"
  485. " is %ld, should be %ld", (long int) n + 1,
  486. (long int) ret_extent, (long int) extent[n]);
  487. }
  488. }
  489. }
  490. for (n = 0; n < rank; n++)
  491. {
  492. count[n] = 0;
  493. dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
  494. }
  495. dest = retarray->base_addr;
  496. while(1)
  497. {
  498. *dest = 0;
  499. count[0]++;
  500. dest += dstride[0];
  501. n = 0;
  502. while (count[n] == extent[n])
  503. {
  504. /* When we get to the end of a dimension, reset it and increment
  505. the next dimension. */
  506. count[n] = 0;
  507. /* We could precalculate these products, but this is a less
  508. frequently used path so probably not worth it. */
  509. dest -= dstride[n] * extent[n];
  510. n++;
  511. if (n >= rank)
  512. return;
  513. else
  514. {
  515. count[n]++;
  516. dest += dstride[n];
  517. }
  518. }
  519. }
  520. }
  521. #endif