eoshift0.c 8.3 KB

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