minloc1_16_r16.c 13 KB

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