findloc0_c8.c 9.3 KB

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