unpack_c16.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* Specific implementation of the UNPACK intrinsic
  2. Copyright (C) 2008-2015 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 <stdlib.h>
  23. #include <assert.h>
  24. #include <string.h>
  25. #if defined (HAVE_GFC_COMPLEX_16)
  26. void
  27. unpack0_c16 (gfc_array_c16 *ret, const gfc_array_c16 *vector,
  28. const gfc_array_l1 *mask, const GFC_COMPLEX_16 *fptr)
  29. {
  30. /* r.* indicates the return array. */
  31. index_type rstride[GFC_MAX_DIMENSIONS];
  32. index_type rstride0;
  33. index_type rs;
  34. GFC_COMPLEX_16 * restrict rptr;
  35. /* v.* indicates the vector array. */
  36. index_type vstride0;
  37. GFC_COMPLEX_16 *vptr;
  38. /* Value for field, this is constant. */
  39. const GFC_COMPLEX_16 fval = *fptr;
  40. /* m.* indicates the mask array. */
  41. index_type mstride[GFC_MAX_DIMENSIONS];
  42. index_type mstride0;
  43. const GFC_LOGICAL_1 *mptr;
  44. index_type count[GFC_MAX_DIMENSIONS];
  45. index_type extent[GFC_MAX_DIMENSIONS];
  46. index_type n;
  47. index_type dim;
  48. int empty;
  49. int mask_kind;
  50. empty = 0;
  51. mptr = mask->base_addr;
  52. /* Use the same loop for all logical types, by using GFC_LOGICAL_1
  53. and using shifting to address size and endian issues. */
  54. mask_kind = GFC_DESCRIPTOR_SIZE (mask);
  55. if (mask_kind == 1 || mask_kind == 2 || mask_kind == 4 || mask_kind == 8
  56. #ifdef HAVE_GFC_LOGICAL_16
  57. || mask_kind == 16
  58. #endif
  59. )
  60. {
  61. /* Do not convert a NULL pointer as we use test for NULL below. */
  62. if (mptr)
  63. mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
  64. }
  65. else
  66. runtime_error ("Funny sized logical array");
  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_16));
  86. }
  87. else
  88. {
  89. dim = GFC_DESCRIPTOR_RANK (ret);
  90. /* Initialize to avoid -Wmaybe-uninitialized complaints. */
  91. rstride[0] = 1;
  92. for (n = 0; n < dim; n++)
  93. {
  94. count[n] = 0;
  95. extent[n] = GFC_DESCRIPTOR_EXTENT(ret,n);
  96. empty = empty || extent[n] <= 0;
  97. rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,n);
  98. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
  99. }
  100. if (rstride[0] == 0)
  101. rstride[0] = 1;
  102. }
  103. if (empty)
  104. return;
  105. if (mstride[0] == 0)
  106. mstride[0] = 1;
  107. vstride0 = GFC_DESCRIPTOR_STRIDE(vector,0);
  108. if (vstride0 == 0)
  109. vstride0 = 1;
  110. rstride0 = rstride[0];
  111. mstride0 = mstride[0];
  112. rptr = ret->base_addr;
  113. vptr = vector->base_addr;
  114. while (rptr)
  115. {
  116. if (*mptr)
  117. {
  118. /* From vector. */
  119. *rptr = *vptr;
  120. vptr += vstride0;
  121. }
  122. else
  123. {
  124. /* From field. */
  125. *rptr = fval;
  126. }
  127. /* Advance to the next element. */
  128. rptr += rstride0;
  129. mptr += mstride0;
  130. count[0]++;
  131. n = 0;
  132. while (count[n] == extent[n])
  133. {
  134. /* When we get to the end of a dimension, reset it and increment
  135. the next dimension. */
  136. count[n] = 0;
  137. /* We could precalculate these products, but this is a less
  138. frequently used path so probably not worth it. */
  139. rptr -= rstride[n] * extent[n];
  140. mptr -= mstride[n] * extent[n];
  141. n++;
  142. if (n >= dim)
  143. {
  144. /* Break out of the loop. */
  145. rptr = NULL;
  146. break;
  147. }
  148. else
  149. {
  150. count[n]++;
  151. rptr += rstride[n];
  152. mptr += mstride[n];
  153. }
  154. }
  155. }
  156. }
  157. void
  158. unpack1_c16 (gfc_array_c16 *ret, const gfc_array_c16 *vector,
  159. const gfc_array_l1 *mask, const gfc_array_c16 *field)
  160. {
  161. /* r.* indicates the return array. */
  162. index_type rstride[GFC_MAX_DIMENSIONS];
  163. index_type rstride0;
  164. index_type rs;
  165. GFC_COMPLEX_16 * restrict rptr;
  166. /* v.* indicates the vector array. */
  167. index_type vstride0;
  168. GFC_COMPLEX_16 *vptr;
  169. /* f.* indicates the field array. */
  170. index_type fstride[GFC_MAX_DIMENSIONS];
  171. index_type fstride0;
  172. const GFC_COMPLEX_16 *fptr;
  173. /* m.* indicates the mask array. */
  174. index_type mstride[GFC_MAX_DIMENSIONS];
  175. index_type mstride0;
  176. const GFC_LOGICAL_1 *mptr;
  177. index_type count[GFC_MAX_DIMENSIONS];
  178. index_type extent[GFC_MAX_DIMENSIONS];
  179. index_type n;
  180. index_type dim;
  181. int empty;
  182. int mask_kind;
  183. empty = 0;
  184. mptr = mask->base_addr;
  185. /* Use the same loop for all logical types, by using GFC_LOGICAL_1
  186. and using shifting to address size and endian issues. */
  187. mask_kind = GFC_DESCRIPTOR_SIZE (mask);
  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. {
  194. /* Do not convert a NULL pointer as we use test for NULL below. */
  195. if (mptr)
  196. mptr = GFOR_POINTER_TO_L1 (mptr, mask_kind);
  197. }
  198. else
  199. runtime_error ("Funny sized logical array");
  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_16));
  220. }
  221. else
  222. {
  223. dim = GFC_DESCRIPTOR_RANK (ret);
  224. /* Initialize to avoid -Wmaybe-uninitialized complaints. */
  225. rstride[0] = 1;
  226. for (n = 0; n < dim; n++)
  227. {
  228. count[n] = 0;
  229. extent[n] = GFC_DESCRIPTOR_EXTENT(ret,n);
  230. empty = empty || extent[n] <= 0;
  231. rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,n);
  232. fstride[n] = GFC_DESCRIPTOR_STRIDE(field,n);
  233. mstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(mask,n);
  234. }
  235. if (rstride[0] == 0)
  236. rstride[0] = 1;
  237. }
  238. if (empty)
  239. return;
  240. if (fstride[0] == 0)
  241. fstride[0] = 1;
  242. if (mstride[0] == 0)
  243. mstride[0] = 1;
  244. vstride0 = GFC_DESCRIPTOR_STRIDE(vector,0);
  245. if (vstride0 == 0)
  246. vstride0 = 1;
  247. rstride0 = rstride[0];
  248. fstride0 = fstride[0];
  249. mstride0 = mstride[0];
  250. rptr = ret->base_addr;
  251. fptr = field->base_addr;
  252. vptr = vector->base_addr;
  253. while (rptr)
  254. {
  255. if (*mptr)
  256. {
  257. /* From vector. */
  258. *rptr = *vptr;
  259. vptr += vstride0;
  260. }
  261. else
  262. {
  263. /* From field. */
  264. *rptr = *fptr;
  265. }
  266. /* Advance to the next element. */
  267. rptr += rstride0;
  268. fptr += fstride0;
  269. mptr += mstride0;
  270. count[0]++;
  271. n = 0;
  272. while (count[n] == extent[n])
  273. {
  274. /* When we get to the end of a dimension, reset it and increment
  275. the next dimension. */
  276. count[n] = 0;
  277. /* We could precalculate these products, but this is a less
  278. frequently used path so probably not worth it. */
  279. rptr -= rstride[n] * extent[n];
  280. fptr -= fstride[n] * extent[n];
  281. mptr -= mstride[n] * extent[n];
  282. n++;
  283. if (n >= dim)
  284. {
  285. /* Break out of the loop. */
  286. rptr = NULL;
  287. break;
  288. }
  289. else
  290. {
  291. count[n]++;
  292. rptr += rstride[n];
  293. fptr += fstride[n];
  294. mptr += mstride[n];
  295. }
  296. }
  297. }
  298. }
  299. #endif