drop isl_map_drop_redundant_divs
[isl.git] / isl_point.c
blobc25b76fba679547356c2193348344d00973772a9
1 #include <isl_map_private.h>
2 #include <isl_point_private.h>
3 #include <isl/set.h>
4 #include <isl/union_set.h>
5 #include <isl_sample.h>
6 #include <isl_scan.h>
7 #include <isl_seq.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;
31 if (!dim || !vec)
32 goto error;
34 if (vec->size > 1 + isl_space_dim(dim, isl_dim_all)) {
35 vec = isl_vec_cow(vec);
36 if (!vec)
37 goto error;
38 vec->size = 1 + isl_space_dim(dim, isl_dim_all);
41 pnt = isl_alloc_type(dim->ctx, struct isl_point);
42 if (!pnt)
43 goto error;
45 pnt->ref = 1;
46 pnt->dim = dim;
47 pnt->vec = vec;
49 return pnt;
50 error:
51 isl_space_free(dim);
52 isl_vec_free(vec);
53 return NULL;
56 __isl_give isl_point *isl_point_zero(__isl_take isl_space *dim)
58 isl_vec *vec;
60 if (!dim)
61 return NULL;
62 vec = isl_vec_alloc(dim->ctx, 1 + isl_space_dim(dim, isl_dim_all));
63 if (!vec)
64 goto error;
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);
68 error:
69 isl_space_free(dim);
70 return NULL;
73 __isl_give isl_point *isl_point_dup(__isl_keep isl_point *pnt)
75 struct isl_point *pnt2;
77 if (!pnt)
78 return NULL;
79 pnt2 = isl_point_alloc(isl_space_copy(pnt->dim), isl_vec_copy(pnt->vec));
80 return pnt2;
83 __isl_give isl_point *isl_point_cow(__isl_take isl_point *pnt)
85 struct isl_point *pnt2;
86 if (!pnt)
87 return NULL;
89 if (pnt->ref == 1)
90 return pnt;
92 pnt2 = isl_point_dup(pnt);
93 isl_point_free(pnt);
94 return pnt2;
97 __isl_give isl_point *isl_point_copy(__isl_keep isl_point *pnt)
99 if (!pnt)
100 return NULL;
102 pnt->ref++;
103 return pnt;
106 void isl_point_free(__isl_take isl_point *pnt)
108 if (!pnt)
109 return;
111 if (--pnt->ref > 0)
112 return;
114 isl_space_free(pnt->dim);
115 isl_vec_free(pnt->vec);
116 free(pnt);
119 __isl_give isl_point *isl_point_void(__isl_take isl_space *dim)
121 if (!dim)
122 return NULL;
124 return isl_point_alloc(dim, isl_vec_alloc(dim->ctx, 0));
127 isl_bool isl_point_is_void(__isl_keep isl_point *pnt)
129 if (!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))
139 return -1;
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]);
149 return 0;
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)
157 isl_ctx *ctx;
158 isl_val *v;
160 if (!pnt)
161 return NULL;
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],
175 pnt->vec->el[0]);
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))
183 return pnt;
185 pnt = isl_point_cow(pnt);
186 if (!pnt)
187 return NULL;
188 pnt->vec = isl_vec_cow(pnt->vec);
189 if (!pnt->vec)
190 goto error;
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);
197 return pnt;
198 error:
199 isl_point_free(pnt);
200 return NULL;
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)
208 if (!pnt || !v)
209 goto error;
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)) {
222 isl_val_free(v);
223 return pnt;
226 pnt = isl_point_cow(pnt);
227 if (!pnt)
228 goto error;
229 pnt->vec = isl_vec_cow(pnt->vec);
230 if (!pnt->vec)
231 goto error;
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);
237 } else {
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);
243 if (!pnt->vec)
244 goto error;
247 isl_val_free(v);
248 return pnt;
249 error:
250 isl_val_free(v);
251 isl_point_free(pnt);
252 return NULL;
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))
259 return pnt;
261 pnt = isl_point_cow(pnt);
262 if (!pnt)
263 return NULL;
264 pnt->vec = isl_vec_cow(pnt->vec);
265 if (!pnt->vec)
266 goto error;
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);
273 return pnt;
274 error:
275 isl_point_free(pnt);
276 return NULL;
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))
283 return pnt;
285 pnt = isl_point_cow(pnt);
286 if (!pnt)
287 return NULL;
288 pnt->vec = isl_vec_cow(pnt->vec);
289 if (!pnt->vec)
290 goto error;
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);
297 return pnt;
298 error:
299 isl_point_free(pnt);
300 return NULL;
303 struct isl_foreach_point {
304 struct isl_scan_callback callback;
305 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
306 void *user;
307 isl_space *dim;
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;
314 isl_point *pnt;
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 };
325 int i;
327 if (!set)
328 return isl_stat_error;
330 fp.dim = isl_set_get_space(set);
331 if (!fp.dim)
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);
338 if (!set)
339 goto error;
341 for (i = 0; i < set->n; ++i)
342 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
343 &fp.callback) < 0)
344 goto error;
346 isl_set_free(set);
347 isl_space_free(fp.dim);
349 return isl_stat_ok;
350 error:
351 isl_set_free(set);
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)
364 int i;
365 struct isl_vec *vec;
366 unsigned dim;
367 isl_bool contains;
369 if (!bmap || !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);
378 if (!vec)
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],
386 bmap->div[i][0]);
389 contains = isl_basic_map_contains(bmap, vec);
391 isl_vec_free(vec);
392 return contains;
395 isl_bool isl_map_contains_point(__isl_keep isl_map *map,
396 __isl_keep isl_point *point)
398 int i;
399 isl_bool found = isl_bool_false;
401 if (!map || !point)
402 return isl_bool_error;
404 map = isl_map_copy(map);
405 map = isl_map_compute_divs(map);
406 if (!map)
407 return isl_bool_error;
409 for (i = 0; i < map->n; ++i) {
410 found = isl_basic_map_contains_point(map->p[i], point);
411 if (found < 0)
412 goto error;
413 if (found)
414 break;
416 isl_map_free(map);
418 return found;
419 error:
420 isl_map_free(map);
421 return isl_bool_error;
424 isl_bool isl_set_contains_point(__isl_keep isl_set *set,
425 __isl_keep isl_point *point)
427 return isl_map_contains_point(set_to_map(set), point);
430 __isl_give isl_basic_set *isl_basic_set_from_point(__isl_take isl_point *pnt)
432 isl_basic_set *bset;
433 isl_basic_set *model;
435 if (!pnt)
436 return NULL;
438 model = isl_basic_set_empty(isl_space_copy(pnt->dim));
439 bset = isl_basic_set_from_vec(isl_vec_copy(pnt->vec));
440 bset = isl_basic_set_from_underlying_set(bset, model);
441 isl_point_free(pnt);
443 return bset;
446 __isl_give isl_set *isl_set_from_point(__isl_take isl_point *pnt)
448 isl_basic_set *bset;
449 bset = isl_basic_set_from_point(pnt);
450 return isl_set_from_basic_set(bset);
453 /* Construct a union set, containing the single element "pnt".
454 * If "pnt" is void, then return an empty union set.
456 __isl_give isl_union_set *isl_union_set_from_point(__isl_take isl_point *pnt)
458 if (!pnt)
459 return NULL;
460 if (isl_point_is_void(pnt)) {
461 isl_space *space;
463 space = isl_point_get_space(pnt);
464 isl_point_free(pnt);
465 return isl_union_set_empty(space);
468 return isl_union_set_from_set(isl_set_from_point(pnt));
471 __isl_give isl_basic_set *isl_basic_set_box_from_points(
472 __isl_take isl_point *pnt1, __isl_take isl_point *pnt2)
474 isl_basic_set *bset = NULL;
475 unsigned total;
476 int i;
477 int k;
478 isl_int t;
480 isl_int_init(t);
482 if (!pnt1 || !pnt2)
483 goto error;
485 isl_assert(pnt1->dim->ctx,
486 isl_space_is_equal(pnt1->dim, pnt2->dim), goto error);
488 if (isl_point_is_void(pnt1) && isl_point_is_void(pnt2)) {
489 isl_space *dim = isl_space_copy(pnt1->dim);
490 isl_point_free(pnt1);
491 isl_point_free(pnt2);
492 isl_int_clear(t);
493 return isl_basic_set_empty(dim);
495 if (isl_point_is_void(pnt1)) {
496 isl_point_free(pnt1);
497 isl_int_clear(t);
498 return isl_basic_set_from_point(pnt2);
500 if (isl_point_is_void(pnt2)) {
501 isl_point_free(pnt2);
502 isl_int_clear(t);
503 return isl_basic_set_from_point(pnt1);
506 total = isl_space_dim(pnt1->dim, isl_dim_all);
507 bset = isl_basic_set_alloc_space(isl_space_copy(pnt1->dim), 0, 0, 2 * total);
509 for (i = 0; i < total; ++i) {
510 isl_int_mul(t, pnt1->vec->el[1 + i], pnt2->vec->el[0]);
511 isl_int_submul(t, pnt2->vec->el[1 + i], pnt1->vec->el[0]);
513 k = isl_basic_set_alloc_inequality(bset);
514 if (k < 0)
515 goto error;
516 isl_seq_clr(bset->ineq[k] + 1, total);
517 if (isl_int_is_pos(t)) {
518 isl_int_set_si(bset->ineq[k][1 + i], -1);
519 isl_int_set(bset->ineq[k][0], pnt1->vec->el[1 + i]);
520 } else {
521 isl_int_set_si(bset->ineq[k][1 + i], 1);
522 isl_int_neg(bset->ineq[k][0], pnt1->vec->el[1 + i]);
524 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt1->vec->el[0]);
526 k = isl_basic_set_alloc_inequality(bset);
527 if (k < 0)
528 goto error;
529 isl_seq_clr(bset->ineq[k] + 1, total);
530 if (isl_int_is_pos(t)) {
531 isl_int_set_si(bset->ineq[k][1 + i], 1);
532 isl_int_neg(bset->ineq[k][0], pnt2->vec->el[1 + i]);
533 } else {
534 isl_int_set_si(bset->ineq[k][1 + i], -1);
535 isl_int_set(bset->ineq[k][0], pnt2->vec->el[1 + i]);
537 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt2->vec->el[0]);
540 bset = isl_basic_set_finalize(bset);
542 isl_point_free(pnt1);
543 isl_point_free(pnt2);
545 isl_int_clear(t);
547 return bset;
548 error:
549 isl_point_free(pnt1);
550 isl_point_free(pnt2);
551 isl_int_clear(t);
552 isl_basic_set_free(bset);
553 return NULL;
556 __isl_give isl_set *isl_set_box_from_points(__isl_take isl_point *pnt1,
557 __isl_take isl_point *pnt2)
559 isl_basic_set *bset;
560 bset = isl_basic_set_box_from_points(pnt1, pnt2);
561 return isl_set_from_basic_set(bset);
564 /* Print the coordinate at position "pos" of the point "pnt".
566 static __isl_give isl_printer *print_coordinate(__isl_take isl_printer *p,
567 struct isl_print_space_data *data, unsigned pos)
569 isl_point *pnt = data->user;
571 p = isl_printer_print_isl_int(p, pnt->vec->el[1 + pos]);
572 if (!isl_int_is_one(pnt->vec->el[0])) {
573 p = isl_printer_print_str(p, "/");
574 p = isl_printer_print_isl_int(p, pnt->vec->el[0]);
577 return p;
580 __isl_give isl_printer *isl_printer_print_point(
581 __isl_take isl_printer *p, __isl_keep isl_point *pnt)
583 struct isl_print_space_data data = { 0 };
584 int i;
585 unsigned nparam;
587 if (!pnt)
588 return p;
589 if (isl_point_is_void(pnt)) {
590 p = isl_printer_print_str(p, "void");
591 return p;
594 nparam = isl_space_dim(pnt->dim, isl_dim_param);
595 if (nparam > 0) {
596 p = isl_printer_print_str(p, "[");
597 for (i = 0; i < nparam; ++i) {
598 const char *name;
599 if (i)
600 p = isl_printer_print_str(p, ", ");
601 name = isl_space_get_dim_name(pnt->dim, isl_dim_param, i);
602 if (name) {
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;
616 data.user = pnt;
617 p = isl_printer_print_str(p, "{ ");
618 p = isl_print_space(pnt->dim, p, 0, &data);
619 p = isl_printer_print_str(p, " }");
620 return p;