unpack_c4.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* Specific implementation of the UNPACK intrinsic
  2. Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. Contributed by Thomas Koenig <tkoenig@gcc.gnu.org>, based on
  4. unpack_generic.c by Paul Brook <paul@nowt.org>.
  5. This file is part of the GNU Fortran runtime library (libgfortran).
  6. Libgfortran is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public
  8. License as published by the Free Software Foundation; either
  9. version 3 of the License, or (at your option) any later version.
  10. Ligbfortran is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. #include "libgfortran.h"
  22. #include <string.h>
  23. #if defined (HAVE_GFC_COMPLEX_4)
  24. void
  25. unpack0_c4 (gfc_array_c4 *ret, const gfc_array_c4 *vector,
  26. const gfc_array_l1 *mask, const GFC_COMPLEX_4 *fptr)
  27. {
  28. /* r.* indicates the return array. */
  29. index_type rstride[GFC_MAX_DIMENSIONS];
  30. index_type rstride0;
  31. index_type rs;
  32. GFC_COMPLEX_4 * restrict rptr;
  33. /* v.* indicates the vector array. */
  34. index_type vstride0;
  35. GFC_COMPLEX_4 *vptr;
  36. /* Value for field, this is constant. */
  37. const GFC_COMPLEX_4 fval = *fptr;
  38. /* m.* indicates the mask array. */
  39. index_type mstride[GFC_MAX_DIMENSIONS];
  40. index_type mstride0;
  41. const GFC_LOGICAL_1 *mptr;
  42. index_type count[GFC_MAX_DIMENSIONS];
  43. index_type extent[GFC_MAX_DIMENSIONS];
  44. index_type n;
  45. index_type dim;
  46. int empty;
  47. int mask_kind;
  48. empty = 0;
  49. mptr = mask->base_addr;
  50. /* Use the same loop for all logical types, by using GFC_LOGICAL_1
  51. and using shifting to address size and endian issues. */
  52. mask_kind = GFC_DESCRIPTOR_SIZE (mask);
  53. if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
  54. #ifdef HAVE_GFC_LOGICAL_16
  55. || mask_kind == 16
  56. #endif
  57. )
  58. {
  59. /* Do not convert a NULL pointer as we use test for NULL below. */
  60. if (mptr)
  61. mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
  62. }
  63. else
  64. runtime_error ("Funny sized logical array");
  65. /* Initialize to avoid -Wmaybe-uninitialized complaints. */
  66. rstride[0] = 1;
  67. if (ret->base_addr == NULL)
  68. {
  69. /* The front end has signalled that we need to populate the
  70. return array descriptor. */
  71. dim = GFC_DESCRIPTOR_RANK (mask);
  72. rs = 1;
  73. for (n = 0; n < dim; n++)
  74. {
  75. count[n] = 0;
  76. GFC_DIMENSION_SET(ret->dim[n], 0,
  77. GFC_DESCRIPTOR_EXTENT(mask,n) - 1, rs);
  78. extent[n] = GFC_DESCRIPTOR_EXTENT(ret,n);
  79. empty = empty || extent[n] <= 0;
  80. rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,n);
  81. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
  82. rs *= extent[n];
  83. }
  84. ret->offset = 0;
  85. ret->base_addr = xmallocarray (rs, sizeof (GFC_COMPLEX_4));
  86. }
  87. else
  88. {
  89. dim = GFC_DESCRIPTOR_RANK (ret);
  90. for (n = 0; n < dim; n++)
  91. {
  92. count[n] = 0;
  93. extent[n] = GFC_DESCRIPTOR_EXTENT(ret,n);
  94. empty = empty || extent[n] <= 0;
  95. rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,n);
  96. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
  97. }
  98. if (rstride[0] == 0)
  99. rstride[0] = 1;
  100. }
  101. if (empty)
  102. return;
  103. if (mstride[0] == 0)
  104. mstride[0] = 1;
  105. vstride0 = GFC_DESCRIPTOR_STRIDE(vector,0);
  106. if (vstride0 == 0)
  107. vstride0 = 1;
  108. rstride0 = rstride[0];
  109. mstride0 = mstride[0];
  110. rptr = ret->base_addr;
  111. vptr = vector->base_addr;
  112. while (rptr)
  113. {
  114. if (*mptr)
  115. {
  116. /* From vector. */
  117. *rptr = *vptr;
  118. vptr += vstride0;
  119. }
  120. else
  121. {
  122. /* From field. */
  123. *rptr = fval;
  124. }
  125. /* Advance to the next element. */
  126. rptr += rstride0;
  127. mptr += mstride0;
  128. count[0]++;
  129. n = 0;
  130. while (count[n] == extent[n])
  131. {
  132. /* When we get to the end of a dimension, reset it and increment
  133. the next dimension. */
  134. count[n] = 0;
  135. /* We could precalculate these products, but this is a less
  136. frequently used path so probably not worth it. */
  137. rptr -= rstride[n] * extent[n];
  138. mptr -= mstride[n] * extent[n];
  139. n++;
  140. if (n >= dim)
  141. {
  142. /* Break out of the loop. */
  143. rptr = NULL;
  144. break;
  145. }
  146. else
  147. {
  148. count[n]++;
  149. rptr += rstride[n];
  150. mptr += mstride[n];
  151. }
  152. }
  153. }
  154. }
  155. void
  156. unpack1_c4 (gfc_array_c4 *ret, const gfc_array_c4 *vector,
  157. const gfc_array_l1 *mask, const gfc_array_c4 *field)
  158. {
  159. /* r.* indicates the return array. */
  160. index_type rstride[GFC_MAX_DIMENSIONS];
  161. index_type rstride0;
  162. index_type rs;
  163. GFC_COMPLEX_4 * restrict rptr;
  164. /* v.* indicates the vector array. */
  165. index_type vstride0;
  166. GFC_COMPLEX_4 *vptr;
  167. /* f.* indicates the field array. */
  168. index_type fstride[GFC_MAX_DIMENSIONS];
  169. index_type fstride0;
  170. const GFC_COMPLEX_4 *fptr;
  171. /* m.* indicates the mask array. */
  172. index_type mstride[GFC_MAX_DIMENSIONS];
  173. index_type mstride0;
  174. const GFC_LOGICAL_1 *mptr;
  175. index_type count[GFC_MAX_DIMENSIONS];
  176. index_type extent[GFC_MAX_DIMENSIONS];
  177. index_type n;
  178. index_type dim;
  179. int empty;
  180. int mask_kind;
  181. empty = 0;
  182. mptr = mask->base_addr;
  183. /* Use the same loop for all logical types, by using GFC_LOGICAL_1
  184. and using shifting to address size and endian issues. */
  185. mask_kind = GFC_DESCRIPTOR_SIZE (mask);
  186. if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
  187. #ifdef HAVE_GFC_LOGICAL_16
  188. || mask_kind == 16
  189. #endif
  190. )
  191. {
  192. /* Do not convert a NULL pointer as we use test for NULL below. */
  193. if (mptr)
  194. mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
  195. }
  196. else
  197. runtime_error ("Funny sized logical array");
  198. /* Initialize to avoid -Wmaybe-uninitialized complaints. */
  199. rstride[0] = 1;
  200. if (ret->base_addr == NULL)
  201. {
  202. /* The front end has signalled that we need to populate the
  203. return array descriptor. */
  204. dim = GFC_DESCRIPTOR_RANK (mask);
  205. rs = 1;
  206. for (n = 0; n < dim; n++)
  207. {
  208. count[n] = 0;
  209. GFC_DIMENSION_SET(ret->dim[n], 0,
  210. GFC_DESCRIPTOR_EXTENT(mask,n) - 1, rs);
  211. extent[n] = GFC_DESCRIPTOR_EXTENT(ret,n);
  212. empty = empty || extent[n] <= 0;
  213. rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,n);
  214. fstride[n] = GFC_DESCRIPTOR_STRIDE(field,n);
  215. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
  216. rs *= extent[n];
  217. }
  218. ret->offset = 0;
  219. ret->base_addr = xmallocarray (rs, sizeof (GFC_COMPLEX_4));
  220. }
  221. else
  222. {
  223. dim = GFC_DESCRIPTOR_RANK (ret);
  224. for (n = 0; n < dim; n++)
  225. {
  226. count[n] = 0;
  227. extent[n] = GFC_DESCRIPTOR_EXTENT(ret,n);
  228. empty = empty || extent[n] <= 0;
  229. rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,n);
  230. fstride[n] = GFC_DESCRIPTOR_STRIDE(field,n);
  231. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
  232. }
  233. if (rstride[0] == 0)
  234. rstride[0] = 1;
  235. }
  236. if (empty)
  237. return;
  238. if (fstride[0] == 0)
  239. fstride[0] = 1;
  240. if (mstride[0] == 0)
  241. mstride[0] = 1;
  242. vstride0 = GFC_DESCRIPTOR_STRIDE(vector,0);
  243. if (vstride0 == 0)
  244. vstride0 = 1;
  245. rstride0 = rstride[0];
  246. fstride0 = fstride[0];
  247. mstride0 = mstride[0];
  248. rptr = ret->base_addr;
  249. fptr = field->base_addr;
  250. vptr = vector->base_addr;
  251. while (rptr)
  252. {
  253. if (*mptr)
  254. {
  255. /* From vector. */
  256. *rptr = *vptr;
  257. vptr += vstride0;
  258. }
  259. else
  260. {
  261. /* From field. */
  262. *rptr = *fptr;
  263. }
  264. /* Advance to the next element. */
  265. rptr += rstride0;
  266. fptr += fstride0;
  267. mptr += mstride0;
  268. count[0]++;
  269. n = 0;
  270. while (count[n] == extent[n])
  271. {
  272. /* When we get to the end of a dimension, reset it and increment
  273. the next dimension. */
  274. count[n] = 0;
  275. /* We could precalculate these products, but this is a less
  276. frequently used path so probably not worth it. */
  277. rptr -= rstride[n] * extent[n];
  278. fptr -= fstride[n] * extent[n];
  279. mptr -= mstride[n] * extent[n];
  280. n++;
  281. if (n >= dim)
  282. {
  283. /* Break out of the loop. */
  284. rptr = NULL;
  285. break;
  286. }
  287. else
  288. {
  289. count[n]++;
  290. rptr += rstride[n];
  291. fptr += fstride[n];
  292. mptr += mstride[n];
  293. }
  294. }
  295. }
  296. }
  297. #endif