isl_tab_from_recession_cone: take basic set instead of basic map as argument
[platform/upstream/isl.git] / basis_reduction_tab.c
1 #include <assert.h>
2 #include "isl_seq.h"
3 #include "isl_tab.h"
4
5 struct tab_lp {
6         struct isl_ctx  *ctx;
7         struct isl_vec  *row;
8         struct isl_tab  *tab;
9         struct isl_tab_undo     **stack;
10         isl_int         *obj;
11         isl_int          opt;
12         isl_int          opt_denom;
13         int              neq;
14         unsigned         dim;
15         int              n_ineq;
16 };
17
18 static struct tab_lp *init_lp(struct isl_basic_set *bset);
19 static void set_lp_obj(struct tab_lp *lp, isl_int *row, int dim);
20 static int solve_lp(struct tab_lp *lp);
21 static void get_obj_val(struct tab_lp* lp, mpq_t *F);
22 static void delete_lp(struct tab_lp *lp);
23 static int add_lp_row(struct tab_lp *lp, isl_int *row, int dim);
24 static void get_alpha(struct tab_lp* lp, int row, mpq_t *alpha);
25 static void del_lp_row(struct tab_lp *lp);
26
27 #define GBR_LP                              struct tab_lp
28 #define GBR_type                            mpq_t
29 #define GBR_init(v)                         mpq_init(v)
30 #define GBR_clear(v)                        mpq_clear(v)
31 #define GBR_set(a,b)                        mpq_set(a,b)
32 #define GBR_set_ui(a,b)                     mpq_set_ui(a,b,1)
33 #define GBR_mul(a,b,c)                      mpq_mul(a,b,c)
34 #define GBR_lt(a,b)                         (mpq_cmp(a,b) < 0)
35 #define GBR_floor(a,b)                      mpz_fdiv_q(a,mpq_numref(b),mpq_denref(b))
36 #define GBR_ceil(a,b)                       mpz_cdiv_q(a,mpq_numref(b),mpq_denref(b))
37 #define GBR_lp_init(P)                      init_lp(P)
38 #define GBR_lp_set_obj(lp, obj, dim)        set_lp_obj(lp, obj, dim)
39 #define GBR_lp_solve(lp)                    solve_lp(lp)
40 #define GBR_lp_get_obj_val(lp, F)           get_obj_val(lp, F)
41 #define GBR_lp_delete(lp)                   delete_lp(lp)
42 #define GBR_lp_next_row(lp)                 lp->neq
43 #define GBR_lp_add_row(lp, row, dim)        add_lp_row(lp, row, dim)
44 #define GBR_lp_get_alpha(lp, row, alpha)    get_alpha(lp, row, alpha)
45 #define GBR_lp_del_row(lp)                  del_lp_row(lp);
46 #include "basis_reduction_templ.c"
47
48 /* Set up a tableau for the Cartesian product of bset with itself.
49  * This could be optimized by first setting up a tableau for bset
50  * and then performing the Cartesian product on the tableau.
51  */
52 static struct isl_tab *gbr_tab(struct isl_basic_set *bset,
53         struct isl_vec *row)
54 {
55         int i, j;
56         unsigned dim;
57         struct isl_tab *tab;
58
59         if (!bset || !row)
60                 return NULL;
61
62         dim = isl_basic_set_total_dim(bset);
63         tab = isl_tab_alloc(bset->ctx, 2 * bset->n_ineq + dim + 1, 2 * dim, 0);
64
65         for (i = 0; i < 2; ++i) {
66                 isl_seq_clr(row->el + 1 + (1 - i) * dim, dim);
67                 for (j = 0; j < bset->n_ineq; ++j) {
68                         isl_int_set(row->el[0], bset->ineq[j][0]);
69                         isl_seq_cpy(row->el + 1 + i * dim,
70                                     bset->ineq[j] + 1, dim);
71                         tab = isl_tab_add_ineq(tab, row->el);
72                         if (!tab || tab->empty)
73                                 return tab;
74                 }
75         }
76
77         return tab;
78 }
79
80 static struct tab_lp *init_lp(struct isl_basic_set *bset)
81 {
82         struct tab_lp *lp = NULL;
83
84         if (!bset)
85                 return NULL;
86
87         isl_assert(bset->ctx, bset->n_eq == 0, return NULL);
88
89         lp = isl_calloc_type(bset->ctx, struct tab_lp);
90         if (!lp)
91                 return NULL;
92
93         isl_int_init(lp->opt);
94         isl_int_init(lp->opt_denom);
95
96         lp->dim = isl_basic_set_total_dim(bset);
97         lp->n_ineq = bset->n_ineq;
98
99         lp->ctx = bset->ctx;
100         isl_ctx_ref(lp->ctx);
101
102         lp->stack = isl_alloc_array(lp->ctx, struct isl_tab_undo *, lp->dim);
103
104         lp->row = isl_vec_alloc(lp->ctx, 1 + 2 * lp->dim);
105         if (!lp->row)
106                 goto error;
107         lp->tab = gbr_tab(bset, lp->row);
108         if (!lp->tab)
109                 goto error;
110         lp->obj = NULL;
111         lp->neq = 0;
112
113         return lp;
114 error:
115         delete_lp(lp);
116         return NULL;
117 }
118
119 static void set_lp_obj(struct tab_lp *lp, isl_int *row, int dim)
120 {
121         lp->obj = row;
122 }
123
124 static int solve_lp(struct tab_lp *lp)
125 {
126         enum isl_lp_result res;
127         unsigned flags = 0;
128
129         isl_int_set_si(lp->row->el[0], 0);
130         isl_seq_cpy(lp->row->el + 1, lp->obj, lp->dim);
131         isl_seq_neg(lp->row->el + 1 + lp->dim, lp->obj, lp->dim);
132         if (lp->neq)
133                 flags = ISL_TAB_SAVE_DUAL;
134         res = isl_tab_min(lp->tab, lp->row->el, lp->ctx->one,
135                           &lp->opt, &lp->opt_denom, flags);
136         if (res != isl_lp_ok)
137                 return -1;
138         return 0;
139 }
140
141 static void get_obj_val(struct tab_lp* lp, mpq_t *F)
142 {
143         isl_int_neg(mpq_numref(*F), lp->opt);
144         isl_int_set(mpq_denref(*F), lp->opt_denom);
145 }
146
147 static void delete_lp(struct tab_lp *lp)
148 {
149         if (!lp)
150                 return;
151
152         isl_int_clear(lp->opt);
153         isl_int_clear(lp->opt_denom);
154         isl_vec_free(lp->row);
155         free(lp->stack);
156         isl_tab_free(lp->tab);
157         isl_ctx_deref(lp->ctx);
158         free(lp);
159 }
160
161 static int add_lp_row(struct tab_lp *lp, isl_int *row, int dim)
162 {
163         lp->stack[lp->neq] = isl_tab_snap(lp->tab);
164
165         isl_int_set_si(lp->row->el[0], 0);
166         isl_seq_cpy(lp->row->el + 1, row, lp->dim);
167         isl_seq_neg(lp->row->el + 1 + lp->dim, row, lp->dim);
168
169         lp->tab = isl_tab_add_valid_eq(lp->tab, lp->row->el);
170
171         return lp->neq++;
172 }
173
174 static void get_alpha(struct tab_lp* lp, int row, mpq_t *alpha)
175 {
176         row += 2 * lp->n_ineq;
177         isl_int_neg(mpq_numref(*alpha), lp->tab->dual->el[1 + row]);
178         isl_int_set(mpq_denref(*alpha), lp->tab->dual->el[0]);
179 }
180
181 static void del_lp_row(struct tab_lp *lp)
182 {
183         lp->neq--;
184         isl_tab_rollback(lp->tab, lp->stack[lp->neq]);
185 }