cshift1_8.c 7.2 KB

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