add isl_space_domain_wrapped_domain
[isl.git] / isl_multi_templ.c
blobac4d480d3fe722bd30cb5a263d5fac4b2a910ec7
1 /*
2 * Copyright 2011 Sven Verdoolaege
3 * Copyright 2012-2014 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
9 */
11 #include <isl/id.h>
12 #include <isl_space_private.h>
13 #include <isl/set.h>
14 #include <isl_reordering.h>
16 #include <isl_multi_macro.h>
18 #define MULTI_NAME(BASE) "isl_multi_" #BASE
20 isl_ctx *FN(MULTI(BASE),get_ctx)(__isl_keep MULTI(BASE) *multi)
22 return multi ? isl_space_get_ctx(multi->space) : NULL;
25 /* Return the space of "multi".
27 __isl_keep isl_space *FN(MULTI(BASE),peek_space)(__isl_keep MULTI(BASE) *multi)
29 return multi ? multi->space : NULL;
32 __isl_give isl_space *FN(MULTI(BASE),get_space)(__isl_keep MULTI(BASE) *multi)
34 return isl_space_copy(FN(MULTI(BASE),peek_space)(multi));
37 __isl_give isl_space *FN(MULTI(BASE),get_domain_space)(
38 __isl_keep MULTI(BASE) *multi)
40 return multi ? isl_space_domain(isl_space_copy(multi->space)) : NULL;
43 /* Allocate a multi expression living in "space".
45 * If the number of base expressions is zero, then make sure
46 * there is enough room in the structure for the explicit domain,
47 * in case the type supports such an explicit domain.
49 __isl_give MULTI(BASE) *FN(MULTI(BASE),alloc)(__isl_take isl_space *space)
51 isl_ctx *ctx;
52 isl_size n;
53 MULTI(BASE) *multi;
55 n = isl_space_dim(space, isl_dim_out);
56 if (n < 0)
57 goto error;
59 ctx = isl_space_get_ctx(space);
60 if (n > 0)
61 multi = isl_calloc(ctx, MULTI(BASE),
62 sizeof(MULTI(BASE)) + (n - 1) * sizeof(struct EL *));
63 else
64 multi = isl_calloc(ctx, MULTI(BASE), sizeof(MULTI(BASE)));
65 if (!multi)
66 goto error;
68 multi->space = space;
69 multi->n = n;
70 multi->ref = 1;
71 if (FN(MULTI(BASE),has_explicit_domain)(multi))
72 multi = FN(MULTI(BASE),init_explicit_domain)(multi);
73 return multi;
74 error:
75 isl_space_free(space);
76 return NULL;
79 __isl_give MULTI(BASE) *FN(MULTI(BASE),dup)(__isl_keep MULTI(BASE) *multi)
81 int i;
82 MULTI(BASE) *dup;
84 if (!multi)
85 return NULL;
87 dup = FN(MULTI(BASE),alloc)(isl_space_copy(multi->space));
88 if (!dup)
89 return NULL;
91 for (i = 0; i < multi->n; ++i)
92 dup = FN(FN(MULTI(BASE),set),BASE)(dup, i,
93 FN(EL,copy)(multi->u.p[i]));
94 if (FN(MULTI(BASE),has_explicit_domain)(multi))
95 dup = FN(MULTI(BASE),copy_explicit_domain)(dup, multi);
97 return dup;
100 __isl_give MULTI(BASE) *FN(MULTI(BASE),cow)(__isl_take MULTI(BASE) *multi)
102 if (!multi)
103 return NULL;
105 if (multi->ref == 1)
106 return multi;
108 multi->ref--;
109 return FN(MULTI(BASE),dup)(multi);
112 __isl_give MULTI(BASE) *FN(MULTI(BASE),copy)(__isl_keep MULTI(BASE) *multi)
114 if (!multi)
115 return NULL;
117 multi->ref++;
118 return multi;
121 __isl_null MULTI(BASE) *FN(MULTI(BASE),free)(__isl_take MULTI(BASE) *multi)
123 int i;
125 if (!multi)
126 return NULL;
128 if (--multi->ref > 0)
129 return NULL;
131 isl_space_free(multi->space);
132 for (i = 0; i < multi->n; ++i)
133 FN(EL,free)(multi->u.p[i]);
134 if (FN(MULTI(BASE),has_explicit_domain)(multi))
135 FN(MULTI(BASE),free_explicit_domain)(multi);
136 free(multi);
138 return NULL;
141 /* Return the space of "multi".
142 * The caller is not allowed to modify "multi" between this call
143 * and the call to *_restore_space because the number
144 * of references needs to stay the same.
145 * The only exception is that isl_multi_*_free can be called instead.
146 * No copy is taken of multi->space if "multi" has only one reference
147 * such that it can be modified inplace if both have only a single reference.
149 __isl_give isl_space *FN(MULTI(BASE),take_space)(__isl_keep MULTI(BASE) *multi)
151 isl_space *space;
153 if (!multi)
154 return NULL;
155 if (multi->ref != 1)
156 return FN(MULTI(BASE),get_space)(multi);
157 space = multi->space;
158 multi->space = NULL;
159 return space;
162 /* Set the space of "multi" to "space", where the space of "multi"
163 * may be missing due to a preceding call to isl_multi_*_take_space.
164 * However, in this case, "multi" only has a single reference and
165 * then the call to isl_multi_*_cow has no effect.
167 __isl_give MULTI(BASE) *FN(MULTI(BASE),restore_space)(
168 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
170 if (!multi || !space)
171 goto error;
173 if (multi->space == space) {
174 isl_space_free(space);
175 return multi;
178 multi = FN(MULTI(BASE),cow)(multi);
179 if (!multi)
180 goto error;
181 isl_space_free(multi->space);
182 multi->space = space;
184 return multi;
185 error:
186 FN(MULTI(BASE),free)(multi);
187 isl_space_free(space);
188 return NULL;
191 isl_size FN(MULTI(BASE),dim)(__isl_keep MULTI(BASE) *multi,
192 enum isl_dim_type type)
194 return isl_space_dim(FN(MULTI(BASE),peek_space)(multi), type);
197 /* Return the number of base expressions in "multi".
199 isl_size FN(MULTI(BASE),size)(__isl_keep MULTI(BASE) *multi)
201 return multi ? multi->n : isl_size_error;
204 #undef TYPE
205 #define TYPE MULTI(BASE)
206 static
207 #include "check_type_range_templ.c"
209 /* Return the base expression at position "pos" in "multi".
211 static __isl_give EL *FN(MULTI(BASE),peek_at)(__isl_keep MULTI(BASE) *multi,
212 int pos)
214 if (FN(MULTI(BASE),check_range)(multi, isl_dim_out, pos, 1) < 0)
215 return NULL;
216 return multi->u.p[pos];
219 /* Return a copy of the base expression at position "pos" in "multi".
221 __isl_give EL *FN(MULTI(BASE),get_at)(__isl_keep MULTI(BASE) *multi, int pos)
223 return FN(EL,copy)(FN(MULTI(BASE),peek_at)(multi, pos));
226 /* This is an alternative name for the function above.
228 __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
229 int pos)
231 return FN(MULTI(BASE),get_at)(multi, pos);
234 /* Return the base expression at position "pos" in "multi".
235 * This may be either a copy or the base expression itself
236 * if there is only one reference to "multi".
237 * This allows the base expression to be modified inplace
238 * if both the multi expression and this base expression
239 * have only a single reference.
240 * The caller is not allowed to modify "multi" between this call and
241 * the subsequent call to isl_multi_*_restore_at_*.
242 * The only exception is that isl_multi_*_free can be called instead.
244 static __isl_give EL *FN(MULTI(BASE),take_at)(__isl_keep MULTI(BASE) *multi,
245 int pos)
247 EL *el;
249 if (!multi)
250 return NULL;
251 if (multi->ref != 1)
252 return FN(MULTI(BASE),get_at)(multi, pos);
253 if (FN(MULTI(BASE),check_range)(multi, isl_dim_out, pos, 1) < 0)
254 return NULL;
255 el = multi->u.p[pos];
256 multi->u.p[pos] = NULL;
257 return el;
260 /* Set the element at position "pos" of "multi" to "el",
261 * where the position may be empty if "multi" has only a single reference.
263 static __isl_give MULTI(BASE) *FN(MULTI(BASE),restore_at)(
264 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
266 if (FN(MULTI(BASE),check_range)(multi, isl_dim_out, pos, 1) < 0 || !el)
267 goto error;
269 if (multi->u.p[pos] == el) {
270 FN(EL,free)(el);
271 return multi;
274 multi = FN(MULTI(BASE),cow)(multi);
275 if (!multi)
276 goto error;
278 FN(EL,free)(multi->u.p[pos]);
279 multi->u.p[pos] = el;
281 return multi;
282 error:
283 FN(MULTI(BASE),free)(multi);
284 FN(EL,free)(el);
285 return NULL;
288 /* Set the element at position "pos" of "multi" to "el",
289 * where the position may be empty if "multi" has only a single reference.
290 * However, the space of "multi" is available and is checked
291 * for compatibility with "el".
293 static __isl_give MULTI(BASE) *FN(MULTI(BASE),restore_check_space)(
294 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
296 isl_space *space;
298 space = FN(MULTI(BASE),peek_space)(multi);
299 if (FN(EL,check_match_domain_space)(el, space) < 0)
300 multi = FN(MULTI(BASE),free)(multi);
301 return FN(MULTI(BASE),restore_at)(multi, pos, el);
304 /* Replace the base expression at position "pos" in "multi" with "el".
306 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_at)(
307 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
309 isl_space *multi_space = NULL;
310 isl_space *el_space = NULL;
311 isl_bool match;
313 multi_space = FN(MULTI(BASE),get_space)(multi);
314 match = FN(EL,matching_params)(el, multi_space);
315 if (match < 0)
316 goto error;
317 if (!match) {
318 multi = FN(MULTI(BASE),align_params)(multi,
319 FN(EL,get_space)(el));
320 isl_space_free(multi_space);
321 multi_space = FN(MULTI(BASE),get_space)(multi);
322 el = FN(EL,align_params)(el, isl_space_copy(multi_space));
325 multi = FN(MULTI(BASE),restore_check_space)(multi, pos, el);
327 isl_space_free(multi_space);
328 isl_space_free(el_space);
330 return multi;
331 error:
332 FN(MULTI(BASE),free)(multi);
333 FN(EL,free)(el);
334 isl_space_free(multi_space);
335 isl_space_free(el_space);
336 return NULL;
339 /* This is an alternative name for the function above.
341 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
342 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
344 return FN(MULTI(BASE),set_at)(multi, pos, el);
347 /* Return the base expressions of "multi" as a list.
349 __isl_give LIST(EL) *FN(MULTI(BASE),get_list)(
350 __isl_keep MULTI(BASE) *multi)
352 isl_size n;
353 int i;
354 LIST(EL) *list;
356 n = FN(MULTI(BASE),size)(multi);
357 if (n < 0)
358 return NULL;
359 list = FN(LIST(EL),alloc)(FN(MULTI(BASE),get_ctx(multi)), n);
360 for (i = 0; i < n; ++i) {
361 EL *el = FN(MULTI(BASE),get_at)(multi, i);
362 list = FN(LIST(EL),add)(list, el);
365 return list;
368 /* Reset the space of "multi". This function is called from isl_pw_templ.c
369 * and doesn't know if the space of an element object is represented
370 * directly or through its domain. It therefore passes along both,
371 * which we pass along to the element function since we don't know how
372 * that is represented either.
374 * If "multi" has an explicit domain, then the caller is expected
375 * to make sure that any modification that would change the dimensions
376 * of the explicit domain has bee applied before this function is called.
378 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
379 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
380 __isl_take isl_space *domain)
382 isl_size n;
383 int i;
385 n = FN(MULTI(BASE),size)(multi);
386 if (n < 0 || !space || !domain)
387 goto error;
389 for (i = 0; i < n; ++i) {
390 EL *el;
392 el = FN(MULTI(BASE),take_at)(multi, i);
393 el = FN(EL,reset_domain_space)(el, isl_space_copy(domain));
394 multi = FN(MULTI(BASE),restore_at)(multi, i, el);
396 if (FN(MULTI(BASE),has_explicit_domain)(multi))
397 multi = FN(MULTI(BASE),reset_explicit_domain_space)(multi,
398 isl_space_copy(domain));
399 isl_space_free(domain);
401 multi = FN(MULTI(BASE),restore_space)(multi, space);
403 return multi;
404 error:
405 isl_space_free(domain);
406 isl_space_free(space);
407 FN(MULTI(BASE),free)(multi);
408 return NULL;
411 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
412 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
414 isl_space *space;
416 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
417 isl_space_copy(multi->space));
418 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
421 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
422 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
424 isl_space *domain;
426 domain = isl_space_domain(isl_space_copy(space));
427 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
430 /* Reset the user pointer on all identifiers of parameters and tuples
431 * of the space of "multi".
433 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_user)(
434 __isl_take MULTI(BASE) *multi)
436 isl_space *space;
438 space = FN(MULTI(BASE),get_space)(multi);
439 space = isl_space_reset_user(space);
441 return FN(MULTI(BASE),reset_space)(multi, space);
444 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
445 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
447 int i;
448 isl_size n;
449 isl_space *space;
451 n = FN(MULTI(BASE),size)(multi);
452 if (n < 0 || !exp)
453 goto error;
455 for (i = 0; i < n; ++i) {
456 EL *el;
458 el = FN(MULTI(BASE),take_at)(multi, i);
459 el = FN(EL,realign_domain)(el, isl_reordering_copy(exp));
460 multi = FN(MULTI(BASE),restore_at)(multi, i, el);
463 space = isl_reordering_get_space(exp);
464 multi = FN(MULTI(BASE),reset_domain_space)(multi, space);
466 isl_reordering_free(exp);
467 return multi;
468 error:
469 isl_reordering_free(exp);
470 FN(MULTI(BASE),free)(multi);
471 return NULL;
474 /* Align the parameters of "multi" to those of "model".
476 * If "multi" has an explicit domain, then align the parameters
477 * of the domain first.
479 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
480 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
482 isl_ctx *ctx;
483 isl_bool equal_params;
484 isl_reordering *exp;
486 if (!multi || !model)
487 goto error;
489 equal_params = isl_space_has_equal_params(multi->space, model);
490 if (equal_params < 0)
491 goto error;
492 if (equal_params) {
493 isl_space_free(model);
494 return multi;
497 ctx = isl_space_get_ctx(model);
498 if (!isl_space_has_named_params(model))
499 isl_die(ctx, isl_error_invalid,
500 "model has unnamed parameters", goto error);
501 if (!isl_space_has_named_params(multi->space))
502 isl_die(ctx, isl_error_invalid,
503 "input has unnamed parameters", goto error);
505 if (FN(MULTI(BASE),has_explicit_domain)(multi)) {
506 multi = FN(MULTI(BASE),align_explicit_domain_params)(multi,
507 isl_space_copy(model));
508 if (!multi)
509 goto error;
511 exp = isl_parameter_alignment_reordering(multi->space, model);
512 exp = isl_reordering_extend_space(exp,
513 FN(MULTI(BASE),get_domain_space)(multi));
514 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
516 isl_space_free(model);
517 return multi;
518 error:
519 isl_space_free(model);
520 FN(MULTI(BASE),free)(multi);
521 return NULL;
524 /* Create a multi expression in the given space with the elements of "list"
525 * as base expressions.
527 * Since isl_multi_*_restore_* assumes that the element and
528 * the multi expression have matching spaces, the alignment
529 * (if any) needs to be performed beforehand.
531 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
532 __isl_take isl_space *space, __isl_take LIST(EL) *list)
534 int i;
535 isl_size n, dim;
536 isl_ctx *ctx;
537 MULTI(BASE) *multi;
539 dim = isl_space_dim(space, isl_dim_out);
540 n = FN(FN(LIST(EL),n),BASE)(list);
541 if (dim < 0 || n < 0)
542 goto error;
544 ctx = isl_space_get_ctx(space);
545 if (n != dim)
546 isl_die(ctx, isl_error_invalid,
547 "invalid number of elements in list", goto error);
549 for (i = 0; i < n; ++i) {
550 EL *el = FN(LIST(EL),peek)(list, i);
551 space = isl_space_align_params(space, FN(EL,get_space)(el));
553 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
554 for (i = 0; i < n; ++i) {
555 EL *el = FN(FN(LIST(EL),get),BASE)(list, i);
556 el = FN(EL,align_params)(el, isl_space_copy(space));
557 multi = FN(MULTI(BASE),restore_check_space)(multi, i, el);
560 isl_space_free(space);
561 FN(LIST(EL),free)(list);
562 return multi;
563 error:
564 isl_space_free(space);
565 FN(LIST(EL),free)(list);
566 return NULL;
569 /* This function performs the same operation as isl_multi_*_from_*_list,
570 * but is considered as a function on an isl_space when exported.
572 __isl_give MULTI(BASE) *FN(isl_space_multi,BASE)(__isl_take isl_space *space,
573 __isl_take LIST(EL) *list)
575 return FN(FN(MULTI(BASE),from),LIST(BASE))(space, list);
578 /* Drop the "n" output dimensions of "multi" starting at "first",
579 * where the space is assumed to have been adjusted already.
581 static __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_output_dims)(
582 __isl_take MULTI(BASE) *multi, unsigned first, unsigned n)
584 int i;
586 multi = FN(MULTI(BASE),cow)(multi);
587 if (!multi)
588 return NULL;
590 for (i = 0; i < n; ++i)
591 FN(EL,free)(multi->u.p[first + i]);
592 for (i = first; i + n < multi->n; ++i)
593 multi->u.p[i] = multi->u.p[i + n];
594 multi->n -= n;
595 if (n > 0 && FN(MULTI(BASE),has_explicit_domain)(multi))
596 multi = FN(MULTI(BASE),init_explicit_domain)(multi);
598 return multi;
601 __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
602 __isl_take MULTI(BASE) *multi,
603 enum isl_dim_type type, unsigned first, unsigned n)
605 isl_space *space;
606 isl_size size;
607 int i;
609 if (FN(MULTI(BASE),check_range)(multi, type, first, n) < 0)
610 return FN(MULTI(BASE),free)(multi);
612 space = FN(MULTI(BASE),take_space)(multi);
613 space = isl_space_drop_dims(space, type, first, n);
614 multi = FN(MULTI(BASE),restore_space)(multi, space);
616 if (type == isl_dim_out)
617 return FN(MULTI(BASE),drop_output_dims)(multi, first, n);
619 if (FN(MULTI(BASE),has_explicit_domain)(multi))
620 multi = FN(MULTI(BASE),drop_explicit_domain_dims)(multi,
621 type, first, n);
623 size = FN(MULTI(BASE),size)(multi);
624 if (size < 0)
625 return FN(MULTI(BASE),free)(multi);
626 for (i = 0; i < size; ++i) {
627 EL *el;
629 el = FN(MULTI(BASE),take_at)(multi, i);
630 el = FN(EL,drop_dims)(el, type, first, n);
631 multi = FN(MULTI(BASE),restore_at)(multi, i, el);
634 return multi;
637 #undef TYPE
638 #define TYPE MULTI(BASE)
640 #include "isl_check_named_params_templ.c"
641 static
642 #include "isl_align_params_bin_templ.c"
644 /* Given two MULTI(BASE)s A -> B and C -> D,
645 * construct a MULTI(BASE) (A * C) -> [B -> D].
647 * If "multi1" and/or "multi2" has an explicit domain, then
648 * intersect the domain of the result with these explicit domains.
650 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product)(
651 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
653 int i;
654 isl_size n1, n2;
655 EL *el;
656 isl_space *space;
657 MULTI(BASE) *res;
659 FN(MULTI(BASE),align_params_bin)(&multi1, &multi2);
660 n1 = FN(MULTI(BASE),size)(multi1);
661 n2 = FN(MULTI(BASE),size)(multi2);
662 if (n1 < 0 || n2 < 0)
663 goto error;
665 space = isl_space_range_product(FN(MULTI(BASE),get_space)(multi1),
666 FN(MULTI(BASE),get_space)(multi2));
667 res = FN(MULTI(BASE),alloc)(space);
669 for (i = 0; i < n1; ++i) {
670 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
671 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
674 for (i = 0; i < n2; ++i) {
675 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
676 res = FN(FN(MULTI(BASE),set),BASE)(res, n1 + i, el);
679 if (FN(MULTI(BASE),has_explicit_domain)(multi1))
680 res = FN(MULTI(BASE),intersect_explicit_domain)(res, multi1);
681 if (FN(MULTI(BASE),has_explicit_domain)(multi2))
682 res = FN(MULTI(BASE),intersect_explicit_domain)(res, multi2);
684 FN(MULTI(BASE),free)(multi1);
685 FN(MULTI(BASE),free)(multi2);
686 return res;
687 error:
688 FN(MULTI(BASE),free)(multi1);
689 FN(MULTI(BASE),free)(multi2);
690 return NULL;
693 /* Is the range of "multi" a wrapped relation?
695 isl_bool FN(MULTI(BASE),range_is_wrapping)(__isl_keep MULTI(BASE) *multi)
697 if (!multi)
698 return isl_bool_error;
699 return isl_space_range_is_wrapping(multi->space);
702 /* Given a function A -> [B -> C], extract the function A -> B.
704 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_domain)(
705 __isl_take MULTI(BASE) *multi)
707 isl_space *space;
708 isl_size total, keep;
710 total = FN(MULTI(BASE),dim)(multi, isl_dim_out);
711 if (total < 0)
712 return FN(MULTI(BASE),free)(multi);
713 if (!isl_space_range_is_wrapping(multi->space))
714 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
715 "range is not a product",
716 return FN(MULTI(BASE),free)(multi));
718 space = FN(MULTI(BASE),get_space)(multi);
719 space = isl_space_range_factor_domain(space);
720 keep = isl_space_dim(space, isl_dim_out);
721 if (keep < 0)
722 multi = FN(MULTI(BASE),free)(multi);
723 multi = FN(MULTI(BASE),drop_dims)(multi,
724 isl_dim_out, keep, total - keep);
725 multi = FN(MULTI(BASE),reset_space)(multi, space);
727 return multi;
730 /* Given a function A -> [B -> C], extract the function A -> C.
732 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_range)(
733 __isl_take MULTI(BASE) *multi)
735 isl_space *space;
736 isl_size total, keep;
738 total = FN(MULTI(BASE),dim)(multi, isl_dim_out);
739 if (total < 0)
740 return FN(MULTI(BASE),free)(multi);
741 if (!isl_space_range_is_wrapping(multi->space))
742 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
743 "range is not a product",
744 return FN(MULTI(BASE),free)(multi));
746 space = FN(MULTI(BASE),get_space)(multi);
747 space = isl_space_range_factor_range(space);
748 keep = isl_space_dim(space, isl_dim_out);
749 if (keep < 0)
750 multi = FN(MULTI(BASE),free)(multi);
751 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_out, 0, total - keep);
752 multi = FN(MULTI(BASE),reset_space)(multi, space);
754 return multi;
757 /* Given a function [B -> C], extract the function C.
759 __isl_give MULTI(BASE) *FN(MULTI(BASE),factor_range)(
760 __isl_take MULTI(BASE) *multi)
762 isl_space *space;
763 isl_size total, keep;
765 total = FN(MULTI(BASE),dim)(multi, isl_dim_set);
766 if (total < 0)
767 return FN(MULTI(BASE),free)(multi);
768 if (!isl_space_is_wrapping(multi->space))
769 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
770 "not a product", return FN(MULTI(BASE),free)(multi));
772 space = FN(MULTI(BASE),get_space)(multi);
773 space = isl_space_factor_range(space);
774 keep = isl_space_dim(space, isl_dim_set);
775 if (keep < 0)
776 multi = FN(MULTI(BASE),free)(multi);
777 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_set, 0, total - keep);
778 multi = FN(MULTI(BASE),reset_space)(multi, space);
780 return multi;
783 __isl_give MULTI(BASE) *FN(MULTI(BASE),flatten_range)(
784 __isl_take MULTI(BASE) *multi)
786 isl_space *space;
788 space = FN(MULTI(BASE),take_space)(multi);
789 space = isl_space_flatten_range(space);
790 multi = FN(MULTI(BASE),restore_space)(multi, space);
792 return multi;
795 /* Given two MULTI(BASE)s A -> B and C -> D,
796 * construct a MULTI(BASE) (A * C) -> (B, D).
798 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
799 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
801 MULTI(BASE) *multi;
803 multi = FN(MULTI(BASE),range_product)(multi1, multi2);
804 multi = FN(MULTI(BASE),flatten_range)(multi);
805 return multi;
808 /* Given two multi expressions, "multi1"
810 * [A] -> [B1 B2]
812 * where B2 starts at position "pos", and "multi2"
814 * [A] -> [D]
816 * return the multi expression
818 * [A] -> [B1 D B2]
820 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_splice)(
821 __isl_take MULTI(BASE) *multi1, unsigned pos,
822 __isl_take MULTI(BASE) *multi2)
824 MULTI(BASE) *res;
825 isl_size dim;
827 dim = FN(MULTI(BASE),size)(multi1);
828 if (dim < 0 || !multi2)
829 goto error;
831 if (FN(MULTI(BASE),check_range)(multi1, isl_dim_out, pos, 0) < 0)
832 goto error;
834 res = FN(MULTI(BASE),copy)(multi1);
835 res = FN(MULTI(BASE),drop_dims)(res, isl_dim_out, pos, dim - pos);
836 multi1 = FN(MULTI(BASE),drop_dims)(multi1, isl_dim_out, 0, pos);
838 res = FN(MULTI(BASE),flat_range_product)(res, multi2);
839 res = FN(MULTI(BASE),flat_range_product)(res, multi1);
841 return res;
842 error:
843 FN(MULTI(BASE),free)(multi1);
844 FN(MULTI(BASE),free)(multi2);
845 return NULL;
848 #undef TYPE
849 #define TYPE MULTI(BASE)
851 static
852 #include "isl_type_has_equal_space_bin_templ.c"
853 static
854 #include "isl_type_check_equal_space_templ.c"
856 /* This function is currently only used from isl_aff.c
858 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
859 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
860 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
861 __attribute__ ((unused));
863 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
864 * return the result.
866 * If "multi2" has an explicit domain, then
867 * intersect the domain of the result with this explicit domain.
869 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
870 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
871 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
873 isl_size n;
874 int i;
876 FN(MULTI(BASE),align_params_bin)(&multi1, &multi2);
877 n = FN(MULTI(BASE),size)(multi1);
878 if (n < 0 || FN(MULTI(BASE),check_equal_space)(multi1, multi2) < 0)
879 goto error;
881 for (i = 0; i < n; ++i) {
882 EL *el1, *el2;
884 el2 = FN(MULTI(BASE),get_at)(multi2, i);
885 el1 = FN(MULTI(BASE),take_at)(multi1, i);
886 el1 = fn(el1, el2);
887 multi1 = FN(MULTI(BASE),restore_at)(multi1, i, el1);
890 if (FN(MULTI(BASE),has_explicit_domain)(multi2))
891 multi1 = FN(MULTI(BASE),intersect_explicit_domain)(multi1,
892 multi2);
894 FN(MULTI(BASE),free)(multi2);
895 return multi1;
896 error:
897 FN(MULTI(BASE),free)(multi1);
898 FN(MULTI(BASE),free)(multi2);
899 return NULL;
902 /* Only used on some multi-expressions.
904 static isl_bool FN(MULTI(BASE),any)(__isl_keep MULTI(BASE) *multi,
905 isl_bool (*test)(__isl_keep EL *)) __attribute__ ((unused));
907 /* Does "test" succeed on any base expression of "multi"?
909 static isl_bool FN(MULTI(BASE),any)(__isl_keep MULTI(BASE) *multi,
910 isl_bool (*test)(__isl_keep EL *))
912 isl_size n;
913 int i;
915 n = FN(MULTI(BASE),size)(multi);
916 if (n < 0)
917 return isl_bool_error;
919 for (i = 0; i < n; ++i) {
920 isl_bool any = test(multi->u.p[i]);
921 if (any < 0 || any)
922 return any;
925 return isl_bool_false;
928 /* Only used on some multi-expressions.
930 static isl_bool FN(MULTI(BASE),every)(__isl_keep MULTI(BASE) *multi,
931 isl_bool (*test)(__isl_keep EL *)) __attribute__ ((unused));
933 /* Does "test" succeed on every base expression of "multi"?
935 static isl_bool FN(MULTI(BASE),every)(__isl_keep MULTI(BASE) *multi,
936 isl_bool (*test)(__isl_keep EL *))
938 isl_size n;
939 int i;
941 n = FN(MULTI(BASE),size)(multi);
942 if (n < 0)
943 return isl_bool_error;
945 for (i = 0; i < n; ++i) {
946 isl_bool every = test(multi->u.p[i]);
947 if (every < 0 || !every)
948 return every;
951 return isl_bool_true;
954 /* Convert a multiple expression defined over a parameter domain
955 * into one that is defined over a zero-dimensional set.
957 __isl_give MULTI(BASE) *FN(MULTI(BASE),from_range)(
958 __isl_take MULTI(BASE) *multi)
960 isl_space *space;
962 if (!multi)
963 return NULL;
964 if (!isl_space_is_set(multi->space))
965 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
966 "not living in a set space",
967 return FN(MULTI(BASE),free)(multi));
969 space = FN(MULTI(BASE),get_space)(multi);
970 space = isl_space_from_range(space);
971 multi = FN(MULTI(BASE),reset_space)(multi, space);
973 return multi;
976 /* Are "multi1" and "multi2" obviously equal?
978 isl_bool FN(MULTI(BASE),plain_is_equal)(__isl_keep MULTI(BASE) *multi1,
979 __isl_keep MULTI(BASE) *multi2)
981 int i;
982 isl_bool equal;
984 if (!multi1 || !multi2)
985 return isl_bool_error;
986 if (multi1->n != multi2->n)
987 return isl_bool_false;
988 equal = isl_space_is_equal(multi1->space, multi2->space);
989 if (equal < 0 || !equal)
990 return equal;
992 for (i = 0; i < multi1->n; ++i) {
993 equal = FN(EL,plain_is_equal)(multi1->u.p[i], multi2->u.p[i]);
994 if (equal < 0 || !equal)
995 return equal;
998 if (FN(MULTI(BASE),has_explicit_domain)(multi1) ||
999 FN(MULTI(BASE),has_explicit_domain)(multi2)) {
1000 equal = FN(MULTI(BASE),equal_explicit_domain)(multi1, multi2);
1001 if (equal < 0 || !equal)
1002 return equal;
1005 return isl_bool_true;