maxval_i16.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /* Implementation of the MAXVAL 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. #if defined (HAVE_GFC_INTEGER_16) && defined (HAVE_GFC_INTEGER_16)
  22. extern void maxval_i16 (gfc_array_i16 * const restrict,
  23. gfc_array_i16 * const restrict, const index_type * const restrict);
  24. export_proto(maxval_i16);
  25. void
  26. maxval_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 MAXVAL 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. " MAXVAL 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", "MAXVAL");
  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. #if defined (GFC_INTEGER_16_INFINITY)
  119. result = -GFC_INTEGER_16_INFINITY;
  120. #else
  121. result = (-GFC_INTEGER_16_HUGE-1);
  122. #endif
  123. if (len <= 0)
  124. *dest = (-GFC_INTEGER_16_HUGE-1);
  125. else
  126. {
  127. #if ! defined HAVE_BACK_ARG
  128. for (n = 0; n < len; n++, src += delta)
  129. {
  130. #endif
  131. #if defined (GFC_INTEGER_16_QUIET_NAN)
  132. if (*src >= result)
  133. break;
  134. }
  135. if (unlikely (n >= len))
  136. result = GFC_INTEGER_16_QUIET_NAN;
  137. else for (; n < len; n++, src += delta)
  138. {
  139. #endif
  140. if (*src > result)
  141. result = *src;
  142. }
  143. *dest = result;
  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 mmaxval_i16 (gfc_array_i16 * const restrict,
  177. gfc_array_i16 * const restrict, const index_type * const restrict,
  178. gfc_array_l1 * const restrict);
  179. export_proto(mmaxval_i16);
  180. void
  181. mmaxval_i16 (gfc_array_i16 * const restrict retarray,
  182. gfc_array_i16 * const restrict array,
  183. const index_type * const restrict pdim,
  184. gfc_array_l1 * const restrict mask)
  185. {
  186. index_type count[GFC_MAX_DIMENSIONS];
  187. index_type extent[GFC_MAX_DIMENSIONS];
  188. index_type sstride[GFC_MAX_DIMENSIONS];
  189. index_type dstride[GFC_MAX_DIMENSIONS];
  190. index_type mstride[GFC_MAX_DIMENSIONS];
  191. GFC_INTEGER_16 * restrict dest;
  192. const GFC_INTEGER_16 * restrict base;
  193. const GFC_LOGICAL_1 * restrict mbase;
  194. index_type rank;
  195. index_type dim;
  196. index_type n;
  197. index_type len;
  198. index_type delta;
  199. index_type mdelta;
  200. int mask_kind;
  201. if (mask == NULL)
  202. {
  203. #ifdef HAVE_BACK_ARG
  204. maxval_i16 (retarray, array, pdim, back);
  205. #else
  206. maxval_i16 (retarray, array, pdim);
  207. #endif
  208. return;
  209. }
  210. dim = (*pdim) - 1;
  211. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  212. if (unlikely (dim < 0 || dim > rank))
  213. {
  214. runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
  215. "is %ld, should be between 1 and %ld",
  216. (long int) dim + 1, (long int) rank + 1);
  217. }
  218. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  219. if (len <= 0)
  220. return;
  221. mbase = mask->base_addr;
  222. mask_kind = GFC_DESCRIPTOR_SIZE (mask);
  223. if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
  224. #ifdef HAVE_GFC_LOGICAL_16
  225. || mask_kind == 16
  226. #endif
  227. )
  228. mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
  229. else
  230. runtime_error ("Funny sized logical array");
  231. delta = GFC_DESCRIPTOR_STRIDE(array,dim);
  232. mdelta = GFC_DESCRIPTOR_STRIDE_BYTES(mask,dim);
  233. for (n = 0; n < dim; n++)
  234. {
  235. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
  236. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
  237. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  238. if (extent[n] < 0)
  239. extent[n] = 0;
  240. }
  241. for (n = dim; n < rank; n++)
  242. {
  243. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n + 1);
  244. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask, n + 1);
  245. extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
  246. if (extent[n] < 0)
  247. extent[n] = 0;
  248. }
  249. if (retarray->base_addr == NULL)
  250. {
  251. size_t alloc_size, str;
  252. for (n = 0; n < rank; n++)
  253. {
  254. if (n == 0)
  255. str = 1;
  256. else
  257. str= GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
  258. GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
  259. }
  260. alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
  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_INTEGER_16));
  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);
  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_INTEGER_16 * restrict src;
  296. const GFC_LOGICAL_1 * restrict msrc;
  297. GFC_INTEGER_16 result;
  298. src = base;
  299. msrc = mbase;
  300. {
  301. #if defined (GFC_INTEGER_16_INFINITY)
  302. result = -GFC_INTEGER_16_INFINITY;
  303. #else
  304. result = (-GFC_INTEGER_16_HUGE-1);
  305. #endif
  306. #if defined (GFC_INTEGER_16_QUIET_NAN)
  307. int non_empty_p = 0;
  308. #endif
  309. for (n = 0; n < len; n++, src += delta, msrc += mdelta)
  310. {
  311. #if defined (GFC_INTEGER_16_INFINITY) || defined (GFC_INTEGER_16_QUIET_NAN)
  312. if (*msrc)
  313. {
  314. #if defined (GFC_INTEGER_16_QUIET_NAN)
  315. non_empty_p = 1;
  316. if (*src >= result)
  317. #endif
  318. break;
  319. }
  320. }
  321. if (unlikely (n >= len))
  322. {
  323. #if defined (GFC_INTEGER_16_QUIET_NAN)
  324. result = non_empty_p ? GFC_INTEGER_16_QUIET_NAN : (-GFC_INTEGER_16_HUGE-1);
  325. #else
  326. result = (-GFC_INTEGER_16_HUGE-1);
  327. #endif
  328. }
  329. else for (; n < len; n++, src += delta, msrc += mdelta)
  330. {
  331. #endif
  332. if (*msrc && *src > result)
  333. result = *src;
  334. }
  335. *dest = result;
  336. }
  337. /* Advance to the next element. */
  338. count[0]++;
  339. base += sstride[0];
  340. mbase += mstride[0];
  341. dest += dstride[0];
  342. n = 0;
  343. while (count[n] == extent[n])
  344. {
  345. /* When we get to the end of a dimension, reset it and increment
  346. the next dimension. */
  347. count[n] = 0;
  348. /* We could precalculate these products, but this is a less
  349. frequently used path so probably not worth it. */
  350. base -= sstride[n] * extent[n];
  351. mbase -= mstride[n] * extent[n];
  352. dest -= dstride[n] * extent[n];
  353. n++;
  354. if (n >= rank)
  355. {
  356. /* Break out of the loop. */
  357. base = NULL;
  358. break;
  359. }
  360. else
  361. {
  362. count[n]++;
  363. base += sstride[n];
  364. mbase += mstride[n];
  365. dest += dstride[n];
  366. }
  367. }
  368. }
  369. }
  370. extern void smaxval_i16 (gfc_array_i16 * const restrict,
  371. gfc_array_i16 * const restrict, const index_type * const restrict,
  372. GFC_LOGICAL_4 *);
  373. export_proto(smaxval_i16);
  374. void
  375. smaxval_i16 (gfc_array_i16 * const restrict retarray,
  376. gfc_array_i16 * const restrict array,
  377. const index_type * const restrict pdim,
  378. GFC_LOGICAL_4 * mask)
  379. {
  380. index_type count[GFC_MAX_DIMENSIONS];
  381. index_type extent[GFC_MAX_DIMENSIONS];
  382. index_type dstride[GFC_MAX_DIMENSIONS];
  383. GFC_INTEGER_16 * restrict dest;
  384. index_type rank;
  385. index_type n;
  386. index_type dim;
  387. if (mask == NULL || *mask)
  388. {
  389. #ifdef HAVE_BACK_ARG
  390. maxval_i16 (retarray, array, pdim, back);
  391. #else
  392. maxval_i16 (retarray, array, pdim);
  393. #endif
  394. return;
  395. }
  396. /* Make dim zero based to avoid confusion. */
  397. dim = (*pdim) - 1;
  398. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  399. if (unlikely (dim < 0 || dim > rank))
  400. {
  401. runtime_error ("Dim argument incorrect in MAXVAL intrinsic: "
  402. "is %ld, should be between 1 and %ld",
  403. (long int) dim + 1, (long int) rank + 1);
  404. }
  405. for (n = 0; n < dim; n++)
  406. {
  407. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  408. if (extent[n] <= 0)
  409. extent[n] = 0;
  410. }
  411. for (n = dim; n < rank; n++)
  412. {
  413. extent[n] =
  414. GFC_DESCRIPTOR_EXTENT(array,n + 1);
  415. if (extent[n] <= 0)
  416. extent[n] = 0;
  417. }
  418. if (retarray->base_addr == NULL)
  419. {
  420. size_t alloc_size, str;
  421. for (n = 0; n < rank; n++)
  422. {
  423. if (n == 0)
  424. str = 1;
  425. else
  426. str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
  427. GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
  428. }
  429. retarray->offset = 0;
  430. retarray->dtype.rank = rank;
  431. alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
  432. if (alloc_size == 0)
  433. {
  434. /* Make sure we have a zero-sized array. */
  435. GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
  436. return;
  437. }
  438. else
  439. retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_INTEGER_16));
  440. }
  441. else
  442. {
  443. if (rank != GFC_DESCRIPTOR_RANK (retarray))
  444. runtime_error ("rank of return array incorrect in"
  445. " MAXVAL intrinsic: is %ld, should be %ld",
  446. (long int) (GFC_DESCRIPTOR_RANK (retarray)),
  447. (long int) rank);
  448. if (unlikely (compile_options.bounds_check))
  449. {
  450. for (n=0; n < rank; n++)
  451. {
  452. index_type ret_extent;
  453. ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
  454. if (extent[n] != ret_extent)
  455. runtime_error ("Incorrect extent in return value of"
  456. " MAXVAL intrinsic in dimension %ld:"
  457. " is %ld, should be %ld", (long int) n + 1,
  458. (long int) ret_extent, (long int) extent[n]);
  459. }
  460. }
  461. }
  462. for (n = 0; n < rank; n++)
  463. {
  464. count[n] = 0;
  465. dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
  466. }
  467. dest = retarray->base_addr;
  468. while(1)
  469. {
  470. *dest = (-GFC_INTEGER_16_HUGE-1);
  471. count[0]++;
  472. dest += dstride[0];
  473. n = 0;
  474. while (count[n] == extent[n])
  475. {
  476. /* When we get to the end of a dimension, reset it and increment
  477. the next dimension. */
  478. count[n] = 0;
  479. /* We could precalculate these products, but this is a less
  480. frequently used path so probably not worth it. */
  481. dest -= dstride[n] * extent[n];
  482. n++;
  483. if (n >= rank)
  484. return;
  485. else
  486. {
  487. count[n]++;
  488. dest += dstride[n];
  489. }
  490. }
  491. }
  492. }
  493. #endif