cshift0.m4 4.6 KB

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