1 #include <isl_map_private.h>
2 #include <isl_point_private.h>
4 #include <isl_sample.h>
7 #include <isl_space_private.h>
9 isl_ctx *isl_point_get_ctx(__isl_keep isl_point *pnt)
11 return pnt ? isl_space_get_ctx(pnt->dim) : NULL;
14 __isl_give isl_space *isl_point_get_space(__isl_keep isl_point *pnt)
16 return pnt ? isl_space_copy(pnt->dim) : NULL;
19 __isl_give isl_point *isl_point_alloc(__isl_take isl_space *dim,
20 __isl_take isl_vec *vec)
22 struct isl_point *pnt;
27 if (vec->size > 1 + isl_space_dim(dim, isl_dim_all)) {
28 vec = isl_vec_cow(vec);
31 vec->size = 1 + isl_space_dim(dim, isl_dim_all);
34 pnt = isl_alloc_type(dim->ctx, struct isl_point);
49 __isl_give isl_point *isl_point_zero(__isl_take isl_space *dim)
55 vec = isl_vec_alloc(dim->ctx, 1 + isl_space_dim(dim, isl_dim_all));
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_space_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_space_free(pnt->dim);
108 isl_vec_free(pnt->vec);
112 __isl_give isl_point *isl_point_void(__isl_take isl_space *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 int 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))
134 if (pos < 0 || pos >= isl_space_dim(pnt->dim, type))
135 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
136 "position out of bounds", return -1);
138 if (type == isl_dim_set)
139 pos += isl_space_dim(pnt->dim, isl_dim_param);
140 isl_int_set(*v, pnt->vec->el[1 + pos]);
145 __isl_give isl_point *isl_point_set_coordinate(__isl_take isl_point *pnt,
146 enum isl_dim_type type, int pos, isl_int v)
148 if (!pnt || isl_point_is_void(pnt))
151 pnt = isl_point_cow(pnt);
154 pnt->vec = isl_vec_cow(pnt->vec);
158 if (type == isl_dim_set)
159 pos += isl_space_dim(pnt->dim, isl_dim_param);
161 isl_int_set(pnt->vec->el[1 + pos], v);
169 __isl_give isl_point *isl_point_add_ui(__isl_take isl_point *pnt,
170 enum isl_dim_type type, int pos, unsigned val)
172 if (!pnt || isl_point_is_void(pnt))
175 pnt = isl_point_cow(pnt);
178 pnt->vec = isl_vec_cow(pnt->vec);
182 if (type == isl_dim_set)
183 pos += isl_space_dim(pnt->dim, isl_dim_param);
185 isl_int_add_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
193 __isl_give isl_point *isl_point_sub_ui(__isl_take isl_point *pnt,
194 enum isl_dim_type type, int pos, unsigned val)
196 if (!pnt || isl_point_is_void(pnt))
199 pnt = isl_point_cow(pnt);
202 pnt->vec = isl_vec_cow(pnt->vec);
206 if (type == isl_dim_set)
207 pos += isl_space_dim(pnt->dim, isl_dim_param);
209 isl_int_sub_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
217 struct isl_foreach_point {
218 struct isl_scan_callback callback;
219 int (*fn)(__isl_take isl_point *pnt, void *user);
224 static int foreach_point(struct isl_scan_callback *cb, __isl_take isl_vec *sample)
226 struct isl_foreach_point *fp = (struct isl_foreach_point *)cb;
229 pnt = isl_point_alloc(isl_space_copy(fp->dim), sample);
231 return fp->fn(pnt, fp->user);
234 int isl_set_foreach_point(__isl_keep isl_set *set,
235 int (*fn)(__isl_take isl_point *pnt, void *user), void *user)
237 struct isl_foreach_point fp = { { &foreach_point }, fn, user };
243 fp.dim = isl_set_get_space(set);
247 set = isl_set_copy(set);
248 set = isl_set_cow(set);
249 set = isl_set_make_disjoint(set);
250 set = isl_set_compute_divs(set);
254 for (i = 0; i < set->n; ++i)
255 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
260 isl_space_free(fp.dim);
265 isl_space_free(fp.dim);
269 /* Return 1 if "bmap" contains the point "point".
270 * "bmap" is assumed to have known divs.
271 * The point is first extended with the divs and then passed
272 * to basic_map_contains.
274 int isl_basic_map_contains_point(__isl_keep isl_basic_map *bmap,
275 __isl_keep isl_point *point)
284 isl_assert(bmap->ctx, isl_space_is_equal(bmap->dim, point->dim), return -1);
285 if (bmap->n_div == 0)
286 return isl_basic_map_contains(bmap, point->vec);
288 dim = isl_basic_map_total_dim(bmap) - bmap->n_div;
289 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
293 isl_seq_cpy(vec->el, point->vec->el, point->vec->size);
294 for (i = 0; i < bmap->n_div; ++i) {
295 isl_seq_inner_product(bmap->div[i] + 1, vec->el,
296 1 + dim + i, &vec->el[1+dim+i]);
297 isl_int_fdiv_q(vec->el[1+dim+i], vec->el[1+dim+i],
301 contains = isl_basic_map_contains(bmap, vec);
307 int isl_map_contains_point(__isl_keep isl_map *map, __isl_keep isl_point *point)
315 map = isl_map_copy(map);
316 map = isl_map_compute_divs(map);
320 for (i = 0; i < map->n; ++i) {
321 found = isl_basic_map_contains_point(map->p[i], point);
335 int isl_set_contains_point(__isl_keep isl_set *set, __isl_keep isl_point *point)
337 return isl_map_contains_point((isl_map *)set, point);
340 __isl_give isl_basic_set *isl_basic_set_from_point(__isl_take isl_point *pnt)
343 isl_basic_set *model;
345 model = isl_basic_set_empty(isl_space_copy(pnt->dim));
346 bset = isl_basic_set_from_vec(isl_vec_copy(pnt->vec));
347 bset = isl_basic_set_from_underlying_set(bset, model);
353 __isl_give isl_set *isl_set_from_point(__isl_take isl_point *pnt)
356 bset = isl_basic_set_from_point(pnt);
357 return isl_set_from_basic_set(bset);
360 __isl_give isl_basic_set *isl_basic_set_box_from_points(
361 __isl_take isl_point *pnt1, __isl_take isl_point *pnt2)
374 isl_assert(pnt1->dim->ctx,
375 isl_space_is_equal(pnt1->dim, pnt2->dim), goto error);
377 if (isl_point_is_void(pnt1) && isl_point_is_void(pnt2)) {
378 isl_space *dim = isl_space_copy(pnt1->dim);
379 isl_point_free(pnt1);
380 isl_point_free(pnt2);
382 return isl_basic_set_empty(dim);
384 if (isl_point_is_void(pnt1)) {
385 isl_point_free(pnt1);
387 return isl_basic_set_from_point(pnt2);
389 if (isl_point_is_void(pnt2)) {
390 isl_point_free(pnt2);
392 return isl_basic_set_from_point(pnt1);
395 total = isl_space_dim(pnt1->dim, isl_dim_all);
396 bset = isl_basic_set_alloc_space(isl_space_copy(pnt1->dim), 0, 0, 2 * total);
398 for (i = 0; i < total; ++i) {
399 isl_int_mul(t, pnt1->vec->el[1 + i], pnt2->vec->el[0]);
400 isl_int_submul(t, pnt2->vec->el[1 + i], pnt1->vec->el[0]);
402 k = isl_basic_set_alloc_inequality(bset);
405 isl_seq_clr(bset->ineq[k] + 1, total);
406 if (isl_int_is_pos(t)) {
407 isl_int_set_si(bset->ineq[k][1 + i], -1);
408 isl_int_set(bset->ineq[k][0], pnt1->vec->el[1 + i]);
410 isl_int_set_si(bset->ineq[k][1 + i], 1);
411 isl_int_neg(bset->ineq[k][0], pnt1->vec->el[1 + i]);
413 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt1->vec->el[0]);
415 k = isl_basic_set_alloc_inequality(bset);
418 isl_seq_clr(bset->ineq[k] + 1, total);
419 if (isl_int_is_pos(t)) {
420 isl_int_set_si(bset->ineq[k][1 + i], 1);
421 isl_int_neg(bset->ineq[k][0], pnt2->vec->el[1 + i]);
423 isl_int_set_si(bset->ineq[k][1 + i], -1);
424 isl_int_set(bset->ineq[k][0], pnt2->vec->el[1 + i]);
426 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt2->vec->el[0]);
429 bset = isl_basic_set_finalize(bset);
431 isl_point_free(pnt1);
432 isl_point_free(pnt2);
438 isl_point_free(pnt1);
439 isl_point_free(pnt2);
444 __isl_give isl_set *isl_set_box_from_points(__isl_take isl_point *pnt1,
445 __isl_take isl_point *pnt2)
448 bset = isl_basic_set_box_from_points(pnt1, pnt2);
449 return isl_set_from_basic_set(bset);
452 __isl_give isl_printer *isl_printer_print_point(
453 __isl_take isl_printer *p, __isl_keep isl_point *pnt)
461 if (isl_point_is_void(pnt)) {
462 p = isl_printer_print_str(p, "void");
466 nparam = isl_space_dim(pnt->dim, isl_dim_param);
467 dim = isl_space_dim(pnt->dim, isl_dim_set);
469 p = isl_printer_print_str(p, "[");
470 for (i = 0; i < nparam; ++i) {
473 p = isl_printer_print_str(p, ", ");
474 name = isl_space_get_dim_name(pnt->dim, isl_dim_param, i);
476 p = isl_printer_print_str(p, name);
477 p = isl_printer_print_str(p, " = ");
479 p = isl_printer_print_isl_int(p, pnt->vec->el[1 + i]);
480 if (!isl_int_is_one(pnt->vec->el[0])) {
481 p = isl_printer_print_str(p, "/");
482 p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
485 p = isl_printer_print_str(p, "]");
486 p = isl_printer_print_str(p, " -> ");
488 p = isl_printer_print_str(p, "[");
489 for (i = 0; i < dim; ++i) {
491 p = isl_printer_print_str(p, ", ");
492 p = isl_printer_print_isl_int(p, pnt->vec->el[1 + nparam + i]);
493 if (!isl_int_is_one(pnt->vec->el[0])) {
494 p = isl_printer_print_str(p, "/");
495 p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
498 p = isl_printer_print_str(p, "]");