isl_basic_{map,set}: explicitly store constraints defining div
[platform/upstream/isl.git] / isl_map_polylib.c
1 #include "isl_set.h"
2 #include "isl_map.h"
3 #include "isl_map_polylib.h"
4
5 static void copy_values_from(isl_int *dst, Value *src, unsigned n)
6 {
7         int i;
8
9         for (i = 0; i < n; ++i)
10                 value_assign(dst[i], src[i]);
11 }
12
13 static void copy_values_to(Value *dst, isl_int *src, unsigned n)
14 {
15         int i;
16
17         for (i = 0; i < n; ++i)
18                 value_assign(dst[i], src[i]);
19 }
20
21 static void copy_constraint_from(isl_int *dst, Value *src,
22                 unsigned nparam, unsigned dim, unsigned extra)
23 {
24         copy_values_from(dst, src+1+dim+extra+nparam, 1);
25         copy_values_from(dst+1, src+1+dim+extra, nparam);
26         copy_values_from(dst+1+nparam, src+1, dim);
27         copy_values_from(dst+1+nparam+dim, src+1+dim, extra);
28 }
29
30 static void copy_constraint_to(Value *dst, isl_int *src,
31                 unsigned nparam, unsigned dim, unsigned extra)
32 {
33         copy_values_to(dst+1+dim+extra+nparam, src, 1);
34         copy_values_to(dst+1+dim+extra, src+1, nparam);
35         copy_values_to(dst+1, src+1+nparam, dim);
36         copy_values_to(dst+1+dim, src+1+nparam+dim, extra);
37 }
38
39 static int add_equality(struct isl_ctx *ctx, struct isl_basic_map *bmap,
40                          Value *constraint)
41 {
42         int i = isl_basic_map_alloc_equality(ctx, bmap);
43         if (i < 0)
44                 return -1;
45         copy_constraint_from(bmap->eq[i], constraint, bmap->nparam,
46                              bmap->n_in + bmap->n_out, bmap->extra);
47         return 0;
48 }
49
50 static int add_inequality(struct isl_ctx *ctx, struct isl_basic_map *bmap,
51                          Value *constraint)
52 {
53         int i = isl_basic_map_alloc_inequality(ctx, bmap);
54         if (i < 0)
55                 return -1;
56         copy_constraint_from(bmap->ineq[i], constraint, bmap->nparam,
57                              bmap->n_in + bmap->n_out, bmap->extra);
58         return 0;
59 }
60
61 static struct isl_basic_map *copy_constraints(
62                         struct isl_ctx *ctx, struct isl_basic_map *bmap,
63                         Polyhedron *P)
64 {
65         int i;
66         unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra;
67
68         for (i = 0; i < P->NbConstraints; ++i) {
69                 if (value_zero_p(P->Constraint[i][0])) {
70                         if (add_equality(ctx, bmap, P->Constraint[i]))
71                                 goto error;
72                 } else {
73                         if (add_inequality(ctx, bmap, P->Constraint[i]))
74                                 goto error;
75                 }
76         }
77         for (i = 0; i < bmap->extra; ++i) {
78                 int j = isl_basic_map_alloc_div(ctx, bmap);
79                 if (j == -1)
80                         goto error;
81                 isl_seq_clr(bmap->div[j], 1+1+total);
82         }
83         return bmap;
84 error:
85         isl_basic_map_free(ctx, bmap);
86         return NULL;
87 }
88
89 struct isl_basic_set *isl_basic_set_new_from_polylib(
90                         struct isl_ctx *ctx,
91                         Polyhedron *P, unsigned nparam, unsigned dim)
92 {
93         return (struct isl_basic_set *)
94                 isl_basic_map_new_from_polylib(ctx, P, nparam, 0, dim);
95 }
96
97 struct isl_basic_map *isl_basic_map_new_from_polylib(
98                         struct isl_ctx *ctx, Polyhedron *P,
99                         unsigned nparam, unsigned in, unsigned out)
100 {
101         struct isl_basic_map *bmap;
102         unsigned extra;
103
104         isl_assert(ctx, P, return NULL);
105         isl_assert(ctx, P->Dimension >= nparam + in + out, return NULL);
106
107         extra = P->Dimension - nparam - in - out;
108         bmap = isl_basic_map_alloc(ctx, nparam, in, out, extra,
109                                         P->NbEq, P->NbConstraints - P->NbEq);
110         if (!bmap)
111                 return NULL;
112
113         return copy_constraints(ctx, bmap, P);
114 }
115
116 struct isl_set *isl_set_new_from_polylib(struct isl_ctx *ctx,
117                         Polyhedron *D, unsigned nparam, unsigned dim)
118 {
119         struct isl_set *set = NULL;
120         Polyhedron *P;
121         int n = 0;
122
123         for (P = D; P; P = P->next)
124                 ++n;
125
126         set = isl_set_alloc(ctx, nparam, dim, n, ISL_MAP_DISJOINT);
127         if (!set)
128                 return NULL;
129
130         for (P = D; P; P = P->next)
131                 isl_set_add(ctx, set,
132                     isl_basic_set_new_from_polylib(ctx, P, nparam, dim));
133         return set;
134 }
135
136 struct isl_map *isl_map_new_from_polylib(struct isl_ctx *ctx,
137                         Polyhedron *D,
138                         unsigned nparam, unsigned in, unsigned out)
139 {
140         struct isl_map *map = NULL;
141         Polyhedron *P;
142         int n = 0;
143
144         for (P = D; P; P = P->next)
145                 ++n;
146
147         map = isl_map_alloc(ctx, nparam, in, out, n, ISL_MAP_DISJOINT);
148         if (!map)
149                 return NULL;
150
151         for (P = D; P; P = P->next)
152                 isl_map_add(ctx, map,
153                         isl_basic_map_new_from_polylib(ctx, P,
154                                                             nparam, in, out));
155         return map;
156 }
157
158 Polyhedron *isl_basic_map_to_polylib(struct isl_ctx *ctx,
159                                                 struct isl_basic_map *bmap)
160 {
161         int i;
162         Matrix *M;
163         Polyhedron *P;
164         unsigned off;
165
166         M = Matrix_Alloc(bmap->n_eq + bmap->n_ineq,
167                  1 + bmap->n_in + bmap->n_out + bmap->n_div + bmap->nparam + 1);
168         for (i = 0; i < bmap->n_eq; ++i) {
169                 value_set_si(M->p[i][0], 0);
170                 copy_constraint_to(M->p[i], bmap->eq[i],
171                            bmap->nparam, bmap->n_in + bmap->n_out, bmap->n_div);
172         }
173         off = bmap->n_eq;
174         for (i = 0; i < bmap->n_ineq; ++i) {
175                 value_set_si(M->p[off+i][0], 1);
176                 copy_constraint_to(M->p[off+i], bmap->ineq[i],
177                            bmap->nparam, bmap->n_in + bmap->n_out, bmap->n_div);
178         }
179         P = Constraints2Polyhedron(M, ctx->MaxRays);
180         Matrix_Free(M);
181
182         return P;
183 }
184
185 Polyhedron *isl_map_to_polylib(struct isl_ctx *ctx, struct isl_map *map)
186 {
187         int i;
188         Polyhedron *R = NULL;
189         Polyhedron **next = &R;
190
191         for (i = 0; i < map->n; ++i) {
192                 *next = isl_basic_map_to_polylib(ctx, map->p[i]);
193                 next = &(*next)->next;
194         }
195
196         return R;
197 }
198
199 Polyhedron *isl_basic_set_to_polylib(struct isl_ctx *ctx,
200                         struct isl_basic_set *bset)
201 {
202         return isl_basic_map_to_polylib(ctx,
203                         (struct isl_basic_map *)bset);
204 }
205
206 Polyhedron *isl_set_to_polylib(struct isl_ctx *ctx, struct isl_set *set)
207 {
208         return isl_map_to_polylib(ctx, (struct isl_map *)set);
209 }