isl_tab: introduce support for "big parameters"
[platform/upstream/isl.git] / basis_reduction_tab.c
1 #include <assert.h>
2 #include "isl_tab.h"
3
4 struct tab_lp {
5         struct isl_ctx  *ctx;
6         struct isl_vec  *row;
7         struct isl_tab  *tab;
8         struct isl_tab_undo     **stack;
9         isl_int         *obj;
10         isl_int          opt;
11         isl_int          opt_denom;
12         int              neq;
13         unsigned         dim;
14         int              n_ineq;
15 };
16
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);
25
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"
46
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.
50  */
51 static struct isl_tab *gbr_tab(struct isl_basic_set *bset,
52         struct isl_vec *row)
53 {
54         int i, j;
55         unsigned dim;
56         struct isl_tab *tab;
57
58         if (!bset || !row)
59                 return NULL;
60
61         dim = isl_basic_set_total_dim(bset);
62         tab = isl_tab_alloc(bset->ctx, 2 * bset->n_ineq + dim + 1, 2 * dim, 0);
63
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)
72                                 return tab;
73                 }
74         }
75
76         return tab;
77 }
78
79 static struct tab_lp *init_lp(struct isl_basic_set *bset)
80 {
81         struct tab_lp *lp = NULL;
82
83         if (!bset)
84                 return NULL;
85
86         isl_assert(bset->ctx, bset->n_eq == 0, return NULL);
87
88         lp = isl_calloc_type(bset->ctx, struct tab_lp);
89         if (!lp)
90                 return NULL;
91
92         isl_int_init(lp->opt);
93         isl_int_init(lp->opt_denom);
94
95         lp->dim = isl_basic_set_total_dim(bset);
96         lp->n_ineq = bset->n_ineq;
97
98         lp->ctx = bset->ctx;
99         isl_ctx_ref(lp->ctx);
100
101         lp->stack = isl_alloc_array(lp->ctx, struct isl_tab_undo *, lp->dim);
102
103         lp->row = isl_vec_alloc(lp->ctx, 1 + 2 * lp->dim);
104         if (!lp->row)
105                 goto error;
106         lp->tab = gbr_tab(bset, lp->row);
107         if (!lp->tab)
108                 goto error;
109         lp->obj = NULL;
110         lp->neq = 0;
111
112         return lp;
113 error:
114         delete_lp(lp);
115         return NULL;
116 }
117
118 static void set_lp_obj(struct tab_lp *lp, isl_int *row, int dim)
119 {
120         lp->obj = row;
121 }
122
123 static int solve_lp(struct tab_lp *lp)
124 {
125         enum isl_lp_result res;
126         unsigned flags = 0;
127
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);
131         if (lp->neq)
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)
136                 return -1;
137         return 0;
138 }
139
140 static void get_obj_val(struct tab_lp* lp, mpq_t *F)
141 {
142         isl_int_neg(mpq_numref(*F), lp->opt);
143         isl_int_set(mpq_denref(*F), lp->opt_denom);
144 }
145
146 static void delete_lp(struct tab_lp *lp)
147 {
148         if (!lp)
149                 return;
150
151         isl_int_clear(lp->opt);
152         isl_int_clear(lp->opt_denom);
153         isl_vec_free(lp->row);
154         free(lp->stack);
155         isl_tab_free(lp->tab);
156         isl_ctx_deref(lp->ctx);
157         free(lp);
158 }
159
160 static int add_lp_row(struct tab_lp *lp, isl_int *row, int dim)
161 {
162         lp->stack[lp->neq] = isl_tab_snap(lp->tab);
163
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);
167
168         lp->tab = isl_tab_add_valid_eq(lp->tab, lp->row->el);
169
170         return lp->neq++;
171 }
172
173 static void get_alpha(struct tab_lp* lp, int row, mpq_t *alpha)
174 {
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]);
178 }
179
180 static void del_lp_row(struct tab_lp *lp)
181 {
182         lp->neq--;
183         isl_tab_rollback(lp->tab, lp->stack[lp->neq]);
184 }