1 #include <isl_map_private.h>
2 #include <isl_point_private.h>
4 #include <isl/union_set.h>
5 #include <isl_sample.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
;
41 if (vec
->size
> 1 + isl_space_dim(space
, isl_dim_all
)) {
42 vec
= isl_vec_cow(vec
);
45 vec
->size
= 1 + isl_space_dim(space
, isl_dim_all
);
48 pnt
= isl_alloc_type(space
->ctx
, struct isl_point
);
58 isl_space_free(space
);
63 __isl_give isl_point
*isl_point_zero(__isl_take isl_space
*space
)
69 vec
= isl_vec_alloc(space
->ctx
, 1 + isl_space_dim(space
, isl_dim_all
));
72 isl_int_set_si(vec
->el
[0], 1);
73 isl_seq_clr(vec
->el
+ 1, vec
->size
- 1);
74 return isl_point_alloc(space
, vec
);
76 isl_space_free(space
);
80 __isl_give isl_point
*isl_point_dup(__isl_keep isl_point
*pnt
)
82 struct isl_point
*pnt2
;
86 pnt2
= isl_point_alloc(isl_space_copy(pnt
->dim
), isl_vec_copy(pnt
->vec
));
90 __isl_give isl_point
*isl_point_cow(__isl_take isl_point
*pnt
)
92 struct isl_point
*pnt2
;
99 pnt2
= isl_point_dup(pnt
);
104 __isl_give isl_point
*isl_point_copy(__isl_keep isl_point
*pnt
)
113 __isl_null isl_point
*isl_point_free(__isl_take isl_point
*pnt
)
121 isl_space_free(pnt
->dim
);
122 isl_vec_free(pnt
->vec
);
127 __isl_give isl_point
*isl_point_void(__isl_take isl_space
*space
)
132 return isl_point_alloc(space
, isl_vec_alloc(space
->ctx
, 0));
135 isl_bool
isl_point_is_void(__isl_keep isl_point
*pnt
)
138 return isl_bool_error
;
140 return pnt
->vec
->size
== 0;
143 /* Return the space of "pnt".
144 * This may be either a copy or the space itself
145 * if there is only one reference to "pnt".
146 * This allows the space to be modified inplace
147 * if both the point and its space have only a single reference.
148 * The caller is not allowed to modify "pnt" between this call and
149 * a subsequent call to isl_point_restore_space.
150 * The only exception is that isl_point_free can be called instead.
152 __isl_give isl_space
*isl_point_take_space(__isl_keep isl_point
*pnt
)
159 return isl_point_get_space(pnt
);
165 /* Set the space of "pnt" to "space", where the space of "pnt" may be missing
166 * due to a preceding call to isl_point_take_space.
167 * However, in this case, "pnt" only has a single reference and
168 * then the call to isl_point_cow has no effect.
170 __isl_give isl_point
*isl_point_restore_space(__isl_take isl_point
*pnt
,
171 __isl_take isl_space
*space
)
176 if (pnt
->dim
== space
) {
177 isl_space_free(space
);
181 pnt
= isl_point_cow(pnt
);
184 isl_space_free(pnt
->dim
);
190 isl_space_free(space
);
194 /* Return the coordinate vector of "pnt".
196 __isl_keep isl_vec
*isl_point_peek_vec(__isl_keep isl_point
*pnt
)
198 return pnt
? pnt
->vec
: NULL
;
201 /* Return a copy of the coordinate vector of "pnt".
203 __isl_give isl_vec
*isl_point_get_vec(__isl_keep isl_point
*pnt
)
205 return isl_vec_copy(isl_point_peek_vec(pnt
));
208 /* Return the coordinate vector of "pnt".
209 * This may be either a copy or the coordinate vector itself
210 * if there is only one reference to "pnt".
211 * This allows the coordinate vector to be modified inplace
212 * if both the point and its coordinate vector have only a single reference.
213 * The caller is not allowed to modify "pnt" between this call and
214 * a subsequent call to isl_point_restore_vec.
215 * The only exception is that isl_point_free can be called instead.
217 __isl_give isl_vec
*isl_point_take_vec(__isl_keep isl_point
*pnt
)
224 return isl_point_get_vec(pnt
);
230 /* Set the coordinate vector of "pnt" to "vec",
231 * where the coordinate vector of "pnt" may be missing
232 * due to a preceding call to isl_point_take_vec.
233 * However, in this case, "pnt" only has a single reference and
234 * then the call to isl_point_cow has no effect.
236 __isl_give isl_point
*isl_point_restore_vec(__isl_take isl_point
*pnt
,
237 __isl_take isl_vec
*vec
)
242 if (pnt
->vec
== vec
) {
247 pnt
= isl_point_cow(pnt
);
250 isl_vec_free(pnt
->vec
);
260 /* Return the number of variables of the given type.
262 static unsigned isl_point_dim(__isl_keep isl_point
*pnt
, enum isl_dim_type type
)
264 return pnt
? isl_space_dim(pnt
->dim
, type
) : 0;
267 /* Return the position of the coordinates of the given type
268 * within the sequence of coordinates of "pnt".
270 static int isl_point_var_offset(__isl_keep isl_point
*pnt
,
271 enum isl_dim_type type
)
273 return pnt
? isl_space_offset(pnt
->dim
, type
) : -1;
277 #define TYPE isl_point
279 #include "check_type_range_templ.c"
281 /* Return the value of coordinate "pos" of type "type" of "pnt".
283 __isl_give isl_val
*isl_point_get_coordinate_val(__isl_keep isl_point
*pnt
,
284 enum isl_dim_type type
, int pos
)
293 ctx
= isl_point_get_ctx(pnt
);
294 if (isl_point_is_void(pnt
))
295 isl_die(ctx
, isl_error_invalid
,
296 "void point does not have coordinates", return NULL
);
297 if (isl_point_check_range(pnt
, type
, pos
, 1) < 0)
300 off
= isl_point_var_offset(pnt
, type
);
305 v
= isl_val_rat_from_isl_int(ctx
, pnt
->vec
->el
[1 + pos
],
307 return isl_val_normalize(v
);
310 /* Replace coordinate "pos" of type "type" of "pnt" by "v".
312 __isl_give isl_point
*isl_point_set_coordinate_val(__isl_take isl_point
*pnt
,
313 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
317 if (isl_point_is_void(pnt
))
318 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
319 "void point does not have coordinates", goto error
);
320 if (isl_point_check_range(pnt
, type
, pos
, 1) < 0)
322 if (!isl_val_is_rat(v
))
323 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
324 "expecting rational value", goto error
);
326 if (isl_int_eq(pnt
->vec
->el
[1 + pos
], v
->n
) &&
327 isl_int_eq(pnt
->vec
->el
[0], v
->d
)) {
332 pnt
= isl_point_cow(pnt
);
335 pnt
->vec
= isl_vec_cow(pnt
->vec
);
339 if (isl_int_eq(pnt
->vec
->el
[0], v
->d
)) {
340 isl_int_set(pnt
->vec
->el
[1 + pos
], v
->n
);
341 } else if (isl_int_is_one(v
->d
)) {
342 isl_int_mul(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[0], v
->n
);
344 isl_seq_scale(pnt
->vec
->el
+ 1,
345 pnt
->vec
->el
+ 1, v
->d
, pnt
->vec
->size
- 1);
346 isl_int_mul(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[0], v
->n
);
347 isl_int_mul(pnt
->vec
->el
[0], pnt
->vec
->el
[0], v
->d
);
348 pnt
->vec
= isl_vec_normalize(pnt
->vec
);
361 __isl_give isl_point
*isl_point_add_ui(__isl_take isl_point
*pnt
,
362 enum isl_dim_type type
, int pos
, unsigned val
)
366 if (!pnt
|| isl_point_is_void(pnt
))
369 pnt
= isl_point_cow(pnt
);
372 pnt
->vec
= isl_vec_cow(pnt
->vec
);
376 off
= isl_point_var_offset(pnt
, type
);
381 isl_int_add_ui(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[1 + pos
], val
);
389 __isl_give isl_point
*isl_point_sub_ui(__isl_take isl_point
*pnt
,
390 enum isl_dim_type type
, int pos
, unsigned val
)
394 if (!pnt
|| isl_point_is_void(pnt
))
397 pnt
= isl_point_cow(pnt
);
400 pnt
->vec
= isl_vec_cow(pnt
->vec
);
404 off
= isl_point_var_offset(pnt
, type
);
409 isl_int_sub_ui(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[1 + pos
], val
);
417 struct isl_foreach_point
{
418 struct isl_scan_callback callback
;
419 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
);
424 static isl_stat
foreach_point(struct isl_scan_callback
*cb
,
425 __isl_take isl_vec
*sample
)
427 struct isl_foreach_point
*fp
= (struct isl_foreach_point
*)cb
;
430 pnt
= isl_point_alloc(isl_space_copy(fp
->dim
), sample
);
432 return fp
->fn(pnt
, fp
->user
);
435 isl_stat
isl_set_foreach_point(__isl_keep isl_set
*set
,
436 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
438 struct isl_foreach_point fp
= { { &foreach_point
}, fn
, user
};
442 return isl_stat_error
;
444 fp
.dim
= isl_set_get_space(set
);
446 return isl_stat_error
;
448 set
= isl_set_copy(set
);
449 set
= isl_set_cow(set
);
450 set
= isl_set_make_disjoint(set
);
451 set
= isl_set_compute_divs(set
);
455 for (i
= 0; i
< set
->n
; ++i
)
456 if (isl_basic_set_scan(isl_basic_set_copy(set
->p
[i
]),
461 isl_space_free(fp
.dim
);
466 isl_space_free(fp
.dim
);
467 return isl_stat_error
;
470 /* Return 1 if "bmap" contains the point "point".
471 * "bmap" is assumed to have known divs.
472 * The point is first extended with the divs and then passed
473 * to basic_map_contains.
475 isl_bool
isl_basic_map_contains_point(__isl_keep isl_basic_map
*bmap
,
476 __isl_keep isl_point
*point
)
483 return isl_bool_error
;
484 isl_assert(bmap
->ctx
, isl_space_is_equal(bmap
->dim
, point
->dim
),
485 return isl_bool_error
);
486 if (bmap
->n_div
== 0)
487 return isl_basic_map_contains(bmap
, point
->vec
);
489 local
= isl_local_alloc_from_mat(isl_basic_map_get_divs(bmap
));
490 vec
= isl_point_get_vec(point
);
491 vec
= isl_local_extend_point_vec(local
, vec
);
492 isl_local_free(local
);
494 contains
= isl_basic_map_contains(bmap
, vec
);
500 isl_bool
isl_map_contains_point(__isl_keep isl_map
*map
,
501 __isl_keep isl_point
*point
)
504 isl_bool found
= isl_bool_false
;
507 return isl_bool_error
;
509 map
= isl_map_copy(map
);
510 map
= isl_map_compute_divs(map
);
512 return isl_bool_error
;
514 for (i
= 0; i
< map
->n
; ++i
) {
515 found
= isl_basic_map_contains_point(map
->p
[i
], point
);
526 return isl_bool_error
;
529 isl_bool
isl_set_contains_point(__isl_keep isl_set
*set
,
530 __isl_keep isl_point
*point
)
532 return isl_map_contains_point(set_to_map(set
), point
);
535 __isl_give isl_basic_set
*isl_basic_set_from_point(__isl_take isl_point
*pnt
)
538 isl_basic_set
*model
;
543 model
= isl_basic_set_empty(isl_space_copy(pnt
->dim
));
544 bset
= isl_basic_set_from_vec(isl_vec_copy(pnt
->vec
));
545 bset
= isl_basic_set_from_underlying_set(bset
, model
);
551 __isl_give isl_set
*isl_set_from_point(__isl_take isl_point
*pnt
)
554 bset
= isl_basic_set_from_point(pnt
);
555 return isl_set_from_basic_set(bset
);
558 /* Construct a union set, containing the single element "pnt".
559 * If "pnt" is void, then return an empty union set.
561 __isl_give isl_union_set
*isl_union_set_from_point(__isl_take isl_point
*pnt
)
565 if (isl_point_is_void(pnt
)) {
568 space
= isl_point_get_space(pnt
);
570 return isl_union_set_empty(space
);
573 return isl_union_set_from_set(isl_set_from_point(pnt
));
576 __isl_give isl_basic_set
*isl_basic_set_box_from_points(
577 __isl_take isl_point
*pnt1
, __isl_take isl_point
*pnt2
)
579 isl_basic_set
*bset
= NULL
;
590 isl_assert(pnt1
->dim
->ctx
,
591 isl_space_is_equal(pnt1
->dim
, pnt2
->dim
), goto error
);
593 if (isl_point_is_void(pnt1
) && isl_point_is_void(pnt2
)) {
594 isl_space
*dim
= isl_space_copy(pnt1
->dim
);
595 isl_point_free(pnt1
);
596 isl_point_free(pnt2
);
598 return isl_basic_set_empty(dim
);
600 if (isl_point_is_void(pnt1
)) {
601 isl_point_free(pnt1
);
603 return isl_basic_set_from_point(pnt2
);
605 if (isl_point_is_void(pnt2
)) {
606 isl_point_free(pnt2
);
608 return isl_basic_set_from_point(pnt1
);
611 total
= isl_point_dim(pnt1
, isl_dim_all
);
612 bset
= isl_basic_set_alloc_space(isl_space_copy(pnt1
->dim
), 0, 0, 2 * total
);
614 for (i
= 0; i
< total
; ++i
) {
615 isl_int_mul(t
, pnt1
->vec
->el
[1 + i
], pnt2
->vec
->el
[0]);
616 isl_int_submul(t
, pnt2
->vec
->el
[1 + i
], pnt1
->vec
->el
[0]);
618 k
= isl_basic_set_alloc_inequality(bset
);
621 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
622 if (isl_int_is_pos(t
)) {
623 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
624 isl_int_set(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
626 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
627 isl_int_neg(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
629 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt1
->vec
->el
[0]);
631 k
= isl_basic_set_alloc_inequality(bset
);
634 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
635 if (isl_int_is_pos(t
)) {
636 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
637 isl_int_neg(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
639 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
640 isl_int_set(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
642 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt2
->vec
->el
[0]);
645 bset
= isl_basic_set_finalize(bset
);
647 isl_point_free(pnt1
);
648 isl_point_free(pnt2
);
654 isl_point_free(pnt1
);
655 isl_point_free(pnt2
);
657 isl_basic_set_free(bset
);
661 __isl_give isl_set
*isl_set_box_from_points(__isl_take isl_point
*pnt1
,
662 __isl_take isl_point
*pnt2
)
665 bset
= isl_basic_set_box_from_points(pnt1
, pnt2
);
666 return isl_set_from_basic_set(bset
);
669 /* Print the coordinate at position "pos" of the point "pnt".
671 static __isl_give isl_printer
*print_coordinate(__isl_take isl_printer
*p
,
672 struct isl_print_space_data
*data
, unsigned pos
)
674 isl_point
*pnt
= data
->user
;
676 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[1 + pos
]);
677 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
678 p
= isl_printer_print_str(p
, "/");
679 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[0]);
685 __isl_give isl_printer
*isl_printer_print_point(
686 __isl_take isl_printer
*p
, __isl_keep isl_point
*pnt
)
688 struct isl_print_space_data data
= { 0 };
694 if (isl_point_is_void(pnt
)) {
695 p
= isl_printer_print_str(p
, "void");
699 nparam
= isl_point_dim(pnt
, isl_dim_param
);
701 p
= isl_printer_print_str(p
, "[");
702 for (i
= 0; i
< nparam
; ++i
) {
705 p
= isl_printer_print_str(p
, ", ");
706 name
= isl_space_get_dim_name(pnt
->dim
, isl_dim_param
, i
);
708 p
= isl_printer_print_str(p
, name
);
709 p
= isl_printer_print_str(p
, " = ");
711 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[1 + i
]);
712 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
713 p
= isl_printer_print_str(p
, "/");
714 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[0]);
717 p
= isl_printer_print_str(p
, "]");
718 p
= isl_printer_print_str(p
, " -> ");
720 data
.print_dim
= &print_coordinate
;
722 p
= isl_printer_print_str(p
, "{ ");
723 p
= isl_print_space(pnt
->dim
, p
, 0, &data
);
724 p
= isl_printer_print_str(p
, " }");