reshape_generic.c 10 KB

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