eoshift1_4.c 8.3 KB

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