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 isl_bool
isl_point_is_void(__isl_keep isl_point
*pnt
)
126 return isl_bool_error
;
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 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
);
306 static isl_stat
foreach_point(struct isl_scan_callback
*cb
,
307 __isl_take isl_vec
*sample
)
309 struct isl_foreach_point
*fp
= (struct isl_foreach_point
*)cb
;
312 pnt
= isl_point_alloc(isl_space_copy(fp
->dim
), sample
);
314 return fp
->fn(pnt
, fp
->user
);
317 isl_stat
isl_set_foreach_point(__isl_keep isl_set
*set
,
318 isl_stat (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
320 struct isl_foreach_point fp
= { { &foreach_point
}, fn
, user
};
324 return isl_stat_error
;
326 fp
.dim
= isl_set_get_space(set
);
328 return isl_stat_error
;
330 set
= isl_set_copy(set
);
331 set
= isl_set_cow(set
);
332 set
= isl_set_make_disjoint(set
);
333 set
= isl_set_compute_divs(set
);
337 for (i
= 0; i
< set
->n
; ++i
)
338 if (isl_basic_set_scan(isl_basic_set_copy(set
->p
[i
]),
343 isl_space_free(fp
.dim
);
348 isl_space_free(fp
.dim
);
349 return isl_stat_error
;
352 /* Return 1 if "bmap" contains the point "point".
353 * "bmap" is assumed to have known divs.
354 * The point is first extended with the divs and then passed
355 * to basic_map_contains.
357 isl_bool
isl_basic_map_contains_point(__isl_keep isl_basic_map
*bmap
,
358 __isl_keep isl_point
*point
)
366 return isl_bool_error
;
367 isl_assert(bmap
->ctx
, isl_space_is_equal(bmap
->dim
, point
->dim
),
368 return isl_bool_error
);
369 if (bmap
->n_div
== 0)
370 return isl_basic_map_contains(bmap
, point
->vec
);
372 dim
= isl_basic_map_total_dim(bmap
) - bmap
->n_div
;
373 vec
= isl_vec_alloc(bmap
->ctx
, 1 + dim
+ bmap
->n_div
);
375 return isl_bool_error
;
377 isl_seq_cpy(vec
->el
, point
->vec
->el
, point
->vec
->size
);
378 for (i
= 0; i
< bmap
->n_div
; ++i
) {
379 isl_seq_inner_product(bmap
->div
[i
] + 1, vec
->el
,
380 1 + dim
+ i
, &vec
->el
[1+dim
+i
]);
381 isl_int_fdiv_q(vec
->el
[1+dim
+i
], vec
->el
[1+dim
+i
],
385 contains
= isl_basic_map_contains(bmap
, vec
);
391 int isl_map_contains_point(__isl_keep isl_map
*map
, __isl_keep isl_point
*point
)
399 map
= isl_map_copy(map
);
400 map
= isl_map_compute_divs(map
);
404 for (i
= 0; i
< map
->n
; ++i
) {
405 found
= isl_basic_map_contains_point(map
->p
[i
], point
);
419 isl_bool
isl_set_contains_point(__isl_keep isl_set
*set
,
420 __isl_keep isl_point
*point
)
422 return isl_map_contains_point((isl_map
*)set
, point
);
425 __isl_give isl_basic_set
*isl_basic_set_from_point(__isl_take isl_point
*pnt
)
428 isl_basic_set
*model
;
433 model
= isl_basic_set_empty(isl_space_copy(pnt
->dim
));
434 bset
= isl_basic_set_from_vec(isl_vec_copy(pnt
->vec
));
435 bset
= isl_basic_set_from_underlying_set(bset
, model
);
441 __isl_give isl_set
*isl_set_from_point(__isl_take isl_point
*pnt
)
444 bset
= isl_basic_set_from_point(pnt
);
445 return isl_set_from_basic_set(bset
);
448 __isl_give isl_basic_set
*isl_basic_set_box_from_points(
449 __isl_take isl_point
*pnt1
, __isl_take isl_point
*pnt2
)
462 isl_assert(pnt1
->dim
->ctx
,
463 isl_space_is_equal(pnt1
->dim
, pnt2
->dim
), goto error
);
465 if (isl_point_is_void(pnt1
) && isl_point_is_void(pnt2
)) {
466 isl_space
*dim
= isl_space_copy(pnt1
->dim
);
467 isl_point_free(pnt1
);
468 isl_point_free(pnt2
);
470 return isl_basic_set_empty(dim
);
472 if (isl_point_is_void(pnt1
)) {
473 isl_point_free(pnt1
);
475 return isl_basic_set_from_point(pnt2
);
477 if (isl_point_is_void(pnt2
)) {
478 isl_point_free(pnt2
);
480 return isl_basic_set_from_point(pnt1
);
483 total
= isl_space_dim(pnt1
->dim
, isl_dim_all
);
484 bset
= isl_basic_set_alloc_space(isl_space_copy(pnt1
->dim
), 0, 0, 2 * total
);
486 for (i
= 0; i
< total
; ++i
) {
487 isl_int_mul(t
, pnt1
->vec
->el
[1 + i
], pnt2
->vec
->el
[0]);
488 isl_int_submul(t
, pnt2
->vec
->el
[1 + i
], pnt1
->vec
->el
[0]);
490 k
= isl_basic_set_alloc_inequality(bset
);
493 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
494 if (isl_int_is_pos(t
)) {
495 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
496 isl_int_set(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
498 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
499 isl_int_neg(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
501 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt1
->vec
->el
[0]);
503 k
= isl_basic_set_alloc_inequality(bset
);
506 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
507 if (isl_int_is_pos(t
)) {
508 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
509 isl_int_neg(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
511 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
512 isl_int_set(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
514 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt2
->vec
->el
[0]);
517 bset
= isl_basic_set_finalize(bset
);
519 isl_point_free(pnt1
);
520 isl_point_free(pnt2
);
526 isl_point_free(pnt1
);
527 isl_point_free(pnt2
);
532 __isl_give isl_set
*isl_set_box_from_points(__isl_take isl_point
*pnt1
,
533 __isl_take isl_point
*pnt2
)
536 bset
= isl_basic_set_box_from_points(pnt1
, pnt2
);
537 return isl_set_from_basic_set(bset
);
540 __isl_give isl_printer
*isl_printer_print_point(
541 __isl_take isl_printer
*p
, __isl_keep isl_point
*pnt
)
549 if (isl_point_is_void(pnt
)) {
550 p
= isl_printer_print_str(p
, "void");
554 nparam
= isl_space_dim(pnt
->dim
, isl_dim_param
);
555 dim
= isl_space_dim(pnt
->dim
, isl_dim_set
);
557 p
= isl_printer_print_str(p
, "[");
558 for (i
= 0; i
< nparam
; ++i
) {
561 p
= isl_printer_print_str(p
, ", ");
562 name
= isl_space_get_dim_name(pnt
->dim
, isl_dim_param
, i
);
564 p
= isl_printer_print_str(p
, name
);
565 p
= isl_printer_print_str(p
, " = ");
567 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[1 + i
]);
568 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
569 p
= isl_printer_print_str(p
, "/");
570 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[0]);
573 p
= isl_printer_print_str(p
, "]");
574 p
= isl_printer_print_str(p
, " -> ");
576 p
= isl_printer_print_str(p
, "[");
577 for (i
= 0; i
< dim
; ++i
) {
579 p
= isl_printer_print_str(p
, ", ");
580 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[1 + nparam
+ i
]);
581 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
582 p
= isl_printer_print_str(p
, "/");
583 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[0]);
586 p
= isl_printer_print_str(p
, "]");