maxloc0_4_r16.c 8.9 KB

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