minloc0_4_s1.c 8.6 KB

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