reshape_i4.c 9.5 KB

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