findloc0_s4.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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_UINTEGER_4)
  23. extern void findloc0_s4 (gfc_array_index_type * const restrict retarray,
  24. gfc_array_s4 * const restrict array, GFC_UINTEGER_4 *value,
  25. GFC_LOGICAL_4 back, gfc_charlen_type len_array, gfc_charlen_type len_value);
  26. export_proto(findloc0_s4);
  27. void
  28. findloc0_s4 (gfc_array_index_type * const restrict retarray,
  29. gfc_array_s4 * const restrict array, GFC_UINTEGER_4 *value,
  30. GFC_LOGICAL_4 back, gfc_charlen_type len_array, gfc_charlen_type len_value)
  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_UINTEGER_4 *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) * len_array;
  76. while (1)
  77. {
  78. do
  79. {
  80. if (unlikely(compare_string_char4 (len_array, base, len_value, value) == 0))
  81. {
  82. for (n = 0; n < rank; n++)
  83. dest[n * dstride] = extent[n] - count[n];
  84. return;
  85. }
  86. base -= sstride[0] * len_array;
  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] * len_array;
  97. n++;
  98. if (n >= rank)
  99. return;
  100. else
  101. {
  102. count[n]++;
  103. base -= sstride[n] * len_array;
  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(compare_string_char4 (len_array, base, len_value, value) == 0))
  116. {
  117. for (n = 0; n < rank; n++)
  118. dest[n * dstride] = count[n] + 1;
  119. return;
  120. }
  121. base += sstride[0] * len_array;
  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] * len_array;
  132. n++;
  133. if (n >= rank)
  134. return;
  135. else
  136. {
  137. count[n]++;
  138. base += sstride[n] * len_array;
  139. }
  140. } while (count[n] == extent[n]);
  141. }
  142. }
  143. return;
  144. }
  145. extern void mfindloc0_s4 (gfc_array_index_type * const restrict retarray,
  146. gfc_array_s4 * const restrict array, GFC_UINTEGER_4 *value,
  147. gfc_array_l1 *const restrict, GFC_LOGICAL_4 back, gfc_charlen_type len_array,
  148. gfc_charlen_type len_value);
  149. export_proto(mfindloc0_s4);
  150. void
  151. mfindloc0_s4 (gfc_array_index_type * const restrict retarray,
  152. gfc_array_s4 * const restrict array, GFC_UINTEGER_4 *value,
  153. gfc_array_l1 *const restrict mask, GFC_LOGICAL_4 back,
  154. gfc_charlen_type len_array, gfc_charlen_type len_value)
  155. {
  156. index_type count[GFC_MAX_DIMENSIONS];
  157. index_type extent[GFC_MAX_DIMENSIONS];
  158. index_type sstride[GFC_MAX_DIMENSIONS];
  159. index_type mstride[GFC_MAX_DIMENSIONS];
  160. index_type dstride;
  161. const GFC_UINTEGER_4 *base;
  162. index_type * restrict dest;
  163. GFC_LOGICAL_1 *mbase;
  164. index_type rank;
  165. index_type n;
  166. int mask_kind;
  167. index_type sz;
  168. rank = GFC_DESCRIPTOR_RANK (array);
  169. if (rank <= 0)
  170. runtime_error ("Rank of array needs to be > 0");
  171. if (retarray->base_addr == NULL)
  172. {
  173. GFC_DIMENSION_SET(retarray->dim[0], 0, rank-1, 1);
  174. retarray->dtype.rank = 1;
  175. retarray->offset = 0;
  176. retarray->base_addr = xmallocarray (rank, sizeof (index_type));
  177. }
  178. else
  179. {
  180. if (unlikely (compile_options.bounds_check))
  181. {
  182. bounds_iforeach_return ((array_t *) retarray, (array_t *) array,
  183. "FINDLOC");
  184. bounds_equal_extents ((array_t *) mask, (array_t *) array,
  185. "MASK argument", "FINDLOC");
  186. }
  187. }
  188. mask_kind = GFC_DESCRIPTOR_SIZE (mask);
  189. mbase = mask->base_addr;
  190. if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
  191. #ifdef HAVE_GFC_LOGICAL_16
  192. || mask_kind == 16
  193. #endif
  194. )
  195. mbase = GFOR_POINTER_TO_L1 (mbase, mask_kind);
  196. else
  197. internal_error (NULL, "Funny sized logical array");
  198. dstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
  199. dest = retarray->base_addr;
  200. /* Set the return value. */
  201. for (n = 0; n < rank; n++)
  202. dest[n * dstride] = 0;
  203. sz = 1;
  204. for (n = 0; n < rank; n++)
  205. {
  206. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
  207. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
  208. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  209. sz *= extent[n];
  210. if (extent[n] <= 0)
  211. return;
  212. }
  213. for (n = 0; n < rank; n++)
  214. count[n] = 0;
  215. if (back)
  216. {
  217. base = array->base_addr + (sz - 1) * len_array;
  218. mbase = mbase + (sz - 1) * mask_kind;
  219. while (1)
  220. {
  221. do
  222. {
  223. if (unlikely(*mbase && compare_string_char4 (len_array, base, len_value, value) == 0))
  224. {
  225. for (n = 0; n < rank; n++)
  226. dest[n * dstride] = extent[n] - count[n];
  227. return;
  228. }
  229. base -= sstride[0] * len_array;
  230. mbase -= mstride[0];
  231. } while(++count[0] != extent[0]);
  232. n = 0;
  233. do
  234. {
  235. /* When we get to the end of a dimension, reset it and increment
  236. the next dimension. */
  237. count[n] = 0;
  238. /* We could precalculate these products, but this is a less
  239. frequently used path so probably not worth it. */
  240. base += sstride[n] * extent[n] * len_array;
  241. mbase -= mstride[n] * extent[n];
  242. n++;
  243. if (n >= rank)
  244. return;
  245. else
  246. {
  247. count[n]++;
  248. base -= sstride[n] * len_array;
  249. mbase += mstride[n];
  250. }
  251. } while (count[n] == extent[n]);
  252. }
  253. }
  254. else
  255. {
  256. base = array->base_addr;
  257. while (1)
  258. {
  259. do
  260. {
  261. if (unlikely(*mbase && compare_string_char4 (len_array, base, len_value, value) == 0))
  262. {
  263. for (n = 0; n < rank; n++)
  264. dest[n * dstride] = count[n] + 1;
  265. return;
  266. }
  267. base += sstride[0] * len_array;
  268. mbase += mstride[0];
  269. } while(++count[0] != extent[0]);
  270. n = 0;
  271. do
  272. {
  273. /* When we get to the end of a dimension, reset it and increment
  274. the next dimension. */
  275. count[n] = 0;
  276. /* We could precalculate these products, but this is a less
  277. frequently used path so probably not worth it. */
  278. base -= sstride[n] * extent[n] * len_array;
  279. mbase -= mstride[n] * extent[n];
  280. n++;
  281. if (n >= rank)
  282. return;
  283. else
  284. {
  285. count[n]++;
  286. base += sstride[n]* len_array;
  287. mbase += mstride[n];
  288. }
  289. } while (count[n] == extent[n]);
  290. }
  291. }
  292. return;
  293. }
  294. extern void sfindloc0_s4 (gfc_array_index_type * const restrict retarray,
  295. gfc_array_s4 * const restrict array, GFC_UINTEGER_4 *value,
  296. GFC_LOGICAL_4 *, GFC_LOGICAL_4 back, gfc_charlen_type len_array,
  297. gfc_charlen_type len_value);
  298. export_proto(sfindloc0_s4);
  299. void
  300. sfindloc0_s4 (gfc_array_index_type * const restrict retarray,
  301. gfc_array_s4 * const restrict array, GFC_UINTEGER_4 *value,
  302. GFC_LOGICAL_4 * mask, GFC_LOGICAL_4 back, gfc_charlen_type len_array,
  303. gfc_charlen_type len_value)
  304. {
  305. index_type rank;
  306. index_type dstride;
  307. index_type * restrict dest;
  308. index_type n;
  309. if (mask == NULL || *mask)
  310. {
  311. findloc0_s4 (retarray, array, value, back, len_array, len_value);
  312. return;
  313. }
  314. rank = GFC_DESCRIPTOR_RANK (array);
  315. if (rank <= 0)
  316. internal_error (NULL, "Rank of array needs to be > 0");
  317. if (retarray->base_addr == NULL)
  318. {
  319. GFC_DIMENSION_SET(retarray->dim[0], 0, rank-1, 1);
  320. retarray->dtype.rank = 1;
  321. retarray->offset = 0;
  322. retarray->base_addr = xmallocarray (rank, sizeof (index_type));
  323. }
  324. else if (unlikely (compile_options.bounds_check))
  325. {
  326. bounds_iforeach_return ((array_t *) retarray, (array_t *) array,
  327. "FINDLOC");
  328. }
  329. dstride = GFC_DESCRIPTOR_STRIDE(retarray,0);
  330. dest = retarray->base_addr;
  331. for (n = 0; n<rank; n++)
  332. dest[n * dstride] = 0 ;
  333. }
  334. #endif