2 * Copyright 2010 INRIA Saclay
3 * Copyright 2013 Ecole Normale Superieure
4 * Copyright 2015 Sven Verdoolaege
5 * Copyright 2019 Cerebras Systems
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
13 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
16 #include <isl_map_private.h>
17 #include <isl_point_private.h>
19 #include <isl/union_set.h>
20 #include <isl_sample.h>
23 #include <isl_space_private.h>
24 #include <isl_local_private.h>
25 #include <isl_val_private.h>
26 #include <isl_vec_private.h>
27 #include <isl_output_private.h>
29 #include <set_to_map.c>
31 isl_ctx
*isl_point_get_ctx(__isl_keep isl_point
*pnt
)
33 return pnt
? isl_space_get_ctx(pnt
->dim
) : NULL
;
36 /* Return the space of "pnt".
38 __isl_keep isl_space
*isl_point_peek_space(__isl_keep isl_point
*pnt
)
40 return pnt
? pnt
->dim
: NULL
;
43 __isl_give isl_space
*isl_point_get_space(__isl_keep isl_point
*pnt
)
45 return isl_space_copy(isl_point_peek_space(pnt
));
49 #define TYPE1 isl_basic_map
51 #define TYPE2 isl_point
53 #define TYPE_PAIR isl_basic_map_point
56 #include "isl_type_has_equal_space_templ.c"
58 #include "isl_type_check_equal_space_templ.c"
60 __isl_give isl_point
*isl_point_alloc(__isl_take isl_space
*space
,
61 __isl_take isl_vec
*vec
)
63 struct isl_point
*pnt
;
66 dim
= isl_space_dim(space
, isl_dim_all
);
70 if (vec
->size
> 1 + dim
) {
71 vec
= isl_vec_cow(vec
);
77 pnt
= isl_alloc_type(space
->ctx
, struct isl_point
);
87 isl_space_free(space
);
92 __isl_give isl_point
*isl_point_zero(__isl_take isl_space
*space
)
97 dim
= isl_space_dim(space
, isl_dim_all
);
100 vec
= isl_vec_alloc(space
->ctx
, 1 + dim
);
103 isl_int_set_si(vec
->el
[0], 1);
104 isl_seq_clr(vec
->el
+ 1, vec
->size
- 1);
105 return isl_point_alloc(space
, vec
);
107 isl_space_free(space
);
111 __isl_give isl_point
*isl_point_dup(__isl_keep isl_point
*pnt
)
113 struct isl_point
*pnt2
;
117 pnt2
= isl_point_alloc(isl_space_copy(pnt
->dim
), isl_vec_copy(pnt
->vec
));
121 __isl_give isl_point
*isl_point_cow(__isl_take isl_point
*pnt
)
123 struct isl_point
*pnt2
;
130 pnt2
= isl_point_dup(pnt
);
135 __isl_give isl_point
*isl_point_copy(__isl_keep isl_point
*pnt
)
144 __isl_null isl_point
*isl_point_free(__isl_take isl_point
*pnt
)
152 isl_space_free(pnt
->dim
);
153 isl_vec_free(pnt
->vec
);
158 __isl_give isl_point
*isl_point_void(__isl_take isl_space
*space
)
163 return isl_point_alloc(space
, isl_vec_alloc(space
->ctx
, 0));
166 isl_bool
isl_point_is_void(__isl_keep isl_point
*pnt
)
169 return isl_bool_error
;
171 return isl_bool_ok(pnt
->vec
->size
== 0);
174 /* Return the space of "pnt".
175 * This may be either a copy or the space itself
176 * if there is only one reference to "pnt".
177 * This allows the space to be modified inplace
178 * if both the point and its space have only a single reference.
179 * The caller is not allowed to modify "pnt" between this call and
180 * a subsequent call to isl_point_restore_space.
181 * The only exception is that isl_point_free can be called instead.
183 __isl_give isl_space
*isl_point_take_space(__isl_keep isl_point
*pnt
)
190 return isl_point_get_space(pnt
);
196 /* Set the space of "pnt" to "space", where the space of "pnt" may be missing
197 * due to a preceding call to isl_point_take_space.
198 * However, in this case, "pnt" only has a single reference and
199 * then the call to isl_point_cow has no effect.
201 __isl_give isl_point
*isl_point_restore_space(__isl_take isl_point
*pnt
,
202 __isl_take isl_space
*space
)
207 if (pnt
->dim
== space
) {
208 isl_space_free(space
);
212 pnt
= isl_point_cow(pnt
);
215 isl_space_free(pnt
->dim
);
221 isl_space_free(space
);
225 /* Return the coordinate vector of "pnt".
227 __isl_keep isl_vec
*isl_point_peek_vec(__isl_keep isl_point
*pnt
)
229 return pnt
? pnt
->vec
: NULL
;
232 /* Return a copy of the coordinate vector of "pnt".
234 __isl_give isl_vec
*isl_point_get_vec(__isl_keep isl_point
*pnt
)
236 return isl_vec_copy(isl_point_peek_vec(pnt
));
239 /* Return the coordinate vector of "pnt".
240 * This may be either a copy or the coordinate vector itself
241 * if there is only one reference to "pnt".
242 * This allows the coordinate vector to be modified inplace
243 * if both the point and its coordinate vector have only a single reference.
244 * The caller is not allowed to modify "pnt" between this call and
245 * a subsequent call to isl_point_restore_vec.
246 * The only exception is that isl_point_free can be called instead.
248 __isl_give isl_vec
*isl_point_take_vec(__isl_keep isl_point
*pnt
)
255 return isl_point_get_vec(pnt
);
261 /* Set the coordinate vector of "pnt" to "vec",
262 * where the coordinate vector of "pnt" may be missing
263 * due to a preceding call to isl_point_take_vec.
264 * However, in this case, "pnt" only has a single reference and
265 * then the call to isl_point_cow has no effect.
267 __isl_give isl_point
*isl_point_restore_vec(__isl_take isl_point
*pnt
,
268 __isl_take isl_vec
*vec
)
273 if (pnt
->vec
== vec
) {
278 pnt
= isl_point_cow(pnt
);
281 isl_vec_free(pnt
->vec
);
291 /* Return the number of variables of the given type.
293 static isl_size
isl_point_dim(__isl_keep isl_point
*pnt
, enum isl_dim_type type
)
295 return isl_space_dim(isl_point_peek_space(pnt
), type
);
298 /* Return the position of the coordinates of the given type
299 * within the sequence of coordinates of "pnt".
301 static isl_size
isl_point_var_offset(__isl_keep isl_point
*pnt
,
302 enum isl_dim_type type
)
304 return pnt
? isl_space_offset(pnt
->dim
, type
) : isl_size_error
;
308 #define TYPE isl_point
310 #include "check_type_range_templ.c"
312 /* Return the value of coordinate "pos" of type "type" of "pnt".
314 __isl_give isl_val
*isl_point_get_coordinate_val(__isl_keep isl_point
*pnt
,
315 enum isl_dim_type type
, int pos
)
324 ctx
= isl_point_get_ctx(pnt
);
325 if (isl_point_is_void(pnt
))
326 isl_die(ctx
, isl_error_invalid
,
327 "void point does not have coordinates", return NULL
);
328 if (isl_point_check_range(pnt
, type
, pos
, 1) < 0)
331 off
= isl_point_var_offset(pnt
, type
);
336 v
= isl_val_rat_from_isl_int(ctx
, pnt
->vec
->el
[1 + pos
],
338 return isl_val_normalize(v
);
341 /* Set all entries of "mv" to NaN.
343 static __isl_give isl_multi_val
*set_nan(__isl_take isl_multi_val
*mv
)
349 n
= isl_multi_val_size(mv
);
351 return isl_multi_val_free(mv
);
352 v
= isl_val_nan(isl_multi_val_get_ctx(mv
));
353 for (i
= 0; i
< n
; ++i
)
354 mv
= isl_multi_val_set_at(mv
, i
, isl_val_copy(v
));
360 /* Return the values of the set dimensions of "pnt".
361 * Return a sequence of NaNs in case of a void point.
363 __isl_give isl_multi_val
*isl_point_get_multi_val(__isl_keep isl_point
*pnt
)
370 is_void
= isl_point_is_void(pnt
);
374 mv
= isl_multi_val_alloc(isl_point_get_space(pnt
));
377 n
= isl_multi_val_size(mv
);
379 return isl_multi_val_free(mv
);
380 for (i
= 0; i
< n
; ++i
) {
383 v
= isl_point_get_coordinate_val(pnt
, isl_dim_set
, i
);
384 mv
= isl_multi_val_set_at(mv
, i
, v
);
390 /* Replace coordinate "pos" of type "type" of "pnt" by "v".
392 __isl_give isl_point
*isl_point_set_coordinate_val(__isl_take isl_point
*pnt
,
393 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
397 if (isl_point_is_void(pnt
))
398 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
399 "void point does not have coordinates", goto error
);
400 if (isl_point_check_range(pnt
, type
, pos
, 1) < 0)
402 if (!isl_val_is_rat(v
))
403 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
404 "expecting rational value", goto error
);
406 pos
+= isl_space_offset(isl_point_peek_space(pnt
), type
);
407 if (isl_int_eq(pnt
->vec
->el
[1 + pos
], v
->n
) &&
408 isl_int_eq(pnt
->vec
->el
[0], v
->d
)) {
413 pnt
= isl_point_cow(pnt
);
416 pnt
->vec
= isl_vec_cow(pnt
->vec
);
420 if (isl_int_eq(pnt
->vec
->el
[0], v
->d
)) {
421 isl_int_set(pnt
->vec
->el
[1 + pos
], v
->n
);
422 } else if (isl_int_is_one(v
->d
)) {
423 isl_int_mul(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[0], v
->n
);
425 isl_seq_scale(pnt
->vec
->el
+ 1,
426 pnt
->vec
->el
+ 1, v
->d
, pnt
->vec
->size
- 1);
427 isl_int_mul(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[0], v
->n
);
428 isl_int_mul(pnt
->vec
->el
[0], pnt
->vec
->el
[0], v
->d
);
429 pnt
->vec
= isl_vec_normalize(pnt
->vec
);
442 __isl_give isl_point
*isl_point_add_ui(__isl_take isl_point
*pnt
,
443 enum isl_dim_type type
, int pos
, unsigned val
)
447 if (!pnt
|| isl_point_is_void(pnt
))
450 pnt
= isl_point_cow(pnt
);
453 pnt
->vec
= isl_vec_cow(pnt
->vec
);
457 off
= isl_point_var_offset(pnt
, type
);
462 isl_int_add_ui(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[1 + pos
], val
);
470 __isl_give isl_point
*isl_point_sub_ui(__isl_take isl_point
*pnt
,
471 enum isl_dim_type type
, int pos
, unsigned val
)
475 if (!pnt
|| isl_point_is_void(pnt
))
478 pnt
= isl_point_cow(pnt
);
481 pnt
->vec
= isl_vec_cow(pnt
->vec
);
485 off
= isl_point_var_offset(pnt
, type
);
490 isl_int_sub_ui(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[1 + pos
], val
);
498 struct isl_foreach_point
{
499 struct isl_scan_callback callback
;
500 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
);
505 static isl_stat
foreach_point(struct isl_scan_callback
*cb
,
506 __isl_take isl_vec
*sample
)
508 struct isl_foreach_point
*fp
= (struct isl_foreach_point
*)cb
;
511 pnt
= isl_point_alloc(isl_space_copy(fp
->dim
), sample
);
513 return fp
->fn(pnt
, fp
->user
);
516 isl_stat
isl_set_foreach_point(__isl_keep isl_set
*set
,
517 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
519 struct isl_foreach_point fp
= { { &foreach_point
}, fn
, user
};
523 return isl_stat_error
;
525 fp
.dim
= isl_set_get_space(set
);
527 return isl_stat_error
;
529 set
= isl_set_copy(set
);
530 set
= isl_set_cow(set
);
531 set
= isl_set_make_disjoint(set
);
532 set
= isl_set_compute_divs(set
);
536 for (i
= 0; i
< set
->n
; ++i
)
537 if (isl_basic_set_scan(isl_basic_set_copy(set
->p
[i
]),
542 isl_space_free(fp
.dim
);
547 isl_space_free(fp
.dim
);
548 return isl_stat_error
;
551 /* Return 1 if "bmap" contains the point "point".
552 * "bmap" is assumed to have known divs.
553 * The point is first extended with the divs and then passed
554 * to basic_map_contains.
556 isl_bool
isl_basic_map_contains_point(__isl_keep isl_basic_map
*bmap
,
557 __isl_keep isl_point
*point
)
563 if (isl_basic_map_point_check_equal_space(bmap
, point
) < 0)
564 return isl_bool_error
;
565 if (bmap
->n_div
== 0)
566 return isl_basic_map_contains(bmap
, point
->vec
);
568 local
= isl_local_alloc_from_mat(isl_basic_map_get_divs(bmap
));
569 vec
= isl_point_get_vec(point
);
570 vec
= isl_local_extend_point_vec(local
, vec
);
571 isl_local_free(local
);
573 contains
= isl_basic_map_contains(bmap
, vec
);
579 isl_bool
isl_map_contains_point(__isl_keep isl_map
*map
,
580 __isl_keep isl_point
*point
)
583 isl_bool found
= isl_bool_false
;
586 return isl_bool_error
;
588 map
= isl_map_copy(map
);
589 map
= isl_map_compute_divs(map
);
591 return isl_bool_error
;
593 for (i
= 0; i
< map
->n
; ++i
) {
594 found
= isl_basic_map_contains_point(map
->p
[i
], point
);
605 return isl_bool_error
;
608 isl_bool
isl_set_contains_point(__isl_keep isl_set
*set
,
609 __isl_keep isl_point
*point
)
611 return isl_map_contains_point(set_to_map(set
), point
);
614 __isl_give isl_basic_set
*isl_basic_set_from_point(__isl_take isl_point
*pnt
)
617 isl_basic_set
*model
;
622 model
= isl_basic_set_empty(isl_space_copy(pnt
->dim
));
623 bset
= isl_basic_set_from_vec(isl_vec_copy(pnt
->vec
));
624 bset
= isl_basic_set_from_underlying_set(bset
, model
);
630 __isl_give isl_set
*isl_set_from_point(__isl_take isl_point
*pnt
)
633 bset
= isl_basic_set_from_point(pnt
);
634 return isl_set_from_basic_set(bset
);
637 /* This function performs the same operation as isl_set_from_point,
638 * but is considered as a function on an isl_point when exported.
640 __isl_give isl_set
*isl_point_to_set(__isl_take isl_point
*pnt
)
642 return isl_set_from_point(pnt
);
645 /* Construct a union set, containing the single element "pnt".
646 * If "pnt" is void, then return an empty union set.
648 __isl_give isl_union_set
*isl_union_set_from_point(__isl_take isl_point
*pnt
)
652 if (isl_point_is_void(pnt
)) {
655 space
= isl_point_get_space(pnt
);
657 return isl_union_set_empty(space
);
660 return isl_union_set_from_set(isl_set_from_point(pnt
));
663 __isl_give isl_basic_set
*isl_basic_set_box_from_points(
664 __isl_take isl_point
*pnt1
, __isl_take isl_point
*pnt2
)
666 isl_basic_set
*bset
= NULL
;
677 isl_assert(pnt1
->dim
->ctx
,
678 isl_space_is_equal(pnt1
->dim
, pnt2
->dim
), goto error
);
680 if (isl_point_is_void(pnt1
) && isl_point_is_void(pnt2
)) {
681 isl_space
*space
= isl_space_copy(pnt1
->dim
);
682 isl_point_free(pnt1
);
683 isl_point_free(pnt2
);
685 return isl_basic_set_empty(space
);
687 if (isl_point_is_void(pnt1
)) {
688 isl_point_free(pnt1
);
690 return isl_basic_set_from_point(pnt2
);
692 if (isl_point_is_void(pnt2
)) {
693 isl_point_free(pnt2
);
695 return isl_basic_set_from_point(pnt1
);
698 total
= isl_point_dim(pnt1
, isl_dim_all
);
701 bset
= isl_basic_set_alloc_space(isl_space_copy(pnt1
->dim
), 0, 0, 2 * total
);
703 for (i
= 0; i
< total
; ++i
) {
704 isl_int_mul(t
, pnt1
->vec
->el
[1 + i
], pnt2
->vec
->el
[0]);
705 isl_int_submul(t
, pnt2
->vec
->el
[1 + i
], pnt1
->vec
->el
[0]);
707 k
= isl_basic_set_alloc_inequality(bset
);
710 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
711 if (isl_int_is_pos(t
)) {
712 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
713 isl_int_set(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
715 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
716 isl_int_neg(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
718 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt1
->vec
->el
[0]);
720 k
= isl_basic_set_alloc_inequality(bset
);
723 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
724 if (isl_int_is_pos(t
)) {
725 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
726 isl_int_neg(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
728 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
729 isl_int_set(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
731 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt2
->vec
->el
[0]);
734 bset
= isl_basic_set_finalize(bset
);
736 isl_point_free(pnt1
);
737 isl_point_free(pnt2
);
743 isl_point_free(pnt1
);
744 isl_point_free(pnt2
);
746 isl_basic_set_free(bset
);
750 __isl_give isl_set
*isl_set_box_from_points(__isl_take isl_point
*pnt1
,
751 __isl_take isl_point
*pnt2
)
754 bset
= isl_basic_set_box_from_points(pnt1
, pnt2
);
755 return isl_set_from_basic_set(bset
);
758 /* Print the coordinate at position "pos" of the point "pnt".
760 static __isl_give isl_printer
*print_coordinate(__isl_take isl_printer
*p
,
761 struct isl_print_space_data
*data
, unsigned pos
)
763 isl_point
*pnt
= data
->user
;
765 pos
+= isl_space_offset(data
->space
, data
->type
);
766 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[1 + pos
]);
767 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
768 p
= isl_printer_print_str(p
, "/");
769 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[0]);
775 __isl_give isl_printer
*isl_printer_print_point(
776 __isl_take isl_printer
*p
, __isl_keep isl_point
*pnt
)
778 struct isl_print_space_data data
= { 0 };
784 if (isl_point_is_void(pnt
)) {
785 p
= isl_printer_print_str(p
, "void");
789 nparam
= isl_point_dim(pnt
, isl_dim_param
);
791 return isl_printer_free(p
);
793 p
= isl_printer_print_str(p
, "[");
794 for (i
= 0; i
< nparam
; ++i
) {
797 p
= isl_printer_print_str(p
, ", ");
798 name
= isl_space_get_dim_name(pnt
->dim
, isl_dim_param
, i
);
800 p
= isl_printer_print_str(p
, name
);
801 p
= isl_printer_print_str(p
, " = ");
803 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[1 + i
]);
804 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
805 p
= isl_printer_print_str(p
, "/");
806 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[0]);
809 p
= isl_printer_print_str(p
, "]");
810 p
= isl_printer_print_str(p
, " -> ");
812 data
.print_dim
= &print_coordinate
;
814 p
= isl_printer_print_str(p
, "{ ");
815 p
= isl_print_space(pnt
->dim
, p
, 0, &data
);
816 p
= isl_printer_print_str(p
, " }");