eoshift3.m4 8.9 KB

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