eoshift3_4.c 8.9 KB

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