isl_schedule: explicitly keep track of isl_ctx
[isl.git] / isl_point.c
blobf5320f2ede888fa12ee1af738ba55361ddb62c5b
1 #include <isl_map_private.h>
2 #include <isl_point_private.h>
3 #include <isl/set.h>
4 #include <isl_sample.h>
5 #include <isl_scan.h>
6 #include <isl_seq.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;
27 if (!dim || !vec)
28 goto error;
30 if (vec->size > 1 + isl_space_dim(dim, isl_dim_all)) {
31 vec = isl_vec_cow(vec);
32 if (!vec)
33 goto error;
34 vec->size = 1 + isl_space_dim(dim, isl_dim_all);
37 pnt = isl_alloc_type(dim->ctx, struct isl_point);
38 if (!pnt)
39 goto error;
41 pnt->ref = 1;
42 pnt->dim = dim;
43 pnt->vec = vec;
45 return pnt;
46 error:
47 isl_space_free(dim);
48 isl_vec_free(vec);
49 return NULL;
52 __isl_give isl_point *isl_point_zero(__isl_take isl_space *dim)
54 isl_vec *vec;
56 if (!dim)
57 return NULL;
58 vec = isl_vec_alloc(dim->ctx, 1 + isl_space_dim(dim, isl_dim_all));
59 if (!vec)
60 goto error;
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);
64 error:
65 isl_space_free(dim);
66 return NULL;
69 __isl_give isl_point *isl_point_dup(__isl_keep isl_point *pnt)
71 struct isl_point *pnt2;
73 if (!pnt)
74 return NULL;
75 pnt2 = isl_point_alloc(isl_space_copy(pnt->dim), isl_vec_copy(pnt->vec));
76 return pnt2;
79 __isl_give isl_point *isl_point_cow(__isl_take isl_point *pnt)
81 struct isl_point *pnt2;
82 if (!pnt)
83 return NULL;
85 if (pnt->ref == 1)
86 return pnt;
88 pnt2 = isl_point_dup(pnt);
89 isl_point_free(pnt);
90 return pnt2;
93 __isl_give isl_point *isl_point_copy(__isl_keep isl_point *pnt)
95 if (!pnt)
96 return NULL;
98 pnt->ref++;
99 return pnt;
102 void isl_point_free(__isl_take isl_point *pnt)
104 if (!pnt)
105 return;
107 if (--pnt->ref > 0)
108 return;
110 isl_space_free(pnt->dim);
111 isl_vec_free(pnt->vec);
112 free(pnt);
115 __isl_give isl_point *isl_point_void(__isl_take isl_space *dim)
117 if (!dim)
118 return NULL;
120 return isl_point_alloc(dim, isl_vec_alloc(dim->ctx, 0));
123 int isl_point_is_void(__isl_keep isl_point *pnt)
125 if (!pnt)
126 return -1;
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))
135 return -1;
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]);
145 return 0;
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)
153 isl_ctx *ctx;
154 isl_val *v;
156 if (!pnt)
157 return NULL;
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],
171 pnt->vec->el[0]);
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))
179 return pnt;
181 pnt = isl_point_cow(pnt);
182 if (!pnt)
183 return NULL;
184 pnt->vec = isl_vec_cow(pnt->vec);
185 if (!pnt->vec)
186 goto error;
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);
193 return pnt;
194 error:
195 isl_point_free(pnt);
196 return NULL;
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)
204 if (!pnt || !v)
205 goto error;
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)) {
218 isl_val_free(v);
219 return pnt;
222 pnt = isl_point_cow(pnt);
223 if (!pnt)
224 goto error;
225 pnt->vec = isl_vec_cow(pnt->vec);
226 if (!pnt->vec)
227 goto error;
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);
233 } else {
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);
239 if (!pnt->vec)
240 goto error;
243 isl_val_free(v);
244 return pnt;
245 error:
246 isl_val_free(v);
247 isl_point_free(pnt);
248 return NULL;
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))
255 return pnt;
257 pnt = isl_point_cow(pnt);
258 if (!pnt)
259 return NULL;
260 pnt->vec = isl_vec_cow(pnt->vec);
261 if (!pnt->vec)
262 goto error;
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);
269 return pnt;
270 error:
271 isl_point_free(pnt);
272 return NULL;
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))
279 return pnt;
281 pnt = isl_point_cow(pnt);
282 if (!pnt)
283 return NULL;
284 pnt->vec = isl_vec_cow(pnt->vec);
285 if (!pnt->vec)
286 goto error;
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);
293 return pnt;
294 error:
295 isl_point_free(pnt);
296 return NULL;
299 struct isl_foreach_point {
300 struct isl_scan_callback callback;
301 int (*fn)(__isl_take isl_point *pnt, void *user);
302 void *user;
303 isl_space *dim;
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;
309 isl_point *pnt;
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 };
320 int i;
322 if (!set)
323 return -1;
325 fp.dim = isl_set_get_space(set);
326 if (!fp.dim)
327 return -1;
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);
333 if (!set)
334 goto error;
336 for (i = 0; i < set->n; ++i)
337 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
338 &fp.callback) < 0)
339 goto error;
341 isl_set_free(set);
342 isl_space_free(fp.dim);
344 return 0;
345 error:
346 isl_set_free(set);
347 isl_space_free(fp.dim);
348 return -1;
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)
359 int i;
360 struct isl_vec *vec;
361 unsigned dim;
362 int contains;
364 if (!bmap || !point)
365 return -1;
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);
372 if (!vec)
373 return -1;
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],
380 bmap->div[i][0]);
383 contains = isl_basic_map_contains(bmap, vec);
385 isl_vec_free(vec);
386 return contains;
389 int isl_map_contains_point(__isl_keep isl_map *map, __isl_keep isl_point *point)
391 int i;
392 int found = 0;
394 if (!map || !point)
395 return -1;
397 map = isl_map_copy(map);
398 map = isl_map_compute_divs(map);
399 if (!map)
400 return -1;
402 for (i = 0; i < map->n; ++i) {
403 found = isl_basic_map_contains_point(map->p[i], point);
404 if (found < 0)
405 goto error;
406 if (found)
407 break;
409 isl_map_free(map);
411 return found;
412 error:
413 isl_map_free(map);
414 return -1;
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)
424 isl_basic_set *bset;
425 isl_basic_set *model;
427 if (!pnt)
428 return NULL;
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);
433 isl_point_free(pnt);
435 return bset;
438 __isl_give isl_set *isl_set_from_point(__isl_take isl_point *pnt)
440 isl_basic_set *bset;
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)
448 isl_basic_set *bset;
449 unsigned total;
450 int i;
451 int k;
452 isl_int t;
454 isl_int_init(t);
456 if (!pnt1 || !pnt2)
457 goto error;
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);
466 isl_int_clear(t);
467 return isl_basic_set_empty(dim);
469 if (isl_point_is_void(pnt1)) {
470 isl_point_free(pnt1);
471 isl_int_clear(t);
472 return isl_basic_set_from_point(pnt2);
474 if (isl_point_is_void(pnt2)) {
475 isl_point_free(pnt2);
476 isl_int_clear(t);
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);
488 if (k < 0)
489 goto error;
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]);
494 } else {
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);
501 if (k < 0)
502 goto error;
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]);
507 } else {
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);
519 isl_int_clear(t);
521 return bset;
522 error:
523 isl_point_free(pnt1);
524 isl_point_free(pnt2);
525 isl_int_clear(t);
526 return NULL;
529 __isl_give isl_set *isl_set_box_from_points(__isl_take isl_point *pnt1,
530 __isl_take isl_point *pnt2)
532 isl_basic_set *bset;
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)
540 int i;
541 unsigned nparam;
542 unsigned dim;
544 if (!pnt)
545 return p;
546 if (isl_point_is_void(pnt)) {
547 p = isl_printer_print_str(p, "void");
548 return p;
551 nparam = isl_space_dim(pnt->dim, isl_dim_param);
552 dim = isl_space_dim(pnt->dim, isl_dim_set);
553 if (nparam > 0) {
554 p = isl_printer_print_str(p, "[");
555 for (i = 0; i < nparam; ++i) {
556 const char *name;
557 if (i)
558 p = isl_printer_print_str(p, ", ");
559 name = isl_space_get_dim_name(pnt->dim, isl_dim_param, i);
560 if (name) {
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) {
575 if (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, "]");
584 return p;