eoshift1.m4 8.3 KB

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