reshape.m4 9.4 KB

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