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 __isl_null
MULTI(BASE
) *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
);
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
);
433 /* Drop the id on the specified tuple.
435 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_tuple_id
)(
436 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
)
442 if (!FN(MULTI(BASE
),has_tuple_id
)(multi
, type
))
445 multi
= FN(MULTI(BASE
),cow
)(multi
);
449 space
= FN(MULTI(BASE
),get_space
)(multi
);
450 space
= isl_space_reset_tuple_id(space
, type
);
452 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
455 /* Reset the user pointer on all identifiers of parameters and tuples
456 * of the space of "multi".
458 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_user
)(
459 __isl_take
MULTI(BASE
) *multi
)
463 space
= FN(MULTI(BASE
),get_space
)(multi
);
464 space
= isl_space_reset_user(space
);
466 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
469 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),realign_domain
)(
470 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_reordering
*exp
)
474 multi
= FN(MULTI(BASE
),cow
)(multi
);
478 for (i
= 0; i
< multi
->n
; ++i
) {
479 multi
->p
[i
] = FN(EL
,realign_domain
)(multi
->p
[i
],
480 isl_reordering_copy(exp
));
485 multi
= FN(MULTI(BASE
),reset_domain_space
)(multi
,
486 isl_space_copy(exp
->dim
));
488 isl_reordering_free(exp
);
491 isl_reordering_free(exp
);
492 FN(MULTI(BASE
),free
)(multi
);
496 /* Align the parameters of "multi" to those of "model".
498 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params
)(
499 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*model
)
504 if (!multi
|| !model
)
507 if (isl_space_match(multi
->space
, isl_dim_param
,
508 model
, isl_dim_param
)) {
509 isl_space_free(model
);
513 ctx
= isl_space_get_ctx(model
);
514 if (!isl_space_has_named_params(model
))
515 isl_die(ctx
, isl_error_invalid
,
516 "model has unnamed parameters", goto error
);
517 if (!isl_space_has_named_params(multi
->space
))
518 isl_die(ctx
, isl_error_invalid
,
519 "input has unnamed parameters", goto error
);
521 model
= isl_space_params(model
);
522 exp
= isl_parameter_alignment_reordering(multi
->space
, model
);
523 exp
= isl_reordering_extend_space(exp
,
524 FN(MULTI(BASE
),get_domain_space
)(multi
));
525 multi
= FN(MULTI(BASE
),realign_domain
)(multi
, exp
);
527 isl_space_free(model
);
530 isl_space_free(model
);
531 FN(MULTI(BASE
),free
)(multi
);
535 #if !defined(NO_GIST) || !defined(NO_INTERSECT_DOMAIN)
536 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params_multi_set_and
)(
537 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*set
,
538 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi
,
539 __isl_take isl_set
*set
))
545 if (isl_space_match(multi
->space
, isl_dim_param
,
546 set
->dim
, isl_dim_param
))
547 return fn(multi
, set
);
548 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
549 if (!isl_space_has_named_params(multi
->space
) ||
550 !isl_space_has_named_params(set
->dim
))
551 isl_die(ctx
, isl_error_invalid
,
552 "unaligned unnamed parameters", goto error
);
553 multi
= FN(MULTI(BASE
),align_params
)(multi
, isl_set_get_space(set
));
554 set
= isl_set_align_params(set
, FN(MULTI(BASE
),get_space
)(multi
));
555 return fn(multi
, set
);
557 FN(MULTI(BASE
),free
)(multi
);
564 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist_aligned
)(
565 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*context
)
569 multi
= FN(MULTI(BASE
),cow
)(multi
);
570 if (!multi
|| !context
)
573 for (i
= 0; i
< multi
->n
; ++i
) {
574 multi
->p
[i
] = FN(EL
,gist
)(multi
->p
[i
], isl_set_copy(context
));
579 isl_set_free(context
);
582 isl_set_free(context
);
583 FN(MULTI(BASE
),free
)(multi
);
587 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist
)(__isl_take
MULTI(BASE
) *multi
,
588 __isl_take isl_set
*context
)
590 return FN(MULTI(BASE
),align_params_multi_set_and
)(multi
, context
,
591 &FN(MULTI(BASE
),gist_aligned
));
594 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist_params
)(
595 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*context
)
597 isl_space
*space
= FN(MULTI(BASE
),get_domain_space
)(multi
);
598 isl_set
*dom_context
= isl_set_universe(space
);
599 dom_context
= isl_set_intersect_params(dom_context
, context
);
600 return FN(MULTI(BASE
),gist
)(multi
, dom_context
);
604 #ifndef NO_INTERSECT_DOMAIN
605 /* Transform the domain of "multi" by combining it with "domain"
608 * The parameters of "multi" and "domain" are assumed to have been aligned.
610 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),intersect_aligned
)(
611 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*domain
,
612 __isl_give EL
*(*fn
)(EL
*el
, __isl_take isl_set
*set2
))
616 if (!multi
|| !domain
)
620 isl_set_free(domain
);
624 multi
= FN(MULTI(BASE
),cow
)(multi
);
628 for (i
= 0; i
< multi
->n
; ++i
) {
629 multi
->p
[i
] = fn(multi
->p
[i
], isl_set_copy(domain
));
634 isl_set_free(domain
);
637 isl_set_free(domain
);
638 FN(MULTI(BASE
),free
)(multi
);
642 /* Intersect the domain of "multi" with "domain".
644 * The parameters of "multi" and "domain" are assumed to have been aligned.
646 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),intersect_domain_aligned
)(
647 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*domain
)
649 return FN(MULTI(BASE
),intersect_aligned
)(multi
, domain
,
650 &FN(EL
,intersect_domain
));
653 /* Intersect the domain of "multi" with "domain".
655 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),intersect_domain
)(
656 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*domain
)
658 return FN(MULTI(BASE
),align_params_multi_set_and
)(multi
, domain
,
659 &FN(MULTI(BASE
),intersect_domain_aligned
));
662 /* Intersect the parameter domain of "multi" with "domain".
664 * The parameters of "multi" and "domain" are assumed to have been aligned.
666 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),intersect_params_aligned
)(
667 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*domain
)
669 return FN(MULTI(BASE
),intersect_aligned
)(multi
, domain
,
670 &FN(EL
,intersect_params
));
673 /* Intersect the parameter domain of "multi" with "domain".
675 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),intersect_params
)(
676 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*domain
)
678 return FN(MULTI(BASE
),align_params_multi_set_and
)(multi
, domain
,
679 &FN(MULTI(BASE
),intersect_params_aligned
));
683 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),LIST(BASE
))(
684 __isl_take isl_space
*space
, __isl_take
LIST(EL
) *list
)
694 ctx
= isl_space_get_ctx(space
);
695 n
= FN(FN(LIST(EL
),n
),BASE
)(list
);
696 if (n
!= isl_space_dim(space
, isl_dim_out
))
697 isl_die(ctx
, isl_error_invalid
,
698 "invalid number of elements in list", goto error
);
700 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
701 for (i
= 0; i
< n
; ++i
) {
702 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
703 FN(FN(LIST(EL
),get
),BASE
)(list
, i
));
706 isl_space_free(space
);
707 FN(LIST(EL
),free
)(list
);
710 isl_space_free(space
);
711 FN(LIST(EL
),free
)(list
);
716 /* Create a multi expression in the given space that maps each
717 * input dimension to the corresponding output dimension.
719 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),identity
)(__isl_take isl_space
*space
)
728 if (isl_space_is_set(space
))
729 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
730 "expecting map space", goto error
);
732 n
= isl_space_dim(space
, isl_dim_out
);
733 if (n
!= isl_space_dim(space
, isl_dim_in
))
734 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
735 "number of input and output dimensions needs to be "
736 "the same", goto error
);
738 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
741 isl_space_free(space
);
745 space
= isl_space_domain(space
);
746 ls
= isl_local_space_from_space(space
);
748 for (i
= 0; i
< n
; ++i
) {
750 el
= FN(EL
,var_on_domain
)(isl_local_space_copy(ls
),
752 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
, el
);
755 isl_local_space_free(ls
);
759 isl_space_free(space
);
764 /* Construct a multi expression in the given space with value zero in
765 * each of the output dimensions.
767 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),zero
)(__isl_take isl_space
*space
)
775 n
= isl_space_dim(space
, isl_dim_out
);
776 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
779 isl_space_free(space
);
785 space
= isl_space_domain(space
);
786 ls
= isl_local_space_from_space(space
);
787 el
= FN(EL
,zero_on_domain
)(ls
);
789 for (i
= 0; i
< n
; ++i
)
790 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
800 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),BASE
)(__isl_take EL
*el
)
804 multi
= FN(MULTI(BASE
),alloc
)(FN(EL
,get_space
)(el
));
805 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, 0, el
);
811 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),drop_dims
)(
812 __isl_take
MULTI(BASE
) *multi
,
813 enum isl_dim_type type
, unsigned first
, unsigned n
)
818 multi
= FN(MULTI(BASE
),cow
)(multi
);
822 dim
= FN(MULTI(BASE
),dim
)(multi
, type
);
823 if (first
+ n
> dim
|| first
+ n
< first
)
824 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
825 "index out of bounds",
826 return FN(MULTI(BASE
),cow
)(multi
));
828 multi
->space
= isl_space_drop_dims(multi
->space
, type
, first
, n
);
830 return FN(MULTI(BASE
),cow
)(multi
);
832 if (type
== isl_dim_out
) {
833 for (i
= 0; i
< n
; ++i
)
834 FN(EL
,free
)(multi
->p
[first
+ i
]);
835 for (i
= first
; i
+ n
< multi
->n
; ++i
)
836 multi
->p
[i
] = multi
->p
[i
+ n
];
842 for (i
= 0; i
< multi
->n
; ++i
) {
843 multi
->p
[i
] = FN(EL
,drop_dims
)(multi
->p
[i
], type
, first
, n
);
845 return FN(MULTI(BASE
),cow
)(multi
);
851 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
853 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params_multi_multi_and
)(
854 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
855 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi1
,
856 __isl_take
MULTI(BASE
) *multi2
))
860 if (!multi1
|| !multi2
)
862 if (isl_space_match(multi1
->space
, isl_dim_param
,
863 multi2
->space
, isl_dim_param
))
864 return fn(multi1
, multi2
);
865 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
866 if (!isl_space_has_named_params(multi1
->space
) ||
867 !isl_space_has_named_params(multi2
->space
))
868 isl_die(ctx
, isl_error_invalid
,
869 "unaligned unnamed parameters", goto error
);
870 multi1
= FN(MULTI(BASE
),align_params
)(multi1
,
871 FN(MULTI(BASE
),get_space
)(multi2
));
872 multi2
= FN(MULTI(BASE
),align_params
)(multi2
,
873 FN(MULTI(BASE
),get_space
)(multi1
));
874 return fn(multi1
, multi2
);
876 FN(MULTI(BASE
),free
)(multi1
);
877 FN(MULTI(BASE
),free
)(multi2
);
881 /* Given two MULTI(BASE)s A -> B and C -> D,
882 * construct a MULTI(BASE) (A * C) -> [B -> D].
884 * The parameters are assumed to have been aligned.
886 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product_aligned
)(
887 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
894 if (!multi1
|| !multi2
)
897 space
= isl_space_range_product(FN(MULTI(BASE
),get_space
)(multi1
),
898 FN(MULTI(BASE
),get_space
)(multi2
));
899 res
= FN(MULTI(BASE
),alloc
)(space
);
901 n1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
902 n2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
904 for (i
= 0; i
< n1
; ++i
) {
905 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
906 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
909 for (i
= 0; i
< n2
; ++i
) {
910 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
911 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, n1
+ i
, el
);
914 FN(MULTI(BASE
),free
)(multi1
);
915 FN(MULTI(BASE
),free
)(multi2
);
918 FN(MULTI(BASE
),free
)(multi1
);
919 FN(MULTI(BASE
),free
)(multi2
);
923 /* Given two MULTI(BASE)s A -> B and C -> D,
924 * construct a MULTI(BASE) (A * C) -> [B -> D].
926 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product
)(
927 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
929 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
930 &FN(MULTI(BASE
),range_product_aligned
));
933 /* Is the range of "multi" a wrapped relation?
935 int FN(MULTI(BASE
),range_is_wrapping
)(__isl_keep
MULTI(BASE
) *multi
)
939 return isl_space_range_is_wrapping(multi
->space
);
942 /* Given a function A -> [B -> C], extract the function A -> B.
944 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_domain
)(
945 __isl_take
MULTI(BASE
) *multi
)
952 if (!isl_space_range_is_wrapping(multi
->space
))
953 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
954 "range is not a product",
955 return FN(MULTI(BASE
),free
)(multi
));
957 space
= FN(MULTI(BASE
),get_space
)(multi
);
958 total
= isl_space_dim(space
, isl_dim_out
);
959 space
= isl_space_range_factor_domain(space
);
960 keep
= isl_space_dim(space
, isl_dim_out
);
961 multi
= FN(MULTI(BASE
),drop_dims
)(multi
,
962 isl_dim_out
, keep
, total
- keep
);
963 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
968 /* Given a function A -> [B -> C], extract the function A -> C.
970 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_range
)(
971 __isl_take
MULTI(BASE
) *multi
)
978 if (!isl_space_range_is_wrapping(multi
->space
))
979 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
980 "range is not a product",
981 return FN(MULTI(BASE
),free
)(multi
));
983 space
= FN(MULTI(BASE
),get_space
)(multi
);
984 total
= isl_space_dim(space
, isl_dim_out
);
985 space
= isl_space_range_factor_range(space
);
986 keep
= isl_space_dim(space
, isl_dim_out
);
987 multi
= FN(MULTI(BASE
),drop_dims
)(multi
, isl_dim_out
, 0, total
- keep
);
988 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
993 /* Given two MULTI(BASE)s A -> B and C -> D,
994 * construct a MULTI(BASE) [A -> C] -> [B -> D].
996 * The parameters are assumed to have been aligned.
998 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),product_aligned
)(
999 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
1005 int in1
, in2
, out1
, out2
;
1007 in1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_in
);
1008 in2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_in
);
1009 out1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
1010 out2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
1011 space
= isl_space_product(FN(MULTI(BASE
),get_space
)(multi1
),
1012 FN(MULTI(BASE
),get_space
)(multi2
));
1013 res
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
1014 space
= isl_space_domain(space
);
1016 for (i
= 0; i
< out1
; ++i
) {
1017 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
1018 el
= FN(EL
,insert_dims
)(el
, isl_dim_in
, in1
, in2
);
1019 el
= FN(EL
,reset_domain_space
)(el
, isl_space_copy(space
));
1020 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
1023 for (i
= 0; i
< out2
; ++i
) {
1024 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
1025 el
= FN(EL
,insert_dims
)(el
, isl_dim_in
, 0, in1
);
1026 el
= FN(EL
,reset_domain_space
)(el
, isl_space_copy(space
));
1027 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, out1
+ i
, el
);
1030 isl_space_free(space
);
1031 FN(MULTI(BASE
),free
)(multi1
);
1032 FN(MULTI(BASE
),free
)(multi2
);
1036 /* Given two MULTI(BASE)s A -> B and C -> D,
1037 * construct a MULTI(BASE) [A -> C] -> [B -> D].
1039 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),product
)(
1040 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
1042 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
1043 &FN(MULTI(BASE
),product_aligned
));
1046 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flatten_range
)(
1047 __isl_take
MULTI(BASE
) *multi
)
1052 if (!multi
->space
->nested
[1])
1055 multi
= FN(MULTI(BASE
),cow
)(multi
);
1059 multi
->space
= isl_space_flatten_range(multi
->space
);
1061 return FN(MULTI(BASE
),free
)(multi
);
1066 /* Given two MULTI(BASE)s A -> B and C -> D,
1067 * construct a MULTI(BASE) (A * C) -> (B, D).
1069 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flat_range_product
)(
1070 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
1074 multi
= FN(MULTI(BASE
),range_product
)(multi1
, multi2
);
1075 multi
= FN(MULTI(BASE
),flatten_range
)(multi
);
1079 /* Given two multi expressions, "multi1"
1083 * where B2 starts at position "pos", and "multi2"
1087 * return the multi expression
1091 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_splice
)(
1092 __isl_take
MULTI(BASE
) *multi1
, unsigned pos
,
1093 __isl_take
MULTI(BASE
) *multi2
)
1098 if (!multi1
|| !multi2
)
1101 dim
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
1103 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
1104 "index out of bounds", goto error
);
1106 res
= FN(MULTI(BASE
),copy
)(multi1
);
1107 res
= FN(MULTI(BASE
),drop_dims
)(res
, isl_dim_out
, pos
, dim
- pos
);
1108 multi1
= FN(MULTI(BASE
),drop_dims
)(multi1
, isl_dim_out
, 0, pos
);
1110 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi2
);
1111 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi1
);
1115 FN(MULTI(BASE
),free
)(multi1
);
1116 FN(MULTI(BASE
),free
)(multi2
);
1120 /* Given two multi expressions, "multi1"
1122 * [A1 A2] -> [B1 B2]
1124 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
1129 * return the multi expression
1131 * [A1 C A2] -> [B1 D B2]
1133 * We first insert input dimensions to obtain
1135 * [A1 C A2] -> [B1 B2]
1141 * and then apply range_splice.
1143 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),splice
)(
1144 __isl_take
MULTI(BASE
) *multi1
, unsigned in_pos
, unsigned out_pos
,
1145 __isl_take
MULTI(BASE
) *multi2
)
1150 if (!multi1
|| !multi2
)
1153 n_in1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_in
);
1155 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
1156 "index out of bounds", goto error
);
1158 n_in2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_in
);
1160 multi1
= FN(MULTI(BASE
),insert_dims
)(multi1
, isl_dim_in
, in_pos
, n_in2
);
1161 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, n_in2
,
1163 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, 0, in_pos
);
1165 return FN(MULTI(BASE
),range_splice
)(multi1
, out_pos
, multi2
);
1167 FN(MULTI(BASE
),free
)(multi1
);
1168 FN(MULTI(BASE
),free
)(multi2
);
1172 /* This function is currently only used from isl_aff.c
1174 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
1175 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
1176 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
1177 __attribute__ ((unused
));
1179 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
1180 * return the result.
1182 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
1183 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
1184 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
1189 multi1
= FN(MULTI(BASE
),cow
)(multi1
);
1190 if (!multi1
|| !multi2
)
1193 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
1194 if (!isl_space_is_equal(multi1
->space
, multi2
->space
))
1195 isl_die(ctx
, isl_error_invalid
,
1196 "spaces don't match", goto error
);
1198 for (i
= 0; i
< multi1
->n
; ++i
) {
1199 multi1
->p
[i
] = fn(multi1
->p
[i
], FN(EL
,copy
)(multi2
->p
[i
]));
1204 FN(MULTI(BASE
),free
)(multi2
);
1207 FN(MULTI(BASE
),free
)(multi1
);
1208 FN(MULTI(BASE
),free
)(multi2
);
1212 /* Multiply the elements of "multi" by "v" and return the result.
1214 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_val
)(__isl_take
MULTI(BASE
) *multi
,
1215 __isl_take isl_val
*v
)
1222 if (isl_val_is_one(v
)) {
1227 if (!isl_val_is_rat(v
))
1228 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1229 "expecting rational factor", goto error
);
1231 multi
= FN(MULTI(BASE
),cow
)(multi
);
1235 for (i
= 0; i
< multi
->n
; ++i
) {
1236 multi
->p
[i
] = FN(EL
,scale_val
)(multi
->p
[i
], isl_val_copy(v
));
1245 return FN(MULTI(BASE
),free
)(multi
);
1248 /* Multiply the elements of "multi" by the corresponding element of "mv"
1249 * and return the result.
1251 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_multi_val
)(
1252 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1259 if (!isl_space_tuple_match(multi
->space
, isl_dim_out
,
1260 mv
->space
, isl_dim_set
))
1261 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1262 "spaces don't match", goto error
);
1264 multi
= FN(MULTI(BASE
),cow
)(multi
);
1268 for (i
= 0; i
< multi
->n
; ++i
) {
1271 v
= isl_multi_val_get_val(mv
, i
);
1272 multi
->p
[i
] = FN(EL
,scale_val
)(multi
->p
[i
], v
);
1277 isl_multi_val_free(mv
);
1280 isl_multi_val_free(mv
);
1281 return FN(MULTI(BASE
),free
)(multi
);
1284 /* Divide the elements of "multi" by the corresponding element of "mv"
1285 * and return the result.
1287 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_down_multi_val
)(
1288 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1295 if (!isl_space_tuple_match(multi
->space
, isl_dim_out
,
1296 mv
->space
, isl_dim_set
))
1297 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1298 "spaces don't match", goto error
);
1300 multi
= FN(MULTI(BASE
),cow
)(multi
);
1304 for (i
= 0; i
< multi
->n
; ++i
) {
1307 v
= isl_multi_val_get_val(mv
, i
);
1308 multi
->p
[i
] = FN(EL
,scale_down_val
)(multi
->p
[i
], v
);
1313 isl_multi_val_free(mv
);
1316 isl_multi_val_free(mv
);
1317 return FN(MULTI(BASE
),free
)(multi
);
1320 #ifndef NO_MOVE_DIMS
1321 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1322 * to dimensions of "dst_type" at "dst_pos".
1324 * We only support moving input dimensions to parameters and vice versa.
1326 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),move_dims
)(__isl_take
MULTI(BASE
) *multi
,
1327 enum isl_dim_type dst_type
, unsigned dst_pos
,
1328 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1336 !isl_space_is_named_or_nested(multi
->space
, src_type
) &&
1337 !isl_space_is_named_or_nested(multi
->space
, dst_type
))
1340 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
1341 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1342 "cannot move output/set dimension",
1343 return FN(MULTI(BASE
),free
)(multi
));
1344 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
1345 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1347 return FN(MULTI(BASE
),free
)(multi
));
1348 if (src_pos
+ n
> isl_space_dim(multi
->space
, src_type
))
1349 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1350 "range out of bounds",
1351 return FN(MULTI(BASE
),free
)(multi
));
1352 if (dst_type
== src_type
)
1353 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_unsupported
,
1354 "moving dims within the same type not supported",
1355 return FN(MULTI(BASE
),free
)(multi
));
1357 multi
= FN(MULTI(BASE
),cow
)(multi
);
1361 multi
->space
= isl_space_move_dims(multi
->space
, dst_type
, dst_pos
,
1362 src_type
, src_pos
, n
);
1364 return FN(MULTI(BASE
),free
)(multi
);
1366 for (i
= 0; i
< multi
->n
; ++i
) {
1367 multi
->p
[i
] = FN(EL
,move_dims
)(multi
->p
[i
], dst_type
, dst_pos
,
1368 src_type
, src_pos
, n
);
1370 return FN(MULTI(BASE
),free
)(multi
);
1377 /* Convert a multiple expression defined over a parameter domain
1378 * into one that is defined over a zero-dimensional set.
1380 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),from_range
)(
1381 __isl_take
MULTI(BASE
) *multi
)
1387 if (!isl_space_is_set(multi
->space
))
1388 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1389 "not living in a set space",
1390 return FN(MULTI(BASE
),free
)(multi
));
1392 space
= FN(MULTI(BASE
),get_space
)(multi
);
1393 space
= isl_space_from_range(space
);
1394 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
1399 /* Are "multi1" and "multi2" obviously equal?
1401 int FN(MULTI(BASE
),plain_is_equal
)(__isl_keep
MULTI(BASE
) *multi1
,
1402 __isl_keep
MULTI(BASE
) *multi2
)
1407 if (!multi1
|| !multi2
)
1409 if (multi1
->n
!= multi2
->n
)
1411 equal
= isl_space_is_equal(multi1
->space
, multi2
->space
);
1412 if (equal
< 0 || !equal
)
1415 for (i
= 0; i
< multi1
->n
; ++i
) {
1416 equal
= FN(EL
,plain_is_equal
)(multi1
->p
[i
], multi2
->p
[i
]);
1417 if (equal
< 0 || !equal
)
1425 /* Return the shared domain of the elements of "multi".
1427 __isl_give isl_set
*FN(MULTI(BASE
),domain
)(__isl_take
MULTI(BASE
) *multi
)
1435 dom
= isl_set_universe(FN(MULTI(BASE
),get_domain_space
)(multi
));
1436 for (i
= 0; i
< multi
->n
; ++i
) {
1439 dom_i
= FN(EL
,domain
)(FN(FN(MULTI(BASE
),get
),BASE
)(multi
, i
));
1440 dom
= isl_set_intersect(dom
, dom_i
);
1443 FN(MULTI(BASE
),free
)(multi
);