add exported isl_pw_aff_param_on_domain_id
[isl.git] / isl_point.c
blob8bd76adef6209311a72cfc48901fa4915b4cba83
1 #include <isl_map_private.h>
2 #include <isl_point_private.h>
3 #include <isl/set.h>
4 #include <isl/union_set.h>
5 #include <isl_sample.h>
6 #include <isl_scan.h>
7 #include <isl_seq.h>
8 #include <isl_space_private.h>
9 #include <isl_local_private.h>
10 #include <isl_val_private.h>
11 #include <isl_vec_private.h>
12 #include <isl_output_private.h>
14 #include <set_to_map.c>
16 isl_ctx *isl_point_get_ctx(__isl_keep isl_point *pnt)
18 return pnt ? isl_space_get_ctx(pnt->dim) : NULL;
21 /* Return the space of "pnt".
23 __isl_keep isl_space *isl_point_peek_space(__isl_keep isl_point *pnt)
25 return pnt ? pnt->dim : NULL;
28 __isl_give isl_space *isl_point_get_space(__isl_keep isl_point *pnt)
30 return isl_space_copy(isl_point_peek_space(pnt));
33 __isl_give isl_point *isl_point_alloc(__isl_take isl_space *space,
34 __isl_take isl_vec *vec)
36 struct isl_point *pnt;
37 isl_size dim;
39 dim = isl_space_dim(space, isl_dim_all);
40 if (dim < 0 || !vec)
41 goto error;
43 if (vec->size > 1 + dim) {
44 vec = isl_vec_cow(vec);
45 if (!vec)
46 goto error;
47 vec->size = 1 + dim;
50 pnt = isl_alloc_type(space->ctx, struct isl_point);
51 if (!pnt)
52 goto error;
54 pnt->ref = 1;
55 pnt->dim = space;
56 pnt->vec = vec;
58 return pnt;
59 error:
60 isl_space_free(space);
61 isl_vec_free(vec);
62 return NULL;
65 __isl_give isl_point *isl_point_zero(__isl_take isl_space *space)
67 isl_vec *vec;
68 isl_size dim;
70 dim = isl_space_dim(space, isl_dim_all);
71 if (dim < 0)
72 goto error;
73 vec = isl_vec_alloc(space->ctx, 1 + dim);
74 if (!vec)
75 goto error;
76 isl_int_set_si(vec->el[0], 1);
77 isl_seq_clr(vec->el + 1, vec->size - 1);
78 return isl_point_alloc(space, vec);
79 error:
80 isl_space_free(space);
81 return NULL;
84 __isl_give isl_point *isl_point_dup(__isl_keep isl_point *pnt)
86 struct isl_point *pnt2;
88 if (!pnt)
89 return NULL;
90 pnt2 = isl_point_alloc(isl_space_copy(pnt->dim), isl_vec_copy(pnt->vec));
91 return pnt2;
94 __isl_give isl_point *isl_point_cow(__isl_take isl_point *pnt)
96 struct isl_point *pnt2;
97 if (!pnt)
98 return NULL;
100 if (pnt->ref == 1)
101 return pnt;
103 pnt2 = isl_point_dup(pnt);
104 isl_point_free(pnt);
105 return pnt2;
108 __isl_give isl_point *isl_point_copy(__isl_keep isl_point *pnt)
110 if (!pnt)
111 return NULL;
113 pnt->ref++;
114 return pnt;
117 __isl_null isl_point *isl_point_free(__isl_take isl_point *pnt)
119 if (!pnt)
120 return NULL;
122 if (--pnt->ref > 0)
123 return NULL;
125 isl_space_free(pnt->dim);
126 isl_vec_free(pnt->vec);
127 free(pnt);
128 return NULL;
131 __isl_give isl_point *isl_point_void(__isl_take isl_space *space)
133 if (!space)
134 return NULL;
136 return isl_point_alloc(space, isl_vec_alloc(space->ctx, 0));
139 isl_bool isl_point_is_void(__isl_keep isl_point *pnt)
141 if (!pnt)
142 return isl_bool_error;
144 return isl_bool_ok(pnt->vec->size == 0);
147 /* Return the space of "pnt".
148 * This may be either a copy or the space itself
149 * if there is only one reference to "pnt".
150 * This allows the space to be modified inplace
151 * if both the point and its space have only a single reference.
152 * The caller is not allowed to modify "pnt" between this call and
153 * a subsequent call to isl_point_restore_space.
154 * The only exception is that isl_point_free can be called instead.
156 __isl_give isl_space *isl_point_take_space(__isl_keep isl_point *pnt)
158 isl_space *space;
160 if (!pnt)
161 return NULL;
162 if (pnt->ref != 1)
163 return isl_point_get_space(pnt);
164 space = pnt->dim;
165 pnt->dim = NULL;
166 return space;
169 /* Set the space of "pnt" to "space", where the space of "pnt" may be missing
170 * due to a preceding call to isl_point_take_space.
171 * However, in this case, "pnt" only has a single reference and
172 * then the call to isl_point_cow has no effect.
174 __isl_give isl_point *isl_point_restore_space(__isl_take isl_point *pnt,
175 __isl_take isl_space *space)
177 if (!pnt || !space)
178 goto error;
180 if (pnt->dim == space) {
181 isl_space_free(space);
182 return pnt;
185 pnt = isl_point_cow(pnt);
186 if (!pnt)
187 goto error;
188 isl_space_free(pnt->dim);
189 pnt->dim = space;
191 return pnt;
192 error:
193 isl_point_free(pnt);
194 isl_space_free(space);
195 return NULL;
198 /* Return the coordinate vector of "pnt".
200 __isl_keep isl_vec *isl_point_peek_vec(__isl_keep isl_point *pnt)
202 return pnt ? pnt->vec : NULL;
205 /* Return a copy of the coordinate vector of "pnt".
207 __isl_give isl_vec *isl_point_get_vec(__isl_keep isl_point *pnt)
209 return isl_vec_copy(isl_point_peek_vec(pnt));
212 /* Return the coordinate vector of "pnt".
213 * This may be either a copy or the coordinate vector itself
214 * if there is only one reference to "pnt".
215 * This allows the coordinate vector to be modified inplace
216 * if both the point and its coordinate vector have only a single reference.
217 * The caller is not allowed to modify "pnt" between this call and
218 * a subsequent call to isl_point_restore_vec.
219 * The only exception is that isl_point_free can be called instead.
221 __isl_give isl_vec *isl_point_take_vec(__isl_keep isl_point *pnt)
223 isl_vec *vec;
225 if (!pnt)
226 return NULL;
227 if (pnt->ref != 1)
228 return isl_point_get_vec(pnt);
229 vec = pnt->vec;
230 pnt->vec = NULL;
231 return vec;
234 /* Set the coordinate vector of "pnt" to "vec",
235 * where the coordinate vector of "pnt" may be missing
236 * due to a preceding call to isl_point_take_vec.
237 * However, in this case, "pnt" only has a single reference and
238 * then the call to isl_point_cow has no effect.
240 __isl_give isl_point *isl_point_restore_vec(__isl_take isl_point *pnt,
241 __isl_take isl_vec *vec)
243 if (!pnt || !vec)
244 goto error;
246 if (pnt->vec == vec) {
247 isl_vec_free(vec);
248 return pnt;
251 pnt = isl_point_cow(pnt);
252 if (!pnt)
253 goto error;
254 isl_vec_free(pnt->vec);
255 pnt->vec = vec;
257 return pnt;
258 error:
259 isl_point_free(pnt);
260 isl_vec_free(vec);
261 return NULL;
264 /* Return the number of variables of the given type.
266 static isl_size isl_point_dim(__isl_keep isl_point *pnt, enum isl_dim_type type)
268 return isl_space_dim(isl_point_peek_space(pnt), type);
271 /* Return the position of the coordinates of the given type
272 * within the sequence of coordinates of "pnt".
274 static isl_size isl_point_var_offset(__isl_keep isl_point *pnt,
275 enum isl_dim_type type)
277 return pnt ? isl_space_offset(pnt->dim, type) : isl_size_error;
280 #undef TYPE
281 #define TYPE isl_point
282 static
283 #include "check_type_range_templ.c"
285 /* Return the value of coordinate "pos" of type "type" of "pnt".
287 __isl_give isl_val *isl_point_get_coordinate_val(__isl_keep isl_point *pnt,
288 enum isl_dim_type type, int pos)
290 isl_ctx *ctx;
291 isl_val *v;
292 isl_size off;
294 if (!pnt)
295 return NULL;
297 ctx = isl_point_get_ctx(pnt);
298 if (isl_point_is_void(pnt))
299 isl_die(ctx, isl_error_invalid,
300 "void point does not have coordinates", return NULL);
301 if (isl_point_check_range(pnt, type, pos, 1) < 0)
302 return NULL;
304 off = isl_point_var_offset(pnt, type);
305 if (off < 0)
306 return NULL;
307 pos += off;
309 v = isl_val_rat_from_isl_int(ctx, pnt->vec->el[1 + pos],
310 pnt->vec->el[0]);
311 return isl_val_normalize(v);
314 /* Replace coordinate "pos" of type "type" of "pnt" by "v".
316 __isl_give isl_point *isl_point_set_coordinate_val(__isl_take isl_point *pnt,
317 enum isl_dim_type type, int pos, __isl_take isl_val *v)
319 if (!pnt || !v)
320 goto error;
321 if (isl_point_is_void(pnt))
322 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
323 "void point does not have coordinates", goto error);
324 if (isl_point_check_range(pnt, type, pos, 1) < 0)
325 goto error;
326 if (!isl_val_is_rat(v))
327 isl_die(isl_point_get_ctx(pnt), isl_error_invalid,
328 "expecting rational value", goto error);
330 pos += isl_space_offset(isl_point_peek_space(pnt), type);
331 if (isl_int_eq(pnt->vec->el[1 + pos], v->n) &&
332 isl_int_eq(pnt->vec->el[0], v->d)) {
333 isl_val_free(v);
334 return pnt;
337 pnt = isl_point_cow(pnt);
338 if (!pnt)
339 goto error;
340 pnt->vec = isl_vec_cow(pnt->vec);
341 if (!pnt->vec)
342 goto error;
344 if (isl_int_eq(pnt->vec->el[0], v->d)) {
345 isl_int_set(pnt->vec->el[1 + pos], v->n);
346 } else if (isl_int_is_one(v->d)) {
347 isl_int_mul(pnt->vec->el[1 + pos], pnt->vec->el[0], v->n);
348 } else {
349 isl_seq_scale(pnt->vec->el + 1,
350 pnt->vec->el + 1, v->d, pnt->vec->size - 1);
351 isl_int_mul(pnt->vec->el[1 + pos], pnt->vec->el[0], v->n);
352 isl_int_mul(pnt->vec->el[0], pnt->vec->el[0], v->d);
353 pnt->vec = isl_vec_normalize(pnt->vec);
354 if (!pnt->vec)
355 goto error;
358 isl_val_free(v);
359 return pnt;
360 error:
361 isl_val_free(v);
362 isl_point_free(pnt);
363 return NULL;
366 __isl_give isl_point *isl_point_add_ui(__isl_take isl_point *pnt,
367 enum isl_dim_type type, int pos, unsigned val)
369 isl_size off;
371 if (!pnt || isl_point_is_void(pnt))
372 return pnt;
374 pnt = isl_point_cow(pnt);
375 if (!pnt)
376 return NULL;
377 pnt->vec = isl_vec_cow(pnt->vec);
378 if (!pnt->vec)
379 goto error;
381 off = isl_point_var_offset(pnt, type);
382 if (off < 0)
383 goto error;
384 pos += off;
386 isl_int_add_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
388 return pnt;
389 error:
390 isl_point_free(pnt);
391 return NULL;
394 __isl_give isl_point *isl_point_sub_ui(__isl_take isl_point *pnt,
395 enum isl_dim_type type, int pos, unsigned val)
397 isl_size off;
399 if (!pnt || isl_point_is_void(pnt))
400 return pnt;
402 pnt = isl_point_cow(pnt);
403 if (!pnt)
404 return NULL;
405 pnt->vec = isl_vec_cow(pnt->vec);
406 if (!pnt->vec)
407 goto error;
409 off = isl_point_var_offset(pnt, type);
410 if (off < 0)
411 goto error;
412 pos += off;
414 isl_int_sub_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
416 return pnt;
417 error:
418 isl_point_free(pnt);
419 return NULL;
422 struct isl_foreach_point {
423 struct isl_scan_callback callback;
424 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
425 void *user;
426 isl_space *dim;
429 static isl_stat foreach_point(struct isl_scan_callback *cb,
430 __isl_take isl_vec *sample)
432 struct isl_foreach_point *fp = (struct isl_foreach_point *)cb;
433 isl_point *pnt;
435 pnt = isl_point_alloc(isl_space_copy(fp->dim), sample);
437 return fp->fn(pnt, fp->user);
440 isl_stat isl_set_foreach_point(__isl_keep isl_set *set,
441 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
443 struct isl_foreach_point fp = { { &foreach_point }, fn, user };
444 int i;
446 if (!set)
447 return isl_stat_error;
449 fp.dim = isl_set_get_space(set);
450 if (!fp.dim)
451 return isl_stat_error;
453 set = isl_set_copy(set);
454 set = isl_set_cow(set);
455 set = isl_set_make_disjoint(set);
456 set = isl_set_compute_divs(set);
457 if (!set)
458 goto error;
460 for (i = 0; i < set->n; ++i)
461 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
462 &fp.callback) < 0)
463 goto error;
465 isl_set_free(set);
466 isl_space_free(fp.dim);
468 return isl_stat_ok;
469 error:
470 isl_set_free(set);
471 isl_space_free(fp.dim);
472 return isl_stat_error;
475 /* Return 1 if "bmap" contains the point "point".
476 * "bmap" is assumed to have known divs.
477 * The point is first extended with the divs and then passed
478 * to basic_map_contains.
480 isl_bool isl_basic_map_contains_point(__isl_keep isl_basic_map *bmap,
481 __isl_keep isl_point *point)
483 isl_local *local;
484 isl_vec *vec;
485 isl_bool contains;
487 if (!bmap || !point)
488 return isl_bool_error;
489 isl_assert(bmap->ctx, isl_space_is_equal(bmap->dim, point->dim),
490 return isl_bool_error);
491 if (bmap->n_div == 0)
492 return isl_basic_map_contains(bmap, point->vec);
494 local = isl_local_alloc_from_mat(isl_basic_map_get_divs(bmap));
495 vec = isl_point_get_vec(point);
496 vec = isl_local_extend_point_vec(local, vec);
497 isl_local_free(local);
499 contains = isl_basic_map_contains(bmap, vec);
501 isl_vec_free(vec);
502 return contains;
505 isl_bool isl_map_contains_point(__isl_keep isl_map *map,
506 __isl_keep isl_point *point)
508 int i;
509 isl_bool found = isl_bool_false;
511 if (!map || !point)
512 return isl_bool_error;
514 map = isl_map_copy(map);
515 map = isl_map_compute_divs(map);
516 if (!map)
517 return isl_bool_error;
519 for (i = 0; i < map->n; ++i) {
520 found = isl_basic_map_contains_point(map->p[i], point);
521 if (found < 0)
522 goto error;
523 if (found)
524 break;
526 isl_map_free(map);
528 return found;
529 error:
530 isl_map_free(map);
531 return isl_bool_error;
534 isl_bool isl_set_contains_point(__isl_keep isl_set *set,
535 __isl_keep isl_point *point)
537 return isl_map_contains_point(set_to_map(set), point);
540 __isl_give isl_basic_set *isl_basic_set_from_point(__isl_take isl_point *pnt)
542 isl_basic_set *bset;
543 isl_basic_set *model;
545 if (!pnt)
546 return NULL;
548 model = isl_basic_set_empty(isl_space_copy(pnt->dim));
549 bset = isl_basic_set_from_vec(isl_vec_copy(pnt->vec));
550 bset = isl_basic_set_from_underlying_set(bset, model);
551 isl_point_free(pnt);
553 return bset;
556 __isl_give isl_set *isl_set_from_point(__isl_take isl_point *pnt)
558 isl_basic_set *bset;
559 bset = isl_basic_set_from_point(pnt);
560 return isl_set_from_basic_set(bset);
563 /* Construct a union set, containing the single element "pnt".
564 * If "pnt" is void, then return an empty union set.
566 __isl_give isl_union_set *isl_union_set_from_point(__isl_take isl_point *pnt)
568 if (!pnt)
569 return NULL;
570 if (isl_point_is_void(pnt)) {
571 isl_space *space;
573 space = isl_point_get_space(pnt);
574 isl_point_free(pnt);
575 return isl_union_set_empty(space);
578 return isl_union_set_from_set(isl_set_from_point(pnt));
581 __isl_give isl_basic_set *isl_basic_set_box_from_points(
582 __isl_take isl_point *pnt1, __isl_take isl_point *pnt2)
584 isl_basic_set *bset = NULL;
585 isl_size total;
586 int i;
587 int k;
588 isl_int t;
590 isl_int_init(t);
592 if (!pnt1 || !pnt2)
593 goto error;
595 isl_assert(pnt1->dim->ctx,
596 isl_space_is_equal(pnt1->dim, pnt2->dim), goto error);
598 if (isl_point_is_void(pnt1) && isl_point_is_void(pnt2)) {
599 isl_space *dim = isl_space_copy(pnt1->dim);
600 isl_point_free(pnt1);
601 isl_point_free(pnt2);
602 isl_int_clear(t);
603 return isl_basic_set_empty(dim);
605 if (isl_point_is_void(pnt1)) {
606 isl_point_free(pnt1);
607 isl_int_clear(t);
608 return isl_basic_set_from_point(pnt2);
610 if (isl_point_is_void(pnt2)) {
611 isl_point_free(pnt2);
612 isl_int_clear(t);
613 return isl_basic_set_from_point(pnt1);
616 total = isl_point_dim(pnt1, isl_dim_all);
617 if (total < 0)
618 goto error;
619 bset = isl_basic_set_alloc_space(isl_space_copy(pnt1->dim), 0, 0, 2 * total);
621 for (i = 0; i < total; ++i) {
622 isl_int_mul(t, pnt1->vec->el[1 + i], pnt2->vec->el[0]);
623 isl_int_submul(t, pnt2->vec->el[1 + i], pnt1->vec->el[0]);
625 k = isl_basic_set_alloc_inequality(bset);
626 if (k < 0)
627 goto error;
628 isl_seq_clr(bset->ineq[k] + 1, total);
629 if (isl_int_is_pos(t)) {
630 isl_int_set_si(bset->ineq[k][1 + i], -1);
631 isl_int_set(bset->ineq[k][0], pnt1->vec->el[1 + i]);
632 } else {
633 isl_int_set_si(bset->ineq[k][1 + i], 1);
634 isl_int_neg(bset->ineq[k][0], pnt1->vec->el[1 + i]);
636 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt1->vec->el[0]);
638 k = isl_basic_set_alloc_inequality(bset);
639 if (k < 0)
640 goto error;
641 isl_seq_clr(bset->ineq[k] + 1, total);
642 if (isl_int_is_pos(t)) {
643 isl_int_set_si(bset->ineq[k][1 + i], 1);
644 isl_int_neg(bset->ineq[k][0], pnt2->vec->el[1 + i]);
645 } else {
646 isl_int_set_si(bset->ineq[k][1 + i], -1);
647 isl_int_set(bset->ineq[k][0], pnt2->vec->el[1 + i]);
649 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt2->vec->el[0]);
652 bset = isl_basic_set_finalize(bset);
654 isl_point_free(pnt1);
655 isl_point_free(pnt2);
657 isl_int_clear(t);
659 return bset;
660 error:
661 isl_point_free(pnt1);
662 isl_point_free(pnt2);
663 isl_int_clear(t);
664 isl_basic_set_free(bset);
665 return NULL;
668 __isl_give isl_set *isl_set_box_from_points(__isl_take isl_point *pnt1,
669 __isl_take isl_point *pnt2)
671 isl_basic_set *bset;
672 bset = isl_basic_set_box_from_points(pnt1, pnt2);
673 return isl_set_from_basic_set(bset);
676 /* Print the coordinate at position "pos" of the point "pnt".
678 static __isl_give isl_printer *print_coordinate(__isl_take isl_printer *p,
679 struct isl_print_space_data *data, unsigned pos)
681 isl_point *pnt = data->user;
683 pos += isl_space_offset(data->space, data->type);
684 p = isl_printer_print_isl_int(p, pnt->vec->el[1 + pos]);
685 if (!isl_int_is_one(pnt->vec->el[0])) {
686 p = isl_printer_print_str(p, "/");
687 p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
690 return p;
693 __isl_give isl_printer *isl_printer_print_point(
694 __isl_take isl_printer *p, __isl_keep isl_point *pnt)
696 struct isl_print_space_data data = { 0 };
697 int i;
698 isl_size nparam;
700 if (!pnt)
701 return p;
702 if (isl_point_is_void(pnt)) {
703 p = isl_printer_print_str(p, "void");
704 return p;
707 nparam = isl_point_dim(pnt, isl_dim_param);
708 if (nparam < 0)
709 return isl_printer_free(p);
710 if (nparam > 0) {
711 p = isl_printer_print_str(p, "[");
712 for (i = 0; i < nparam; ++i) {
713 const char *name;
714 if (i)
715 p = isl_printer_print_str(p, ", ");
716 name = isl_space_get_dim_name(pnt->dim, isl_dim_param, i);
717 if (name) {
718 p = isl_printer_print_str(p, name);
719 p = isl_printer_print_str(p, " = ");
721 p = isl_printer_print_isl_int(p, pnt->vec->el[1 + i]);
722 if (!isl_int_is_one(pnt->vec->el[0])) {
723 p = isl_printer_print_str(p, "/");
724 p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
727 p = isl_printer_print_str(p, "]");
728 p = isl_printer_print_str(p, " -> ");
730 data.print_dim = &print_coordinate;
731 data.user = pnt;
732 p = isl_printer_print_str(p, "{ ");
733 p = isl_print_space(pnt->dim, p, 0, &data);
734 p = isl_printer_print_str(p, " }");
735 return p;