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_point *isl_point_alloc(__isl_take isl_dim *dim,
15 __isl_take isl_vec *vec)
17 struct isl_point *pnt;
22 if (vec->size > 1 + isl_dim_total(dim)) {
23 vec = isl_vec_cow(vec);
26 vec->size = 1 + isl_dim_total(dim);
29 pnt = isl_alloc_type(dim->ctx, struct isl_point);
44 __isl_give isl_point *isl_point_zero(__isl_take isl_dim *dim)
50 vec = isl_vec_alloc(dim->ctx, 1 + isl_dim_total(dim));
53 isl_int_set_si(vec->el[0], 1);
54 isl_seq_clr(vec->el + 1, vec->size - 1);
55 return isl_point_alloc(dim, vec);
61 __isl_give isl_point *isl_point_dup(__isl_keep isl_point *pnt)
63 struct isl_point *pnt2;
67 pnt2 = isl_point_alloc(isl_dim_copy(pnt->dim), isl_vec_copy(pnt->vec));
71 __isl_give isl_point *isl_point_cow(__isl_take isl_point *pnt)
73 struct isl_point *pnt2;
80 pnt2 = isl_point_dup(pnt);
85 __isl_give isl_point *isl_point_copy(__isl_keep isl_point *pnt)
94 void isl_point_free(__isl_take isl_point *pnt)
102 isl_dim_free(pnt->dim);
103 isl_vec_free(pnt->vec);
107 __isl_give isl_point *isl_point_void(__isl_take isl_dim *dim)
112 return isl_point_alloc(dim, isl_vec_alloc(dim->ctx, 0));
115 int isl_point_is_void(__isl_keep isl_point *pnt)
120 return pnt->vec->size == 0;
123 void isl_point_get_coordinate(__isl_keep isl_point *pnt,
124 enum isl_dim_type type, int pos, isl_int *v)
126 if (!pnt || isl_point_is_void(pnt))
128 if (type == isl_dim_set)
129 pos += isl_dim_size(pnt->dim, isl_dim_param);
130 isl_int_set(*v, pnt->vec->el[1 + pos]);
133 __isl_give isl_point *isl_point_set_coordinate(__isl_take isl_point *pnt,
134 enum isl_dim_type type, int pos, isl_int v)
136 if (!pnt || isl_point_is_void(pnt))
139 pnt = isl_point_cow(pnt);
142 pnt->vec = isl_vec_cow(pnt->vec);
146 if (type == isl_dim_set)
147 pos += isl_dim_size(pnt->dim, isl_dim_param);
149 isl_int_set(pnt->vec->el[1 + pos], v);
157 __isl_give isl_point *isl_point_add_ui(__isl_take isl_point *pnt,
158 enum isl_dim_type type, int pos, unsigned val)
160 if (!pnt || isl_point_is_void(pnt))
163 pnt = isl_point_cow(pnt);
166 pnt->vec = isl_vec_cow(pnt->vec);
170 if (type == isl_dim_set)
171 pos += isl_dim_size(pnt->dim, isl_dim_param);
173 isl_int_add_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
181 __isl_give isl_point *isl_point_sub_ui(__isl_take isl_point *pnt,
182 enum isl_dim_type type, int pos, unsigned val)
184 if (!pnt || isl_point_is_void(pnt))
187 pnt = isl_point_cow(pnt);
190 pnt->vec = isl_vec_cow(pnt->vec);
194 if (type == isl_dim_set)
195 pos += isl_dim_size(pnt->dim, isl_dim_param);
197 isl_int_sub_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
205 struct isl_foreach_point {
206 struct isl_scan_callback callback;
207 int (*fn)(__isl_take isl_point *pnt, void *user);
212 static int foreach_point(struct isl_scan_callback *cb, __isl_take isl_vec *sample)
214 struct isl_foreach_point *fp = (struct isl_foreach_point *)cb;
217 pnt = isl_point_alloc(isl_dim_copy(fp->dim), sample);
219 return fp->fn(pnt, fp->user);
222 int isl_set_foreach_point(__isl_keep isl_set *set,
223 int (*fn)(__isl_take isl_point *pnt, void *user), void *user)
225 struct isl_foreach_point fp = { { &foreach_point }, fn, user };
231 fp.dim = isl_set_get_dim(set);
235 set = isl_set_copy(set);
236 set = isl_set_cow(set);
237 set = isl_set_make_disjoint(set);
238 set = isl_set_compute_divs(set);
242 for (i = 0; i < set->n; ++i)
243 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
248 isl_dim_free(fp.dim);
253 isl_dim_free(fp.dim);
257 /* Return 1 if "bmap" contains the point "point".
258 * "bmap" is assumed to have known divs.
259 * The point is first extended with the divs and then passed
260 * to basic_map_contains.
262 int isl_basic_map_contains_point(__isl_keep isl_basic_map *bmap,
263 __isl_keep isl_point *point)
272 isl_assert(bmap->ctx, isl_dim_equal(bmap->dim, point->dim), return -1);
273 if (bmap->n_div == 0)
274 return isl_basic_map_contains(bmap, point->vec);
276 dim = isl_basic_map_total_dim(bmap) - bmap->n_div;
277 vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
281 isl_seq_cpy(vec->el, point->vec->el, point->vec->size);
282 for (i = 0; i < bmap->n_div; ++i) {
283 isl_seq_inner_product(bmap->div[i] + 1, vec->el,
284 1 + dim + i, &vec->el[1+dim+i]);
285 isl_int_fdiv_q(vec->el[1+dim+i], vec->el[1+dim+i],
289 contains = isl_basic_map_contains(bmap, vec);
295 int isl_map_contains_point(__isl_keep isl_map *map, __isl_keep isl_point *point)
303 map = isl_map_copy(map);
304 map = isl_map_compute_divs(map);
308 for (i = 0; i < map->n; ++i) {
309 found = isl_basic_map_contains_point(map->p[i], point);
323 int isl_set_contains_point(__isl_keep isl_set *set, __isl_keep isl_point *point)
325 return isl_map_contains_point((isl_map *)set, point);
328 __isl_give isl_basic_set *isl_basic_set_from_point(__isl_take isl_point *pnt)
331 isl_basic_set *model;
333 model = isl_basic_set_empty(isl_dim_copy(pnt->dim));
334 bset = isl_basic_set_from_vec(isl_vec_copy(pnt->vec));
335 bset = isl_basic_set_from_underlying_set(bset, model);
341 __isl_give isl_set *isl_set_from_point(__isl_take isl_point *pnt)
344 bset = isl_basic_set_from_point(pnt);
345 return isl_set_from_basic_set(bset);
348 __isl_give isl_basic_set *isl_basic_set_box_from_points(
349 __isl_take isl_point *pnt1, __isl_take isl_point *pnt2)
362 isl_assert(pnt1->dim->ctx,
363 isl_dim_equal(pnt1->dim, pnt2->dim), goto error);
365 if (isl_point_is_void(pnt1) && isl_point_is_void(pnt2)) {
366 isl_dim *dim = isl_dim_copy(pnt1->dim);
367 isl_point_free(pnt1);
368 isl_point_free(pnt2);
370 return isl_basic_set_empty(dim);
372 if (isl_point_is_void(pnt1)) {
373 isl_point_free(pnt1);
375 return isl_basic_set_from_point(pnt2);
377 if (isl_point_is_void(pnt2)) {
378 isl_point_free(pnt2);
380 return isl_basic_set_from_point(pnt1);
383 total = isl_dim_total(pnt1->dim);
384 bset = isl_basic_set_alloc_dim(isl_dim_copy(pnt1->dim), 0, 0, 2 * total);
386 for (i = 0; i < total; ++i) {
387 isl_int_mul(t, pnt1->vec->el[1 + i], pnt2->vec->el[0]);
388 isl_int_submul(t, pnt2->vec->el[1 + i], pnt1->vec->el[0]);
390 k = isl_basic_set_alloc_inequality(bset);
393 isl_seq_clr(bset->ineq[k] + 1, total);
394 if (isl_int_is_pos(t)) {
395 isl_int_set_si(bset->ineq[k][1 + i], -1);
396 isl_int_set(bset->ineq[k][0], pnt1->vec->el[1 + i]);
398 isl_int_set_si(bset->ineq[k][1 + i], 1);
399 isl_int_neg(bset->ineq[k][0], pnt1->vec->el[1 + i]);
401 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt1->vec->el[0]);
403 k = isl_basic_set_alloc_inequality(bset);
406 isl_seq_clr(bset->ineq[k] + 1, total);
407 if (isl_int_is_pos(t)) {
408 isl_int_set_si(bset->ineq[k][1 + i], 1);
409 isl_int_neg(bset->ineq[k][0], pnt2->vec->el[1 + i]);
411 isl_int_set_si(bset->ineq[k][1 + i], -1);
412 isl_int_set(bset->ineq[k][0], pnt2->vec->el[1 + i]);
414 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt2->vec->el[0]);
417 bset = isl_basic_set_finalize(bset);
419 isl_point_free(pnt1);
420 isl_point_free(pnt2);
426 isl_point_free(pnt1);
427 isl_point_free(pnt2);
432 __isl_give isl_set *isl_set_box_from_points(__isl_take isl_point *pnt1,
433 __isl_take isl_point *pnt2)
436 bset = isl_basic_set_box_from_points(pnt1, pnt2);
437 return isl_set_from_basic_set(bset);
440 void isl_point_print(__isl_keep isl_point *pnt, FILE *out)
448 if (isl_point_is_void(pnt)) {
449 fprintf(out, "void\n");
453 nparam = isl_dim_size(pnt->dim, isl_dim_param);
454 dim = isl_dim_size(pnt->dim, isl_dim_set);
457 for (i = 0; i < nparam; ++i) {
461 name = isl_dim_get_name(pnt->dim, isl_dim_param, i);
463 fprintf(out, "%s = ", name);
464 isl_int_print(out, pnt->vec->el[1 + i], 0);
465 if (!isl_int_is_one(pnt->vec->el[0])) {
467 isl_int_print(out, pnt->vec->el[0], 0);
470 fprintf(out, "] -> ");
473 for (i = 0; i < dim; ++i) {
476 isl_int_print(out, pnt->vec->el[1 + nparam + i], 0);
477 if (!isl_int_is_one(pnt->vec->el[0])) {
479 isl_int_print(out, pnt->vec->el[0], 0);