1 #include <isl_point_private.h>
3 #include <isl_map_private.h>
4 #include <isl_sample.h>
7 #include <isl_dim_private.h>
9 __isl_give isl_point *isl_point_alloc(__isl_take isl_dim *dim,
10 __isl_take isl_vec *vec)
12 struct isl_point *pnt;
17 if (vec->size > 1 + isl_dim_total(dim)) {
18 vec = isl_vec_cow(vec);
21 vec->size = 1 + isl_dim_total(dim);
24 pnt = isl_alloc_type(dim->ctx, struct isl_point);
39 __isl_give isl_point *isl_point_zero(__isl_take isl_dim *dim)
45 vec = isl_vec_alloc(dim->ctx, 1 + isl_dim_total(dim));
48 isl_int_set_si(vec->el[0], 1);
49 isl_seq_clr(vec->el + 1, vec->size - 1);
50 return isl_point_alloc(dim, vec);
56 __isl_give isl_point *isl_point_dup(__isl_keep isl_point *pnt)
58 struct isl_point *pnt2;
62 pnt2 = isl_point_alloc(isl_dim_copy(pnt->dim), isl_vec_copy(pnt->vec));
66 __isl_give isl_point *isl_point_cow(__isl_take isl_point *pnt)
68 struct isl_point *pnt2;
75 pnt2 = isl_point_dup(pnt);
80 __isl_give isl_point *isl_point_copy(__isl_keep isl_point *pnt)
89 void isl_point_free(__isl_take isl_point *pnt)
97 isl_dim_free(pnt->dim);
98 isl_vec_free(pnt->vec);
102 __isl_give isl_point *isl_point_void(__isl_take isl_dim *dim)
107 return isl_point_alloc(dim, isl_vec_alloc(dim->ctx, 0));
110 int isl_point_is_void(__isl_keep isl_point *pnt)
115 return pnt->vec->size == 0;
118 void isl_point_get_coordinate(__isl_keep isl_point *pnt,
119 enum isl_dim_type type, int pos, isl_int *v)
121 if (!pnt || isl_point_is_void(pnt))
123 if (type == isl_dim_set)
124 pos += isl_dim_size(pnt->dim, isl_dim_param);
125 isl_int_set(*v, pnt->vec->el[1 + pos]);
128 __isl_give isl_point *isl_point_set_coordinate(__isl_take isl_point *pnt,
129 enum isl_dim_type type, int pos, isl_int v)
131 if (!pnt || isl_point_is_void(pnt))
134 pnt = isl_point_cow(pnt);
137 pnt->vec = isl_vec_cow(pnt->vec);
141 if (type == isl_dim_set)
142 pos += isl_dim_size(pnt->dim, isl_dim_param);
144 isl_int_set(pnt->vec->el[1 + pos], v);
152 __isl_give isl_point *isl_point_add_ui(__isl_take isl_point *pnt,
153 enum isl_dim_type type, int pos, unsigned val)
155 if (!pnt || isl_point_is_void(pnt))
158 pnt = isl_point_cow(pnt);
161 pnt->vec = isl_vec_cow(pnt->vec);
165 if (type == isl_dim_set)
166 pos += isl_dim_size(pnt->dim, isl_dim_param);
168 isl_int_add_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
176 __isl_give isl_point *isl_point_sub_ui(__isl_take isl_point *pnt,
177 enum isl_dim_type type, int pos, unsigned val)
179 if (!pnt || isl_point_is_void(pnt))
182 pnt = isl_point_cow(pnt);
185 pnt->vec = isl_vec_cow(pnt->vec);
189 if (type == isl_dim_set)
190 pos += isl_dim_size(pnt->dim, isl_dim_param);
192 isl_int_sub_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
200 struct isl_foreach_point {
201 struct isl_scan_callback callback;
202 int (*fn)(__isl_take isl_point *pnt, void *user);
207 static int foreach_point(struct isl_scan_callback *cb, __isl_take isl_vec *sample)
209 struct isl_foreach_point *fp = (struct isl_foreach_point *)cb;
212 pnt = isl_point_alloc(isl_dim_copy(fp->dim), sample);
214 return fp->fn(pnt, fp->user);
217 int isl_set_foreach_point(__isl_keep isl_set *set,
218 int (*fn)(__isl_take isl_point *pnt, void *user), void *user)
220 struct isl_foreach_point fp = { { &foreach_point }, fn, user };
226 fp.dim = isl_set_get_dim(set);
230 set = isl_set_copy(set);
231 set = isl_set_cow(set);
232 set = isl_set_make_disjoint(set);
233 set = isl_set_compute_divs(set);
237 for (i = 0; i < set->n; ++i)
238 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
243 isl_dim_free(fp.dim);
248 isl_dim_free(fp.dim);
252 /* Return 1 if "bmap" contains the point "point".
253 * "bmap" is assumed to have known divs.
254 * The point is first extended with the divs and then passed
255 * to basic_map_contains.
257 int isl_basic_map_contains_point(__isl_keep isl_basic_map *bmap,
258 __isl_keep isl_point *point)
267 isl_assert(bmap->ctx, isl_dim_equal(bmap->dim, point->dim), return -1);
268 if (bmap->n_div == 0)
269 return isl_basic_map_contains(bmap, point->vec);
271 dim = isl_basic_map_total_dim(bmap) - bmap->n_div;
272 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
276 isl_seq_cpy(vec->el, point->vec->el, point->vec->size);
277 for (i = 0; i < bmap->n_div; ++i) {
278 isl_seq_inner_product(bmap->div[i] + 1, vec->el,
279 1 + dim + i, &vec->el[1+dim+i]);
280 isl_int_fdiv_q(vec->el[1+dim+i], vec->el[1+dim+i],
284 contains = isl_basic_map_contains(bmap, vec);
290 int isl_map_contains_point(__isl_keep isl_map *map, __isl_keep isl_point *point)
298 map = isl_map_copy(map);
299 map = isl_map_compute_divs(map);
303 for (i = 0; i < map->n; ++i) {
304 found = isl_basic_map_contains_point(map->p[i], point);
318 int isl_set_contains_point(__isl_keep isl_set *set, __isl_keep isl_point *point)
320 return isl_map_contains_point((isl_map *)set, point);
323 __isl_give isl_set *isl_set_from_point(__isl_take isl_point *pnt)
326 isl_basic_set *model;
328 model = isl_basic_set_empty(isl_dim_copy(pnt->dim));
329 bset = isl_basic_set_from_vec(isl_vec_copy(pnt->vec));
330 bset = isl_basic_set_from_underlying_set(bset, model);
333 return isl_set_from_basic_set(bset);
336 __isl_give isl_set *isl_set_box_from_points(__isl_take isl_point *pnt1,
337 __isl_take isl_point *pnt2)
350 isl_assert(pnt1->dim->ctx,
351 isl_dim_equal(pnt1->dim, pnt2->dim), goto error);
353 if (isl_point_is_void(pnt1) && isl_point_is_void(pnt2)) {
354 isl_dim *dim = isl_dim_copy(pnt1->dim);
355 isl_point_free(pnt1);
356 isl_point_free(pnt2);
358 return isl_set_empty(dim);
360 if (isl_point_is_void(pnt1)) {
361 isl_point_free(pnt1);
363 return isl_set_from_point(pnt2);
365 if (isl_point_is_void(pnt2)) {
366 isl_point_free(pnt2);
368 return isl_set_from_point(pnt1);
371 total = isl_dim_total(pnt1->dim);
372 bset = isl_basic_set_alloc_dim(isl_dim_copy(pnt1->dim), 0, 0, 2 * total);
374 for (i = 0; i < total; ++i) {
375 isl_int_mul(t, pnt1->vec->el[1 + i], pnt2->vec->el[0]);
376 isl_int_submul(t, pnt2->vec->el[1 + i], pnt1->vec->el[0]);
378 k = isl_basic_set_alloc_inequality(bset);
381 isl_seq_clr(bset->ineq[k] + 1, total);
382 if (isl_int_is_pos(t)) {
383 isl_int_set_si(bset->ineq[k][1 + i], -1);
384 isl_int_set(bset->ineq[k][0], pnt1->vec->el[1 + i]);
386 isl_int_set_si(bset->ineq[k][1 + i], 1);
387 isl_int_neg(bset->ineq[k][0], pnt1->vec->el[1 + i]);
389 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt1->vec->el[0]);
391 k = isl_basic_set_alloc_inequality(bset);
394 isl_seq_clr(bset->ineq[k] + 1, total);
395 if (isl_int_is_pos(t)) {
396 isl_int_set_si(bset->ineq[k][1 + i], 1);
397 isl_int_neg(bset->ineq[k][0], pnt2->vec->el[1 + i]);
399 isl_int_set_si(bset->ineq[k][1 + i], -1);
400 isl_int_set(bset->ineq[k][0], pnt2->vec->el[1 + i]);
402 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt2->vec->el[0]);
405 bset = isl_basic_set_finalize(bset);
407 isl_point_free(pnt1);
408 isl_point_free(pnt2);
412 return isl_set_from_basic_set(bset);
414 isl_point_free(pnt1);
415 isl_point_free(pnt2);
420 void isl_point_print(__isl_keep isl_point *pnt, FILE *out)
428 if (isl_point_is_void(pnt)) {
429 fprintf(out, "void\n");
433 nparam = isl_dim_size(pnt->dim, isl_dim_param);
434 dim = isl_dim_size(pnt->dim, isl_dim_set);
437 for (i = 0; i < nparam; ++i) {
441 name = isl_dim_get_name(pnt->dim, isl_dim_param, i);
443 fprintf(out, "%s = ", name);
444 isl_int_print(out, pnt->vec->el[1 + i], 0);
445 if (!isl_int_is_one(pnt->vec->el[0])) {
447 isl_int_print(out, pnt->vec->el[0], 0);
450 fprintf(out, "] -> ");
453 for (i = 0; i < dim; ++i) {
456 isl_int_print(out, pnt->vec->el[1 + nparam + i], 0);
457 if (!isl_int_is_one(pnt->vec->el[0])) {
459 isl_int_print(out, pnt->vec->el[0], 0);