isl_*_copy_tuple_id: use BASE instead of TYPE to select variant
[isl.git] / isl_multi_templ.c
blobc7ce1312cf688ae97e17fcf1fa1e621157b5597f
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, *multi_space;
416 multi_space = FN(MULTI(BASE),get_space)(multi);
417 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
418 multi_space);
419 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
422 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
423 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
425 isl_space *domain;
427 domain = isl_space_domain(isl_space_copy(space));
428 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
431 /* Reset the user pointer on all identifiers of parameters and tuples
432 * of the space of "multi".
434 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_user)(
435 __isl_take MULTI(BASE) *multi)
437 isl_space *space;
439 space = FN(MULTI(BASE),get_space)(multi);
440 space = isl_space_reset_user(space);
442 return FN(MULTI(BASE),reset_space)(multi, space);
445 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
446 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
448 int i;
449 isl_size n;
450 isl_space *space;
452 n = FN(MULTI(BASE),size)(multi);
453 if (n < 0 || !exp)
454 goto error;
456 for (i = 0; i < n; ++i) {
457 EL *el;
459 el = FN(MULTI(BASE),take_at)(multi, i);
460 el = FN(EL,realign_domain)(el, isl_reordering_copy(exp));
461 multi = FN(MULTI(BASE),restore_at)(multi, i, el);
464 space = isl_reordering_get_space(exp);
465 multi = FN(MULTI(BASE),reset_domain_space)(multi, space);
467 isl_reordering_free(exp);
468 return multi;
469 error:
470 isl_reordering_free(exp);
471 FN(MULTI(BASE),free)(multi);
472 return NULL;
475 /* Align the parameters of "multi" to those of "model".
477 * If "multi" has an explicit domain, then align the parameters
478 * of the domain first.
480 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
481 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
483 isl_ctx *ctx;
484 isl_bool equal_params;
485 isl_reordering *exp;
487 if (!multi || !model)
488 goto error;
490 equal_params = isl_space_has_equal_params(multi->space, model);
491 if (equal_params < 0)
492 goto error;
493 if (equal_params) {
494 isl_space_free(model);
495 return multi;
498 ctx = isl_space_get_ctx(model);
499 if (!isl_space_has_named_params(model))
500 isl_die(ctx, isl_error_invalid,
501 "model has unnamed parameters", goto error);
502 if (!isl_space_has_named_params(multi->space))
503 isl_die(ctx, isl_error_invalid,
504 "input has unnamed parameters", goto error);
506 if (FN(MULTI(BASE),has_explicit_domain)(multi)) {
507 multi = FN(MULTI(BASE),align_explicit_domain_params)(multi,
508 isl_space_copy(model));
509 if (!multi)
510 goto error;
512 exp = isl_parameter_alignment_reordering(multi->space, model);
513 exp = isl_reordering_extend_space(exp,
514 FN(MULTI(BASE),get_domain_space)(multi));
515 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
517 isl_space_free(model);
518 return multi;
519 error:
520 isl_space_free(model);
521 FN(MULTI(BASE),free)(multi);
522 return NULL;
525 /* Create a multi expression in the given space with the elements of "list"
526 * as base expressions.
528 * Since isl_multi_*_restore_* assumes that the element and
529 * the multi expression have matching spaces, the alignment
530 * (if any) needs to be performed beforehand.
532 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
533 __isl_take isl_space *space, __isl_take LIST(EL) *list)
535 int i;
536 isl_size n, dim;
537 isl_ctx *ctx;
538 MULTI(BASE) *multi;
540 dim = isl_space_dim(space, isl_dim_out);
541 n = FN(FN(LIST(EL),n),BASE)(list);
542 if (dim < 0 || n < 0)
543 goto error;
545 ctx = isl_space_get_ctx(space);
546 if (n != dim)
547 isl_die(ctx, isl_error_invalid,
548 "invalid number of elements in list", goto error);
550 for (i = 0; i < n; ++i) {
551 EL *el = FN(LIST(EL),peek)(list, i);
552 space = isl_space_align_params(space, FN(EL,get_space)(el));
554 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
555 for (i = 0; i < n; ++i) {
556 EL *el = FN(FN(LIST(EL),get),BASE)(list, i);
557 el = FN(EL,align_params)(el, isl_space_copy(space));
558 multi = FN(MULTI(BASE),restore_check_space)(multi, i, el);
561 isl_space_free(space);
562 FN(LIST(EL),free)(list);
563 return multi;
564 error:
565 isl_space_free(space);
566 FN(LIST(EL),free)(list);
567 return NULL;
570 /* This function performs the same operation as isl_multi_*_from_*_list,
571 * but is considered as a function on an isl_space when exported.
573 __isl_give MULTI(BASE) *FN(isl_space_multi,BASE)(__isl_take isl_space *space,
574 __isl_take LIST(EL) *list)
576 return FN(FN(MULTI(BASE),from),LIST(BASE))(space, list);
579 /* Drop the "n" output dimensions of "multi" starting at "first",
580 * where the space is assumed to have been adjusted already.
582 static __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_output_dims)(
583 __isl_take MULTI(BASE) *multi, unsigned first, unsigned n)
585 int i;
587 multi = FN(MULTI(BASE),cow)(multi);
588 if (!multi)
589 return NULL;
591 for (i = 0; i < n; ++i)
592 FN(EL,free)(multi->u.p[first + i]);
593 for (i = first; i + n < multi->n; ++i)
594 multi->u.p[i] = multi->u.p[i + n];
595 multi->n -= n;
596 if (n > 0 && FN(MULTI(BASE),has_explicit_domain)(multi))
597 multi = FN(MULTI(BASE),init_explicit_domain)(multi);
599 return multi;
602 __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
603 __isl_take MULTI(BASE) *multi,
604 enum isl_dim_type type, unsigned first, unsigned n)
606 isl_space *space;
607 isl_size size;
608 int i;
610 if (FN(MULTI(BASE),check_range)(multi, type, first, n) < 0)
611 return FN(MULTI(BASE),free)(multi);
613 space = FN(MULTI(BASE),take_space)(multi);
614 space = isl_space_drop_dims(space, type, first, n);
615 multi = FN(MULTI(BASE),restore_space)(multi, space);
617 if (type == isl_dim_out)
618 return FN(MULTI(BASE),drop_output_dims)(multi, first, n);
620 if (FN(MULTI(BASE),has_explicit_domain)(multi))
621 multi = FN(MULTI(BASE),drop_explicit_domain_dims)(multi,
622 type, first, n);
624 size = FN(MULTI(BASE),size)(multi);
625 if (size < 0)
626 return FN(MULTI(BASE),free)(multi);
627 for (i = 0; i < size; ++i) {
628 EL *el;
630 el = FN(MULTI(BASE),take_at)(multi, i);
631 el = FN(EL,drop_dims)(el, type, first, n);
632 multi = FN(MULTI(BASE),restore_at)(multi, i, el);
635 return multi;
638 #undef TYPE
639 #define TYPE MULTI(BASE)
641 #include "isl_check_named_params_templ.c"
642 static
643 #include "isl_align_params_bin_templ.c"
645 /* Given two MULTI(BASE)s A -> B and C -> D,
646 * construct a MULTI(BASE) (A * C) -> [B -> D].
648 * If "multi1" and/or "multi2" has an explicit domain, then
649 * intersect the domain of the result with these explicit domains.
651 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product)(
652 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
654 int i;
655 isl_size n1, n2;
656 EL *el;
657 isl_space *space;
658 MULTI(BASE) *res;
660 FN(MULTI(BASE),align_params_bin)(&multi1, &multi2);
661 n1 = FN(MULTI(BASE),size)(multi1);
662 n2 = FN(MULTI(BASE),size)(multi2);
663 if (n1 < 0 || n2 < 0)
664 goto error;
666 space = isl_space_range_product(FN(MULTI(BASE),get_space)(multi1),
667 FN(MULTI(BASE),get_space)(multi2));
668 res = FN(MULTI(BASE),alloc)(space);
670 for (i = 0; i < n1; ++i) {
671 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
672 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
675 for (i = 0; i < n2; ++i) {
676 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
677 res = FN(FN(MULTI(BASE),set),BASE)(res, n1 + i, el);
680 if (FN(MULTI(BASE),has_explicit_domain)(multi1))
681 res = FN(MULTI(BASE),intersect_explicit_domain)(res, multi1);
682 if (FN(MULTI(BASE),has_explicit_domain)(multi2))
683 res = FN(MULTI(BASE),intersect_explicit_domain)(res, multi2);
685 FN(MULTI(BASE),free)(multi1);
686 FN(MULTI(BASE),free)(multi2);
687 return res;
688 error:
689 FN(MULTI(BASE),free)(multi1);
690 FN(MULTI(BASE),free)(multi2);
691 return NULL;
694 /* Is the range of "multi" a wrapped relation?
696 isl_bool FN(MULTI(BASE),range_is_wrapping)(__isl_keep MULTI(BASE) *multi)
698 if (!multi)
699 return isl_bool_error;
700 return isl_space_range_is_wrapping(multi->space);
703 /* Given a function A -> [B -> C], extract the function A -> B.
705 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_domain)(
706 __isl_take MULTI(BASE) *multi)
708 isl_space *space;
709 isl_size total, keep;
711 total = FN(MULTI(BASE),dim)(multi, isl_dim_out);
712 if (total < 0)
713 return FN(MULTI(BASE),free)(multi);
714 if (!isl_space_range_is_wrapping(multi->space))
715 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
716 "range is not a product",
717 return FN(MULTI(BASE),free)(multi));
719 space = FN(MULTI(BASE),get_space)(multi);
720 space = isl_space_range_factor_domain(space);
721 keep = isl_space_dim(space, isl_dim_out);
722 if (keep < 0)
723 multi = FN(MULTI(BASE),free)(multi);
724 multi = FN(MULTI(BASE),drop_dims)(multi,
725 isl_dim_out, keep, total - keep);
726 multi = FN(MULTI(BASE),reset_space)(multi, space);
728 return multi;
731 /* Given a function A -> [B -> C], extract the function A -> C.
733 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_range)(
734 __isl_take MULTI(BASE) *multi)
736 isl_space *space;
737 isl_size total, keep;
739 total = FN(MULTI(BASE),dim)(multi, isl_dim_out);
740 if (total < 0)
741 return FN(MULTI(BASE),free)(multi);
742 if (!isl_space_range_is_wrapping(multi->space))
743 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
744 "range is not a product",
745 return FN(MULTI(BASE),free)(multi));
747 space = FN(MULTI(BASE),get_space)(multi);
748 space = isl_space_range_factor_range(space);
749 keep = isl_space_dim(space, isl_dim_out);
750 if (keep < 0)
751 multi = FN(MULTI(BASE),free)(multi);
752 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_out, 0, total - keep);
753 multi = FN(MULTI(BASE),reset_space)(multi, space);
755 return multi;
758 /* Given a function [B -> C], extract the function C.
760 __isl_give MULTI(BASE) *FN(MULTI(BASE),factor_range)(
761 __isl_take MULTI(BASE) *multi)
763 isl_space *space;
764 isl_size total, keep;
766 total = FN(MULTI(BASE),dim)(multi, isl_dim_set);
767 if (total < 0)
768 return FN(MULTI(BASE),free)(multi);
769 if (!isl_space_is_wrapping(multi->space))
770 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
771 "not a product", return FN(MULTI(BASE),free)(multi));
773 space = FN(MULTI(BASE),get_space)(multi);
774 space = isl_space_factor_range(space);
775 keep = isl_space_dim(space, isl_dim_set);
776 if (keep < 0)
777 multi = FN(MULTI(BASE),free)(multi);
778 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_set, 0, total - keep);
779 multi = FN(MULTI(BASE),reset_space)(multi, space);
781 return multi;
784 __isl_give MULTI(BASE) *FN(MULTI(BASE),flatten_range)(
785 __isl_take MULTI(BASE) *multi)
787 isl_space *space;
789 space = FN(MULTI(BASE),take_space)(multi);
790 space = isl_space_flatten_range(space);
791 multi = FN(MULTI(BASE),restore_space)(multi, space);
793 return multi;
796 /* Given two MULTI(BASE)s A -> B and C -> D,
797 * construct a MULTI(BASE) (A * C) -> (B, D).
799 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
800 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
802 MULTI(BASE) *multi;
804 multi = FN(MULTI(BASE),range_product)(multi1, multi2);
805 multi = FN(MULTI(BASE),flatten_range)(multi);
806 return multi;
809 /* Given two multi expressions, "multi1"
811 * [A] -> [B1 B2]
813 * where B2 starts at position "pos", and "multi2"
815 * [A] -> [D]
817 * return the multi expression
819 * [A] -> [B1 D B2]
821 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_splice)(
822 __isl_take MULTI(BASE) *multi1, unsigned pos,
823 __isl_take MULTI(BASE) *multi2)
825 MULTI(BASE) *res;
826 isl_size dim;
828 dim = FN(MULTI(BASE),size)(multi1);
829 if (dim < 0 || !multi2)
830 goto error;
832 if (FN(MULTI(BASE),check_range)(multi1, isl_dim_out, pos, 0) < 0)
833 goto error;
835 res = FN(MULTI(BASE),copy)(multi1);
836 res = FN(MULTI(BASE),drop_dims)(res, isl_dim_out, pos, dim - pos);
837 multi1 = FN(MULTI(BASE),drop_dims)(multi1, isl_dim_out, 0, pos);
839 res = FN(MULTI(BASE),flat_range_product)(res, multi2);
840 res = FN(MULTI(BASE),flat_range_product)(res, multi1);
842 return res;
843 error:
844 FN(MULTI(BASE),free)(multi1);
845 FN(MULTI(BASE),free)(multi2);
846 return NULL;
849 #undef TYPE
850 #define TYPE MULTI(BASE)
852 static
853 #include "isl_type_has_equal_space_bin_templ.c"
854 static
855 #include "isl_type_check_equal_space_templ.c"
857 /* This function is currently only used from isl_aff.c
859 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
860 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
861 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
862 __attribute__ ((unused));
864 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
865 * return the result.
867 * If "multi2" has an explicit domain, then
868 * intersect the domain of the result with this explicit domain.
870 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
871 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
872 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
874 isl_size n;
875 int i;
877 FN(MULTI(BASE),align_params_bin)(&multi1, &multi2);
878 n = FN(MULTI(BASE),size)(multi1);
879 if (n < 0 || FN(MULTI(BASE),check_equal_space)(multi1, multi2) < 0)
880 goto error;
882 for (i = 0; i < n; ++i) {
883 EL *el1, *el2;
885 el2 = FN(MULTI(BASE),get_at)(multi2, i);
886 el1 = FN(MULTI(BASE),take_at)(multi1, i);
887 el1 = fn(el1, el2);
888 multi1 = FN(MULTI(BASE),restore_at)(multi1, i, el1);
891 if (FN(MULTI(BASE),has_explicit_domain)(multi2))
892 multi1 = FN(MULTI(BASE),intersect_explicit_domain)(multi1,
893 multi2);
895 FN(MULTI(BASE),free)(multi2);
896 return multi1;
897 error:
898 FN(MULTI(BASE),free)(multi1);
899 FN(MULTI(BASE),free)(multi2);
900 return NULL;
903 /* Only used on some multi-expressions.
905 static isl_bool FN(MULTI(BASE),any)(__isl_keep MULTI(BASE) *multi,
906 isl_bool (*test)(__isl_keep EL *)) __attribute__ ((unused));
908 /* Does "test" succeed on any base expression of "multi"?
910 static isl_bool FN(MULTI(BASE),any)(__isl_keep MULTI(BASE) *multi,
911 isl_bool (*test)(__isl_keep EL *))
913 isl_size n;
914 int i;
916 n = FN(MULTI(BASE),size)(multi);
917 if (n < 0)
918 return isl_bool_error;
920 for (i = 0; i < n; ++i) {
921 isl_bool any = test(multi->u.p[i]);
922 if (any < 0 || any)
923 return any;
926 return isl_bool_false;
929 /* Only used on some multi-expressions.
931 static isl_bool FN(MULTI(BASE),every)(__isl_keep MULTI(BASE) *multi,
932 isl_bool (*test)(__isl_keep EL *)) __attribute__ ((unused));
934 /* Does "test" succeed on every base expression of "multi"?
936 static isl_bool FN(MULTI(BASE),every)(__isl_keep MULTI(BASE) *multi,
937 isl_bool (*test)(__isl_keep EL *))
939 isl_size n;
940 int i;
942 n = FN(MULTI(BASE),size)(multi);
943 if (n < 0)
944 return isl_bool_error;
946 for (i = 0; i < n; ++i) {
947 isl_bool every = test(multi->u.p[i]);
948 if (every < 0 || !every)
949 return every;
952 return isl_bool_true;
955 /* Convert a multiple expression defined over a parameter domain
956 * into one that is defined over a zero-dimensional set.
958 __isl_give MULTI(BASE) *FN(MULTI(BASE),from_range)(
959 __isl_take MULTI(BASE) *multi)
961 isl_space *space;
963 if (!multi)
964 return NULL;
965 if (!isl_space_is_set(multi->space))
966 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
967 "not living in a set space",
968 return FN(MULTI(BASE),free)(multi));
970 space = FN(MULTI(BASE),get_space)(multi);
971 space = isl_space_from_range(space);
972 multi = FN(MULTI(BASE),reset_space)(multi, space);
974 return multi;
977 /* Are "multi1" and "multi2" obviously equal?
979 isl_bool FN(MULTI(BASE),plain_is_equal)(__isl_keep MULTI(BASE) *multi1,
980 __isl_keep MULTI(BASE) *multi2)
982 int i;
983 isl_bool equal;
985 if (!multi1 || !multi2)
986 return isl_bool_error;
987 if (multi1->n != multi2->n)
988 return isl_bool_false;
989 equal = isl_space_is_equal(multi1->space, multi2->space);
990 if (equal < 0 || !equal)
991 return equal;
993 for (i = 0; i < multi1->n; ++i) {
994 equal = FN(EL,plain_is_equal)(multi1->u.p[i], multi2->u.p[i]);
995 if (equal < 0 || !equal)
996 return equal;
999 if (FN(MULTI(BASE),has_explicit_domain)(multi1) ||
1000 FN(MULTI(BASE),has_explicit_domain)(multi2)) {
1001 equal = FN(MULTI(BASE),equal_explicit_domain)(multi1, multi2);
1002 if (equal < 0 || !equal)
1003 return equal;
1006 return isl_bool_true;