minval_i4.c 13 KB

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