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>
12 #include <isl/deprecated/point_int.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 __isl_give isl_space
*isl_point_get_space(__isl_keep isl_point
*pnt
)
23 return pnt
? isl_space_copy(pnt
->dim
) : NULL
;
26 __isl_give isl_point
*isl_point_alloc(__isl_take isl_space
*dim
,
27 __isl_take isl_vec
*vec
)
29 struct isl_point
*pnt
;
34 if (vec
->size
> 1 + isl_space_dim(dim
, isl_dim_all
)) {
35 vec
= isl_vec_cow(vec
);
38 vec
->size
= 1 + isl_space_dim(dim
, isl_dim_all
);
41 pnt
= isl_alloc_type(dim
->ctx
, struct isl_point
);
56 __isl_give isl_point
*isl_point_zero(__isl_take isl_space
*dim
)
62 vec
= isl_vec_alloc(dim
->ctx
, 1 + isl_space_dim(dim
, isl_dim_all
));
65 isl_int_set_si(vec
->el
[0], 1);
66 isl_seq_clr(vec
->el
+ 1, vec
->size
- 1);
67 return isl_point_alloc(dim
, vec
);
73 __isl_give isl_point
*isl_point_dup(__isl_keep isl_point
*pnt
)
75 struct isl_point
*pnt2
;
79 pnt2
= isl_point_alloc(isl_space_copy(pnt
->dim
), isl_vec_copy(pnt
->vec
));
83 __isl_give isl_point
*isl_point_cow(__isl_take isl_point
*pnt
)
85 struct isl_point
*pnt2
;
92 pnt2
= isl_point_dup(pnt
);
97 __isl_give isl_point
*isl_point_copy(__isl_keep isl_point
*pnt
)
106 __isl_null isl_point
*isl_point_free(__isl_take isl_point
*pnt
)
114 isl_space_free(pnt
->dim
);
115 isl_vec_free(pnt
->vec
);
120 __isl_give isl_point
*isl_point_void(__isl_take isl_space
*dim
)
125 return isl_point_alloc(dim
, isl_vec_alloc(dim
->ctx
, 0));
128 isl_bool
isl_point_is_void(__isl_keep isl_point
*pnt
)
131 return isl_bool_error
;
133 return pnt
->vec
->size
== 0;
136 int isl_point_get_coordinate(__isl_keep isl_point
*pnt
,
137 enum isl_dim_type type
, int pos
, isl_int
*v
)
139 if (!pnt
|| isl_point_is_void(pnt
))
142 if (pos
< 0 || pos
>= isl_space_dim(pnt
->dim
, type
))
143 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
144 "position out of bounds", return -1);
146 if (type
== isl_dim_set
)
147 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
148 isl_int_set(*v
, pnt
->vec
->el
[1 + pos
]);
153 /* Return the value of coordinate "pos" of type "type" of "pnt".
155 __isl_give isl_val
*isl_point_get_coordinate_val(__isl_keep isl_point
*pnt
,
156 enum isl_dim_type type
, int pos
)
164 ctx
= isl_point_get_ctx(pnt
);
165 if (isl_point_is_void(pnt
))
166 isl_die(ctx
, isl_error_invalid
,
167 "void point does not have coordinates", return NULL
);
168 if (pos
< 0 || pos
>= isl_space_dim(pnt
->dim
, type
))
169 isl_die(ctx
, isl_error_invalid
,
170 "position out of bounds", return NULL
);
172 if (type
== isl_dim_set
)
173 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
175 v
= isl_val_rat_from_isl_int(ctx
, pnt
->vec
->el
[1 + pos
],
177 return isl_val_normalize(v
);
180 __isl_give isl_point
*isl_point_set_coordinate(__isl_take isl_point
*pnt
,
181 enum isl_dim_type type
, int pos
, isl_int v
)
183 if (!pnt
|| isl_point_is_void(pnt
))
186 pnt
= isl_point_cow(pnt
);
189 pnt
->vec
= isl_vec_cow(pnt
->vec
);
193 if (type
== isl_dim_set
)
194 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
196 isl_int_set(pnt
->vec
->el
[1 + pos
], v
);
204 /* Replace coordinate "pos" of type "type" of "pnt" by "v".
206 __isl_give isl_point
*isl_point_set_coordinate_val(__isl_take isl_point
*pnt
,
207 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
211 if (isl_point_is_void(pnt
))
212 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
213 "void point does not have coordinates", goto error
);
214 if (pos
< 0 || pos
>= isl_space_dim(pnt
->dim
, type
))
215 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
216 "position out of bounds", goto error
);
217 if (!isl_val_is_rat(v
))
218 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
219 "expecting rational value", goto error
);
221 if (isl_int_eq(pnt
->vec
->el
[1 + pos
], v
->n
) &&
222 isl_int_eq(pnt
->vec
->el
[0], v
->d
)) {
227 pnt
= isl_point_cow(pnt
);
230 pnt
->vec
= isl_vec_cow(pnt
->vec
);
234 if (isl_int_eq(pnt
->vec
->el
[0], v
->d
)) {
235 isl_int_set(pnt
->vec
->el
[1 + pos
], v
->n
);
236 } else if (isl_int_is_one(v
->d
)) {
237 isl_int_mul(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[0], v
->n
);
239 isl_seq_scale(pnt
->vec
->el
+ 1,
240 pnt
->vec
->el
+ 1, v
->d
, pnt
->vec
->size
- 1);
241 isl_int_mul(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[0], v
->n
);
242 isl_int_mul(pnt
->vec
->el
[0], pnt
->vec
->el
[0], v
->d
);
243 pnt
->vec
= isl_vec_normalize(pnt
->vec
);
256 __isl_give isl_point
*isl_point_add_ui(__isl_take isl_point
*pnt
,
257 enum isl_dim_type type
, int pos
, unsigned val
)
259 if (!pnt
|| isl_point_is_void(pnt
))
262 pnt
= isl_point_cow(pnt
);
265 pnt
->vec
= isl_vec_cow(pnt
->vec
);
269 if (type
== isl_dim_set
)
270 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
272 isl_int_add_ui(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[1 + pos
], val
);
280 __isl_give isl_point
*isl_point_sub_ui(__isl_take isl_point
*pnt
,
281 enum isl_dim_type type
, int pos
, unsigned val
)
283 if (!pnt
|| isl_point_is_void(pnt
))
286 pnt
= isl_point_cow(pnt
);
289 pnt
->vec
= isl_vec_cow(pnt
->vec
);
293 if (type
== isl_dim_set
)
294 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
296 isl_int_sub_ui(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[1 + pos
], val
);
304 struct isl_foreach_point
{
305 struct isl_scan_callback callback
;
306 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
);
311 static isl_stat
foreach_point(struct isl_scan_callback
*cb
,
312 __isl_take isl_vec
*sample
)
314 struct isl_foreach_point
*fp
= (struct isl_foreach_point
*)cb
;
317 pnt
= isl_point_alloc(isl_space_copy(fp
->dim
), sample
);
319 return fp
->fn(pnt
, fp
->user
);
322 isl_stat
isl_set_foreach_point(__isl_keep isl_set
*set
,
323 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
325 struct isl_foreach_point fp
= { { &foreach_point
}, fn
, user
};
329 return isl_stat_error
;
331 fp
.dim
= isl_set_get_space(set
);
333 return isl_stat_error
;
335 set
= isl_set_copy(set
);
336 set
= isl_set_cow(set
);
337 set
= isl_set_make_disjoint(set
);
338 set
= isl_set_compute_divs(set
);
342 for (i
= 0; i
< set
->n
; ++i
)
343 if (isl_basic_set_scan(isl_basic_set_copy(set
->p
[i
]),
348 isl_space_free(fp
.dim
);
353 isl_space_free(fp
.dim
);
354 return isl_stat_error
;
357 /* Return 1 if "bmap" contains the point "point".
358 * "bmap" is assumed to have known divs.
359 * The point is first extended with the divs and then passed
360 * to basic_map_contains.
362 isl_bool
isl_basic_map_contains_point(__isl_keep isl_basic_map
*bmap
,
363 __isl_keep isl_point
*point
)
371 return isl_bool_error
;
372 isl_assert(bmap
->ctx
, isl_space_is_equal(bmap
->dim
, point
->dim
),
373 return isl_bool_error
);
374 if (bmap
->n_div
== 0)
375 return isl_basic_map_contains(bmap
, point
->vec
);
377 dim
= isl_basic_map_total_dim(bmap
) - bmap
->n_div
;
378 vec
= isl_vec_alloc(bmap
->ctx
, 1 + dim
+ bmap
->n_div
);
380 return isl_bool_error
;
382 isl_seq_cpy(vec
->el
, point
->vec
->el
, point
->vec
->size
);
383 for (i
= 0; i
< bmap
->n_div
; ++i
) {
384 isl_seq_inner_product(bmap
->div
[i
] + 1, vec
->el
,
385 1 + dim
+ i
, &vec
->el
[1+dim
+i
]);
386 isl_int_fdiv_q(vec
->el
[1+dim
+i
], vec
->el
[1+dim
+i
],
390 contains
= isl_basic_map_contains(bmap
, vec
);
396 isl_bool
isl_map_contains_point(__isl_keep isl_map
*map
,
397 __isl_keep isl_point
*point
)
400 isl_bool found
= isl_bool_false
;
403 return isl_bool_error
;
405 map
= isl_map_copy(map
);
406 map
= isl_map_compute_divs(map
);
408 return isl_bool_error
;
410 for (i
= 0; i
< map
->n
; ++i
) {
411 found
= isl_basic_map_contains_point(map
->p
[i
], point
);
422 return isl_bool_error
;
425 isl_bool
isl_set_contains_point(__isl_keep isl_set
*set
,
426 __isl_keep isl_point
*point
)
428 return isl_map_contains_point(set_to_map(set
), point
);
431 __isl_give isl_basic_set
*isl_basic_set_from_point(__isl_take isl_point
*pnt
)
434 isl_basic_set
*model
;
439 model
= isl_basic_set_empty(isl_space_copy(pnt
->dim
));
440 bset
= isl_basic_set_from_vec(isl_vec_copy(pnt
->vec
));
441 bset
= isl_basic_set_from_underlying_set(bset
, model
);
447 __isl_give isl_set
*isl_set_from_point(__isl_take isl_point
*pnt
)
450 bset
= isl_basic_set_from_point(pnt
);
451 return isl_set_from_basic_set(bset
);
454 /* Construct a union set, containing the single element "pnt".
455 * If "pnt" is void, then return an empty union set.
457 __isl_give isl_union_set
*isl_union_set_from_point(__isl_take isl_point
*pnt
)
461 if (isl_point_is_void(pnt
)) {
464 space
= isl_point_get_space(pnt
);
466 return isl_union_set_empty(space
);
469 return isl_union_set_from_set(isl_set_from_point(pnt
));
472 __isl_give isl_basic_set
*isl_basic_set_box_from_points(
473 __isl_take isl_point
*pnt1
, __isl_take isl_point
*pnt2
)
475 isl_basic_set
*bset
= NULL
;
486 isl_assert(pnt1
->dim
->ctx
,
487 isl_space_is_equal(pnt1
->dim
, pnt2
->dim
), goto error
);
489 if (isl_point_is_void(pnt1
) && isl_point_is_void(pnt2
)) {
490 isl_space
*dim
= isl_space_copy(pnt1
->dim
);
491 isl_point_free(pnt1
);
492 isl_point_free(pnt2
);
494 return isl_basic_set_empty(dim
);
496 if (isl_point_is_void(pnt1
)) {
497 isl_point_free(pnt1
);
499 return isl_basic_set_from_point(pnt2
);
501 if (isl_point_is_void(pnt2
)) {
502 isl_point_free(pnt2
);
504 return isl_basic_set_from_point(pnt1
);
507 total
= isl_space_dim(pnt1
->dim
, isl_dim_all
);
508 bset
= isl_basic_set_alloc_space(isl_space_copy(pnt1
->dim
), 0, 0, 2 * total
);
510 for (i
= 0; i
< total
; ++i
) {
511 isl_int_mul(t
, pnt1
->vec
->el
[1 + i
], pnt2
->vec
->el
[0]);
512 isl_int_submul(t
, pnt2
->vec
->el
[1 + i
], pnt1
->vec
->el
[0]);
514 k
= isl_basic_set_alloc_inequality(bset
);
517 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
518 if (isl_int_is_pos(t
)) {
519 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
520 isl_int_set(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
522 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
523 isl_int_neg(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
525 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt1
->vec
->el
[0]);
527 k
= isl_basic_set_alloc_inequality(bset
);
530 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
531 if (isl_int_is_pos(t
)) {
532 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
533 isl_int_neg(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
535 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
536 isl_int_set(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
538 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt2
->vec
->el
[0]);
541 bset
= isl_basic_set_finalize(bset
);
543 isl_point_free(pnt1
);
544 isl_point_free(pnt2
);
550 isl_point_free(pnt1
);
551 isl_point_free(pnt2
);
553 isl_basic_set_free(bset
);
557 __isl_give isl_set
*isl_set_box_from_points(__isl_take isl_point
*pnt1
,
558 __isl_take isl_point
*pnt2
)
561 bset
= isl_basic_set_box_from_points(pnt1
, pnt2
);
562 return isl_set_from_basic_set(bset
);
565 /* Print the coordinate at position "pos" of the point "pnt".
567 static __isl_give isl_printer
*print_coordinate(__isl_take isl_printer
*p
,
568 struct isl_print_space_data
*data
, unsigned pos
)
570 isl_point
*pnt
= data
->user
;
572 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[1 + pos
]);
573 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
574 p
= isl_printer_print_str(p
, "/");
575 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[0]);
581 __isl_give isl_printer
*isl_printer_print_point(
582 __isl_take isl_printer
*p
, __isl_keep isl_point
*pnt
)
584 struct isl_print_space_data data
= { 0 };
590 if (isl_point_is_void(pnt
)) {
591 p
= isl_printer_print_str(p
, "void");
595 nparam
= isl_space_dim(pnt
->dim
, isl_dim_param
);
597 p
= isl_printer_print_str(p
, "[");
598 for (i
= 0; i
< nparam
; ++i
) {
601 p
= isl_printer_print_str(p
, ", ");
602 name
= isl_space_get_dim_name(pnt
->dim
, isl_dim_param
, i
);
604 p
= isl_printer_print_str(p
, name
);
605 p
= isl_printer_print_str(p
, " = ");
607 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[1 + i
]);
608 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
609 p
= isl_printer_print_str(p
, "/");
610 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[0]);
613 p
= isl_printer_print_str(p
, "]");
614 p
= isl_printer_print_str(p
, " -> ");
616 data
.print_dim
= &print_coordinate
;
618 p
= isl_printer_print_str(p
, "{ ");
619 p
= isl_print_space(pnt
->dim
, p
, 0, &data
);
620 p
= isl_printer_print_str(p
, " }");