4 #include "isl_set_polylib.h"
5 #include "isl_map_polylib.h"
6 #include "isl_map_private.h"
8 static void copy_values_from(isl_int
*dst
, Value
*src
, unsigned n
)
12 for (i
= 0; i
< n
; ++i
)
13 value_assign(dst
[i
], src
[i
]);
16 static void copy_values_to(Value
*dst
, isl_int
*src
, unsigned n
)
20 for (i
= 0; i
< n
; ++i
)
21 value_assign(dst
[i
], src
[i
]);
24 static void copy_constraint_from(isl_int
*dst
, Value
*src
,
25 unsigned nparam
, unsigned dim
, unsigned extra
)
27 copy_values_from(dst
, src
+1+dim
+extra
+nparam
, 1);
28 copy_values_from(dst
+1, src
+1+dim
+extra
, nparam
);
29 copy_values_from(dst
+1+nparam
, src
+1, dim
);
30 copy_values_from(dst
+1+nparam
+dim
, src
+1+dim
, extra
);
33 static void copy_constraint_to(Value
*dst
, isl_int
*src
,
34 unsigned nparam
, unsigned dim
, unsigned extra
)
36 copy_values_to(dst
+1+dim
+extra
+nparam
, src
, 1);
37 copy_values_to(dst
+1+dim
+extra
, src
+1, nparam
);
38 copy_values_to(dst
+1, src
+1+nparam
, dim
);
39 copy_values_to(dst
+1+dim
, src
+1+nparam
+dim
, extra
);
42 static int add_equality(struct isl_ctx
*ctx
, struct isl_basic_map
*bmap
,
48 int i
= isl_basic_map_alloc_equality(bmap
);
51 nparam
= isl_basic_map_n_param(bmap
);
52 n_in
= isl_basic_map_n_in(bmap
);
53 n_out
= isl_basic_map_n_out(bmap
);
54 copy_constraint_from(bmap
->eq
[i
], constraint
, nparam
,
55 n_in
+ n_out
, bmap
->extra
);
59 static int add_inequality(struct isl_ctx
*ctx
, struct isl_basic_map
*bmap
,
65 int i
= isl_basic_map_alloc_inequality(bmap
);
68 nparam
= isl_basic_map_n_param(bmap
);
69 n_in
= isl_basic_map_n_in(bmap
);
70 n_out
= isl_basic_map_n_out(bmap
);
71 copy_constraint_from(bmap
->ineq
[i
], constraint
, nparam
,
72 n_in
+ n_out
, bmap
->extra
);
76 static struct isl_basic_map
*copy_constraints(
77 struct isl_ctx
*ctx
, struct isl_basic_map
*bmap
,
81 unsigned total
= isl_basic_map_total_dim(bmap
);
83 for (i
= 0; i
< P
->NbConstraints
; ++i
) {
84 if (value_zero_p(P
->Constraint
[i
][0])) {
85 if (add_equality(ctx
, bmap
, P
->Constraint
[i
]))
88 if (add_inequality(ctx
, bmap
, P
->Constraint
[i
]))
92 for (i
= 0; i
< bmap
->extra
; ++i
) {
93 int j
= isl_basic_map_alloc_div(bmap
);
96 isl_seq_clr(bmap
->div
[j
], 1+1+total
);
100 isl_basic_map_free(bmap
);
104 struct isl_basic_set
*isl_basic_set_new_from_polylib(Polyhedron
*P
,
109 isl_assert(dim
->ctx
, dim
->n_in
== 0, return NULL
);
111 return (struct isl_basic_set
*)
112 isl_basic_map_new_from_polylib(P
, dim
);
115 struct isl_basic_map
*isl_basic_map_new_from_polylib(Polyhedron
*P
,
118 struct isl_basic_map
*bmap
;
124 isl_assert(dim
->ctx
, P
, goto error
);
125 isl_assert(dim
->ctx
, P
->Dimension
>= isl_dim_total(dim
), goto error
);
127 extra
= P
->Dimension
- isl_dim_total(dim
);
128 bmap
= isl_basic_map_alloc_dim(dim
, extra
,
129 P
->NbEq
, P
->NbConstraints
- P
->NbEq
);
133 bmap
= copy_constraints(dim
->ctx
, bmap
, P
);
134 bmap
= isl_basic_map_simplify(bmap
);
135 return isl_basic_map_finalize(bmap
);
141 struct isl_set
*isl_set_new_from_polylib(Polyhedron
*D
, struct isl_dim
*dim
)
143 struct isl_set
*set
= NULL
;
149 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
151 for (P
= D
; P
; P
= P
->next
)
154 set
= isl_set_alloc_dim(isl_dim_copy(dim
), n
, ISL_MAP_DISJOINT
);
158 for (P
= D
; P
; P
= P
->next
)
160 isl_basic_set_new_from_polylib(P
, isl_dim_copy(dim
)));
162 set
= isl_set_remove_empty_parts(set
);
169 struct isl_map
*isl_map_new_from_polylib(Polyhedron
*D
, struct isl_dim
*dim
)
171 struct isl_map
*map
= NULL
;
178 for (P
= D
; P
; P
= P
->next
)
181 map
= isl_map_alloc_dim(isl_dim_copy(dim
), n
, ISL_MAP_DISJOINT
);
185 for (P
= D
; P
; P
= P
->next
)
187 isl_basic_map_new_from_polylib(P
, isl_dim_copy(dim
)));
189 map
= isl_map_remove_empty_parts(map
);
196 Polyhedron
*isl_basic_map_to_polylib(struct isl_basic_map
*bmap
)
209 nparam
= isl_basic_map_n_param(bmap
);
210 n_in
= isl_basic_map_n_in(bmap
);
211 n_out
= isl_basic_map_n_out(bmap
);
212 M
= Matrix_Alloc(bmap
->n_eq
+ bmap
->n_ineq
,
213 1 + n_in
+ n_out
+ bmap
->n_div
+ nparam
+ 1);
214 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
215 value_set_si(M
->p
[i
][0], 0);
216 copy_constraint_to(M
->p
[i
], bmap
->eq
[i
],
217 nparam
, n_in
+ n_out
, bmap
->n_div
);
220 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
221 value_set_si(M
->p
[off
+i
][0], 1);
222 copy_constraint_to(M
->p
[off
+i
], bmap
->ineq
[i
],
223 nparam
, n_in
+ n_out
, bmap
->n_div
);
225 P
= Constraints2Polyhedron(M
, bmap
->ctx
->MaxRays
);
231 Polyhedron
*isl_map_to_polylib(struct isl_map
*map
)
234 Polyhedron
*R
= NULL
;
235 Polyhedron
**next
= &R
;
240 for (i
= 0; i
< map
->n
; ++i
) {
241 *next
= isl_basic_map_to_polylib(map
->p
[i
]);
242 next
= &(*next
)->next
;
245 return R
? R
: Empty_Polyhedron(isl_dim_total(map
->dim
));
248 Polyhedron
*isl_basic_set_to_polylib(struct isl_basic_set
*bset
)
250 return isl_basic_map_to_polylib((struct isl_basic_map
*)bset
);
253 Polyhedron
*isl_set_to_polylib(struct isl_set
*set
)
255 return isl_map_to_polylib((struct isl_map
*)set
);