spread_c8.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* Special implementation of the SPREAD intrinsic
  2. Copyright (C) 2008-2015 Free Software Foundation, Inc.
  3. Contributed by Thomas Koenig <tkoenig@gcc.gnu.org>, based on
  4. spread_generic.c written by Paul Brook <paul@nowt.org>
  5. This file is part of the GNU Fortran runtime library (libgfortran).
  6. Libgfortran is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public
  8. License as published by the Free Software Foundation; either
  9. version 3 of the License, or (at your option) any later version.
  10. Ligbfortran is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. #include "libgfortran.h"
  22. #include <stdlib.h>
  23. #include <assert.h>
  24. #include <string.h>
  25. #if defined (HAVE_GFC_COMPLEX_8)
  26. void
  27. spread_c8 (gfc_array_c8 *ret, const gfc_array_c8 *source,
  28. const index_type along, const index_type pncopies)
  29. {
  30. /* r.* indicates the return array. */
  31. index_type rstride[GFC_MAX_DIMENSIONS];
  32. index_type rstride0;
  33. index_type rdelta = 0;
  34. index_type rrank;
  35. index_type rs;
  36. GFC_COMPLEX_8 *rptr;
  37. GFC_COMPLEX_8 * restrict dest;
  38. /* s.* indicates the source array. */
  39. index_type sstride[GFC_MAX_DIMENSIONS];
  40. index_type sstride0;
  41. index_type srank;
  42. const GFC_COMPLEX_8 *sptr;
  43. index_type count[GFC_MAX_DIMENSIONS];
  44. index_type extent[GFC_MAX_DIMENSIONS];
  45. index_type n;
  46. index_type dim;
  47. index_type ncopies;
  48. srank = GFC_DESCRIPTOR_RANK(source);
  49. rrank = srank + 1;
  50. if (rrank > GFC_MAX_DIMENSIONS)
  51. runtime_error ("return rank too large in spread()");
  52. if (along > rrank)
  53. runtime_error ("dim outside of rank in spread()");
  54. ncopies = pncopies;
  55. if (ret->base_addr == NULL)
  56. {
  57. size_t ub, stride;
  58. /* The front end has signalled that we need to populate the
  59. return array descriptor. */
  60. ret->dtype = (source->dtype & ~GFC_DTYPE_RANK_MASK) | rrank;
  61. dim = 0;
  62. rs = 1;
  63. for (n = 0; n < rrank; n++)
  64. {
  65. stride = rs;
  66. if (n == along - 1)
  67. {
  68. ub = ncopies - 1;
  69. rdelta = rs;
  70. rs *= ncopies;
  71. }
  72. else
  73. {
  74. count[dim] = 0;
  75. extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
  76. sstride[dim] = GFC_DESCRIPTOR_STRIDE(source,dim);
  77. rstride[dim] = rs;
  78. ub = extent[dim] - 1;
  79. rs *= extent[dim];
  80. dim++;
  81. }
  82. GFC_DIMENSION_SET(ret->dim[n], 0, ub, stride);
  83. }
  84. ret->offset = 0;
  85. /* xmallocarray allocates a single byte for zero size. */
  86. ret->base_addr = xmallocarray (rs, sizeof(GFC_COMPLEX_8));
  87. if (rs <= 0)
  88. return;
  89. }
  90. else
  91. {
  92. int zero_sized;
  93. zero_sized = 0;
  94. dim = 0;
  95. if (GFC_DESCRIPTOR_RANK(ret) != rrank)
  96. runtime_error ("rank mismatch in spread()");
  97. if (unlikely (compile_options.bounds_check))
  98. {
  99. for (n = 0; n < rrank; n++)
  100. {
  101. index_type ret_extent;
  102. ret_extent = GFC_DESCRIPTOR_EXTENT(ret,n);
  103. if (n == along - 1)
  104. {
  105. rdelta = GFC_DESCRIPTOR_STRIDE(ret,n);
  106. if (ret_extent != ncopies)
  107. runtime_error("Incorrect extent in return value of SPREAD"
  108. " intrinsic in dimension %ld: is %ld,"
  109. " should be %ld", (long int) n+1,
  110. (long int) ret_extent, (long int) ncopies);
  111. }
  112. else
  113. {
  114. count[dim] = 0;
  115. extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
  116. if (ret_extent != extent[dim])
  117. runtime_error("Incorrect extent in return value of SPREAD"
  118. " intrinsic in dimension %ld: is %ld,"
  119. " should be %ld", (long int) n+1,
  120. (long int) ret_extent,
  121. (long int) extent[dim]);
  122. if (extent[dim] <= 0)
  123. zero_sized = 1;
  124. sstride[dim] = GFC_DESCRIPTOR_STRIDE(source,dim);
  125. rstride[dim] = GFC_DESCRIPTOR_STRIDE(ret,n);
  126. dim++;
  127. }
  128. }
  129. }
  130. else
  131. {
  132. for (n = 0; n < rrank; n++)
  133. {
  134. if (n == along - 1)
  135. {
  136. rdelta = GFC_DESCRIPTOR_STRIDE(ret,n);
  137. }
  138. else
  139. {
  140. count[dim] = 0;
  141. extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
  142. if (extent[dim] <= 0)
  143. zero_sized = 1;
  144. sstride[dim] = GFC_DESCRIPTOR_STRIDE(source,dim);
  145. rstride[dim] = GFC_DESCRIPTOR_STRIDE(ret,n);
  146. dim++;
  147. }
  148. }
  149. }
  150. if (zero_sized)
  151. return;
  152. if (sstride[0] == 0)
  153. sstride[0] = 1;
  154. }
  155. sstride0 = sstride[0];
  156. rstride0 = rstride[0];
  157. rptr = ret->base_addr;
  158. sptr = source->base_addr;
  159. while (sptr)
  160. {
  161. /* Spread this element. */
  162. dest = rptr;
  163. for (n = 0; n < ncopies; n++)
  164. {
  165. *dest = *sptr;
  166. dest += rdelta;
  167. }
  168. /* Advance to the next element. */
  169. sptr += sstride0;
  170. rptr += rstride0;
  171. count[0]++;
  172. n = 0;
  173. while (count[n] == extent[n])
  174. {
  175. /* When we get to the end of a dimension, reset it and increment
  176. the next dimension. */
  177. count[n] = 0;
  178. /* We could precalculate these products, but this is a less
  179. frequently used path so probably not worth it. */
  180. sptr -= sstride[n] * extent[n];
  181. rptr -= rstride[n] * extent[n];
  182. n++;
  183. if (n >= srank)
  184. {
  185. /* Break out of the loop. */
  186. sptr = NULL;
  187. break;
  188. }
  189. else
  190. {
  191. count[n]++;
  192. sptr += sstride[n];
  193. rptr += rstride[n];
  194. }
  195. }
  196. }
  197. }
  198. /* This version of spread_internal treats the special case of a scalar
  199. source. This is much simpler than the more general case above. */
  200. void
  201. spread_scalar_c8 (gfc_array_c8 *ret, const GFC_COMPLEX_8 *source,
  202. const index_type along, const index_type pncopies)
  203. {
  204. int n;
  205. int ncopies = pncopies;
  206. GFC_COMPLEX_8 * restrict dest;
  207. index_type stride;
  208. if (GFC_DESCRIPTOR_RANK (ret) != 1)
  209. runtime_error ("incorrect destination rank in spread()");
  210. if (along > 1)
  211. runtime_error ("dim outside of rank in spread()");
  212. if (ret->base_addr == NULL)
  213. {
  214. ret->base_addr = xmallocarray (ncopies, sizeof (GFC_COMPLEX_8));
  215. ret->offset = 0;
  216. GFC_DIMENSION_SET(ret->dim[0], 0, ncopies - 1, 1);
  217. }
  218. else
  219. {
  220. if (ncopies - 1 > (GFC_DESCRIPTOR_EXTENT(ret,0) - 1)
  221. / GFC_DESCRIPTOR_STRIDE(ret,0))
  222. runtime_error ("dim too large in spread()");
  223. }
  224. dest = ret->base_addr;
  225. stride = GFC_DESCRIPTOR_STRIDE(ret,0);
  226. for (n = 0; n < ncopies; n++)
  227. {
  228. *dest = *source;
  229. dest += stride;
  230. }
  231. }
  232. #endif