2 Free Software Foundation, Inc.
3 Contributed by Thomas Koenig
5 This file is part of the GNU Fortran runtime library (libgfortran).
7 Libgfortran is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 Libgfortran is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "libgfortran.h"
29 /* Auxiliary functions for bounds checking, mostly to reduce library size. */
31 /* Bounds checking for the return values of the iforeach functions (such
32 as maxloc and minloc). The extent of ret_array must
33 must match the rank of array. */
36 bounds_iforeach_return (array_t
*retarray
, array_t
*array
, const char *name
)
40 index_type ret_extent
;
42 ret_rank
= GFC_DESCRIPTOR_RANK (retarray
);
45 runtime_error ("Incorrect rank of return array in %s intrinsic:"
46 "is %ld, should be 1", name
, (long int) ret_rank
);
48 rank
= GFC_DESCRIPTOR_RANK (array
);
49 ret_extent
= GFC_DESCRIPTOR_EXTENT(retarray
,0);
50 if (ret_extent
!= rank
)
51 runtime_error ("Incorrect extent in return value of"
52 " %s intrinsic: is %ld, should be %ld",
53 name
, (long int) ret_extent
, (long int) rank
);
57 /* Check the return of functions generated from ifunction.m4.
58 We check the array descriptor "a" against the extents precomputed
59 from ifunction.m4, and complain about the argument a_name in the
60 intrinsic function. */
63 bounds_ifunction_return (array_t
* a
, const index_type
* extent
,
64 const char * a_name
, const char * intrinsic
)
71 rank
= GFC_DESCRIPTOR_RANK (a
);
75 for (n
= 0; n
< rank
; n
++)
83 runtime_error ("Incorrect size in %s of %s"
84 " intrinsic: should be zero-sized",
90 runtime_error ("Incorrect size of %s in %s"
91 " intrinsic: should not be zero-sized",
94 for (n
= 0; n
< rank
; n
++)
97 a_extent
= GFC_DESCRIPTOR_EXTENT(a
, n
);
98 if (a_extent
!= extent
[n
])
99 runtime_error("Incorrect extent in %s of %s"
100 " intrinsic in dimension %ld: is %ld,"
101 " should be %ld", a_name
, intrinsic
, (long int) n
+ 1,
102 (long int) a_extent
, (long int) extent
[n
]);
108 /* Check that two arrays have equal extents, or are both zero-sized. Abort
109 with a runtime error if this is not the case. Complain that a has the
113 bounds_equal_extents (array_t
*a
, array_t
*b
, const char *a_name
,
114 const char *intrinsic
)
116 index_type a_size
, b_size
, n
;
118 assert (GFC_DESCRIPTOR_RANK(a
) == GFC_DESCRIPTOR_RANK(b
));
126 runtime_error ("Incorrect size of %s in %s"
127 " intrinsic: should be zero-sized",
133 runtime_error ("Incorrect size of %s of %s"
134 " intrinsic: Should not be zero-sized",
137 for (n
= 0; n
< GFC_DESCRIPTOR_RANK (b
); n
++)
139 index_type a_extent
, b_extent
;
141 a_extent
= GFC_DESCRIPTOR_EXTENT(a
, n
);
142 b_extent
= GFC_DESCRIPTOR_EXTENT(b
, n
);
143 if (a_extent
!= b_extent
)
144 runtime_error("Incorrect extent in %s of %s"
145 " intrinsic in dimension %ld: is %ld,"
146 " should be %ld", a_name
, intrinsic
, (long int) n
+ 1,
147 (long int) a_extent
, (long int) b_extent
);
152 /* Check that the extents of a and b agree, except that a has a missing
153 dimension in argument which. Complain about a if anything is wrong. */
156 bounds_reduced_extents (array_t
*a
, array_t
*b
, int which
, const char *a_name
,
157 const char *intrinsic
)
160 index_type i
, n
, a_size
, b_size
;
162 assert (GFC_DESCRIPTOR_RANK(a
) == GFC_DESCRIPTOR_RANK(b
) - 1);
170 runtime_error ("Incorrect size in %s of %s"
171 " intrinsic: should not be zero-sized",
177 runtime_error ("Incorrect size of %s of %s"
178 " intrinsic: should be zero-sized",
182 for (n
= 0; n
< GFC_DESCRIPTOR_RANK (b
); n
++)
184 index_type a_extent
, b_extent
;
188 a_extent
= GFC_DESCRIPTOR_EXTENT(a
, i
);
189 b_extent
= GFC_DESCRIPTOR_EXTENT(b
, n
);
190 if (a_extent
!= b_extent
)
191 runtime_error("Incorrect extent in %s of %s"
192 " intrinsic in dimension %ld: is %ld,"
193 " should be %ld", a_name
, intrinsic
, (long int) i
+ 1,
194 (long int) a_extent
, (long int) b_extent
);
201 /* count_0 - count all the true elements in an array. The front
202 end usually inlines this, we need this for bounds checking
205 index_type
count_0 (const gfc_array_l1
* array
)
207 const GFC_LOGICAL_1
* restrict base
;
211 index_type count
[GFC_MAX_DIMENSIONS
];
212 index_type extent
[GFC_MAX_DIMENSIONS
];
213 index_type sstride
[GFC_MAX_DIMENSIONS
];
217 rank
= GFC_DESCRIPTOR_RANK (array
);
218 kind
= GFC_DESCRIPTOR_SIZE (array
);
222 if (kind
== 1 || kind
== 2 || kind
== 4 || kind
== 8
223 #ifdef HAVE_GFC_LOGICAL_16
229 base
= GFOR_POINTER_TO_L1 (base
, kind
);
232 internal_error (NULL
, "Funny sized logical array in count_0");
234 for (n
= 0; n
< rank
; n
++)
236 sstride
[n
] = GFC_DESCRIPTOR_STRIDE_BYTES(array
,n
);
237 extent
[n
] = GFC_DESCRIPTOR_EXTENT(array
,n
);
246 while (continue_loop
)
254 while (count
[n
] == extent
[n
])
257 base
-= sstride
[n
] * extent
[n
];