cshift0.c 13 KB

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