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 void isl_point_free(__isl_take isl_point
*pnt
)
114 isl_space_free(pnt
->dim
);
115 isl_vec_free(pnt
->vec
);
119 __isl_give isl_point
*isl_point_void(__isl_take isl_space
*dim
)
124 return isl_point_alloc(dim
, isl_vec_alloc(dim
->ctx
, 0));
127 isl_bool
isl_point_is_void(__isl_keep isl_point
*pnt
)
130 return isl_bool_error
;
132 return pnt
->vec
->size
== 0;
135 int isl_point_get_coordinate(__isl_keep isl_point
*pnt
,
136 enum isl_dim_type type
, int pos
, isl_int
*v
)
138 if (!pnt
|| isl_point_is_void(pnt
))
141 if (pos
< 0 || pos
>= isl_space_dim(pnt
->dim
, type
))
142 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
143 "position out of bounds", return -1);
145 if (type
== isl_dim_set
)
146 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
147 isl_int_set(*v
, pnt
->vec
->el
[1 + pos
]);
152 /* Return the value of coordinate "pos" of type "type" of "pnt".
154 __isl_give isl_val
*isl_point_get_coordinate_val(__isl_keep isl_point
*pnt
,
155 enum isl_dim_type type
, int pos
)
163 ctx
= isl_point_get_ctx(pnt
);
164 if (isl_point_is_void(pnt
))
165 isl_die(ctx
, isl_error_invalid
,
166 "void point does not have coordinates", return NULL
);
167 if (pos
< 0 || pos
>= isl_space_dim(pnt
->dim
, type
))
168 isl_die(ctx
, isl_error_invalid
,
169 "position out of bounds", return NULL
);
171 if (type
== isl_dim_set
)
172 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
174 v
= isl_val_rat_from_isl_int(ctx
, pnt
->vec
->el
[1 + pos
],
176 return isl_val_normalize(v
);
179 __isl_give isl_point
*isl_point_set_coordinate(__isl_take isl_point
*pnt
,
180 enum isl_dim_type type
, int pos
, isl_int v
)
182 if (!pnt
|| isl_point_is_void(pnt
))
185 pnt
= isl_point_cow(pnt
);
188 pnt
->vec
= isl_vec_cow(pnt
->vec
);
192 if (type
== isl_dim_set
)
193 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
195 isl_int_set(pnt
->vec
->el
[1 + pos
], v
);
203 /* Replace coordinate "pos" of type "type" of "pnt" by "v".
205 __isl_give isl_point
*isl_point_set_coordinate_val(__isl_take isl_point
*pnt
,
206 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
210 if (isl_point_is_void(pnt
))
211 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
212 "void point does not have coordinates", goto error
);
213 if (pos
< 0 || pos
>= isl_space_dim(pnt
->dim
, type
))
214 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
215 "position out of bounds", goto error
);
216 if (!isl_val_is_rat(v
))
217 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
218 "expecting rational value", goto error
);
220 if (isl_int_eq(pnt
->vec
->el
[1 + pos
], v
->n
) &&
221 isl_int_eq(pnt
->vec
->el
[0], v
->d
)) {
226 pnt
= isl_point_cow(pnt
);
229 pnt
->vec
= isl_vec_cow(pnt
->vec
);
233 if (isl_int_eq(pnt
->vec
->el
[0], v
->d
)) {
234 isl_int_set(pnt
->vec
->el
[1 + pos
], v
->n
);
235 } else if (isl_int_is_one(v
->d
)) {
236 isl_int_mul(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[0], v
->n
);
238 isl_seq_scale(pnt
->vec
->el
+ 1,
239 pnt
->vec
->el
+ 1, v
->d
, pnt
->vec
->size
- 1);
240 isl_int_mul(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[0], v
->n
);
241 isl_int_mul(pnt
->vec
->el
[0], pnt
->vec
->el
[0], v
->d
);
242 pnt
->vec
= isl_vec_normalize(pnt
->vec
);
255 __isl_give isl_point
*isl_point_add_ui(__isl_take isl_point
*pnt
,
256 enum isl_dim_type type
, int pos
, unsigned val
)
258 if (!pnt
|| isl_point_is_void(pnt
))
261 pnt
= isl_point_cow(pnt
);
264 pnt
->vec
= isl_vec_cow(pnt
->vec
);
268 if (type
== isl_dim_set
)
269 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
271 isl_int_add_ui(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[1 + pos
], val
);
279 __isl_give isl_point
*isl_point_sub_ui(__isl_take isl_point
*pnt
,
280 enum isl_dim_type type
, int pos
, unsigned val
)
282 if (!pnt
|| isl_point_is_void(pnt
))
285 pnt
= isl_point_cow(pnt
);
288 pnt
->vec
= isl_vec_cow(pnt
->vec
);
292 if (type
== isl_dim_set
)
293 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
295 isl_int_sub_ui(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[1 + pos
], val
);
303 struct isl_foreach_point
{
304 struct isl_scan_callback callback
;
305 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
);
310 static isl_stat
foreach_point(struct isl_scan_callback
*cb
,
311 __isl_take isl_vec
*sample
)
313 struct isl_foreach_point
*fp
= (struct isl_foreach_point
*)cb
;
316 pnt
= isl_point_alloc(isl_space_copy(fp
->dim
), sample
);
318 return fp
->fn(pnt
, fp
->user
);
321 isl_stat
isl_set_foreach_point(__isl_keep isl_set
*set
,
322 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
324 struct isl_foreach_point fp
= { { &foreach_point
}, fn
, user
};
328 return isl_stat_error
;
330 fp
.dim
= isl_set_get_space(set
);
332 return isl_stat_error
;
334 set
= isl_set_copy(set
);
335 set
= isl_set_cow(set
);
336 set
= isl_set_make_disjoint(set
);
337 set
= isl_set_compute_divs(set
);
341 for (i
= 0; i
< set
->n
; ++i
)
342 if (isl_basic_set_scan(isl_basic_set_copy(set
->p
[i
]),
347 isl_space_free(fp
.dim
);
352 isl_space_free(fp
.dim
);
353 return isl_stat_error
;
356 /* Return 1 if "bmap" contains the point "point".
357 * "bmap" is assumed to have known divs.
358 * The point is first extended with the divs and then passed
359 * to basic_map_contains.
361 isl_bool
isl_basic_map_contains_point(__isl_keep isl_basic_map
*bmap
,
362 __isl_keep isl_point
*point
)
370 return isl_bool_error
;
371 isl_assert(bmap
->ctx
, isl_space_is_equal(bmap
->dim
, point
->dim
),
372 return isl_bool_error
);
373 if (bmap
->n_div
== 0)
374 return isl_basic_map_contains(bmap
, point
->vec
);
376 dim
= isl_basic_map_total_dim(bmap
) - bmap
->n_div
;
377 vec
= isl_vec_alloc(bmap
->ctx
, 1 + dim
+ bmap
->n_div
);
379 return isl_bool_error
;
381 isl_seq_cpy(vec
->el
, point
->vec
->el
, point
->vec
->size
);
382 for (i
= 0; i
< bmap
->n_div
; ++i
) {
383 isl_seq_inner_product(bmap
->div
[i
] + 1, vec
->el
,
384 1 + dim
+ i
, &vec
->el
[1+dim
+i
]);
385 isl_int_fdiv_q(vec
->el
[1+dim
+i
], vec
->el
[1+dim
+i
],
389 contains
= isl_basic_map_contains(bmap
, vec
);
395 int isl_map_contains_point(__isl_keep isl_map
*map
, __isl_keep isl_point
*point
)
403 map
= isl_map_copy(map
);
404 map
= isl_map_compute_divs(map
);
408 for (i
= 0; i
< map
->n
; ++i
) {
409 found
= isl_basic_map_contains_point(map
->p
[i
], point
);
423 isl_bool
isl_set_contains_point(__isl_keep isl_set
*set
,
424 __isl_keep isl_point
*point
)
426 return isl_map_contains_point(set_to_map(set
), point
);
429 __isl_give isl_basic_set
*isl_basic_set_from_point(__isl_take isl_point
*pnt
)
432 isl_basic_set
*model
;
437 model
= isl_basic_set_empty(isl_space_copy(pnt
->dim
));
438 bset
= isl_basic_set_from_vec(isl_vec_copy(pnt
->vec
));
439 bset
= isl_basic_set_from_underlying_set(bset
, model
);
445 __isl_give isl_set
*isl_set_from_point(__isl_take isl_point
*pnt
)
448 bset
= isl_basic_set_from_point(pnt
);
449 return isl_set_from_basic_set(bset
);
452 /* Construct a union set, containing the single element "pnt".
453 * If "pnt" is void, then return an empty union set.
455 __isl_give isl_union_set
*isl_union_set_from_point(__isl_take isl_point
*pnt
)
459 if (isl_point_is_void(pnt
)) {
462 space
= isl_point_get_space(pnt
);
464 return isl_union_set_empty(space
);
467 return isl_union_set_from_set(isl_set_from_point(pnt
));
470 __isl_give isl_basic_set
*isl_basic_set_box_from_points(
471 __isl_take isl_point
*pnt1
, __isl_take isl_point
*pnt2
)
484 isl_assert(pnt1
->dim
->ctx
,
485 isl_space_is_equal(pnt1
->dim
, pnt2
->dim
), goto error
);
487 if (isl_point_is_void(pnt1
) && isl_point_is_void(pnt2
)) {
488 isl_space
*dim
= isl_space_copy(pnt1
->dim
);
489 isl_point_free(pnt1
);
490 isl_point_free(pnt2
);
492 return isl_basic_set_empty(dim
);
494 if (isl_point_is_void(pnt1
)) {
495 isl_point_free(pnt1
);
497 return isl_basic_set_from_point(pnt2
);
499 if (isl_point_is_void(pnt2
)) {
500 isl_point_free(pnt2
);
502 return isl_basic_set_from_point(pnt1
);
505 total
= isl_space_dim(pnt1
->dim
, isl_dim_all
);
506 bset
= isl_basic_set_alloc_space(isl_space_copy(pnt1
->dim
), 0, 0, 2 * total
);
508 for (i
= 0; i
< total
; ++i
) {
509 isl_int_mul(t
, pnt1
->vec
->el
[1 + i
], pnt2
->vec
->el
[0]);
510 isl_int_submul(t
, pnt2
->vec
->el
[1 + i
], pnt1
->vec
->el
[0]);
512 k
= isl_basic_set_alloc_inequality(bset
);
515 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
516 if (isl_int_is_pos(t
)) {
517 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
518 isl_int_set(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
520 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
521 isl_int_neg(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
523 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt1
->vec
->el
[0]);
525 k
= isl_basic_set_alloc_inequality(bset
);
528 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
529 if (isl_int_is_pos(t
)) {
530 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
531 isl_int_neg(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
533 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
534 isl_int_set(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
536 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt2
->vec
->el
[0]);
539 bset
= isl_basic_set_finalize(bset
);
541 isl_point_free(pnt1
);
542 isl_point_free(pnt2
);
548 isl_point_free(pnt1
);
549 isl_point_free(pnt2
);
554 __isl_give isl_set
*isl_set_box_from_points(__isl_take isl_point
*pnt1
,
555 __isl_take isl_point
*pnt2
)
558 bset
= isl_basic_set_box_from_points(pnt1
, pnt2
);
559 return isl_set_from_basic_set(bset
);
562 /* Print the coordinate at position "pos" of the point "pnt".
564 static __isl_give isl_printer
*print_coordinate(__isl_take isl_printer
*p
,
565 struct isl_print_space_data
*data
, unsigned pos
)
567 isl_point
*pnt
= data
->user
;
569 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[1 + pos
]);
570 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
571 p
= isl_printer_print_str(p
, "/");
572 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[0]);
578 __isl_give isl_printer
*isl_printer_print_point(
579 __isl_take isl_printer
*p
, __isl_keep isl_point
*pnt
)
581 struct isl_print_space_data data
= { 0 };
588 if (isl_point_is_void(pnt
)) {
589 p
= isl_printer_print_str(p
, "void");
593 nparam
= isl_space_dim(pnt
->dim
, isl_dim_param
);
594 dim
= isl_space_dim(pnt
->dim
, isl_dim_set
);
596 p
= isl_printer_print_str(p
, "[");
597 for (i
= 0; i
< nparam
; ++i
) {
600 p
= isl_printer_print_str(p
, ", ");
601 name
= isl_space_get_dim_name(pnt
->dim
, isl_dim_param
, i
);
603 p
= isl_printer_print_str(p
, name
);
604 p
= isl_printer_print_str(p
, " = ");
606 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[1 + i
]);
607 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
608 p
= isl_printer_print_str(p
, "/");
609 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[0]);
612 p
= isl_printer_print_str(p
, "]");
613 p
= isl_printer_print_str(p
, " -> ");
615 data
.print_dim
= &print_coordinate
;
617 p
= isl_printer_print_str(p
, "{ ");
618 p
= isl_print_space(pnt
->dim
, p
, 0, &data
);
619 p
= isl_printer_print_str(p
, " }");