2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU GPLv2 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 #include <isl/val_gmp.h>
13 #include <isl/constraint.h>
14 #include "isl_set_polylib.h"
15 #include "isl_map_polylib.h"
17 static __isl_give isl_constraint
*copy_constraint_from(
18 __isl_take isl_constraint
*dst
, Value
*src
)
21 isl_ctx
*ctx
= isl_constraint_get_ctx(dst
);
23 enum isl_dim_type types
[] = { isl_dim_in
, isl_dim_out
, isl_dim_param
};
26 for (i
= 0; i
< 3; ++i
) {
27 int n
= isl_constraint_dim(dst
, types
[i
]);
28 for (j
= 0; j
< n
; ++j
, ++k
) {
29 v
= isl_val_int_from_gmp(ctx
, src
[k
]);
30 dst
= isl_constraint_set_coefficient_val(dst
, types
[i
],
35 v
= isl_val_int_from_gmp(ctx
, src
[k
]);
36 dst
= isl_constraint_set_constant_val(dst
, v
);
41 static __isl_give isl_basic_map
*add_equality(__isl_take isl_basic_map
*bmap
,
46 c
= isl_constraint_alloc_equality(isl_basic_map_get_local_space(bmap
));
48 c
= copy_constraint_from(c
, constraint
);
50 bmap
= isl_basic_map_add_constraint(bmap
, c
);
55 static __isl_give isl_basic_map
*add_inequality(__isl_take isl_basic_map
*bmap
,
61 ls
= isl_basic_map_get_local_space(bmap
);
62 c
= isl_constraint_alloc_inequality(ls
);
64 copy_constraint_from(c
, constraint
);
66 bmap
= isl_basic_map_add_constraint(bmap
, c
);
71 static __isl_give isl_basic_map
*copy_constraints(
72 __isl_take isl_basic_map
*bmap
, Polyhedron
*P
)
76 for (i
= 0; i
< P
->NbConstraints
; ++i
) {
77 if (value_zero_p(P
->Constraint
[i
][0]))
78 bmap
= add_equality(bmap
, P
->Constraint
[i
]);
80 bmap
= add_inequality(bmap
, P
->Constraint
[i
]);
86 struct isl_basic_set
*isl_basic_set_new_from_polylib(Polyhedron
*P
,
87 struct isl_space
*dim
)
93 ctx
= isl_space_get_ctx(dim
);
94 isl_assert(ctx
, isl_space_dim(dim
, isl_dim_in
) == 0, return NULL
);
96 return (struct isl_basic_set
*)
97 isl_basic_map_new_from_polylib(P
, dim
);
100 struct isl_basic_map
*isl_basic_map_new_from_polylib(Polyhedron
*P
,
101 struct isl_space
*dim
)
104 struct isl_basic_map
*bmap
;
111 ctx
= isl_space_get_ctx(dim
);
112 isl_assert(ctx
, P
, goto error
);
113 isl_assert(ctx
, P
->Dimension
>= isl_space_dim(dim
, isl_dim_all
),
116 n_out
= isl_space_dim(dim
, isl_dim_out
);
117 extra
= P
->Dimension
- isl_space_dim(dim
, isl_dim_all
);
118 dim
= isl_space_from_domain(isl_space_wrap(dim
));
119 dim
= isl_space_add_dims(dim
, isl_dim_out
, extra
);
120 bmap
= isl_basic_map_universe(dim
);
124 bmap
= copy_constraints(bmap
, P
);
125 bmap
= isl_basic_set_unwrap(isl_basic_map_domain(bmap
));
133 struct isl_set
*isl_set_new_from_polylib(Polyhedron
*D
, struct isl_space
*dim
)
136 struct isl_set
*set
= NULL
;
141 ctx
= isl_space_get_ctx(dim
);
142 isl_assert(ctx
, isl_space_dim(dim
, isl_dim_in
) == 0, return NULL
);
144 set
= isl_set_empty(isl_space_copy(dim
));
148 for (P
= D
; P
; P
= P
->next
)
149 set
= isl_set_union_disjoint(set
,
150 isl_set_from_basic_set(
151 isl_basic_set_new_from_polylib(P
, isl_space_copy(dim
))));
159 static isl_stat
count_constraints(__isl_take isl_constraint
*c
, void *user
)
161 int *n
= (int *)user
;
163 isl_constraint_free(c
);
167 struct isl_poly_copy
{
172 static isl_stat
copy_constraint_to(__isl_take isl_constraint
*c
, void *user
)
175 enum isl_dim_type types
[] = { isl_dim_in
, isl_dim_out
,
176 isl_dim_div
, isl_dim_param
};
177 struct isl_poly_copy
*data
= (struct isl_poly_copy
*)user
;
180 if (isl_constraint_is_equality(c
))
181 value_set_si(data
->M
->p
[data
->n
][0], 0);
183 value_set_si(data
->M
->p
[data
->n
][0], 1);
185 for (i
= 0; i
< 4; ++i
) {
186 int n
= isl_constraint_dim(c
, types
[i
]);
187 for (j
= 0; j
< n
; ++j
, ++k
) {
188 v
= isl_constraint_get_coefficient_val(c
, types
[i
], j
);
189 isl_val_get_num_gmp(v
, data
->M
->p
[data
->n
][k
]);
193 v
= isl_constraint_get_constant_val(c
);
194 isl_val_get_num_gmp(v
, data
->M
->p
[data
->n
][k
]);
196 isl_constraint_free(c
);
201 Polyhedron
*isl_basic_map_to_polylib(struct isl_basic_map
*bmap
)
211 struct isl_poly_copy data
;
216 if (isl_basic_map_is_rational(bmap
))
217 max_rays
= POL_NO_DUAL
;
219 max_rays
= POL_NO_DUAL
| POL_INTEGER
;
221 if (isl_basic_map_foreach_constraint(bmap
, &count_constraints
, &n
) < 0)
224 nparam
= isl_basic_map_n_param(bmap
);
225 n_in
= isl_basic_map_n_in(bmap
);
226 n_out
= isl_basic_map_n_out(bmap
);
227 n_div
= isl_basic_map_dim(bmap
, isl_dim_div
);
228 data
.M
= Matrix_Alloc(n
, 1 + n_in
+ n_out
+ n_div
+ nparam
+ 1);
230 if (isl_basic_map_foreach_constraint(bmap
,
231 ©_constraint_to
, &data
) < 0) {
235 P
= Constraints2Polyhedron(data
.M
, max_rays
);
241 static isl_stat
add_basic_map(__isl_take isl_basic_map
*bmap
, void *user
)
243 Polyhedron
***next
= user
;
245 **next
= isl_basic_map_to_polylib(bmap
);
246 *next
= &(**next
)->next
;
248 isl_basic_map_free(bmap
);
252 Polyhedron
*isl_map_to_polylib(struct isl_map
*map
)
255 Polyhedron
*R
= NULL
;
256 Polyhedron
**next
= &R
;
261 if (isl_map_foreach_basic_map(map
, &add_basic_map
, &next
) < 0)
264 return R
? R
: Empty_Polyhedron(isl_map_dim(map
, isl_dim_all
));
270 Polyhedron
*isl_basic_set_to_polylib(struct isl_basic_set
*bset
)
272 return isl_basic_map_to_polylib((struct isl_basic_map
*)bset
);
275 Polyhedron
*isl_set_to_polylib(struct isl_set
*set
)
277 return isl_map_to_polylib((struct isl_map
*)set
);