1 #include <isl_point_private.h>
3 #include <isl_map_private.h>
4 #include <isl_sample.h>
8 __isl_give isl_point
*isl_point_alloc(__isl_take isl_dim
*dim
,
9 __isl_take isl_vec
*vec
)
11 struct isl_point
*pnt
;
16 pnt
= isl_alloc_type(dim
->ctx
, struct isl_point
);
20 if (vec
->size
> 1 + isl_dim_total(dim
)) {
21 vec
= isl_vec_cow(vec
);
24 vec
->size
= 1 + isl_dim_total(dim
);
38 __isl_give isl_point
*isl_point_zero(__isl_take isl_dim
*dim
)
44 vec
= isl_vec_alloc(dim
->ctx
, 1 + isl_dim_total(dim
));
47 isl_int_set_si(vec
->el
[0], 1);
48 isl_seq_clr(vec
->el
+ 1, vec
->size
- 1);
49 return isl_point_alloc(dim
, vec
);
55 __isl_give isl_point
*isl_point_dup(__isl_keep isl_point
*pnt
)
57 struct isl_point
*pnt2
;
61 pnt2
= isl_point_alloc(isl_dim_copy(pnt
->dim
), isl_vec_copy(pnt
->vec
));
65 __isl_give isl_point
*isl_point_cow(__isl_take isl_point
*pnt
)
67 struct isl_point
*pnt2
;
74 pnt2
= isl_point_dup(pnt
);
79 __isl_give isl_point
*isl_point_copy(__isl_keep isl_point
*pnt
)
88 void isl_point_free(__isl_take isl_point
*pnt
)
96 isl_dim_free(pnt
->dim
);
97 isl_vec_free(pnt
->vec
);
101 __isl_give isl_point
*isl_point_void(__isl_take isl_dim
*dim
)
106 return isl_point_alloc(dim
, isl_vec_alloc(dim
->ctx
, 0));
109 int isl_point_is_void(__isl_keep isl_point
*pnt
)
114 return pnt
->vec
->size
== 0;
117 void isl_point_get_coordinate(__isl_keep isl_point
*pnt
,
118 enum isl_dim_type type
, int pos
, isl_int
*v
)
120 if (!pnt
|| isl_point_is_void(pnt
))
122 if (type
== isl_dim_set
)
123 pos
+= isl_dim_size(pnt
->dim
, isl_dim_param
);
124 isl_int_set(*v
, pnt
->vec
->el
[1 + pos
]);
127 __isl_give isl_point
*isl_point_set_coordinate(__isl_take isl_point
*pnt
,
128 enum isl_dim_type type
, int pos
, isl_int v
)
130 if (!pnt
|| isl_point_is_void(pnt
))
133 pnt
= isl_point_cow(pnt
);
136 pnt
->vec
= isl_vec_cow(pnt
->vec
);
140 if (type
== isl_dim_set
)
141 pos
+= isl_dim_size(pnt
->dim
, isl_dim_param
);
143 isl_int_set(pnt
->vec
->el
[1 + pos
], v
);
151 __isl_give isl_point
*isl_point_add_ui(__isl_take isl_point
*pnt
,
152 enum isl_dim_type type
, int pos
, unsigned val
)
154 if (!pnt
|| isl_point_is_void(pnt
))
157 pnt
= isl_point_cow(pnt
);
160 pnt
->vec
= isl_vec_cow(pnt
->vec
);
164 if (type
== isl_dim_set
)
165 pos
+= isl_dim_size(pnt
->dim
, isl_dim_param
);
167 isl_int_add_ui(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[1 + pos
], val
);
175 __isl_give isl_point
*isl_point_sub_ui(__isl_take isl_point
*pnt
,
176 enum isl_dim_type type
, int pos
, unsigned val
)
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_dim_size(pnt
->dim
, isl_dim_param
);
191 isl_int_sub_ui(pnt
->vec
->el
[1 + pos
], pnt
->vec
->el
[1 + pos
], val
);
199 struct isl_foreach_point
{
200 struct isl_scan_callback callback
;
201 int (*fn
)(__isl_take isl_point
*pnt
, void *user
);
206 static int foreach_point(struct isl_scan_callback
*cb
, __isl_take isl_vec
*sample
)
208 struct isl_foreach_point
*fp
= (struct isl_foreach_point
*)cb
;
211 pnt
= isl_point_alloc(isl_dim_copy(fp
->dim
), sample
);
213 return fp
->fn(pnt
, fp
->user
);
216 int isl_set_foreach_point(__isl_keep isl_set
*set
,
217 int (*fn
)(__isl_take isl_point
*pnt
, void *user
), void *user
)
219 struct isl_foreach_point fp
= { { &foreach_point
}, fn
, user
};
225 fp
.dim
= isl_set_get_dim(set
);
229 set
= isl_set_copy(set
);
230 set
= isl_set_cow(set
);
231 set
= isl_set_make_disjoint(set
);
232 set
= isl_set_compute_divs(set
);
236 for (i
= 0; i
< set
->n
; ++i
)
237 if (isl_basic_set_scan(isl_basic_set_copy(set
->p
[i
]),
242 isl_dim_free(fp
.dim
);
247 isl_dim_free(fp
.dim
);
251 /* Return 1 if "bmap" contains the point "point".
252 * "bmap" is assumed to have known divs.
253 * The point is first extended with the divs and then passed
254 * to basic_map_contains.
256 int isl_basic_map_contains_point(__isl_keep isl_basic_map
*bmap
,
257 __isl_keep isl_point
*point
)
266 isl_assert(bmap
->ctx
, isl_dim_equal(bmap
->dim
, point
->dim
), return -1);
267 if (bmap
->n_div
== 0)
268 return isl_basic_map_contains(bmap
, point
->vec
);
270 dim
= isl_basic_map_total_dim(bmap
) - bmap
->n_div
;
271 vec
= isl_vec_alloc(bmap
->ctx
, 1 + dim
+ bmap
->n_div
);
275 isl_seq_cpy(vec
->el
, point
->vec
->el
, point
->vec
->size
);
276 for (i
= 0; i
< bmap
->n_div
; ++i
) {
277 isl_seq_inner_product(bmap
->div
[i
] + 1, vec
->el
,
278 1 + dim
+ i
, &vec
->el
[1+dim
+i
]);
279 isl_int_fdiv_q(vec
->el
[1+dim
+i
], vec
->el
[1+dim
+i
],
283 contains
= isl_basic_map_contains(bmap
, vec
);
289 int isl_map_contains_point(__isl_keep isl_map
*map
, __isl_keep isl_point
*point
)
297 map
= isl_map_copy(map
);
298 map
= isl_map_compute_divs(map
);
302 for (i
= 0; i
< map
->n
; ++i
) {
303 found
= isl_basic_map_contains_point(map
->p
[i
], point
);
317 int isl_set_contains_point(__isl_keep isl_set
*set
, __isl_keep isl_point
*point
)
319 return isl_map_contains_point((isl_map
*)set
, point
);
322 __isl_give isl_set
*isl_set_box_from_points(__isl_take isl_point
*pnt1
,
323 __isl_take isl_point
*pnt2
)
336 isl_assert(pnt1
->dim
->ctx
,
337 isl_dim_equal(pnt1
->dim
, pnt2
->dim
), goto error
);
339 if (isl_point_is_void(pnt1
) && isl_point_is_void(pnt2
)) {
340 isl_dim
*dim
= isl_dim_copy(pnt1
->dim
);
341 isl_point_free(pnt1
);
342 isl_point_free(pnt2
);
344 return isl_set_empty(dim
);
346 if (isl_point_is_void(pnt1
)) {
347 isl_basic_set
*model
;
348 model
= isl_basic_set_empty(isl_dim_copy(pnt2
->dim
));
349 bset
= isl_basic_set_from_vec(isl_vec_copy(pnt2
->vec
));
350 bset
= isl_basic_set_from_underlying_set(bset
, model
);
351 isl_point_free(pnt1
);
352 isl_point_free(pnt2
);
354 return isl_set_from_basic_set(bset
);
356 if (isl_point_is_void(pnt2
)) {
357 isl_basic_set
*model
;
358 model
= isl_basic_set_empty(isl_dim_copy(pnt1
->dim
));
359 bset
= isl_basic_set_from_vec(isl_vec_copy(pnt1
->vec
));
360 bset
= isl_basic_set_from_underlying_set(bset
, model
);
361 isl_point_free(pnt1
);
362 isl_point_free(pnt2
);
364 return isl_set_from_basic_set(bset
);
367 total
= isl_dim_total(pnt1
->dim
);
368 bset
= isl_basic_set_alloc_dim(isl_dim_copy(pnt1
->dim
), 0, 0, 2 * total
);
370 for (i
= 0; i
< total
; ++i
) {
371 isl_int_mul(t
, pnt1
->vec
->el
[1 + i
], pnt2
->vec
->el
[0]);
372 isl_int_submul(t
, pnt2
->vec
->el
[1 + i
], pnt1
->vec
->el
[0]);
374 k
= isl_basic_set_alloc_inequality(bset
);
377 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
378 if (isl_int_is_pos(t
)) {
379 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
380 isl_int_set(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
382 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
383 isl_int_neg(bset
->ineq
[k
][0], pnt1
->vec
->el
[1 + i
]);
385 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt1
->vec
->el
[0]);
387 k
= isl_basic_set_alloc_inequality(bset
);
390 isl_seq_clr(bset
->ineq
[k
] + 1, total
);
391 if (isl_int_is_pos(t
)) {
392 isl_int_set_si(bset
->ineq
[k
][1 + i
], 1);
393 isl_int_neg(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
395 isl_int_set_si(bset
->ineq
[k
][1 + i
], -1);
396 isl_int_set(bset
->ineq
[k
][0], pnt2
->vec
->el
[1 + i
]);
398 isl_int_fdiv_q(bset
->ineq
[k
][0], bset
->ineq
[k
][0], pnt2
->vec
->el
[0]);
401 bset
= isl_basic_set_finalize(bset
);
403 isl_point_free(pnt1
);
404 isl_point_free(pnt2
);
408 return isl_set_from_basic_set(bset
);
410 isl_point_free(pnt1
);
411 isl_point_free(pnt2
);
416 void isl_point_print(__isl_keep isl_point
*pnt
, FILE *out
)
424 if (isl_point_is_void(pnt
)) {
425 fprintf(out
, "void\n");
429 nparam
= isl_dim_size(pnt
->dim
, isl_dim_param
);
430 dim
= isl_dim_size(pnt
->dim
, isl_dim_set
);
433 for (i
= 0; i
< nparam
; ++i
) {
437 name
= isl_dim_get_name(pnt
->dim
, isl_dim_param
, i
);
439 fprintf(out
, "%s = ", name
);
440 isl_int_print(out
, pnt
->vec
->el
[1 + i
], 0);
441 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
443 isl_int_print(out
, pnt
->vec
->el
[0], 0);
446 fprintf(out
, "] -> ");
449 for (i
= 0; i
< dim
; ++i
) {
452 isl_int_print(out
, pnt
->vec
->el
[1 + nparam
+ i
], 0);
453 if (!isl_int_is_one(pnt
->vec
->el
[0])) {
455 isl_int_print(out
, pnt
->vec
->el
[0], 0);