parity_l4.c 4.7 KB

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