2 #include <isl/stream.h>
3 #include <isl_map_private.h>
4 #include <isl_polynomial_private.h>
6 #include <isl/options.h>
7 #include <isl/deprecated/point_int.h>
8 #include <isl/deprecated/polynomial_int.h>
10 struct bound_options
{
11 struct isl_options
*isl
;
14 int continue_on_error
;
17 ISL_ARGS_START(struct bound_options
, bound_options_args
)
18 ISL_ARG_CHILD(struct bound_options
, isl
, "isl", &isl_options_args
,
20 ISL_ARG_BOOL(struct bound_options
, verify
, 'T', "verify", 0, NULL
)
21 ISL_ARG_BOOL(struct bound_options
, print_all
, 'A', "print-all", 0, NULL
)
22 ISL_ARG_BOOL(struct bound_options
, continue_on_error
, '\0', "continue-on-error", 0, NULL
)
25 ISL_ARG_DEF(bound_options
, struct bound_options
, bound_options_args
)
27 static __isl_give isl_set
*set_bounds(__isl_take isl_set
*set
)
34 nparam
= isl_set_dim(set
, isl_dim_param
);
35 r
= nparam
>= 8 ? 5 : nparam
>= 5 ? 15 : 50;
37 pt
= isl_set_sample_point(isl_set_copy(set
));
38 pt2
= isl_point_copy(pt
);
40 for (i
= 0; i
< nparam
; ++i
) {
41 pt
= isl_point_add_ui(pt
, isl_dim_param
, i
, r
);
42 pt2
= isl_point_sub_ui(pt2
, isl_dim_param
, i
, r
);
45 box
= isl_set_box_from_points(pt
, pt2
);
47 return isl_set_intersect(set
, box
);
50 struct verify_point_bound
{
51 struct bound_options
*options
;
57 isl_pw_qpolynomial_fold
*pwf
;
58 isl_pw_qpolynomial_fold
*bound
;
61 static isl_stat
verify_point(__isl_take isl_point
*pnt
, void *user
)
66 struct verify_point_bound
*vpb
= (struct verify_point_bound
*) user
;
69 isl_pw_qpolynomial_fold
*pwf
;
70 isl_val
*bound
= NULL
;
78 FILE *out
= vpb
->options
->print_all
? stdout
: stderr
;
90 ctx
= isl_point_get_ctx(pnt
);
91 p
= isl_printer_to_file(ctx
, out
);
95 pwf
= isl_pw_qpolynomial_fold_copy(vpb
->pwf
);
97 nparam
= isl_pw_qpolynomial_fold_dim(pwf
, isl_dim_param
);
98 for (i
= 0; i
< nparam
; ++i
) {
99 isl_point_get_coordinate(pnt
, isl_dim_param
, i
, &t
);
100 pwf
= isl_pw_qpolynomial_fold_fix_dim(pwf
, isl_dim_param
, i
, t
);
103 bound
= isl_pw_qpolynomial_fold_eval(
104 isl_pw_qpolynomial_fold_copy(vpb
->bound
),
105 isl_point_copy(pnt
));
107 dom
= isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf
));
108 bounded
= isl_set_is_bounded(dom
);
114 opt
= isl_pw_qpolynomial_fold_eval(
115 isl_pw_qpolynomial_fold_copy(pwf
),
116 isl_set_sample_point(isl_set_copy(dom
)));
118 opt
= isl_pw_qpolynomial_fold_max(isl_pw_qpolynomial_fold_copy(pwf
));
120 opt
= isl_pw_qpolynomial_fold_min(isl_pw_qpolynomial_fold_copy(pwf
));
122 nvar
= isl_set_dim(dom
, isl_dim_set
);
123 if (vpb
->exact
&& bounded
)
124 ok
= isl_val_eq(opt
, bound
);
126 ok
= isl_val_le(opt
, bound
);
128 ok
= isl_val_le(bound
, opt
);
132 if (vpb
->options
->print_all
|| !ok
) {
133 p
= isl_printer_print_str(p
, minmax
);
134 p
= isl_printer_print_str(p
, "(");
135 for (i
= 0; i
< nparam
; ++i
) {
137 p
= isl_printer_print_str(p
, ", ");
138 isl_point_get_coordinate(pnt
, isl_dim_param
, i
, &t
);
139 p
= isl_printer_print_isl_int(p
, t
);
141 p
= isl_printer_print_str(p
, ") = ");
142 p
= isl_printer_print_val(p
, bound
);
143 p
= isl_printer_print_str(p
, ", ");
144 p
= isl_printer_print_str(p
, bounded
? "opt" : "sample");
145 p
= isl_printer_print_str(p
, " = ");
146 p
= isl_printer_print_val(p
, opt
);
148 p
= isl_printer_print_str(p
, ". OK");
150 p
= isl_printer_print_str(p
, ". NOT OK");
151 p
= isl_printer_end_line(p
);
152 } else if ((vpb
->n
% vpb
->stride
) == 0) {
153 p
= isl_printer_print_str(p
, "o");
154 p
= isl_printer_flush(p
);
162 isl_pw_qpolynomial_fold_free(pwf
);
175 if (vpb
->options
->continue_on_error
)
178 return (vpb
->n
>= 1 && ok
) ? isl_stat_ok
: isl_stat_error
;
181 static int check_solution(__isl_take isl_pw_qpolynomial_fold
*pwf
,
182 __isl_take isl_pw_qpolynomial_fold
*bound
, int exact
,
183 struct bound_options
*options
)
185 struct verify_point_bound vpb
;
191 dom
= isl_pw_qpolynomial_fold_domain(isl_pw_qpolynomial_fold_copy(pwf
));
192 context
= isl_set_params(isl_set_copy(dom
));
193 context
= isl_set_remove_divs(context
);
194 context
= set_bounds(context
);
199 isl_int_set_si(max
, 200);
200 r
= isl_set_count_upto(context
, max
, &count
);
202 n
= isl_int_get_si(count
);
205 isl_int_clear(count
);
207 vpb
.options
= options
;
211 vpb
.stride
= n
> 70 ? 1 + (n
+ 1)/70 : 1;
215 if (!options
->print_all
) {
216 for (i
= 0; i
< vpb
.n
; i
+= vpb
.stride
)
222 isl_set_foreach_point(context
, verify_point
, &vpb
);
224 isl_set_free(context
);
226 isl_pw_qpolynomial_fold_free(pwf
);
227 isl_pw_qpolynomial_fold_free(bound
);
229 if (!options
->print_all
)
233 fprintf(stderr
, "Check failed !\n");
240 int main(int argc
, char **argv
)
243 isl_pw_qpolynomial_fold
*copy
;
244 isl_pw_qpolynomial_fold
*pwf
;
247 struct bound_options
*options
;
251 options
= bound_options_new_with_defaults();
253 argc
= bound_options_parse(options
, argc
, argv
, ISL_ARG_ALL
);
255 ctx
= isl_ctx_alloc_with_options(&bound_options_args
, options
);
257 s
= isl_stream_new_file(ctx
, stdin
);
258 obj
= isl_stream_read_obj(s
);
259 if (obj
.type
== isl_obj_pw_qpolynomial
)
260 pwf
= isl_pw_qpolynomial_fold_from_pw_qpolynomial(isl_fold_max
,
262 else if (obj
.type
== isl_obj_pw_qpolynomial_fold
)
265 obj
.type
->free(obj
.v
);
266 isl_die(ctx
, isl_error_invalid
, "invalid input", goto error
);
270 copy
= isl_pw_qpolynomial_fold_copy(pwf
);
272 pwf
= isl_pw_qpolynomial_fold_bound(pwf
, &exact
);
273 pwf
= isl_pw_qpolynomial_fold_coalesce(pwf
);
275 if (options
->verify
) {
276 r
= check_solution(copy
, pwf
, exact
, options
);
279 printf("# NOT exact\n");
280 isl_pw_qpolynomial_fold_print(pwf
, stdout
, 0);
281 fprintf(stdout
, "\n");
282 isl_pw_qpolynomial_fold_free(pwf
);