2 * Copyright 2011 Sven Verdoolaege
3 * Copyright 2012-2013 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>
13 #include <isl_val_private.h>
15 #include <isl_reordering.h>
17 #include <isl_multi_macro.h>
19 #define MULTI_NAME(BASE) "isl_multi_" #BASE
20 #define xLIST(EL) EL ## _list
21 #define LIST(EL) xLIST(EL)
23 isl_ctx
*FN(MULTI(BASE
),get_ctx
)(__isl_keep
MULTI(BASE
) *multi
)
25 return multi
? isl_space_get_ctx(multi
->space
) : NULL
;
28 /* Return the space of "multi".
30 __isl_keep isl_space
*FN(MULTI(BASE
),peek_space
)(__isl_keep
MULTI(BASE
) *multi
)
32 return multi
? multi
->space
: NULL
;
35 __isl_give isl_space
*FN(MULTI(BASE
),get_space
)(__isl_keep
MULTI(BASE
) *multi
)
37 return isl_space_copy(FN(MULTI(BASE
),peek_space
)(multi
));
40 /* Return the position of the dimension of the given type and name
42 * Return -1 if no such dimension can be found.
44 int FN(MULTI(BASE
),find_dim_by_name
)(__isl_keep
MULTI(BASE
) *multi
,
45 enum isl_dim_type type
, const char *name
)
49 return isl_space_find_dim_by_name(multi
->space
, type
, name
);
52 __isl_give isl_space
*FN(MULTI(BASE
),get_domain_space
)(
53 __isl_keep
MULTI(BASE
) *multi
)
55 return multi
? isl_space_domain(isl_space_copy(multi
->space
)) : NULL
;
58 /* Allocate a multi expression living in "space".
60 * If the number of base expressions is zero, then make sure
61 * there is enough room in the structure for the explicit domain,
62 * in case the type supports such an explicit domain.
64 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),alloc
)(__isl_take isl_space
*space
)
73 ctx
= isl_space_get_ctx(space
);
74 n
= isl_space_dim(space
, isl_dim_out
);
76 multi
= isl_calloc(ctx
, MULTI(BASE
),
77 sizeof(MULTI(BASE
)) + (n
- 1) * sizeof(struct EL
*));
79 multi
= isl_calloc(ctx
, MULTI(BASE
), sizeof(MULTI(BASE
)));
86 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
))
87 multi
= FN(MULTI(BASE
),init_explicit_domain
)(multi
);
90 isl_space_free(space
);
94 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),dup
)(__isl_keep
MULTI(BASE
) *multi
)
102 dup
= FN(MULTI(BASE
),alloc
)(isl_space_copy(multi
->space
));
106 for (i
= 0; i
< multi
->n
; ++i
)
107 dup
= FN(FN(MULTI(BASE
),set
),BASE
)(dup
, i
,
108 FN(EL
,copy
)(multi
->u
.p
[i
]));
109 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
))
110 dup
= FN(MULTI(BASE
),copy_explicit_domain
)(dup
, multi
);
115 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),cow
)(__isl_take
MULTI(BASE
) *multi
)
124 return FN(MULTI(BASE
),dup
)(multi
);
127 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),copy
)(__isl_keep
MULTI(BASE
) *multi
)
136 __isl_null
MULTI(BASE
) *FN(MULTI(BASE
),free
)(__isl_take
MULTI(BASE
) *multi
)
143 if (--multi
->ref
> 0)
146 isl_space_free(multi
->space
);
147 for (i
= 0; i
< multi
->n
; ++i
)
148 FN(EL
,free
)(multi
->u
.p
[i
]);
149 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
))
150 FN(MULTI(BASE
),free_explicit_domain
)(multi
);
156 unsigned FN(MULTI(BASE
),dim
)(__isl_keep
MULTI(BASE
) *multi
,
157 enum isl_dim_type type
)
159 return multi
? isl_space_dim(multi
->space
, type
) : 0;
162 /* Return the position of the first dimension of "type" with id "id".
163 * Return -1 if there is no such dimension.
165 int FN(MULTI(BASE
),find_dim_by_id
)(__isl_keep
MULTI(BASE
) *multi
,
166 enum isl_dim_type type
, __isl_keep isl_id
*id
)
170 return isl_space_find_dim_by_id(multi
->space
, type
, id
);
173 /* Return the id of the given dimension.
175 __isl_give isl_id
*FN(MULTI(BASE
),get_dim_id
)(__isl_keep
MULTI(BASE
) *multi
,
176 enum isl_dim_type type
, unsigned pos
)
178 return multi
? isl_space_get_dim_id(multi
->space
, type
, pos
) : NULL
;
181 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_name
)(
182 __isl_take
MULTI(BASE
) *multi
,
183 enum isl_dim_type type
, unsigned pos
, const char *s
)
187 multi
= FN(MULTI(BASE
),cow
)(multi
);
191 multi
->space
= isl_space_set_dim_name(multi
->space
, type
, pos
, s
);
193 return FN(MULTI(BASE
),free
)(multi
);
195 if (type
== isl_dim_out
)
197 for (i
= 0; i
< multi
->n
; ++i
) {
198 multi
->u
.p
[i
] = FN(EL
,set_dim_name
)(multi
->u
.p
[i
],
201 return FN(MULTI(BASE
),free
)(multi
);
207 const char *FN(MULTI(BASE
),get_tuple_name
)(__isl_keep
MULTI(BASE
) *multi
,
208 enum isl_dim_type type
)
210 return multi
? isl_space_get_tuple_name(multi
->space
, type
) : NULL
;
213 /* Does the specified tuple have an id?
215 isl_bool
FN(MULTI(BASE
),has_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
216 enum isl_dim_type type
)
219 return isl_bool_error
;
220 return isl_space_has_tuple_id(multi
->space
, type
);
223 /* Return the id of the specified tuple.
225 __isl_give isl_id
*FN(MULTI(BASE
),get_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
226 enum isl_dim_type type
)
228 return multi
? isl_space_get_tuple_id(multi
->space
, type
) : NULL
;
232 #define TYPE MULTI(BASE)
234 #include "check_type_range_templ.c"
236 __isl_give EL
*FN(FN(MULTI(BASE
),get
),BASE
)(__isl_keep
MULTI(BASE
) *multi
,
241 if (FN(MULTI(BASE
),check_range
)(multi
, isl_dim_out
, pos
, 1) < 0)
243 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
244 return FN(EL
,copy
)(multi
->u
.p
[pos
]);
247 /* Set the element at position "pos" of "multi" to "el",
248 * where the position may be empty if "multi" has only a single reference.
250 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),restore
)(
251 __isl_take
MULTI(BASE
) *multi
, int pos
, __isl_take EL
*el
)
253 multi
= FN(MULTI(BASE
),cow
)(multi
);
257 if (FN(MULTI(BASE
),check_range
)(multi
, isl_dim_out
, pos
, 1) < 0)
260 FN(EL
,free
)(multi
->u
.p
[pos
]);
261 multi
->u
.p
[pos
] = el
;
265 FN(MULTI(BASE
),free
)(multi
);
270 /* Set the element at position "pos" of "multi" to "el",
271 * where the position may be empty if "multi" has only a single reference.
272 * However, the space of "multi" is available and is checked
273 * for compatibility with "el".
275 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),restore_check_space
)(
276 __isl_take
MULTI(BASE
) *multi
, int pos
, __isl_take EL
*el
)
280 space
= FN(MULTI(BASE
),peek_space
)(multi
);
281 if (FN(EL
,check_match_domain_space
)(el
, space
) < 0)
282 multi
= FN(MULTI(BASE
),free
)(multi
);
283 return FN(MULTI(BASE
),restore
)(multi
, pos
, el
);
286 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),set
),BASE
)(
287 __isl_take
MULTI(BASE
) *multi
, int pos
, __isl_take EL
*el
)
289 isl_space
*multi_space
= NULL
;
290 isl_space
*el_space
= NULL
;
293 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
294 match
= FN(EL
,matching_params
)(el
, multi_space
);
298 multi
= FN(MULTI(BASE
),align_params
)(multi
,
299 FN(EL
,get_space
)(el
));
300 isl_space_free(multi_space
);
301 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
302 el
= FN(EL
,align_params
)(el
, isl_space_copy(multi_space
));
305 multi
= FN(MULTI(BASE
),restore_check_space
)(multi
, pos
, el
);
307 isl_space_free(multi_space
);
308 isl_space_free(el_space
);
312 FN(MULTI(BASE
),free
)(multi
);
314 isl_space_free(multi_space
);
315 isl_space_free(el_space
);
319 /* Reset the space of "multi". This function is called from isl_pw_templ.c
320 * and doesn't know if the space of an element object is represented
321 * directly or through its domain. It therefore passes along both,
322 * which we pass along to the element function since we don't know how
323 * that is represented either.
325 * If "multi" has an explicit domain, then the caller is expected
326 * to make sure that any modification that would change the dimensions
327 * of the explicit domain has bee applied before this function is called.
329 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space_and_domain
)(
330 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
,
331 __isl_take isl_space
*domain
)
335 multi
= FN(MULTI(BASE
),cow
)(multi
);
336 if (!multi
|| !space
|| !domain
)
339 for (i
= 0; i
< multi
->n
; ++i
) {
340 multi
->u
.p
[i
] = FN(EL
,reset_domain_space
)(multi
->u
.p
[i
],
341 isl_space_copy(domain
));
345 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
)) {
346 multi
= FN(MULTI(BASE
),reset_explicit_domain_space
)(multi
,
347 isl_space_copy(domain
));
351 isl_space_free(domain
);
352 isl_space_free(multi
->space
);
353 multi
->space
= space
;
357 isl_space_free(domain
);
358 isl_space_free(space
);
359 FN(MULTI(BASE
),free
)(multi
);
363 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_domain_space
)(
364 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*domain
)
368 space
= isl_space_extend_domain_with_range(isl_space_copy(domain
),
369 isl_space_copy(multi
->space
));
370 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
373 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space
)(
374 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
)
378 domain
= isl_space_domain(isl_space_copy(space
));
379 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
382 /* Set the id of the given dimension of "multi" to "id".
384 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_id
)(
385 __isl_take
MULTI(BASE
) *multi
,
386 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
390 multi
= FN(MULTI(BASE
),cow
)(multi
);
394 space
= FN(MULTI(BASE
),get_space
)(multi
);
395 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
397 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
400 FN(MULTI(BASE
),free
)(multi
);
404 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_name
)(
405 __isl_keep
MULTI(BASE
) *multi
, enum isl_dim_type type
,
410 multi
= FN(MULTI(BASE
),cow
)(multi
);
414 space
= FN(MULTI(BASE
),get_space
)(multi
);
415 space
= isl_space_set_tuple_name(space
, type
, s
);
417 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
420 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_id
)(
421 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
,
422 __isl_take isl_id
*id
)
426 multi
= FN(MULTI(BASE
),cow
)(multi
);
430 space
= FN(MULTI(BASE
),get_space
)(multi
);
431 space
= isl_space_set_tuple_id(space
, type
, id
);
433 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
439 /* Drop the id on the specified tuple.
441 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_tuple_id
)(
442 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
)
448 if (!FN(MULTI(BASE
),has_tuple_id
)(multi
, type
))
451 multi
= FN(MULTI(BASE
),cow
)(multi
);
455 space
= FN(MULTI(BASE
),get_space
)(multi
);
456 space
= isl_space_reset_tuple_id(space
, type
);
458 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
461 /* Reset the user pointer on all identifiers of parameters and tuples
462 * of the space of "multi".
464 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_user
)(
465 __isl_take
MULTI(BASE
) *multi
)
469 space
= FN(MULTI(BASE
),get_space
)(multi
);
470 space
= isl_space_reset_user(space
);
472 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
475 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),realign_domain
)(
476 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_reordering
*exp
)
481 multi
= FN(MULTI(BASE
),cow
)(multi
);
485 for (i
= 0; i
< multi
->n
; ++i
) {
486 multi
->u
.p
[i
] = FN(EL
,realign_domain
)(multi
->u
.p
[i
],
487 isl_reordering_copy(exp
));
492 space
= isl_reordering_get_space(exp
);
493 multi
= FN(MULTI(BASE
),reset_domain_space
)(multi
, space
);
495 isl_reordering_free(exp
);
498 isl_reordering_free(exp
);
499 FN(MULTI(BASE
),free
)(multi
);
503 /* Align the parameters of "multi" to those of "model".
505 * If "multi" has an explicit domain, then align the parameters
506 * of the domain first.
508 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params
)(
509 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*model
)
512 isl_bool equal_params
;
515 if (!multi
|| !model
)
518 equal_params
= isl_space_has_equal_params(multi
->space
, model
);
519 if (equal_params
< 0)
522 isl_space_free(model
);
526 ctx
= isl_space_get_ctx(model
);
527 if (!isl_space_has_named_params(model
))
528 isl_die(ctx
, isl_error_invalid
,
529 "model has unnamed parameters", goto error
);
530 if (!isl_space_has_named_params(multi
->space
))
531 isl_die(ctx
, isl_error_invalid
,
532 "input has unnamed parameters", goto error
);
534 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
)) {
535 multi
= FN(MULTI(BASE
),align_explicit_domain_params
)(multi
,
536 isl_space_copy(model
));
540 exp
= isl_parameter_alignment_reordering(multi
->space
, model
);
541 exp
= isl_reordering_extend_space(exp
,
542 FN(MULTI(BASE
),get_domain_space
)(multi
));
543 multi
= FN(MULTI(BASE
),realign_domain
)(multi
, exp
);
545 isl_space_free(model
);
548 isl_space_free(model
);
549 FN(MULTI(BASE
),free
)(multi
);
553 /* Create a multi expression in the given space with the elements of "list"
554 * as base expressions.
556 * Since isl_multi_*_restore_* assumes that the element and
557 * the multi expression have matching spaces, the alignment
558 * (if any) needs to be performed beforehand.
560 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),LIST(BASE
))(
561 __isl_take isl_space
*space
, __isl_take
LIST(EL
) *list
)
571 ctx
= isl_space_get_ctx(space
);
572 n
= FN(FN(LIST(EL
),n
),BASE
)(list
);
573 if (n
!= isl_space_dim(space
, isl_dim_out
))
574 isl_die(ctx
, isl_error_invalid
,
575 "invalid number of elements in list", goto error
);
577 for (i
= 0; i
< n
; ++i
) {
578 EL
*el
= FN(LIST(EL
),peek
)(list
, i
);
579 space
= isl_space_align_params(space
, FN(EL
,get_space
)(el
));
581 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
582 for (i
= 0; i
< n
; ++i
) {
583 EL
*el
= FN(FN(LIST(EL
),get
),BASE
)(list
, i
);
584 el
= FN(EL
,align_params
)(el
, isl_space_copy(space
));
585 multi
= FN(MULTI(BASE
),restore_check_space
)(multi
, i
, el
);
588 isl_space_free(space
);
589 FN(LIST(EL
),free
)(list
);
592 isl_space_free(space
);
593 FN(LIST(EL
),free
)(list
);
597 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),drop_dims
)(
598 __isl_take
MULTI(BASE
) *multi
,
599 enum isl_dim_type type
, unsigned first
, unsigned n
)
603 multi
= FN(MULTI(BASE
),cow
)(multi
);
604 if (FN(MULTI(BASE
),check_range
)(multi
, type
, first
, n
) < 0)
605 return FN(MULTI(BASE
),free
)(multi
);
607 multi
->space
= isl_space_drop_dims(multi
->space
, type
, first
, n
);
609 return FN(MULTI(BASE
),free
)(multi
);
611 if (type
== isl_dim_out
) {
612 for (i
= 0; i
< n
; ++i
)
613 FN(EL
,free
)(multi
->u
.p
[first
+ i
]);
614 for (i
= first
; i
+ n
< multi
->n
; ++i
)
615 multi
->u
.p
[i
] = multi
->u
.p
[i
+ n
];
617 if (n
> 0 && FN(MULTI(BASE
),has_explicit_domain
)(multi
))
618 multi
= FN(MULTI(BASE
),init_explicit_domain
)(multi
);
623 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
))
624 multi
= FN(MULTI(BASE
),drop_explicit_domain_dims
)(multi
,
629 for (i
= 0; i
< multi
->n
; ++i
) {
630 multi
->u
.p
[i
] = FN(EL
,drop_dims
)(multi
->u
.p
[i
], type
, first
, n
);
632 return FN(MULTI(BASE
),free
)(multi
);
638 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
640 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params_multi_multi_and
)(
641 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
642 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi1
,
643 __isl_take
MULTI(BASE
) *multi2
))
646 isl_bool equal_params
;
648 if (!multi1
|| !multi2
)
650 equal_params
= isl_space_has_equal_params(multi1
->space
, multi2
->space
);
651 if (equal_params
< 0)
654 return fn(multi1
, multi2
);
655 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
656 if (!isl_space_has_named_params(multi1
->space
) ||
657 !isl_space_has_named_params(multi2
->space
))
658 isl_die(ctx
, isl_error_invalid
,
659 "unaligned unnamed parameters", goto error
);
660 multi1
= FN(MULTI(BASE
),align_params
)(multi1
,
661 FN(MULTI(BASE
),get_space
)(multi2
));
662 multi2
= FN(MULTI(BASE
),align_params
)(multi2
,
663 FN(MULTI(BASE
),get_space
)(multi1
));
664 return fn(multi1
, multi2
);
666 FN(MULTI(BASE
),free
)(multi1
);
667 FN(MULTI(BASE
),free
)(multi2
);
671 /* Given two MULTI(BASE)s A -> B and C -> D,
672 * construct a MULTI(BASE) (A * C) -> [B -> D].
674 * The parameters are assumed to have been aligned.
676 * If "multi1" and/or "multi2" has an explicit domain, then
677 * intersect the domain of the result with these explicit domains.
679 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product_aligned
)(
680 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
687 if (!multi1
|| !multi2
)
690 space
= isl_space_range_product(FN(MULTI(BASE
),get_space
)(multi1
),
691 FN(MULTI(BASE
),get_space
)(multi2
));
692 res
= FN(MULTI(BASE
),alloc
)(space
);
694 n1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
695 n2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
697 for (i
= 0; i
< n1
; ++i
) {
698 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
699 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
702 for (i
= 0; i
< n2
; ++i
) {
703 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
704 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, n1
+ i
, el
);
707 if (FN(MULTI(BASE
),has_explicit_domain
)(multi1
))
708 res
= FN(MULTI(BASE
),intersect_explicit_domain
)(res
, multi1
);
709 if (FN(MULTI(BASE
),has_explicit_domain
)(multi2
))
710 res
= FN(MULTI(BASE
),intersect_explicit_domain
)(res
, multi2
);
712 FN(MULTI(BASE
),free
)(multi1
);
713 FN(MULTI(BASE
),free
)(multi2
);
716 FN(MULTI(BASE
),free
)(multi1
);
717 FN(MULTI(BASE
),free
)(multi2
);
721 /* Given two MULTI(BASE)s A -> B and C -> D,
722 * construct a MULTI(BASE) (A * C) -> [B -> D].
724 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product
)(
725 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
727 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
728 &FN(MULTI(BASE
),range_product_aligned
));
731 /* Is the range of "multi" a wrapped relation?
733 isl_bool
FN(MULTI(BASE
),range_is_wrapping
)(__isl_keep
MULTI(BASE
) *multi
)
736 return isl_bool_error
;
737 return isl_space_range_is_wrapping(multi
->space
);
740 /* Given a function A -> [B -> C], extract the function A -> B.
742 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_domain
)(
743 __isl_take
MULTI(BASE
) *multi
)
750 if (!isl_space_range_is_wrapping(multi
->space
))
751 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
752 "range is not a product",
753 return FN(MULTI(BASE
),free
)(multi
));
755 space
= FN(MULTI(BASE
),get_space
)(multi
);
756 total
= isl_space_dim(space
, isl_dim_out
);
757 space
= isl_space_range_factor_domain(space
);
758 keep
= isl_space_dim(space
, isl_dim_out
);
759 multi
= FN(MULTI(BASE
),drop_dims
)(multi
,
760 isl_dim_out
, keep
, total
- keep
);
761 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
766 /* Given a function A -> [B -> C], extract the function A -> C.
768 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_range
)(
769 __isl_take
MULTI(BASE
) *multi
)
776 if (!isl_space_range_is_wrapping(multi
->space
))
777 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
778 "range is not a product",
779 return FN(MULTI(BASE
),free
)(multi
));
781 space
= FN(MULTI(BASE
),get_space
)(multi
);
782 total
= isl_space_dim(space
, isl_dim_out
);
783 space
= isl_space_range_factor_range(space
);
784 keep
= isl_space_dim(space
, isl_dim_out
);
785 multi
= FN(MULTI(BASE
),drop_dims
)(multi
, isl_dim_out
, 0, total
- keep
);
786 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
791 /* Given a function [B -> C], extract the function C.
793 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),factor_range
)(
794 __isl_take
MULTI(BASE
) *multi
)
801 if (!isl_space_is_wrapping(multi
->space
))
802 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
803 "not a product", return FN(MULTI(BASE
),free
)(multi
));
805 space
= FN(MULTI(BASE
),get_space
)(multi
);
806 total
= isl_space_dim(space
, isl_dim_set
);
807 space
= isl_space_factor_range(space
);
808 keep
= isl_space_dim(space
, isl_dim_set
);
809 multi
= FN(MULTI(BASE
),drop_dims
)(multi
, isl_dim_set
, 0, total
- keep
);
810 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
815 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flatten_range
)(
816 __isl_take
MULTI(BASE
) *multi
)
821 if (!multi
->space
->nested
[1])
824 multi
= FN(MULTI(BASE
),cow
)(multi
);
828 multi
->space
= isl_space_flatten_range(multi
->space
);
830 return FN(MULTI(BASE
),free
)(multi
);
835 /* Given two MULTI(BASE)s A -> B and C -> D,
836 * construct a MULTI(BASE) (A * C) -> (B, D).
838 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flat_range_product
)(
839 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
843 multi
= FN(MULTI(BASE
),range_product
)(multi1
, multi2
);
844 multi
= FN(MULTI(BASE
),flatten_range
)(multi
);
848 /* Given two multi expressions, "multi1"
852 * where B2 starts at position "pos", and "multi2"
856 * return the multi expression
860 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_splice
)(
861 __isl_take
MULTI(BASE
) *multi1
, unsigned pos
,
862 __isl_take
MULTI(BASE
) *multi2
)
867 if (!multi1
|| !multi2
)
870 if (FN(MULTI(BASE
),check_range
)(multi1
, isl_dim_out
, pos
, 0) < 0)
872 dim
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
874 res
= FN(MULTI(BASE
),copy
)(multi1
);
875 res
= FN(MULTI(BASE
),drop_dims
)(res
, isl_dim_out
, pos
, dim
- pos
);
876 multi1
= FN(MULTI(BASE
),drop_dims
)(multi1
, isl_dim_out
, 0, pos
);
878 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi2
);
879 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi1
);
883 FN(MULTI(BASE
),free
)(multi1
);
884 FN(MULTI(BASE
),free
)(multi2
);
888 /* Check that "multi1" and "multi2" live in the same space,
889 * reporting an error if they do not.
891 static isl_stat
FN(MULTI(BASE
),check_equal_space
)(
892 __isl_keep
MULTI(BASE
) *multi1
, __isl_keep
MULTI(BASE
) *multi2
)
896 if (!multi1
|| !multi2
)
897 return isl_stat_error
;
899 equal
= isl_space_is_equal(multi1
->space
, multi2
->space
);
901 return isl_stat_error
;
903 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
904 "spaces don't match", return isl_stat_error
);
909 /* This function is currently only used from isl_aff.c
911 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
912 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
913 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
914 __attribute__ ((unused
));
916 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
919 * If "multi2" has an explicit domain, then
920 * intersect the domain of the result with this explicit domain.
922 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
923 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
924 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
928 multi1
= FN(MULTI(BASE
),cow
)(multi1
);
929 if (FN(MULTI(BASE
),check_equal_space
)(multi1
, multi2
) < 0)
932 for (i
= 0; i
< multi1
->n
; ++i
) {
933 multi1
->u
.p
[i
] = fn(multi1
->u
.p
[i
],
934 FN(EL
,copy
)(multi2
->u
.p
[i
]));
939 if (FN(MULTI(BASE
),has_explicit_domain
)(multi2
))
940 multi1
= FN(MULTI(BASE
),intersect_explicit_domain
)(multi1
,
943 FN(MULTI(BASE
),free
)(multi2
);
946 FN(MULTI(BASE
),free
)(multi1
);
947 FN(MULTI(BASE
),free
)(multi2
);
951 /* Add "multi2" to "multi1" and return the result.
953 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
955 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),add_aligned
)(
956 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
958 return FN(MULTI(BASE
),bin_op
)(multi1
, multi2
, &FN(EL
,add
));
961 /* Add "multi2" to "multi1" and return the result.
963 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),add
)(__isl_take
MULTI(BASE
) *multi1
,
964 __isl_take
MULTI(BASE
) *multi2
)
966 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
967 &FN(MULTI(BASE
),add_aligned
));
970 /* Subtract "multi2" from "multi1" and return the result.
972 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
974 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),sub_aligned
)(
975 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
977 return FN(MULTI(BASE
),bin_op
)(multi1
, multi2
, &FN(EL
,sub
));
980 /* Subtract "multi2" from "multi1" and return the result.
982 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),sub
)(__isl_take
MULTI(BASE
) *multi1
,
983 __isl_take
MULTI(BASE
) *multi2
)
985 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
986 &FN(MULTI(BASE
),sub_aligned
));
989 /* Multiply the elements of "multi" by "v" and return the result.
991 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_val
)(__isl_take
MULTI(BASE
) *multi
,
992 __isl_take isl_val
*v
)
999 if (isl_val_is_one(v
)) {
1004 if (!isl_val_is_rat(v
))
1005 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1006 "expecting rational factor", goto error
);
1008 multi
= FN(MULTI(BASE
),cow
)(multi
);
1012 for (i
= 0; i
< multi
->n
; ++i
) {
1013 multi
->u
.p
[i
] = FN(EL
,scale_val
)(multi
->u
.p
[i
],
1023 return FN(MULTI(BASE
),free
)(multi
);
1026 /* Divide the elements of "multi" by "v" and return the result.
1028 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_down_val
)(
1029 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_val
*v
)
1036 if (isl_val_is_one(v
)) {
1041 if (!isl_val_is_rat(v
))
1042 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1043 "expecting rational factor", goto error
);
1044 if (isl_val_is_zero(v
))
1045 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1046 "cannot scale down by zero", goto error
);
1048 multi
= FN(MULTI(BASE
),cow
)(multi
);
1052 for (i
= 0; i
< multi
->n
; ++i
) {
1053 multi
->u
.p
[i
] = FN(EL
,scale_down_val
)(multi
->u
.p
[i
],
1063 return FN(MULTI(BASE
),free
)(multi
);
1066 /* Multiply the elements of "multi" by the corresponding element of "mv"
1067 * and return the result.
1069 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_multi_val
)(
1070 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1077 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1078 mv
->space
, isl_dim_set
))
1079 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1080 "spaces don't match", goto error
);
1082 multi
= FN(MULTI(BASE
),cow
)(multi
);
1086 for (i
= 0; i
< multi
->n
; ++i
) {
1089 v
= isl_multi_val_get_val(mv
, i
);
1090 multi
->u
.p
[i
] = FN(EL
,scale_val
)(multi
->u
.p
[i
], v
);
1095 isl_multi_val_free(mv
);
1098 isl_multi_val_free(mv
);
1099 return FN(MULTI(BASE
),free
)(multi
);
1102 /* Divide the elements of "multi" by the corresponding element of "mv"
1103 * and return the result.
1105 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_down_multi_val
)(
1106 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1113 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1114 mv
->space
, isl_dim_set
))
1115 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1116 "spaces don't match", goto error
);
1118 multi
= FN(MULTI(BASE
),cow
)(multi
);
1122 for (i
= 0; i
< multi
->n
; ++i
) {
1125 v
= isl_multi_val_get_val(mv
, i
);
1126 multi
->u
.p
[i
] = FN(EL
,scale_down_val
)(multi
->u
.p
[i
], v
);
1131 isl_multi_val_free(mv
);
1134 isl_multi_val_free(mv
);
1135 return FN(MULTI(BASE
),free
)(multi
);
1138 /* Compute the residues of the elements of "multi" modulo
1139 * the corresponding element of "mv" and return the result.
1141 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),mod_multi_val
)(
1142 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1149 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1150 mv
->space
, isl_dim_set
))
1151 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1152 "spaces don't match", goto error
);
1154 multi
= FN(MULTI(BASE
),cow
)(multi
);
1158 for (i
= 0; i
< multi
->n
; ++i
) {
1161 v
= isl_multi_val_get_val(mv
, i
);
1162 multi
->u
.p
[i
] = FN(EL
,mod_val
)(multi
->u
.p
[i
], v
);
1167 isl_multi_val_free(mv
);
1170 isl_multi_val_free(mv
);
1171 return FN(MULTI(BASE
),free
)(multi
);
1174 /* Convert a multiple expression defined over a parameter domain
1175 * into one that is defined over a zero-dimensional set.
1177 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),from_range
)(
1178 __isl_take
MULTI(BASE
) *multi
)
1184 if (!isl_space_is_set(multi
->space
))
1185 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1186 "not living in a set space",
1187 return FN(MULTI(BASE
),free
)(multi
));
1189 space
= FN(MULTI(BASE
),get_space
)(multi
);
1190 space
= isl_space_from_range(space
);
1191 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
1196 /* Are "multi1" and "multi2" obviously equal?
1198 isl_bool
FN(MULTI(BASE
),plain_is_equal
)(__isl_keep
MULTI(BASE
) *multi1
,
1199 __isl_keep
MULTI(BASE
) *multi2
)
1204 if (!multi1
|| !multi2
)
1205 return isl_bool_error
;
1206 if (multi1
->n
!= multi2
->n
)
1207 return isl_bool_false
;
1208 equal
= isl_space_is_equal(multi1
->space
, multi2
->space
);
1209 if (equal
< 0 || !equal
)
1212 for (i
= 0; i
< multi1
->n
; ++i
) {
1213 equal
= FN(EL
,plain_is_equal
)(multi1
->u
.p
[i
], multi2
->u
.p
[i
]);
1214 if (equal
< 0 || !equal
)
1218 if (FN(MULTI(BASE
),has_explicit_domain
)(multi1
) ||
1219 FN(MULTI(BASE
),has_explicit_domain
)(multi2
)) {
1220 equal
= FN(MULTI(BASE
),equal_explicit_domain
)(multi1
, multi2
);
1221 if (equal
< 0 || !equal
)
1225 return isl_bool_true
;
1228 /* Does "multi" involve any NaNs?
1230 isl_bool
FN(MULTI(BASE
),involves_nan
)(__isl_keep
MULTI(BASE
) *multi
)
1235 return isl_bool_error
;
1237 return isl_bool_false
;
1239 for (i
= 0; i
< multi
->n
; ++i
) {
1240 isl_bool has_nan
= FN(EL
,involves_nan
)(multi
->u
.p
[i
]);
1241 if (has_nan
< 0 || has_nan
)
1245 return isl_bool_false
;
1248 /* Return the opposite of "multi".
1250 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),neg
)(__isl_take
MULTI(BASE
) *multi
)
1254 multi
= FN(MULTI(BASE
),cow
)(multi
);
1258 for (i
= 0; i
< multi
->n
; ++i
) {
1259 multi
->u
.p
[i
] = FN(EL
,neg
)(multi
->u
.p
[i
]);
1261 return FN(MULTI(BASE
),free
)(multi
);