2 #include <isl/val_gmp.h>
4 #include <isl_set_polylib.h>
7 static __isl_give isl_mat
*extract_equalities(isl_ctx
*ctx
, Matrix
*M
)
15 for (i
= 0; i
< M
->NbRows
; ++i
)
16 if (value_zero_p(M
->p
[i
][0]))
19 eq
= isl_mat_alloc(ctx
, n
, M
->NbColumns
- 1);
20 for (i
= 0; i
< M
->NbRows
; ++i
) {
21 if (!value_zero_p(M
->p
[i
][0]))
23 for (j
= 0; j
< M
->NbColumns
- 1; ++j
) {
24 v
= isl_val_int_from_gmp(ctx
, M
->p
[i
][1 + j
]);
25 eq
= isl_mat_set_element_val(eq
, i
, j
, v
);
32 static __isl_give isl_mat
*extract_inequalities(isl_ctx
*ctx
, Matrix
*M
)
40 for (i
= 0; i
< M
->NbRows
; ++i
)
41 if (!value_zero_p(M
->p
[i
][0]))
44 ineq
= isl_mat_alloc(ctx
, n
, M
->NbColumns
- 1);
45 for (i
= 0; i
< M
->NbRows
; ++i
) {
46 if (value_zero_p(M
->p
[i
][0]))
48 for (j
= 0; j
< M
->NbColumns
- 1; ++j
) {
49 v
= isl_val_int_from_gmp(ctx
, M
->p
[i
][1 + j
]);
50 ineq
= isl_mat_set_element_val(ineq
, i
, j
, v
);
57 enum order_sign
isl_polyhedron_affine_sign(Polyhedron
*D
, Matrix
*T
,
58 struct barvinok_options
*options
)
61 isl_ctx
*ctx
= isl_ctx_alloc();
66 isl_val
*min
, *max
= NULL
;
68 enum order_sign sign
= order_undefined
;
70 assert(D
->Dimension
== T
->NbColumns
- 1);
72 dim
= isl_space_set_alloc(ctx
, 0, D
->Dimension
);
73 ls
= isl_local_space_from_space(isl_space_copy(dim
));
74 bset
= isl_basic_set_new_from_polylib(D
, dim
);
75 aff
= isl_aff_zero_on_domain(ls
);
76 for (i
= 0; i
< D
->Dimension
; ++i
) {
77 v
= isl_val_int_from_gmp(ctx
, T
->p
[0][i
]);
78 aff
= isl_aff_set_coefficient_val(aff
, isl_dim_in
, i
, v
);
80 v
= isl_val_int_from_gmp(ctx
, T
->p
[0][D
->Dimension
]);
81 aff
= isl_aff_set_constant_val(aff
, v
);
82 v
= isl_val_int_from_gmp(ctx
, T
->p
[1][D
->Dimension
]);
83 aff
= isl_aff_scale_down_val(aff
, v
);
85 min
= isl_basic_set_min_lp_val(bset
, aff
);
86 min
= isl_val_ceil(min
);
89 if (isl_val_is_nan(min
))
90 sign
= order_undefined
;
91 else if (isl_val_is_pos(min
))
94 max
= isl_basic_set_max_lp_val(bset
, aff
);
95 max
= isl_val_floor(max
);
98 if (isl_val_is_neg(max
))
100 else if (isl_val_is_zero(min
) && isl_val_is_zero(max
))
102 else if (isl_val_is_zero(min
))
104 else if (isl_val_is_zero(max
))
107 sign
= order_unknown
;
110 isl_basic_set_free(bset
);
119 static enum lp_result
isl_lp_result2lp_result(enum isl_lp_result res
)
124 case isl_lp_unbounded
:
133 enum lp_result
isl_constraints_opt(Matrix
*C
, Value
*obj
, Value denom
,
134 enum lp_dir dir
, Value
*opt
)
137 isl_ctx
*ctx
= isl_ctx_alloc();
144 enum isl_lp_result res
;
145 int max
= dir
== lp_max
;
147 eq
= extract_equalities(ctx
, C
);
148 ineq
= extract_inequalities(ctx
, C
);
149 dim
= isl_space_set_alloc(ctx
, 0, C
->NbColumns
- 2);
150 ls
= isl_local_space_from_space(isl_space_copy(dim
));
151 bset
= isl_basic_set_from_constraint_matrices(dim
, eq
, ineq
,
152 isl_dim_set
, isl_dim_div
, isl_dim_param
, isl_dim_cst
);
153 aff
= isl_aff_zero_on_domain(ls
);
154 for (i
= 0; i
< C
->NbColumns
- 2; ++i
) {
155 v
= isl_val_int_from_gmp(ctx
, obj
[i
]);
156 aff
= isl_aff_set_coefficient_val(aff
, isl_dim_in
, i
, v
);
158 v
= isl_val_int_from_gmp(ctx
, obj
[C
->NbColumns
- 2]);
159 aff
= isl_aff_set_constant_val(aff
, v
);
160 v
= isl_val_int_from_gmp(ctx
, denom
);
161 aff
= isl_aff_scale_down_val(aff
, v
);
164 v
= isl_val_floor(isl_basic_set_max_lp_val(bset
, aff
));
166 v
= isl_val_ceil(isl_basic_set_min_lp_val(bset
, aff
));
169 else if (isl_val_is_nan(v
))
171 else if (!isl_val_is_rat(v
))
172 res
= isl_lp_unbounded
;
175 isl_val_get_num_gmp(v
, *opt
);
180 isl_basic_set_free(bset
);
183 return isl_lp_result2lp_result(res
);