eoshift2.c 8.9 KB

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