cshift0.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* Generic 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. 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 <string.h>
  22. static void
  23. cshift0 (gfc_array_char * ret, const gfc_array_char * array,
  24. ptrdiff_t shift, int which, index_type size)
  25. {
  26. /* r.* indicates the return array. */
  27. index_type rstride[GFC_MAX_DIMENSIONS];
  28. index_type rstride0;
  29. index_type roffset;
  30. char *rptr;
  31. /* s.* indicates the source array. */
  32. index_type sstride[GFC_MAX_DIMENSIONS];
  33. index_type sstride0;
  34. index_type soffset;
  35. const char *sptr;
  36. index_type count[GFC_MAX_DIMENSIONS];
  37. index_type extent[GFC_MAX_DIMENSIONS];
  38. index_type dim;
  39. index_type len;
  40. index_type n;
  41. index_type arraysize;
  42. index_type type_size;
  43. if (which < 1 || which > GFC_DESCRIPTOR_RANK (array))
  44. runtime_error ("Argument 'DIM' is out of range in call to 'CSHIFT'");
  45. arraysize = size0 ((array_t *) array);
  46. if (ret->base_addr == NULL)
  47. {
  48. int i;
  49. ret->offset = 0;
  50. GFC_DTYPE_COPY(ret,array);
  51. for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
  52. {
  53. index_type ub, str;
  54. ub = GFC_DESCRIPTOR_EXTENT(array,i) - 1;
  55. if (i == 0)
  56. str = 1;
  57. else
  58. str = GFC_DESCRIPTOR_EXTENT(ret,i-1) *
  59. GFC_DESCRIPTOR_STRIDE(ret,i-1);
  60. GFC_DIMENSION_SET(ret->dim[i], 0, ub, str);
  61. }
  62. /* xmallocarray allocates a single byte for zero size. */
  63. ret->base_addr = xmallocarray (arraysize, size);
  64. }
  65. else if (unlikely (compile_options.bounds_check))
  66. {
  67. bounds_equal_extents ((array_t *) ret, (array_t *) array,
  68. "return value", "CSHIFT");
  69. }
  70. if (arraysize == 0)
  71. return;
  72. type_size = GFC_DTYPE_TYPE_SIZE (array);
  73. switch(type_size)
  74. {
  75. case GFC_DTYPE_LOGICAL_1:
  76. case GFC_DTYPE_INTEGER_1:
  77. cshift0_i1 ((gfc_array_i1 *)ret, (gfc_array_i1 *) array, shift, which);
  78. return;
  79. case GFC_DTYPE_LOGICAL_2:
  80. case GFC_DTYPE_INTEGER_2:
  81. cshift0_i2 ((gfc_array_i2 *)ret, (gfc_array_i2 *) array, shift, which);
  82. return;
  83. case GFC_DTYPE_LOGICAL_4:
  84. case GFC_DTYPE_INTEGER_4:
  85. cshift0_i4 ((gfc_array_i4 *)ret, (gfc_array_i4 *) array, shift, which);
  86. return;
  87. case GFC_DTYPE_LOGICAL_8:
  88. case GFC_DTYPE_INTEGER_8:
  89. cshift0_i8 ((gfc_array_i8 *)ret, (gfc_array_i8 *) array, shift, which);
  90. return;
  91. #ifdef HAVE_GFC_INTEGER_16
  92. case GFC_DTYPE_LOGICAL_16:
  93. case GFC_DTYPE_INTEGER_16:
  94. cshift0_i16 ((gfc_array_i16 *)ret, (gfc_array_i16 *) array, shift,
  95. which);
  96. return;
  97. #endif
  98. case GFC_DTYPE_REAL_4:
  99. cshift0_r4 ((gfc_array_r4 *)ret, (gfc_array_r4 *) array, shift, which);
  100. return;
  101. case GFC_DTYPE_REAL_8:
  102. cshift0_r8 ((gfc_array_r8 *)ret, (gfc_array_r8 *) array, shift, which);
  103. return;
  104. /* FIXME: This here is a hack, which will have to be removed when
  105. the array descriptor is reworked. Currently, we don't store the
  106. kind value for the type, but only the size. Because on targets with
  107. __float128, we have sizeof(logn double) == sizeof(__float128),
  108. we cannot discriminate here and have to fall back to the generic
  109. handling (which is suboptimal). */
  110. #if !defined(GFC_REAL_16_IS_FLOAT128)
  111. # ifdef HAVE_GFC_REAL_10
  112. case GFC_DTYPE_REAL_10:
  113. cshift0_r10 ((gfc_array_r10 *)ret, (gfc_array_r10 *) array, shift,
  114. which);
  115. return;
  116. # endif
  117. # ifdef HAVE_GFC_REAL_16
  118. case GFC_DTYPE_REAL_16:
  119. cshift0_r16 ((gfc_array_r16 *)ret, (gfc_array_r16 *) array, shift,
  120. which);
  121. return;
  122. # endif
  123. #endif
  124. case GFC_DTYPE_COMPLEX_4:
  125. cshift0_c4 ((gfc_array_c4 *)ret, (gfc_array_c4 *) array, shift, which);
  126. return;
  127. case GFC_DTYPE_COMPLEX_8:
  128. cshift0_c8 ((gfc_array_c8 *)ret, (gfc_array_c8 *) array, shift, which);
  129. return;
  130. /* FIXME: This here is a hack, which will have to be removed when
  131. the array descriptor is reworked. Currently, we don't store the
  132. kind value for the type, but only the size. Because on targets with
  133. __float128, we have sizeof(logn double) == sizeof(__float128),
  134. we cannot discriminate here and have to fall back to the generic
  135. handling (which is suboptimal). */
  136. #if !defined(GFC_REAL_16_IS_FLOAT128)
  137. # ifdef HAVE_GFC_COMPLEX_10
  138. case GFC_DTYPE_COMPLEX_10:
  139. cshift0_c10 ((gfc_array_c10 *)ret, (gfc_array_c10 *) array, shift,
  140. which);
  141. return;
  142. # endif
  143. # ifdef HAVE_GFC_COMPLEX_16
  144. case GFC_DTYPE_COMPLEX_16:
  145. cshift0_c16 ((gfc_array_c16 *)ret, (gfc_array_c16 *) array, shift,
  146. which);
  147. return;
  148. # endif
  149. #endif
  150. default:
  151. break;
  152. }
  153. switch (size)
  154. {
  155. /* Let's check the actual alignment of the data pointers. If they
  156. are suitably aligned, we can safely call the unpack functions. */
  157. case sizeof (GFC_INTEGER_1):
  158. cshift0_i1 ((gfc_array_i1 *) ret, (gfc_array_i1 *) array, shift,
  159. which);
  160. break;
  161. case sizeof (GFC_INTEGER_2):
  162. if (GFC_UNALIGNED_2(ret->base_addr) || GFC_UNALIGNED_2(array->base_addr))
  163. break;
  164. else
  165. {
  166. cshift0_i2 ((gfc_array_i2 *) ret, (gfc_array_i2 *) array, shift,
  167. which);
  168. return;
  169. }
  170. case sizeof (GFC_INTEGER_4):
  171. if (GFC_UNALIGNED_4(ret->base_addr) || GFC_UNALIGNED_4(array->base_addr))
  172. break;
  173. else
  174. {
  175. cshift0_i4 ((gfc_array_i4 *)ret, (gfc_array_i4 *) array, shift,
  176. which);
  177. return;
  178. }
  179. case sizeof (GFC_INTEGER_8):
  180. if (GFC_UNALIGNED_8(ret->base_addr) || GFC_UNALIGNED_8(array->base_addr))
  181. {
  182. /* Let's try to use the complex routines. First, a sanity
  183. check that the sizes match; this should be optimized to
  184. a no-op. */
  185. if (sizeof(GFC_INTEGER_8) != sizeof(GFC_COMPLEX_4))
  186. break;
  187. if (GFC_UNALIGNED_C4(ret->base_addr)
  188. || GFC_UNALIGNED_C4(array->base_addr))
  189. break;
  190. cshift0_c4 ((gfc_array_c4 *) ret, (gfc_array_c4 *) array, shift,
  191. which);
  192. return;
  193. }
  194. else
  195. {
  196. cshift0_i8 ((gfc_array_i8 *)ret, (gfc_array_i8 *) array, shift,
  197. which);
  198. return;
  199. }
  200. #ifdef HAVE_GFC_INTEGER_16
  201. case sizeof (GFC_INTEGER_16):
  202. if (GFC_UNALIGNED_16(ret->base_addr)
  203. || GFC_UNALIGNED_16(array->base_addr))
  204. {
  205. /* Let's try to use the complex routines. First, a sanity
  206. check that the sizes match; this should be optimized to
  207. a no-op. */
  208. if (sizeof(GFC_INTEGER_16) != sizeof(GFC_COMPLEX_8))
  209. break;
  210. if (GFC_UNALIGNED_C8(ret->base_addr)
  211. || GFC_UNALIGNED_C8(array->base_addr))
  212. break;
  213. cshift0_c8 ((gfc_array_c8 *) ret, (gfc_array_c8 *) array, shift,
  214. which);
  215. return;
  216. }
  217. else
  218. {
  219. cshift0_i16 ((gfc_array_i16 *) ret, (gfc_array_i16 *) array,
  220. shift, which);
  221. return;
  222. }
  223. #else
  224. case sizeof (GFC_COMPLEX_8):
  225. if (GFC_UNALIGNED_C8(ret->base_addr)
  226. || GFC_UNALIGNED_C8(array->base_addr))
  227. break;
  228. else
  229. {
  230. cshift0_c8 ((gfc_array_c8 *) ret, (gfc_array_c8 *) array, shift,
  231. which);
  232. return;
  233. }
  234. #endif
  235. default:
  236. break;
  237. }
  238. which = which - 1;
  239. sstride[0] = 0;
  240. rstride[0] = 0;
  241. extent[0] = 1;
  242. count[0] = 0;
  243. n = 0;
  244. /* Initialized for avoiding compiler warnings. */
  245. roffset = size;
  246. soffset = size;
  247. len = 0;
  248. for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
  249. {
  250. if (dim == which)
  251. {
  252. roffset = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
  253. if (roffset == 0)
  254. roffset = size;
  255. soffset = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
  256. if (soffset == 0)
  257. soffset = size;
  258. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  259. }
  260. else
  261. {
  262. count[n] = 0;
  263. extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
  264. rstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(ret,dim);
  265. sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
  266. n++;
  267. }
  268. }
  269. if (sstride[0] == 0)
  270. sstride[0] = size;
  271. if (rstride[0] == 0)
  272. rstride[0] = size;
  273. dim = GFC_DESCRIPTOR_RANK (array);
  274. rstride0 = rstride[0];
  275. sstride0 = sstride[0];
  276. rptr = ret->base_addr;
  277. sptr = array->base_addr;
  278. shift = len == 0 ? 0 : shift % (ptrdiff_t)len;
  279. if (shift < 0)
  280. shift += len;
  281. while (rptr)
  282. {
  283. /* Do the shift for this dimension. */
  284. /* If elements are contiguous, perform the operation
  285. in two block moves. */
  286. if (soffset == size && roffset == size)
  287. {
  288. size_t len1 = shift * size;
  289. size_t len2 = (len - shift) * size;
  290. memcpy (rptr, sptr + len1, len2);
  291. memcpy (rptr + len2, sptr, len1);
  292. }
  293. else
  294. {
  295. /* Otherwise, we'll have to perform the copy one element at
  296. a time. */
  297. char *dest = rptr;
  298. const char *src = &sptr[shift * soffset];
  299. for (n = 0; n < len - shift; n++)
  300. {
  301. memcpy (dest, src, size);
  302. dest += roffset;
  303. src += soffset;
  304. }
  305. for (src = sptr, n = 0; n < shift; n++)
  306. {
  307. memcpy (dest, src, size);
  308. dest += roffset;
  309. src += soffset;
  310. }
  311. }
  312. /* Advance to the next section. */
  313. rptr += rstride0;
  314. sptr += sstride0;
  315. count[0]++;
  316. n = 0;
  317. while (count[n] == extent[n])
  318. {
  319. /* When we get to the end of a dimension, reset it and increment
  320. the next dimension. */
  321. count[n] = 0;
  322. /* We could precalculate these products, but this is a less
  323. frequently used path so probably not worth it. */
  324. rptr -= rstride[n] * extent[n];
  325. sptr -= sstride[n] * extent[n];
  326. n++;
  327. if (n >= dim - 1)
  328. {
  329. /* Break out of the loop. */
  330. rptr = NULL;
  331. break;
  332. }
  333. else
  334. {
  335. count[n]++;
  336. rptr += rstride[n];
  337. sptr += sstride[n];
  338. }
  339. }
  340. }
  341. }
  342. #define DEFINE_CSHIFT(N) \
  343. extern void cshift0_##N (gfc_array_char *, const gfc_array_char *, \
  344. const GFC_INTEGER_##N *, const GFC_INTEGER_##N *); \
  345. export_proto(cshift0_##N); \
  346. \
  347. void \
  348. cshift0_##N (gfc_array_char *ret, const gfc_array_char *array, \
  349. const GFC_INTEGER_##N *pshift, const GFC_INTEGER_##N *pdim) \
  350. { \
  351. cshift0 (ret, array, *pshift, pdim ? *pdim : 1, \
  352. GFC_DESCRIPTOR_SIZE (array)); \
  353. } \
  354. \
  355. extern void cshift0_##N##_char (gfc_array_char *, GFC_INTEGER_4, \
  356. const gfc_array_char *, \
  357. const GFC_INTEGER_##N *, \
  358. const GFC_INTEGER_##N *, GFC_INTEGER_4); \
  359. export_proto(cshift0_##N##_char); \
  360. \
  361. void \
  362. cshift0_##N##_char (gfc_array_char *ret, \
  363. GFC_INTEGER_4 ret_length __attribute__((unused)), \
  364. const gfc_array_char *array, \
  365. const GFC_INTEGER_##N *pshift, \
  366. const GFC_INTEGER_##N *pdim, \
  367. GFC_INTEGER_4 array_length) \
  368. { \
  369. cshift0 (ret, array, *pshift, pdim ? *pdim : 1, array_length); \
  370. } \
  371. \
  372. extern void cshift0_##N##_char4 (gfc_array_char *, GFC_INTEGER_4, \
  373. const gfc_array_char *, \
  374. const GFC_INTEGER_##N *, \
  375. const GFC_INTEGER_##N *, GFC_INTEGER_4); \
  376. export_proto(cshift0_##N##_char4); \
  377. \
  378. void \
  379. cshift0_##N##_char4 (gfc_array_char *ret, \
  380. GFC_INTEGER_4 ret_length __attribute__((unused)), \
  381. const gfc_array_char *array, \
  382. const GFC_INTEGER_##N *pshift, \
  383. const GFC_INTEGER_##N *pdim, \
  384. GFC_INTEGER_4 array_length) \
  385. { \
  386. cshift0 (ret, array, *pshift, pdim ? *pdim : 1, \
  387. array_length * sizeof (gfc_char4_t)); \
  388. }
  389. DEFINE_CSHIFT (1);
  390. DEFINE_CSHIFT (2);
  391. DEFINE_CSHIFT (4);
  392. DEFINE_CSHIFT (8);
  393. #ifdef HAVE_GFC_INTEGER_16
  394. DEFINE_CSHIFT (16);
  395. #endif