eoshift0.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* Generic implementation of the EOSHIFT 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. #include <string.h>
  22. static void
  23. eoshift0 (gfc_array_char * ret, const gfc_array_char * array,
  24. index_type shift, const char * pbound, int which, index_type size,
  25. const char *filler, index_type filler_len)
  26. {
  27. /* r.* indicates the return array. */
  28. index_type rstride[GFC_MAX_DIMENSIONS];
  29. index_type rstride0;
  30. index_type roffset;
  31. char * restrict rptr;
  32. char *dest;
  33. /* s.* indicates the source array. */
  34. index_type sstride[GFC_MAX_DIMENSIONS];
  35. index_type sstride0;
  36. index_type soffset;
  37. const char *sptr;
  38. const char *src;
  39. index_type count[GFC_MAX_DIMENSIONS];
  40. index_type extent[GFC_MAX_DIMENSIONS];
  41. index_type dim;
  42. index_type len;
  43. index_type n;
  44. index_type arraysize;
  45. bool do_blocked;
  46. /* The compiler cannot figure out that these are set, initialize
  47. them to avoid warnings. */
  48. len = 0;
  49. soffset = 0;
  50. roffset = 0;
  51. arraysize = size0 ((array_t *) array);
  52. if (ret->base_addr == NULL)
  53. {
  54. int i;
  55. ret->offset = 0;
  56. GFC_DTYPE_COPY(ret,array);
  57. for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
  58. {
  59. index_type ub, str;
  60. ub = GFC_DESCRIPTOR_EXTENT(array,i) - 1;
  61. if (i == 0)
  62. str = 1;
  63. else
  64. str = GFC_DESCRIPTOR_EXTENT(ret,i-1)
  65. * GFC_DESCRIPTOR_STRIDE(ret,i-1);
  66. GFC_DIMENSION_SET(ret->dim[i], 0, ub, str);
  67. }
  68. /* xmallocarray allocates a single byte for zero size. */
  69. ret->base_addr = xmallocarray (arraysize, size);
  70. }
  71. else if (unlikely (compile_options.bounds_check))
  72. {
  73. bounds_equal_extents ((array_t *) ret, (array_t *) array,
  74. "return value", "EOSHIFT");
  75. }
  76. if (arraysize == 0)
  77. return;
  78. which = which - 1;
  79. extent[0] = 1;
  80. count[0] = 0;
  81. sstride[0] = -1;
  82. rstride[0] = -1;
  83. if (which > 0)
  84. {
  85. /* Test if both ret and array are contiguous. */
  86. index_type r_ex, a_ex;
  87. r_ex = 1;
  88. a_ex = 1;
  89. do_blocked = true;
  90. dim = GFC_DESCRIPTOR_RANK (array);
  91. for (n = 0; n < dim; n ++)
  92. {
  93. index_type rs, as;
  94. rs = GFC_DESCRIPTOR_STRIDE (ret, n);
  95. if (rs != r_ex)
  96. {
  97. do_blocked = false;
  98. break;
  99. }
  100. as = GFC_DESCRIPTOR_STRIDE (array, n);
  101. if (as != a_ex)
  102. {
  103. do_blocked = false;
  104. break;
  105. }
  106. r_ex *= GFC_DESCRIPTOR_EXTENT (ret, n);
  107. a_ex *= GFC_DESCRIPTOR_EXTENT (array, n);
  108. }
  109. }
  110. else
  111. do_blocked = false;
  112. n = 0;
  113. if (do_blocked)
  114. {
  115. /* For contiguous arrays, use the relationship that
  116. dimension(n1,n2,n3) :: a, b
  117. b = eoshift(a,sh,3)
  118. can be dealt with as if
  119. dimension(n1*n2*n3) :: an, bn
  120. bn = eoshift(a,sh*n1*n2,1)
  121. so a block move can be used for dim>1. */
  122. len = GFC_DESCRIPTOR_STRIDE(array, which)
  123. * GFC_DESCRIPTOR_EXTENT(array, which);
  124. shift *= GFC_DESCRIPTOR_STRIDE(array, which);
  125. roffset = size;
  126. soffset = size;
  127. for (dim = which + 1; dim < GFC_DESCRIPTOR_RANK (array); dim++)
  128. {
  129. count[n] = 0;
  130. extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
  131. rstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
  132. sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
  133. n++;
  134. }
  135. count[n] = 0;
  136. dim = GFC_DESCRIPTOR_RANK (array) - which;
  137. }
  138. else
  139. {
  140. for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
  141. {
  142. if (dim == which)
  143. {
  144. roffset = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
  145. if (roffset == 0)
  146. roffset = size;
  147. soffset = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
  148. if (soffset == 0)
  149. soffset = size;
  150. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  151. }
  152. else
  153. {
  154. count[n] = 0;
  155. extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
  156. rstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
  157. sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
  158. n++;
  159. }
  160. }
  161. dim = GFC_DESCRIPTOR_RANK (array);
  162. }
  163. if ((shift >= 0 ? shift : -shift) > len)
  164. {
  165. shift = len;
  166. len = 0;
  167. }
  168. else
  169. {
  170. if (shift > 0)
  171. len = len - shift;
  172. else
  173. len = len + shift;
  174. }
  175. rstride0 = rstride[0];
  176. sstride0 = sstride[0];
  177. rptr = ret->base_addr;
  178. sptr = array->base_addr;
  179. while (rptr)
  180. {
  181. /* Do the shift for this dimension. */
  182. if (shift > 0)
  183. {
  184. src = &sptr[shift * soffset];
  185. dest = rptr;
  186. }
  187. else
  188. {
  189. src = sptr;
  190. dest = &rptr[-shift * roffset];
  191. }
  192. /* If the elements are contiguous, perform a single block move. */
  193. if (soffset == size && roffset == size)
  194. {
  195. size_t chunk = size * len;
  196. memcpy (dest, src, chunk);
  197. dest += chunk;
  198. }
  199. else
  200. {
  201. for (n = 0; n < len; n++)
  202. {
  203. memcpy (dest, src, size);
  204. dest += roffset;
  205. src += soffset;
  206. }
  207. }
  208. if (shift >= 0)
  209. {
  210. n = shift;
  211. }
  212. else
  213. {
  214. dest = rptr;
  215. n = -shift;
  216. }
  217. if (pbound)
  218. while (n--)
  219. {
  220. memcpy (dest, pbound, size);
  221. dest += roffset;
  222. }
  223. else
  224. while (n--)
  225. {
  226. index_type i;
  227. if (filler_len == 1)
  228. memset (dest, filler[0], size);
  229. else
  230. for (i = 0; i < size ; i += filler_len)
  231. memcpy (&dest[i], filler, filler_len);
  232. dest += roffset;
  233. }
  234. /* Advance to the next section. */
  235. rptr += rstride0;
  236. sptr += sstride0;
  237. count[0]++;
  238. n = 0;
  239. while (count[n] == extent[n])
  240. {
  241. /* When we get to the end of a dimension, reset it and increment
  242. the next dimension. */
  243. count[n] = 0;
  244. /* We could precalculate these products, but this is a less
  245. frequently used path so probably not worth it. */
  246. rptr -= rstride[n] * extent[n];
  247. sptr -= sstride[n] * extent[n];
  248. n++;
  249. if (n >= dim - 1)
  250. {
  251. /* Break out of the loop. */
  252. rptr = NULL;
  253. break;
  254. }
  255. else
  256. {
  257. count[n]++;
  258. rptr += rstride[n];
  259. sptr += sstride[n];
  260. }
  261. }
  262. }
  263. }
  264. #define DEFINE_EOSHIFT(N) \
  265. extern void eoshift0_##N (gfc_array_char *, const gfc_array_char *, \
  266. const GFC_INTEGER_##N *, const char *, \
  267. const GFC_INTEGER_##N *); \
  268. export_proto(eoshift0_##N); \
  269. \
  270. void \
  271. eoshift0_##N (gfc_array_char *ret, const gfc_array_char *array, \
  272. const GFC_INTEGER_##N *pshift, const char *pbound, \
  273. const GFC_INTEGER_##N *pdim) \
  274. { \
  275. eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
  276. GFC_DESCRIPTOR_SIZE (array), "\0", 1); \
  277. } \
  278. \
  279. extern void eoshift0_##N##_char (gfc_array_char *, GFC_INTEGER_4, \
  280. const gfc_array_char *, \
  281. const GFC_INTEGER_##N *, const char *, \
  282. const GFC_INTEGER_##N *, GFC_INTEGER_4, \
  283. GFC_INTEGER_4); \
  284. export_proto(eoshift0_##N##_char); \
  285. \
  286. void \
  287. eoshift0_##N##_char (gfc_array_char *ret, \
  288. GFC_INTEGER_4 ret_length __attribute__((unused)), \
  289. const gfc_array_char *array, \
  290. const GFC_INTEGER_##N *pshift, \
  291. const char *pbound, \
  292. const GFC_INTEGER_##N *pdim, \
  293. GFC_INTEGER_4 array_length, \
  294. GFC_INTEGER_4 bound_length __attribute__((unused))) \
  295. { \
  296. eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
  297. array_length, " ", 1); \
  298. } \
  299. \
  300. extern void eoshift0_##N##_char4 (gfc_array_char *, GFC_INTEGER_4, \
  301. const gfc_array_char *, \
  302. const GFC_INTEGER_##N *, const char *, \
  303. const GFC_INTEGER_##N *, GFC_INTEGER_4, \
  304. GFC_INTEGER_4); \
  305. export_proto(eoshift0_##N##_char4); \
  306. \
  307. void \
  308. eoshift0_##N##_char4 (gfc_array_char *ret, \
  309. GFC_INTEGER_4 ret_length __attribute__((unused)), \
  310. const gfc_array_char *array, \
  311. const GFC_INTEGER_##N *pshift, \
  312. const char *pbound, \
  313. const GFC_INTEGER_##N *pdim, \
  314. GFC_INTEGER_4 array_length, \
  315. GFC_INTEGER_4 bound_length __attribute__((unused))) \
  316. { \
  317. static const gfc_char4_t space = (unsigned char) ' '; \
  318. eoshift0 (ret, array, *pshift, pbound, pdim ? *pdim : 1, \
  319. array_length * sizeof (gfc_char4_t), (const char *) &space, \
  320. sizeof (gfc_char4_t)); \
  321. }
  322. DEFINE_EOSHIFT (1);
  323. DEFINE_EOSHIFT (2);
  324. DEFINE_EOSHIFT (4);
  325. DEFINE_EOSHIFT (8);
  326. #ifdef HAVE_GFC_INTEGER_16
  327. DEFINE_EOSHIFT (16);
  328. #endif