1 #include "isl_sample.h"
2 #include "isl_sample_piplib.h"
6 #include "isl_map_private.h"
7 #include "isl_equalities.h"
9 static struct isl_vec
*point_sample(struct isl_ctx
*ctx
,
10 struct isl_basic_set
*bset
)
12 struct isl_vec
*sample
;
13 isl_basic_set_free(bset
);
14 sample
= isl_vec_alloc(ctx
, 1);
17 isl_int_set_si(sample
->block
.data
[0], 1);
21 static struct isl_vec
*interval_sample(struct isl_ctx
*ctx
,
22 struct isl_basic_set
*bset
)
24 struct isl_vec
*sample
;
26 bset
= isl_basic_set_simplify(bset
);
30 return isl_basic_set_sample(bset
);
31 sample
= isl_vec_alloc(ctx
, 2);
32 isl_int_set_si(sample
->block
.data
[0], 1);
33 if (bset
->n_ineq
== 0)
34 isl_int_set_si(sample
->block
.data
[1], 0);
39 if (isl_int_is_one(bset
->ineq
[0][1]))
40 isl_int_neg(sample
->block
.data
[1], bset
->ineq
[0][0]);
42 isl_int_set(sample
->block
.data
[1], bset
->ineq
[0][0]);
43 for (i
= 1; i
< bset
->n_ineq
; ++i
) {
44 isl_seq_inner_product(sample
->block
.data
,
45 bset
->ineq
[i
], 2, &t
);
46 if (isl_int_is_neg(t
))
50 if (i
< bset
->n_ineq
) {
51 isl_vec_free(ctx
, sample
);
52 sample
= isl_vec_alloc(ctx
, 0);
55 isl_basic_set_free(bset
);
59 static struct isl_mat
*independent_bounds(struct isl_ctx
*ctx
,
60 struct isl_basic_set
*bset
)
63 struct isl_mat
*dirs
= NULL
;
69 dim
= isl_basic_set_n_dim(bset
);
70 if (bset
->n_ineq
== 0)
71 return isl_mat_alloc(ctx
, 0, dim
);
73 dirs
= isl_mat_alloc(ctx
, dim
, dim
);
76 isl_seq_cpy(dirs
->row
[0], bset
->ineq
[0]+1, dirs
->n_col
);
77 for (j
= 1, n
= 1; n
< dim
&& j
< bset
->n_ineq
; ++j
) {
80 isl_seq_cpy(dirs
->row
[n
], bset
->ineq
[j
]+1, dirs
->n_col
);
82 pos
= isl_seq_first_non_zero(dirs
->row
[n
], dirs
->n_col
);
85 for (i
= 0; i
< n
; ++i
) {
87 pos_i
= isl_seq_first_non_zero(dirs
->row
[i
], dirs
->n_col
);
92 isl_seq_elim(dirs
->row
[n
], dirs
->row
[i
], pos
,
94 pos
= isl_seq_first_non_zero(dirs
->row
[n
], dirs
->n_col
);
102 isl_int
*t
= dirs
->row
[n
];
103 for (k
= n
; k
> i
; --k
)
104 dirs
->row
[k
] = dirs
->row
[k
-1];
113 static struct isl_basic_set
*remove_lineality(struct isl_ctx
*ctx
,
114 struct isl_basic_set
*bset
, struct isl_mat
*bounds
, struct isl_mat
**T
)
116 struct isl_mat
*U
= NULL
;
117 unsigned old_dim
, new_dim
;
119 old_dim
= isl_basic_set_n_dim(bset
);
120 new_dim
= bounds
->n_row
;
122 bounds
= isl_mat_left_hermite(ctx
, bounds
, 0, &U
, NULL
);
125 U
= isl_mat_lin_to_aff(ctx
, U
);
126 U
= isl_mat_drop_cols(ctx
, U
, 1 + new_dim
, old_dim
- new_dim
);
127 bset
= isl_basic_set_preimage(bset
, isl_mat_copy(ctx
, U
));
131 isl_mat_free(ctx
, bounds
);
134 isl_mat_free(ctx
, bounds
);
135 isl_mat_free(ctx
, U
);
136 isl_basic_set_free(bset
);
140 struct isl_vec
*isl_basic_set_sample(struct isl_basic_set
*bset
)
143 struct isl_mat
*bounds
;
149 if (ISL_F_ISSET(bset
, ISL_BASIC_SET_EMPTY
)) {
150 isl_basic_set_free(bset
);
151 return isl_vec_alloc(ctx
, 0);
154 dim
= isl_basic_set_n_dim(bset
);
155 isl_assert(ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
156 isl_assert(ctx
, bset
->n_div
== 0, goto error
);
158 if (bset
->n_eq
> 0) {
160 struct isl_vec
*sample
;
162 bset
= isl_basic_set_remove_equalities(bset
, &T
, NULL
);
163 sample
= isl_basic_set_sample(bset
);
164 if (sample
&& sample
->size
!= 0)
165 sample
= isl_mat_vec_product(ctx
, T
, sample
);
167 isl_mat_free(ctx
, T
);
171 return point_sample(ctx
, bset
);
173 return interval_sample(ctx
, bset
);
174 bounds
= independent_bounds(ctx
, bset
);
177 if (bounds
->n_row
== dim
)
178 isl_mat_free(ctx
, bounds
);
181 struct isl_vec
*sample
;
183 bset
= remove_lineality(ctx
, bset
, bounds
, &T
);
184 sample
= isl_basic_set_sample(bset
);
185 if (sample
&& sample
->size
!= 0)
186 sample
= isl_mat_vec_product(ctx
, T
, sample
);
188 isl_mat_free(ctx
, T
);
191 return isl_pip_basic_set_sample(bset
);
193 isl_basic_set_free(bset
);