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 #define xCAT(A,B) A ## B
16 #define CAT(A,B) xCAT(A,B)
18 #define EL CAT(isl_,BASE)
19 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
20 #define FN(TYPE,NAME) xFN(TYPE,NAME)
21 #define xMULTI(BASE) isl_multi_ ## BASE
22 #define MULTI(BASE) xMULTI(BASE)
23 #define MULTI_NAME(BASE) "isl_multi_" #BASE
24 #define xLIST(EL) EL ## _list
25 #define LIST(EL) xLIST(EL)
27 isl_ctx
*FN(MULTI(BASE
),get_ctx
)(__isl_keep
MULTI(BASE
) *multi
)
29 return multi
? isl_space_get_ctx(multi
->space
) : NULL
;
32 __isl_give isl_space
*FN(MULTI(BASE
),get_space
)(__isl_keep
MULTI(BASE
) *multi
)
34 return multi
? isl_space_copy(multi
->space
) : NULL
;
37 /* Return the position of the dimension of the given type and name
39 * Return -1 if no such dimension can be found.
41 int FN(MULTI(BASE
),find_dim_by_name
)(__isl_keep
MULTI(BASE
) *multi
,
42 enum isl_dim_type type
, const char *name
)
46 return isl_space_find_dim_by_name(multi
->space
, type
, name
);
49 __isl_give isl_space
*FN(MULTI(BASE
),get_domain_space
)(
50 __isl_keep
MULTI(BASE
) *multi
)
52 return multi
? isl_space_domain(isl_space_copy(multi
->space
)) : NULL
;
55 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),alloc
)(__isl_take isl_space
*space
)
64 ctx
= isl_space_get_ctx(space
);
65 n
= isl_space_dim(space
, isl_dim_out
);
66 multi
= isl_calloc(ctx
, MULTI(BASE
),
67 sizeof(MULTI(BASE
)) + (n
- 1) * sizeof(struct EL
*));
76 isl_space_free(space
);
80 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),dup
)(__isl_keep
MULTI(BASE
) *multi
)
88 dup
= FN(MULTI(BASE
),alloc
)(isl_space_copy(multi
->space
));
92 for (i
= 0; i
< multi
->n
; ++i
)
93 dup
= FN(FN(MULTI(BASE
),set
),BASE
)(dup
, i
,
94 FN(EL
,copy
)(multi
->p
[i
]));
99 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),cow
)(__isl_take
MULTI(BASE
) *multi
)
108 return FN(MULTI(BASE
),dup
)(multi
);
111 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),copy
)(__isl_keep
MULTI(BASE
) *multi
)
120 __isl_null
MULTI(BASE
) *FN(MULTI(BASE
),free
)(__isl_take
MULTI(BASE
) *multi
)
127 if (--multi
->ref
> 0)
130 isl_space_free(multi
->space
);
131 for (i
= 0; i
< multi
->n
; ++i
)
132 FN(EL
,free
)(multi
->p
[i
]);
138 /* Check whether "multi" has non-zero coefficients for any dimension
139 * in the given range or if any of these dimensions appear
140 * with non-zero coefficients in any of the integer divisions involved.
142 int FN(MULTI(BASE
),involves_dims
)(__isl_keep
MULTI(BASE
) *multi
,
143 enum isl_dim_type type
, unsigned first
, unsigned n
)
149 if (multi
->n
== 0 || n
== 0)
152 for (i
= 0; i
< multi
->n
; ++i
) {
155 involves
= FN(EL
,involves_dims
)(multi
->p
[i
], type
, first
, n
);
156 if (involves
< 0 || involves
)
163 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),insert_dims
)(
164 __isl_take
MULTI(BASE
) *multi
,
165 enum isl_dim_type type
, unsigned first
, unsigned n
)
171 if (type
== isl_dim_out
)
172 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
173 "cannot insert output/set dimensions",
174 return FN(MULTI(BASE
),free
)(multi
));
175 if (n
== 0 && !isl_space_is_named_or_nested(multi
->space
, type
))
178 multi
= FN(MULTI(BASE
),cow
)(multi
);
182 multi
->space
= isl_space_insert_dims(multi
->space
, type
, first
, n
);
184 return FN(MULTI(BASE
),free
)(multi
);
186 for (i
= 0; i
< multi
->n
; ++i
) {
187 multi
->p
[i
] = FN(EL
,insert_dims
)(multi
->p
[i
], type
, first
, n
);
189 return FN(MULTI(BASE
),free
)(multi
);
195 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),add_dims
)(__isl_take
MULTI(BASE
) *multi
,
196 enum isl_dim_type type
, unsigned n
)
200 pos
= FN(MULTI(BASE
),dim
)(multi
, type
);
202 return FN(MULTI(BASE
),insert_dims
)(multi
, type
, pos
, n
);
205 unsigned FN(MULTI(BASE
),dim
)(__isl_keep
MULTI(BASE
) *multi
,
206 enum isl_dim_type type
)
208 return multi
? isl_space_dim(multi
->space
, type
) : 0;
211 /* Return the position of the first dimension of "type" with id "id".
212 * Return -1 if there is no such dimension.
214 int FN(MULTI(BASE
),find_dim_by_id
)(__isl_keep
MULTI(BASE
) *multi
,
215 enum isl_dim_type type
, __isl_keep isl_id
*id
)
219 return isl_space_find_dim_by_id(multi
->space
, type
, id
);
222 /* Return the id of the given dimension.
224 __isl_give isl_id
*FN(MULTI(BASE
),get_dim_id
)(__isl_keep
MULTI(BASE
) *multi
,
225 enum isl_dim_type type
, unsigned pos
)
227 return multi
? isl_space_get_dim_id(multi
->space
, type
, pos
) : NULL
;
230 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_name
)(
231 __isl_take
MULTI(BASE
) *multi
,
232 enum isl_dim_type type
, unsigned pos
, const char *s
)
236 multi
= FN(MULTI(BASE
),cow
)(multi
);
240 multi
->space
= isl_space_set_dim_name(multi
->space
, type
, pos
, s
);
242 return FN(MULTI(BASE
),free
)(multi
);
244 if (type
== isl_dim_out
)
246 for (i
= 0; i
< multi
->n
; ++i
) {
247 multi
->p
[i
] = FN(EL
,set_dim_name
)(multi
->p
[i
], type
, pos
, s
);
249 return FN(MULTI(BASE
),free
)(multi
);
255 const char *FN(MULTI(BASE
),get_tuple_name
)(__isl_keep
MULTI(BASE
) *multi
,
256 enum isl_dim_type type
)
258 return multi
? isl_space_get_tuple_name(multi
->space
, type
) : NULL
;
261 /* Does the specified tuple have an id?
263 int FN(MULTI(BASE
),has_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
264 enum isl_dim_type type
)
266 return multi
? isl_space_has_tuple_id(multi
->space
, type
) : -1;
269 /* Return the id of the specified tuple.
271 __isl_give isl_id
*FN(MULTI(BASE
),get_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
272 enum isl_dim_type type
)
274 return multi
? isl_space_get_tuple_id(multi
->space
, type
) : NULL
;
277 __isl_give EL
*FN(FN(MULTI(BASE
),get
),BASE
)(__isl_keep
MULTI(BASE
) *multi
,
284 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
285 if (pos
< 0 || pos
>= multi
->n
)
286 isl_die(ctx
, isl_error_invalid
,
287 "index out of bounds", return NULL
);
288 return FN(EL
,copy
)(multi
->p
[pos
]);
291 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),set
),BASE
)(
292 __isl_take
MULTI(BASE
) *multi
, int pos
, __isl_take EL
*el
)
294 isl_space
*multi_space
= NULL
;
295 isl_space
*el_space
= NULL
;
298 multi
= FN(MULTI(BASE
),cow
)(multi
);
302 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
303 match
= FN(EL
,matching_params
)(el
, multi_space
);
307 multi
= FN(MULTI(BASE
),align_params
)(multi
,
308 FN(EL
,get_space
)(el
));
309 isl_space_free(multi_space
);
310 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
311 el
= FN(EL
,align_params
)(el
, isl_space_copy(multi_space
));
313 if (FN(EL
,check_match_domain_space
)(el
, multi_space
) < 0)
316 if (pos
< 0 || pos
>= multi
->n
)
317 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
318 "index out of bounds", goto error
);
320 FN(EL
,free
)(multi
->p
[pos
]);
323 isl_space_free(multi_space
);
324 isl_space_free(el_space
);
328 FN(MULTI(BASE
),free
)(multi
);
330 isl_space_free(multi_space
);
331 isl_space_free(el_space
);
335 /* Reset the space of "multi". This function is called from isl_pw_templ.c
336 * and doesn't know if the space of an element object is represented
337 * directly or through its domain. It therefore passes along both,
338 * which we pass along to the element function since we don't how
339 * that is represented either.
341 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space_and_domain
)(
342 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
,
343 __isl_take isl_space
*domain
)
347 multi
= FN(MULTI(BASE
),cow
)(multi
);
348 if (!multi
|| !space
|| !domain
)
351 for (i
= 0; i
< multi
->n
; ++i
) {
352 multi
->p
[i
] = FN(EL
,reset_domain_space
)(multi
->p
[i
],
353 isl_space_copy(domain
));
357 isl_space_free(domain
);
358 isl_space_free(multi
->space
);
359 multi
->space
= space
;
363 isl_space_free(domain
);
364 isl_space_free(space
);
365 FN(MULTI(BASE
),free
)(multi
);
369 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_domain_space
)(
370 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*domain
)
374 space
= isl_space_extend_domain_with_range(isl_space_copy(domain
),
375 isl_space_copy(multi
->space
));
376 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
379 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space
)(
380 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
)
384 domain
= isl_space_domain(isl_space_copy(space
));
385 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
388 /* Set the id of the given dimension of "multi" to "id".
390 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_id
)(
391 __isl_take
MULTI(BASE
) *multi
,
392 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
396 multi
= FN(MULTI(BASE
),cow
)(multi
);
400 space
= FN(MULTI(BASE
),get_space
)(multi
);
401 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
403 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
406 FN(MULTI(BASE
),free
)(multi
);
410 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_name
)(
411 __isl_keep
MULTI(BASE
) *multi
, enum isl_dim_type type
,
416 multi
= FN(MULTI(BASE
),cow
)(multi
);
420 space
= FN(MULTI(BASE
),get_space
)(multi
);
421 space
= isl_space_set_tuple_name(space
, type
, s
);
423 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
426 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_id
)(
427 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
,
428 __isl_take isl_id
*id
)
432 multi
= FN(MULTI(BASE
),cow
)(multi
);
436 space
= FN(MULTI(BASE
),get_space
)(multi
);
437 space
= isl_space_set_tuple_id(space
, type
, id
);
439 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
445 /* Drop the id on the specified tuple.
447 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_tuple_id
)(
448 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
)
454 if (!FN(MULTI(BASE
),has_tuple_id
)(multi
, type
))
457 multi
= FN(MULTI(BASE
),cow
)(multi
);
461 space
= FN(MULTI(BASE
),get_space
)(multi
);
462 space
= isl_space_reset_tuple_id(space
, type
);
464 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
467 /* Reset the user pointer on all identifiers of parameters and tuples
468 * of the space of "multi".
470 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_user
)(
471 __isl_take
MULTI(BASE
) *multi
)
475 space
= FN(MULTI(BASE
),get_space
)(multi
);
476 space
= isl_space_reset_user(space
);
478 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
481 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),realign_domain
)(
482 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_reordering
*exp
)
486 multi
= FN(MULTI(BASE
),cow
)(multi
);
490 for (i
= 0; i
< multi
->n
; ++i
) {
491 multi
->p
[i
] = FN(EL
,realign_domain
)(multi
->p
[i
],
492 isl_reordering_copy(exp
));
497 multi
= FN(MULTI(BASE
),reset_domain_space
)(multi
,
498 isl_space_copy(exp
->dim
));
500 isl_reordering_free(exp
);
503 isl_reordering_free(exp
);
504 FN(MULTI(BASE
),free
)(multi
);
508 /* Align the parameters of "multi" to those of "model".
510 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params
)(
511 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*model
)
516 if (!multi
|| !model
)
519 if (isl_space_match(multi
->space
, isl_dim_param
,
520 model
, isl_dim_param
)) {
521 isl_space_free(model
);
525 ctx
= isl_space_get_ctx(model
);
526 if (!isl_space_has_named_params(model
))
527 isl_die(ctx
, isl_error_invalid
,
528 "model has unnamed parameters", goto error
);
529 if (!isl_space_has_named_params(multi
->space
))
530 isl_die(ctx
, isl_error_invalid
,
531 "input has unnamed parameters", goto error
);
533 model
= isl_space_params(model
);
534 exp
= isl_parameter_alignment_reordering(multi
->space
, model
);
535 exp
= isl_reordering_extend_space(exp
,
536 FN(MULTI(BASE
),get_domain_space
)(multi
));
537 multi
= FN(MULTI(BASE
),realign_domain
)(multi
, exp
);
539 isl_space_free(model
);
542 isl_space_free(model
);
543 FN(MULTI(BASE
),free
)(multi
);
547 #if !defined(NO_GIST) || !defined(NO_INTERSECT_DOMAIN)
548 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params_multi_set_and
)(
549 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*set
,
550 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi
,
551 __isl_take isl_set
*set
))
557 if (isl_space_match(multi
->space
, isl_dim_param
,
558 set
->dim
, isl_dim_param
))
559 return fn(multi
, set
);
560 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
561 if (!isl_space_has_named_params(multi
->space
) ||
562 !isl_space_has_named_params(set
->dim
))
563 isl_die(ctx
, isl_error_invalid
,
564 "unaligned unnamed parameters", goto error
);
565 multi
= FN(MULTI(BASE
),align_params
)(multi
, isl_set_get_space(set
));
566 set
= isl_set_align_params(set
, FN(MULTI(BASE
),get_space
)(multi
));
567 return fn(multi
, set
);
569 FN(MULTI(BASE
),free
)(multi
);
576 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist_aligned
)(
577 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*context
)
581 multi
= FN(MULTI(BASE
),cow
)(multi
);
582 if (!multi
|| !context
)
585 for (i
= 0; i
< multi
->n
; ++i
) {
586 multi
->p
[i
] = FN(EL
,gist
)(multi
->p
[i
], isl_set_copy(context
));
591 isl_set_free(context
);
594 isl_set_free(context
);
595 FN(MULTI(BASE
),free
)(multi
);
599 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist
)(__isl_take
MULTI(BASE
) *multi
,
600 __isl_take isl_set
*context
)
602 return FN(MULTI(BASE
),align_params_multi_set_and
)(multi
, context
,
603 &FN(MULTI(BASE
),gist_aligned
));
606 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist_params
)(
607 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*context
)
609 isl_space
*space
= FN(MULTI(BASE
),get_domain_space
)(multi
);
610 isl_set
*dom_context
= isl_set_universe(space
);
611 dom_context
= isl_set_intersect_params(dom_context
, context
);
612 return FN(MULTI(BASE
),gist
)(multi
, dom_context
);
616 #ifndef NO_INTERSECT_DOMAIN
617 /* Transform the domain of "multi" by combining it with "domain"
620 * The parameters of "multi" and "domain" are assumed to have been aligned.
622 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),intersect_aligned
)(
623 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*domain
,
624 __isl_give EL
*(*fn
)(EL
*el
, __isl_take isl_set
*set2
))
628 if (!multi
|| !domain
)
632 isl_set_free(domain
);
636 multi
= FN(MULTI(BASE
),cow
)(multi
);
640 for (i
= 0; i
< multi
->n
; ++i
) {
641 multi
->p
[i
] = fn(multi
->p
[i
], isl_set_copy(domain
));
646 isl_set_free(domain
);
649 isl_set_free(domain
);
650 FN(MULTI(BASE
),free
)(multi
);
654 /* Intersect the domain of "multi" with "domain".
656 * The parameters of "multi" and "domain" are assumed to have been aligned.
658 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),intersect_domain_aligned
)(
659 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*domain
)
661 return FN(MULTI(BASE
),intersect_aligned
)(multi
, domain
,
662 &FN(EL
,intersect_domain
));
665 /* Intersect the domain of "multi" with "domain".
667 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),intersect_domain
)(
668 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*domain
)
670 return FN(MULTI(BASE
),align_params_multi_set_and
)(multi
, domain
,
671 &FN(MULTI(BASE
),intersect_domain_aligned
));
674 /* Intersect the parameter domain of "multi" with "domain".
676 * The parameters of "multi" and "domain" are assumed to have been aligned.
678 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),intersect_params_aligned
)(
679 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*domain
)
681 return FN(MULTI(BASE
),intersect_aligned
)(multi
, domain
,
682 &FN(EL
,intersect_params
));
685 /* Intersect the parameter domain of "multi" with "domain".
687 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),intersect_params
)(
688 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*domain
)
690 return FN(MULTI(BASE
),align_params_multi_set_and
)(multi
, domain
,
691 &FN(MULTI(BASE
),intersect_params_aligned
));
695 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),LIST(BASE
))(
696 __isl_take isl_space
*space
, __isl_take
LIST(EL
) *list
)
706 ctx
= isl_space_get_ctx(space
);
707 n
= FN(FN(LIST(EL
),n
),BASE
)(list
);
708 if (n
!= isl_space_dim(space
, isl_dim_out
))
709 isl_die(ctx
, isl_error_invalid
,
710 "invalid number of elements in list", goto error
);
712 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
713 for (i
= 0; i
< n
; ++i
) {
714 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
715 FN(FN(LIST(EL
),get
),BASE
)(list
, i
));
718 isl_space_free(space
);
719 FN(LIST(EL
),free
)(list
);
722 isl_space_free(space
);
723 FN(LIST(EL
),free
)(list
);
728 /* Create a multi expression in the given space that maps each
729 * input dimension to the corresponding output dimension.
731 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),identity
)(__isl_take isl_space
*space
)
740 if (isl_space_is_set(space
))
741 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
742 "expecting map space", goto error
);
744 n
= isl_space_dim(space
, isl_dim_out
);
745 if (n
!= isl_space_dim(space
, isl_dim_in
))
746 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
747 "number of input and output dimensions needs to be "
748 "the same", goto error
);
750 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
753 isl_space_free(space
);
757 space
= isl_space_domain(space
);
758 ls
= isl_local_space_from_space(space
);
760 for (i
= 0; i
< n
; ++i
) {
762 el
= FN(EL
,var_on_domain
)(isl_local_space_copy(ls
),
764 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
, el
);
767 isl_local_space_free(ls
);
771 isl_space_free(space
);
776 /* Construct a multi expression in the given space with value zero in
777 * each of the output dimensions.
779 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),zero
)(__isl_take isl_space
*space
)
787 n
= isl_space_dim(space
, isl_dim_out
);
788 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
791 isl_space_free(space
);
797 space
= isl_space_domain(space
);
798 ls
= isl_local_space_from_space(space
);
799 el
= FN(EL
,zero_on_domain
)(ls
);
801 for (i
= 0; i
< n
; ++i
)
802 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
812 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),BASE
)(__isl_take EL
*el
)
816 multi
= FN(MULTI(BASE
),alloc
)(FN(EL
,get_space
)(el
));
817 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, 0, el
);
823 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),drop_dims
)(
824 __isl_take
MULTI(BASE
) *multi
,
825 enum isl_dim_type type
, unsigned first
, unsigned n
)
830 multi
= FN(MULTI(BASE
),cow
)(multi
);
834 dim
= FN(MULTI(BASE
),dim
)(multi
, type
);
835 if (first
+ n
> dim
|| first
+ n
< first
)
836 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
837 "index out of bounds",
838 return FN(MULTI(BASE
),cow
)(multi
));
840 multi
->space
= isl_space_drop_dims(multi
->space
, type
, first
, n
);
842 return FN(MULTI(BASE
),cow
)(multi
);
844 if (type
== isl_dim_out
) {
845 for (i
= 0; i
< n
; ++i
)
846 FN(EL
,free
)(multi
->p
[first
+ i
]);
847 for (i
= first
; i
+ n
< multi
->n
; ++i
)
848 multi
->p
[i
] = multi
->p
[i
+ n
];
854 for (i
= 0; i
< multi
->n
; ++i
) {
855 multi
->p
[i
] = FN(EL
,drop_dims
)(multi
->p
[i
], type
, first
, n
);
857 return FN(MULTI(BASE
),cow
)(multi
);
863 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
865 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params_multi_multi_and
)(
866 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
867 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi1
,
868 __isl_take
MULTI(BASE
) *multi2
))
872 if (!multi1
|| !multi2
)
874 if (isl_space_match(multi1
->space
, isl_dim_param
,
875 multi2
->space
, isl_dim_param
))
876 return fn(multi1
, multi2
);
877 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
878 if (!isl_space_has_named_params(multi1
->space
) ||
879 !isl_space_has_named_params(multi2
->space
))
880 isl_die(ctx
, isl_error_invalid
,
881 "unaligned unnamed parameters", goto error
);
882 multi1
= FN(MULTI(BASE
),align_params
)(multi1
,
883 FN(MULTI(BASE
),get_space
)(multi2
));
884 multi2
= FN(MULTI(BASE
),align_params
)(multi2
,
885 FN(MULTI(BASE
),get_space
)(multi1
));
886 return fn(multi1
, multi2
);
888 FN(MULTI(BASE
),free
)(multi1
);
889 FN(MULTI(BASE
),free
)(multi2
);
893 /* Given two MULTI(BASE)s A -> B and C -> D,
894 * construct a MULTI(BASE) (A * C) -> [B -> D].
896 * The parameters are assumed to have been aligned.
898 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product_aligned
)(
899 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
906 if (!multi1
|| !multi2
)
909 space
= isl_space_range_product(FN(MULTI(BASE
),get_space
)(multi1
),
910 FN(MULTI(BASE
),get_space
)(multi2
));
911 res
= FN(MULTI(BASE
),alloc
)(space
);
913 n1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
914 n2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
916 for (i
= 0; i
< n1
; ++i
) {
917 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
918 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
921 for (i
= 0; i
< n2
; ++i
) {
922 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
923 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, n1
+ i
, el
);
926 FN(MULTI(BASE
),free
)(multi1
);
927 FN(MULTI(BASE
),free
)(multi2
);
930 FN(MULTI(BASE
),free
)(multi1
);
931 FN(MULTI(BASE
),free
)(multi2
);
935 /* Given two MULTI(BASE)s A -> B and C -> D,
936 * construct a MULTI(BASE) (A * C) -> [B -> D].
938 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product
)(
939 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
941 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
942 &FN(MULTI(BASE
),range_product_aligned
));
945 /* Is the range of "multi" a wrapped relation?
947 int FN(MULTI(BASE
),range_is_wrapping
)(__isl_keep
MULTI(BASE
) *multi
)
951 return isl_space_range_is_wrapping(multi
->space
);
954 /* Given a function A -> [B -> C], extract the function A -> B.
956 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_domain
)(
957 __isl_take
MULTI(BASE
) *multi
)
964 if (!isl_space_range_is_wrapping(multi
->space
))
965 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
966 "range is not a product",
967 return FN(MULTI(BASE
),free
)(multi
));
969 space
= FN(MULTI(BASE
),get_space
)(multi
);
970 total
= isl_space_dim(space
, isl_dim_out
);
971 space
= isl_space_range_factor_domain(space
);
972 keep
= isl_space_dim(space
, isl_dim_out
);
973 multi
= FN(MULTI(BASE
),drop_dims
)(multi
,
974 isl_dim_out
, keep
, total
- keep
);
975 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
980 /* Given a function A -> [B -> C], extract the function A -> C.
982 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_range
)(
983 __isl_take
MULTI(BASE
) *multi
)
990 if (!isl_space_range_is_wrapping(multi
->space
))
991 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
992 "range is not a product",
993 return FN(MULTI(BASE
),free
)(multi
));
995 space
= FN(MULTI(BASE
),get_space
)(multi
);
996 total
= isl_space_dim(space
, isl_dim_out
);
997 space
= isl_space_range_factor_range(space
);
998 keep
= isl_space_dim(space
, isl_dim_out
);
999 multi
= FN(MULTI(BASE
),drop_dims
)(multi
, isl_dim_out
, 0, total
- keep
);
1000 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
1005 /* Given two MULTI(BASE)s A -> B and C -> D,
1006 * construct a MULTI(BASE) [A -> C] -> [B -> D].
1008 * The parameters are assumed to have been aligned.
1010 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),product_aligned
)(
1011 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
1017 int in1
, in2
, out1
, out2
;
1019 in1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_in
);
1020 in2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_in
);
1021 out1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
1022 out2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
1023 space
= isl_space_product(FN(MULTI(BASE
),get_space
)(multi1
),
1024 FN(MULTI(BASE
),get_space
)(multi2
));
1025 res
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
1026 space
= isl_space_domain(space
);
1028 for (i
= 0; i
< out1
; ++i
) {
1029 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
1030 el
= FN(EL
,insert_dims
)(el
, isl_dim_in
, in1
, in2
);
1031 el
= FN(EL
,reset_domain_space
)(el
, isl_space_copy(space
));
1032 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
1035 for (i
= 0; i
< out2
; ++i
) {
1036 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
1037 el
= FN(EL
,insert_dims
)(el
, isl_dim_in
, 0, in1
);
1038 el
= FN(EL
,reset_domain_space
)(el
, isl_space_copy(space
));
1039 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, out1
+ i
, el
);
1042 isl_space_free(space
);
1043 FN(MULTI(BASE
),free
)(multi1
);
1044 FN(MULTI(BASE
),free
)(multi2
);
1048 /* Given two MULTI(BASE)s A -> B and C -> D,
1049 * construct a MULTI(BASE) [A -> C] -> [B -> D].
1051 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),product
)(
1052 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
1054 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
1055 &FN(MULTI(BASE
),product_aligned
));
1058 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flatten_range
)(
1059 __isl_take
MULTI(BASE
) *multi
)
1064 if (!multi
->space
->nested
[1])
1067 multi
= FN(MULTI(BASE
),cow
)(multi
);
1071 multi
->space
= isl_space_flatten_range(multi
->space
);
1073 return FN(MULTI(BASE
),free
)(multi
);
1078 /* Given two MULTI(BASE)s A -> B and C -> D,
1079 * construct a MULTI(BASE) (A * C) -> (B, D).
1081 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flat_range_product
)(
1082 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
1086 multi
= FN(MULTI(BASE
),range_product
)(multi1
, multi2
);
1087 multi
= FN(MULTI(BASE
),flatten_range
)(multi
);
1091 /* Given two multi expressions, "multi1"
1095 * where B2 starts at position "pos", and "multi2"
1099 * return the multi expression
1103 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_splice
)(
1104 __isl_take
MULTI(BASE
) *multi1
, unsigned pos
,
1105 __isl_take
MULTI(BASE
) *multi2
)
1110 if (!multi1
|| !multi2
)
1113 dim
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
1115 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
1116 "index out of bounds", goto error
);
1118 res
= FN(MULTI(BASE
),copy
)(multi1
);
1119 res
= FN(MULTI(BASE
),drop_dims
)(res
, isl_dim_out
, pos
, dim
- pos
);
1120 multi1
= FN(MULTI(BASE
),drop_dims
)(multi1
, isl_dim_out
, 0, pos
);
1122 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi2
);
1123 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi1
);
1127 FN(MULTI(BASE
),free
)(multi1
);
1128 FN(MULTI(BASE
),free
)(multi2
);
1132 /* Given two multi expressions, "multi1"
1134 * [A1 A2] -> [B1 B2]
1136 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
1141 * return the multi expression
1143 * [A1 C A2] -> [B1 D B2]
1145 * We first insert input dimensions to obtain
1147 * [A1 C A2] -> [B1 B2]
1153 * and then apply range_splice.
1155 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),splice
)(
1156 __isl_take
MULTI(BASE
) *multi1
, unsigned in_pos
, unsigned out_pos
,
1157 __isl_take
MULTI(BASE
) *multi2
)
1162 if (!multi1
|| !multi2
)
1165 n_in1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_in
);
1167 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
1168 "index out of bounds", goto error
);
1170 n_in2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_in
);
1172 multi1
= FN(MULTI(BASE
),insert_dims
)(multi1
, isl_dim_in
, in_pos
, n_in2
);
1173 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, n_in2
,
1175 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, 0, in_pos
);
1177 return FN(MULTI(BASE
),range_splice
)(multi1
, out_pos
, multi2
);
1179 FN(MULTI(BASE
),free
)(multi1
);
1180 FN(MULTI(BASE
),free
)(multi2
);
1184 /* This function is currently only used from isl_aff.c
1186 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
1187 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
1188 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
1189 __attribute__ ((unused
));
1191 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
1192 * return the result.
1194 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
1195 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
1196 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
1201 multi1
= FN(MULTI(BASE
),cow
)(multi1
);
1202 if (!multi1
|| !multi2
)
1205 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
1206 if (!isl_space_is_equal(multi1
->space
, multi2
->space
))
1207 isl_die(ctx
, isl_error_invalid
,
1208 "spaces don't match", goto error
);
1210 for (i
= 0; i
< multi1
->n
; ++i
) {
1211 multi1
->p
[i
] = fn(multi1
->p
[i
], FN(EL
,copy
)(multi2
->p
[i
]));
1216 FN(MULTI(BASE
),free
)(multi2
);
1219 FN(MULTI(BASE
),free
)(multi1
);
1220 FN(MULTI(BASE
),free
)(multi2
);
1224 /* Multiply the elements of "multi" by "v" and return the result.
1226 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_val
)(__isl_take
MULTI(BASE
) *multi
,
1227 __isl_take isl_val
*v
)
1234 if (isl_val_is_one(v
)) {
1239 if (!isl_val_is_rat(v
))
1240 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1241 "expecting rational factor", goto error
);
1243 multi
= FN(MULTI(BASE
),cow
)(multi
);
1247 for (i
= 0; i
< multi
->n
; ++i
) {
1248 multi
->p
[i
] = FN(EL
,scale_val
)(multi
->p
[i
], isl_val_copy(v
));
1257 return FN(MULTI(BASE
),free
)(multi
);
1260 /* Divide the elements of "multi" by "v" and return the result.
1262 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_down_val
)(
1263 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_val
*v
)
1270 if (isl_val_is_one(v
)) {
1275 if (!isl_val_is_rat(v
))
1276 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1277 "expecting rational factor", goto error
);
1278 if (isl_val_is_zero(v
))
1279 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1280 "cannot scale down by zero", goto error
);
1282 multi
= FN(MULTI(BASE
),cow
)(multi
);
1286 for (i
= 0; i
< multi
->n
; ++i
) {
1287 multi
->p
[i
] = FN(EL
,scale_down_val
)(multi
->p
[i
],
1297 return FN(MULTI(BASE
),free
)(multi
);
1300 /* Multiply the elements of "multi" by the corresponding element of "mv"
1301 * and return the result.
1303 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_multi_val
)(
1304 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1311 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1312 mv
->space
, isl_dim_set
))
1313 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1314 "spaces don't match", goto error
);
1316 multi
= FN(MULTI(BASE
),cow
)(multi
);
1320 for (i
= 0; i
< multi
->n
; ++i
) {
1323 v
= isl_multi_val_get_val(mv
, i
);
1324 multi
->p
[i
] = FN(EL
,scale_val
)(multi
->p
[i
], v
);
1329 isl_multi_val_free(mv
);
1332 isl_multi_val_free(mv
);
1333 return FN(MULTI(BASE
),free
)(multi
);
1336 /* Divide the elements of "multi" by the corresponding element of "mv"
1337 * and return the result.
1339 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_down_multi_val
)(
1340 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1347 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1348 mv
->space
, isl_dim_set
))
1349 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1350 "spaces don't match", goto error
);
1352 multi
= FN(MULTI(BASE
),cow
)(multi
);
1356 for (i
= 0; i
< multi
->n
; ++i
) {
1359 v
= isl_multi_val_get_val(mv
, i
);
1360 multi
->p
[i
] = FN(EL
,scale_down_val
)(multi
->p
[i
], v
);
1365 isl_multi_val_free(mv
);
1368 isl_multi_val_free(mv
);
1369 return FN(MULTI(BASE
),free
)(multi
);
1372 #ifndef NO_MOVE_DIMS
1373 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1374 * to dimensions of "dst_type" at "dst_pos".
1376 * We only support moving input dimensions to parameters and vice versa.
1378 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),move_dims
)(__isl_take
MULTI(BASE
) *multi
,
1379 enum isl_dim_type dst_type
, unsigned dst_pos
,
1380 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1388 !isl_space_is_named_or_nested(multi
->space
, src_type
) &&
1389 !isl_space_is_named_or_nested(multi
->space
, dst_type
))
1392 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
1393 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1394 "cannot move output/set dimension",
1395 return FN(MULTI(BASE
),free
)(multi
));
1396 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
1397 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1399 return FN(MULTI(BASE
),free
)(multi
));
1400 if (src_pos
+ n
> isl_space_dim(multi
->space
, src_type
))
1401 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1402 "range out of bounds",
1403 return FN(MULTI(BASE
),free
)(multi
));
1404 if (dst_type
== src_type
)
1405 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_unsupported
,
1406 "moving dims within the same type not supported",
1407 return FN(MULTI(BASE
),free
)(multi
));
1409 multi
= FN(MULTI(BASE
),cow
)(multi
);
1413 multi
->space
= isl_space_move_dims(multi
->space
, dst_type
, dst_pos
,
1414 src_type
, src_pos
, n
);
1416 return FN(MULTI(BASE
),free
)(multi
);
1418 for (i
= 0; i
< multi
->n
; ++i
) {
1419 multi
->p
[i
] = FN(EL
,move_dims
)(multi
->p
[i
], dst_type
, dst_pos
,
1420 src_type
, src_pos
, n
);
1422 return FN(MULTI(BASE
),free
)(multi
);
1429 /* Convert a multiple expression defined over a parameter domain
1430 * into one that is defined over a zero-dimensional set.
1432 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),from_range
)(
1433 __isl_take
MULTI(BASE
) *multi
)
1439 if (!isl_space_is_set(multi
->space
))
1440 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1441 "not living in a set space",
1442 return FN(MULTI(BASE
),free
)(multi
));
1444 space
= FN(MULTI(BASE
),get_space
)(multi
);
1445 space
= isl_space_from_range(space
);
1446 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
1451 /* Are "multi1" and "multi2" obviously equal?
1453 int FN(MULTI(BASE
),plain_is_equal
)(__isl_keep
MULTI(BASE
) *multi1
,
1454 __isl_keep
MULTI(BASE
) *multi2
)
1459 if (!multi1
|| !multi2
)
1461 if (multi1
->n
!= multi2
->n
)
1463 equal
= isl_space_is_equal(multi1
->space
, multi2
->space
);
1464 if (equal
< 0 || !equal
)
1467 for (i
= 0; i
< multi1
->n
; ++i
) {
1468 equal
= FN(EL
,plain_is_equal
)(multi1
->p
[i
], multi2
->p
[i
]);
1469 if (equal
< 0 || !equal
)
1477 /* Return the shared domain of the elements of "multi".
1479 __isl_give isl_set
*FN(MULTI(BASE
),domain
)(__isl_take
MULTI(BASE
) *multi
)
1487 dom
= isl_set_universe(FN(MULTI(BASE
),get_domain_space
)(multi
));
1488 for (i
= 0; i
< multi
->n
; ++i
) {
1491 dom_i
= FN(EL
,domain
)(FN(FN(MULTI(BASE
),get
),BASE
)(multi
, i
));
1492 dom
= isl_set_intersect(dom
, dom_i
);
1495 FN(MULTI(BASE
),free
)(multi
);