reshape_r10.c 9.2 KB

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