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