parity_l4.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Implementation of the PARITY intrinsic
  2. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. Contributed by Tobias Burnus <burnus@net-b.de>
  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. #if defined (HAVE_GFC_LOGICAL_4) && defined (HAVE_GFC_LOGICAL_4)
  22. extern void parity_l4 (gfc_array_l4 * const restrict,
  23. gfc_array_l4 * const restrict, const index_type * const restrict);
  24. export_proto(parity_l4);
  25. void
  26. parity_l4 (gfc_array_l4 * const restrict retarray,
  27. gfc_array_l4 * const restrict array,
  28. const index_type * const restrict pdim)
  29. {
  30. index_type count[GFC_MAX_DIMENSIONS];
  31. index_type extent[GFC_MAX_DIMENSIONS];
  32. index_type sstride[GFC_MAX_DIMENSIONS];
  33. index_type dstride[GFC_MAX_DIMENSIONS];
  34. const GFC_LOGICAL_4 * restrict base;
  35. GFC_LOGICAL_4 * restrict dest;
  36. index_type rank;
  37. index_type n;
  38. index_type len;
  39. index_type delta;
  40. index_type dim;
  41. int continue_loop;
  42. /* Make dim zero based to avoid confusion. */
  43. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  44. dim = (*pdim) - 1;
  45. if (unlikely (dim < 0 || dim > rank))
  46. {
  47. runtime_error ("Dim argument incorrect in PARITY intrinsic: "
  48. "is %ld, should be between 1 and %ld",
  49. (long int) dim + 1, (long int) rank + 1);
  50. }
  51. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  52. if (len < 0)
  53. len = 0;
  54. delta = GFC_DESCRIPTOR_STRIDE(array,dim);
  55. for (n = 0; n < dim; n++)
  56. {
  57. sstride[n] = GFC_DESCRIPTOR_STRIDE(array,n);
  58. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  59. if (extent[n] < 0)
  60. extent[n] = 0;
  61. }
  62. for (n = dim; n < rank; n++)
  63. {
  64. sstride[n] = GFC_DESCRIPTOR_STRIDE(array, n + 1);
  65. extent[n] = GFC_DESCRIPTOR_EXTENT(array, n + 1);
  66. if (extent[n] < 0)
  67. extent[n] = 0;
  68. }
  69. if (retarray->base_addr == NULL)
  70. {
  71. size_t alloc_size, str;
  72. for (n = 0; n < rank; n++)
  73. {
  74. if (n == 0)
  75. str = 1;
  76. else
  77. str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
  78. GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
  79. }
  80. retarray->offset = 0;
  81. retarray->dtype.rank = rank;
  82. alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
  83. retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_LOGICAL_4));
  84. if (alloc_size == 0)
  85. {
  86. /* Make sure we have a zero-sized array. */
  87. GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
  88. return;
  89. }
  90. }
  91. else
  92. {
  93. if (rank != GFC_DESCRIPTOR_RANK (retarray))
  94. runtime_error ("rank of return array incorrect in"
  95. " PARITY intrinsic: is %ld, should be %ld",
  96. (long int) (GFC_DESCRIPTOR_RANK (retarray)),
  97. (long int) rank);
  98. if (unlikely (compile_options.bounds_check))
  99. bounds_ifunction_return ((array_t *) retarray, extent,
  100. "return value", "PARITY");
  101. }
  102. for (n = 0; n < rank; n++)
  103. {
  104. count[n] = 0;
  105. dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
  106. if (extent[n] <= 0)
  107. return;
  108. }
  109. base = array->base_addr;
  110. dest = retarray->base_addr;
  111. continue_loop = 1;
  112. while (continue_loop)
  113. {
  114. const GFC_LOGICAL_4 * restrict src;
  115. GFC_LOGICAL_4 result;
  116. src = base;
  117. {
  118. result = 0;
  119. if (len <= 0)
  120. *dest = 0;
  121. else
  122. {
  123. #if ! defined HAVE_BACK_ARG
  124. for (n = 0; n < len; n++, src += delta)
  125. {
  126. #endif
  127. result = result != *src;
  128. }
  129. *dest = result;
  130. }
  131. }
  132. /* Advance to the next element. */
  133. count[0]++;
  134. base += sstride[0];
  135. dest += dstride[0];
  136. n = 0;
  137. while (count[n] == extent[n])
  138. {
  139. /* When we get to the end of a dimension, reset it and increment
  140. the next dimension. */
  141. count[n] = 0;
  142. /* We could precalculate these products, but this is a less
  143. frequently used path so probably not worth it. */
  144. base -= sstride[n] * extent[n];
  145. dest -= dstride[n] * extent[n];
  146. n++;
  147. if (n >= rank)
  148. {
  149. /* Break out of the loop. */
  150. continue_loop = 0;
  151. break;
  152. }
  153. else
  154. {
  155. count[n]++;
  156. base += sstride[n];
  157. dest += dstride[n];
  158. }
  159. }
  160. }
  161. }
  162. #endif