any_l1.c 5.6 KB

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