reshape_generic.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. /* Generic implementation of the RESHAPE intrinsic
  2. Copyright (C) 2002-2015 Free Software Foundation, Inc.
  3. Contributed by Paul Brook <paul@nowt.org>
  4. This file is part of the GNU Fortran 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. Ligbfortran 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 <stdlib.h>
  22. #include <string.h>
  23. #include <assert.h>
  24. typedef GFC_ARRAY_DESCRIPTOR(1, index_type) shape_type;
  25. typedef GFC_ARRAY_DESCRIPTOR(GFC_MAX_DIMENSIONS, char) parray;
  26. static void
  27. reshape_internal (parray *ret, parray *source, shape_type *shape,
  28. parray *pad, shape_type *order, index_type size)
  29. {
  30. /* r.* indicates the return array. */
  31. index_type rcount[GFC_MAX_DIMENSIONS];
  32. index_type rextent[GFC_MAX_DIMENSIONS];
  33. index_type rstride[GFC_MAX_DIMENSIONS];
  34. index_type rstride0;
  35. index_type rdim;
  36. index_type rsize;
  37. index_type rs;
  38. index_type rex;
  39. char * restrict rptr;
  40. /* s.* indicates the source array. */
  41. index_type scount[GFC_MAX_DIMENSIONS];
  42. index_type sextent[GFC_MAX_DIMENSIONS];
  43. index_type sstride[GFC_MAX_DIMENSIONS];
  44. index_type sstride0;
  45. index_type sdim;
  46. index_type ssize;
  47. const char *sptr;
  48. /* p.* indicates the pad array. */
  49. index_type pcount[GFC_MAX_DIMENSIONS];
  50. index_type pextent[GFC_MAX_DIMENSIONS];
  51. index_type pstride[GFC_MAX_DIMENSIONS];
  52. index_type pdim;
  53. index_type psize;
  54. const char *pptr;
  55. const char *src;
  56. int n;
  57. int dim;
  58. int sempty, pempty, shape_empty;
  59. index_type shape_data[GFC_MAX_DIMENSIONS];
  60. rdim = GFC_DESCRIPTOR_EXTENT(shape,0);
  61. if (rdim != GFC_DESCRIPTOR_RANK(ret))
  62. runtime_error("rank of return array incorrect in RESHAPE intrinsic");
  63. shape_empty = 0;
  64. for (n = 0; n < rdim; n++)
  65. {
  66. shape_data[n] = shape->base_addr[n * GFC_DESCRIPTOR_STRIDE(shape,0)];
  67. if (shape_data[n] <= 0)
  68. {
  69. shape_data[n] = 0;
  70. shape_empty = 1;
  71. }
  72. }
  73. if (ret->base_addr == NULL)
  74. {
  75. index_type alloc_size;
  76. rs = 1;
  77. for (n = 0; n < rdim; n++)
  78. {
  79. rex = shape_data[n];
  80. GFC_DIMENSION_SET(ret->dim[n],0,rex - 1,rs);
  81. rs *= rex;
  82. }
  83. ret->offset = 0;
  84. if (unlikely (rs < 1))
  85. alloc_size = 0; /* xmalloc will allocate 1 byte. */
  86. else
  87. alloc_size = rs;
  88. ret->base_addr = xmallocarray (alloc_size, size);
  89. ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rdim;
  90. }
  91. if (shape_empty)
  92. return;
  93. if (pad)
  94. {
  95. pdim = GFC_DESCRIPTOR_RANK (pad);
  96. psize = 1;
  97. pempty = 0;
  98. for (n = 0; n < pdim; n++)
  99. {
  100. pcount[n] = 0;
  101. pstride[n] = GFC_DESCRIPTOR_STRIDE(pad,n);
  102. pextent[n] = GFC_DESCRIPTOR_EXTENT(pad,n);
  103. if (pextent[n] <= 0)
  104. {
  105. pempty = 1;
  106. pextent[n] = 0;
  107. }
  108. if (psize == pstride[n])
  109. psize *= pextent[n];
  110. else
  111. psize = 0;
  112. }
  113. pptr = pad->base_addr;
  114. }
  115. else
  116. {
  117. pdim = 0;
  118. psize = 1;
  119. pempty = 1;
  120. pptr = NULL;
  121. }
  122. if (unlikely (compile_options.bounds_check))
  123. {
  124. index_type ret_extent, source_extent;
  125. rs = 1;
  126. for (n = 0; n < rdim; n++)
  127. {
  128. rs *= shape_data[n];
  129. ret_extent = GFC_DESCRIPTOR_EXTENT(ret,n);
  130. if (ret_extent != shape_data[n])
  131. runtime_error("Incorrect extent in return value of RESHAPE"
  132. " intrinsic in dimension %ld: is %ld,"
  133. " should be %ld", (long int) n+1,
  134. (long int) ret_extent, (long int) shape_data[n]);
  135. }
  136. source_extent = 1;
  137. sdim = GFC_DESCRIPTOR_RANK (source);
  138. for (n = 0; n < sdim; n++)
  139. {
  140. index_type se;
  141. se = GFC_DESCRIPTOR_EXTENT(source,n);
  142. source_extent *= se > 0 ? se : 0;
  143. }
  144. if (rs > source_extent && (!pad || pempty))
  145. runtime_error("Incorrect size in SOURCE argument to RESHAPE"
  146. " intrinsic: is %ld, should be %ld",
  147. (long int) source_extent, (long int) rs);
  148. if (order)
  149. {
  150. int seen[GFC_MAX_DIMENSIONS];
  151. index_type v;
  152. for (n = 0; n < rdim; n++)
  153. seen[n] = 0;
  154. for (n = 0; n < rdim; n++)
  155. {
  156. v = order->base_addr[n * GFC_DESCRIPTOR_STRIDE(order,0)] - 1;
  157. if (v < 0 || v >= rdim)
  158. runtime_error("Value %ld out of range in ORDER argument"
  159. " to RESHAPE intrinsic", (long int) v + 1);
  160. if (seen[v] != 0)
  161. runtime_error("Duplicate value %ld in ORDER argument to"
  162. " RESHAPE intrinsic", (long int) v + 1);
  163. seen[v] = 1;
  164. }
  165. }
  166. }
  167. rsize = 1;
  168. for (n = 0; n < rdim; n++)
  169. {
  170. if (order)
  171. dim = order->base_addr[n * GFC_DESCRIPTOR_STRIDE(order,0)] - 1;
  172. else
  173. dim = n;
  174. rcount[n] = 0;
  175. rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,dim);
  176. rextent[n] = GFC_DESCRIPTOR_EXTENT(ret,dim);
  177. if (rextent[n] != shape_data[dim])
  178. runtime_error ("shape and target do not conform");
  179. if (rsize == rstride[n])
  180. rsize *= rextent[n];
  181. else
  182. rsize = 0;
  183. if (rextent[n] <= 0)
  184. return;
  185. }
  186. sdim = GFC_DESCRIPTOR_RANK (source);
  187. ssize = 1;
  188. sempty = 0;
  189. for (n = 0; n < sdim; n++)
  190. {
  191. scount[n] = 0;
  192. sstride[n] = GFC_DESCRIPTOR_STRIDE(source,n);
  193. sextent[n] = GFC_DESCRIPTOR_EXTENT(source,n);
  194. if (sextent[n] <= 0)
  195. {
  196. sempty = 1;
  197. sextent[n] = 0;
  198. }
  199. if (ssize == sstride[n])
  200. ssize *= sextent[n];
  201. else
  202. ssize = 0;
  203. }
  204. if (rsize != 0 && ssize != 0 && psize != 0)
  205. {
  206. rsize *= size;
  207. ssize *= size;
  208. psize *= size;
  209. reshape_packed (ret->base_addr, rsize, source->base_addr, ssize,
  210. pad ? pad->base_addr : NULL, psize);
  211. return;
  212. }
  213. rptr = ret->base_addr;
  214. src = sptr = source->base_addr;
  215. rstride0 = rstride[0] * size;
  216. sstride0 = sstride[0] * size;
  217. if (sempty && pempty)
  218. abort ();
  219. if (sempty)
  220. {
  221. /* Pretend we are using the pad array the first time around, too. */
  222. src = pptr;
  223. sptr = pptr;
  224. sdim = pdim;
  225. for (dim = 0; dim < pdim; dim++)
  226. {
  227. scount[dim] = pcount[dim];
  228. sextent[dim] = pextent[dim];
  229. sstride[dim] = pstride[dim];
  230. sstride0 = pstride[0] * size;
  231. }
  232. }
  233. while (rptr)
  234. {
  235. /* Select between the source and pad arrays. */
  236. memcpy(rptr, src, size);
  237. /* Advance to the next element. */
  238. rptr += rstride0;
  239. src += sstride0;
  240. rcount[0]++;
  241. scount[0]++;
  242. /* Advance to the next destination element. */
  243. n = 0;
  244. while (rcount[n] == rextent[n])
  245. {
  246. /* When we get to the end of a dimension, reset it and increment
  247. the next dimension. */
  248. rcount[n] = 0;
  249. /* We could precalculate these products, but this is a less
  250. frequently used path so probably not worth it. */
  251. rptr -= rstride[n] * rextent[n] * size;
  252. n++;
  253. if (n == rdim)
  254. {
  255. /* Break out of the loop. */
  256. rptr = NULL;
  257. break;
  258. }
  259. else
  260. {
  261. rcount[n]++;
  262. rptr += rstride[n] * size;
  263. }
  264. }
  265. /* Advance to the next source element. */
  266. n = 0;
  267. while (scount[n] == sextent[n])
  268. {
  269. /* When we get to the end of a dimension, reset it and increment
  270. the next dimension. */
  271. scount[n] = 0;
  272. /* We could precalculate these products, but this is a less
  273. frequently used path so probably not worth it. */
  274. src -= sstride[n] * sextent[n] * size;
  275. n++;
  276. if (n == sdim)
  277. {
  278. if (sptr && pad)
  279. {
  280. /* Switch to the pad array. */
  281. sptr = NULL;
  282. sdim = pdim;
  283. for (dim = 0; dim < pdim; dim++)
  284. {
  285. scount[dim] = pcount[dim];
  286. sextent[dim] = pextent[dim];
  287. sstride[dim] = pstride[dim];
  288. sstride0 = sstride[0] * size;
  289. }
  290. }
  291. /* We now start again from the beginning of the pad array. */
  292. src = pptr;
  293. break;
  294. }
  295. else
  296. {
  297. scount[n]++;
  298. src += sstride[n] * size;
  299. }
  300. }
  301. }
  302. }
  303. extern void reshape (parray *, parray *, shape_type *, parray *, shape_type *);
  304. export_proto(reshape);
  305. void
  306. reshape (parray *ret, parray *source, shape_type *shape, parray *pad,
  307. shape_type *order)
  308. {
  309. reshape_internal (ret, source, shape, pad, order,
  310. GFC_DESCRIPTOR_SIZE (source));
  311. }
  312. extern void reshape_char (parray *, gfc_charlen_type, parray *, shape_type *,
  313. parray *, shape_type *, gfc_charlen_type,
  314. gfc_charlen_type);
  315. export_proto(reshape_char);
  316. void
  317. reshape_char (parray *ret, gfc_charlen_type ret_length __attribute__((unused)),
  318. parray *source, shape_type *shape, parray *pad,
  319. shape_type *order, gfc_charlen_type source_length,
  320. gfc_charlen_type pad_length __attribute__((unused)))
  321. {
  322. reshape_internal (ret, source, shape, pad, order, source_length);
  323. }
  324. extern void reshape_char4 (parray *, gfc_charlen_type, parray *, shape_type *,
  325. parray *, shape_type *, gfc_charlen_type,
  326. gfc_charlen_type);
  327. export_proto(reshape_char4);
  328. void
  329. reshape_char4 (parray *ret, gfc_charlen_type ret_length __attribute__((unused)),
  330. parray *source, shape_type *shape, parray *pad,
  331. shape_type *order, gfc_charlen_type source_length,
  332. gfc_charlen_type pad_length __attribute__((unused)))
  333. {
  334. reshape_internal (ret, source, shape, pad, order,
  335. source_length * sizeof (gfc_char4_t));
  336. }