all_l2.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* Implementation of the ALL intrinsic
  2. Copyright (C) 2002-2022 Free Software Foundation, Inc.
  3. Contributed by Paul Brook <paul@nowt.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. #if defined (HAVE_GFC_LOGICAL_2)
  22. extern void all_l2 (gfc_array_l2 * const restrict,
  23. gfc_array_l1 * const restrict, const index_type * const restrict);
  24. export_proto(all_l2);
  25. void
  26. all_l2 (gfc_array_l2 * const restrict retarray,
  27. gfc_array_l1 * 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_1 * restrict base;
  35. GFC_LOGICAL_2 * restrict dest;
  36. index_type rank;
  37. index_type n;
  38. index_type len;
  39. index_type delta;
  40. index_type dim;
  41. int src_kind;
  42. int continue_loop;
  43. /* Make dim zero based to avoid confusion. */
  44. dim = (*pdim) - 1;
  45. rank = GFC_DESCRIPTOR_RANK (array) - 1;
  46. src_kind = GFC_DESCRIPTOR_SIZE (array);
  47. len = GFC_DESCRIPTOR_EXTENT(array,dim);
  48. if (len < 0)
  49. len = 0;
  50. delta = GFC_DESCRIPTOR_STRIDE_BYTES(array,dim);
  51. for (n = 0; n < dim; n++)
  52. {
  53. sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,n);
  54. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  55. if (extent[n] < 0)
  56. extent[n] = 0;
  57. }
  58. for (n = dim; n < rank; n++)
  59. {
  60. sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,n + 1);
  61. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n + 1);
  62. if (extent[n] < 0)
  63. extent[n] = 0;
  64. }
  65. if (retarray->base_addr == NULL)
  66. {
  67. size_t alloc_size, str;
  68. for (n = 0; n < rank; n++)
  69. {
  70. if (n == 0)
  71. str = 1;
  72. else
  73. str = GFC_DESCRIPTOR_STRIDE(retarray,n-1) * extent[n-1];
  74. GFC_DIMENSION_SET(retarray->dim[n], 0, extent[n] - 1, str);
  75. }
  76. retarray->offset = 0;
  77. retarray->dtype.rank = rank;
  78. alloc_size = GFC_DESCRIPTOR_STRIDE(retarray,rank-1) * extent[rank-1];
  79. if (alloc_size == 0)
  80. {
  81. /* Make sure we have a zero-sized array. */
  82. GFC_DIMENSION_SET(retarray->dim[0], 0, -1, 1);
  83. return;
  84. }
  85. else
  86. retarray->base_addr = xmallocarray (alloc_size, sizeof (GFC_LOGICAL_2));
  87. }
  88. else
  89. {
  90. if (rank != GFC_DESCRIPTOR_RANK (retarray))
  91. runtime_error ("rank of return array incorrect in"
  92. " ALL 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. {
  97. for (n=0; n < rank; n++)
  98. {
  99. index_type ret_extent;
  100. ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,n);
  101. if (extent[n] != ret_extent)
  102. runtime_error ("Incorrect extent in return value of"
  103. " ALL intrinsic in dimension %d:"
  104. " is %ld, should be %ld", (int) n + 1,
  105. (long int) ret_extent, (long int) extent[n]);
  106. }
  107. }
  108. }
  109. for (n = 0; n < rank; n++)
  110. {
  111. count[n] = 0;
  112. dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n);
  113. if (extent[n] <= 0)
  114. return;
  115. }
  116. base = array->base_addr;
  117. if (src_kind == 1 || src_kind == 2 || src_kind == 4 || src_kind == 8
  118. #ifdef HAVE_GFC_LOGICAL_16
  119. || src_kind == 16
  120. #endif
  121. )
  122. {
  123. if (base)
  124. base = GFOR_POINTER_TO_L1 (base, src_kind);
  125. }
  126. else
  127. internal_error (NULL, "Funny sized logical array in ALL intrinsic");
  128. dest = retarray->base_addr;
  129. continue_loop = 1;
  130. while (continue_loop)
  131. {
  132. const GFC_LOGICAL_1 * restrict src;
  133. GFC_LOGICAL_2 result;
  134. src = base;
  135. {
  136. /* Return true only if all the elements are set. */
  137. result = 1;
  138. if (len <= 0)
  139. *dest = 1;
  140. else
  141. {
  142. for (n = 0; n < len; n++, src += delta)
  143. {
  144. if (! *src)
  145. {
  146. result = 0;
  147. break;
  148. }
  149. }
  150. *dest = result;
  151. }
  152. }
  153. /* Advance to the next element. */
  154. count[0]++;
  155. base += sstride[0];
  156. dest += dstride[0];
  157. n = 0;
  158. while (count[n] == extent[n])
  159. {
  160. /* When we get to the end of a dimension, reset it and increment
  161. the next dimension. */
  162. count[n] = 0;
  163. /* We could precalculate these products, but this is a less
  164. frequently used path so probably not worth it. */
  165. base -= sstride[n] * extent[n];
  166. dest -= dstride[n] * extent[n];
  167. n++;
  168. if (n >= rank)
  169. {
  170. /* Break out of the loop. */
  171. continue_loop = 0;
  172. break;
  173. }
  174. else
  175. {
  176. count[n]++;
  177. base += sstride[n];
  178. dest += dstride[n];
  179. }
  180. }
  181. }
  182. }
  183. #endif