isl_aff_neg_basic_set: extract out isl_aff_pos_basic_set
[isl.git] / isl_space.c
blob94c5ea8bb9f1cd2fcbc97cf60c2d0b7617b83528
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2013-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <stdlib.h>
16 #include <string.h>
17 #include <isl_space_private.h>
18 #include <isl_id_private.h>
19 #include <isl_reordering.h>
21 isl_ctx *isl_space_get_ctx(__isl_keep isl_space *dim)
23 return dim ? dim->ctx : NULL;
26 __isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
27 unsigned nparam, unsigned n_in, unsigned n_out)
29 isl_space *dim;
31 dim = isl_alloc_type(ctx, struct isl_space);
32 if (!dim)
33 return NULL;
35 dim->ctx = ctx;
36 isl_ctx_ref(ctx);
37 dim->ref = 1;
38 dim->nparam = nparam;
39 dim->n_in = n_in;
40 dim->n_out = n_out;
42 dim->tuple_id[0] = NULL;
43 dim->tuple_id[1] = NULL;
45 dim->nested[0] = NULL;
46 dim->nested[1] = NULL;
48 dim->n_id = 0;
49 dim->ids = NULL;
51 return dim;
54 /* Mark the space as being that of a set, by setting the domain tuple
55 * to isl_id_none.
57 static __isl_give isl_space *mark_as_set(__isl_take isl_space *space)
59 space = isl_space_cow(space);
60 if (!space)
61 return NULL;
62 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
63 return space;
66 /* Is the space that of a set?
68 isl_bool isl_space_is_set(__isl_keep isl_space *space)
70 if (!space)
71 return isl_bool_error;
72 if (space->n_in != 0 || space->nested[0])
73 return isl_bool_false;
74 if (space->tuple_id[0] != &isl_id_none)
75 return isl_bool_false;
76 return isl_bool_true;
79 /* Is the given space that of a map?
81 isl_bool isl_space_is_map(__isl_keep isl_space *space)
83 if (!space)
84 return isl_bool_error;
85 return space->tuple_id[0] != &isl_id_none &&
86 space->tuple_id[1] != &isl_id_none;
89 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
90 unsigned nparam, unsigned dim)
92 isl_space *space;
93 space = isl_space_alloc(ctx, nparam, 0, dim);
94 space = mark_as_set(space);
95 return space;
98 /* Mark the space as being that of a parameter domain, by setting
99 * both tuples to isl_id_none.
101 static __isl_give isl_space *mark_as_params(isl_space *space)
103 if (!space)
104 return NULL;
105 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
106 space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
107 return space;
110 /* Is the space that of a parameter domain?
112 isl_bool isl_space_is_params(__isl_keep isl_space *space)
114 if (!space)
115 return isl_bool_error;
116 if (space->n_in != 0 || space->nested[0] ||
117 space->n_out != 0 || space->nested[1])
118 return isl_bool_false;
119 if (space->tuple_id[0] != &isl_id_none)
120 return isl_bool_false;
121 if (space->tuple_id[1] != &isl_id_none)
122 return isl_bool_false;
123 return isl_bool_true;
126 /* Create a space for a parameter domain.
128 __isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
130 isl_space *space;
131 space = isl_space_alloc(ctx, nparam, 0, 0);
132 space = mark_as_params(space);
133 return space;
136 static unsigned global_pos(__isl_keep isl_space *dim,
137 enum isl_dim_type type, unsigned pos)
139 struct isl_ctx *ctx = dim->ctx;
141 switch (type) {
142 case isl_dim_param:
143 isl_assert(ctx, pos < dim->nparam,
144 return isl_space_dim(dim, isl_dim_all));
145 return pos;
146 case isl_dim_in:
147 isl_assert(ctx, pos < dim->n_in,
148 return isl_space_dim(dim, isl_dim_all));
149 return pos + dim->nparam;
150 case isl_dim_out:
151 isl_assert(ctx, pos < dim->n_out,
152 return isl_space_dim(dim, isl_dim_all));
153 return pos + dim->nparam + dim->n_in;
154 default:
155 isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
157 return isl_space_dim(dim, isl_dim_all);
160 /* Extend length of ids array to the total number of dimensions.
162 static __isl_give isl_space *extend_ids(__isl_take isl_space *dim)
164 isl_id **ids;
165 int i;
167 if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
168 return dim;
170 if (!dim->ids) {
171 dim->ids = isl_calloc_array(dim->ctx,
172 isl_id *, isl_space_dim(dim, isl_dim_all));
173 if (!dim->ids)
174 goto error;
175 } else {
176 ids = isl_realloc_array(dim->ctx, dim->ids,
177 isl_id *, isl_space_dim(dim, isl_dim_all));
178 if (!ids)
179 goto error;
180 dim->ids = ids;
181 for (i = dim->n_id; i < isl_space_dim(dim, isl_dim_all); ++i)
182 dim->ids[i] = NULL;
185 dim->n_id = isl_space_dim(dim, isl_dim_all);
187 return dim;
188 error:
189 isl_space_free(dim);
190 return NULL;
193 static __isl_give isl_space *set_id(__isl_take isl_space *dim,
194 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
196 dim = isl_space_cow(dim);
198 if (!dim)
199 goto error;
201 pos = global_pos(dim, type, pos);
202 if (pos == isl_space_dim(dim, isl_dim_all))
203 goto error;
205 if (pos >= dim->n_id) {
206 if (!id)
207 return dim;
208 dim = extend_ids(dim);
209 if (!dim)
210 goto error;
213 dim->ids[pos] = id;
215 return dim;
216 error:
217 isl_id_free(id);
218 isl_space_free(dim);
219 return NULL;
222 static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
223 enum isl_dim_type type, unsigned pos)
225 if (!dim)
226 return NULL;
228 pos = global_pos(dim, type, pos);
229 if (pos == isl_space_dim(dim, isl_dim_all))
230 return NULL;
231 if (pos >= dim->n_id)
232 return NULL;
233 return dim->ids[pos];
236 static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
238 switch (type) {
239 case isl_dim_param: return 0;
240 case isl_dim_in: return dim->nparam;
241 case isl_dim_out: return dim->nparam + dim->n_in;
242 default: return 0;
246 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
248 switch (type) {
249 case isl_dim_param: return dim->nparam;
250 case isl_dim_in: return dim->n_in;
251 case isl_dim_out: return dim->n_out;
252 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
253 default: return 0;
257 unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
259 if (!dim)
260 return 0;
261 return n(dim, type);
264 unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
266 if (!dim)
267 return 0;
268 return offset(dim, type);
271 static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
272 enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
273 enum isl_dim_type src_type)
275 int i;
276 isl_id *id;
278 if (!dst)
279 return NULL;
281 for (i = 0; i < n(src, src_type); ++i) {
282 id = get_id(src, src_type, i);
283 if (!id)
284 continue;
285 dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
286 if (!dst)
287 return NULL;
289 return dst;
292 __isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
294 isl_space *dup;
295 if (!dim)
296 return NULL;
297 dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
298 if (!dup)
299 return NULL;
300 if (dim->tuple_id[0] &&
301 !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
302 goto error;
303 if (dim->tuple_id[1] &&
304 !(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1])))
305 goto error;
306 if (dim->nested[0] && !(dup->nested[0] = isl_space_copy(dim->nested[0])))
307 goto error;
308 if (dim->nested[1] && !(dup->nested[1] = isl_space_copy(dim->nested[1])))
309 goto error;
310 if (!dim->ids)
311 return dup;
312 dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
313 dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
314 dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
315 return dup;
316 error:
317 isl_space_free(dup);
318 return NULL;
321 __isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
323 if (!dim)
324 return NULL;
326 if (dim->ref == 1)
327 return dim;
328 dim->ref--;
329 return isl_space_dup(dim);
332 __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
334 if (!dim)
335 return NULL;
337 dim->ref++;
338 return dim;
341 __isl_null isl_space *isl_space_free(__isl_take isl_space *space)
343 int i;
345 if (!space)
346 return NULL;
348 if (--space->ref > 0)
349 return NULL;
351 isl_id_free(space->tuple_id[0]);
352 isl_id_free(space->tuple_id[1]);
354 isl_space_free(space->nested[0]);
355 isl_space_free(space->nested[1]);
357 for (i = 0; i < space->n_id; ++i)
358 isl_id_free(space->ids[i]);
359 free(space->ids);
360 isl_ctx_deref(space->ctx);
362 free(space);
364 return NULL;
367 /* Check if "s" is a valid dimension or tuple name.
368 * We currently only forbid names that look like a number.
370 * s is assumed to be non-NULL.
372 static int name_ok(isl_ctx *ctx, const char *s)
374 char *p;
375 long dummy;
377 dummy = strtol(s, &p, 0);
378 if (p != s)
379 isl_die(ctx, isl_error_invalid, "name looks like a number",
380 return 0);
382 return 1;
385 /* Is it possible for the given dimension type to have a tuple id?
387 static int space_can_have_id(__isl_keep isl_space *space,
388 enum isl_dim_type type)
390 if (!space)
391 return 0;
392 if (isl_space_is_params(space))
393 isl_die(space->ctx, isl_error_invalid,
394 "parameter spaces don't have tuple ids", return 0);
395 if (isl_space_is_set(space) && type != isl_dim_set)
396 isl_die(space->ctx, isl_error_invalid,
397 "set spaces can only have a set id", return 0);
398 if (type != isl_dim_in && type != isl_dim_out)
399 isl_die(space->ctx, isl_error_invalid,
400 "only input, output and set tuples can have ids",
401 return 0);
403 return 1;
406 /* Does the tuple have an id?
408 isl_bool isl_space_has_tuple_id(__isl_keep isl_space *dim,
409 enum isl_dim_type type)
411 if (!space_can_have_id(dim, type))
412 return isl_bool_error;
413 return dim->tuple_id[type - isl_dim_in] != NULL;
416 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
417 enum isl_dim_type type)
419 int has_id;
421 if (!dim)
422 return NULL;
423 has_id = isl_space_has_tuple_id(dim, type);
424 if (has_id < 0)
425 return NULL;
426 if (!has_id)
427 isl_die(dim->ctx, isl_error_invalid,
428 "tuple has no id", return NULL);
429 return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
432 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
433 enum isl_dim_type type, __isl_take isl_id *id)
435 dim = isl_space_cow(dim);
436 if (!dim || !id)
437 goto error;
438 if (type != isl_dim_in && type != isl_dim_out)
439 isl_die(dim->ctx, isl_error_invalid,
440 "only input, output and set tuples can have names",
441 goto error);
443 isl_id_free(dim->tuple_id[type - isl_dim_in]);
444 dim->tuple_id[type - isl_dim_in] = id;
446 return dim;
447 error:
448 isl_id_free(id);
449 isl_space_free(dim);
450 return NULL;
453 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
454 enum isl_dim_type type)
456 dim = isl_space_cow(dim);
457 if (!dim)
458 return NULL;
459 if (type != isl_dim_in && type != isl_dim_out)
460 isl_die(dim->ctx, isl_error_invalid,
461 "only input, output and set tuples can have names",
462 goto error);
464 isl_id_free(dim->tuple_id[type - isl_dim_in]);
465 dim->tuple_id[type - isl_dim_in] = NULL;
467 return dim;
468 error:
469 isl_space_free(dim);
470 return NULL;
473 /* Set the id of the given dimension of "space" to "id".
474 * If the dimension already has an id, then it is replaced.
475 * If the dimension is a parameter, then we need to change it
476 * in the nested spaces (if any) as well.
478 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
479 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
481 space = isl_space_cow(space);
482 if (!space || !id)
483 goto error;
485 if (type == isl_dim_param) {
486 int i;
488 for (i = 0; i < 2; ++i) {
489 if (!space->nested[i])
490 continue;
491 space->nested[i] =
492 isl_space_set_dim_id(space->nested[i],
493 type, pos, isl_id_copy(id));
494 if (!space->nested[i])
495 goto error;
499 isl_id_free(get_id(space, type, pos));
500 return set_id(space, type, pos, id);
501 error:
502 isl_id_free(id);
503 isl_space_free(space);
504 return NULL;
507 /* Reset the id of the given dimension of "space".
508 * If the dimension already has an id, then it is removed.
509 * If the dimension is a parameter, then we need to reset it
510 * in the nested spaces (if any) as well.
512 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
513 enum isl_dim_type type, unsigned pos)
515 space = isl_space_cow(space);
516 if (!space)
517 goto error;
519 if (type == isl_dim_param) {
520 int i;
522 for (i = 0; i < 2; ++i) {
523 if (!space->nested[i])
524 continue;
525 space->nested[i] =
526 isl_space_reset_dim_id(space->nested[i],
527 type, pos);
528 if (!space->nested[i])
529 goto error;
533 isl_id_free(get_id(space, type, pos));
534 return set_id(space, type, pos, NULL);
535 error:
536 isl_space_free(space);
537 return NULL;
540 isl_bool isl_space_has_dim_id(__isl_keep isl_space *dim,
541 enum isl_dim_type type, unsigned pos)
543 if (!dim)
544 return isl_bool_error;
545 return get_id(dim, type, pos) != NULL;
548 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
549 enum isl_dim_type type, unsigned pos)
551 if (!dim)
552 return NULL;
553 if (!get_id(dim, type, pos))
554 isl_die(dim->ctx, isl_error_invalid,
555 "dim has no id", return NULL);
556 return isl_id_copy(get_id(dim, type, pos));
559 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
560 enum isl_dim_type type, const char *s)
562 isl_id *id;
564 if (!dim)
565 return NULL;
567 if (!s)
568 return isl_space_reset_tuple_id(dim, type);
570 if (!name_ok(dim->ctx, s))
571 goto error;
573 id = isl_id_alloc(dim->ctx, s, NULL);
574 return isl_space_set_tuple_id(dim, type, id);
575 error:
576 isl_space_free(dim);
577 return NULL;
580 /* Does the tuple have a name?
582 isl_bool isl_space_has_tuple_name(__isl_keep isl_space *space,
583 enum isl_dim_type type)
585 isl_id *id;
587 if (!space_can_have_id(space, type))
588 return isl_bool_error;
589 id = space->tuple_id[type - isl_dim_in];
590 return id && id->name;
593 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
594 enum isl_dim_type type)
596 isl_id *id;
597 if (!dim)
598 return NULL;
599 if (type != isl_dim_in && type != isl_dim_out)
600 return NULL;
601 id = dim->tuple_id[type - isl_dim_in];
602 return id ? id->name : NULL;
605 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
606 enum isl_dim_type type, unsigned pos,
607 const char *s)
609 isl_id *id;
611 if (!dim)
612 return NULL;
613 if (!s)
614 return isl_space_reset_dim_id(dim, type, pos);
615 if (!name_ok(dim->ctx, s))
616 goto error;
617 id = isl_id_alloc(dim->ctx, s, NULL);
618 return isl_space_set_dim_id(dim, type, pos, id);
619 error:
620 isl_space_free(dim);
621 return NULL;
624 /* Does the given dimension have a name?
626 isl_bool isl_space_has_dim_name(__isl_keep isl_space *space,
627 enum isl_dim_type type, unsigned pos)
629 isl_id *id;
631 if (!space)
632 return isl_bool_error;
633 id = get_id(space, type, pos);
634 return id && id->name;
637 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
638 enum isl_dim_type type, unsigned pos)
640 isl_id *id = get_id(dim, type, pos);
641 return id ? id->name : NULL;
644 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
645 __isl_keep isl_id *id)
647 int i;
648 int offset;
649 int n;
651 if (!dim || !id)
652 return -1;
654 offset = isl_space_offset(dim, type);
655 n = isl_space_dim(dim, type);
656 for (i = 0; i < n && offset + i < dim->n_id; ++i)
657 if (dim->ids[offset + i] == id)
658 return i;
660 return -1;
663 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
664 enum isl_dim_type type, const char *name)
666 int i;
667 int offset;
668 int n;
670 if (!space || !name)
671 return -1;
673 offset = isl_space_offset(space, type);
674 n = isl_space_dim(space, type);
675 for (i = 0; i < n && offset + i < space->n_id; ++i) {
676 isl_id *id = get_id(space, type, i);
677 if (id && id->name && !strcmp(id->name, name))
678 return i;
681 return -1;
684 /* Reset the user pointer on all identifiers of parameters and tuples
685 * of "space".
687 __isl_give isl_space *isl_space_reset_user(__isl_take isl_space *space)
689 int i;
690 isl_ctx *ctx;
691 isl_id *id;
692 const char *name;
694 if (!space)
695 return NULL;
697 ctx = isl_space_get_ctx(space);
699 for (i = 0; i < space->nparam && i < space->n_id; ++i) {
700 if (!isl_id_get_user(space->ids[i]))
701 continue;
702 space = isl_space_cow(space);
703 if (!space)
704 return NULL;
705 name = isl_id_get_name(space->ids[i]);
706 id = isl_id_alloc(ctx, name, NULL);
707 isl_id_free(space->ids[i]);
708 space->ids[i] = id;
709 if (!id)
710 return isl_space_free(space);
713 for (i = 0; i < 2; ++i) {
714 if (!space->tuple_id[i])
715 continue;
716 if (!isl_id_get_user(space->tuple_id[i]))
717 continue;
718 space = isl_space_cow(space);
719 if (!space)
720 return NULL;
721 name = isl_id_get_name(space->tuple_id[i]);
722 id = isl_id_alloc(ctx, name, NULL);
723 isl_id_free(space->tuple_id[i]);
724 space->tuple_id[i] = id;
725 if (!id)
726 return isl_space_free(space);
729 for (i = 0; i < 2; ++i) {
730 if (!space->nested[i])
731 continue;
732 space = isl_space_cow(space);
733 if (!space)
734 return NULL;
735 space->nested[i] = isl_space_reset_user(space->nested[i]);
736 if (!space->nested[i])
737 return isl_space_free(space);
740 return space;
743 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
744 enum isl_dim_type type)
746 if (!dim)
747 return NULL;
748 if (type == isl_dim_in)
749 return dim->tuple_id[0];
750 if (type == isl_dim_out)
751 return dim->tuple_id[1];
752 return NULL;
755 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
756 enum isl_dim_type type)
758 if (!dim)
759 return NULL;
760 if (type == isl_dim_in)
761 return dim->nested[0];
762 if (type == isl_dim_out)
763 return dim->nested[1];
764 return NULL;
767 /* Are the two spaces the same, apart from positions and names of parameters?
769 isl_bool isl_space_has_equal_tuples(__isl_keep isl_space *space1,
770 __isl_keep isl_space *space2)
772 if (!space1 || !space2)
773 return isl_bool_error;
774 if (space1 == space2)
775 return isl_bool_true;
776 return isl_space_tuple_is_equal(space1, isl_dim_in,
777 space2, isl_dim_in) &&
778 isl_space_tuple_is_equal(space1, isl_dim_out,
779 space2, isl_dim_out);
782 /* Check if the tuple of type "type1" of "space1" is the same as
783 * the tuple of type "type2" of "space2".
785 * That is, check if the tuples have the same identifier, the same dimension
786 * and the same internal structure.
787 * The identifiers of the dimensions inside the tuples do not affect the result.
789 * Note that this function only checks the tuples themselves.
790 * If nested tuples are involved, then we need to be careful not
791 * to have result affected by possibly differing parameters
792 * in those nested tuples.
794 isl_bool isl_space_tuple_is_equal(__isl_keep isl_space *space1,
795 enum isl_dim_type type1, __isl_keep isl_space *space2,
796 enum isl_dim_type type2)
798 isl_id *id1, *id2;
799 isl_space *nested1, *nested2;
801 if (!space1 || !space2)
802 return isl_bool_error;
804 if (space1 == space2 && type1 == type2)
805 return isl_bool_true;
807 if (n(space1, type1) != n(space2, type2))
808 return isl_bool_false;
809 id1 = tuple_id(space1, type1);
810 id2 = tuple_id(space2, type2);
811 if (!id1 ^ !id2)
812 return isl_bool_false;
813 if (id1 && id1 != id2)
814 return isl_bool_false;
815 nested1 = nested(space1, type1);
816 nested2 = nested(space2, type2);
817 if (!nested1 ^ !nested2)
818 return isl_bool_false;
819 if (nested1 && !isl_space_has_equal_tuples(nested1, nested2))
820 return isl_bool_false;
821 return isl_bool_true;
824 /* This is the old, undocumented, name for isl_space_tuple_is_equal.
825 * It will be removed at some point.
827 int isl_space_tuple_match(__isl_keep isl_space *space1, enum isl_dim_type type1,
828 __isl_keep isl_space *space2, enum isl_dim_type type2)
830 return isl_space_tuple_is_equal(space1, type1, space2, type2);
833 static isl_bool match(__isl_keep isl_space *space1, enum isl_dim_type type1,
834 __isl_keep isl_space *space2, enum isl_dim_type type2)
836 int i;
838 if (space1 == space2 && type1 == type2)
839 return isl_bool_true;
841 if (!isl_space_tuple_is_equal(space1, type1, space2, type2))
842 return isl_bool_false;
844 if (!space1->ids && !space2->ids)
845 return isl_bool_true;
847 for (i = 0; i < n(space1, type1); ++i) {
848 if (get_id(space1, type1, i) != get_id(space2, type2, i))
849 return isl_bool_false;
851 return isl_bool_true;
854 /* Do "space1" and "space2" have the same parameters?
856 isl_bool isl_space_has_equal_params(__isl_keep isl_space *space1,
857 __isl_keep isl_space *space2)
859 if (!space1 || !space2)
860 return isl_bool_error;
862 return match(space1, isl_dim_param, space2, isl_dim_param);
865 /* Do "space1" and "space2" have the same identifiers for all
866 * the tuple variables?
868 isl_bool isl_space_has_equal_ids(__isl_keep isl_space *space1,
869 __isl_keep isl_space *space2)
871 isl_bool equal;
873 if (!space1 || !space2)
874 return isl_bool_error;
876 equal = match(space1, isl_dim_in, space2, isl_dim_in);
877 if (equal < 0 || !equal)
878 return equal;
879 return match(space1, isl_dim_out, space2, isl_dim_out);
882 isl_bool isl_space_match(__isl_keep isl_space *space1, enum isl_dim_type type1,
883 __isl_keep isl_space *space2, enum isl_dim_type type2)
885 if (!space1 || !space2)
886 return isl_bool_error;
888 return match(space1, type1, space2, type2);
891 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
892 unsigned first, unsigned n, __isl_keep isl_id **ids)
894 int i;
896 for (i = 0; i < n ; ++i)
897 ids[i] = get_id(dim, type, first + i);
900 static __isl_give isl_space *space_extend(__isl_take isl_space *space,
901 unsigned nparam, unsigned n_in, unsigned n_out)
903 isl_id **ids = NULL;
905 if (!space)
906 return NULL;
907 if (space->nparam == nparam &&
908 space->n_in == n_in && space->n_out == n_out)
909 return space;
911 isl_assert(space->ctx, space->nparam <= nparam, goto error);
912 isl_assert(space->ctx, space->n_in <= n_in, goto error);
913 isl_assert(space->ctx, space->n_out <= n_out, goto error);
915 space = isl_space_cow(space);
916 if (!space)
917 goto error;
919 if (space->ids) {
920 unsigned n;
921 n = nparam + n_in + n_out;
922 if (n < nparam || n < n_in || n < n_out)
923 isl_die(isl_space_get_ctx(space), isl_error_invalid,
924 "overflow in total number of dimensions",
925 goto error);
926 ids = isl_calloc_array(space->ctx, isl_id *, n);
927 if (!ids)
928 goto error;
929 get_ids(space, isl_dim_param, 0, space->nparam, ids);
930 get_ids(space, isl_dim_in, 0, space->n_in, ids + nparam);
931 get_ids(space, isl_dim_out, 0, space->n_out,
932 ids + nparam + n_in);
933 free(space->ids);
934 space->ids = ids;
935 space->n_id = nparam + n_in + n_out;
937 space->nparam = nparam;
938 space->n_in = n_in;
939 space->n_out = n_out;
941 return space;
942 error:
943 free(ids);
944 isl_space_free(space);
945 return NULL;
948 __isl_give isl_space *isl_space_extend(__isl_take isl_space *space,
949 unsigned nparam, unsigned n_in, unsigned n_out)
951 return space_extend(space, nparam, n_in, n_out);
954 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *space,
955 enum isl_dim_type type, unsigned n)
957 space = isl_space_reset(space, type);
958 if (!space)
959 return NULL;
960 switch (type) {
961 case isl_dim_param:
962 space = space_extend(space,
963 space->nparam + n, space->n_in, space->n_out);
964 if (space && space->nested[0] &&
965 !(space->nested[0] = isl_space_add_dims(space->nested[0],
966 isl_dim_param, n)))
967 goto error;
968 if (space && space->nested[1] &&
969 !(space->nested[1] = isl_space_add_dims(space->nested[1],
970 isl_dim_param, n)))
971 goto error;
972 return space;
973 case isl_dim_in:
974 return space_extend(space,
975 space->nparam, space->n_in + n, space->n_out);
976 case isl_dim_out:
977 return space_extend(space,
978 space->nparam, space->n_in, space->n_out + n);
979 default:
980 isl_die(space->ctx, isl_error_invalid,
981 "cannot add dimensions of specified type", goto error);
983 error:
984 isl_space_free(space);
985 return NULL;
988 static int valid_dim_type(enum isl_dim_type type)
990 switch (type) {
991 case isl_dim_param:
992 case isl_dim_in:
993 case isl_dim_out:
994 return 1;
995 default:
996 return 0;
1000 /* Insert "n" dimensions of type "type" at position "pos".
1001 * If we are inserting parameters, then they are also inserted in
1002 * any nested spaces.
1004 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
1005 enum isl_dim_type type, unsigned pos, unsigned n)
1007 isl_id **ids = NULL;
1009 if (!dim)
1010 return NULL;
1011 if (n == 0)
1012 return isl_space_reset(dim, type);
1014 if (!valid_dim_type(type))
1015 isl_die(dim->ctx, isl_error_invalid,
1016 "cannot insert dimensions of specified type",
1017 goto error);
1019 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
1021 dim = isl_space_cow(dim);
1022 if (!dim)
1023 return NULL;
1025 if (dim->ids) {
1026 enum isl_dim_type t, o = isl_dim_param;
1027 int off;
1028 int s[3];
1029 ids = isl_calloc_array(dim->ctx, isl_id *,
1030 dim->nparam + dim->n_in + dim->n_out + n);
1031 if (!ids)
1032 goto error;
1033 off = 0;
1034 s[isl_dim_param - o] = dim->nparam;
1035 s[isl_dim_in - o] = dim->n_in;
1036 s[isl_dim_out - o] = dim->n_out;
1037 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1038 if (t != type) {
1039 get_ids(dim, t, 0, s[t - o], ids + off);
1040 off += s[t - o];
1041 } else {
1042 get_ids(dim, t, 0, pos, ids + off);
1043 off += pos + n;
1044 get_ids(dim, t, pos, s[t - o] - pos, ids + off);
1045 off += s[t - o] - pos;
1048 free(dim->ids);
1049 dim->ids = ids;
1050 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
1052 switch (type) {
1053 case isl_dim_param: dim->nparam += n; break;
1054 case isl_dim_in: dim->n_in += n; break;
1055 case isl_dim_out: dim->n_out += n; break;
1056 default: ;
1058 dim = isl_space_reset(dim, type);
1060 if (type == isl_dim_param) {
1061 if (dim && dim->nested[0] &&
1062 !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
1063 isl_dim_param, pos, n)))
1064 goto error;
1065 if (dim && dim->nested[1] &&
1066 !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
1067 isl_dim_param, pos, n)))
1068 goto error;
1071 return dim;
1072 error:
1073 isl_space_free(dim);
1074 return NULL;
1077 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
1078 enum isl_dim_type dst_type, unsigned dst_pos,
1079 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1081 int i;
1083 if (!dim)
1084 return NULL;
1085 if (n == 0) {
1086 dim = isl_space_reset(dim, src_type);
1087 return isl_space_reset(dim, dst_type);
1090 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
1091 goto error);
1093 if (dst_type == src_type && dst_pos == src_pos)
1094 return dim;
1096 isl_assert(dim->ctx, dst_type != src_type, goto error);
1098 dim = isl_space_reset(dim, src_type);
1099 dim = isl_space_reset(dim, dst_type);
1101 dim = isl_space_cow(dim);
1102 if (!dim)
1103 return NULL;
1105 if (dim->ids) {
1106 isl_id **ids;
1107 enum isl_dim_type t, o = isl_dim_param;
1108 int off;
1109 int s[3];
1110 ids = isl_calloc_array(dim->ctx, isl_id *,
1111 dim->nparam + dim->n_in + dim->n_out);
1112 if (!ids)
1113 goto error;
1114 off = 0;
1115 s[isl_dim_param - o] = dim->nparam;
1116 s[isl_dim_in - o] = dim->n_in;
1117 s[isl_dim_out - o] = dim->n_out;
1118 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1119 if (t == dst_type) {
1120 get_ids(dim, t, 0, dst_pos, ids + off);
1121 off += dst_pos;
1122 get_ids(dim, src_type, src_pos, n, ids + off);
1123 off += n;
1124 get_ids(dim, t, dst_pos, s[t - o] - dst_pos,
1125 ids + off);
1126 off += s[t - o] - dst_pos;
1127 } else if (t == src_type) {
1128 get_ids(dim, t, 0, src_pos, ids + off);
1129 off += src_pos;
1130 get_ids(dim, t, src_pos + n,
1131 s[t - o] - src_pos - n, ids + off);
1132 off += s[t - o] - src_pos - n;
1133 } else {
1134 get_ids(dim, t, 0, s[t - o], ids + off);
1135 off += s[t - o];
1138 free(dim->ids);
1139 dim->ids = ids;
1140 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
1143 switch (dst_type) {
1144 case isl_dim_param: dim->nparam += n; break;
1145 case isl_dim_in: dim->n_in += n; break;
1146 case isl_dim_out: dim->n_out += n; break;
1147 default: ;
1150 switch (src_type) {
1151 case isl_dim_param: dim->nparam -= n; break;
1152 case isl_dim_in: dim->n_in -= n; break;
1153 case isl_dim_out: dim->n_out -= n; break;
1154 default: ;
1157 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1158 return dim;
1160 for (i = 0; i < 2; ++i) {
1161 if (!dim->nested[i])
1162 continue;
1163 dim->nested[i] = isl_space_replace(dim->nested[i],
1164 isl_dim_param, dim);
1165 if (!dim->nested[i])
1166 goto error;
1169 return dim;
1170 error:
1171 isl_space_free(dim);
1172 return NULL;
1175 /* Check that "space1" and "space2" have the same parameters,
1176 * reporting an error if they do not.
1178 isl_stat isl_space_check_equal_params(__isl_keep isl_space *space1,
1179 __isl_keep isl_space *space2)
1181 isl_bool equal;
1183 equal = isl_space_has_equal_params(space1, space2);
1184 if (equal < 0)
1185 return isl_stat_error;
1186 if (!equal)
1187 isl_die(isl_space_get_ctx(space1), isl_error_invalid,
1188 "parameters need to match", return isl_stat_error);
1189 return isl_stat_ok;
1192 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1193 __isl_take isl_space *right)
1195 isl_space *dim;
1197 if (isl_space_check_equal_params(left, right) < 0)
1198 goto error;
1200 isl_assert(left->ctx,
1201 isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_in),
1202 goto error);
1204 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1205 if (!dim)
1206 goto error;
1208 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1209 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1210 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1212 if (dim && left->tuple_id[0] &&
1213 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1214 goto error;
1215 if (dim && right->tuple_id[1] &&
1216 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1217 goto error;
1218 if (dim && left->nested[0] &&
1219 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1220 goto error;
1221 if (dim && right->nested[1] &&
1222 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1223 goto error;
1225 isl_space_free(left);
1226 isl_space_free(right);
1228 return dim;
1229 error:
1230 isl_space_free(left);
1231 isl_space_free(right);
1232 return NULL;
1235 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1236 * { [A -> B] -> [C -> D] }.
1237 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1239 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1240 __isl_take isl_space *right)
1242 isl_space *dom1, *dom2, *nest1, *nest2;
1243 int is_set;
1245 if (!left || !right)
1246 goto error;
1248 is_set = isl_space_is_set(left);
1249 if (is_set != isl_space_is_set(right))
1250 isl_die(isl_space_get_ctx(left), isl_error_invalid,
1251 "expecting either two set spaces or two map spaces",
1252 goto error);
1253 if (is_set)
1254 return isl_space_range_product(left, right);
1256 if (isl_space_check_equal_params(left, right) < 0)
1257 goto error;
1259 dom1 = isl_space_domain(isl_space_copy(left));
1260 dom2 = isl_space_domain(isl_space_copy(right));
1261 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1263 dom1 = isl_space_range(left);
1264 dom2 = isl_space_range(right);
1265 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1267 return isl_space_join(isl_space_reverse(nest1), nest2);
1268 error:
1269 isl_space_free(left);
1270 isl_space_free(right);
1271 return NULL;
1274 /* Given two spaces { A -> C } and { B -> C }, construct the space
1275 * { [A -> B] -> C }
1277 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1278 __isl_take isl_space *right)
1280 isl_space *ran, *dom1, *dom2, *nest;
1282 if (isl_space_check_equal_params(left, right) < 0)
1283 goto error;
1285 if (!isl_space_tuple_is_equal(left, isl_dim_out, right, isl_dim_out))
1286 isl_die(left->ctx, isl_error_invalid,
1287 "ranges need to match", goto error);
1289 ran = isl_space_range(isl_space_copy(left));
1291 dom1 = isl_space_domain(left);
1292 dom2 = isl_space_domain(right);
1293 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1295 return isl_space_join(isl_space_reverse(nest), ran);
1296 error:
1297 isl_space_free(left);
1298 isl_space_free(right);
1299 return NULL;
1302 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1303 __isl_take isl_space *right)
1305 isl_space *dom, *ran1, *ran2, *nest;
1307 if (isl_space_check_equal_params(left, right) < 0)
1308 goto error;
1310 if (!isl_space_tuple_is_equal(left, isl_dim_in, right, isl_dim_in))
1311 isl_die(left->ctx, isl_error_invalid,
1312 "domains need to match", goto error);
1314 dom = isl_space_domain(isl_space_copy(left));
1316 ran1 = isl_space_range(left);
1317 ran2 = isl_space_range(right);
1318 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1320 return isl_space_join(isl_space_reverse(dom), nest);
1321 error:
1322 isl_space_free(left);
1323 isl_space_free(right);
1324 return NULL;
1327 /* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1329 __isl_give isl_space *isl_space_factor_domain(__isl_take isl_space *space)
1331 space = isl_space_domain_factor_domain(space);
1332 space = isl_space_range_factor_domain(space);
1333 return space;
1336 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1338 __isl_give isl_space *isl_space_domain_factor_domain(
1339 __isl_take isl_space *space)
1341 isl_space *nested;
1342 isl_space *domain;
1344 if (!space)
1345 return NULL;
1346 if (!isl_space_domain_is_wrapping(space))
1347 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1348 "domain not a product", return isl_space_free(space));
1350 nested = space->nested[0];
1351 domain = isl_space_copy(space);
1352 domain = isl_space_drop_dims(domain, isl_dim_in,
1353 nested->n_in, nested->n_out);
1354 if (!domain)
1355 return isl_space_free(space);
1356 if (nested->tuple_id[0]) {
1357 domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
1358 if (!domain->tuple_id[0])
1359 goto error;
1361 if (nested->nested[0]) {
1362 domain->nested[0] = isl_space_copy(nested->nested[0]);
1363 if (!domain->nested[0])
1364 goto error;
1367 isl_space_free(space);
1368 return domain;
1369 error:
1370 isl_space_free(space);
1371 isl_space_free(domain);
1372 return NULL;
1375 /* Given a space of the form [A -> B] -> C, return the space B -> C.
1377 __isl_give isl_space *isl_space_domain_factor_range(
1378 __isl_take isl_space *space)
1380 isl_space *nested;
1381 isl_space *range;
1383 if (!space)
1384 return NULL;
1385 if (!isl_space_domain_is_wrapping(space))
1386 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1387 "domain not a product", return isl_space_free(space));
1389 nested = space->nested[0];
1390 range = isl_space_copy(space);
1391 range = isl_space_drop_dims(range, isl_dim_in, 0, nested->n_in);
1392 if (!range)
1393 return isl_space_free(space);
1394 if (nested->tuple_id[1]) {
1395 range->tuple_id[0] = isl_id_copy(nested->tuple_id[1]);
1396 if (!range->tuple_id[0])
1397 goto error;
1399 if (nested->nested[1]) {
1400 range->nested[0] = isl_space_copy(nested->nested[1]);
1401 if (!range->nested[0])
1402 goto error;
1405 isl_space_free(space);
1406 return range;
1407 error:
1408 isl_space_free(space);
1409 isl_space_free(range);
1410 return NULL;
1413 /* Given a space of the form A -> [B -> C], return the space A -> B.
1415 __isl_give isl_space *isl_space_range_factor_domain(
1416 __isl_take isl_space *space)
1418 isl_space *nested;
1419 isl_space *domain;
1421 if (!space)
1422 return NULL;
1423 if (!isl_space_range_is_wrapping(space))
1424 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1425 "range not a product", return isl_space_free(space));
1427 nested = space->nested[1];
1428 domain = isl_space_copy(space);
1429 domain = isl_space_drop_dims(domain, isl_dim_out,
1430 nested->n_in, nested->n_out);
1431 if (!domain)
1432 return isl_space_free(space);
1433 if (nested->tuple_id[0]) {
1434 domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
1435 if (!domain->tuple_id[1])
1436 goto error;
1438 if (nested->nested[0]) {
1439 domain->nested[1] = isl_space_copy(nested->nested[0]);
1440 if (!domain->nested[1])
1441 goto error;
1444 isl_space_free(space);
1445 return domain;
1446 error:
1447 isl_space_free(space);
1448 isl_space_free(domain);
1449 return NULL;
1452 /* Internal function that selects the range of the map that is
1453 * embedded in either a set space or the range of a map space.
1454 * In particular, given a space of the form [A -> B], return the space B.
1455 * Given a space of the form A -> [B -> C], return the space A -> C.
1457 static __isl_give isl_space *range_factor_range(__isl_take isl_space *space)
1459 isl_space *nested;
1460 isl_space *range;
1462 if (!space)
1463 return NULL;
1465 nested = space->nested[1];
1466 range = isl_space_copy(space);
1467 range = isl_space_drop_dims(range, isl_dim_out, 0, nested->n_in);
1468 if (!range)
1469 return isl_space_free(space);
1470 if (nested->tuple_id[1]) {
1471 range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
1472 if (!range->tuple_id[1])
1473 goto error;
1475 if (nested->nested[1]) {
1476 range->nested[1] = isl_space_copy(nested->nested[1]);
1477 if (!range->nested[1])
1478 goto error;
1481 isl_space_free(space);
1482 return range;
1483 error:
1484 isl_space_free(space);
1485 isl_space_free(range);
1486 return NULL;
1489 /* Given a space of the form A -> [B -> C], return the space A -> C.
1491 __isl_give isl_space *isl_space_range_factor_range(
1492 __isl_take isl_space *space)
1494 if (!space)
1495 return NULL;
1496 if (!isl_space_range_is_wrapping(space))
1497 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1498 "range not a product", return isl_space_free(space));
1500 return range_factor_range(space);
1503 /* Given a space of the form [A -> B], return the space B.
1505 static __isl_give isl_space *set_factor_range(__isl_take isl_space *space)
1507 if (!space)
1508 return NULL;
1509 if (!isl_space_is_wrapping(space))
1510 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1511 "not a product", return isl_space_free(space));
1513 return range_factor_range(space);
1516 /* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1517 * Given a space of the form [A -> B], return the space B.
1519 __isl_give isl_space *isl_space_factor_range(__isl_take isl_space *space)
1521 if (!space)
1522 return NULL;
1523 if (isl_space_is_set(space))
1524 return set_factor_range(space);
1525 space = isl_space_domain_factor_range(space);
1526 space = isl_space_range_factor_range(space);
1527 return space;
1530 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1532 isl_ctx *ctx;
1533 isl_id **ids = NULL;
1535 if (!dim)
1536 return NULL;
1537 ctx = isl_space_get_ctx(dim);
1538 if (!isl_space_is_set(dim))
1539 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1540 dim = isl_space_cow(dim);
1541 if (!dim)
1542 return NULL;
1543 if (dim->ids) {
1544 ids = isl_calloc_array(dim->ctx, isl_id *,
1545 dim->nparam + dim->n_out + dim->n_out);
1546 if (!ids)
1547 goto error;
1548 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1549 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1551 dim->n_in = dim->n_out;
1552 if (ids) {
1553 free(dim->ids);
1554 dim->ids = ids;
1555 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1556 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1558 isl_id_free(dim->tuple_id[0]);
1559 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1560 isl_space_free(dim->nested[0]);
1561 dim->nested[0] = isl_space_copy(dim->nested[1]);
1562 return dim;
1563 error:
1564 isl_space_free(dim);
1565 return NULL;
1568 __isl_give isl_space *isl_space_map_from_domain_and_range(
1569 __isl_take isl_space *domain, __isl_take isl_space *range)
1571 if (!domain || !range)
1572 goto error;
1573 if (!isl_space_is_set(domain))
1574 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1575 "domain is not a set space", goto error);
1576 if (!isl_space_is_set(range))
1577 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1578 "range is not a set space", goto error);
1579 return isl_space_join(isl_space_reverse(domain), range);
1580 error:
1581 isl_space_free(domain);
1582 isl_space_free(range);
1583 return NULL;
1586 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1587 enum isl_dim_type type,
1588 unsigned first, unsigned n, __isl_take isl_id **ids)
1590 int i;
1592 for (i = 0; i < n ; ++i)
1593 dim = set_id(dim, type, first + i, ids[i]);
1595 return dim;
1598 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1600 unsigned t;
1601 isl_space *nested;
1602 isl_id **ids = NULL;
1603 isl_id *id;
1605 if (!dim)
1606 return NULL;
1607 if (match(dim, isl_dim_in, dim, isl_dim_out))
1608 return dim;
1610 dim = isl_space_cow(dim);
1611 if (!dim)
1612 return NULL;
1614 id = dim->tuple_id[0];
1615 dim->tuple_id[0] = dim->tuple_id[1];
1616 dim->tuple_id[1] = id;
1618 nested = dim->nested[0];
1619 dim->nested[0] = dim->nested[1];
1620 dim->nested[1] = nested;
1622 if (dim->ids) {
1623 int n_id = dim->n_in + dim->n_out;
1624 ids = isl_alloc_array(dim->ctx, isl_id *, n_id);
1625 if (n_id && !ids)
1626 goto error;
1627 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1628 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1631 t = dim->n_in;
1632 dim->n_in = dim->n_out;
1633 dim->n_out = t;
1635 if (dim->ids) {
1636 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1637 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1638 free(ids);
1641 return dim;
1642 error:
1643 free(ids);
1644 isl_space_free(dim);
1645 return NULL;
1648 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1649 enum isl_dim_type type, unsigned first, unsigned num)
1651 int i;
1653 if (!dim)
1654 return NULL;
1656 if (num == 0)
1657 return isl_space_reset(dim, type);
1659 if (!valid_dim_type(type))
1660 isl_die(dim->ctx, isl_error_invalid,
1661 "cannot drop dimensions of specified type", goto error);
1663 if (first + num > n(dim, type) || first + num < first)
1664 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1665 "index out of bounds", return isl_space_free(dim));
1666 dim = isl_space_cow(dim);
1667 if (!dim)
1668 goto error;
1669 if (dim->ids) {
1670 dim = extend_ids(dim);
1671 if (!dim)
1672 goto error;
1673 for (i = 0; i < num; ++i)
1674 isl_id_free(get_id(dim, type, first + i));
1675 for (i = first+num; i < n(dim, type); ++i)
1676 set_id(dim, type, i - num, get_id(dim, type, i));
1677 switch (type) {
1678 case isl_dim_param:
1679 get_ids(dim, isl_dim_in, 0, dim->n_in,
1680 dim->ids + offset(dim, isl_dim_in) - num);
1681 case isl_dim_in:
1682 get_ids(dim, isl_dim_out, 0, dim->n_out,
1683 dim->ids + offset(dim, isl_dim_out) - num);
1684 default:
1687 dim->n_id -= num;
1689 switch (type) {
1690 case isl_dim_param: dim->nparam -= num; break;
1691 case isl_dim_in: dim->n_in -= num; break;
1692 case isl_dim_out: dim->n_out -= num; break;
1693 default: ;
1695 dim = isl_space_reset(dim, type);
1696 if (type == isl_dim_param) {
1697 if (dim && dim->nested[0] &&
1698 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1699 isl_dim_param, first, num)))
1700 goto error;
1701 if (dim && dim->nested[1] &&
1702 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1703 isl_dim_param, first, num)))
1704 goto error;
1706 return dim;
1707 error:
1708 isl_space_free(dim);
1709 return NULL;
1712 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1713 unsigned first, unsigned n)
1715 if (!dim)
1716 return NULL;
1717 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1720 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1721 unsigned first, unsigned n)
1723 if (!dim)
1724 return NULL;
1725 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1728 __isl_give isl_space *isl_space_domain(__isl_take isl_space *space)
1730 if (!space)
1731 return NULL;
1732 space = isl_space_drop_dims(space, isl_dim_out, 0, space->n_out);
1733 space = isl_space_reverse(space);
1734 space = mark_as_set(space);
1735 return space;
1738 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1740 if (!dim)
1741 return NULL;
1742 if (!isl_space_is_set(dim))
1743 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1744 "not a set space", goto error);
1745 dim = isl_space_reverse(dim);
1746 dim = isl_space_reset(dim, isl_dim_out);
1747 return dim;
1748 error:
1749 isl_space_free(dim);
1750 return NULL;
1753 __isl_give isl_space *isl_space_range(__isl_take isl_space *space)
1755 if (!space)
1756 return NULL;
1757 space = isl_space_drop_dims(space, isl_dim_in, 0, space->n_in);
1758 space = mark_as_set(space);
1759 return space;
1762 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1764 if (!dim)
1765 return NULL;
1766 if (!isl_space_is_set(dim))
1767 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1768 "not a set space", goto error);
1769 return isl_space_reset(dim, isl_dim_in);
1770 error:
1771 isl_space_free(dim);
1772 return NULL;
1775 /* Given a map space A -> B, return the map space [A -> B] -> A.
1777 __isl_give isl_space *isl_space_domain_map(__isl_take isl_space *space)
1779 isl_space *domain;
1781 domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1782 space = isl_space_from_domain(isl_space_wrap(space));
1783 space = isl_space_join(space, domain);
1785 return space;
1788 /* Given a map space A -> B, return the map space [A -> B] -> B.
1790 __isl_give isl_space *isl_space_range_map(__isl_take isl_space *space)
1792 isl_space *range;
1794 range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1795 space = isl_space_from_domain(isl_space_wrap(space));
1796 space = isl_space_join(space, range);
1798 return space;
1801 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1803 if (isl_space_is_params(space))
1804 return space;
1805 space = isl_space_drop_dims(space,
1806 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1807 space = isl_space_drop_dims(space,
1808 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1809 space = mark_as_params(space);
1810 return space;
1813 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1815 if (!space)
1816 return NULL;
1817 if (!isl_space_is_params(space))
1818 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1819 "not a parameter space", goto error);
1820 return isl_space_reset(space, isl_dim_set);
1821 error:
1822 isl_space_free(space);
1823 return NULL;
1826 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1827 unsigned n_div)
1829 int i;
1831 if (!dim)
1832 return NULL;
1833 if (n_div == 0 &&
1834 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1835 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1836 dim = isl_space_cow(dim);
1837 if (!dim)
1838 return NULL;
1839 dim->n_out += dim->nparam + dim->n_in + n_div;
1840 dim->nparam = 0;
1841 dim->n_in = 0;
1843 for (i = 0; i < dim->n_id; ++i)
1844 isl_id_free(get_id(dim, isl_dim_out, i));
1845 dim->n_id = 0;
1846 dim = isl_space_reset(dim, isl_dim_in);
1847 dim = isl_space_reset(dim, isl_dim_out);
1849 return dim;
1852 /* Are the two spaces the same, including positions and names of parameters?
1854 isl_bool isl_space_is_equal(__isl_keep isl_space *space1,
1855 __isl_keep isl_space *space2)
1857 isl_bool equal;
1859 if (!space1 || !space2)
1860 return isl_bool_error;
1861 if (space1 == space2)
1862 return isl_bool_true;
1863 equal = isl_space_has_equal_params(space1, space2);
1864 if (equal < 0 || !equal)
1865 return equal;
1866 return isl_space_has_equal_tuples(space1, space2);
1869 /* Is space1 equal to the domain of space2?
1871 * In the internal version we also allow space2 to be the space of a set,
1872 * provided space1 is a parameter space.
1874 isl_bool isl_space_is_domain_internal(__isl_keep isl_space *space1,
1875 __isl_keep isl_space *space2)
1877 isl_bool equal_params;
1879 if (!space1 || !space2)
1880 return isl_bool_error;
1881 if (!isl_space_is_set(space1))
1882 return isl_bool_false;
1883 equal_params = isl_space_has_equal_params(space1, space2);
1884 if (equal_params < 0 || !equal_params)
1885 return equal_params;
1886 return isl_space_tuple_is_equal(space1, isl_dim_set,
1887 space2, isl_dim_in);
1890 /* Is space1 equal to the domain of space2?
1892 isl_bool isl_space_is_domain(__isl_keep isl_space *space1,
1893 __isl_keep isl_space *space2)
1895 if (!space2)
1896 return isl_bool_error;
1897 if (!isl_space_is_map(space2))
1898 return isl_bool_false;
1899 return isl_space_is_domain_internal(space1, space2);
1902 /* Is space1 equal to the range of space2?
1904 * In the internal version, space2 is allowed to be the space of a set,
1905 * in which case it should be equal to space1.
1907 isl_bool isl_space_is_range_internal(__isl_keep isl_space *space1,
1908 __isl_keep isl_space *space2)
1910 isl_bool equal_params;
1912 if (!space1 || !space2)
1913 return isl_bool_error;
1914 if (!isl_space_is_set(space1))
1915 return isl_bool_false;
1916 equal_params = isl_space_has_equal_params(space1, space2);
1917 if (equal_params < 0 || !equal_params)
1918 return equal_params;
1919 return isl_space_tuple_is_equal(space1, isl_dim_set,
1920 space2, isl_dim_out);
1923 /* Is space1 equal to the range of space2?
1925 isl_bool isl_space_is_range(__isl_keep isl_space *space1,
1926 __isl_keep isl_space *space2)
1928 if (!space2)
1929 return isl_bool_error;
1930 if (!isl_space_is_map(space2))
1931 return isl_bool_false;
1932 return isl_space_is_range_internal(space1, space2);
1935 /* Update "hash" by hashing in "space".
1936 * Changes in this function should be reflected in isl_hash_space_domain.
1938 static uint32_t isl_hash_space(uint32_t hash, __isl_keep isl_space *space)
1940 int i;
1941 isl_id *id;
1943 if (!space)
1944 return hash;
1946 isl_hash_byte(hash, space->nparam % 256);
1947 isl_hash_byte(hash, space->n_in % 256);
1948 isl_hash_byte(hash, space->n_out % 256);
1950 for (i = 0; i < space->nparam; ++i) {
1951 id = get_id(space, isl_dim_param, i);
1952 hash = isl_hash_id(hash, id);
1955 id = tuple_id(space, isl_dim_in);
1956 hash = isl_hash_id(hash, id);
1957 id = tuple_id(space, isl_dim_out);
1958 hash = isl_hash_id(hash, id);
1960 hash = isl_hash_space(hash, space->nested[0]);
1961 hash = isl_hash_space(hash, space->nested[1]);
1963 return hash;
1966 /* Update "hash" by hashing in the domain of "space".
1967 * The result of this function is equal to the result of applying
1968 * isl_hash_space to the domain of "space".
1970 static uint32_t isl_hash_space_domain(uint32_t hash,
1971 __isl_keep isl_space *space)
1973 int i;
1974 isl_id *id;
1976 if (!space)
1977 return hash;
1979 isl_hash_byte(hash, space->nparam % 256);
1980 isl_hash_byte(hash, 0);
1981 isl_hash_byte(hash, space->n_in % 256);
1983 for (i = 0; i < space->nparam; ++i) {
1984 id = get_id(space, isl_dim_param, i);
1985 hash = isl_hash_id(hash, id);
1988 hash = isl_hash_id(hash, &isl_id_none);
1989 id = tuple_id(space, isl_dim_in);
1990 hash = isl_hash_id(hash, id);
1992 hash = isl_hash_space(hash, space->nested[0]);
1994 return hash;
1997 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1999 uint32_t hash;
2001 if (!dim)
2002 return 0;
2004 hash = isl_hash_init();
2005 hash = isl_hash_space(hash, dim);
2007 return hash;
2010 /* Return the hash value of the domain of "space".
2011 * That is, isl_space_get_domain_hash(space) is equal to
2012 * isl_space_get_hash(isl_space_domain(space)).
2014 uint32_t isl_space_get_domain_hash(__isl_keep isl_space *space)
2016 uint32_t hash;
2018 if (!space)
2019 return 0;
2021 hash = isl_hash_init();
2022 hash = isl_hash_space_domain(hash, space);
2024 return hash;
2027 isl_bool isl_space_is_wrapping(__isl_keep isl_space *dim)
2029 if (!dim)
2030 return isl_bool_error;
2032 if (!isl_space_is_set(dim))
2033 return isl_bool_false;
2035 return dim->nested[1] != NULL;
2038 /* Is "space" the space of a map where the domain is a wrapped map space?
2040 isl_bool isl_space_domain_is_wrapping(__isl_keep isl_space *space)
2042 if (!space)
2043 return isl_bool_error;
2045 if (isl_space_is_set(space))
2046 return isl_bool_false;
2048 return space->nested[0] != NULL;
2051 /* Is "space" the space of a map where the range is a wrapped map space?
2053 isl_bool isl_space_range_is_wrapping(__isl_keep isl_space *space)
2055 if (!space)
2056 return isl_bool_error;
2058 if (isl_space_is_set(space))
2059 return isl_bool_false;
2061 return space->nested[1] != NULL;
2064 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
2066 isl_space *wrap;
2068 if (!dim)
2069 return NULL;
2071 wrap = isl_space_set_alloc(dim->ctx,
2072 dim->nparam, dim->n_in + dim->n_out);
2074 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
2075 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
2076 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
2078 if (!wrap)
2079 goto error;
2081 wrap->nested[1] = dim;
2083 return wrap;
2084 error:
2085 isl_space_free(dim);
2086 return NULL;
2089 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
2091 isl_space *unwrap;
2093 if (!dim)
2094 return NULL;
2096 if (!isl_space_is_wrapping(dim))
2097 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
2098 goto error);
2100 unwrap = isl_space_copy(dim->nested[1]);
2101 isl_space_free(dim);
2103 return unwrap;
2104 error:
2105 isl_space_free(dim);
2106 return NULL;
2109 isl_bool isl_space_is_named_or_nested(__isl_keep isl_space *space,
2110 enum isl_dim_type type)
2112 if (type != isl_dim_in && type != isl_dim_out)
2113 return isl_bool_false;
2114 if (!space)
2115 return isl_bool_error;
2116 if (space->tuple_id[type - isl_dim_in])
2117 return isl_bool_true;
2118 if (space->nested[type - isl_dim_in])
2119 return isl_bool_true;
2120 return isl_bool_false;
2123 isl_bool isl_space_may_be_set(__isl_keep isl_space *space)
2125 isl_bool nested;
2127 if (!space)
2128 return isl_bool_error;
2129 if (isl_space_is_set(space))
2130 return isl_bool_true;
2131 if (isl_space_dim(space, isl_dim_in) != 0)
2132 return isl_bool_false;
2133 nested = isl_space_is_named_or_nested(space, isl_dim_in);
2134 if (nested < 0 || nested)
2135 return isl_bool_not(nested);
2136 return isl_bool_true;
2139 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
2140 enum isl_dim_type type)
2142 if (!isl_space_is_named_or_nested(dim, type))
2143 return dim;
2145 dim = isl_space_cow(dim);
2146 if (!dim)
2147 return NULL;
2149 isl_id_free(dim->tuple_id[type - isl_dim_in]);
2150 dim->tuple_id[type - isl_dim_in] = NULL;
2151 isl_space_free(dim->nested[type - isl_dim_in]);
2152 dim->nested[type - isl_dim_in] = NULL;
2154 return dim;
2157 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
2159 if (!dim)
2160 return NULL;
2161 if (!dim->nested[0] && !dim->nested[1])
2162 return dim;
2164 if (dim->nested[0])
2165 dim = isl_space_reset(dim, isl_dim_in);
2166 if (dim && dim->nested[1])
2167 dim = isl_space_reset(dim, isl_dim_out);
2169 return dim;
2172 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
2174 if (!dim)
2175 return NULL;
2176 if (!dim->nested[0])
2177 return dim;
2179 return isl_space_reset(dim, isl_dim_in);
2182 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
2184 if (!dim)
2185 return NULL;
2186 if (!dim->nested[1])
2187 return dim;
2189 return isl_space_reset(dim, isl_dim_out);
2192 /* Replace the dimensions of the given type of dst by those of src.
2194 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
2195 enum isl_dim_type type, __isl_keep isl_space *src)
2197 dst = isl_space_cow(dst);
2199 if (!dst || !src)
2200 goto error;
2202 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
2203 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
2204 dst = copy_ids(dst, type, 0, src, type);
2206 if (dst && type == isl_dim_param) {
2207 int i;
2208 for (i = 0; i <= 1; ++i) {
2209 if (!dst->nested[i])
2210 continue;
2211 dst->nested[i] = isl_space_replace(dst->nested[i],
2212 type, src);
2213 if (!dst->nested[i])
2214 goto error;
2218 return dst;
2219 error:
2220 isl_space_free(dst);
2221 return NULL;
2224 /* Given a dimension specification "dim" of a set, create a dimension
2225 * specification for the lift of the set. In particular, the result
2226 * is of the form [dim -> local[..]], with n_local variables in the
2227 * range of the wrapped map.
2229 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
2231 isl_space *local_dim;
2233 if (!dim)
2234 return NULL;
2236 local_dim = isl_space_dup(dim);
2237 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
2238 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
2239 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
2240 dim = isl_space_join(isl_space_from_domain(dim),
2241 isl_space_from_range(local_dim));
2242 dim = isl_space_wrap(dim);
2243 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
2245 return dim;
2248 isl_bool isl_space_can_zip(__isl_keep isl_space *dim)
2250 if (!dim)
2251 return isl_bool_error;
2253 return dim->nested[0] && dim->nested[1];
2256 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
2258 isl_space *dom, *ran;
2259 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2261 if (!isl_space_can_zip(dim))
2262 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
2263 goto error);
2265 if (!dim)
2266 return NULL;
2267 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
2268 ran = isl_space_unwrap(isl_space_range(dim));
2269 dom_dom = isl_space_domain(isl_space_copy(dom));
2270 dom_ran = isl_space_range(dom);
2271 ran_dom = isl_space_domain(isl_space_copy(ran));
2272 ran_ran = isl_space_range(ran);
2273 dom = isl_space_join(isl_space_from_domain(dom_dom),
2274 isl_space_from_range(ran_dom));
2275 ran = isl_space_join(isl_space_from_domain(dom_ran),
2276 isl_space_from_range(ran_ran));
2277 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2278 isl_space_from_range(isl_space_wrap(ran)));
2279 error:
2280 isl_space_free(dim);
2281 return NULL;
2284 /* Can we apply isl_space_curry to "space"?
2285 * That is, does it have a nested relation in its domain?
2287 isl_bool isl_space_can_curry(__isl_keep isl_space *space)
2289 if (!space)
2290 return isl_bool_error;
2292 return !!space->nested[0];
2295 /* Given a space (A -> B) -> C, return the corresponding space
2296 * A -> (B -> C).
2298 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
2300 isl_space *dom, *ran;
2301 isl_space *dom_dom, *dom_ran;
2303 if (!space)
2304 return NULL;
2306 if (!isl_space_can_curry(space))
2307 isl_die(space->ctx, isl_error_invalid,
2308 "space cannot be curried", goto error);
2310 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2311 ran = isl_space_range(space);
2312 dom_dom = isl_space_domain(isl_space_copy(dom));
2313 dom_ran = isl_space_range(dom);
2314 ran = isl_space_join(isl_space_from_domain(dom_ran),
2315 isl_space_from_range(ran));
2316 return isl_space_join(isl_space_from_domain(dom_dom),
2317 isl_space_from_range(isl_space_wrap(ran)));
2318 error:
2319 isl_space_free(space);
2320 return NULL;
2323 /* Can isl_space_range_curry be applied to "space"?
2324 * That is, does it have a nested relation in its range,
2325 * the domain of which is itself a nested relation?
2327 isl_bool isl_space_can_range_curry(__isl_keep isl_space *space)
2329 isl_bool can;
2331 if (!space)
2332 return isl_bool_error;
2333 can = isl_space_range_is_wrapping(space);
2334 if (can < 0 || !can)
2335 return can;
2336 return isl_space_can_curry(space->nested[1]);
2339 /* Given a space A -> ((B -> C) -> D), return the corresponding space
2340 * A -> (B -> (C -> D)).
2342 __isl_give isl_space *isl_space_range_curry(__isl_take isl_space *space)
2344 if (!space)
2345 return NULL;
2347 if (!isl_space_can_range_curry(space))
2348 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2349 "space range cannot be curried",
2350 return isl_space_free(space));
2352 space = isl_space_cow(space);
2353 if (!space)
2354 return NULL;
2355 space->nested[1] = isl_space_curry(space->nested[1]);
2356 if (!space->nested[1])
2357 return isl_space_free(space);
2359 return space;
2362 /* Can we apply isl_space_uncurry to "space"?
2363 * That is, does it have a nested relation in its range?
2365 isl_bool isl_space_can_uncurry(__isl_keep isl_space *space)
2367 if (!space)
2368 return isl_bool_error;
2370 return !!space->nested[1];
2373 /* Given a space A -> (B -> C), return the corresponding space
2374 * (A -> B) -> C.
2376 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
2378 isl_space *dom, *ran;
2379 isl_space *ran_dom, *ran_ran;
2381 if (!space)
2382 return NULL;
2384 if (!isl_space_can_uncurry(space))
2385 isl_die(space->ctx, isl_error_invalid,
2386 "space cannot be uncurried",
2387 return isl_space_free(space));
2389 dom = isl_space_domain(isl_space_copy(space));
2390 ran = isl_space_unwrap(isl_space_range(space));
2391 ran_dom = isl_space_domain(isl_space_copy(ran));
2392 ran_ran = isl_space_range(ran);
2393 dom = isl_space_join(isl_space_from_domain(dom),
2394 isl_space_from_range(ran_dom));
2395 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2396 isl_space_from_range(ran_ran));
2399 isl_bool isl_space_has_named_params(__isl_keep isl_space *space)
2401 int i;
2402 unsigned off;
2404 if (!space)
2405 return isl_bool_error;
2406 if (space->nparam == 0)
2407 return isl_bool_true;
2408 off = isl_space_offset(space, isl_dim_param);
2409 if (off + space->nparam > space->n_id)
2410 return isl_bool_false;
2411 for (i = 0; i < space->nparam; ++i)
2412 if (!space->ids[off + i])
2413 return isl_bool_false;
2414 return isl_bool_true;
2417 /* Check that "space" has only named parameters, reporting an error
2418 * if it does not.
2420 isl_stat isl_space_check_named_params(__isl_keep isl_space *space)
2422 isl_bool named;
2424 named = isl_space_has_named_params(space);
2425 if (named < 0)
2426 return isl_stat_error;
2427 if (!named)
2428 isl_die(isl_space_get_ctx(space), isl_error_invalid,
2429 "unaligned unnamed parameters", return isl_stat_error);
2431 return isl_stat_ok;
2434 /* Align the initial parameters of dim1 to match the order in dim2.
2436 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
2437 __isl_take isl_space *dim2)
2439 isl_reordering *exp;
2441 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
2442 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
2443 "parameter alignment requires named parameters",
2444 goto error);
2446 dim2 = isl_space_params(dim2);
2447 exp = isl_parameter_alignment_reordering(dim1, dim2);
2448 exp = isl_reordering_extend_space(exp, dim1);
2449 isl_space_free(dim2);
2450 if (!exp)
2451 return NULL;
2452 dim1 = isl_space_copy(exp->dim);
2453 isl_reordering_free(exp);
2454 return dim1;
2455 error:
2456 isl_space_free(dim1);
2457 isl_space_free(dim2);
2458 return NULL;
2461 /* Given the space of set (domain), construct a space for a map
2462 * with as domain the given space and as range the range of "model".
2464 __isl_give isl_space *isl_space_extend_domain_with_range(
2465 __isl_take isl_space *space, __isl_take isl_space *model)
2467 if (!model)
2468 goto error;
2470 space = isl_space_from_domain(space);
2471 space = isl_space_add_dims(space, isl_dim_out,
2472 isl_space_dim(model, isl_dim_out));
2473 if (isl_space_has_tuple_id(model, isl_dim_out))
2474 space = isl_space_set_tuple_id(space, isl_dim_out,
2475 isl_space_get_tuple_id(model, isl_dim_out));
2476 if (!space)
2477 goto error;
2478 if (model->nested[1]) {
2479 isl_space *nested = isl_space_copy(model->nested[1]);
2480 int n_nested, n_space;
2481 nested = isl_space_align_params(nested, isl_space_copy(space));
2482 n_nested = isl_space_dim(nested, isl_dim_param);
2483 n_space = isl_space_dim(space, isl_dim_param);
2484 if (n_nested > n_space)
2485 nested = isl_space_drop_dims(nested, isl_dim_param,
2486 n_space, n_nested - n_space);
2487 if (!nested)
2488 goto error;
2489 space->nested[1] = nested;
2491 isl_space_free(model);
2492 return space;
2493 error:
2494 isl_space_free(model);
2495 isl_space_free(space);
2496 return NULL;
2499 /* Compare the "type" dimensions of two isl_spaces.
2501 * The order is fairly arbitrary.
2503 static int isl_space_cmp_type(__isl_keep isl_space *space1,
2504 __isl_keep isl_space *space2, enum isl_dim_type type)
2506 int cmp;
2507 isl_space *nested1, *nested2;
2509 if (isl_space_dim(space1, type) != isl_space_dim(space2, type))
2510 return isl_space_dim(space1, type) -
2511 isl_space_dim(space2, type);
2513 cmp = isl_id_cmp(tuple_id(space1, type), tuple_id(space2, type));
2514 if (cmp != 0)
2515 return cmp;
2517 nested1 = nested(space1, type);
2518 nested2 = nested(space2, type);
2519 if (!nested1 != !nested2)
2520 return !nested1 - !nested2;
2522 if (nested1)
2523 return isl_space_cmp(nested1, nested2);
2525 return 0;
2528 /* Compare two isl_spaces.
2530 * The order is fairly arbitrary.
2532 int isl_space_cmp(__isl_keep isl_space *space1, __isl_keep isl_space *space2)
2534 int i;
2535 int cmp;
2537 if (space1 == space2)
2538 return 0;
2539 if (!space1)
2540 return -1;
2541 if (!space2)
2542 return 1;
2544 cmp = isl_space_cmp_type(space1, space2, isl_dim_param);
2545 if (cmp != 0)
2546 return cmp;
2547 cmp = isl_space_cmp_type(space1, space2, isl_dim_in);
2548 if (cmp != 0)
2549 return cmp;
2550 cmp = isl_space_cmp_type(space1, space2, isl_dim_out);
2551 if (cmp != 0)
2552 return cmp;
2554 if (!space1->ids && !space2->ids)
2555 return 0;
2557 for (i = 0; i < n(space1, isl_dim_param); ++i) {
2558 cmp = isl_id_cmp(get_id(space1, isl_dim_param, i),
2559 get_id(space2, isl_dim_param, i));
2560 if (cmp != 0)
2561 return cmp;
2564 return 0;