cshift0_c8.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* Helper function for cshift functions.
  2. Copyright (C) 2008-2022 Free Software Foundation, Inc.
  3. Contributed by Thomas Koenig <tkoenig@gcc.gnu.org>
  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. #if defined (HAVE_GFC_COMPLEX_8)
  23. void
  24. cshift0_c8 (gfc_array_c8 *ret, const gfc_array_c8 *array, ptrdiff_t shift,
  25. int which)
  26. {
  27. /* r.* indicates the return array. */
  28. index_type rstride[GFC_MAX_DIMENSIONS];
  29. index_type rstride0;
  30. index_type roffset;
  31. GFC_COMPLEX_8 *rptr;
  32. /* s.* indicates the source array. */
  33. index_type sstride[GFC_MAX_DIMENSIONS];
  34. index_type sstride0;
  35. index_type soffset;
  36. const GFC_COMPLEX_8 *sptr;
  37. index_type count[GFC_MAX_DIMENSIONS];
  38. index_type extent[GFC_MAX_DIMENSIONS];
  39. index_type dim;
  40. index_type len;
  41. index_type n;
  42. bool do_blocked;
  43. index_type r_ex, a_ex;
  44. which = which - 1;
  45. sstride[0] = 0;
  46. rstride[0] = 0;
  47. extent[0] = 1;
  48. count[0] = 0;
  49. n = 0;
  50. /* Initialized for avoiding compiler warnings. */
  51. roffset = 1;
  52. soffset = 1;
  53. len = 0;
  54. r_ex = 1;
  55. a_ex = 1;
  56. if (which > 0)
  57. {
  58. /* Test if both ret and array are contiguous. */
  59. do_blocked = true;
  60. dim = GFC_DESCRIPTOR_RANK (array);
  61. for (n = 0; n < dim; n ++)
  62. {
  63. index_type rs, as;
  64. rs = GFC_DESCRIPTOR_STRIDE (ret, n);
  65. if (rs != r_ex)
  66. {
  67. do_blocked = false;
  68. break;
  69. }
  70. as = GFC_DESCRIPTOR_STRIDE (array, n);
  71. if (as != a_ex)
  72. {
  73. do_blocked = false;
  74. break;
  75. }
  76. r_ex *= GFC_DESCRIPTOR_EXTENT (ret, n);
  77. a_ex *= GFC_DESCRIPTOR_EXTENT (array, n);
  78. }
  79. }
  80. else
  81. do_blocked = false;
  82. n = 0;
  83. if (do_blocked)
  84. {
  85. /* For contiguous arrays, use the relationship that
  86. dimension(n1,n2,n3) :: a, b
  87. b = cshift(a,sh,3)
  88. can be dealt with as if
  89. dimension(n1*n2*n3) :: an, bn
  90. bn = cshift(a,sh*n1*n2,1)
  91. we can used a more blocked algorithm for dim>1. */
  92. sstride[0] = 1;
  93. rstride[0] = 1;
  94. roffset = 1;
  95. soffset = 1;
  96. len = GFC_DESCRIPTOR_STRIDE(array, which)
  97. * GFC_DESCRIPTOR_EXTENT(array, which);
  98. shift *= GFC_DESCRIPTOR_STRIDE(array, which);
  99. for (dim = which + 1; dim < GFC_DESCRIPTOR_RANK (array); dim++)
  100. {
  101. count[n] = 0;
  102. extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
  103. rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,dim);
  104. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,dim);
  105. n++;
  106. }
  107. dim = GFC_DESCRIPTOR_RANK (array) - which;
  108. }
  109. else
  110. {
  111. for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
  112. {
  113. if (dim == which)
  114. {
  115. roffset = GFC_DESCRIPTOR_STRIDE(ret,dim);
  116. if (roffset == 0)
  117. roffset = 1;
  118. soffset = GFC_DESCRIPTOR_STRIDE(array,dim);
  119. if (soffset == 0)
  120. soffset = 1;
  121. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  122. }
  123. else
  124. {
  125. count[n] = 0;
  126. extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
  127. rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,dim);
  128. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,dim);
  129. n++;
  130. }
  131. }
  132. if (sstride[0] == 0)
  133. sstride[0] = 1;
  134. if (rstride[0] == 0)
  135. rstride[0] = 1;
  136. dim = GFC_DESCRIPTOR_RANK (array);
  137. }
  138. rstride0 = rstride[0];
  139. sstride0 = sstride[0];
  140. rptr = ret->base_addr;
  141. sptr = array->base_addr;
  142. /* Avoid the costly modulo for trivially in-bound shifts. */
  143. if (shift < 0 || shift >= len)
  144. {
  145. shift = len == 0 ? 0 : shift % (ptrdiff_t)len;
  146. if (shift < 0)
  147. shift += len;
  148. }
  149. while (rptr)
  150. {
  151. /* Do the shift for this dimension. */
  152. /* If elements are contiguous, perform the operation
  153. in two block moves. */
  154. if (soffset == 1 && roffset == 1)
  155. {
  156. size_t len1 = shift * sizeof (GFC_COMPLEX_8);
  157. size_t len2 = (len - shift) * sizeof (GFC_COMPLEX_8);
  158. memcpy (rptr, sptr + shift, len2);
  159. memcpy (rptr + (len - shift), sptr, len1);
  160. }
  161. else
  162. {
  163. /* Otherwise, we will have to perform the copy one element at
  164. a time. */
  165. GFC_COMPLEX_8 *dest = rptr;
  166. const GFC_COMPLEX_8 *src = &sptr[shift * soffset];
  167. for (n = 0; n < len - shift; n++)
  168. {
  169. *dest = *src;
  170. dest += roffset;
  171. src += soffset;
  172. }
  173. for (src = sptr, n = 0; n < shift; n++)
  174. {
  175. *dest = *src;
  176. dest += roffset;
  177. src += soffset;
  178. }
  179. }
  180. /* Advance to the next section. */
  181. rptr += rstride0;
  182. sptr += sstride0;
  183. count[0]++;
  184. n = 0;
  185. while (count[n] == extent[n])
  186. {
  187. /* When we get to the end of a dimension, reset it and increment
  188. the next dimension. */
  189. count[n] = 0;
  190. /* We could precalculate these products, but this is a less
  191. frequently used path so probably not worth it. */
  192. rptr -= rstride[n] * extent[n];
  193. sptr -= sstride[n] * extent[n];
  194. n++;
  195. if (n >= dim - 1)
  196. {
  197. /* Break out of the loop. */
  198. rptr = NULL;
  199. break;
  200. }
  201. else
  202. {
  203. count[n]++;
  204. rptr += rstride[n];
  205. sptr += sstride[n];
  206. }
  207. }
  208. }
  209. return;
  210. }
  211. #endif