maxloc0_16_r8.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 95 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_8) && defined (HAVE_GFC_INTEGER_16)
  23. extern void maxloc0_16_r8 (gfc_array_i16 * const restrict retarray,
  24. gfc_array_r8 * const restrict array, GFC_LOGICAL_4);
  25. export_proto(maxloc0_16_r8);
  26. void
  27. maxloc0_16_r8 (gfc_array_i16 * const restrict retarray,
  28. gfc_array_r8 * const restrict array, GFC_LOGICAL_4 back)
  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;
  34. const GFC_REAL_8 *base;
  35. GFC_INTEGER_16 * restrict dest;
  36. index_type rank;
  37. index_type n;
  38. rank = GFC_DESCRIPTOR_RANK (array);
  39. if (rank <= 0)
  40. runtime_error ("Rank of array needs to be > 0");
  41. if (retarray->base_addr == NULL)
  42. {
  43. GFC_DIMENSION_SET(retarray->dim[0], 0, rank-1, 1);
  44. retarray->dtype.rank = 1;
  45. retarray->offset = 0;
  46. retarray->base_addr = xmallocarray (rank, sizeof (GFC_INTEGER_16));
  47. }
  48. else
  49. {
  50. if (unlikely (compile_options.bounds_check))
  51. bounds_iforeach_return ((array_t *) retarray, (array_t *) array,
  52. "MAXLOC");
  53. }
  54. dstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
  55. dest = retarray->base_addr;
  56. for (n = 0; n < rank; n++)
  57. {
  58. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
  59. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  60. count[n] = 0;
  61. if (extent[n] <= 0)
  62. {
  63. /* Set the return value. */
  64. for (n = 0; n < rank; n++)
  65. dest[n * dstride] = 0;
  66. return;
  67. }
  68. }
  69. base = array->base_addr;
  70. /* Initialize the return value. */
  71. for (n = 0; n < rank; n++)
  72. dest[n * dstride] = 1;
  73. {
  74. GFC_REAL_8 maxval;
  75. #if defined(GFC_REAL_8_QUIET_NAN)
  76. int fast = 0;
  77. #endif
  78. #if defined(GFC_REAL_8_INFINITY)
  79. maxval = -GFC_REAL_8_INFINITY;
  80. #else
  81. maxval = -GFC_REAL_8_HUGE;
  82. #endif
  83. while (base)
  84. {
  85. /* Implementation start. */
  86. #if defined(GFC_REAL_8_QUIET_NAN)
  87. if (unlikely (!fast))
  88. {
  89. do
  90. {
  91. if (*base >= maxval)
  92. {
  93. fast = 1;
  94. maxval = *base;
  95. for (n = 0; n < rank; n++)
  96. dest[n * dstride] = count[n] + 1;
  97. break;
  98. }
  99. base += sstride[0];
  100. }
  101. while (++count[0] != extent[0]);
  102. if (likely (fast))
  103. continue;
  104. }
  105. else
  106. #endif
  107. if (back)
  108. do
  109. {
  110. if (unlikely (*base >= maxval))
  111. {
  112. maxval = *base;
  113. for (n = 0; n < rank; n++)
  114. dest[n * dstride] = count[n] + 1;
  115. }
  116. base += sstride[0];
  117. }
  118. while (++count[0] != extent[0]);
  119. else
  120. do
  121. {
  122. if (unlikely (*base > maxval))
  123. {
  124. maxval = *base;
  125. for (n = 0; n < rank; n++)
  126. dest[n * dstride] = count[n] + 1;
  127. }
  128. /* Implementation end. */
  129. /* Advance to the next element. */
  130. base += sstride[0];
  131. }
  132. while (++count[0] != extent[0]);
  133. n = 0;
  134. do
  135. {
  136. /* When we get to the end of a dimension, reset it and increment
  137. the next dimension. */
  138. count[n] = 0;
  139. /* We could precalculate these products, but this is a less
  140. frequently used path so probably not worth it. */
  141. base -= sstride[n] * extent[n];
  142. n++;
  143. if (n >= rank)
  144. {
  145. /* Break out of the loop. */
  146. base = NULL;
  147. break;
  148. }
  149. else
  150. {
  151. count[n]++;
  152. base += sstride[n];
  153. }
  154. }
  155. while (count[n] == extent[n]);
  156. }
  157. }
  158. }
  159. extern void mmaxloc0_16_r8 (gfc_array_i16 * const restrict,
  160. gfc_array_r8 * const restrict, gfc_array_l1 * const restrict,
  161. GFC_LOGICAL_4);
  162. export_proto(mmaxloc0_16_r8);
  163. void
  164. mmaxloc0_16_r8 (gfc_array_i16 * const restrict retarray,
  165. gfc_array_r8 * const restrict array,
  166. gfc_array_l1 * const restrict mask, GFC_LOGICAL_4 back)
  167. {
  168. index_type count[GFC_MAX_DIMENSIONS];
  169. index_type extent[GFC_MAX_DIMENSIONS];
  170. index_type sstride[GFC_MAX_DIMENSIONS];
  171. index_type mstride[GFC_MAX_DIMENSIONS];
  172. index_type dstride;
  173. GFC_INTEGER_16 *dest;
  174. const GFC_REAL_8 *base;
  175. GFC_LOGICAL_1 *mbase;
  176. int rank;
  177. index_type n;
  178. int mask_kind;
  179. if (mask == NULL)
  180. {
  181. maxloc0_16_r8 (retarray, array, back);
  182. return;
  183. }
  184. rank = GFC_DESCRIPTOR_RANK (array);
  185. if (rank <= 0)
  186. runtime_error ("Rank of array needs to be > 0");
  187. if (retarray->base_addr == NULL)
  188. {
  189. GFC_DIMENSION_SET(retarray->dim[0], 0, rank - 1, 1);
  190. retarray->dtype.rank = 1;
  191. retarray->offset = 0;
  192. retarray->base_addr = xmallocarray (rank, sizeof (GFC_INTEGER_16));
  193. }
  194. else
  195. {
  196. if (unlikely (compile_options.bounds_check))
  197. {
  198. bounds_iforeach_return ((array_t *) retarray, (array_t *) array,
  199. "MAXLOC");
  200. bounds_equal_extents ((array_t *) mask, (array_t *) array,
  201. "MASK argument", "MAXLOC");
  202. }
  203. }
  204. mask_kind = GFC_DESCRIPTOR_SIZE (mask);
  205. mbase = mask->base_addr;
  206. if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
  207. #ifdef HAVE_GFC_LOGICAL_16
  208. || mask_kind == 16
  209. #endif
  210. )
  211. mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
  212. else
  213. runtime_error ("Funny sized logical array");
  214. dstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
  215. dest = retarray->base_addr;
  216. for (n = 0; n < rank; n++)
  217. {
  218. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
  219. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
  220. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  221. count[n] = 0;
  222. if (extent[n] <= 0)
  223. {
  224. /* Set the return value. */
  225. for (n = 0; n < rank; n++)
  226. dest[n * dstride] = 0;
  227. return;
  228. }
  229. }
  230. base = array->base_addr;
  231. /* Initialize the return value. */
  232. for (n = 0; n < rank; n++)
  233. dest[n * dstride] = 0;
  234. {
  235. GFC_REAL_8 maxval;
  236. int fast = 0;
  237. #if defined(GFC_REAL_8_INFINITY)
  238. maxval = -GFC_REAL_8_INFINITY;
  239. #else
  240. maxval = -GFC_REAL_8_HUGE;
  241. #endif
  242. while (base)
  243. {
  244. /* Implementation start. */
  245. if (unlikely (!fast))
  246. {
  247. do
  248. {
  249. if (*mbase)
  250. {
  251. #if defined(GFC_REAL_8_QUIET_NAN)
  252. if (unlikely (dest[0] == 0))
  253. for (n = 0; n < rank; n++)
  254. dest[n * dstride] = count[n] + 1;
  255. if (*base >= maxval)
  256. #endif
  257. {
  258. fast = 1;
  259. maxval = *base;
  260. for (n = 0; n < rank; n++)
  261. dest[n * dstride] = count[n] + 1;
  262. break;
  263. }
  264. }
  265. base += sstride[0];
  266. mbase += mstride[0];
  267. }
  268. while (++count[0] != extent[0]);
  269. if (likely (fast))
  270. continue;
  271. }
  272. else
  273. if (back)
  274. do
  275. {
  276. if (*mbase && *base >= maxval)
  277. {
  278. maxval = *base;
  279. for (n = 0; n < rank; n++)
  280. dest[n * dstride] = count[n] + 1;
  281. }
  282. base += sstride[0];
  283. }
  284. while (++count[0] != extent[0]);
  285. else
  286. do
  287. {
  288. if (*mbase && unlikely (*base > maxval))
  289. {
  290. maxval = *base;
  291. for (n = 0; n < rank; n++)
  292. dest[n * dstride] = count[n] + 1;
  293. }
  294. /* Implementation end. */
  295. /* Advance to the next element. */
  296. base += sstride[0];
  297. mbase += mstride[0];
  298. }
  299. while (++count[0] != extent[0]);
  300. n = 0;
  301. do
  302. {
  303. /* When we get to the end of a dimension, reset it and increment
  304. the next dimension. */
  305. count[n] = 0;
  306. /* We could precalculate these products, but this is a less
  307. frequently used path so probably not worth it. */
  308. base -= sstride[n] * extent[n];
  309. mbase -= mstride[n] * extent[n];
  310. n++;
  311. if (n >= rank)
  312. {
  313. /* Break out of the loop. */
  314. base = NULL;
  315. break;
  316. }
  317. else
  318. {
  319. count[n]++;
  320. base += sstride[n];
  321. mbase += mstride[n];
  322. }
  323. }
  324. while (count[n] == extent[n]);
  325. }
  326. }
  327. }
  328. extern void smaxloc0_16_r8 (gfc_array_i16 * const restrict,
  329. gfc_array_r8 * const restrict, GFC_LOGICAL_4 *, GFC_LOGICAL_4);
  330. export_proto(smaxloc0_16_r8);
  331. void
  332. smaxloc0_16_r8 (gfc_array_i16 * const restrict retarray,
  333. gfc_array_r8 * const restrict array,
  334. GFC_LOGICAL_4 * mask, GFC_LOGICAL_4 back)
  335. {
  336. index_type rank;
  337. index_type dstride;
  338. index_type n;
  339. GFC_INTEGER_16 *dest;
  340. if (mask == NULL || *mask)
  341. {
  342. maxloc0_16_r8 (retarray, array, back);
  343. return;
  344. }
  345. rank = GFC_DESCRIPTOR_RANK (array);
  346. if (rank <= 0)
  347. runtime_error ("Rank of array needs to be > 0");
  348. if (retarray->base_addr == NULL)
  349. {
  350. GFC_DIMENSION_SET(retarray->dim[0], 0, rank-1, 1);
  351. retarray->dtype.rank = 1;
  352. retarray->offset = 0;
  353. retarray->base_addr = xmallocarray (rank, sizeof (GFC_INTEGER_16));
  354. }
  355. else if (unlikely (compile_options.bounds_check))
  356. {
  357. bounds_iforeach_return ((array_t *) retarray, (array_t *) array,
  358. "MAXLOC");
  359. }
  360. dstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
  361. dest = retarray->base_addr;
  362. for (n = 0; n<rank; n++)
  363. dest[n * dstride] = 0 ;
  364. }
  365. #endif