1 #include <isl_map_private.h>
2 #include <isl_point_private.h>
4 #include <isl_sample.h>
7 #include <isl_dim_private.h>
9 isl_ctx *isl_point_get_ctx(__isl_keep isl_point *pnt)
11 return pnt ? isl_dim_get_ctx(pnt->dim) : NULL;
14 __isl_give isl_dim *isl_point_get_dim(__isl_keep isl_point *pnt)
16 return pnt ? isl_dim_copy(pnt->dim) : NULL;
19 __isl_give isl_point *isl_point_alloc(__isl_take isl_dim *dim,
20 __isl_take isl_vec *vec)
22 struct isl_point *pnt;
27 if (vec->size > 1 + isl_dim_total(dim)) {
28 vec = isl_vec_cow(vec);
31 vec->size = 1 + isl_dim_total(dim);
34 pnt = isl_alloc_type(dim->ctx, struct isl_point);
49 __isl_give isl_point *isl_point_zero(__isl_take isl_dim *dim)
55 vec = isl_vec_alloc(dim->ctx, 1 + isl_dim_total(dim));
58 isl_int_set_si(vec->el[0], 1);
59 isl_seq_clr(vec->el + 1, vec->size - 1);
60 return isl_point_alloc(dim, vec);
66 __isl_give isl_point *isl_point_dup(__isl_keep isl_point *pnt)
68 struct isl_point *pnt2;
72 pnt2 = isl_point_alloc(isl_dim_copy(pnt->dim), isl_vec_copy(pnt->vec));
76 __isl_give isl_point *isl_point_cow(__isl_take isl_point *pnt)
78 struct isl_point *pnt2;
85 pnt2 = isl_point_dup(pnt);
90 __isl_give isl_point *isl_point_copy(__isl_keep isl_point *pnt)
99 void isl_point_free(__isl_take isl_point *pnt)
107 isl_dim_free(pnt->dim);
108 isl_vec_free(pnt->vec);
112 __isl_give isl_point *isl_point_void(__isl_take isl_dim *dim)
117 return isl_point_alloc(dim, isl_vec_alloc(dim->ctx, 0));
120 int isl_point_is_void(__isl_keep isl_point *pnt)
125 return pnt->vec->size == 0;
128 void isl_point_get_coordinate(__isl_keep isl_point *pnt,
129 enum isl_dim_type type, int pos, isl_int *v)
131 if (!pnt || isl_point_is_void(pnt))
133 if (type == isl_dim_set)
134 pos += isl_dim_size(pnt->dim, isl_dim_param);
135 isl_int_set(*v, pnt->vec->el[1 + pos]);
138 __isl_give isl_point *isl_point_set_coordinate(__isl_take isl_point *pnt,
139 enum isl_dim_type type, int pos, isl_int v)
141 if (!pnt || isl_point_is_void(pnt))
144 pnt = isl_point_cow(pnt);
147 pnt->vec = isl_vec_cow(pnt->vec);
151 if (type == isl_dim_set)
152 pos += isl_dim_size(pnt->dim, isl_dim_param);
154 isl_int_set(pnt->vec->el[1 + pos], v);
162 __isl_give isl_point *isl_point_add_ui(__isl_take isl_point *pnt,
163 enum isl_dim_type type, int pos, unsigned val)
165 if (!pnt || isl_point_is_void(pnt))
168 pnt = isl_point_cow(pnt);
171 pnt->vec = isl_vec_cow(pnt->vec);
175 if (type == isl_dim_set)
176 pos += isl_dim_size(pnt->dim, isl_dim_param);
178 isl_int_add_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
186 __isl_give isl_point *isl_point_sub_ui(__isl_take isl_point *pnt,
187 enum isl_dim_type type, int pos, unsigned val)
189 if (!pnt || isl_point_is_void(pnt))
192 pnt = isl_point_cow(pnt);
195 pnt->vec = isl_vec_cow(pnt->vec);
199 if (type == isl_dim_set)
200 pos += isl_dim_size(pnt->dim, isl_dim_param);
202 isl_int_sub_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
210 struct isl_foreach_point {
211 struct isl_scan_callback callback;
212 int (*fn)(__isl_take isl_point *pnt, void *user);
217 static int foreach_point(struct isl_scan_callback *cb, __isl_take isl_vec *sample)
219 struct isl_foreach_point *fp = (struct isl_foreach_point *)cb;
222 pnt = isl_point_alloc(isl_dim_copy(fp->dim), sample);
224 return fp->fn(pnt, fp->user);
227 int isl_set_foreach_point(__isl_keep isl_set *set,
228 int (*fn)(__isl_take isl_point *pnt, void *user), void *user)
230 struct isl_foreach_point fp = { { &foreach_point }, fn, user };
236 fp.dim = isl_set_get_dim(set);
240 set = isl_set_copy(set);
241 set = isl_set_cow(set);
242 set = isl_set_make_disjoint(set);
243 set = isl_set_compute_divs(set);
247 for (i = 0; i < set->n; ++i)
248 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
253 isl_dim_free(fp.dim);
258 isl_dim_free(fp.dim);
262 /* Return 1 if "bmap" contains the point "point".
263 * "bmap" is assumed to have known divs.
264 * The point is first extended with the divs and then passed
265 * to basic_map_contains.
267 int isl_basic_map_contains_point(__isl_keep isl_basic_map *bmap,
268 __isl_keep isl_point *point)
277 isl_assert(bmap->ctx, isl_dim_equal(bmap->dim, point->dim), return -1);
278 if (bmap->n_div == 0)
279 return isl_basic_map_contains(bmap, point->vec);
281 dim = isl_basic_map_total_dim(bmap) - bmap->n_div;
282 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
286 isl_seq_cpy(vec->el, point->vec->el, point->vec->size);
287 for (i = 0; i < bmap->n_div; ++i) {
288 isl_seq_inner_product(bmap->div[i] + 1, vec->el,
289 1 + dim + i, &vec->el[1+dim+i]);
290 isl_int_fdiv_q(vec->el[1+dim+i], vec->el[1+dim+i],
294 contains = isl_basic_map_contains(bmap, vec);
300 int isl_map_contains_point(__isl_keep isl_map *map, __isl_keep isl_point *point)
308 map = isl_map_copy(map);
309 map = isl_map_compute_divs(map);
313 for (i = 0; i < map->n; ++i) {
314 found = isl_basic_map_contains_point(map->p[i], point);
328 int isl_set_contains_point(__isl_keep isl_set *set, __isl_keep isl_point *point)
330 return isl_map_contains_point((isl_map *)set, point);
333 __isl_give isl_basic_set *isl_basic_set_from_point(__isl_take isl_point *pnt)
336 isl_basic_set *model;
338 model = isl_basic_set_empty(isl_dim_copy(pnt->dim));
339 bset = isl_basic_set_from_vec(isl_vec_copy(pnt->vec));
340 bset = isl_basic_set_from_underlying_set(bset, model);
346 __isl_give isl_set *isl_set_from_point(__isl_take isl_point *pnt)
349 bset = isl_basic_set_from_point(pnt);
350 return isl_set_from_basic_set(bset);
353 __isl_give isl_basic_set *isl_basic_set_box_from_points(
354 __isl_take isl_point *pnt1, __isl_take isl_point *pnt2)
367 isl_assert(pnt1->dim->ctx,
368 isl_dim_equal(pnt1->dim, pnt2->dim), goto error);
370 if (isl_point_is_void(pnt1) && isl_point_is_void(pnt2)) {
371 isl_dim *dim = isl_dim_copy(pnt1->dim);
372 isl_point_free(pnt1);
373 isl_point_free(pnt2);
375 return isl_basic_set_empty(dim);
377 if (isl_point_is_void(pnt1)) {
378 isl_point_free(pnt1);
380 return isl_basic_set_from_point(pnt2);
382 if (isl_point_is_void(pnt2)) {
383 isl_point_free(pnt2);
385 return isl_basic_set_from_point(pnt1);
388 total = isl_dim_total(pnt1->dim);
389 bset = isl_basic_set_alloc_dim(isl_dim_copy(pnt1->dim), 0, 0, 2 * total);
391 for (i = 0; i < total; ++i) {
392 isl_int_mul(t, pnt1->vec->el[1 + i], pnt2->vec->el[0]);
393 isl_int_submul(t, pnt2->vec->el[1 + i], pnt1->vec->el[0]);
395 k = isl_basic_set_alloc_inequality(bset);
398 isl_seq_clr(bset->ineq[k] + 1, total);
399 if (isl_int_is_pos(t)) {
400 isl_int_set_si(bset->ineq[k][1 + i], -1);
401 isl_int_set(bset->ineq[k][0], pnt1->vec->el[1 + i]);
403 isl_int_set_si(bset->ineq[k][1 + i], 1);
404 isl_int_neg(bset->ineq[k][0], pnt1->vec->el[1 + i]);
406 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt1->vec->el[0]);
408 k = isl_basic_set_alloc_inequality(bset);
411 isl_seq_clr(bset->ineq[k] + 1, total);
412 if (isl_int_is_pos(t)) {
413 isl_int_set_si(bset->ineq[k][1 + i], 1);
414 isl_int_neg(bset->ineq[k][0], pnt2->vec->el[1 + i]);
416 isl_int_set_si(bset->ineq[k][1 + i], -1);
417 isl_int_set(bset->ineq[k][0], pnt2->vec->el[1 + i]);
419 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt2->vec->el[0]);
422 bset = isl_basic_set_finalize(bset);
424 isl_point_free(pnt1);
425 isl_point_free(pnt2);
431 isl_point_free(pnt1);
432 isl_point_free(pnt2);
437 __isl_give isl_set *isl_set_box_from_points(__isl_take isl_point *pnt1,
438 __isl_take isl_point *pnt2)
441 bset = isl_basic_set_box_from_points(pnt1, pnt2);
442 return isl_set_from_basic_set(bset);
445 __isl_give isl_printer *isl_printer_print_point(
446 __isl_take isl_printer *p, __isl_keep isl_point *pnt)
454 if (isl_point_is_void(pnt)) {
455 p = isl_printer_print_str(p, "void");
459 nparam = isl_dim_size(pnt->dim, isl_dim_param);
460 dim = isl_dim_size(pnt->dim, isl_dim_set);
462 p = isl_printer_print_str(p, "[");
463 for (i = 0; i < nparam; ++i) {
466 p = isl_printer_print_str(p, ", ");
467 name = isl_dim_get_name(pnt->dim, isl_dim_param, i);
469 p = isl_printer_print_str(p, name);
470 p = isl_printer_print_str(p, " = ");
472 p = isl_printer_print_isl_int(p, pnt->vec->el[1 + i]);
473 if (!isl_int_is_one(pnt->vec->el[0])) {
474 p = isl_printer_print_str(p, "/");
475 p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
478 p = isl_printer_print_str(p, "]");
479 p = isl_printer_print_str(p, " -> ");
481 p = isl_printer_print_str(p, "[");
482 for (i = 0; i < dim; ++i) {
484 p = isl_printer_print_str(p, ", ");
485 p = isl_printer_print_isl_int(p, pnt->vec->el[1 + nparam + i]);
486 if (!isl_int_is_one(pnt->vec->el[0])) {
487 p = isl_printer_print_str(p, "/");
488 p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
491 p = isl_printer_print_str(p, "]");