spread_i1.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* Special implementation of the SPREAD intrinsic
  2. Copyright (C) 2008-2022 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 <string.h>
  23. #if defined (HAVE_GFC_INTEGER_1)
  24. void
  25. spread_i1 (gfc_array_i1 *ret, const gfc_array_i1 *source,
  26. const index_type along, const index_type pncopies)
  27. {
  28. /* r.* indicates the return array. */
  29. index_type rstride[GFC_MAX_DIMENSIONS];
  30. index_type rstride0;
  31. index_type rdelta = 0;
  32. index_type rrank;
  33. index_type rs;
  34. GFC_INTEGER_1 *rptr;
  35. GFC_INTEGER_1 * restrict dest;
  36. /* s.* indicates the source array. */
  37. index_type sstride[GFC_MAX_DIMENSIONS];
  38. index_type sstride0;
  39. index_type srank;
  40. const GFC_INTEGER_1 *sptr;
  41. index_type count[GFC_MAX_DIMENSIONS];
  42. index_type extent[GFC_MAX_DIMENSIONS];
  43. index_type n;
  44. index_type dim;
  45. index_type ncopies;
  46. srank = GFC_DESCRIPTOR_RANK(source);
  47. rrank = srank + 1;
  48. if (rrank > GFC_MAX_DIMENSIONS)
  49. runtime_error ("return rank too large in spread()");
  50. if (along > rrank)
  51. runtime_error ("dim outside of rank in spread()");
  52. ncopies = pncopies;
  53. if (ret->base_addr == NULL)
  54. {
  55. size_t ub, stride;
  56. /* The front end has signalled that we need to populate the
  57. return array descriptor. */
  58. ret->dtype.rank = rrank;
  59. dim = 0;
  60. rs = 1;
  61. for (n = 0; n < rrank; n++)
  62. {
  63. stride = rs;
  64. if (n == along - 1)
  65. {
  66. ub = ncopies - 1;
  67. rdelta = rs;
  68. rs *= ncopies;
  69. }
  70. else
  71. {
  72. count[dim] = 0;
  73. extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
  74. sstride[dim] = GFC_DESCRIPTOR_STRIDE(source,dim);
  75. rstride[dim] = rs;
  76. ub = extent[dim] - 1;
  77. rs *= extent[dim];
  78. dim++;
  79. }
  80. GFC_DIMENSION_SET(ret->dim[n], 0, ub, stride);
  81. }
  82. ret->offset = 0;
  83. /* xmallocarray allocates a single byte for zero size. */
  84. ret->base_addr = xmallocarray (rs, sizeof(GFC_INTEGER_1));
  85. if (rs <= 0)
  86. return;
  87. }
  88. else
  89. {
  90. int zero_sized;
  91. zero_sized = 0;
  92. dim = 0;
  93. if (GFC_DESCRIPTOR_RANK(ret) != rrank)
  94. runtime_error ("rank mismatch in spread()");
  95. if (unlikely (compile_options.bounds_check))
  96. {
  97. for (n = 0; n < rrank; n++)
  98. {
  99. index_type ret_extent;
  100. ret_extent = GFC_DESCRIPTOR_EXTENT(ret,n);
  101. if (n == along - 1)
  102. {
  103. rdelta = GFC_DESCRIPTOR_STRIDE(ret,n);
  104. if (ret_extent != ncopies)
  105. runtime_error("Incorrect extent in return value of SPREAD"
  106. " intrinsic in dimension %ld: is %ld,"
  107. " should be %ld", (long int) n+1,
  108. (long int) ret_extent, (long int) ncopies);
  109. }
  110. else
  111. {
  112. count[dim] = 0;
  113. extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
  114. if (ret_extent != extent[dim])
  115. runtime_error("Incorrect extent in return value of SPREAD"
  116. " intrinsic in dimension %ld: is %ld,"
  117. " should be %ld", (long int) n+1,
  118. (long int) ret_extent,
  119. (long int) extent[dim]);
  120. if (extent[dim] <= 0)
  121. zero_sized = 1;
  122. sstride[dim] = GFC_DESCRIPTOR_STRIDE(source,dim);
  123. rstride[dim] = GFC_DESCRIPTOR_STRIDE(ret,n);
  124. dim++;
  125. }
  126. }
  127. }
  128. else
  129. {
  130. for (n = 0; n < rrank; n++)
  131. {
  132. if (n == along - 1)
  133. {
  134. rdelta = GFC_DESCRIPTOR_STRIDE(ret,n);
  135. }
  136. else
  137. {
  138. count[dim] = 0;
  139. extent[dim] = GFC_DESCRIPTOR_EXTENT(source,dim);
  140. if (extent[dim] <= 0)
  141. zero_sized = 1;
  142. sstride[dim] = GFC_DESCRIPTOR_STRIDE(source,dim);
  143. rstride[dim] = GFC_DESCRIPTOR_STRIDE(ret,n);
  144. dim++;
  145. }
  146. }
  147. }
  148. if (zero_sized)
  149. return;
  150. if (sstride[0] == 0)
  151. sstride[0] = 1;
  152. }
  153. sstride0 = sstride[0];
  154. rstride0 = rstride[0];
  155. rptr = ret->base_addr;
  156. sptr = source->base_addr;
  157. while (sptr)
  158. {
  159. /* Spread this element. */
  160. dest = rptr;
  161. for (n = 0; n < ncopies; n++)
  162. {
  163. *dest = *sptr;
  164. dest += rdelta;
  165. }
  166. /* Advance to the next element. */
  167. sptr += sstride0;
  168. rptr += rstride0;
  169. count[0]++;
  170. n = 0;
  171. while (count[n] == extent[n])
  172. {
  173. /* When we get to the end of a dimension, reset it and increment
  174. the next dimension. */
  175. count[n] = 0;
  176. /* We could precalculate these products, but this is a less
  177. frequently used path so probably not worth it. */
  178. sptr -= sstride[n] * extent[n];
  179. rptr -= rstride[n] * extent[n];
  180. n++;
  181. if (n >= srank)
  182. {
  183. /* Break out of the loop. */
  184. sptr = NULL;
  185. break;
  186. }
  187. else
  188. {
  189. count[n]++;
  190. sptr += sstride[n];
  191. rptr += rstride[n];
  192. }
  193. }
  194. }
  195. }
  196. /* This version of spread_internal treats the special case of a scalar
  197. source. This is much simpler than the more general case above. */
  198. void
  199. spread_scalar_i1 (gfc_array_i1 *ret, const GFC_INTEGER_1 *source,
  200. const index_type along, const index_type ncopies)
  201. {
  202. GFC_INTEGER_1 * restrict dest;
  203. index_type stride;
  204. if (GFC_DESCRIPTOR_RANK (ret) != 1)
  205. runtime_error ("incorrect destination rank in spread()");
  206. if (along > 1)
  207. runtime_error ("dim outside of rank in spread()");
  208. if (ret->base_addr == NULL)
  209. {
  210. ret->base_addr = xmallocarray (ncopies, sizeof (GFC_INTEGER_1));
  211. ret->offset = 0;
  212. GFC_DIMENSION_SET(ret->dim[0], 0, ncopies - 1, 1);
  213. }
  214. else
  215. {
  216. if (ncopies - 1 > (GFC_DESCRIPTOR_EXTENT(ret,0) - 1)
  217. / GFC_DESCRIPTOR_STRIDE(ret,0))
  218. runtime_error ("dim too large in spread()");
  219. }
  220. dest = ret->base_addr;
  221. stride = GFC_DESCRIPTOR_STRIDE(ret,0);
  222. for (index_type n = 0; n < ncopies; n++)
  223. {
  224. *dest = *source;
  225. dest += stride;
  226. }
  227. }
  228. #endif