unpack.m4 8.7 KB

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