1 #include <isl_map_private.h>
2 #include <isl_point_private.h>
4 #include <isl_sample.h>
7 #include <isl_space_private.h>
8 #include <isl_val_private.h>
9 #include <isl_vec_private.h>
10 #include <isl/deprecated/point_int.h>
12 isl_ctx
*isl_point_get_ctx(__isl_keep isl_point
*pnt
)
14 return pnt
? isl_space_get_ctx(pnt
->dim
) : NULL
;
17 __isl_give isl_space
*isl_point_get_space(__isl_keep isl_point
*pnt
)
19 return pnt
? isl_space_copy(pnt
->dim
) : NULL
;
22 __isl_give isl_point
*isl_point_alloc(__isl_take isl_space
*dim
,
23 __isl_take isl_vec
*vec
)
25 struct isl_point
*pnt
;
30 if (vec
->size
> 1 + isl_space_dim(dim
, isl_dim_all
)) {
31 vec
= isl_vec_cow(vec
);
34 vec
->size
= 1 + isl_space_dim(dim
, isl_dim_all
);
37 pnt
= isl_alloc_type(dim
->ctx
, struct isl_point
);
52 __isl_give isl_point
*isl_point_zero(__isl_take isl_space
*dim
)
58 vec
= isl_vec_alloc(dim
->ctx
, 1 + isl_space_dim(dim
, isl_dim_all
));
61 isl_int_set_si(vec
->el
[0], 1);
62 isl_seq_clr(vec
->el
+ 1, vec
->size
- 1);
63 return isl_point_alloc(dim
, vec
);
69 __isl_give isl_point
*isl_point_dup(__isl_keep isl_point
*pnt
)
71 struct isl_point
*pnt2
;
75 pnt2
= isl_point_alloc(isl_space_copy(pnt
->dim
), isl_vec_copy(pnt
->vec
));
79 __isl_give isl_point
*isl_point_cow(__isl_take isl_point
*pnt
)
81 struct isl_point
*pnt2
;
88 pnt2
= isl_point_dup(pnt
);
93 __isl_give isl_point
*isl_point_copy(__isl_keep isl_point
*pnt
)
102 void isl_point_free(__isl_take isl_point
*pnt
)
110 isl_space_free(pnt
->dim
);
111 isl_vec_free(pnt
->vec
);
115 __isl_give isl_point
*isl_point_void(__isl_take isl_space
*dim
)
120 return isl_point_alloc(dim
, isl_vec_alloc(dim
->ctx
, 0));
123 int isl_point_is_void(__isl_keep isl_point
*pnt
)
128 return pnt
->vec
->size
== 0;
131 int isl_point_get_coordinate(__isl_keep isl_point
*pnt
,
132 enum isl_dim_type type
, int pos
, isl_int
*v
)
134 if (!pnt
|| isl_point_is_void(pnt
))
137 if (pos
< 0 || pos
>= isl_space_dim(pnt
->dim
, type
))
138 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
139 "position out of bounds", return -1);
141 if (type
== isl_dim_set
)
142 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
143 isl_int_set(*v
, pnt
->vec
->el
[1 + pos
]);
148 /* Return the value of coordinate "pos" of type "type" of "pnt".
150 __isl_give isl_val
*isl_point_get_coordinate_val(__isl_keep isl_point
*pnt
,
151 enum isl_dim_type type
, int pos
)
159 ctx
= isl_point_get_ctx(pnt
);
160 if (isl_point_is_void(pnt
))
161 isl_die(ctx
, isl_error_invalid
,
162 "void point does not have coordinates", return NULL
);
163 if (pos
< 0 || pos
>= isl_space_dim(pnt
->dim
, type
))
164 isl_die(ctx
, isl_error_invalid
,
165 "position out of bounds", return NULL
);
167 if (type
== isl_dim_set
)
168 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
170 v
= isl_val_rat_from_isl_int(ctx
, pnt
->vec
->el
[1 + pos
],
172 return isl_val_normalize(v
);
175 __isl_give isl_point
*isl_point_set_coordinate(__isl_take isl_point
*pnt
,
176 enum isl_dim_type type
, int pos
, isl_int v
)
178 if (!pnt
|| isl_point_is_void(pnt
))
181 pnt
= isl_point_cow(pnt
);
184 pnt
->vec
= isl_vec_cow(pnt
->vec
);
188 if (type
== isl_dim_set
)
189 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
191 isl_int_set(pnt
->vec
->el
[1 + pos
], v
);
199 /* Replace coordinate "pos" of type "type" of "pnt" by "v".
201 __isl_give isl_point
*isl_point_set_coordinate_val(__isl_take isl_point
*pnt
,
202 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
206 if (isl_point_is_void(pnt
))
207 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
208 "void point does not have coordinates", goto error
);
209 if (pos
< 0 || pos
>= isl_space_dim(pnt
->dim
, type
))
210 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
211 "position out of bounds", goto error
);
212 if (!isl_val_is_rat(v
))
213 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
214 "expecting rational value", goto error
);
216 if (isl_int_eq(pnt
->vec
->el
[1 + pos
], v
->n
) &&
217 isl_int_eq(pnt
->vec
->el
[0], v
->d
)) {
222 pnt
= isl_point_cow(pnt
);
225 pnt
->vec
= isl_vec_cow(pnt
->vec
);
229 if (isl_int_eq(pnt
->vec
->el
[0], v
->d
)) {
230 isl_int_set(pnt
->vec
->el
[1 + pos
], v
->n
);
231 } else if (isl_int_is_one(v
->d
)) {
232 isl_int_mul(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[0], v
->n
);
234 isl_seq_scale(pnt
->vec
->el
+ 1,
235 pnt
->vec
->el
+ 1, v
->d
, pnt
->vec
->size
- 1);
236 isl_int_mul(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[0], v
->n
);
237 isl_int_mul(pnt
->vec
->el
[0], pnt
->vec
->el
[0], v
->d
);
238 pnt
->vec
= isl_vec_normalize(pnt
->vec
);
251 __isl_give isl_point
*isl_point_add_ui(__isl_take isl_point
*pnt
,
252 enum isl_dim_type type
, int pos
, unsigned val
)
254 if (!pnt
|| isl_point_is_void(pnt
))
257 pnt
= isl_point_cow(pnt
);
260 pnt
->vec
= isl_vec_cow(pnt
->vec
);
264 if (type
== isl_dim_set
)
265 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
267 isl_int_add_ui(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[1 + pos
], val
);
275 __isl_give isl_point
*isl_point_sub_ui(__isl_take isl_point
*pnt
,
276 enum isl_dim_type type
, int pos
, unsigned val
)
278 if (!pnt
|| isl_point_is_void(pnt
))
281 pnt
= isl_point_cow(pnt
);
284 pnt
->vec
= isl_vec_cow(pnt
->vec
);
288 if (type
== isl_dim_set
)
289 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
291 isl_int_sub_ui(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[1 + pos
], val
);
299 struct isl_foreach_point
{
300 struct isl_scan_callback callback
;
301 int (*fn
)(__isl_take isl_point
*pnt
, void *user
);
306 static int foreach_point(struct isl_scan_callback
*cb
, __isl_take isl_vec
*sample
)
308 struct isl_foreach_point
*fp
= (struct isl_foreach_point
*)cb
;
311 pnt
= isl_point_alloc(isl_space_copy(fp
->dim
), sample
);
313 return fp
->fn(pnt
, fp
->user
);
316 int isl_set_foreach_point(__isl_keep isl_set
*set
,
317 int (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
319 struct isl_foreach_point fp
= { { &foreach_point
}, fn
, user
};
325 fp
.dim
= isl_set_get_space(set
);
329 set
= isl_set_copy(set
);
330 set
= isl_set_cow(set
);
331 set
= isl_set_make_disjoint(set
);
332 set
= isl_set_compute_divs(set
);
336 for (i
= 0; i
< set
->n
; ++i
)
337 if (isl_basic_set_scan(isl_basic_set_copy(set
->p
[i
]),
342 isl_space_free(fp
.dim
);
347 isl_space_free(fp
.dim
);
351 /* Return 1 if "bmap" contains the point "point".
352 * "bmap" is assumed to have known divs.
353 * The point is first extended with the divs and then passed
354 * to basic_map_contains.
356 int isl_basic_map_contains_point(__isl_keep isl_basic_map
*bmap
,
357 __isl_keep isl_point
*point
)
366 isl_assert(bmap
->ctx
, isl_space_is_equal(bmap
->dim
, point
->dim
), return -1);
367 if (bmap
->n_div
== 0)
368 return isl_basic_map_contains(bmap
, point
->vec
);
370 dim
= isl_basic_map_total_dim(bmap
) - bmap
->n_div
;
371 vec
= isl_vec_alloc(bmap
->ctx
, 1 + dim
+ bmap
->n_div
);
375 isl_seq_cpy(vec
->el
, point
->vec
->el
, point
->vec
->size
);
376 for (i
= 0; i
< bmap
->n_div
; ++i
) {
377 isl_seq_inner_product(bmap
->div
[i
] + 1, vec
->el
,
378 1 + dim
+ i
, &vec
->el
[1+dim
+i
]);
379 isl_int_fdiv_q(vec
->el
[1+dim
+i
], vec
->el
[1+dim
+i
],
383 contains
= isl_basic_map_contains(bmap
, vec
);
389 int isl_map_contains_point(__isl_keep isl_map
*map
, __isl_keep isl_point
*point
)
397 map
= isl_map_copy(map
);
398 map
= isl_map_compute_divs(map
);
402 for (i
= 0; i
< map
->n
; ++i
) {
403 found
= isl_basic_map_contains_point(map
->p
[i
], point
);
417 int isl_set_contains_point(__isl_keep isl_set
*set
, __isl_keep isl_point
*point
)
419 return isl_map_contains_point((isl_map
*)set
, point
);
422 __isl_give isl_basic_set
*isl_basic_set_from_point(__isl_take isl_point
*pnt
)
425 isl_basic_set
*model
;
430 model
= isl_basic_set_empty(isl_space_copy(pnt
->dim
));
431 bset
= isl_basic_set_from_vec(isl_vec_copy(pnt
->vec
));
432 bset
= isl_basic_set_from_underlying_set(bset
, model
);
438 __isl_give isl_set
*isl_set_from_point(__isl_take isl_point
*pnt
)
441 bset
= isl_basic_set_from_point(pnt
);
442 return isl_set_from_basic_set(bset
);
445 __isl_give isl_basic_set
*isl_basic_set_box_from_points(
446 __isl_take isl_point
*pnt1
, __isl_take isl_point
*pnt2
)
459 isl_assert(pnt1
->dim
->ctx
,
460 isl_space_is_equal(pnt1
->dim
, pnt2
->dim
), goto error
);
462 if (isl_point_is_void(pnt1
) && isl_point_is_void(pnt2
)) {
463 isl_space
*dim
= isl_space_copy(pnt1
->dim
);
464 isl_point_free(pnt1
);
465 isl_point_free(pnt2
);
467 return isl_basic_set_empty(dim
);
469 if (isl_point_is_void(pnt1
)) {
470 isl_point_free(pnt1
);
472 return isl_basic_set_from_point(pnt2
);
474 if (isl_point_is_void(pnt2
)) {
475 isl_point_free(pnt2
);
477 return isl_basic_set_from_point(pnt1
);
480 total
= isl_space_dim(pnt1
->dim
, isl_dim_all
);
481 bset
= isl_basic_set_alloc_space(isl_space_copy(pnt1
->dim
), 0, 0, 2 * total
);
483 for (i
= 0; i
< total
; ++i
) {
484 isl_int_mul(t
, pnt1
->vec
->el
[1 + i
], pnt2
->vec
->el
[0]);
485 isl_int_submul(t
, pnt2
->vec
->el
[1 + i
], pnt1
->vec
->el
[0]);
487 k
= isl_basic_set_alloc_inequality(bset
);
490 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
491 if (isl_int_is_pos(t
)) {
492 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
493 isl_int_set(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
495 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
496 isl_int_neg(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
498 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt1
->vec
->el
[0]);
500 k
= isl_basic_set_alloc_inequality(bset
);
503 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
504 if (isl_int_is_pos(t
)) {
505 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
506 isl_int_neg(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
508 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
509 isl_int_set(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
511 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt2
->vec
->el
[0]);
514 bset
= isl_basic_set_finalize(bset
);
516 isl_point_free(pnt1
);
517 isl_point_free(pnt2
);
523 isl_point_free(pnt1
);
524 isl_point_free(pnt2
);
529 __isl_give isl_set
*isl_set_box_from_points(__isl_take isl_point
*pnt1
,
530 __isl_take isl_point
*pnt2
)
533 bset
= isl_basic_set_box_from_points(pnt1
, pnt2
);
534 return isl_set_from_basic_set(bset
);
537 __isl_give isl_printer
*isl_printer_print_point(
538 __isl_take isl_printer
*p
, __isl_keep isl_point
*pnt
)
546 if (isl_point_is_void(pnt
)) {
547 p
= isl_printer_print_str(p
, "void");
551 nparam
= isl_space_dim(pnt
->dim
, isl_dim_param
);
552 dim
= isl_space_dim(pnt
->dim
, isl_dim_set
);
554 p
= isl_printer_print_str(p
, "[");
555 for (i
= 0; i
< nparam
; ++i
) {
558 p
= isl_printer_print_str(p
, ", ");
559 name
= isl_space_get_dim_name(pnt
->dim
, isl_dim_param
, i
);
561 p
= isl_printer_print_str(p
, name
);
562 p
= isl_printer_print_str(p
, " = ");
564 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[1 + i
]);
565 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
566 p
= isl_printer_print_str(p
, "/");
567 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[0]);
570 p
= isl_printer_print_str(p
, "]");
571 p
= isl_printer_print_str(p
, " -> ");
573 p
= isl_printer_print_str(p
, "[");
574 for (i
= 0; i
< dim
; ++i
) {
576 p
= isl_printer_print_str(p
, ", ");
577 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[1 + nparam
+ i
]);
578 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
579 p
= isl_printer_print_str(p
, "/");
580 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[0]);
583 p
= isl_printer_print_str(p
, "]");