8 struct isl_tab_undo
**stack
;
17 static struct tab_lp
*init_lp(struct isl_basic_set
*bset
);
18 static void set_lp_obj(struct tab_lp
*lp
, isl_int
*row
, int dim
);
19 static int solve_lp(struct tab_lp
*lp
);
20 static void get_obj_val(struct tab_lp
* lp
, mpq_t
*F
);
21 static void delete_lp(struct tab_lp
*lp
);
22 static int add_lp_row(struct tab_lp
*lp
, isl_int
*row
, int dim
);
23 static void get_alpha(struct tab_lp
* lp
, int row
, mpq_t
*alpha
);
24 static void del_lp_row(struct tab_lp
*lp
);
26 #define GBR_LP struct tab_lp
27 #define GBR_type mpq_t
28 #define GBR_init(v) mpq_init(v)
29 #define GBR_clear(v) mpq_clear(v)
30 #define GBR_set(a,b) mpq_set(a,b)
31 #define GBR_set_ui(a,b) mpq_set_ui(a,b,1)
32 #define GBR_mul(a,b,c) mpq_mul(a,b,c)
33 #define GBR_lt(a,b) (mpq_cmp(a,b) < 0)
34 #define GBR_floor(a,b) mpz_fdiv_q(a,mpq_numref(b),mpq_denref(b))
35 #define GBR_ceil(a,b) mpz_cdiv_q(a,mpq_numref(b),mpq_denref(b))
36 #define GBR_lp_init(P) init_lp(P)
37 #define GBR_lp_set_obj(lp, obj, dim) set_lp_obj(lp, obj, dim)
38 #define GBR_lp_solve(lp) solve_lp(lp)
39 #define GBR_lp_get_obj_val(lp, F) get_obj_val(lp, F)
40 #define GBR_lp_delete(lp) delete_lp(lp)
41 #define GBR_lp_next_row(lp) lp->neq
42 #define GBR_lp_add_row(lp, row, dim) add_lp_row(lp, row, dim)
43 #define GBR_lp_get_alpha(lp, row, alpha) get_alpha(lp, row, alpha)
44 #define GBR_lp_del_row(lp) del_lp_row(lp);
45 #include "basis_reduction_templ.c"
47 /* Set up a tableau for the Cartesian product of bset with itself.
48 * This could be optimized by first setting up a tableau for bset
49 * and then performing the Cartesian product on the tableau.
51 static struct isl_tab
*gbr_tab(struct isl_basic_set
*bset
,
61 dim
= isl_basic_set_total_dim(bset
);
62 tab
= isl_tab_alloc(bset
->ctx
, 2 * bset
->n_ineq
+ dim
+ 1, 2 * dim
, 0);
64 for (i
= 0; i
< 2; ++i
) {
65 isl_seq_clr(row
->el
+ 1 + (1 - i
) * dim
, dim
);
66 for (j
= 0; j
< bset
->n_ineq
; ++j
) {
67 isl_int_set(row
->el
[0], bset
->ineq
[j
][0]);
68 isl_seq_cpy(row
->el
+ 1 + i
* dim
,
69 bset
->ineq
[j
] + 1, dim
);
70 tab
= isl_tab_add_ineq(tab
, row
->el
);
71 if (!tab
|| tab
->empty
)
79 static struct tab_lp
*init_lp(struct isl_basic_set
*bset
)
81 struct tab_lp
*lp
= NULL
;
86 isl_assert(bset
->ctx
, bset
->n_eq
== 0, return NULL
);
88 lp
= isl_calloc_type(bset
->ctx
, struct tab_lp
);
92 isl_int_init(lp
->opt
);
93 isl_int_init(lp
->opt_denom
);
95 lp
->dim
= isl_basic_set_total_dim(bset
);
96 lp
->n_ineq
= bset
->n_ineq
;
101 lp
->stack
= isl_alloc_array(lp
->ctx
, struct isl_tab_undo
*, lp
->dim
);
103 lp
->row
= isl_vec_alloc(lp
->ctx
, 1 + 2 * lp
->dim
);
106 lp
->tab
= gbr_tab(bset
, lp
->row
);
118 static void set_lp_obj(struct tab_lp
*lp
, isl_int
*row
, int dim
)
123 static int solve_lp(struct tab_lp
*lp
)
125 enum isl_lp_result res
;
128 isl_int_set_si(lp
->row
->el
[0], 0);
129 isl_seq_cpy(lp
->row
->el
+ 1, lp
->obj
, lp
->dim
);
130 isl_seq_neg(lp
->row
->el
+ 1 + lp
->dim
, lp
->obj
, lp
->dim
);
132 flags
= ISL_TAB_SAVE_DUAL
;
133 res
= isl_tab_min(lp
->tab
, lp
->row
->el
, lp
->ctx
->one
,
134 &lp
->opt
, &lp
->opt_denom
, flags
);
135 if (res
!= isl_lp_ok
)
140 static void get_obj_val(struct tab_lp
* lp
, mpq_t
*F
)
142 isl_int_neg(mpq_numref(*F
), lp
->opt
);
143 isl_int_set(mpq_denref(*F
), lp
->opt_denom
);
146 static void delete_lp(struct tab_lp
*lp
)
151 isl_int_clear(lp
->opt
);
152 isl_int_clear(lp
->opt_denom
);
153 isl_vec_free(lp
->row
);
155 isl_tab_free(lp
->tab
);
156 isl_ctx_deref(lp
->ctx
);
160 static int add_lp_row(struct tab_lp
*lp
, isl_int
*row
, int dim
)
162 lp
->stack
[lp
->neq
] = isl_tab_snap(lp
->tab
);
164 isl_int_set_si(lp
->row
->el
[0], 0);
165 isl_seq_cpy(lp
->row
->el
+ 1, row
, lp
->dim
);
166 isl_seq_neg(lp
->row
->el
+ 1 + lp
->dim
, row
, lp
->dim
);
168 lp
->tab
= isl_tab_add_valid_eq(lp
->tab
, lp
->row
->el
);
173 static void get_alpha(struct tab_lp
* lp
, int row
, mpq_t
*alpha
)
175 row
+= 2 * lp
->n_ineq
;
176 isl_int_neg(mpq_numref(*alpha
), lp
->tab
->dual
->el
[1 + row
]);
177 isl_int_set(mpq_denref(*alpha
), lp
->tab
->dual
->el
[0]);
180 static void del_lp_row(struct tab_lp
*lp
)
183 isl_tab_rollback(lp
->tab
, lp
->stack
[lp
->neq
]);