cshift1_16.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /* Implementation of the CSHIFT intrinsic
  2. Copyright (C) 2003-2022 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 <string.h>
  22. #if defined (HAVE_GFC_INTEGER_16)
  23. static void
  24. cshift1 (gfc_array_char * const restrict ret,
  25. const gfc_array_char * const restrict array,
  26. const gfc_array_i16 * const restrict h,
  27. const GFC_INTEGER_16 * const restrict pwhich)
  28. {
  29. /* r.* indicates the return array. */
  30. index_type rstride[GFC_MAX_DIMENSIONS];
  31. index_type rstride0;
  32. index_type roffset;
  33. char *rptr;
  34. char *dest;
  35. /* s.* indicates the source array. */
  36. index_type sstride[GFC_MAX_DIMENSIONS];
  37. index_type sstride0;
  38. index_type soffset;
  39. const char *sptr;
  40. const char *src;
  41. /* h.* indicates the shift array. */
  42. index_type hstride[GFC_MAX_DIMENSIONS];
  43. index_type hstride0;
  44. const GFC_INTEGER_16 *hptr;
  45. index_type count[GFC_MAX_DIMENSIONS];
  46. index_type extent[GFC_MAX_DIMENSIONS];
  47. index_type dim;
  48. index_type len;
  49. index_type n;
  50. int which;
  51. GFC_INTEGER_16 sh;
  52. index_type arraysize;
  53. index_type size;
  54. index_type type_size;
  55. if (pwhich)
  56. which = *pwhich - 1;
  57. else
  58. which = 0;
  59. if (which < 0 || (which + 1) > GFC_DESCRIPTOR_RANK (array))
  60. runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
  61. size = GFC_DESCRIPTOR_SIZE(array);
  62. arraysize = size0 ((array_t *)array);
  63. if (ret->base_addr == NULL)
  64. {
  65. ret->base_addr = xmallocarray (arraysize, size);
  66. ret->offset = 0;
  67. GFC_DTYPE_COPY(ret,array);
  68. for (index_type i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
  69. {
  70. index_type ub, str;
  71. ub = GFC_DESCRIPTOR_EXTENT(array,i) - 1;
  72. if (i == 0)
  73. str = 1;
  74. else
  75. str = GFC_DESCRIPTOR_EXTENT(ret,i-1) *
  76. GFC_DESCRIPTOR_STRIDE(ret,i-1);
  77. GFC_DIMENSION_SET(ret->dim[i], 0, ub, str);
  78. }
  79. }
  80. else if (unlikely (compile_options.bounds_check))
  81. {
  82. bounds_equal_extents ((array_t *) ret, (array_t *) array,
  83. "return value", "CSHIFT");
  84. }
  85. if (unlikely (compile_options.bounds_check))
  86. {
  87. bounds_reduced_extents ((array_t *) h, (array_t *) array, which,
  88. "SHIFT argument", "CSHIFT");
  89. }
  90. if (arraysize == 0)
  91. return;
  92. /* See if we should dispatch to a helper function. */
  93. type_size = GFC_DTYPE_TYPE_SIZE (array);
  94. switch (type_size)
  95. {
  96. case GFC_DTYPE_LOGICAL_1:
  97. case GFC_DTYPE_INTEGER_1:
  98. cshift1_16_i1 ((gfc_array_i1 *)ret, (gfc_array_i1 *) array,
  99. h, pwhich);
  100. return;
  101. case GFC_DTYPE_LOGICAL_2:
  102. case GFC_DTYPE_INTEGER_2:
  103. cshift1_16_i2 ((gfc_array_i2 *)ret, (gfc_array_i2 *) array,
  104. h, pwhich);
  105. return;
  106. case GFC_DTYPE_LOGICAL_4:
  107. case GFC_DTYPE_INTEGER_4:
  108. cshift1_16_i4 ((gfc_array_i4 *)ret, (gfc_array_i4 *) array,
  109. h, pwhich);
  110. return;
  111. case GFC_DTYPE_LOGICAL_8:
  112. case GFC_DTYPE_INTEGER_8:
  113. cshift1_16_i8 ((gfc_array_i8 *)ret, (gfc_array_i8 *) array,
  114. h, pwhich);
  115. return;
  116. #if defined (HAVE_INTEGER_16)
  117. case GFC_DTYPE_LOGICAL_16:
  118. case GFC_DTYPE_INTEGER_16:
  119. cshift1_16_i16 ((gfc_array_i16 *)ret, (gfc_array_i16 *) array,
  120. h, pwhich);
  121. return;
  122. #endif
  123. case GFC_DTYPE_REAL_4:
  124. cshift1_16_r4 ((gfc_array_r4 *)ret, (gfc_array_r4 *) array,
  125. h, pwhich);
  126. return;
  127. case GFC_DTYPE_REAL_8:
  128. cshift1_16_r8 ((gfc_array_r8 *)ret, (gfc_array_r8 *) array,
  129. h, pwhich);
  130. return;
  131. #if defined (HAVE_REAL_10)
  132. case GFC_DTYPE_REAL_10:
  133. cshift1_16_r10 ((gfc_array_r10 *)ret, (gfc_array_r10 *) array,
  134. h, pwhich);
  135. return;
  136. #endif
  137. #if defined (HAVE_REAL_16)
  138. case GFC_DTYPE_REAL_16:
  139. cshift1_16_r16 ((gfc_array_r16 *)ret, (gfc_array_r16 *) array,
  140. h, pwhich);
  141. return;
  142. #endif
  143. case GFC_DTYPE_COMPLEX_4:
  144. cshift1_16_c4 ((gfc_array_c4 *)ret, (gfc_array_c4 *) array,
  145. h, pwhich);
  146. return;
  147. case GFC_DTYPE_COMPLEX_8:
  148. cshift1_16_c8 ((gfc_array_c8 *)ret, (gfc_array_c8 *) array,
  149. h, pwhich);
  150. return;
  151. #if defined (HAVE_COMPLEX_10)
  152. case GFC_DTYPE_COMPLEX_10:
  153. cshift1_16_c10 ((gfc_array_c10 *)ret, (gfc_array_c10 *) array,
  154. h, pwhich);
  155. return;
  156. #endif
  157. #if defined (HAVE_COMPLEX_16)
  158. case GFC_DTYPE_COMPLEX_16:
  159. cshift1_16_c16 ((gfc_array_c16 *)ret, (gfc_array_c16 *) array,
  160. h, pwhich);
  161. return;
  162. #endif
  163. default:
  164. break;
  165. }
  166. extent[0] = 1;
  167. count[0] = 0;
  168. n = 0;
  169. /* Initialized for avoiding compiler warnings. */
  170. roffset = size;
  171. soffset = size;
  172. len = 0;
  173. for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
  174. {
  175. if (dim == which)
  176. {
  177. roffset = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
  178. if (roffset == 0)
  179. roffset = size;
  180. soffset = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
  181. if (soffset == 0)
  182. soffset = size;
  183. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  184. }
  185. else
  186. {
  187. count[n] = 0;
  188. extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
  189. rstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
  190. sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
  191. hstride[n] = GFC_DESCRIPTOR_STRIDE(h,n);
  192. n++;
  193. }
  194. }
  195. if (sstride[0] == 0)
  196. sstride[0] = size;
  197. if (rstride[0] == 0)
  198. rstride[0] = size;
  199. if (hstride[0] == 0)
  200. hstride[0] = 1;
  201. dim = GFC_DESCRIPTOR_RANK (array);
  202. rstride0 = rstride[0];
  203. sstride0 = sstride[0];
  204. hstride0 = hstride[0];
  205. rptr = ret->base_addr;
  206. sptr = array->base_addr;
  207. hptr = h->base_addr;
  208. while (rptr)
  209. {
  210. /* Do the shift for this dimension. */
  211. sh = *hptr;
  212. /* Normal case should be -len < sh < len; try to
  213. avoid the expensive remainder operation if possible. */
  214. if (sh < 0)
  215. sh += len;
  216. if (unlikely (sh >= len || sh < 0))
  217. {
  218. sh = sh % len;
  219. if (sh < 0)
  220. sh += len;
  221. }
  222. src = &sptr[sh * soffset];
  223. dest = rptr;
  224. if (soffset == size && roffset == size)
  225. {
  226. size_t len1 = sh * size;
  227. size_t len2 = (len - sh) * size;
  228. memcpy (rptr, sptr + len1, len2);
  229. memcpy (rptr + len2, sptr, len1);
  230. }
  231. else
  232. {
  233. for (n = 0; n < len - sh; n++)
  234. {
  235. memcpy (dest, src, size);
  236. dest += roffset;
  237. src += soffset;
  238. }
  239. for (src = sptr, n = 0; n < sh; n++)
  240. {
  241. memcpy (dest, src, size);
  242. dest += roffset;
  243. src += soffset;
  244. }
  245. }
  246. /* Advance to the next section. */
  247. rptr += rstride0;
  248. sptr += sstride0;
  249. hptr += hstride0;
  250. count[0]++;
  251. n = 0;
  252. while (count[n] == extent[n])
  253. {
  254. /* When we get to the end of a dimension, reset it and increment
  255. the next dimension. */
  256. count[n] = 0;
  257. /* We could precalculate these products, but this is a less
  258. frequently used path so probably not worth it. */
  259. rptr -= rstride[n] * extent[n];
  260. sptr -= sstride[n] * extent[n];
  261. hptr -= hstride[n] * extent[n];
  262. n++;
  263. if (n >= dim - 1)
  264. {
  265. /* Break out of the loop. */
  266. rptr = NULL;
  267. break;
  268. }
  269. else
  270. {
  271. count[n]++;
  272. rptr += rstride[n];
  273. sptr += sstride[n];
  274. hptr += hstride[n];
  275. }
  276. }
  277. }
  278. }
  279. void cshift1_16 (gfc_array_char * const restrict,
  280. const gfc_array_char * const restrict,
  281. const gfc_array_i16 * const restrict,
  282. const GFC_INTEGER_16 * const restrict);
  283. export_proto(cshift1_16);
  284. void
  285. cshift1_16 (gfc_array_char * const restrict ret,
  286. const gfc_array_char * const restrict array,
  287. const gfc_array_i16 * const restrict h,
  288. const GFC_INTEGER_16 * const restrict pwhich)
  289. {
  290. cshift1 (ret, array, h, pwhich);
  291. }
  292. void cshift1_16_char (gfc_array_char * const restrict ret,
  293. GFC_INTEGER_4,
  294. const gfc_array_char * const restrict array,
  295. const gfc_array_i16 * const restrict h,
  296. const GFC_INTEGER_16 * const restrict pwhich,
  297. GFC_INTEGER_4);
  298. export_proto(cshift1_16_char);
  299. void
  300. cshift1_16_char (gfc_array_char * const restrict ret,
  301. GFC_INTEGER_4 ret_length __attribute__((unused)),
  302. const gfc_array_char * const restrict array,
  303. const gfc_array_i16 * const restrict h,
  304. const GFC_INTEGER_16 * const restrict pwhich,
  305. GFC_INTEGER_4 array_length __attribute__((unused)))
  306. {
  307. cshift1 (ret, array, h, pwhich);
  308. }
  309. void cshift1_16_char4 (gfc_array_char * const restrict ret,
  310. GFC_INTEGER_4,
  311. const gfc_array_char * const restrict array,
  312. const gfc_array_i16 * const restrict h,
  313. const GFC_INTEGER_16 * const restrict pwhich,
  314. GFC_INTEGER_4);
  315. export_proto(cshift1_16_char4);
  316. void
  317. cshift1_16_char4 (gfc_array_char * const restrict ret,
  318. GFC_INTEGER_4 ret_length __attribute__((unused)),
  319. const gfc_array_char * const restrict array,
  320. const gfc_array_i16 * const restrict h,
  321. const GFC_INTEGER_16 * const restrict pwhich,
  322. GFC_INTEGER_4 array_length __attribute__((unused)))
  323. {
  324. cshift1 (ret, array, h, pwhich);
  325. }
  326. #endif