eoshift1_4.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* 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. #if defined (HAVE_GFC_INTEGER_4)
  23. static void
  24. eoshift1 (gfc_array_char * const restrict ret,
  25. const gfc_array_char * const restrict array,
  26. const gfc_array_i4 * const restrict h,
  27. const char * const restrict pbound,
  28. const GFC_INTEGER_4 * const restrict pwhich,
  29. const char * filler, index_type filler_len)
  30. {
  31. /* r.* indicates the return array. */
  32. index_type rstride[GFC_MAX_DIMENSIONS];
  33. index_type rstride0;
  34. index_type roffset;
  35. char *rptr;
  36. char * restrict dest;
  37. /* s.* indicates the source array. */
  38. index_type sstride[GFC_MAX_DIMENSIONS];
  39. index_type sstride0;
  40. index_type soffset;
  41. const char *sptr;
  42. const char *src;
  43. /* h.* indicates the shift array. */
  44. index_type hstride[GFC_MAX_DIMENSIONS];
  45. index_type hstride0;
  46. const GFC_INTEGER_4 *hptr;
  47. index_type count[GFC_MAX_DIMENSIONS];
  48. index_type extent[GFC_MAX_DIMENSIONS];
  49. index_type dim;
  50. index_type len;
  51. index_type n;
  52. index_type size;
  53. index_type arraysize;
  54. int which;
  55. GFC_INTEGER_4 sh;
  56. GFC_INTEGER_4 delta;
  57. /* The compiler cannot figure out that these are set, initialize
  58. them to avoid warnings. */
  59. len = 0;
  60. soffset = 0;
  61. roffset = 0;
  62. size = GFC_DESCRIPTOR_SIZE(array);
  63. if (pwhich)
  64. which = *pwhich - 1;
  65. else
  66. which = 0;
  67. extent[0] = 1;
  68. count[0] = 0;
  69. arraysize = size0 ((array_t *) array);
  70. if (ret->base_addr == NULL)
  71. {
  72. ret->offset = 0;
  73. GFC_DTYPE_COPY(ret,array);
  74. for (index_type i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
  75. {
  76. index_type ub, str;
  77. ub = GFC_DESCRIPTOR_EXTENT(array,i) - 1;
  78. if (i == 0)
  79. str = 1;
  80. else
  81. str = GFC_DESCRIPTOR_EXTENT(ret,i-1)
  82. * GFC_DESCRIPTOR_STRIDE(ret,i-1);
  83. GFC_DIMENSION_SET(ret->dim[i], 0, ub, str);
  84. }
  85. /* xmallocarray allocates a single byte for zero size. */
  86. ret->base_addr = xmallocarray (arraysize, size);
  87. }
  88. else if (unlikely (compile_options.bounds_check))
  89. {
  90. bounds_equal_extents ((array_t *) ret, (array_t *) array,
  91. "return value", "EOSHIFT");
  92. }
  93. if (unlikely (compile_options.bounds_check))
  94. {
  95. bounds_reduced_extents ((array_t *) h, (array_t *) array, which,
  96. "SHIFT argument", "EOSHIFT");
  97. }
  98. if (arraysize == 0)
  99. return;
  100. n = 0;
  101. for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
  102. {
  103. if (dim == which)
  104. {
  105. roffset = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
  106. if (roffset == 0)
  107. roffset = size;
  108. soffset = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
  109. if (soffset == 0)
  110. soffset = size;
  111. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  112. }
  113. else
  114. {
  115. count[n] = 0;
  116. extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
  117. rstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
  118. sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
  119. hstride[n] = GFC_DESCRIPTOR_STRIDE(h,n);
  120. n++;
  121. }
  122. }
  123. if (sstride[0] == 0)
  124. sstride[0] = size;
  125. if (rstride[0] == 0)
  126. rstride[0] = size;
  127. if (hstride[0] == 0)
  128. hstride[0] = 1;
  129. dim = GFC_DESCRIPTOR_RANK (array);
  130. rstride0 = rstride[0];
  131. sstride0 = sstride[0];
  132. hstride0 = hstride[0];
  133. rptr = ret->base_addr;
  134. sptr = array->base_addr;
  135. hptr = h->base_addr;
  136. while (rptr)
  137. {
  138. /* Do the shift for this dimension. */
  139. sh = *hptr;
  140. if (( sh >= 0 ? sh : -sh ) > len)
  141. {
  142. delta = len;
  143. sh = len;
  144. }
  145. else
  146. delta = (sh >= 0) ? sh: -sh;
  147. if (sh > 0)
  148. {
  149. src = &sptr[delta * soffset];
  150. dest = rptr;
  151. }
  152. else
  153. {
  154. src = sptr;
  155. dest = &rptr[delta * roffset];
  156. }
  157. /* If the elements are contiguous, perform a single block move. */
  158. if (soffset == size && roffset == size)
  159. {
  160. size_t chunk = size * (len - delta);
  161. memcpy (dest, src, chunk);
  162. dest += chunk;
  163. }
  164. else
  165. {
  166. for (n = 0; n < len - delta; n++)
  167. {
  168. memcpy (dest, src, size);
  169. dest += roffset;
  170. src += soffset;
  171. }
  172. }
  173. if (sh < 0)
  174. dest = rptr;
  175. n = delta;
  176. if (pbound)
  177. while (n--)
  178. {
  179. memcpy (dest, pbound, size);
  180. dest += roffset;
  181. }
  182. else
  183. while (n--)
  184. {
  185. index_type i;
  186. if (filler_len == 1)
  187. memset (dest, filler[0], size);
  188. else
  189. for (i = 0; i < size; i += filler_len)
  190. memcpy (&dest[i], filler, filler_len);
  191. dest += roffset;
  192. }
  193. /* Advance to the next section. */
  194. rptr += rstride0;
  195. sptr += sstride0;
  196. hptr += hstride0;
  197. count[0]++;
  198. n = 0;
  199. while (count[n] == extent[n])
  200. {
  201. /* When we get to the end of a dimension, reset it and increment
  202. the next dimension. */
  203. count[n] = 0;
  204. /* We could precalculate these products, but this is a less
  205. frequently used path so probably not worth it. */
  206. rptr -= rstride[n] * extent[n];
  207. sptr -= sstride[n] * extent[n];
  208. hptr -= hstride[n] * extent[n];
  209. n++;
  210. if (n >= dim - 1)
  211. {
  212. /* Break out of the loop. */
  213. rptr = NULL;
  214. break;
  215. }
  216. else
  217. {
  218. count[n]++;
  219. rptr += rstride[n];
  220. sptr += sstride[n];
  221. hptr += hstride[n];
  222. }
  223. }
  224. }
  225. }
  226. void eoshift1_4 (gfc_array_char * const restrict,
  227. const gfc_array_char * const restrict,
  228. const gfc_array_i4 * const restrict, const char * const restrict,
  229. const GFC_INTEGER_4 * const restrict);
  230. export_proto(eoshift1_4);
  231. void
  232. eoshift1_4 (gfc_array_char * const restrict ret,
  233. const gfc_array_char * const restrict array,
  234. const gfc_array_i4 * const restrict h,
  235. const char * const restrict pbound,
  236. const GFC_INTEGER_4 * const restrict pwhich)
  237. {
  238. eoshift1 (ret, array, h, pbound, pwhich, "\0", 1);
  239. }
  240. void eoshift1_4_char (gfc_array_char * const restrict,
  241. GFC_INTEGER_4,
  242. const gfc_array_char * const restrict,
  243. const gfc_array_i4 * const restrict,
  244. const char * const restrict,
  245. const GFC_INTEGER_4 * const restrict,
  246. GFC_INTEGER_4, GFC_INTEGER_4);
  247. export_proto(eoshift1_4_char);
  248. void
  249. eoshift1_4_char (gfc_array_char * const restrict ret,
  250. GFC_INTEGER_4 ret_length __attribute__((unused)),
  251. const gfc_array_char * const restrict array,
  252. const gfc_array_i4 * const restrict h,
  253. const char * const restrict pbound,
  254. const GFC_INTEGER_4 * const restrict pwhich,
  255. GFC_INTEGER_4 array_length __attribute__((unused)),
  256. GFC_INTEGER_4 bound_length __attribute__((unused)))
  257. {
  258. eoshift1 (ret, array, h, pbound, pwhich, " ", 1);
  259. }
  260. void eoshift1_4_char4 (gfc_array_char * const restrict,
  261. GFC_INTEGER_4,
  262. const gfc_array_char * const restrict,
  263. const gfc_array_i4 * const restrict,
  264. const char * const restrict,
  265. const GFC_INTEGER_4 * const restrict,
  266. GFC_INTEGER_4, GFC_INTEGER_4);
  267. export_proto(eoshift1_4_char4);
  268. void
  269. eoshift1_4_char4 (gfc_array_char * const restrict ret,
  270. GFC_INTEGER_4 ret_length __attribute__((unused)),
  271. const gfc_array_char * const restrict array,
  272. const gfc_array_i4 * const restrict h,
  273. const char * const restrict pbound,
  274. const GFC_INTEGER_4 * const restrict pwhich,
  275. GFC_INTEGER_4 array_length __attribute__((unused)),
  276. GFC_INTEGER_4 bound_length __attribute__((unused)))
  277. {
  278. static const gfc_char4_t space = (unsigned char) ' ';
  279. eoshift1 (ret, array, h, pbound, pwhich,
  280. (const char *) &space, sizeof (gfc_char4_t));
  281. }
  282. #endif