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