cshift1.m4 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. `/* Implementation of the CSHIFT intrinsic
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. Contributed by Feng Wang <wf_cs@yahoo.com>
  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. Ligbfortran 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. cshift1 (gfc_array_char * const restrict ret,
  28. const gfc_array_char * const restrict array,
  29. const 'atype` * const restrict h,
  30. const 'atype_name` * const restrict pwhich)
  31. {
  32. /* r.* indicates the return array. */
  33. index_type rstride[GFC_MAX_DIMENSIONS];
  34. index_type rstride0;
  35. index_type roffset;
  36. char *rptr;
  37. char *dest;
  38. /* s.* indicates the source array. */
  39. index_type sstride[GFC_MAX_DIMENSIONS];
  40. index_type sstride0;
  41. index_type soffset;
  42. const char *sptr;
  43. const char *src;
  44. /* h.* indicates the shift array. */
  45. index_type hstride[GFC_MAX_DIMENSIONS];
  46. index_type hstride0;
  47. const 'atype_name` *hptr;
  48. index_type count[GFC_MAX_DIMENSIONS];
  49. index_type extent[GFC_MAX_DIMENSIONS];
  50. index_type dim;
  51. index_type len;
  52. index_type n;
  53. int which;
  54. 'atype_name` sh;
  55. index_type arraysize;
  56. index_type size;
  57. if (pwhich)
  58. which = *pwhich - 1;
  59. else
  60. which = 0;
  61. if (which < 0 || (which + 1) > GFC_DESCRIPTOR_RANK (array))
  62. runtime_error ("Argument ''`DIM''` is out of range in call to ''`CSHIFT''`");
  63. size = GFC_DESCRIPTOR_SIZE(array);
  64. arraysize = size0 ((array_t *)array);
  65. if (ret->base_addr == NULL)
  66. {
  67. int i;
  68. ret->base_addr = xmallocarray (arraysize, size);
  69. ret->offset = 0;
  70. ret->dtype = array->dtype;
  71. for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
  72. {
  73. index_type ub, str;
  74. ub = GFC_DESCRIPTOR_EXTENT(array,i) - 1;
  75. if (i == 0)
  76. str = 1;
  77. else
  78. str = GFC_DESCRIPTOR_EXTENT(ret,i-1) *
  79. GFC_DESCRIPTOR_STRIDE(ret,i-1);
  80. GFC_DIMENSION_SET(ret->dim[i], 0, ub, str);
  81. }
  82. }
  83. else if (unlikely (compile_options.bounds_check))
  84. {
  85. bounds_equal_extents ((array_t *) ret, (array_t *) array,
  86. "return value", "CSHIFT");
  87. }
  88. if (unlikely (compile_options.bounds_check))
  89. {
  90. bounds_reduced_extents ((array_t *) h, (array_t *) array, which,
  91. "SHIFT argument", "CSHIFT");
  92. }
  93. if (arraysize == 0)
  94. return;
  95. extent[0] = 1;
  96. count[0] = 0;
  97. n = 0;
  98. /* Initialized for avoiding compiler warnings. */
  99. roffset = size;
  100. soffset = size;
  101. len = 0;
  102. for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
  103. {
  104. if (dim == which)
  105. {
  106. roffset = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
  107. if (roffset == 0)
  108. roffset = size;
  109. soffset = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
  110. if (soffset == 0)
  111. soffset = size;
  112. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  113. }
  114. else
  115. {
  116. count[n] = 0;
  117. extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
  118. rstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
  119. sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
  120. hstride[n] = GFC_DESCRIPTOR_STRIDE(h,n);
  121. n++;
  122. }
  123. }
  124. if (sstride[0] == 0)
  125. sstride[0] = size;
  126. if (rstride[0] == 0)
  127. rstride[0] = size;
  128. if (hstride[0] == 0)
  129. hstride[0] = 1;
  130. dim = GFC_DESCRIPTOR_RANK (array);
  131. rstride0 = rstride[0];
  132. sstride0 = sstride[0];
  133. hstride0 = hstride[0];
  134. rptr = ret->base_addr;
  135. sptr = array->base_addr;
  136. hptr = h->base_addr;
  137. while (rptr)
  138. {
  139. /* Do the shift for this dimension. */
  140. sh = *hptr;
  141. sh = (div (sh, len)).rem;
  142. if (sh < 0)
  143. sh += len;
  144. src = &sptr[sh * soffset];
  145. dest = rptr;
  146. for (n = 0; n < len; n++)
  147. {
  148. memcpy (dest, src, size);
  149. dest += roffset;
  150. if (n == len - sh - 1)
  151. src = sptr;
  152. else
  153. src += soffset;
  154. }
  155. /* Advance to the next section. */
  156. rptr += rstride0;
  157. sptr += sstride0;
  158. hptr += hstride0;
  159. count[0]++;
  160. n = 0;
  161. while (count[n] == extent[n])
  162. {
  163. /* When we get to the end of a dimension, reset it and increment
  164. the next dimension. */
  165. count[n] = 0;
  166. /* We could precalculate these products, but this is a less
  167. frequently used path so probably not worth it. */
  168. rptr -= rstride[n] * extent[n];
  169. sptr -= sstride[n] * extent[n];
  170. hptr -= hstride[n] * extent[n];
  171. n++;
  172. if (n >= dim - 1)
  173. {
  174. /* Break out of the loop. */
  175. rptr = NULL;
  176. break;
  177. }
  178. else
  179. {
  180. count[n]++;
  181. rptr += rstride[n];
  182. sptr += sstride[n];
  183. hptr += hstride[n];
  184. }
  185. }
  186. }
  187. }
  188. void cshift1_'atype_kind` (gfc_array_char * const restrict,
  189. const gfc_array_char * const restrict,
  190. const 'atype` * const restrict,
  191. const 'atype_name` * const restrict);
  192. export_proto(cshift1_'atype_kind`);
  193. void
  194. cshift1_'atype_kind` (gfc_array_char * const restrict ret,
  195. const gfc_array_char * const restrict array,
  196. const 'atype` * const restrict h,
  197. const 'atype_name` * const restrict pwhich)
  198. {
  199. cshift1 (ret, array, h, pwhich);
  200. }
  201. void cshift1_'atype_kind`_char (gfc_array_char * const restrict ret,
  202. GFC_INTEGER_4,
  203. const gfc_array_char * const restrict array,
  204. const 'atype` * const restrict h,
  205. const 'atype_name` * const restrict pwhich,
  206. GFC_INTEGER_4);
  207. export_proto(cshift1_'atype_kind`_char);
  208. void
  209. cshift1_'atype_kind`_char (gfc_array_char * const restrict ret,
  210. GFC_INTEGER_4 ret_length __attribute__((unused)),
  211. const gfc_array_char * const restrict array,
  212. const 'atype` * const restrict h,
  213. const 'atype_name` * const restrict pwhich,
  214. GFC_INTEGER_4 array_length __attribute__((unused)))
  215. {
  216. cshift1 (ret, array, h, pwhich);
  217. }
  218. void cshift1_'atype_kind`_char4 (gfc_array_char * const restrict ret,
  219. GFC_INTEGER_4,
  220. const gfc_array_char * const restrict array,
  221. const 'atype` * const restrict h,
  222. const 'atype_name` * const restrict pwhich,
  223. GFC_INTEGER_4);
  224. export_proto(cshift1_'atype_kind`_char4);
  225. void
  226. cshift1_'atype_kind`_char4 (gfc_array_char * const restrict ret,
  227. GFC_INTEGER_4 ret_length __attribute__((unused)),
  228. const gfc_array_char * const restrict array,
  229. const 'atype` * const restrict h,
  230. const 'atype_name` * const restrict pwhich,
  231. GFC_INTEGER_4 array_length __attribute__((unused)))
  232. {
  233. cshift1 (ret, array, h, pwhich);
  234. }
  235. #endif'