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
11 #include <isl_space_private.h>
13 #include <isl_reordering.h>
15 #include <isl_multi_macro.h>
17 #define MULTI_NAME(BASE) "isl_multi_" #BASE
18 #define xLIST(EL) EL ## _list
19 #define LIST(EL) xLIST(EL)
21 isl_ctx
*FN(MULTI(BASE
),get_ctx
)(__isl_keep
MULTI(BASE
) *multi
)
23 return multi
? isl_space_get_ctx(multi
->space
) : NULL
;
26 __isl_give isl_space
*FN(MULTI(BASE
),get_space
)(__isl_keep
MULTI(BASE
) *multi
)
28 return multi
? isl_space_copy(multi
->space
) : NULL
;
31 /* Return the position of the dimension of the given type and name
33 * Return -1 if no such dimension can be found.
35 int FN(MULTI(BASE
),find_dim_by_name
)(__isl_keep
MULTI(BASE
) *multi
,
36 enum isl_dim_type type
, const char *name
)
40 return isl_space_find_dim_by_name(multi
->space
, type
, name
);
43 __isl_give isl_space
*FN(MULTI(BASE
),get_domain_space
)(
44 __isl_keep
MULTI(BASE
) *multi
)
46 return multi
? isl_space_domain(isl_space_copy(multi
->space
)) : NULL
;
49 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),alloc
)(__isl_take isl_space
*space
)
58 ctx
= isl_space_get_ctx(space
);
59 n
= isl_space_dim(space
, isl_dim_out
);
60 multi
= isl_calloc(ctx
, MULTI(BASE
),
61 sizeof(MULTI(BASE
)) + (n
- 1) * sizeof(struct EL
*));
70 isl_space_free(space
);
74 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),dup
)(__isl_keep
MULTI(BASE
) *multi
)
82 dup
= FN(MULTI(BASE
),alloc
)(isl_space_copy(multi
->space
));
86 for (i
= 0; i
< multi
->n
; ++i
)
87 dup
= FN(FN(MULTI(BASE
),set
),BASE
)(dup
, i
,
88 FN(EL
,copy
)(multi
->p
[i
]));
93 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),cow
)(__isl_take
MULTI(BASE
) *multi
)
102 return FN(MULTI(BASE
),dup
)(multi
);
105 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),copy
)(__isl_keep
MULTI(BASE
) *multi
)
114 __isl_null
MULTI(BASE
) *FN(MULTI(BASE
),free
)(__isl_take
MULTI(BASE
) *multi
)
121 if (--multi
->ref
> 0)
124 isl_space_free(multi
->space
);
125 for (i
= 0; i
< multi
->n
; ++i
)
126 FN(EL
,free
)(multi
->p
[i
]);
133 /* Check whether "multi" has non-zero coefficients for any dimension
134 * in the given range or if any of these dimensions appear
135 * with non-zero coefficients in any of the integer divisions involved.
137 int FN(MULTI(BASE
),involves_dims
)(__isl_keep
MULTI(BASE
) *multi
,
138 enum isl_dim_type type
, unsigned first
, unsigned n
)
144 if (multi
->n
== 0 || n
== 0)
147 for (i
= 0; i
< multi
->n
; ++i
) {
150 involves
= FN(EL
,involves_dims
)(multi
->p
[i
], type
, first
, n
);
151 if (involves
< 0 || involves
)
158 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),insert_dims
)(
159 __isl_take
MULTI(BASE
) *multi
,
160 enum isl_dim_type type
, unsigned first
, unsigned n
)
166 if (type
== isl_dim_out
)
167 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
168 "cannot insert output/set dimensions",
169 return FN(MULTI(BASE
),free
)(multi
));
170 if (n
== 0 && !isl_space_is_named_or_nested(multi
->space
, type
))
173 multi
= FN(MULTI(BASE
),cow
)(multi
);
177 multi
->space
= isl_space_insert_dims(multi
->space
, type
, first
, n
);
179 return FN(MULTI(BASE
),free
)(multi
);
181 for (i
= 0; i
< multi
->n
; ++i
) {
182 multi
->p
[i
] = FN(EL
,insert_dims
)(multi
->p
[i
], type
, first
, n
);
184 return FN(MULTI(BASE
),free
)(multi
);
190 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),add_dims
)(__isl_take
MULTI(BASE
) *multi
,
191 enum isl_dim_type type
, unsigned n
)
195 pos
= FN(MULTI(BASE
),dim
)(multi
, type
);
197 return FN(MULTI(BASE
),insert_dims
)(multi
, type
, pos
, n
);
201 unsigned FN(MULTI(BASE
),dim
)(__isl_keep
MULTI(BASE
) *multi
,
202 enum isl_dim_type type
)
204 return multi
? isl_space_dim(multi
->space
, type
) : 0;
207 /* Return the position of the first dimension of "type" with id "id".
208 * Return -1 if there is no such dimension.
210 int FN(MULTI(BASE
),find_dim_by_id
)(__isl_keep
MULTI(BASE
) *multi
,
211 enum isl_dim_type type
, __isl_keep isl_id
*id
)
215 return isl_space_find_dim_by_id(multi
->space
, type
, id
);
218 /* Return the id of the given dimension.
220 __isl_give isl_id
*FN(MULTI(BASE
),get_dim_id
)(__isl_keep
MULTI(BASE
) *multi
,
221 enum isl_dim_type type
, unsigned pos
)
223 return multi
? isl_space_get_dim_id(multi
->space
, type
, pos
) : NULL
;
226 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_name
)(
227 __isl_take
MULTI(BASE
) *multi
,
228 enum isl_dim_type type
, unsigned pos
, const char *s
)
232 multi
= FN(MULTI(BASE
),cow
)(multi
);
236 multi
->space
= isl_space_set_dim_name(multi
->space
, type
, pos
, s
);
238 return FN(MULTI(BASE
),free
)(multi
);
240 if (type
== isl_dim_out
)
242 for (i
= 0; i
< multi
->n
; ++i
) {
243 multi
->p
[i
] = FN(EL
,set_dim_name
)(multi
->p
[i
], type
, pos
, s
);
245 return FN(MULTI(BASE
),free
)(multi
);
251 const char *FN(MULTI(BASE
),get_tuple_name
)(__isl_keep
MULTI(BASE
) *multi
,
252 enum isl_dim_type type
)
254 return multi
? isl_space_get_tuple_name(multi
->space
, type
) : NULL
;
257 /* Does the specified tuple have an id?
259 int FN(MULTI(BASE
),has_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
260 enum isl_dim_type type
)
262 return multi
? isl_space_has_tuple_id(multi
->space
, type
) : -1;
265 /* Return the id of the specified tuple.
267 __isl_give isl_id
*FN(MULTI(BASE
),get_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
268 enum isl_dim_type type
)
270 return multi
? isl_space_get_tuple_id(multi
->space
, type
) : NULL
;
273 __isl_give EL
*FN(FN(MULTI(BASE
),get
),BASE
)(__isl_keep
MULTI(BASE
) *multi
,
280 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
281 if (pos
< 0 || pos
>= multi
->n
)
282 isl_die(ctx
, isl_error_invalid
,
283 "index out of bounds", return NULL
);
284 return FN(EL
,copy
)(multi
->p
[pos
]);
287 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),set
),BASE
)(
288 __isl_take
MULTI(BASE
) *multi
, int pos
, __isl_take EL
*el
)
290 isl_space
*multi_space
= NULL
;
291 isl_space
*el_space
= NULL
;
294 multi
= FN(MULTI(BASE
),cow
)(multi
);
298 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
299 match
= FN(EL
,matching_params
)(el
, multi_space
);
303 multi
= FN(MULTI(BASE
),align_params
)(multi
,
304 FN(EL
,get_space
)(el
));
305 isl_space_free(multi_space
);
306 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
307 el
= FN(EL
,align_params
)(el
, isl_space_copy(multi_space
));
309 if (FN(EL
,check_match_domain_space
)(el
, multi_space
) < 0)
312 if (pos
< 0 || pos
>= multi
->n
)
313 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
314 "index out of bounds", goto error
);
316 FN(EL
,free
)(multi
->p
[pos
]);
319 isl_space_free(multi_space
);
320 isl_space_free(el_space
);
324 FN(MULTI(BASE
),free
)(multi
);
326 isl_space_free(multi_space
);
327 isl_space_free(el_space
);
331 /* Reset the space of "multi". This function is called from isl_pw_templ.c
332 * and doesn't know if the space of an element object is represented
333 * directly or through its domain. It therefore passes along both,
334 * which we pass along to the element function since we don't how
335 * that is represented either.
337 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space_and_domain
)(
338 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
,
339 __isl_take isl_space
*domain
)
343 multi
= FN(MULTI(BASE
),cow
)(multi
);
344 if (!multi
|| !space
|| !domain
)
347 for (i
= 0; i
< multi
->n
; ++i
) {
348 multi
->p
[i
] = FN(EL
,reset_domain_space
)(multi
->p
[i
],
349 isl_space_copy(domain
));
353 isl_space_free(domain
);
354 isl_space_free(multi
->space
);
355 multi
->space
= space
;
359 isl_space_free(domain
);
360 isl_space_free(space
);
361 FN(MULTI(BASE
),free
)(multi
);
365 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_domain_space
)(
366 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*domain
)
370 space
= isl_space_extend_domain_with_range(isl_space_copy(domain
),
371 isl_space_copy(multi
->space
));
372 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
375 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space
)(
376 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
)
380 domain
= isl_space_domain(isl_space_copy(space
));
381 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
384 /* Set the id of the given dimension of "multi" to "id".
386 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_id
)(
387 __isl_take
MULTI(BASE
) *multi
,
388 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
392 multi
= FN(MULTI(BASE
),cow
)(multi
);
396 space
= FN(MULTI(BASE
),get_space
)(multi
);
397 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
399 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
402 FN(MULTI(BASE
),free
)(multi
);
406 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_name
)(
407 __isl_keep
MULTI(BASE
) *multi
, enum isl_dim_type type
,
412 multi
= FN(MULTI(BASE
),cow
)(multi
);
416 space
= FN(MULTI(BASE
),get_space
)(multi
);
417 space
= isl_space_set_tuple_name(space
, type
, s
);
419 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
422 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_id
)(
423 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
,
424 __isl_take isl_id
*id
)
428 multi
= FN(MULTI(BASE
),cow
)(multi
);
432 space
= FN(MULTI(BASE
),get_space
)(multi
);
433 space
= isl_space_set_tuple_id(space
, type
, id
);
435 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
441 /* Drop the id on the specified tuple.
443 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_tuple_id
)(
444 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
)
450 if (!FN(MULTI(BASE
),has_tuple_id
)(multi
, type
))
453 multi
= FN(MULTI(BASE
),cow
)(multi
);
457 space
= FN(MULTI(BASE
),get_space
)(multi
);
458 space
= isl_space_reset_tuple_id(space
, type
);
460 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
463 /* Reset the user pointer on all identifiers of parameters and tuples
464 * of the space of "multi".
466 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_user
)(
467 __isl_take
MULTI(BASE
) *multi
)
471 space
= FN(MULTI(BASE
),get_space
)(multi
);
472 space
= isl_space_reset_user(space
);
474 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
477 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),realign_domain
)(
478 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_reordering
*exp
)
482 multi
= FN(MULTI(BASE
),cow
)(multi
);
486 for (i
= 0; i
< multi
->n
; ++i
) {
487 multi
->p
[i
] = FN(EL
,realign_domain
)(multi
->p
[i
],
488 isl_reordering_copy(exp
));
493 multi
= FN(MULTI(BASE
),reset_domain_space
)(multi
,
494 isl_space_copy(exp
->dim
));
496 isl_reordering_free(exp
);
499 isl_reordering_free(exp
);
500 FN(MULTI(BASE
),free
)(multi
);
504 /* Align the parameters of "multi" to those of "model".
506 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params
)(
507 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*model
)
512 if (!multi
|| !model
)
515 if (isl_space_match(multi
->space
, isl_dim_param
,
516 model
, isl_dim_param
)) {
517 isl_space_free(model
);
521 ctx
= isl_space_get_ctx(model
);
522 if (!isl_space_has_named_params(model
))
523 isl_die(ctx
, isl_error_invalid
,
524 "model has unnamed parameters", goto error
);
525 if (!isl_space_has_named_params(multi
->space
))
526 isl_die(ctx
, isl_error_invalid
,
527 "input has unnamed parameters", goto error
);
529 model
= isl_space_params(model
);
530 exp
= isl_parameter_alignment_reordering(multi
->space
, model
);
531 exp
= isl_reordering_extend_space(exp
,
532 FN(MULTI(BASE
),get_domain_space
)(multi
));
533 multi
= FN(MULTI(BASE
),realign_domain
)(multi
, exp
);
535 isl_space_free(model
);
538 isl_space_free(model
);
539 FN(MULTI(BASE
),free
)(multi
);
543 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),LIST(BASE
))(
544 __isl_take isl_space
*space
, __isl_take
LIST(EL
) *list
)
554 ctx
= isl_space_get_ctx(space
);
555 n
= FN(FN(LIST(EL
),n
),BASE
)(list
);
556 if (n
!= isl_space_dim(space
, isl_dim_out
))
557 isl_die(ctx
, isl_error_invalid
,
558 "invalid number of elements in list", goto error
);
560 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
561 for (i
= 0; i
< n
; ++i
) {
562 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
563 FN(FN(LIST(EL
),get
),BASE
)(list
, i
));
566 isl_space_free(space
);
567 FN(LIST(EL
),free
)(list
);
570 isl_space_free(space
);
571 FN(LIST(EL
),free
)(list
);
576 /* Create a multi expression in the given space that maps each
577 * input dimension to the corresponding output dimension.
579 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),identity
)(__isl_take isl_space
*space
)
588 if (isl_space_is_set(space
))
589 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
590 "expecting map space", goto error
);
592 n
= isl_space_dim(space
, isl_dim_out
);
593 if (n
!= isl_space_dim(space
, isl_dim_in
))
594 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
595 "number of input and output dimensions needs to be "
596 "the same", goto error
);
598 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
601 isl_space_free(space
);
605 space
= isl_space_domain(space
);
606 ls
= isl_local_space_from_space(space
);
608 for (i
= 0; i
< n
; ++i
) {
610 el
= FN(EL
,var_on_domain
)(isl_local_space_copy(ls
),
612 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
, el
);
615 isl_local_space_free(ls
);
619 isl_space_free(space
);
625 /* Construct a multi expression in the given space with value zero in
626 * each of the output dimensions.
628 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),zero
)(__isl_take isl_space
*space
)
636 n
= isl_space_dim(space
, isl_dim_out
);
637 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
640 isl_space_free(space
);
646 space
= isl_space_domain(space
);
647 ls
= isl_local_space_from_space(space
);
648 el
= FN(EL
,zero_on_domain
)(ls
);
650 for (i
= 0; i
< n
; ++i
)
651 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
662 /* Create a multiple expression with a single output/set dimension
664 * For most multiple expression types, the base type has a single
665 * output/set dimension and the space of the result is therefore
666 * the same as the space of the input.
667 * In the case of isl_multi_union_pw_aff, however, the base type
668 * lives in a parameter space and we therefore need to add
669 * a single set dimension.
671 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),BASE
)(__isl_take EL
*el
)
676 space
= FN(EL
,get_space(el
));
677 if (isl_space_is_params(space
)) {
678 space
= isl_space_set_from_params(space
);
679 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
681 multi
= FN(MULTI(BASE
),alloc
)(space
);
682 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, 0, el
);
688 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),drop_dims
)(
689 __isl_take
MULTI(BASE
) *multi
,
690 enum isl_dim_type type
, unsigned first
, unsigned n
)
695 multi
= FN(MULTI(BASE
),cow
)(multi
);
699 dim
= FN(MULTI(BASE
),dim
)(multi
, type
);
700 if (first
+ n
> dim
|| first
+ n
< first
)
701 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
702 "index out of bounds",
703 return FN(MULTI(BASE
),cow
)(multi
));
705 multi
->space
= isl_space_drop_dims(multi
->space
, type
, first
, n
);
707 return FN(MULTI(BASE
),cow
)(multi
);
709 if (type
== isl_dim_out
) {
710 for (i
= 0; i
< n
; ++i
)
711 FN(EL
,free
)(multi
->p
[first
+ i
]);
712 for (i
= first
; i
+ n
< multi
->n
; ++i
)
713 multi
->p
[i
] = multi
->p
[i
+ n
];
719 for (i
= 0; i
< multi
->n
; ++i
) {
720 multi
->p
[i
] = FN(EL
,drop_dims
)(multi
->p
[i
], type
, first
, n
);
722 return FN(MULTI(BASE
),cow
)(multi
);
728 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
730 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params_multi_multi_and
)(
731 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
732 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi1
,
733 __isl_take
MULTI(BASE
) *multi2
))
737 if (!multi1
|| !multi2
)
739 if (isl_space_match(multi1
->space
, isl_dim_param
,
740 multi2
->space
, isl_dim_param
))
741 return fn(multi1
, multi2
);
742 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
743 if (!isl_space_has_named_params(multi1
->space
) ||
744 !isl_space_has_named_params(multi2
->space
))
745 isl_die(ctx
, isl_error_invalid
,
746 "unaligned unnamed parameters", goto error
);
747 multi1
= FN(MULTI(BASE
),align_params
)(multi1
,
748 FN(MULTI(BASE
),get_space
)(multi2
));
749 multi2
= FN(MULTI(BASE
),align_params
)(multi2
,
750 FN(MULTI(BASE
),get_space
)(multi1
));
751 return fn(multi1
, multi2
);
753 FN(MULTI(BASE
),free
)(multi1
);
754 FN(MULTI(BASE
),free
)(multi2
);
758 /* Given two MULTI(BASE)s A -> B and C -> D,
759 * construct a MULTI(BASE) (A * C) -> [B -> D].
761 * The parameters are assumed to have been aligned.
763 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product_aligned
)(
764 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
771 if (!multi1
|| !multi2
)
774 space
= isl_space_range_product(FN(MULTI(BASE
),get_space
)(multi1
),
775 FN(MULTI(BASE
),get_space
)(multi2
));
776 res
= FN(MULTI(BASE
),alloc
)(space
);
778 n1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
779 n2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
781 for (i
= 0; i
< n1
; ++i
) {
782 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
783 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
786 for (i
= 0; i
< n2
; ++i
) {
787 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
788 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, n1
+ i
, el
);
791 FN(MULTI(BASE
),free
)(multi1
);
792 FN(MULTI(BASE
),free
)(multi2
);
795 FN(MULTI(BASE
),free
)(multi1
);
796 FN(MULTI(BASE
),free
)(multi2
);
800 /* Given two MULTI(BASE)s A -> B and C -> D,
801 * construct a MULTI(BASE) (A * C) -> [B -> D].
803 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product
)(
804 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
806 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
807 &FN(MULTI(BASE
),range_product_aligned
));
810 /* Is the range of "multi" a wrapped relation?
812 int FN(MULTI(BASE
),range_is_wrapping
)(__isl_keep
MULTI(BASE
) *multi
)
816 return isl_space_range_is_wrapping(multi
->space
);
819 /* Given a function A -> [B -> C], extract the function A -> B.
821 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_domain
)(
822 __isl_take
MULTI(BASE
) *multi
)
829 if (!isl_space_range_is_wrapping(multi
->space
))
830 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
831 "range is not a product",
832 return FN(MULTI(BASE
),free
)(multi
));
834 space
= FN(MULTI(BASE
),get_space
)(multi
);
835 total
= isl_space_dim(space
, isl_dim_out
);
836 space
= isl_space_range_factor_domain(space
);
837 keep
= isl_space_dim(space
, isl_dim_out
);
838 multi
= FN(MULTI(BASE
),drop_dims
)(multi
,
839 isl_dim_out
, keep
, total
- keep
);
840 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
845 /* Given a function A -> [B -> C], extract the function A -> C.
847 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_range
)(
848 __isl_take
MULTI(BASE
) *multi
)
855 if (!isl_space_range_is_wrapping(multi
->space
))
856 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
857 "range is not a product",
858 return FN(MULTI(BASE
),free
)(multi
));
860 space
= FN(MULTI(BASE
),get_space
)(multi
);
861 total
= isl_space_dim(space
, isl_dim_out
);
862 space
= isl_space_range_factor_range(space
);
863 keep
= isl_space_dim(space
, isl_dim_out
);
864 multi
= FN(MULTI(BASE
),drop_dims
)(multi
, isl_dim_out
, 0, total
- keep
);
865 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
871 /* Given two MULTI(BASE)s A -> B and C -> D,
872 * construct a MULTI(BASE) [A -> C] -> [B -> D].
874 * The parameters are assumed to have been aligned.
876 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),product_aligned
)(
877 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
883 int in1
, in2
, out1
, out2
;
885 in1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_in
);
886 in2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_in
);
887 out1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
888 out2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
889 space
= isl_space_product(FN(MULTI(BASE
),get_space
)(multi1
),
890 FN(MULTI(BASE
),get_space
)(multi2
));
891 res
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
892 space
= isl_space_domain(space
);
894 for (i
= 0; i
< out1
; ++i
) {
895 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
896 el
= FN(EL
,insert_dims
)(el
, isl_dim_in
, in1
, in2
);
897 el
= FN(EL
,reset_domain_space
)(el
, isl_space_copy(space
));
898 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
901 for (i
= 0; i
< out2
; ++i
) {
902 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
903 el
= FN(EL
,insert_dims
)(el
, isl_dim_in
, 0, in1
);
904 el
= FN(EL
,reset_domain_space
)(el
, isl_space_copy(space
));
905 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, out1
+ i
, el
);
908 isl_space_free(space
);
909 FN(MULTI(BASE
),free
)(multi1
);
910 FN(MULTI(BASE
),free
)(multi2
);
914 /* Given two MULTI(BASE)s A -> B and C -> D,
915 * construct a MULTI(BASE) [A -> C] -> [B -> D].
917 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),product
)(
918 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
920 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
921 &FN(MULTI(BASE
),product_aligned
));
925 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flatten_range
)(
926 __isl_take
MULTI(BASE
) *multi
)
931 if (!multi
->space
->nested
[1])
934 multi
= FN(MULTI(BASE
),cow
)(multi
);
938 multi
->space
= isl_space_flatten_range(multi
->space
);
940 return FN(MULTI(BASE
),free
)(multi
);
945 /* Given two MULTI(BASE)s A -> B and C -> D,
946 * construct a MULTI(BASE) (A * C) -> (B, D).
948 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flat_range_product
)(
949 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
953 multi
= FN(MULTI(BASE
),range_product
)(multi1
, multi2
);
954 multi
= FN(MULTI(BASE
),flatten_range
)(multi
);
958 /* Given two multi expressions, "multi1"
962 * where B2 starts at position "pos", and "multi2"
966 * return the multi expression
970 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_splice
)(
971 __isl_take
MULTI(BASE
) *multi1
, unsigned pos
,
972 __isl_take
MULTI(BASE
) *multi2
)
977 if (!multi1
|| !multi2
)
980 dim
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
982 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
983 "index out of bounds", goto error
);
985 res
= FN(MULTI(BASE
),copy
)(multi1
);
986 res
= FN(MULTI(BASE
),drop_dims
)(res
, isl_dim_out
, pos
, dim
- pos
);
987 multi1
= FN(MULTI(BASE
),drop_dims
)(multi1
, isl_dim_out
, 0, pos
);
989 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi2
);
990 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi1
);
994 FN(MULTI(BASE
),free
)(multi1
);
995 FN(MULTI(BASE
),free
)(multi2
);
1000 /* Given two multi expressions, "multi1"
1002 * [A1 A2] -> [B1 B2]
1004 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
1009 * return the multi expression
1011 * [A1 C A2] -> [B1 D B2]
1013 * We first insert input dimensions to obtain
1015 * [A1 C A2] -> [B1 B2]
1021 * and then apply range_splice.
1023 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),splice
)(
1024 __isl_take
MULTI(BASE
) *multi1
, unsigned in_pos
, unsigned out_pos
,
1025 __isl_take
MULTI(BASE
) *multi2
)
1030 if (!multi1
|| !multi2
)
1033 n_in1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_in
);
1035 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
1036 "index out of bounds", goto error
);
1038 n_in2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_in
);
1040 multi1
= FN(MULTI(BASE
),insert_dims
)(multi1
, isl_dim_in
, in_pos
, n_in2
);
1041 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, n_in2
,
1043 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, 0, in_pos
);
1045 return FN(MULTI(BASE
),range_splice
)(multi1
, out_pos
, multi2
);
1047 FN(MULTI(BASE
),free
)(multi1
);
1048 FN(MULTI(BASE
),free
)(multi2
);
1053 /* This function is currently only used from isl_aff.c
1055 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
1056 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
1057 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
1058 __attribute__ ((unused
));
1060 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
1061 * return the result.
1063 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
1064 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
1065 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
1070 multi1
= FN(MULTI(BASE
),cow
)(multi1
);
1071 if (!multi1
|| !multi2
)
1074 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
1075 if (!isl_space_is_equal(multi1
->space
, multi2
->space
))
1076 isl_die(ctx
, isl_error_invalid
,
1077 "spaces don't match", goto error
);
1079 for (i
= 0; i
< multi1
->n
; ++i
) {
1080 multi1
->p
[i
] = fn(multi1
->p
[i
], FN(EL
,copy
)(multi2
->p
[i
]));
1085 FN(MULTI(BASE
),free
)(multi2
);
1088 FN(MULTI(BASE
),free
)(multi1
);
1089 FN(MULTI(BASE
),free
)(multi2
);
1093 /* Subtract "multi2" from "multi1" and return the result.
1095 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
1097 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),sub_aligned
)(
1098 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
1100 return FN(MULTI(BASE
),bin_op
)(multi1
, multi2
, &FN(EL
,sub
));
1103 /* Subtract "multi2" from "multi1" and return the result.
1105 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),sub
)(__isl_take
MULTI(BASE
) *multi1
,
1106 __isl_take
MULTI(BASE
) *multi2
)
1108 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
1109 &FN(MULTI(BASE
),sub_aligned
));
1112 /* Multiply the elements of "multi" by "v" and return the result.
1114 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_val
)(__isl_take
MULTI(BASE
) *multi
,
1115 __isl_take isl_val
*v
)
1122 if (isl_val_is_one(v
)) {
1127 if (!isl_val_is_rat(v
))
1128 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1129 "expecting rational factor", goto error
);
1131 multi
= FN(MULTI(BASE
),cow
)(multi
);
1135 for (i
= 0; i
< multi
->n
; ++i
) {
1136 multi
->p
[i
] = FN(EL
,scale_val
)(multi
->p
[i
], isl_val_copy(v
));
1145 return FN(MULTI(BASE
),free
)(multi
);
1148 /* Divide the elements of "multi" by "v" and return the result.
1150 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_down_val
)(
1151 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_val
*v
)
1158 if (isl_val_is_one(v
)) {
1163 if (!isl_val_is_rat(v
))
1164 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1165 "expecting rational factor", goto error
);
1166 if (isl_val_is_zero(v
))
1167 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1168 "cannot scale down by zero", goto error
);
1170 multi
= FN(MULTI(BASE
),cow
)(multi
);
1174 for (i
= 0; i
< multi
->n
; ++i
) {
1175 multi
->p
[i
] = FN(EL
,scale_down_val
)(multi
->p
[i
],
1185 return FN(MULTI(BASE
),free
)(multi
);
1188 /* Multiply the elements of "multi" by the corresponding element of "mv"
1189 * and return the result.
1191 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_multi_val
)(
1192 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1199 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1200 mv
->space
, isl_dim_set
))
1201 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1202 "spaces don't match", goto error
);
1204 multi
= FN(MULTI(BASE
),cow
)(multi
);
1208 for (i
= 0; i
< multi
->n
; ++i
) {
1211 v
= isl_multi_val_get_val(mv
, i
);
1212 multi
->p
[i
] = FN(EL
,scale_val
)(multi
->p
[i
], v
);
1217 isl_multi_val_free(mv
);
1220 isl_multi_val_free(mv
);
1221 return FN(MULTI(BASE
),free
)(multi
);
1224 /* Divide the elements of "multi" by the corresponding element of "mv"
1225 * and return the result.
1227 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_down_multi_val
)(
1228 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1235 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1236 mv
->space
, isl_dim_set
))
1237 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1238 "spaces don't match", goto error
);
1240 multi
= FN(MULTI(BASE
),cow
)(multi
);
1244 for (i
= 0; i
< multi
->n
; ++i
) {
1247 v
= isl_multi_val_get_val(mv
, i
);
1248 multi
->p
[i
] = FN(EL
,scale_down_val
)(multi
->p
[i
], v
);
1253 isl_multi_val_free(mv
);
1256 isl_multi_val_free(mv
);
1257 return FN(MULTI(BASE
),free
)(multi
);
1260 /* Compute the residues of the elements of "multi" modulo
1261 * the corresponding element of "mv" and return the result.
1263 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),mod_multi_val
)(
1264 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1271 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1272 mv
->space
, isl_dim_set
))
1273 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1274 "spaces don't match", goto error
);
1276 multi
= FN(MULTI(BASE
),cow
)(multi
);
1280 for (i
= 0; i
< multi
->n
; ++i
) {
1283 v
= isl_multi_val_get_val(mv
, i
);
1284 multi
->p
[i
] = FN(EL
,mod_val
)(multi
->p
[i
], v
);
1289 isl_multi_val_free(mv
);
1292 isl_multi_val_free(mv
);
1293 return FN(MULTI(BASE
),free
)(multi
);
1296 #ifndef NO_MOVE_DIMS
1297 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1298 * to dimensions of "dst_type" at "dst_pos".
1300 * We only support moving input dimensions to parameters and vice versa.
1302 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),move_dims
)(__isl_take
MULTI(BASE
) *multi
,
1303 enum isl_dim_type dst_type
, unsigned dst_pos
,
1304 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1312 !isl_space_is_named_or_nested(multi
->space
, src_type
) &&
1313 !isl_space_is_named_or_nested(multi
->space
, dst_type
))
1316 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
1317 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1318 "cannot move output/set dimension",
1319 return FN(MULTI(BASE
),free
)(multi
));
1320 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
1321 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1323 return FN(MULTI(BASE
),free
)(multi
));
1324 if (src_pos
+ n
> isl_space_dim(multi
->space
, src_type
))
1325 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1326 "range out of bounds",
1327 return FN(MULTI(BASE
),free
)(multi
));
1328 if (dst_type
== src_type
)
1329 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_unsupported
,
1330 "moving dims within the same type not supported",
1331 return FN(MULTI(BASE
),free
)(multi
));
1333 multi
= FN(MULTI(BASE
),cow
)(multi
);
1337 multi
->space
= isl_space_move_dims(multi
->space
, dst_type
, dst_pos
,
1338 src_type
, src_pos
, n
);
1340 return FN(MULTI(BASE
),free
)(multi
);
1342 for (i
= 0; i
< multi
->n
; ++i
) {
1343 multi
->p
[i
] = FN(EL
,move_dims
)(multi
->p
[i
], dst_type
, dst_pos
,
1344 src_type
, src_pos
, n
);
1346 return FN(MULTI(BASE
),free
)(multi
);
1353 /* Convert a multiple expression defined over a parameter domain
1354 * into one that is defined over a zero-dimensional set.
1356 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),from_range
)(
1357 __isl_take
MULTI(BASE
) *multi
)
1363 if (!isl_space_is_set(multi
->space
))
1364 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1365 "not living in a set space",
1366 return FN(MULTI(BASE
),free
)(multi
));
1368 space
= FN(MULTI(BASE
),get_space
)(multi
);
1369 space
= isl_space_from_range(space
);
1370 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
1375 /* Are "multi1" and "multi2" obviously equal?
1377 int FN(MULTI(BASE
),plain_is_equal
)(__isl_keep
MULTI(BASE
) *multi1
,
1378 __isl_keep
MULTI(BASE
) *multi2
)
1383 if (!multi1
|| !multi2
)
1385 if (multi1
->n
!= multi2
->n
)
1387 equal
= isl_space_is_equal(multi1
->space
, multi2
->space
);
1388 if (equal
< 0 || !equal
)
1391 for (i
= 0; i
< multi1
->n
; ++i
) {
1392 equal
= FN(EL
,plain_is_equal
)(multi1
->p
[i
], multi2
->p
[i
]);
1393 if (equal
< 0 || !equal
)
1401 /* Return the shared domain of the elements of "multi".
1403 __isl_give isl_set
*FN(MULTI(BASE
),domain
)(__isl_take
MULTI(BASE
) *multi
)
1411 dom
= isl_set_universe(FN(MULTI(BASE
),get_domain_space
)(multi
));
1412 for (i
= 0; i
< multi
->n
; ++i
) {
1415 dom_i
= FN(EL
,domain
)(FN(FN(MULTI(BASE
),get
),BASE
)(multi
, i
));
1416 dom
= isl_set_intersect(dom
, dom_i
);
1419 FN(MULTI(BASE
),free
)(multi
);
1425 /* Return the opposite of "multi".
1427 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),neg
)(__isl_take
MULTI(BASE
) *multi
)
1431 multi
= FN(MULTI(BASE
),cow
)(multi
);
1435 for (i
= 0; i
< multi
->n
; ++i
) {
1436 multi
->p
[i
] = FN(EL
,neg
)(multi
->p
[i
]);
1438 return FN(MULTI(BASE
),free
)(multi
);