bounds.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* Copyright (C) 2009-2015 Free Software Foundation, Inc.
  2. Contributed by Thomas Koenig
  3. This file is part of the GNU Fortran runtime library (libgfortran).
  4. Libgfortran is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. Libgfortran is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "libgfortran.h"
  20. #include <assert.h>
  21. /* Auxiliary functions for bounds checking, mostly to reduce library size. */
  22. /* Bounds checking for the return values of the iforeach functions (such
  23. as maxloc and minloc). The extent of ret_array must
  24. must match the rank of array. */
  25. void
  26. bounds_iforeach_return (array_t *retarray, array_t *array, const char *name)
  27. {
  28. index_type rank;
  29. index_type ret_rank;
  30. index_type ret_extent;
  31. ret_rank = GFC_DESCRIPTOR_RANK (retarray);
  32. if (ret_rank != 1)
  33. runtime_error ("Incorrect rank of return array in %s intrinsic:"
  34. "is %ld, should be 1", name, (long int) ret_rank);
  35. rank = GFC_DESCRIPTOR_RANK (array);
  36. ret_extent = GFC_DESCRIPTOR_EXTENT(retarray,0);
  37. if (ret_extent != rank)
  38. runtime_error ("Incorrect extent in return value of"
  39. " %s intrinsic: is %ld, should be %ld",
  40. name, (long int) ret_extent, (long int) rank);
  41. }
  42. /* Check the return of functions generated from ifunction.m4.
  43. We check the array descriptor "a" against the extents precomputed
  44. from ifunction.m4, and complain about the argument a_name in the
  45. intrinsic function. */
  46. void
  47. bounds_ifunction_return (array_t * a, const index_type * extent,
  48. const char * a_name, const char * intrinsic)
  49. {
  50. int empty;
  51. int n;
  52. int rank;
  53. index_type a_size;
  54. rank = GFC_DESCRIPTOR_RANK (a);
  55. a_size = size0 (a);
  56. empty = 0;
  57. for (n = 0; n < rank; n++)
  58. {
  59. if (extent[n] == 0)
  60. empty = 1;
  61. }
  62. if (empty)
  63. {
  64. if (a_size != 0)
  65. runtime_error ("Incorrect size in %s of %s"
  66. " intrinsic: should be zero-sized",
  67. a_name, intrinsic);
  68. }
  69. else
  70. {
  71. if (a_size == 0)
  72. runtime_error ("Incorrect size of %s in %s"
  73. " intrinsic: should not be zero-sized",
  74. a_name, intrinsic);
  75. for (n = 0; n < rank; n++)
  76. {
  77. index_type a_extent;
  78. a_extent = GFC_DESCRIPTOR_EXTENT(a, n);
  79. if (a_extent != extent[n])
  80. runtime_error("Incorrect extent in %s of %s"
  81. " intrinsic in dimension %ld: is %ld,"
  82. " should be %ld", a_name, intrinsic, (long int) n + 1,
  83. (long int) a_extent, (long int) extent[n]);
  84. }
  85. }
  86. }
  87. /* Check that two arrays have equal extents, or are both zero-sized. Abort
  88. with a runtime error if this is not the case. Complain that a has the
  89. wrong size. */
  90. void
  91. bounds_equal_extents (array_t *a, array_t *b, const char *a_name,
  92. const char *intrinsic)
  93. {
  94. index_type a_size, b_size, n;
  95. assert (GFC_DESCRIPTOR_RANK(a) == GFC_DESCRIPTOR_RANK(b));
  96. a_size = size0 (a);
  97. b_size = size0 (b);
  98. if (b_size == 0)
  99. {
  100. if (a_size != 0)
  101. runtime_error ("Incorrect size of %s in %s"
  102. " intrinsic: should be zero-sized",
  103. a_name, intrinsic);
  104. }
  105. else
  106. {
  107. if (a_size == 0)
  108. runtime_error ("Incorrect size of %s of %s"
  109. " intrinsic: Should not be zero-sized",
  110. a_name, intrinsic);
  111. for (n = 0; n < GFC_DESCRIPTOR_RANK (b); n++)
  112. {
  113. index_type a_extent, b_extent;
  114. a_extent = GFC_DESCRIPTOR_EXTENT(a, n);
  115. b_extent = GFC_DESCRIPTOR_EXTENT(b, n);
  116. if (a_extent != b_extent)
  117. runtime_error("Incorrect extent in %s of %s"
  118. " intrinsic in dimension %ld: is %ld,"
  119. " should be %ld", a_name, intrinsic, (long int) n + 1,
  120. (long int) a_extent, (long int) b_extent);
  121. }
  122. }
  123. }
  124. /* Check that the extents of a and b agree, except that a has a missing
  125. dimension in argument which. Complain about a if anything is wrong. */
  126. void
  127. bounds_reduced_extents (array_t *a, array_t *b, int which, const char *a_name,
  128. const char *intrinsic)
  129. {
  130. index_type i, n, a_size, b_size;
  131. assert (GFC_DESCRIPTOR_RANK(a) == GFC_DESCRIPTOR_RANK(b) - 1);
  132. a_size = size0 (a);
  133. b_size = size0 (b);
  134. if (b_size == 0)
  135. {
  136. if (a_size != 0)
  137. runtime_error ("Incorrect size in %s of %s"
  138. " intrinsic: should not be zero-sized",
  139. a_name, intrinsic);
  140. }
  141. else
  142. {
  143. if (a_size == 0)
  144. runtime_error ("Incorrect size of %s of %s"
  145. " intrinsic: should be zero-sized",
  146. a_name, intrinsic);
  147. i = 0;
  148. for (n = 0; n < GFC_DESCRIPTOR_RANK (b); n++)
  149. {
  150. index_type a_extent, b_extent;
  151. if (n != which)
  152. {
  153. a_extent = GFC_DESCRIPTOR_EXTENT(a, i);
  154. b_extent = GFC_DESCRIPTOR_EXTENT(b, n);
  155. if (a_extent != b_extent)
  156. runtime_error("Incorrect extent in %s of %s"
  157. " intrinsic in dimension %ld: is %ld,"
  158. " should be %ld", a_name, intrinsic, (long int) i + 1,
  159. (long int) a_extent, (long int) b_extent);
  160. i++;
  161. }
  162. }
  163. }
  164. }
  165. /* count_0 - count all the true elements in an array. The front
  166. end usually inlines this, we need this for bounds checking
  167. for unpack. */
  168. index_type count_0 (const gfc_array_l1 * array)
  169. {
  170. const GFC_LOGICAL_1 * restrict base;
  171. index_type rank;
  172. int kind;
  173. int continue_loop;
  174. index_type count[GFC_MAX_DIMENSIONS];
  175. index_type extent[GFC_MAX_DIMENSIONS];
  176. index_type sstride[GFC_MAX_DIMENSIONS];
  177. index_type result;
  178. index_type n;
  179. rank = GFC_DESCRIPTOR_RANK (array);
  180. kind = GFC_DESCRIPTOR_SIZE (array);
  181. base = array->base_addr;
  182. if (kind == 1 || kind == 2 || kind == 4 || kind == 8
  183. #ifdef HAVE_GFC_LOGICAL_16
  184. || kind == 16
  185. #endif
  186. )
  187. {
  188. if (base)
  189. base = GFOR_POINTER_TO_L1 (base, kind);
  190. }
  191. else
  192. internal_error (NULL, "Funny sized logical array in count_0");
  193. for (n = 0; n < rank; n++)
  194. {
  195. sstride[n] = GFC_DESCRIPTOR_STRIDE_BYTES(array,n);
  196. extent[n] = GFC_DESCRIPTOR_EXTENT(array,n);
  197. count[n] = 0;
  198. if (extent[n] <= 0)
  199. return 0;
  200. }
  201. result = 0;
  202. continue_loop = 1;
  203. while (continue_loop)
  204. {
  205. if (*base)
  206. result ++;
  207. count[0]++;
  208. base += sstride[0];
  209. n = 0;
  210. while (count[n] == extent[n])
  211. {
  212. count[n] = 0;
  213. base -= sstride[n] * extent[n];
  214. n++;
  215. if (n == rank)
  216. {
  217. continue_loop = 0;
  218. break;
  219. }
  220. else
  221. {
  222. count[n]++;
  223. base += sstride[n];
  224. }
  225. }
  226. }
  227. return result;
  228. }