2 * Copyright 2011 Sven Verdoolaege
3 * Copyright 2012 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 #define xCAT(A,B) A ## B
12 #define CAT(A,B) xCAT(A,B)
14 #define EL CAT(isl_,BASE)
15 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
16 #define FN(TYPE,NAME) xFN(TYPE,NAME)
17 #define xMULTI(BASE) isl_multi_ ## BASE
18 #define MULTI(BASE) xMULTI(BASE)
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 __isl_give isl_space
*FN(MULTI(BASE
),get_space
)(__isl_keep
MULTI(BASE
) *multi
)
30 return multi
? isl_space_copy(multi
->space
) : NULL
;
33 __isl_give isl_space
*FN(MULTI(BASE
),get_domain_space
)(
34 __isl_keep
MULTI(BASE
) *multi
)
36 return multi
? isl_space_domain(isl_space_copy(multi
->space
)) : NULL
;
39 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),alloc
)(__isl_take isl_space
*space
)
48 ctx
= isl_space_get_ctx(space
);
49 n
= isl_space_dim(space
, isl_dim_out
);
50 multi
= isl_calloc(ctx
, MULTI(BASE
),
51 sizeof(MULTI(BASE
)) + (n
- 1) * sizeof(struct EL
*));
60 isl_space_free(space
);
64 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),dup
)(__isl_keep
MULTI(BASE
) *multi
)
72 dup
= FN(MULTI(BASE
),alloc
)(isl_space_copy(multi
->space
));
76 for (i
= 0; i
< multi
->n
; ++i
)
77 dup
= FN(FN(MULTI(BASE
),set
),BASE
)(dup
, i
,
78 FN(EL
,copy
)(multi
->p
[i
]));
83 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),cow
)(__isl_take
MULTI(BASE
) *multi
)
92 return FN(MULTI(BASE
),dup
)(multi
);
95 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),copy
)(__isl_keep
MULTI(BASE
) *multi
)
104 void *FN(MULTI(BASE
),free
)(__isl_take
MULTI(BASE
) *multi
)
111 if (--multi
->ref
> 0)
114 isl_space_free(multi
->space
);
115 for (i
= 0; i
< multi
->n
; ++i
)
116 FN(EL
,free
)(multi
->p
[i
]);
122 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),insert_dims
)(
123 __isl_take
MULTI(BASE
) *multi
,
124 enum isl_dim_type type
, unsigned first
, unsigned n
)
130 if (type
== isl_dim_out
)
131 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
132 "cannot insert output/set dimensions",
133 return FN(MULTI(BASE
),free
)(multi
));
134 if (n
== 0 && !isl_space_is_named_or_nested(multi
->space
, type
))
137 multi
= FN(MULTI(BASE
),cow
)(multi
);
141 multi
->space
= isl_space_insert_dims(multi
->space
, type
, first
, n
);
143 return FN(MULTI(BASE
),free
)(multi
);
145 for (i
= 0; i
< multi
->n
; ++i
) {
146 multi
->p
[i
] = FN(EL
,insert_dims
)(multi
->p
[i
], type
, first
, n
);
148 return FN(MULTI(BASE
),free
)(multi
);
154 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),add_dims
)(__isl_take
MULTI(BASE
) *multi
,
155 enum isl_dim_type type
, unsigned n
)
159 pos
= FN(MULTI(BASE
),dim
)(multi
, type
);
161 return FN(MULTI(BASE
),insert_dims
)(multi
, type
, pos
, n
);
164 unsigned FN(MULTI(BASE
),dim
)(__isl_keep
MULTI(BASE
) *multi
,
165 enum isl_dim_type type
)
167 return multi
? isl_space_dim(multi
->space
, type
) : 0;
170 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_name
)(
171 __isl_take
MULTI(BASE
) *multi
,
172 enum isl_dim_type type
, unsigned pos
, const char *s
)
176 multi
= FN(MULTI(BASE
),cow
)(multi
);
180 multi
->space
= isl_space_set_dim_name(multi
->space
, type
, pos
, s
);
182 return FN(MULTI(BASE
),free
)(multi
);
184 if (type
== isl_dim_out
)
186 for (i
= 0; i
< multi
->n
; ++i
) {
187 multi
->p
[i
] = FN(EL
,set_dim_name
)(multi
->p
[i
], type
, pos
, s
);
189 return FN(MULTI(BASE
),free
)(multi
);
195 const char *FN(MULTI(BASE
),get_tuple_name
)(__isl_keep
MULTI(BASE
) *multi
,
196 enum isl_dim_type type
)
198 return multi
? isl_space_get_tuple_name(multi
->space
, type
) : NULL
;
201 __isl_give EL
*FN(FN(MULTI(BASE
),get
),BASE
)(__isl_keep
MULTI(BASE
) *multi
,
208 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
209 if (pos
< 0 || pos
>= multi
->n
)
210 isl_die(ctx
, isl_error_invalid
,
211 "index out of bounds", return NULL
);
212 return FN(EL
,copy
)(multi
->p
[pos
]);
215 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),set
),BASE
)(
216 __isl_take
MULTI(BASE
) *multi
, int pos
, __isl_take EL
*el
)
218 isl_space
*multi_space
= NULL
;
219 isl_space
*el_space
= NULL
;
221 multi
= FN(MULTI(BASE
),cow
)(multi
);
225 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
226 el_space
= FN(EL
,get_space
)(el
);
228 if (!isl_space_match(multi_space
, isl_dim_param
,
229 el_space
, isl_dim_param
))
230 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
231 "parameters don't match", goto error
);
232 if (!isl_space_tuple_match(multi_space
, isl_dim_in
,
233 el_space
, isl_dim_in
))
234 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
235 "domains don't match", goto error
);
237 if (pos
< 0 || pos
>= multi
->n
)
238 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
239 "index out of bounds", goto error
);
241 FN(EL
,free
)(multi
->p
[pos
]);
244 isl_space_free(multi_space
);
245 isl_space_free(el_space
);
249 FN(MULTI(BASE
),free
)(multi
);
251 isl_space_free(multi_space
);
252 isl_space_free(el_space
);
256 /* Reset the space of "multi". This function is called from isl_pw_templ.c
257 * and doesn't know if the space of an element object is represented
258 * directly or through its domain. It therefore passes along both,
259 * which we pass along to the element function since we don't how
260 * that is represented either.
262 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space_and_domain
)(
263 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
,
264 __isl_take isl_space
*domain
)
268 multi
= FN(MULTI(BASE
),cow
)(multi
);
269 if (!multi
|| !space
|| !domain
)
272 for (i
= 0; i
< multi
->n
; ++i
) {
273 multi
->p
[i
] = FN(EL
,reset_domain_space
)(multi
->p
[i
],
274 isl_space_copy(domain
));
278 isl_space_free(domain
);
279 isl_space_free(multi
->space
);
280 multi
->space
= space
;
284 isl_space_free(domain
);
285 isl_space_free(space
);
286 FN(MULTI(BASE
),free
)(multi
);
290 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_domain_space
)(
291 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*domain
)
295 space
= isl_space_extend_domain_with_range(isl_space_copy(domain
),
296 isl_space_copy(multi
->space
));
297 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
300 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space
)(
301 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
)
305 domain
= isl_space_domain(isl_space_copy(space
));
306 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
309 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_name
)(
310 __isl_keep
MULTI(BASE
) *multi
, enum isl_dim_type type
,
315 multi
= FN(MULTI(BASE
),cow
)(multi
);
319 space
= FN(MULTI(BASE
),get_space
)(multi
);
320 space
= isl_space_set_tuple_name(space
, type
, s
);
322 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
325 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_id
)(
326 __isl_keep
MULTI(BASE
) *multi
, enum isl_dim_type type
,
327 __isl_take isl_id
*id
)
331 multi
= FN(MULTI(BASE
),cow
)(multi
);
333 return isl_id_free(id
);
335 space
= FN(MULTI(BASE
),get_space
)(multi
);
336 space
= isl_space_set_tuple_id(space
, type
, id
);
338 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
341 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),realign_domain
)(
342 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_reordering
*exp
)
346 multi
= FN(MULTI(BASE
),cow
)(multi
);
350 for (i
= 0; i
< multi
->n
; ++i
) {
351 multi
->p
[i
] = FN(EL
,realign_domain
)(multi
->p
[i
],
352 isl_reordering_copy(exp
));
357 multi
= FN(MULTI(BASE
),reset_domain_space
)(multi
,
358 isl_space_copy(exp
->dim
));
360 isl_reordering_free(exp
);
363 isl_reordering_free(exp
);
364 FN(MULTI(BASE
),free
)(multi
);
368 /* Align the parameters of "multi" to those of "model".
370 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params
)(
371 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*model
)
375 if (!multi
|| !model
)
378 ctx
= isl_space_get_ctx(model
);
379 if (!isl_space_has_named_params(model
))
380 isl_die(ctx
, isl_error_invalid
,
381 "model has unnamed parameters", goto error
);
382 if (!isl_space_has_named_params(multi
->space
))
383 isl_die(ctx
, isl_error_invalid
,
384 "input has unnamed parameters", goto error
);
385 if (!isl_space_match(multi
->space
, isl_dim_param
,
386 model
, isl_dim_param
)) {
389 model
= isl_space_params(model
);
390 exp
= isl_parameter_alignment_reordering(multi
->space
, model
);
391 exp
= isl_reordering_extend_space(exp
,
392 FN(MULTI(BASE
),get_domain_space
)(multi
));
393 multi
= FN(MULTI(BASE
),realign_domain
)(multi
, exp
);
396 isl_space_free(model
);
399 isl_space_free(model
);
400 FN(MULTI(BASE
),free
)(multi
);
404 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params_multi_set_and
)(
405 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*set
,
406 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi
,
407 __isl_take isl_set
*set
))
413 if (isl_space_match(multi
->space
, isl_dim_param
,
414 set
->dim
, isl_dim_param
))
415 return fn(multi
, set
);
416 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
417 if (!isl_space_has_named_params(multi
->space
) ||
418 !isl_space_has_named_params(set
->dim
))
419 isl_die(ctx
, isl_error_invalid
,
420 "unaligned unnamed parameters", goto error
);
421 multi
= FN(MULTI(BASE
),align_params
)(multi
, isl_set_get_space(set
));
422 set
= isl_set_align_params(set
, FN(MULTI(BASE
),get_space
)(multi
));
423 return fn(multi
, set
);
425 FN(MULTI(BASE
),free
)(multi
);
430 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist_aligned
)(
431 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*context
)
435 if (!multi
|| !context
)
438 for (i
= 0; i
< multi
->n
; ++i
) {
439 multi
->p
[i
] = FN(EL
,gist
)(multi
->p
[i
], isl_set_copy(context
));
444 isl_set_free(context
);
447 isl_set_free(context
);
448 FN(MULTI(BASE
),free
)(multi
);
452 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist
)(__isl_take
MULTI(BASE
) *multi
,
453 __isl_take isl_set
*context
)
455 return FN(MULTI(BASE
),align_params_multi_set_and
)(multi
, context
,
456 &FN(MULTI(BASE
),gist_aligned
));
459 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist_params
)(
460 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*context
)
462 isl_space
*space
= FN(MULTI(BASE
),get_domain_space
)(multi
);
463 isl_set
*dom_context
= isl_set_universe(space
);
464 dom_context
= isl_set_intersect_params(dom_context
, context
);
465 return FN(MULTI(BASE
),gist
)(multi
, dom_context
);
468 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),LIST(BASE
))(
469 __isl_take isl_space
*space
, __isl_take
LIST(EL
) *list
)
479 ctx
= isl_space_get_ctx(space
);
480 n
= FN(FN(LIST(EL
),n
),BASE
)(list
);
481 if (n
!= isl_space_dim(space
, isl_dim_out
))
482 isl_die(ctx
, isl_error_invalid
,
483 "invalid number of elements in list", goto error
);
485 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
486 for (i
= 0; i
< n
; ++i
) {
487 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
488 FN(FN(LIST(EL
),get
),BASE
)(list
, i
));
491 isl_space_free(space
);
492 FN(LIST(EL
),free
)(list
);
495 isl_space_free(space
);
496 FN(LIST(EL
),free
)(list
);
500 /* Create a multi expression in the given space that maps each
501 * input dimension to the corresponding output dimension.
503 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),identity
)(__isl_take isl_space
*space
)
512 if (isl_space_is_set(space
))
513 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
514 "expecting map space", goto error
);
516 n
= isl_space_dim(space
, isl_dim_out
);
517 if (n
!= isl_space_dim(space
, isl_dim_in
))
518 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
519 "number of input and output dimensions needs to be "
520 "the same", goto error
);
522 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
525 isl_space_free(space
);
529 space
= isl_space_domain(space
);
530 ls
= isl_local_space_from_space(space
);
532 for (i
= 0; i
< n
; ++i
) {
534 el
= FN(EL
,var_on_domain
)(isl_local_space_copy(ls
),
536 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
, el
);
539 isl_local_space_free(ls
);
543 isl_space_free(space
);
547 /* Construct a multi expression in the given space with value zero in
548 * each of the output dimensions.
550 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),zero
)(__isl_take isl_space
*space
)
558 n
= isl_space_dim(space
, isl_dim_out
);
559 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
562 isl_space_free(space
);
568 space
= isl_space_domain(space
);
569 ls
= isl_local_space_from_space(space
);
570 el
= FN(EL
,zero_on_domain
)(ls
);
572 for (i
= 0; i
< n
; ++i
)
573 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
582 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),BASE
)(__isl_take EL
*el
)
586 multi
= FN(MULTI(BASE
),alloc
)(FN(EL
,get_space
)(el
));
587 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, 0, el
);
592 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),drop_dims
)(
593 __isl_take
MULTI(BASE
) *multi
,
594 enum isl_dim_type type
, unsigned first
, unsigned n
)
599 multi
= FN(MULTI(BASE
),cow
)(multi
);
603 dim
= FN(MULTI(BASE
),dim
)(multi
, type
);
604 if (first
+ n
> dim
|| first
+ n
< first
)
605 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
606 "index out of bounds",
607 return FN(MULTI(BASE
),cow
)(multi
));
609 multi
->space
= isl_space_drop_dims(multi
->space
, type
, first
, n
);
611 return FN(MULTI(BASE
),cow
)(multi
);
613 if (type
== isl_dim_out
) {
614 for (i
= 0; i
< n
; ++i
)
615 FN(EL
,free
)(multi
->p
[first
+ i
]);
616 for (i
= first
; i
+ n
< multi
->n
; ++i
)
617 multi
->p
[i
] = multi
->p
[i
+ n
];
623 for (i
= 0; i
< multi
->n
; ++i
) {
624 multi
->p
[i
] = FN(EL
,drop_dims
)(multi
->p
[i
], type
, first
, n
);
626 return FN(MULTI(BASE
),cow
)(multi
);
632 /* Given two MULTI(BASE)s A -> B and C -> D,
633 * construct a MULTI(BASE) (A * C) -> (B, D).
635 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product
)(
636 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
643 if (!multi1
|| !multi2
)
646 space
= isl_space_range_product(FN(MULTI(BASE
),get_space
)(multi1
),
647 FN(MULTI(BASE
),get_space
)(multi2
));
648 res
= FN(MULTI(BASE
),alloc
)(space
);
650 n1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
651 n2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
653 for (i
= 0; i
< n1
; ++i
) {
654 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
655 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
658 for (i
= 0; i
< n2
; ++i
) {
659 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
660 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, n1
+ i
, el
);
663 FN(MULTI(BASE
),free
)(multi1
);
664 FN(MULTI(BASE
),free
)(multi2
);
667 FN(MULTI(BASE
),free
)(multi1
);
668 FN(MULTI(BASE
),free
)(multi2
);
672 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flatten_range
)(
673 __isl_take
MULTI(BASE
) *multi
)
678 if (!multi
->space
->nested
[1])
681 multi
= FN(MULTI(BASE
),cow
)(multi
);
685 multi
->space
= isl_space_flatten_range(multi
->space
);
687 return FN(MULTI(BASE
),free
)(multi
);
692 /* Given two MULTI(BASE)s A -> B and C -> D,
693 * construct a MULTI(BASE) (A * C) -> [B -> D].
695 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flat_range_product
)(
696 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
700 multi
= FN(MULTI(BASE
),range_product
)(multi1
, multi2
);
701 multi
= FN(MULTI(BASE
),flatten_range
)(multi
);
705 /* Given two multi expressions, "multi1"
709 * where B2 starts at position "pos", and "multi2"
713 * return the multi expression
717 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_splice
)(
718 __isl_take
MULTI(BASE
) *multi1
, unsigned pos
,
719 __isl_take
MULTI(BASE
) *multi2
)
724 if (!multi1
|| !multi2
)
727 dim
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
729 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
730 "index out of bounds", goto error
);
732 res
= FN(MULTI(BASE
),copy
)(multi1
);
733 res
= FN(MULTI(BASE
),drop_dims
)(res
, isl_dim_out
, pos
, dim
- pos
);
734 multi1
= FN(MULTI(BASE
),drop_dims
)(multi1
, isl_dim_out
, 0, pos
);
736 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi2
);
737 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi1
);
741 FN(MULTI(BASE
),free
)(multi1
);
742 FN(MULTI(BASE
),free
)(multi2
);
746 /* Given two multi expressions, "multi1"
750 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
755 * return the multi expression
757 * [A1 C A2] -> [B1 D B2]
759 * We first insert input dimensions to obtain
761 * [A1 C A2] -> [B1 B2]
767 * and then apply range_splice.
769 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),splice
)(
770 __isl_take
MULTI(BASE
) *multi1
, unsigned in_pos
, unsigned out_pos
,
771 __isl_take
MULTI(BASE
) *multi2
)
776 if (!multi1
|| !multi2
)
779 n_in1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_in
);
781 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
782 "index out of bounds", goto error
);
784 n_in2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_in
);
786 multi1
= FN(MULTI(BASE
),insert_dims
)(multi1
, isl_dim_in
, in_pos
, n_in2
);
787 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, n_in2
,
789 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, 0, in_pos
);
791 return FN(MULTI(BASE
),range_splice
)(multi1
, out_pos
, multi2
);
793 FN(MULTI(BASE
),free
)(multi1
);
794 FN(MULTI(BASE
),free
)(multi2
);