isl_map_polylib.c: add missing include
[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         return copy_constraints(ctx, bmap, P);
116 }
117
118 struct isl_set *isl_set_new_from_polylib(struct isl_ctx *ctx,
119                         Polyhedron *D, unsigned nparam, unsigned dim)
120 {
121         struct isl_set *set = NULL;
122         Polyhedron *P;
123         int n = 0;
124
125         for (P = D; P; P = P->next)
126                 ++n;
127
128         set = isl_set_alloc(ctx, nparam, dim, n, ISL_MAP_DISJOINT);
129         if (!set)
130                 return NULL;
131
132         for (P = D; P; P = P->next)
133                 isl_set_add(set,
134                     isl_basic_set_new_from_polylib(ctx, P, nparam, dim));
135         return set;
136 }
137
138 struct isl_map *isl_map_new_from_polylib(struct isl_ctx *ctx,
139                         Polyhedron *D,
140                         unsigned nparam, unsigned in, unsigned out)
141 {
142         struct isl_map *map = NULL;
143         Polyhedron *P;
144         int n = 0;
145
146         for (P = D; P; P = P->next)
147                 ++n;
148
149         map = isl_map_alloc(ctx, nparam, in, out, n, ISL_MAP_DISJOINT);
150         if (!map)
151                 return NULL;
152
153         for (P = D; P; P = P->next)
154                 isl_map_add(map, isl_basic_map_new_from_polylib(ctx, P,
155                                                             nparam, in, out));
156         return map;
157 }
158
159 Polyhedron *isl_basic_map_to_polylib(struct isl_basic_map *bmap)
160 {
161         int i;
162         Matrix *M;
163         Polyhedron *P;
164         unsigned off;
165
166         if (!bmap)
167                 return NULL;
168
169         M = Matrix_Alloc(bmap->n_eq + bmap->n_ineq,
170                  1 + bmap->n_in + bmap->n_out + bmap->n_div + bmap->nparam + 1);
171         for (i = 0; i < bmap->n_eq; ++i) {
172                 value_set_si(M->p[i][0], 0);
173                 copy_constraint_to(M->p[i], bmap->eq[i],
174                            bmap->nparam, bmap->n_in + bmap->n_out, bmap->n_div);
175         }
176         off = bmap->n_eq;
177         for (i = 0; i < bmap->n_ineq; ++i) {
178                 value_set_si(M->p[off+i][0], 1);
179                 copy_constraint_to(M->p[off+i], bmap->ineq[i],
180                            bmap->nparam, bmap->n_in + bmap->n_out, bmap->n_div);
181         }
182         P = Constraints2Polyhedron(M, bmap->ctx->MaxRays);
183         Matrix_Free(M);
184
185         return P;
186 }
187
188 Polyhedron *isl_map_to_polylib(struct isl_map *map)
189 {
190         int i;
191         Polyhedron *R = NULL;
192         Polyhedron **next = &R;
193
194         if (!map)
195                 return NULL;
196
197         for (i = 0; i < map->n; ++i) {
198                 *next = isl_basic_map_to_polylib(map->p[i]);
199                 next = &(*next)->next;
200         }
201
202         return R;
203 }
204
205 Polyhedron *isl_basic_set_to_polylib(struct isl_basic_set *bset)
206 {
207         return isl_basic_map_to_polylib((struct isl_basic_map *)bset);
208 }
209
210 Polyhedron *isl_set_to_polylib(struct isl_set *set)
211 {
212         return isl_map_to_polylib((struct isl_map *)set);
213 }