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 __isl_give isl_space
*FN(MULTI(BASE
),get_domain_space
)(
38 __isl_keep
MULTI(BASE
) *multi
)
40 return multi
? isl_space_domain(isl_space_copy(multi
->space
)) : NULL
;
43 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),alloc
)(__isl_take isl_space
*space
)
52 ctx
= isl_space_get_ctx(space
);
53 n
= isl_space_dim(space
, isl_dim_out
);
54 multi
= isl_calloc(ctx
, MULTI(BASE
),
55 sizeof(MULTI(BASE
)) + (n
- 1) * sizeof(struct EL
*));
64 isl_space_free(space
);
68 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),dup
)(__isl_keep
MULTI(BASE
) *multi
)
76 dup
= FN(MULTI(BASE
),alloc
)(isl_space_copy(multi
->space
));
80 for (i
= 0; i
< multi
->n
; ++i
)
81 dup
= FN(FN(MULTI(BASE
),set
),BASE
)(dup
, i
,
82 FN(EL
,copy
)(multi
->p
[i
]));
87 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),cow
)(__isl_take
MULTI(BASE
) *multi
)
96 return FN(MULTI(BASE
),dup
)(multi
);
99 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),copy
)(__isl_keep
MULTI(BASE
) *multi
)
108 void *FN(MULTI(BASE
),free
)(__isl_take
MULTI(BASE
) *multi
)
115 if (--multi
->ref
> 0)
118 isl_space_free(multi
->space
);
119 for (i
= 0; i
< multi
->n
; ++i
)
120 FN(EL
,free
)(multi
->p
[i
]);
126 /* Check whether "multi" has non-zero coefficients for any dimension
127 * in the given range or if any of these dimensions appear
128 * with non-zero coefficients in any of the integer divisions involved.
130 int FN(MULTI(BASE
),involves_dims
)(__isl_keep
MULTI(BASE
) *multi
,
131 enum isl_dim_type type
, unsigned first
, unsigned n
)
137 if (multi
->n
== 0 || n
== 0)
140 for (i
= 0; i
< multi
->n
; ++i
) {
143 involves
= FN(EL
,involves_dims
)(multi
->p
[i
], type
, first
, n
);
144 if (involves
< 0 || involves
)
151 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),insert_dims
)(
152 __isl_take
MULTI(BASE
) *multi
,
153 enum isl_dim_type type
, unsigned first
, unsigned n
)
159 if (type
== isl_dim_out
)
160 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
161 "cannot insert output/set dimensions",
162 return FN(MULTI(BASE
),free
)(multi
));
163 if (n
== 0 && !isl_space_is_named_or_nested(multi
->space
, type
))
166 multi
= FN(MULTI(BASE
),cow
)(multi
);
170 multi
->space
= isl_space_insert_dims(multi
->space
, type
, first
, n
);
172 return FN(MULTI(BASE
),free
)(multi
);
174 for (i
= 0; i
< multi
->n
; ++i
) {
175 multi
->p
[i
] = FN(EL
,insert_dims
)(multi
->p
[i
], type
, first
, n
);
177 return FN(MULTI(BASE
),free
)(multi
);
183 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),add_dims
)(__isl_take
MULTI(BASE
) *multi
,
184 enum isl_dim_type type
, unsigned n
)
188 pos
= FN(MULTI(BASE
),dim
)(multi
, type
);
190 return FN(MULTI(BASE
),insert_dims
)(multi
, type
, pos
, n
);
193 unsigned FN(MULTI(BASE
),dim
)(__isl_keep
MULTI(BASE
) *multi
,
194 enum isl_dim_type type
)
196 return multi
? isl_space_dim(multi
->space
, type
) : 0;
199 /* Return the position of the first dimension of "type" with id "id".
200 * Return -1 if there is no such dimension.
202 int FN(MULTI(BASE
),find_dim_by_id
)(__isl_keep
MULTI(BASE
) *multi
,
203 enum isl_dim_type type
, __isl_keep isl_id
*id
)
207 return isl_space_find_dim_by_id(multi
->space
, type
, id
);
210 /* Return the id of the given dimension.
212 __isl_give isl_id
*FN(MULTI(BASE
),get_dim_id
)(__isl_keep
MULTI(BASE
) *multi
,
213 enum isl_dim_type type
, unsigned pos
)
215 return multi
? isl_space_get_dim_id(multi
->space
, type
, pos
) : NULL
;
218 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_name
)(
219 __isl_take
MULTI(BASE
) *multi
,
220 enum isl_dim_type type
, unsigned pos
, const char *s
)
224 multi
= FN(MULTI(BASE
),cow
)(multi
);
228 multi
->space
= isl_space_set_dim_name(multi
->space
, type
, pos
, s
);
230 return FN(MULTI(BASE
),free
)(multi
);
232 if (type
== isl_dim_out
)
234 for (i
= 0; i
< multi
->n
; ++i
) {
235 multi
->p
[i
] = FN(EL
,set_dim_name
)(multi
->p
[i
], type
, pos
, s
);
237 return FN(MULTI(BASE
),free
)(multi
);
243 const char *FN(MULTI(BASE
),get_tuple_name
)(__isl_keep
MULTI(BASE
) *multi
,
244 enum isl_dim_type type
)
246 return multi
? isl_space_get_tuple_name(multi
->space
, type
) : NULL
;
249 /* Does the specified tuple have an id?
251 int FN(MULTI(BASE
),has_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
252 enum isl_dim_type type
)
254 return multi
? isl_space_has_tuple_id(multi
->space
, type
) : -1;
257 /* Return the id of the specified tuple.
259 __isl_give isl_id
*FN(MULTI(BASE
),get_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
260 enum isl_dim_type type
)
262 return multi
? isl_space_get_tuple_id(multi
->space
, type
) : NULL
;
265 __isl_give EL
*FN(FN(MULTI(BASE
),get
),BASE
)(__isl_keep
MULTI(BASE
) *multi
,
272 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
273 if (pos
< 0 || pos
>= multi
->n
)
274 isl_die(ctx
, isl_error_invalid
,
275 "index out of bounds", return NULL
);
276 return FN(EL
,copy
)(multi
->p
[pos
]);
279 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),set
),BASE
)(
280 __isl_take
MULTI(BASE
) *multi
, int pos
, __isl_take EL
*el
)
282 isl_space
*multi_space
= NULL
;
283 isl_space
*el_space
= NULL
;
286 multi
= FN(MULTI(BASE
),cow
)(multi
);
290 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
291 match
= FN(EL
,matching_params
)(el
, multi_space
);
295 multi
= FN(MULTI(BASE
),align_params
)(multi
,
296 FN(EL
,get_space
)(el
));
297 isl_space_free(multi_space
);
298 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
299 el
= FN(EL
,align_params
)(el
, isl_space_copy(multi_space
));
301 if (FN(EL
,check_match_domain_space
)(el
, multi_space
) < 0)
304 if (pos
< 0 || pos
>= multi
->n
)
305 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
306 "index out of bounds", goto error
);
308 FN(EL
,free
)(multi
->p
[pos
]);
311 isl_space_free(multi_space
);
312 isl_space_free(el_space
);
316 FN(MULTI(BASE
),free
)(multi
);
318 isl_space_free(multi_space
);
319 isl_space_free(el_space
);
323 /* Reset the space of "multi". This function is called from isl_pw_templ.c
324 * and doesn't know if the space of an element object is represented
325 * directly or through its domain. It therefore passes along both,
326 * which we pass along to the element function since we don't how
327 * that is represented either.
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
->p
[i
] = FN(EL
,reset_domain_space
)(multi
->p
[i
],
341 isl_space_copy(domain
));
345 isl_space_free(domain
);
346 isl_space_free(multi
->space
);
347 multi
->space
= space
;
351 isl_space_free(domain
);
352 isl_space_free(space
);
353 FN(MULTI(BASE
),free
)(multi
);
357 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_domain_space
)(
358 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*domain
)
362 space
= isl_space_extend_domain_with_range(isl_space_copy(domain
),
363 isl_space_copy(multi
->space
));
364 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
367 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space
)(
368 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
)
372 domain
= isl_space_domain(isl_space_copy(space
));
373 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
376 /* Set the id of the given dimension of "multi" to "id".
378 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_id
)(
379 __isl_take
MULTI(BASE
) *multi
,
380 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
384 multi
= FN(MULTI(BASE
),cow
)(multi
);
388 space
= FN(MULTI(BASE
),get_space
)(multi
);
389 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
391 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
394 FN(MULTI(BASE
),free
)(multi
);
398 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_name
)(
399 __isl_keep
MULTI(BASE
) *multi
, enum isl_dim_type type
,
404 multi
= FN(MULTI(BASE
),cow
)(multi
);
408 space
= FN(MULTI(BASE
),get_space
)(multi
);
409 space
= isl_space_set_tuple_name(space
, type
, s
);
411 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
414 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_id
)(
415 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
,
416 __isl_take isl_id
*id
)
420 multi
= FN(MULTI(BASE
),cow
)(multi
);
422 return isl_id_free(id
);
424 space
= FN(MULTI(BASE
),get_space
)(multi
);
425 space
= isl_space_set_tuple_id(space
, type
, id
);
427 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
430 /* Drop the id on the specified tuple.
432 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_tuple_id
)(
433 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
)
439 if (!FN(MULTI(BASE
),has_tuple_id
)(multi
, type
))
442 multi
= FN(MULTI(BASE
),cow
)(multi
);
446 space
= FN(MULTI(BASE
),get_space
)(multi
);
447 space
= isl_space_reset_tuple_id(space
, type
);
449 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
452 /* Reset the user pointer on all identifiers of parameters and tuples
453 * of the space of "multi".
455 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_user
)(
456 __isl_take
MULTI(BASE
) *multi
)
460 space
= FN(MULTI(BASE
),get_space
)(multi
);
461 space
= isl_space_reset_user(space
);
463 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
466 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),realign_domain
)(
467 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_reordering
*exp
)
471 multi
= FN(MULTI(BASE
),cow
)(multi
);
475 for (i
= 0; i
< multi
->n
; ++i
) {
476 multi
->p
[i
] = FN(EL
,realign_domain
)(multi
->p
[i
],
477 isl_reordering_copy(exp
));
482 multi
= FN(MULTI(BASE
),reset_domain_space
)(multi
,
483 isl_space_copy(exp
->dim
));
485 isl_reordering_free(exp
);
488 isl_reordering_free(exp
);
489 FN(MULTI(BASE
),free
)(multi
);
493 /* Align the parameters of "multi" to those of "model".
495 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params
)(
496 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*model
)
501 if (!multi
|| !model
)
504 if (isl_space_match(multi
->space
, isl_dim_param
,
505 model
, isl_dim_param
)) {
506 isl_space_free(model
);
510 ctx
= isl_space_get_ctx(model
);
511 if (!isl_space_has_named_params(model
))
512 isl_die(ctx
, isl_error_invalid
,
513 "model has unnamed parameters", goto error
);
514 if (!isl_space_has_named_params(multi
->space
))
515 isl_die(ctx
, isl_error_invalid
,
516 "input has unnamed parameters", goto error
);
518 model
= isl_space_params(model
);
519 exp
= isl_parameter_alignment_reordering(multi
->space
, model
);
520 exp
= isl_reordering_extend_space(exp
,
521 FN(MULTI(BASE
),get_domain_space
)(multi
));
522 multi
= FN(MULTI(BASE
),realign_domain
)(multi
, exp
);
524 isl_space_free(model
);
527 isl_space_free(model
);
528 FN(MULTI(BASE
),free
)(multi
);
532 #if !defined(NO_GIST) || !defined(NO_INTERSECT_DOMAIN)
533 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params_multi_set_and
)(
534 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*set
,
535 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi
,
536 __isl_take isl_set
*set
))
542 if (isl_space_match(multi
->space
, isl_dim_param
,
543 set
->dim
, isl_dim_param
))
544 return fn(multi
, set
);
545 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
546 if (!isl_space_has_named_params(multi
->space
) ||
547 !isl_space_has_named_params(set
->dim
))
548 isl_die(ctx
, isl_error_invalid
,
549 "unaligned unnamed parameters", goto error
);
550 multi
= FN(MULTI(BASE
),align_params
)(multi
, isl_set_get_space(set
));
551 set
= isl_set_align_params(set
, FN(MULTI(BASE
),get_space
)(multi
));
552 return fn(multi
, set
);
554 FN(MULTI(BASE
),free
)(multi
);
561 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist_aligned
)(
562 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*context
)
566 multi
= FN(MULTI(BASE
),cow
)(multi
);
567 if (!multi
|| !context
)
570 for (i
= 0; i
< multi
->n
; ++i
) {
571 multi
->p
[i
] = FN(EL
,gist
)(multi
->p
[i
], isl_set_copy(context
));
576 isl_set_free(context
);
579 isl_set_free(context
);
580 FN(MULTI(BASE
),free
)(multi
);
584 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist
)(__isl_take
MULTI(BASE
) *multi
,
585 __isl_take isl_set
*context
)
587 return FN(MULTI(BASE
),align_params_multi_set_and
)(multi
, context
,
588 &FN(MULTI(BASE
),gist_aligned
));
591 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist_params
)(
592 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*context
)
594 isl_space
*space
= FN(MULTI(BASE
),get_domain_space
)(multi
);
595 isl_set
*dom_context
= isl_set_universe(space
);
596 dom_context
= isl_set_intersect_params(dom_context
, context
);
597 return FN(MULTI(BASE
),gist
)(multi
, dom_context
);
601 #ifndef NO_INTERSECT_DOMAIN
602 /* Transform the domain of "multi" by combining it with "domain"
605 * The parameters of "multi" and "domain" are assumed to have been aligned.
607 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),intersect_aligned
)(
608 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*domain
,
609 __isl_give EL
*(*fn
)(EL
*el
, __isl_take isl_set
*set2
))
613 if (!multi
|| !domain
)
617 isl_set_free(domain
);
621 multi
= FN(MULTI(BASE
),cow
)(multi
);
625 for (i
= 0; i
< multi
->n
; ++i
) {
626 multi
->p
[i
] = fn(multi
->p
[i
], isl_set_copy(domain
));
631 isl_set_free(domain
);
634 isl_set_free(domain
);
635 FN(MULTI(BASE
),free
)(multi
);
639 /* Intersect the domain of "multi" with "domain".
641 * The parameters of "multi" and "domain" are assumed to have been aligned.
643 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),intersect_domain_aligned
)(
644 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*domain
)
646 return FN(MULTI(BASE
),intersect_aligned
)(multi
, domain
,
647 &FN(EL
,intersect_domain
));
650 /* Intersect the domain of "multi" with "domain".
652 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),intersect_domain
)(
653 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*domain
)
655 return FN(MULTI(BASE
),align_params_multi_set_and
)(multi
, domain
,
656 &FN(MULTI(BASE
),intersect_domain_aligned
));
659 /* Intersect the parameter domain of "multi" with "domain".
661 * The parameters of "multi" and "domain" are assumed to have been aligned.
663 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),intersect_params_aligned
)(
664 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*domain
)
666 return FN(MULTI(BASE
),intersect_aligned
)(multi
, domain
,
667 &FN(EL
,intersect_params
));
670 /* Intersect the parameter domain of "multi" with "domain".
672 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),intersect_params
)(
673 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*domain
)
675 return FN(MULTI(BASE
),align_params_multi_set_and
)(multi
, domain
,
676 &FN(MULTI(BASE
),intersect_params_aligned
));
680 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),LIST(BASE
))(
681 __isl_take isl_space
*space
, __isl_take
LIST(EL
) *list
)
691 ctx
= isl_space_get_ctx(space
);
692 n
= FN(FN(LIST(EL
),n
),BASE
)(list
);
693 if (n
!= isl_space_dim(space
, isl_dim_out
))
694 isl_die(ctx
, isl_error_invalid
,
695 "invalid number of elements in list", goto error
);
697 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
698 for (i
= 0; i
< n
; ++i
) {
699 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
700 FN(FN(LIST(EL
),get
),BASE
)(list
, i
));
703 isl_space_free(space
);
704 FN(LIST(EL
),free
)(list
);
707 isl_space_free(space
);
708 FN(LIST(EL
),free
)(list
);
713 /* Create a multi expression in the given space that maps each
714 * input dimension to the corresponding output dimension.
716 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),identity
)(__isl_take isl_space
*space
)
725 if (isl_space_is_set(space
))
726 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
727 "expecting map space", goto error
);
729 n
= isl_space_dim(space
, isl_dim_out
);
730 if (n
!= isl_space_dim(space
, isl_dim_in
))
731 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
732 "number of input and output dimensions needs to be "
733 "the same", goto error
);
735 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
738 isl_space_free(space
);
742 space
= isl_space_domain(space
);
743 ls
= isl_local_space_from_space(space
);
745 for (i
= 0; i
< n
; ++i
) {
747 el
= FN(EL
,var_on_domain
)(isl_local_space_copy(ls
),
749 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
, el
);
752 isl_local_space_free(ls
);
756 isl_space_free(space
);
761 /* Construct a multi expression in the given space with value zero in
762 * each of the output dimensions.
764 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),zero
)(__isl_take isl_space
*space
)
772 n
= isl_space_dim(space
, isl_dim_out
);
773 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
776 isl_space_free(space
);
782 space
= isl_space_domain(space
);
783 ls
= isl_local_space_from_space(space
);
784 el
= FN(EL
,zero_on_domain
)(ls
);
786 for (i
= 0; i
< n
; ++i
)
787 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
797 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),BASE
)(__isl_take EL
*el
)
801 multi
= FN(MULTI(BASE
),alloc
)(FN(EL
,get_space
)(el
));
802 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, 0, el
);
808 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),drop_dims
)(
809 __isl_take
MULTI(BASE
) *multi
,
810 enum isl_dim_type type
, unsigned first
, unsigned n
)
815 multi
= FN(MULTI(BASE
),cow
)(multi
);
819 dim
= FN(MULTI(BASE
),dim
)(multi
, type
);
820 if (first
+ n
> dim
|| first
+ n
< first
)
821 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
822 "index out of bounds",
823 return FN(MULTI(BASE
),cow
)(multi
));
825 multi
->space
= isl_space_drop_dims(multi
->space
, type
, first
, n
);
827 return FN(MULTI(BASE
),cow
)(multi
);
829 if (type
== isl_dim_out
) {
830 for (i
= 0; i
< n
; ++i
)
831 FN(EL
,free
)(multi
->p
[first
+ i
]);
832 for (i
= first
; i
+ n
< multi
->n
; ++i
)
833 multi
->p
[i
] = multi
->p
[i
+ n
];
839 for (i
= 0; i
< multi
->n
; ++i
) {
840 multi
->p
[i
] = FN(EL
,drop_dims
)(multi
->p
[i
], type
, first
, n
);
842 return FN(MULTI(BASE
),cow
)(multi
);
848 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
850 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params_multi_multi_and
)(
851 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
852 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi1
,
853 __isl_take
MULTI(BASE
) *multi2
))
857 if (!multi1
|| !multi2
)
859 if (isl_space_match(multi1
->space
, isl_dim_param
,
860 multi2
->space
, isl_dim_param
))
861 return fn(multi1
, multi2
);
862 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
863 if (!isl_space_has_named_params(multi1
->space
) ||
864 !isl_space_has_named_params(multi2
->space
))
865 isl_die(ctx
, isl_error_invalid
,
866 "unaligned unnamed parameters", goto error
);
867 multi1
= FN(MULTI(BASE
),align_params
)(multi1
,
868 FN(MULTI(BASE
),get_space
)(multi2
));
869 multi2
= FN(MULTI(BASE
),align_params
)(multi2
,
870 FN(MULTI(BASE
),get_space
)(multi1
));
871 return fn(multi1
, multi2
);
873 FN(MULTI(BASE
),free
)(multi1
);
874 FN(MULTI(BASE
),free
)(multi2
);
878 /* Given two MULTI(BASE)s A -> B and C -> D,
879 * construct a MULTI(BASE) (A * C) -> [B -> D].
881 * The parameters are assumed to have been aligned.
883 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product_aligned
)(
884 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
891 if (!multi1
|| !multi2
)
894 space
= isl_space_range_product(FN(MULTI(BASE
),get_space
)(multi1
),
895 FN(MULTI(BASE
),get_space
)(multi2
));
896 res
= FN(MULTI(BASE
),alloc
)(space
);
898 n1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
899 n2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
901 for (i
= 0; i
< n1
; ++i
) {
902 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
903 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
906 for (i
= 0; i
< n2
; ++i
) {
907 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
908 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, n1
+ i
, el
);
911 FN(MULTI(BASE
),free
)(multi1
);
912 FN(MULTI(BASE
),free
)(multi2
);
915 FN(MULTI(BASE
),free
)(multi1
);
916 FN(MULTI(BASE
),free
)(multi2
);
920 /* Given two MULTI(BASE)s A -> B and C -> D,
921 * construct a MULTI(BASE) (A * C) -> [B -> D].
923 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product
)(
924 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
926 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
927 &FN(MULTI(BASE
),range_product_aligned
));
930 /* Is the range of "multi" a wrapped relation?
932 int FN(MULTI(BASE
),range_is_wrapping
)(__isl_keep
MULTI(BASE
) *multi
)
936 return isl_space_range_is_wrapping(multi
->space
);
939 /* Given a function A -> [B -> C], extract the function A -> B.
941 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_domain
)(
942 __isl_take
MULTI(BASE
) *multi
)
949 if (!isl_space_range_is_wrapping(multi
->space
))
950 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
951 "range is not a product",
952 return FN(MULTI(BASE
),free
)(multi
));
954 space
= FN(MULTI(BASE
),get_space
)(multi
);
955 total
= isl_space_dim(space
, isl_dim_out
);
956 space
= isl_space_range_factor_domain(space
);
957 keep
= isl_space_dim(space
, isl_dim_out
);
958 multi
= FN(MULTI(BASE
),drop_dims
)(multi
,
959 isl_dim_out
, keep
, total
- keep
);
960 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
965 /* Given a function A -> [B -> C], extract the function A -> C.
967 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_range
)(
968 __isl_take
MULTI(BASE
) *multi
)
975 if (!isl_space_range_is_wrapping(multi
->space
))
976 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
977 "range is not a product",
978 return FN(MULTI(BASE
),free
)(multi
));
980 space
= FN(MULTI(BASE
),get_space
)(multi
);
981 total
= isl_space_dim(space
, isl_dim_out
);
982 space
= isl_space_range_factor_range(space
);
983 keep
= isl_space_dim(space
, isl_dim_out
);
984 multi
= FN(MULTI(BASE
),drop_dims
)(multi
, isl_dim_out
, 0, total
- keep
);
985 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
990 /* Given two MULTI(BASE)s A -> B and C -> D,
991 * construct a MULTI(BASE) [A -> C] -> [B -> D].
993 * The parameters are assumed to have been aligned.
995 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),product_aligned
)(
996 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
1002 int in1
, in2
, out1
, out2
;
1004 in1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_in
);
1005 in2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_in
);
1006 out1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
1007 out2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
1008 space
= isl_space_product(FN(MULTI(BASE
),get_space
)(multi1
),
1009 FN(MULTI(BASE
),get_space
)(multi2
));
1010 res
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
1011 space
= isl_space_domain(space
);
1013 for (i
= 0; i
< out1
; ++i
) {
1014 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
1015 el
= FN(EL
,insert_dims
)(el
, isl_dim_in
, in1
, in2
);
1016 el
= FN(EL
,reset_domain_space
)(el
, isl_space_copy(space
));
1017 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
1020 for (i
= 0; i
< out2
; ++i
) {
1021 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
1022 el
= FN(EL
,insert_dims
)(el
, isl_dim_in
, 0, in1
);
1023 el
= FN(EL
,reset_domain_space
)(el
, isl_space_copy(space
));
1024 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, out1
+ i
, el
);
1027 isl_space_free(space
);
1028 FN(MULTI(BASE
),free
)(multi1
);
1029 FN(MULTI(BASE
),free
)(multi2
);
1033 /* Given two MULTI(BASE)s A -> B and C -> D,
1034 * construct a MULTI(BASE) [A -> C] -> [B -> D].
1036 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),product
)(
1037 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
1039 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
1040 &FN(MULTI(BASE
),product_aligned
));
1043 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flatten_range
)(
1044 __isl_take
MULTI(BASE
) *multi
)
1049 if (!multi
->space
->nested
[1])
1052 multi
= FN(MULTI(BASE
),cow
)(multi
);
1056 multi
->space
= isl_space_flatten_range(multi
->space
);
1058 return FN(MULTI(BASE
),free
)(multi
);
1063 /* Given two MULTI(BASE)s A -> B and C -> D,
1064 * construct a MULTI(BASE) (A * C) -> (B, D).
1066 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flat_range_product
)(
1067 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
1071 multi
= FN(MULTI(BASE
),range_product
)(multi1
, multi2
);
1072 multi
= FN(MULTI(BASE
),flatten_range
)(multi
);
1076 /* Given two multi expressions, "multi1"
1080 * where B2 starts at position "pos", and "multi2"
1084 * return the multi expression
1088 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_splice
)(
1089 __isl_take
MULTI(BASE
) *multi1
, unsigned pos
,
1090 __isl_take
MULTI(BASE
) *multi2
)
1095 if (!multi1
|| !multi2
)
1098 dim
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
1100 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
1101 "index out of bounds", goto error
);
1103 res
= FN(MULTI(BASE
),copy
)(multi1
);
1104 res
= FN(MULTI(BASE
),drop_dims
)(res
, isl_dim_out
, pos
, dim
- pos
);
1105 multi1
= FN(MULTI(BASE
),drop_dims
)(multi1
, isl_dim_out
, 0, pos
);
1107 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi2
);
1108 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi1
);
1112 FN(MULTI(BASE
),free
)(multi1
);
1113 FN(MULTI(BASE
),free
)(multi2
);
1117 /* Given two multi expressions, "multi1"
1119 * [A1 A2] -> [B1 B2]
1121 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
1126 * return the multi expression
1128 * [A1 C A2] -> [B1 D B2]
1130 * We first insert input dimensions to obtain
1132 * [A1 C A2] -> [B1 B2]
1138 * and then apply range_splice.
1140 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),splice
)(
1141 __isl_take
MULTI(BASE
) *multi1
, unsigned in_pos
, unsigned out_pos
,
1142 __isl_take
MULTI(BASE
) *multi2
)
1147 if (!multi1
|| !multi2
)
1150 n_in1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_in
);
1152 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
1153 "index out of bounds", goto error
);
1155 n_in2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_in
);
1157 multi1
= FN(MULTI(BASE
),insert_dims
)(multi1
, isl_dim_in
, in_pos
, n_in2
);
1158 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, n_in2
,
1160 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, 0, in_pos
);
1162 return FN(MULTI(BASE
),range_splice
)(multi1
, out_pos
, multi2
);
1164 FN(MULTI(BASE
),free
)(multi1
);
1165 FN(MULTI(BASE
),free
)(multi2
);
1169 /* This function is currently only used from isl_aff.c
1171 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
1172 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
1173 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
1174 __attribute__ ((unused
));
1176 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
1177 * return the result.
1179 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
1180 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
1181 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
1186 multi1
= FN(MULTI(BASE
),cow
)(multi1
);
1187 if (!multi1
|| !multi2
)
1190 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
1191 if (!isl_space_is_equal(multi1
->space
, multi2
->space
))
1192 isl_die(ctx
, isl_error_invalid
,
1193 "spaces don't match", goto error
);
1195 for (i
= 0; i
< multi1
->n
; ++i
) {
1196 multi1
->p
[i
] = fn(multi1
->p
[i
], FN(EL
,copy
)(multi2
->p
[i
]));
1201 FN(MULTI(BASE
),free
)(multi2
);
1204 FN(MULTI(BASE
),free
)(multi1
);
1205 FN(MULTI(BASE
),free
)(multi2
);
1209 /* Multiply the elements of "multi" by "v" and return the result.
1211 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_val
)(__isl_take
MULTI(BASE
) *multi
,
1212 __isl_take isl_val
*v
)
1219 if (isl_val_is_one(v
)) {
1224 if (!isl_val_is_rat(v
))
1225 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1226 "expecting rational factor", goto error
);
1228 multi
= FN(MULTI(BASE
),cow
)(multi
);
1232 for (i
= 0; i
< multi
->n
; ++i
) {
1233 multi
->p
[i
] = FN(EL
,scale_val
)(multi
->p
[i
], isl_val_copy(v
));
1242 return FN(MULTI(BASE
),free
)(multi
);
1245 /* Multiply the elements of "multi" by the corresponding element of "mv"
1246 * and return the result.
1248 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_multi_val
)(
1249 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1256 if (!isl_space_tuple_match(multi
->space
, isl_dim_out
,
1257 mv
->space
, isl_dim_set
))
1258 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1259 "spaces don't match", goto error
);
1261 multi
= FN(MULTI(BASE
),cow
)(multi
);
1265 for (i
= 0; i
< multi
->n
; ++i
) {
1268 v
= isl_multi_val_get_val(mv
, i
);
1269 multi
->p
[i
] = FN(EL
,scale_val
)(multi
->p
[i
], v
);
1274 isl_multi_val_free(mv
);
1277 isl_multi_val_free(mv
);
1278 return FN(MULTI(BASE
),free
)(multi
);
1281 /* Divide the elements of "multi" by the corresponding element of "mv"
1282 * and return the result.
1284 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_down_multi_val
)(
1285 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1292 if (!isl_space_tuple_match(multi
->space
, isl_dim_out
,
1293 mv
->space
, isl_dim_set
))
1294 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1295 "spaces don't match", goto error
);
1297 multi
= FN(MULTI(BASE
),cow
)(multi
);
1301 for (i
= 0; i
< multi
->n
; ++i
) {
1304 v
= isl_multi_val_get_val(mv
, i
);
1305 multi
->p
[i
] = FN(EL
,scale_down_val
)(multi
->p
[i
], v
);
1310 isl_multi_val_free(mv
);
1313 isl_multi_val_free(mv
);
1314 return FN(MULTI(BASE
),free
)(multi
);
1317 #ifndef NO_MOVE_DIMS
1318 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1319 * to dimensions of "dst_type" at "dst_pos".
1321 * We only support moving input dimensions to parameters and vice versa.
1323 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),move_dims
)(__isl_take
MULTI(BASE
) *multi
,
1324 enum isl_dim_type dst_type
, unsigned dst_pos
,
1325 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1333 !isl_space_is_named_or_nested(multi
->space
, src_type
) &&
1334 !isl_space_is_named_or_nested(multi
->space
, dst_type
))
1337 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
1338 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1339 "cannot move output/set dimension",
1340 return FN(MULTI(BASE
),free
)(multi
));
1341 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
1342 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1344 return FN(MULTI(BASE
),free
)(multi
));
1345 if (src_pos
+ n
> isl_space_dim(multi
->space
, src_type
))
1346 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1347 "range out of bounds",
1348 return FN(MULTI(BASE
),free
)(multi
));
1349 if (dst_type
== src_type
)
1350 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_unsupported
,
1351 "moving dims within the same type not supported",
1352 return FN(MULTI(BASE
),free
)(multi
));
1354 multi
= FN(MULTI(BASE
),cow
)(multi
);
1358 multi
->space
= isl_space_move_dims(multi
->space
, dst_type
, dst_pos
,
1359 src_type
, src_pos
, n
);
1361 return FN(MULTI(BASE
),free
)(multi
);
1363 for (i
= 0; i
< multi
->n
; ++i
) {
1364 multi
->p
[i
] = FN(EL
,move_dims
)(multi
->p
[i
], dst_type
, dst_pos
,
1365 src_type
, src_pos
, n
);
1367 return FN(MULTI(BASE
),free
)(multi
);
1374 /* Convert a multiple expression defined over a parameter domain
1375 * into one that is defined over a zero-dimensional set.
1377 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),from_range
)(
1378 __isl_take
MULTI(BASE
) *multi
)
1384 if (!isl_space_is_set(multi
->space
))
1385 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1386 "not living in a set space",
1387 return FN(MULTI(BASE
),free
)(multi
));
1389 space
= FN(MULTI(BASE
),get_space
)(multi
);
1390 space
= isl_space_from_range(space
);
1391 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
1396 /* Are "multi1" and "multi2" obviously equal?
1398 int FN(MULTI(BASE
),plain_is_equal
)(__isl_keep
MULTI(BASE
) *multi1
,
1399 __isl_keep
MULTI(BASE
) *multi2
)
1404 if (!multi1
|| !multi2
)
1406 if (multi1
->n
!= multi2
->n
)
1408 equal
= isl_space_is_equal(multi1
->space
, multi2
->space
);
1409 if (equal
< 0 || !equal
)
1412 for (i
= 0; i
< multi1
->n
; ++i
) {
1413 equal
= FN(EL
,plain_is_equal
)(multi1
->p
[i
], multi2
->p
[i
]);
1414 if (equal
< 0 || !equal
)
1422 /* Return the shared domain of the elements of "multi".
1424 __isl_give isl_set
*FN(MULTI(BASE
),domain
)(__isl_take
MULTI(BASE
) *multi
)
1432 dom
= isl_set_universe(FN(MULTI(BASE
),get_domain_space
)(multi
));
1433 for (i
= 0; i
< multi
->n
; ++i
) {
1436 dom_i
= FN(EL
,domain
)(FN(FN(MULTI(BASE
),get
),BASE
)(multi
, i
));
1437 dom
= isl_set_intersect(dom
, dom_i
);
1440 FN(MULTI(BASE
),free
)(multi
);