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>
10 isl_ctx
*isl_point_get_ctx(__isl_keep isl_point
*pnt
)
12 return pnt
? isl_space_get_ctx(pnt
->dim
) : NULL
;
15 __isl_give isl_space
*isl_point_get_space(__isl_keep isl_point
*pnt
)
17 return pnt
? isl_space_copy(pnt
->dim
) : NULL
;
20 __isl_give isl_point
*isl_point_alloc(__isl_take isl_space
*dim
,
21 __isl_take isl_vec
*vec
)
23 struct isl_point
*pnt
;
28 if (vec
->size
> 1 + isl_space_dim(dim
, isl_dim_all
)) {
29 vec
= isl_vec_cow(vec
);
32 vec
->size
= 1 + isl_space_dim(dim
, isl_dim_all
);
35 pnt
= isl_alloc_type(dim
->ctx
, struct isl_point
);
50 __isl_give isl_point
*isl_point_zero(__isl_take isl_space
*dim
)
56 vec
= isl_vec_alloc(dim
->ctx
, 1 + isl_space_dim(dim
, isl_dim_all
));
59 isl_int_set_si(vec
->el
[0], 1);
60 isl_seq_clr(vec
->el
+ 1, vec
->size
- 1);
61 return isl_point_alloc(dim
, vec
);
67 __isl_give isl_point
*isl_point_dup(__isl_keep isl_point
*pnt
)
69 struct isl_point
*pnt2
;
73 pnt2
= isl_point_alloc(isl_space_copy(pnt
->dim
), isl_vec_copy(pnt
->vec
));
77 __isl_give isl_point
*isl_point_cow(__isl_take isl_point
*pnt
)
79 struct isl_point
*pnt2
;
86 pnt2
= isl_point_dup(pnt
);
91 __isl_give isl_point
*isl_point_copy(__isl_keep isl_point
*pnt
)
100 void isl_point_free(__isl_take isl_point
*pnt
)
108 isl_space_free(pnt
->dim
);
109 isl_vec_free(pnt
->vec
);
113 __isl_give isl_point
*isl_point_void(__isl_take isl_space
*dim
)
118 return isl_point_alloc(dim
, isl_vec_alloc(dim
->ctx
, 0));
121 int isl_point_is_void(__isl_keep isl_point
*pnt
)
126 return pnt
->vec
->size
== 0;
129 int isl_point_get_coordinate(__isl_keep isl_point
*pnt
,
130 enum isl_dim_type type
, int pos
, isl_int
*v
)
132 if (!pnt
|| isl_point_is_void(pnt
))
135 if (pos
< 0 || pos
>= isl_space_dim(pnt
->dim
, type
))
136 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
137 "position out of bounds", return -1);
139 if (type
== isl_dim_set
)
140 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
141 isl_int_set(*v
, pnt
->vec
->el
[1 + pos
]);
146 /* Return the value of coordinate "pos" of type "type" of "pnt".
148 __isl_give isl_val
*isl_point_get_coordinate_val(__isl_keep isl_point
*pnt
,
149 enum isl_dim_type type
, int pos
)
157 ctx
= isl_point_get_ctx(pnt
);
158 if (isl_point_is_void(pnt
))
159 isl_die(ctx
, isl_error_invalid
,
160 "void point does not have coordinates", return NULL
);
161 if (pos
< 0 || pos
>= isl_space_dim(pnt
->dim
, type
))
162 isl_die(ctx
, isl_error_invalid
,
163 "position out of bounds", return NULL
);
165 if (type
== isl_dim_set
)
166 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
168 v
= isl_val_rat_from_isl_int(ctx
, pnt
->vec
->el
[1 + pos
],
170 return isl_val_normalize(v
);
173 __isl_give isl_point
*isl_point_set_coordinate(__isl_take isl_point
*pnt
,
174 enum isl_dim_type type
, int pos
, isl_int v
)
176 if (!pnt
|| isl_point_is_void(pnt
))
179 pnt
= isl_point_cow(pnt
);
182 pnt
->vec
= isl_vec_cow(pnt
->vec
);
186 if (type
== isl_dim_set
)
187 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
189 isl_int_set(pnt
->vec
->el
[1 + pos
], v
);
197 /* Replace coordinate "pos" of type "type" of "pnt" by "v".
199 __isl_give isl_point
*isl_point_set_coordinate_val(__isl_take isl_point
*pnt
,
200 enum isl_dim_type type
, int pos
, __isl_take isl_val
*v
)
204 if (isl_point_is_void(pnt
))
205 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
206 "void point does not have coordinates", goto error
);
207 if (pos
< 0 || pos
>= isl_space_dim(pnt
->dim
, type
))
208 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
209 "position out of bounds", goto error
);
210 if (!isl_val_is_rat(v
))
211 isl_die(isl_point_get_ctx(pnt
), isl_error_invalid
,
212 "expecting rational value", goto error
);
214 if (isl_int_eq(pnt
->vec
->el
[1 + pos
], v
->n
) &&
215 isl_int_eq(pnt
->vec
->el
[0], v
->d
)) {
220 pnt
= isl_point_cow(pnt
);
223 pnt
->vec
= isl_vec_cow(pnt
->vec
);
227 if (isl_int_eq(pnt
->vec
->el
[0], v
->d
)) {
228 isl_int_set(pnt
->vec
->el
[1 + pos
], v
->n
);
229 } else if (isl_int_is_one(v
->d
)) {
230 isl_int_mul(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[0], v
->n
);
232 isl_seq_scale(pnt
->vec
->el
+ 1,
233 pnt
->vec
->el
+ 1, v
->d
, pnt
->vec
->size
- 1);
234 isl_int_mul(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[0], v
->n
);
235 isl_int_mul(pnt
->vec
->el
[0], pnt
->vec
->el
[0], v
->d
);
236 pnt
->vec
= isl_vec_normalize(pnt
->vec
);
249 __isl_give isl_point
*isl_point_add_ui(__isl_take isl_point
*pnt
,
250 enum isl_dim_type type
, int pos
, unsigned val
)
252 if (!pnt
|| isl_point_is_void(pnt
))
255 pnt
= isl_point_cow(pnt
);
258 pnt
->vec
= isl_vec_cow(pnt
->vec
);
262 if (type
== isl_dim_set
)
263 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
265 isl_int_add_ui(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[1 + pos
], val
);
273 __isl_give isl_point
*isl_point_sub_ui(__isl_take isl_point
*pnt
,
274 enum isl_dim_type type
, int pos
, unsigned val
)
276 if (!pnt
|| isl_point_is_void(pnt
))
279 pnt
= isl_point_cow(pnt
);
282 pnt
->vec
= isl_vec_cow(pnt
->vec
);
286 if (type
== isl_dim_set
)
287 pos
+= isl_space_dim(pnt
->dim
, isl_dim_param
);
289 isl_int_sub_ui(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[1 + pos
], val
);
297 struct isl_foreach_point
{
298 struct isl_scan_callback callback
;
299 int (*fn
)(__isl_take isl_point
*pnt
, void *user
);
304 static int foreach_point(struct isl_scan_callback
*cb
, __isl_take isl_vec
*sample
)
306 struct isl_foreach_point
*fp
= (struct isl_foreach_point
*)cb
;
309 pnt
= isl_point_alloc(isl_space_copy(fp
->dim
), sample
);
311 return fp
->fn(pnt
, fp
->user
);
314 int isl_set_foreach_point(__isl_keep isl_set
*set
,
315 int (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
317 struct isl_foreach_point fp
= { { &foreach_point
}, fn
, user
};
323 fp
.dim
= isl_set_get_space(set
);
327 set
= isl_set_copy(set
);
328 set
= isl_set_cow(set
);
329 set
= isl_set_make_disjoint(set
);
330 set
= isl_set_compute_divs(set
);
334 for (i
= 0; i
< set
->n
; ++i
)
335 if (isl_basic_set_scan(isl_basic_set_copy(set
->p
[i
]),
340 isl_space_free(fp
.dim
);
345 isl_space_free(fp
.dim
);
349 /* Return 1 if "bmap" contains the point "point".
350 * "bmap" is assumed to have known divs.
351 * The point is first extended with the divs and then passed
352 * to basic_map_contains.
354 int isl_basic_map_contains_point(__isl_keep isl_basic_map
*bmap
,
355 __isl_keep isl_point
*point
)
364 isl_assert(bmap
->ctx
, isl_space_is_equal(bmap
->dim
, point
->dim
), return -1);
365 if (bmap
->n_div
== 0)
366 return isl_basic_map_contains(bmap
, point
->vec
);
368 dim
= isl_basic_map_total_dim(bmap
) - bmap
->n_div
;
369 vec
= isl_vec_alloc(bmap
->ctx
, 1 + dim
+ bmap
->n_div
);
373 isl_seq_cpy(vec
->el
, point
->vec
->el
, point
->vec
->size
);
374 for (i
= 0; i
< bmap
->n_div
; ++i
) {
375 isl_seq_inner_product(bmap
->div
[i
] + 1, vec
->el
,
376 1 + dim
+ i
, &vec
->el
[1+dim
+i
]);
377 isl_int_fdiv_q(vec
->el
[1+dim
+i
], vec
->el
[1+dim
+i
],
381 contains
= isl_basic_map_contains(bmap
, vec
);
387 int isl_map_contains_point(__isl_keep isl_map
*map
, __isl_keep isl_point
*point
)
395 map
= isl_map_copy(map
);
396 map
= isl_map_compute_divs(map
);
400 for (i
= 0; i
< map
->n
; ++i
) {
401 found
= isl_basic_map_contains_point(map
->p
[i
], point
);
415 int isl_set_contains_point(__isl_keep isl_set
*set
, __isl_keep isl_point
*point
)
417 return isl_map_contains_point((isl_map
*)set
, point
);
420 __isl_give isl_basic_set
*isl_basic_set_from_point(__isl_take isl_point
*pnt
)
423 isl_basic_set
*model
;
428 model
= isl_basic_set_empty(isl_space_copy(pnt
->dim
));
429 bset
= isl_basic_set_from_vec(isl_vec_copy(pnt
->vec
));
430 bset
= isl_basic_set_from_underlying_set(bset
, model
);
436 __isl_give isl_set
*isl_set_from_point(__isl_take isl_point
*pnt
)
439 bset
= isl_basic_set_from_point(pnt
);
440 return isl_set_from_basic_set(bset
);
443 __isl_give isl_basic_set
*isl_basic_set_box_from_points(
444 __isl_take isl_point
*pnt1
, __isl_take isl_point
*pnt2
)
457 isl_assert(pnt1
->dim
->ctx
,
458 isl_space_is_equal(pnt1
->dim
, pnt2
->dim
), goto error
);
460 if (isl_point_is_void(pnt1
) && isl_point_is_void(pnt2
)) {
461 isl_space
*dim
= isl_space_copy(pnt1
->dim
);
462 isl_point_free(pnt1
);
463 isl_point_free(pnt2
);
465 return isl_basic_set_empty(dim
);
467 if (isl_point_is_void(pnt1
)) {
468 isl_point_free(pnt1
);
470 return isl_basic_set_from_point(pnt2
);
472 if (isl_point_is_void(pnt2
)) {
473 isl_point_free(pnt2
);
475 return isl_basic_set_from_point(pnt1
);
478 total
= isl_space_dim(pnt1
->dim
, isl_dim_all
);
479 bset
= isl_basic_set_alloc_space(isl_space_copy(pnt1
->dim
), 0, 0, 2 * total
);
481 for (i
= 0; i
< total
; ++i
) {
482 isl_int_mul(t
, pnt1
->vec
->el
[1 + i
], pnt2
->vec
->el
[0]);
483 isl_int_submul(t
, pnt2
->vec
->el
[1 + i
], pnt1
->vec
->el
[0]);
485 k
= isl_basic_set_alloc_inequality(bset
);
488 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
489 if (isl_int_is_pos(t
)) {
490 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
491 isl_int_set(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
493 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
494 isl_int_neg(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
496 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt1
->vec
->el
[0]);
498 k
= isl_basic_set_alloc_inequality(bset
);
501 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
502 if (isl_int_is_pos(t
)) {
503 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
504 isl_int_neg(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
506 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
507 isl_int_set(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
509 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt2
->vec
->el
[0]);
512 bset
= isl_basic_set_finalize(bset
);
514 isl_point_free(pnt1
);
515 isl_point_free(pnt2
);
521 isl_point_free(pnt1
);
522 isl_point_free(pnt2
);
527 __isl_give isl_set
*isl_set_box_from_points(__isl_take isl_point
*pnt1
,
528 __isl_take isl_point
*pnt2
)
531 bset
= isl_basic_set_box_from_points(pnt1
, pnt2
);
532 return isl_set_from_basic_set(bset
);
535 __isl_give isl_printer
*isl_printer_print_point(
536 __isl_take isl_printer
*p
, __isl_keep isl_point
*pnt
)
544 if (isl_point_is_void(pnt
)) {
545 p
= isl_printer_print_str(p
, "void");
549 nparam
= isl_space_dim(pnt
->dim
, isl_dim_param
);
550 dim
= isl_space_dim(pnt
->dim
, isl_dim_set
);
552 p
= isl_printer_print_str(p
, "[");
553 for (i
= 0; i
< nparam
; ++i
) {
556 p
= isl_printer_print_str(p
, ", ");
557 name
= isl_space_get_dim_name(pnt
->dim
, isl_dim_param
, i
);
559 p
= isl_printer_print_str(p
, name
);
560 p
= isl_printer_print_str(p
, " = ");
562 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[1 + i
]);
563 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
564 p
= isl_printer_print_str(p
, "/");
565 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[0]);
568 p
= isl_printer_print_str(p
, "]");
569 p
= isl_printer_print_str(p
, " -> ");
571 p
= isl_printer_print_str(p
, "[");
572 for (i
= 0; i
< dim
; ++i
) {
574 p
= isl_printer_print_str(p
, ", ");
575 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[1 + nparam
+ i
]);
576 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
577 p
= isl_printer_print_str(p
, "/");
578 p
= isl_printer_print_isl_int(p
, pnt
->vec
->el
[0]);
581 p
= isl_printer_print_str(p
, "]");