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
12 #include <isl_space_private.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
)
55 n
= isl_space_dim(space
, isl_dim_out
);
59 ctx
= isl_space_get_ctx(space
);
61 multi
= isl_calloc(ctx
, MULTI(BASE
),
62 sizeof(MULTI(BASE
)) + (n
- 1) * sizeof(struct EL
*));
64 multi
= isl_calloc(ctx
, MULTI(BASE
), sizeof(MULTI(BASE
)));
71 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
))
72 multi
= FN(MULTI(BASE
),init_explicit_domain
)(multi
);
75 isl_space_free(space
);
79 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),dup
)(__isl_keep
MULTI(BASE
) *multi
)
87 dup
= FN(MULTI(BASE
),alloc
)(isl_space_copy(multi
->space
));
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
);
100 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),cow
)(__isl_take
MULTI(BASE
) *multi
)
109 return FN(MULTI(BASE
),dup
)(multi
);
112 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),copy
)(__isl_keep
MULTI(BASE
) *multi
)
121 __isl_null
MULTI(BASE
) *FN(MULTI(BASE
),free
)(__isl_take
MULTI(BASE
) *multi
)
128 if (--multi
->ref
> 0)
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
);
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
)
156 return FN(MULTI(BASE
),get_space
)(multi
);
157 space
= multi
->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
)
173 if (multi
->space
== space
) {
174 isl_space_free(space
);
178 multi
= FN(MULTI(BASE
),cow
)(multi
);
181 isl_space_free(multi
->space
);
182 multi
->space
= space
;
186 FN(MULTI(BASE
),free
)(multi
);
187 isl_space_free(space
);
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
;
205 #define TYPE MULTI(BASE)
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
,
214 if (FN(MULTI(BASE
),check_range
)(multi
, isl_dim_out
, pos
, 1) < 0)
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
,
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
,
252 return FN(MULTI(BASE
),get_at
)(multi
, pos
);
253 if (FN(MULTI(BASE
),check_range
)(multi
, isl_dim_out
, pos
, 1) < 0)
255 el
= multi
->u
.p
[pos
];
256 multi
->u
.p
[pos
] = NULL
;
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
)
269 if (multi
->u
.p
[pos
] == el
) {
274 multi
= FN(MULTI(BASE
),cow
)(multi
);
278 FN(EL
,free
)(multi
->u
.p
[pos
]);
279 multi
->u
.p
[pos
] = el
;
283 FN(MULTI(BASE
),free
)(multi
);
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
)
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
;
313 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
314 match
= FN(EL
,matching_params
)(el
, multi_space
);
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
);
332 FN(MULTI(BASE
),free
)(multi
);
334 isl_space_free(multi_space
);
335 isl_space_free(el_space
);
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
)
356 n
= FN(MULTI(BASE
),size
)(multi
);
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
);
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
)
385 n
= FN(MULTI(BASE
),size
)(multi
);
386 if (n
< 0 || !space
|| !domain
)
389 for (i
= 0; i
< n
; ++i
) {
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
);
405 isl_space_free(domain
);
406 isl_space_free(space
);
407 FN(MULTI(BASE
),free
)(multi
);
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
),
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
)
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
)
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
)
452 n
= FN(MULTI(BASE
),size
)(multi
);
456 for (i
= 0; i
< n
; ++i
) {
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
);
470 isl_reordering_free(exp
);
471 FN(MULTI(BASE
),free
)(multi
);
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
)
484 isl_bool equal_params
;
485 isl_space
*domain_space
;
488 if (!multi
|| !model
)
491 equal_params
= isl_space_has_equal_params(multi
->space
, model
);
492 if (equal_params
< 0)
495 isl_space_free(model
);
499 ctx
= isl_space_get_ctx(model
);
500 if (!isl_space_has_named_params(model
))
501 isl_die(ctx
, isl_error_invalid
,
502 "model has unnamed parameters", goto error
);
503 if (!isl_space_has_named_params(multi
->space
))
504 isl_die(ctx
, isl_error_invalid
,
505 "input has unnamed parameters", goto error
);
507 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
)) {
508 multi
= FN(MULTI(BASE
),align_explicit_domain_params
)(multi
,
509 isl_space_copy(model
));
513 domain_space
= FN(MULTI(BASE
),get_domain_space
)(multi
);
514 exp
= isl_parameter_alignment_reordering(domain_space
, model
);
515 isl_space_free(domain_space
);
516 multi
= FN(MULTI(BASE
),realign_domain
)(multi
, exp
);
518 isl_space_free(model
);
521 isl_space_free(model
);
522 FN(MULTI(BASE
),free
)(multi
);
526 /* Create a multi expression in the given space with the elements of "list"
527 * as base expressions.
529 * Since isl_multi_*_restore_* assumes that the element and
530 * the multi expression have matching spaces, the alignment
531 * (if any) needs to be performed beforehand.
533 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),LIST(BASE
))(
534 __isl_take isl_space
*space
, __isl_take
LIST(EL
) *list
)
541 dim
= isl_space_dim(space
, isl_dim_out
);
542 n
= FN(FN(LIST(EL
),n
),BASE
)(list
);
543 if (dim
< 0 || n
< 0)
546 ctx
= isl_space_get_ctx(space
);
548 isl_die(ctx
, isl_error_invalid
,
549 "invalid number of elements in list", goto error
);
551 for (i
= 0; i
< n
; ++i
) {
552 EL
*el
= FN(LIST(EL
),peek
)(list
, i
);
553 space
= isl_space_align_params(space
, FN(EL
,get_space
)(el
));
555 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
556 for (i
= 0; i
< n
; ++i
) {
557 EL
*el
= FN(FN(LIST(EL
),get
),BASE
)(list
, i
);
558 el
= FN(EL
,align_params
)(el
, isl_space_copy(space
));
559 multi
= FN(MULTI(BASE
),restore_check_space
)(multi
, i
, el
);
562 isl_space_free(space
);
563 FN(LIST(EL
),free
)(list
);
566 isl_space_free(space
);
567 FN(LIST(EL
),free
)(list
);
571 /* This function performs the same operation as isl_multi_*_from_*_list,
572 * but is considered as a function on an isl_space when exported.
574 __isl_give
MULTI(BASE
) *FN(isl_space_multi
,BASE
)(__isl_take isl_space
*space
,
575 __isl_take
LIST(EL
) *list
)
577 return FN(FN(MULTI(BASE
),from
),LIST(BASE
))(space
, list
);
580 /* Drop the "n" output dimensions of "multi" starting at "first",
581 * where the space is assumed to have been adjusted already.
583 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),drop_output_dims
)(
584 __isl_take
MULTI(BASE
) *multi
, unsigned first
, unsigned n
)
588 multi
= FN(MULTI(BASE
),cow
)(multi
);
592 for (i
= 0; i
< n
; ++i
)
593 FN(EL
,free
)(multi
->u
.p
[first
+ i
]);
594 for (i
= first
; i
+ n
< multi
->n
; ++i
)
595 multi
->u
.p
[i
] = multi
->u
.p
[i
+ n
];
597 if (n
> 0 && FN(MULTI(BASE
),has_explicit_domain
)(multi
))
598 multi
= FN(MULTI(BASE
),init_explicit_domain
)(multi
);
603 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),drop_dims
)(
604 __isl_take
MULTI(BASE
) *multi
,
605 enum isl_dim_type type
, unsigned first
, unsigned n
)
611 if (FN(MULTI(BASE
),check_range
)(multi
, type
, first
, n
) < 0)
612 return FN(MULTI(BASE
),free
)(multi
);
614 space
= FN(MULTI(BASE
),take_space
)(multi
);
615 space
= isl_space_drop_dims(space
, type
, first
, n
);
616 multi
= FN(MULTI(BASE
),restore_space
)(multi
, space
);
618 if (type
== isl_dim_out
)
619 return FN(MULTI(BASE
),drop_output_dims
)(multi
, first
, n
);
621 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
))
622 multi
= FN(MULTI(BASE
),drop_explicit_domain_dims
)(multi
,
625 size
= FN(MULTI(BASE
),size
)(multi
);
627 return FN(MULTI(BASE
),free
)(multi
);
628 for (i
= 0; i
< size
; ++i
) {
631 el
= FN(MULTI(BASE
),take_at
)(multi
, i
);
632 el
= FN(EL
,drop_dims
)(el
, type
, first
, n
);
633 multi
= FN(MULTI(BASE
),restore_at
)(multi
, i
, el
);
640 #define TYPE MULTI(BASE)
642 #include "isl_check_named_params_templ.c"
644 #include "isl_align_params_bin_templ.c"
646 /* Given two MULTI(BASE)s A -> B and C -> D,
647 * construct a MULTI(BASE) (A * C) -> [B -> D].
649 * If "multi1" and/or "multi2" has an explicit domain, then
650 * intersect the domain of the result with these explicit domains.
652 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product
)(
653 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
661 FN(MULTI(BASE
),align_params_bin
)(&multi1
, &multi2
);
662 n1
= FN(MULTI(BASE
),size
)(multi1
);
663 n2
= FN(MULTI(BASE
),size
)(multi2
);
664 if (n1
< 0 || n2
< 0)
667 space
= isl_space_range_product(FN(MULTI(BASE
),get_space
)(multi1
),
668 FN(MULTI(BASE
),get_space
)(multi2
));
669 res
= FN(MULTI(BASE
),alloc
)(space
);
671 for (i
= 0; i
< n1
; ++i
) {
672 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
673 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
676 for (i
= 0; i
< n2
; ++i
) {
677 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
678 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, n1
+ i
, el
);
681 if (FN(MULTI(BASE
),has_explicit_domain
)(multi1
))
682 res
= FN(MULTI(BASE
),intersect_explicit_domain
)(res
, multi1
);
683 if (FN(MULTI(BASE
),has_explicit_domain
)(multi2
))
684 res
= FN(MULTI(BASE
),intersect_explicit_domain
)(res
, multi2
);
686 FN(MULTI(BASE
),free
)(multi1
);
687 FN(MULTI(BASE
),free
)(multi2
);
690 FN(MULTI(BASE
),free
)(multi1
);
691 FN(MULTI(BASE
),free
)(multi2
);
695 /* Is the range of "multi" a wrapped relation?
697 isl_bool
FN(MULTI(BASE
),range_is_wrapping
)(__isl_keep
MULTI(BASE
) *multi
)
700 return isl_bool_error
;
701 return isl_space_range_is_wrapping(multi
->space
);
704 /* Given a function A -> [B -> C], extract the function A -> B.
706 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_domain
)(
707 __isl_take
MULTI(BASE
) *multi
)
710 isl_size total
, keep
;
712 total
= FN(MULTI(BASE
),dim
)(multi
, isl_dim_out
);
714 return FN(MULTI(BASE
),free
)(multi
);
715 if (!isl_space_range_is_wrapping(multi
->space
))
716 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
717 "range is not a product",
718 return FN(MULTI(BASE
),free
)(multi
));
720 space
= FN(MULTI(BASE
),get_space
)(multi
);
721 space
= isl_space_range_factor_domain(space
);
722 keep
= isl_space_dim(space
, isl_dim_out
);
724 multi
= FN(MULTI(BASE
),free
)(multi
);
725 multi
= FN(MULTI(BASE
),drop_dims
)(multi
,
726 isl_dim_out
, keep
, total
- keep
);
727 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
732 /* Given a function A -> [B -> C], extract the function A -> C.
734 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_range
)(
735 __isl_take
MULTI(BASE
) *multi
)
738 isl_size total
, keep
;
740 total
= FN(MULTI(BASE
),dim
)(multi
, isl_dim_out
);
742 return FN(MULTI(BASE
),free
)(multi
);
743 if (!isl_space_range_is_wrapping(multi
->space
))
744 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
745 "range is not a product",
746 return FN(MULTI(BASE
),free
)(multi
));
748 space
= FN(MULTI(BASE
),get_space
)(multi
);
749 space
= isl_space_range_factor_range(space
);
750 keep
= isl_space_dim(space
, isl_dim_out
);
752 multi
= FN(MULTI(BASE
),free
)(multi
);
753 multi
= FN(MULTI(BASE
),drop_dims
)(multi
, isl_dim_out
, 0, total
- keep
);
754 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
759 /* Given a function [B -> C], extract the function C.
761 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),factor_range
)(
762 __isl_take
MULTI(BASE
) *multi
)
765 isl_size total
, keep
;
767 total
= FN(MULTI(BASE
),dim
)(multi
, isl_dim_set
);
769 return FN(MULTI(BASE
),free
)(multi
);
770 if (!isl_space_is_wrapping(multi
->space
))
771 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
772 "not a product", return FN(MULTI(BASE
),free
)(multi
));
774 space
= FN(MULTI(BASE
),get_space
)(multi
);
775 space
= isl_space_factor_range(space
);
776 keep
= isl_space_dim(space
, isl_dim_set
);
778 multi
= FN(MULTI(BASE
),free
)(multi
);
779 multi
= FN(MULTI(BASE
),drop_dims
)(multi
, isl_dim_set
, 0, total
- keep
);
780 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
785 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flatten_range
)(
786 __isl_take
MULTI(BASE
) *multi
)
790 space
= FN(MULTI(BASE
),take_space
)(multi
);
791 space
= isl_space_flatten_range(space
);
792 multi
= FN(MULTI(BASE
),restore_space
)(multi
, space
);
797 /* Given two MULTI(BASE)s A -> B and C -> D,
798 * construct a MULTI(BASE) (A * C) -> (B, D).
800 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flat_range_product
)(
801 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
805 multi
= FN(MULTI(BASE
),range_product
)(multi1
, multi2
);
806 multi
= FN(MULTI(BASE
),flatten_range
)(multi
);
810 /* Given two multi expressions, "multi1"
814 * where B2 starts at position "pos", and "multi2"
818 * return the multi expression
822 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_splice
)(
823 __isl_take
MULTI(BASE
) *multi1
, unsigned pos
,
824 __isl_take
MULTI(BASE
) *multi2
)
829 dim
= FN(MULTI(BASE
),size
)(multi1
);
830 if (dim
< 0 || !multi2
)
833 if (FN(MULTI(BASE
),check_range
)(multi1
, isl_dim_out
, pos
, 0) < 0)
836 res
= FN(MULTI(BASE
),copy
)(multi1
);
837 res
= FN(MULTI(BASE
),drop_dims
)(res
, isl_dim_out
, pos
, dim
- pos
);
838 multi1
= FN(MULTI(BASE
),drop_dims
)(multi1
, isl_dim_out
, 0, pos
);
840 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi2
);
841 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi1
);
845 FN(MULTI(BASE
),free
)(multi1
);
846 FN(MULTI(BASE
),free
)(multi2
);
851 #define TYPE MULTI(BASE)
854 #include "isl_type_has_equal_space_bin_templ.c"
856 #include "isl_type_check_equal_space_templ.c"
858 /* This function is currently only used from isl_aff.c
860 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
861 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
862 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
863 __attribute__ ((unused
));
865 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
868 * If "multi2" has an explicit domain, then
869 * intersect the domain of the result with this explicit domain.
871 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
872 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
873 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
878 FN(MULTI(BASE
),align_params_bin
)(&multi1
, &multi2
);
879 n
= FN(MULTI(BASE
),size
)(multi1
);
880 if (n
< 0 || FN(MULTI(BASE
),check_equal_space
)(multi1
, multi2
) < 0)
883 for (i
= 0; i
< n
; ++i
) {
886 el2
= FN(MULTI(BASE
),get_at
)(multi2
, i
);
887 el1
= FN(MULTI(BASE
),take_at
)(multi1
, i
);
889 multi1
= FN(MULTI(BASE
),restore_at
)(multi1
, i
, el1
);
892 if (FN(MULTI(BASE
),has_explicit_domain
)(multi2
))
893 multi1
= FN(MULTI(BASE
),intersect_explicit_domain
)(multi1
,
896 FN(MULTI(BASE
),free
)(multi2
);
899 FN(MULTI(BASE
),free
)(multi1
);
900 FN(MULTI(BASE
),free
)(multi2
);
904 /* Only used on some multi-expressions.
906 static isl_bool
FN(MULTI(BASE
),any
)(__isl_keep
MULTI(BASE
) *multi
,
907 isl_bool (*test
)(__isl_keep EL
*)) __attribute__ ((unused
));
909 /* Does "test" succeed on any base expression of "multi"?
911 static isl_bool
FN(MULTI(BASE
),any
)(__isl_keep
MULTI(BASE
) *multi
,
912 isl_bool (*test
)(__isl_keep EL
*))
917 n
= FN(MULTI(BASE
),size
)(multi
);
919 return isl_bool_error
;
921 for (i
= 0; i
< n
; ++i
) {
922 isl_bool any
= test(multi
->u
.p
[i
]);
927 return isl_bool_false
;
930 /* Only used on some multi-expressions.
932 static isl_bool
FN(MULTI(BASE
),every
)(__isl_keep
MULTI(BASE
) *multi
,
933 isl_bool (*test
)(__isl_keep EL
*)) __attribute__ ((unused
));
935 /* Does "test" succeed on every base expression of "multi"?
937 static isl_bool
FN(MULTI(BASE
),every
)(__isl_keep
MULTI(BASE
) *multi
,
938 isl_bool (*test
)(__isl_keep EL
*))
943 n
= FN(MULTI(BASE
),size
)(multi
);
945 return isl_bool_error
;
947 for (i
= 0; i
< n
; ++i
) {
948 isl_bool every
= test(multi
->u
.p
[i
]);
949 if (every
< 0 || !every
)
953 return isl_bool_true
;
957 #define TYPE MULTI(BASE)
958 #include "isl_from_range_templ.c"
960 /* Are "multi1" and "multi2" obviously equal?
962 isl_bool
FN(MULTI(BASE
),plain_is_equal
)(__isl_keep
MULTI(BASE
) *multi1
,
963 __isl_keep
MULTI(BASE
) *multi2
)
968 if (!multi1
|| !multi2
)
969 return isl_bool_error
;
970 if (multi1
->n
!= multi2
->n
)
971 return isl_bool_false
;
972 equal
= isl_space_is_equal(multi1
->space
, multi2
->space
);
973 if (equal
< 0 || !equal
)
976 for (i
= 0; i
< multi1
->n
; ++i
) {
977 equal
= FN(EL
,plain_is_equal
)(multi1
->u
.p
[i
], multi2
->u
.p
[i
]);
978 if (equal
< 0 || !equal
)
982 if (FN(MULTI(BASE
),has_explicit_domain
)(multi1
) ||
983 FN(MULTI(BASE
),has_explicit_domain
)(multi2
)) {
984 equal
= FN(MULTI(BASE
),equal_explicit_domain
)(multi1
, multi2
);
985 if (equal
< 0 || !equal
)
989 return isl_bool_true
;