cshift0_c8.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* Helper function for cshift functions.
  2. Copyright (C) 2008-2015 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 <stdlib.h>
  22. #include <assert.h>
  23. #include <string.h>
  24. #if defined (HAVE_GFC_COMPLEX_8)
  25. void
  26. cshift0_c8 (gfc_array_c8 *ret, const gfc_array_c8 *array, ptrdiff_t shift,
  27. int which)
  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_8 *rptr;
  34. /* s.* indicates the source array. */
  35. index_type sstride[GFC_MAX_DIMENSIONS];
  36. index_type sstride0;
  37. index_type soffset;
  38. const GFC_COMPLEX_8 *sptr;
  39. index_type count[GFC_MAX_DIMENSIONS];
  40. index_type extent[GFC_MAX_DIMENSIONS];
  41. index_type dim;
  42. index_type len;
  43. index_type n;
  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. for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
  55. {
  56. if (dim == which)
  57. {
  58. roffset = GFC_DESCRIPTOR_STRIDE(ret,dim);
  59. if (roffset == 0)
  60. roffset = 1;
  61. soffset = GFC_DESCRIPTOR_STRIDE(array,dim);
  62. if (soffset == 0)
  63. soffset = 1;
  64. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  65. }
  66. else
  67. {
  68. count[n] = 0;
  69. extent[n] = GFC_DESCRIPTOR_EXTENT(array,dim);
  70. rstride[n] = GFC_DESCRIPTOR_STRIDE(ret,dim);
  71. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,dim);
  72. n++;
  73. }
  74. }
  75. if (sstride[0] == 0)
  76. sstride[0] = 1;
  77. if (rstride[0] == 0)
  78. rstride[0] = 1;
  79. dim = GFC_DESCRIPTOR_RANK (array);
  80. rstride0 = rstride[0];
  81. sstride0 = sstride[0];
  82. rptr = ret->base_addr;
  83. sptr = array->base_addr;
  84. /* Avoid the costly modulo for trivially in-bound shifts. */
  85. if (shift < 0 || shift >= len)
  86. {
  87. shift = len == 0 ? 0 : shift % (ptrdiff_t)len;
  88. if (shift < 0)
  89. shift += len;
  90. }
  91. while (rptr)
  92. {
  93. /* Do the shift for this dimension. */
  94. /* If elements are contiguous, perform the operation
  95. in two block moves. */
  96. if (soffset == 1 && roffset == 1)
  97. {
  98. size_t len1 = shift * sizeof (GFC_COMPLEX_8);
  99. size_t len2 = (len - shift) * sizeof (GFC_COMPLEX_8);
  100. memcpy (rptr, sptr + shift, len2);
  101. memcpy (rptr + (len - shift), sptr, len1);
  102. }
  103. else
  104. {
  105. /* Otherwise, we will have to perform the copy one element at
  106. a time. */
  107. GFC_COMPLEX_8 *dest = rptr;
  108. const GFC_COMPLEX_8 *src = &sptr[shift * soffset];
  109. for (n = 0; n < len - shift; n++)
  110. {
  111. *dest = *src;
  112. dest += roffset;
  113. src += soffset;
  114. }
  115. for (src = sptr, n = 0; n < shift; n++)
  116. {
  117. *dest = *src;
  118. dest += roffset;
  119. src += soffset;
  120. }
  121. }
  122. /* Advance to the next section. */
  123. rptr += rstride0;
  124. sptr += sstride0;
  125. count[0]++;
  126. n = 0;
  127. while (count[n] == extent[n])
  128. {
  129. /* When we get to the end of a dimension, reset it and increment
  130. the next dimension. */
  131. count[n] = 0;
  132. /* We could precalculate these products, but this is a less
  133. frequently used path so probably not worth it. */
  134. rptr -= rstride[n] * extent[n];
  135. sptr -= sstride[n] * extent[n];
  136. n++;
  137. if (n >= dim - 1)
  138. {
  139. /* Break out of the loop. */
  140. rptr = NULL;
  141. break;
  142. }
  143. else
  144. {
  145. count[n]++;
  146. rptr += rstride[n];
  147. sptr += sstride[n];
  148. }
  149. }
  150. }
  151. return;
  152. }
  153. #endif