cshift1_4_c4.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* Implementation of the CSHIFT intrinsic.
  2. Copyright (C) 2017-2022 Free Software Foundation, Inc.
  3. Contributed by Thomas Koenig <tkoenig@gcc.gnu.org>
  4. This file is part of the GNU Fortran 95 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_4) && defined (HAVE_GFC_INTEGER_4)
  23. void
  24. cshift1_4_c4 (gfc_array_c4 * const restrict ret,
  25. const gfc_array_c4 * const restrict array,
  26. const gfc_array_i4 * const restrict h,
  27. const GFC_INTEGER_4 * 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. GFC_COMPLEX_4 *rptr;
  34. GFC_COMPLEX_4 *dest;
  35. /* s.* indicates the source array. */
  36. index_type sstride[GFC_MAX_DIMENSIONS];
  37. index_type sstride0;
  38. index_type soffset;
  39. const GFC_COMPLEX_4 *sptr;
  40. const GFC_COMPLEX_4 *src;
  41. /* h.* indicates the shift array. */
  42. index_type hstride[GFC_MAX_DIMENSIONS];
  43. index_type hstride0;
  44. const GFC_INTEGER_4 *hptr;
  45. index_type count[GFC_MAX_DIMENSIONS];
  46. index_type extent[GFC_MAX_DIMENSIONS];
  47. index_type rs_ex[GFC_MAX_DIMENSIONS];
  48. index_type ss_ex[GFC_MAX_DIMENSIONS];
  49. index_type hs_ex[GFC_MAX_DIMENSIONS];
  50. index_type dim;
  51. index_type len;
  52. index_type n;
  53. int which;
  54. GFC_INTEGER_4 sh;
  55. /* Bounds checking etc is already done by the caller. */
  56. if (pwhich)
  57. which = *pwhich - 1;
  58. else
  59. which = 0;
  60. extent[0] = 1;
  61. count[0] = 0;
  62. n = 0;
  63. /* Initialized for avoiding compiler warnings. */
  64. roffset = 1;
  65. soffset = 1;
  66. len = 0;
  67. for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
  68. {
  69. if (dim == which)
  70. {
  71. roffset = GFC_DESCRIPTOR_STRIDE(ret,dim);
  72. if (roffset == 0)
  73. roffset = 1;
  74. soffset = GFC_DESCRIPTOR_STRIDE(array,dim);
  75. if (soffset == 0)
  76. soffset = 1;
  77. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  78. }
  79. else
  80. {
  81. count[n] = 0;
  82. extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
  83. rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,dim);
  84. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,dim);
  85. hstride[n] = GFC_DESCRIPTOR_STRIDE(h,n);
  86. rs_ex[n] = rstride[n] * extent[n];
  87. ss_ex[n] = sstride[n] * extent[n];
  88. hs_ex[n] = hstride[n] * extent[n];
  89. n++;
  90. }
  91. }
  92. if (sstride[0] == 0)
  93. sstride[0] = 1;
  94. if (rstride[0] == 0)
  95. rstride[0] = 1;
  96. if (hstride[0] == 0)
  97. hstride[0] = 1;
  98. dim = GFC_DESCRIPTOR_RANK (array);
  99. rstride0 = rstride[0];
  100. sstride0 = sstride[0];
  101. hstride0 = hstride[0];
  102. rptr = ret->base_addr;
  103. sptr = array->base_addr;
  104. hptr = h->base_addr;
  105. while (rptr)
  106. {
  107. /* Do the shift for this dimension. */
  108. sh = *hptr;
  109. /* Normal case should be -len < sh < len; try to
  110. avoid the expensive remainder operation if possible. */
  111. if (sh < 0)
  112. sh += len;
  113. if (unlikely(sh >= len || sh < 0))
  114. {
  115. sh = sh % len;
  116. if (sh < 0)
  117. sh += len;
  118. }
  119. src = &sptr[sh * soffset];
  120. dest = rptr;
  121. if (soffset == 1 && roffset == 1)
  122. {
  123. size_t len1 = sh * sizeof (GFC_COMPLEX_4);
  124. size_t len2 = (len - sh) * sizeof (GFC_COMPLEX_4);
  125. memcpy (rptr, sptr + sh, len2);
  126. memcpy (rptr + (len - sh), sptr, len1);
  127. }
  128. else
  129. {
  130. for (n = 0; n < len - sh; n++)
  131. {
  132. *dest = *src;
  133. dest += roffset;
  134. src += soffset;
  135. }
  136. for (src = sptr, n = 0; n < sh; n++)
  137. {
  138. *dest = *src;
  139. dest += roffset;
  140. src += soffset;
  141. }
  142. }
  143. /* Advance to the next section. */
  144. rptr += rstride0;
  145. sptr += sstride0;
  146. hptr += hstride0;
  147. count[0]++;
  148. n = 0;
  149. while (count[n] == extent[n])
  150. {
  151. /* When we get to the end of a dimension, reset it and increment
  152. the next dimension. */
  153. count[n] = 0;
  154. rptr -= rs_ex[n];
  155. sptr -= ss_ex[n];
  156. hptr -= hs_ex[n];
  157. n++;
  158. if (n >= dim - 1)
  159. {
  160. /* Break out of the loop. */
  161. rptr = NULL;
  162. break;
  163. }
  164. else
  165. {
  166. count[n]++;
  167. rptr += rstride[n];
  168. sptr += sstride[n];
  169. hptr += hstride[n];
  170. }
  171. }
  172. }
  173. }
  174. #endif