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_val_private.h>
10 #include <isl_vec_private.h>
11 #include <isl_output_private.h>
13 #include <set_to_map.c>
15 isl_ctx
*isl_point_get_ctx(__isl_keep isl_point
*pnt
)
17 return pnt
? isl_space_get_ctx(pnt
->dim
) : NULL
;
20 /* Return the space of "pnt".
22 __isl_keep isl_space
*isl_point_peek_space(__isl_keep isl_point
*pnt
)
24 return pnt
? pnt
->dim
: NULL
;
27 __isl_give isl_space
*isl_point_get_space(__isl_keep isl_point
*pnt
)
29 return isl_space_copy(isl_point_peek_space(pnt
));
32 __isl_give isl_point
*isl_point_alloc(__isl_take isl_space
*dim
,
33 __isl_take isl_vec
*vec
)
35 struct isl_point
*pnt
;
40 if (vec
->size
> 1 + isl_space_dim(dim
, isl_dim_all
)) {
41 vec
= isl_vec_cow(vec
);
44 vec
->size
= 1 + isl_space_dim(dim
, isl_dim_all
);
47 pnt
= isl_alloc_type(dim
->ctx
, struct isl_point
);
62 __isl_give isl_point
*isl_point_zero(__isl_take isl_space
*dim
)
68 vec
= isl_vec_alloc(dim
->ctx
, 1 + isl_space_dim(dim
, isl_dim_all
));
71 isl_int_set_si(vec
->el
[0], 1);
72 isl_seq_clr(vec
->el
+ 1, vec
->size
- 1);
73 return isl_point_alloc(dim
, vec
);
79 __isl_give isl_point
*isl_point_dup(__isl_keep isl_point
*pnt
)
81 struct isl_point
*pnt2
;
85 pnt2
= isl_point_alloc(isl_space_copy(pnt
->dim
), isl_vec_copy(pnt
->vec
));
89 __isl_give isl_point
*isl_point_cow(__isl_take isl_point
*pnt
)
91 struct isl_point
*pnt2
;
98 pnt2
= isl_point_dup(pnt
);
103 __isl_give isl_point
*isl_point_copy(__isl_keep isl_point
*pnt
)
112 __isl_null isl_point
*isl_point_free(__isl_take isl_point
*pnt
)
120 isl_space_free(pnt
->dim
);
121 isl_vec_free(pnt
->vec
);
126 __isl_give isl_point
*isl_point_void(__isl_take isl_space
*dim
)
131 return isl_point_alloc(dim
, isl_vec_alloc(dim
->ctx
, 0));
134 isl_bool
isl_point_is_void(__isl_keep isl_point
*pnt
)
137 return isl_bool_error
;
139 return pnt
->vec
->size
== 0;
142 /* Return the space of "pnt".
143 * This may be either a copy or the space itself
144 * if there is only one reference to "pnt".
145 * This allows the space to be modified inplace
146 * if both the point and its space have only a single reference.
147 * The caller is not allowed to modify "pnt" between this call and
148 * a subsequent call to isl_point_restore_space.
149 * The only exception is that isl_point_free can be called instead.
151 __isl_give isl_space
*isl_point_take_space(__isl_keep isl_point
*pnt
)
158 return isl_point_get_space(pnt
);
164 /* Set the space of "pnt" to "space", where the space of "pnt" may be missing
165 * due to a preceding call to isl_point_take_space.
166 * However, in this case, "pnt" only has a single reference and
167 * then the call to isl_point_cow has no effect.
169 __isl_give isl_point
*isl_point_restore_space(__isl_take isl_point
*pnt
,
170 __isl_take isl_space
*space
)
175 if (pnt
->dim
== space
) {
176 isl_space_free(space
);
180 pnt
= isl_point_cow(pnt
);
183 isl_space_free(pnt
->dim
);
189 isl_space_free(space
);
193 /* Return the coordinate vector of "pnt".
195 __isl_keep isl_vec
*isl_point_peek_vec(__isl_keep isl_point
*pnt
)
197 return pnt
? pnt
->vec
: NULL
;
200 /* Return a copy of the coordinate vector of "pnt".
202 __isl_give isl_vec
*isl_point_get_vec(__isl_keep isl_point
*pnt
)
204 return isl_vec_copy(isl_point_peek_vec(pnt
));
207 /* Return the coordinate vector of "pnt".
208 * This may be either a copy or the coordinate vector itself
209 * if there is only one reference to "pnt".
210 * This allows the coordinate vector to be modified inplace
211 * if both the point and its coordinate vector have only a single reference.
212 * The caller is not allowed to modify "pnt" between this call and
213 * a subsequent call to isl_point_restore_vec.
214 * The only exception is that isl_point_free can be called instead.
216 __isl_give isl_vec
*isl_point_take_vec(__isl_keep isl_point
*pnt
)
223 return isl_point_get_vec(pnt
);
229 /* Set the coordinate vector of "pnt" to "vec",
230 * where the coordinate vector of "pnt" may be missing
231 * due to a preceding call to isl_point_take_vec.
232 * However, in this case, "pnt" only has a single reference and
233 * then the call to isl_point_cow has no effect.
235 __isl_give isl_point
*isl_point_restore_vec(__isl_take isl_point
*pnt
,
236 __isl_take isl_vec
*vec
)
241 if (pnt
->vec
== vec
) {
246 pnt
= isl_point_cow(pnt
);
249 isl_vec_free(pnt
->vec
);
259 /* Return the value of coordinate "pos" of type "type" of "pnt".
261 __isl_give isl_val
*isl_point_get_coordinate_val(__isl_keep isl_point
*pnt
,
262 enum isl_dim_type type
, int pos
)
270 ctx
= isl_point_get_ctx(pnt
);
271 if (isl_point_is_void(pnt
))
272 isl_die(ctx
, isl_error_invalid
,
273 "void point does not have coordinates", return NULL
);
274 if (pos
< 0 || pos
>= isl_space_dim(pnt
->dim
, type
))
275 isl_die(ctx
, isl_error_invalid
,
276 "position out of bounds", return NULL
);
278 if (type
== isl_dim_set
)
279 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
281 v
= isl_val_rat_from_isl_int(ctx
, pnt
->vec
->el
[1 + pos
],
283 return isl_val_normalize(v
);
286 /* Replace coordinate "pos" of type "type" of "pnt" by "v".
288 __isl_give isl_point
*isl_point_set_coordinate_val(__isl_take isl_point
*pnt
,
289 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
293 if (isl_point_is_void(pnt
))
294 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
295 "void point does not have coordinates", goto error
);
296 if (pos
< 0 || pos
>= isl_space_dim(pnt
->dim
, type
))
297 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
298 "position out of bounds", goto error
);
299 if (!isl_val_is_rat(v
))
300 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
301 "expecting rational value", goto error
);
303 if (isl_int_eq(pnt
->vec
->el
[1 + pos
], v
->n
) &&
304 isl_int_eq(pnt
->vec
->el
[0], v
->d
)) {
309 pnt
= isl_point_cow(pnt
);
312 pnt
->vec
= isl_vec_cow(pnt
->vec
);
316 if (isl_int_eq(pnt
->vec
->el
[0], v
->d
)) {
317 isl_int_set(pnt
->vec
->el
[1 + pos
], v
->n
);
318 } else if (isl_int_is_one(v
->d
)) {
319 isl_int_mul(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[0], v
->n
);
321 isl_seq_scale(pnt
->vec
->el
+ 1,
322 pnt
->vec
->el
+ 1, v
->d
, pnt
->vec
->size
- 1);
323 isl_int_mul(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[0], v
->n
);
324 isl_int_mul(pnt
->vec
->el
[0], pnt
->vec
->el
[0], v
->d
);
325 pnt
->vec
= isl_vec_normalize(pnt
->vec
);
338 __isl_give isl_point
*isl_point_add_ui(__isl_take isl_point
*pnt
,
339 enum isl_dim_type type
, int pos
, unsigned val
)
341 if (!pnt
|| isl_point_is_void(pnt
))
344 pnt
= isl_point_cow(pnt
);
347 pnt
->vec
= isl_vec_cow(pnt
->vec
);
351 if (type
== isl_dim_set
)
352 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
354 isl_int_add_ui(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[1 + pos
], val
);
362 __isl_give isl_point
*isl_point_sub_ui(__isl_take isl_point
*pnt
,
363 enum isl_dim_type type
, int pos
, unsigned val
)
365 if (!pnt
|| isl_point_is_void(pnt
))
368 pnt
= isl_point_cow(pnt
);
371 pnt
->vec
= isl_vec_cow(pnt
->vec
);
375 if (type
== isl_dim_set
)
376 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
378 isl_int_sub_ui(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[1 + pos
], val
);
386 struct isl_foreach_point
{
387 struct isl_scan_callback callback
;
388 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
);
393 static isl_stat
foreach_point(struct isl_scan_callback
*cb
,
394 __isl_take isl_vec
*sample
)
396 struct isl_foreach_point
*fp
= (struct isl_foreach_point
*)cb
;
399 pnt
= isl_point_alloc(isl_space_copy(fp
->dim
), sample
);
401 return fp
->fn(pnt
, fp
->user
);
404 isl_stat
isl_set_foreach_point(__isl_keep isl_set
*set
,
405 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
407 struct isl_foreach_point fp
= { { &foreach_point
}, fn
, user
};
411 return isl_stat_error
;
413 fp
.dim
= isl_set_get_space(set
);
415 return isl_stat_error
;
417 set
= isl_set_copy(set
);
418 set
= isl_set_cow(set
);
419 set
= isl_set_make_disjoint(set
);
420 set
= isl_set_compute_divs(set
);
424 for (i
= 0; i
< set
->n
; ++i
)
425 if (isl_basic_set_scan(isl_basic_set_copy(set
->p
[i
]),
430 isl_space_free(fp
.dim
);
435 isl_space_free(fp
.dim
);
436 return isl_stat_error
;
439 /* Return 1 if "bmap" contains the point "point".
440 * "bmap" is assumed to have known divs.
441 * The point is first extended with the divs and then passed
442 * to basic_map_contains.
444 isl_bool
isl_basic_map_contains_point(__isl_keep isl_basic_map
*bmap
,
445 __isl_keep isl_point
*point
)
453 return isl_bool_error
;
454 isl_assert(bmap
->ctx
, isl_space_is_equal(bmap
->dim
, point
->dim
),
455 return isl_bool_error
);
456 if (bmap
->n_div
== 0)
457 return isl_basic_map_contains(bmap
, point
->vec
);
459 dim
= isl_basic_map_total_dim(bmap
) - bmap
->n_div
;
460 vec
= isl_vec_alloc(bmap
->ctx
, 1 + dim
+ bmap
->n_div
);
462 return isl_bool_error
;
464 isl_seq_cpy(vec
->el
, point
->vec
->el
, point
->vec
->size
);
465 for (i
= 0; i
< bmap
->n_div
; ++i
) {
466 isl_seq_inner_product(bmap
->div
[i
] + 1, vec
->el
,
467 1 + dim
+ i
, &vec
->el
[1+dim
+i
]);
468 isl_int_fdiv_q(vec
->el
[1+dim
+i
], vec
->el
[1+dim
+i
],
472 contains
= isl_basic_map_contains(bmap
, vec
);
478 isl_bool
isl_map_contains_point(__isl_keep isl_map
*map
,
479 __isl_keep isl_point
*point
)
482 isl_bool found
= isl_bool_false
;
485 return isl_bool_error
;
487 map
= isl_map_copy(map
);
488 map
= isl_map_compute_divs(map
);
490 return isl_bool_error
;
492 for (i
= 0; i
< map
->n
; ++i
) {
493 found
= isl_basic_map_contains_point(map
->p
[i
], point
);
504 return isl_bool_error
;
507 isl_bool
isl_set_contains_point(__isl_keep isl_set
*set
,
508 __isl_keep isl_point
*point
)
510 return isl_map_contains_point(set_to_map(set
), point
);
513 __isl_give isl_basic_set
*isl_basic_set_from_point(__isl_take isl_point
*pnt
)
516 isl_basic_set
*model
;
521 model
= isl_basic_set_empty(isl_space_copy(pnt
->dim
));
522 bset
= isl_basic_set_from_vec(isl_vec_copy(pnt
->vec
));
523 bset
= isl_basic_set_from_underlying_set(bset
, model
);
529 __isl_give isl_set
*isl_set_from_point(__isl_take isl_point
*pnt
)
532 bset
= isl_basic_set_from_point(pnt
);
533 return isl_set_from_basic_set(bset
);
536 /* Construct a union set, containing the single element "pnt".
537 * If "pnt" is void, then return an empty union set.
539 __isl_give isl_union_set
*isl_union_set_from_point(__isl_take isl_point
*pnt
)
543 if (isl_point_is_void(pnt
)) {
546 space
= isl_point_get_space(pnt
);
548 return isl_union_set_empty(space
);
551 return isl_union_set_from_set(isl_set_from_point(pnt
));
554 __isl_give isl_basic_set
*isl_basic_set_box_from_points(
555 __isl_take isl_point
*pnt1
, __isl_take isl_point
*pnt2
)
557 isl_basic_set
*bset
= NULL
;
568 isl_assert(pnt1
->dim
->ctx
,
569 isl_space_is_equal(pnt1
->dim
, pnt2
->dim
), goto error
);
571 if (isl_point_is_void(pnt1
) && isl_point_is_void(pnt2
)) {
572 isl_space
*dim
= isl_space_copy(pnt1
->dim
);
573 isl_point_free(pnt1
);
574 isl_point_free(pnt2
);
576 return isl_basic_set_empty(dim
);
578 if (isl_point_is_void(pnt1
)) {
579 isl_point_free(pnt1
);
581 return isl_basic_set_from_point(pnt2
);
583 if (isl_point_is_void(pnt2
)) {
584 isl_point_free(pnt2
);
586 return isl_basic_set_from_point(pnt1
);
589 total
= isl_space_dim(pnt1
->dim
, isl_dim_all
);
590 bset
= isl_basic_set_alloc_space(isl_space_copy(pnt1
->dim
), 0, 0, 2 * total
);
592 for (i
= 0; i
< total
; ++i
) {
593 isl_int_mul(t
, pnt1
->vec
->el
[1 + i
], pnt2
->vec
->el
[0]);
594 isl_int_submul(t
, pnt2
->vec
->el
[1 + i
], pnt1
->vec
->el
[0]);
596 k
= isl_basic_set_alloc_inequality(bset
);
599 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
600 if (isl_int_is_pos(t
)) {
601 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
602 isl_int_set(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
604 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
605 isl_int_neg(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
607 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt1
->vec
->el
[0]);
609 k
= isl_basic_set_alloc_inequality(bset
);
612 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
613 if (isl_int_is_pos(t
)) {
614 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
615 isl_int_neg(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
617 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
618 isl_int_set(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
620 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt2
->vec
->el
[0]);
623 bset
= isl_basic_set_finalize(bset
);
625 isl_point_free(pnt1
);
626 isl_point_free(pnt2
);
632 isl_point_free(pnt1
);
633 isl_point_free(pnt2
);
635 isl_basic_set_free(bset
);
639 __isl_give isl_set
*isl_set_box_from_points(__isl_take isl_point
*pnt1
,
640 __isl_take isl_point
*pnt2
)
643 bset
= isl_basic_set_box_from_points(pnt1
, pnt2
);
644 return isl_set_from_basic_set(bset
);
647 /* Print the coordinate at position "pos" of the point "pnt".
649 static __isl_give isl_printer
*print_coordinate(__isl_take isl_printer
*p
,
650 struct isl_print_space_data
*data
, unsigned pos
)
652 isl_point
*pnt
= data
->user
;
654 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[1 + pos
]);
655 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
656 p
= isl_printer_print_str(p
, "/");
657 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[0]);
663 __isl_give isl_printer
*isl_printer_print_point(
664 __isl_take isl_printer
*p
, __isl_keep isl_point
*pnt
)
666 struct isl_print_space_data data
= { 0 };
672 if (isl_point_is_void(pnt
)) {
673 p
= isl_printer_print_str(p
, "void");
677 nparam
= isl_space_dim(pnt
->dim
, isl_dim_param
);
679 p
= isl_printer_print_str(p
, "[");
680 for (i
= 0; i
< nparam
; ++i
) {
683 p
= isl_printer_print_str(p
, ", ");
684 name
= isl_space_get_dim_name(pnt
->dim
, isl_dim_param
, i
);
686 p
= isl_printer_print_str(p
, name
);
687 p
= isl_printer_print_str(p
, " = ");
689 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[1 + i
]);
690 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
691 p
= isl_printer_print_str(p
, "/");
692 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[0]);
695 p
= isl_printer_print_str(p
, "]");
696 p
= isl_printer_print_str(p
, " -> ");
698 data
.print_dim
= &print_coordinate
;
700 p
= isl_printer_print_str(p
, "{ ");
701 p
= isl_print_space(pnt
->dim
, p
, 0, &data
);
702 p
= isl_printer_print_str(p
, " }");