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 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),insert_dims
)(
127 __isl_take
MULTI(BASE
) *multi
,
128 enum isl_dim_type type
, unsigned first
, unsigned n
)
134 if (type
== isl_dim_out
)
135 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
136 "cannot insert output/set dimensions",
137 return FN(MULTI(BASE
),free
)(multi
));
138 if (n
== 0 && !isl_space_is_named_or_nested(multi
->space
, type
))
141 multi
= FN(MULTI(BASE
),cow
)(multi
);
145 multi
->space
= isl_space_insert_dims(multi
->space
, type
, first
, n
);
147 return FN(MULTI(BASE
),free
)(multi
);
149 for (i
= 0; i
< multi
->n
; ++i
) {
150 multi
->p
[i
] = FN(EL
,insert_dims
)(multi
->p
[i
], type
, first
, n
);
152 return FN(MULTI(BASE
),free
)(multi
);
158 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),add_dims
)(__isl_take
MULTI(BASE
) *multi
,
159 enum isl_dim_type type
, unsigned n
)
163 pos
= FN(MULTI(BASE
),dim
)(multi
, type
);
165 return FN(MULTI(BASE
),insert_dims
)(multi
, type
, pos
, n
);
168 unsigned FN(MULTI(BASE
),dim
)(__isl_keep
MULTI(BASE
) *multi
,
169 enum isl_dim_type type
)
171 return multi
? isl_space_dim(multi
->space
, type
) : 0;
174 /* Return the position of the first dimension of "type" with id "id".
175 * Return -1 if there is no such dimension.
177 int FN(MULTI(BASE
),find_dim_by_id
)(__isl_keep
MULTI(BASE
) *multi
,
178 enum isl_dim_type type
, __isl_keep isl_id
*id
)
182 return isl_space_find_dim_by_id(multi
->space
, type
, id
);
185 /* Return the id of the given dimension.
187 __isl_give isl_id
*FN(MULTI(BASE
),get_dim_id
)(__isl_keep
MULTI(BASE
) *multi
,
188 enum isl_dim_type type
, unsigned pos
)
190 return multi
? isl_space_get_dim_id(multi
->space
, type
, pos
) : NULL
;
193 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_name
)(
194 __isl_take
MULTI(BASE
) *multi
,
195 enum isl_dim_type type
, unsigned pos
, const char *s
)
199 multi
= FN(MULTI(BASE
),cow
)(multi
);
203 multi
->space
= isl_space_set_dim_name(multi
->space
, type
, pos
, s
);
205 return FN(MULTI(BASE
),free
)(multi
);
207 if (type
== isl_dim_out
)
209 for (i
= 0; i
< multi
->n
; ++i
) {
210 multi
->p
[i
] = FN(EL
,set_dim_name
)(multi
->p
[i
], type
, pos
, s
);
212 return FN(MULTI(BASE
),free
)(multi
);
218 const char *FN(MULTI(BASE
),get_tuple_name
)(__isl_keep
MULTI(BASE
) *multi
,
219 enum isl_dim_type type
)
221 return multi
? isl_space_get_tuple_name(multi
->space
, type
) : NULL
;
224 /* Does the specified tuple have an id?
226 int FN(MULTI(BASE
),has_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
227 enum isl_dim_type type
)
229 return multi
? isl_space_has_tuple_id(multi
->space
, type
) : -1;
232 /* Return the id of the specified tuple.
234 __isl_give isl_id
*FN(MULTI(BASE
),get_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
235 enum isl_dim_type type
)
237 return multi
? isl_space_get_tuple_id(multi
->space
, type
) : NULL
;
240 __isl_give EL
*FN(FN(MULTI(BASE
),get
),BASE
)(__isl_keep
MULTI(BASE
) *multi
,
247 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
248 if (pos
< 0 || pos
>= multi
->n
)
249 isl_die(ctx
, isl_error_invalid
,
250 "index out of bounds", return NULL
);
251 return FN(EL
,copy
)(multi
->p
[pos
]);
254 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),set
),BASE
)(
255 __isl_take
MULTI(BASE
) *multi
, int pos
, __isl_take EL
*el
)
257 isl_space
*multi_space
= NULL
;
258 isl_space
*el_space
= NULL
;
261 multi
= FN(MULTI(BASE
),cow
)(multi
);
265 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
266 match
= FN(EL
,matching_params
)(el
, multi_space
);
270 multi
= FN(MULTI(BASE
),align_params
)(multi
,
271 FN(EL
,get_space
)(el
));
272 isl_space_free(multi_space
);
273 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
274 el
= FN(EL
,align_params
)(el
, isl_space_copy(multi_space
));
276 if (FN(EL
,check_match_domain_space
)(el
, multi_space
) < 0)
279 if (pos
< 0 || pos
>= multi
->n
)
280 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
281 "index out of bounds", goto error
);
283 FN(EL
,free
)(multi
->p
[pos
]);
286 isl_space_free(multi_space
);
287 isl_space_free(el_space
);
291 FN(MULTI(BASE
),free
)(multi
);
293 isl_space_free(multi_space
);
294 isl_space_free(el_space
);
298 /* Reset the space of "multi". This function is called from isl_pw_templ.c
299 * and doesn't know if the space of an element object is represented
300 * directly or through its domain. It therefore passes along both,
301 * which we pass along to the element function since we don't how
302 * that is represented either.
304 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space_and_domain
)(
305 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
,
306 __isl_take isl_space
*domain
)
310 multi
= FN(MULTI(BASE
),cow
)(multi
);
311 if (!multi
|| !space
|| !domain
)
314 for (i
= 0; i
< multi
->n
; ++i
) {
315 multi
->p
[i
] = FN(EL
,reset_domain_space
)(multi
->p
[i
],
316 isl_space_copy(domain
));
320 isl_space_free(domain
);
321 isl_space_free(multi
->space
);
322 multi
->space
= space
;
326 isl_space_free(domain
);
327 isl_space_free(space
);
328 FN(MULTI(BASE
),free
)(multi
);
332 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_domain_space
)(
333 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*domain
)
337 space
= isl_space_extend_domain_with_range(isl_space_copy(domain
),
338 isl_space_copy(multi
->space
));
339 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
342 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space
)(
343 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
)
347 domain
= isl_space_domain(isl_space_copy(space
));
348 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
351 /* Set the id of the given dimension of "multi" to "id".
353 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_id
)(
354 __isl_take
MULTI(BASE
) *multi
,
355 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
359 multi
= FN(MULTI(BASE
),cow
)(multi
);
363 space
= FN(MULTI(BASE
),get_space
)(multi
);
364 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
366 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
369 FN(MULTI(BASE
),free
)(multi
);
373 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_name
)(
374 __isl_keep
MULTI(BASE
) *multi
, enum isl_dim_type type
,
379 multi
= FN(MULTI(BASE
),cow
)(multi
);
383 space
= FN(MULTI(BASE
),get_space
)(multi
);
384 space
= isl_space_set_tuple_name(space
, type
, s
);
386 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
389 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_id
)(
390 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
,
391 __isl_take isl_id
*id
)
395 multi
= FN(MULTI(BASE
),cow
)(multi
);
397 return isl_id_free(id
);
399 space
= FN(MULTI(BASE
),get_space
)(multi
);
400 space
= isl_space_set_tuple_id(space
, type
, id
);
402 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
405 /* Drop the id on the specified tuple.
407 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_tuple_id
)(
408 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
)
414 if (!FN(MULTI(BASE
),has_tuple_id
)(multi
, type
))
417 multi
= FN(MULTI(BASE
),cow
)(multi
);
421 space
= FN(MULTI(BASE
),get_space
)(multi
);
422 space
= isl_space_reset_tuple_id(space
, type
);
424 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
427 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),realign_domain
)(
428 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_reordering
*exp
)
432 multi
= FN(MULTI(BASE
),cow
)(multi
);
436 for (i
= 0; i
< multi
->n
; ++i
) {
437 multi
->p
[i
] = FN(EL
,realign_domain
)(multi
->p
[i
],
438 isl_reordering_copy(exp
));
443 multi
= FN(MULTI(BASE
),reset_domain_space
)(multi
,
444 isl_space_copy(exp
->dim
));
446 isl_reordering_free(exp
);
449 isl_reordering_free(exp
);
450 FN(MULTI(BASE
),free
)(multi
);
454 /* Align the parameters of "multi" to those of "model".
456 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params
)(
457 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*model
)
461 if (!multi
|| !model
)
464 ctx
= isl_space_get_ctx(model
);
465 if (!isl_space_has_named_params(model
))
466 isl_die(ctx
, isl_error_invalid
,
467 "model has unnamed parameters", goto error
);
468 if (!isl_space_has_named_params(multi
->space
))
469 isl_die(ctx
, isl_error_invalid
,
470 "input has unnamed parameters", goto error
);
471 if (!isl_space_match(multi
->space
, isl_dim_param
,
472 model
, isl_dim_param
)) {
475 model
= isl_space_params(model
);
476 exp
= isl_parameter_alignment_reordering(multi
->space
, model
);
477 exp
= isl_reordering_extend_space(exp
,
478 FN(MULTI(BASE
),get_domain_space
)(multi
));
479 multi
= FN(MULTI(BASE
),realign_domain
)(multi
, exp
);
482 isl_space_free(model
);
485 isl_space_free(model
);
486 FN(MULTI(BASE
),free
)(multi
);
490 #if !defined(NO_GIST) || !defined(NO_INTERSECT_DOMAIN)
491 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params_multi_set_and
)(
492 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*set
,
493 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi
,
494 __isl_take isl_set
*set
))
500 if (isl_space_match(multi
->space
, isl_dim_param
,
501 set
->dim
, isl_dim_param
))
502 return fn(multi
, set
);
503 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
504 if (!isl_space_has_named_params(multi
->space
) ||
505 !isl_space_has_named_params(set
->dim
))
506 isl_die(ctx
, isl_error_invalid
,
507 "unaligned unnamed parameters", goto error
);
508 multi
= FN(MULTI(BASE
),align_params
)(multi
, isl_set_get_space(set
));
509 set
= isl_set_align_params(set
, FN(MULTI(BASE
),get_space
)(multi
));
510 return fn(multi
, set
);
512 FN(MULTI(BASE
),free
)(multi
);
519 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist_aligned
)(
520 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*context
)
524 multi
= FN(MULTI(BASE
),cow
)(multi
);
525 if (!multi
|| !context
)
528 for (i
= 0; i
< multi
->n
; ++i
) {
529 multi
->p
[i
] = FN(EL
,gist
)(multi
->p
[i
], isl_set_copy(context
));
534 isl_set_free(context
);
537 isl_set_free(context
);
538 FN(MULTI(BASE
),free
)(multi
);
542 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist
)(__isl_take
MULTI(BASE
) *multi
,
543 __isl_take isl_set
*context
)
545 return FN(MULTI(BASE
),align_params_multi_set_and
)(multi
, context
,
546 &FN(MULTI(BASE
),gist_aligned
));
549 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist_params
)(
550 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*context
)
552 isl_space
*space
= FN(MULTI(BASE
),get_domain_space
)(multi
);
553 isl_set
*dom_context
= isl_set_universe(space
);
554 dom_context
= isl_set_intersect_params(dom_context
, context
);
555 return FN(MULTI(BASE
),gist
)(multi
, dom_context
);
559 #ifndef NO_INTERSECT_DOMAIN
560 /* Intersect the domain of "multi" with "domain".
562 * The parameters of "multi" and "domain" are assumed to have been aligned.
564 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),intersect_domain_aligned
)(
565 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*domain
)
569 multi
= FN(MULTI(BASE
),cow
)(multi
);
570 if (!multi
|| !domain
)
573 for (i
= 0; i
< multi
->n
; ++i
) {
574 multi
->p
[i
] = FN(EL
,intersect_domain
)(multi
->p
[i
],
575 isl_set_copy(domain
));
580 isl_set_free(domain
);
583 isl_set_free(domain
);
584 FN(MULTI(BASE
),free
)(multi
);
588 /* Intersect the domain of "multi" with "domain".
590 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),intersect_domain
)(
591 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*domain
)
593 return FN(MULTI(BASE
),align_params_multi_set_and
)(multi
, domain
,
594 &FN(MULTI(BASE
),intersect_domain_aligned
));
598 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),LIST(BASE
))(
599 __isl_take isl_space
*space
, __isl_take
LIST(EL
) *list
)
609 ctx
= isl_space_get_ctx(space
);
610 n
= FN(FN(LIST(EL
),n
),BASE
)(list
);
611 if (n
!= isl_space_dim(space
, isl_dim_out
))
612 isl_die(ctx
, isl_error_invalid
,
613 "invalid number of elements in list", goto error
);
615 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
616 for (i
= 0; i
< n
; ++i
) {
617 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
618 FN(FN(LIST(EL
),get
),BASE
)(list
, i
));
621 isl_space_free(space
);
622 FN(LIST(EL
),free
)(list
);
625 isl_space_free(space
);
626 FN(LIST(EL
),free
)(list
);
631 /* Create a multi expression in the given space that maps each
632 * input dimension to the corresponding output dimension.
634 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),identity
)(__isl_take isl_space
*space
)
643 if (isl_space_is_set(space
))
644 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
645 "expecting map space", goto error
);
647 n
= isl_space_dim(space
, isl_dim_out
);
648 if (n
!= isl_space_dim(space
, isl_dim_in
))
649 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
650 "number of input and output dimensions needs to be "
651 "the same", goto error
);
653 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
656 isl_space_free(space
);
660 space
= isl_space_domain(space
);
661 ls
= isl_local_space_from_space(space
);
663 for (i
= 0; i
< n
; ++i
) {
665 el
= FN(EL
,var_on_domain
)(isl_local_space_copy(ls
),
667 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
, el
);
670 isl_local_space_free(ls
);
674 isl_space_free(space
);
679 /* Construct a multi expression in the given space with value zero in
680 * each of the output dimensions.
682 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),zero
)(__isl_take isl_space
*space
)
690 n
= isl_space_dim(space
, isl_dim_out
);
691 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
694 isl_space_free(space
);
700 space
= isl_space_domain(space
);
701 ls
= isl_local_space_from_space(space
);
702 el
= FN(EL
,zero_on_domain
)(ls
);
704 for (i
= 0; i
< n
; ++i
)
705 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
715 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),BASE
)(__isl_take EL
*el
)
719 multi
= FN(MULTI(BASE
),alloc
)(FN(EL
,get_space
)(el
));
720 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, 0, el
);
726 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),drop_dims
)(
727 __isl_take
MULTI(BASE
) *multi
,
728 enum isl_dim_type type
, unsigned first
, unsigned n
)
733 multi
= FN(MULTI(BASE
),cow
)(multi
);
737 dim
= FN(MULTI(BASE
),dim
)(multi
, type
);
738 if (first
+ n
> dim
|| first
+ n
< first
)
739 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
740 "index out of bounds",
741 return FN(MULTI(BASE
),cow
)(multi
));
743 multi
->space
= isl_space_drop_dims(multi
->space
, type
, first
, n
);
745 return FN(MULTI(BASE
),cow
)(multi
);
747 if (type
== isl_dim_out
) {
748 for (i
= 0; i
< n
; ++i
)
749 FN(EL
,free
)(multi
->p
[first
+ i
]);
750 for (i
= first
; i
+ n
< multi
->n
; ++i
)
751 multi
->p
[i
] = multi
->p
[i
+ n
];
757 for (i
= 0; i
< multi
->n
; ++i
) {
758 multi
->p
[i
] = FN(EL
,drop_dims
)(multi
->p
[i
], type
, first
, n
);
760 return FN(MULTI(BASE
),cow
)(multi
);
766 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
768 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params_multi_multi_and
)(
769 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
770 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi1
,
771 __isl_take
MULTI(BASE
) *multi2
))
775 if (!multi1
|| !multi2
)
777 if (isl_space_match(multi1
->space
, isl_dim_param
,
778 multi2
->space
, isl_dim_param
))
779 return fn(multi1
, multi2
);
780 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
781 if (!isl_space_has_named_params(multi1
->space
) ||
782 !isl_space_has_named_params(multi2
->space
))
783 isl_die(ctx
, isl_error_invalid
,
784 "unaligned unnamed parameters", goto error
);
785 multi1
= FN(MULTI(BASE
),align_params
)(multi1
,
786 FN(MULTI(BASE
),get_space
)(multi2
));
787 multi2
= FN(MULTI(BASE
),align_params
)(multi2
,
788 FN(MULTI(BASE
),get_space
)(multi1
));
789 return fn(multi1
, multi2
);
791 FN(MULTI(BASE
),free
)(multi1
);
792 FN(MULTI(BASE
),free
)(multi2
);
796 /* Given two MULTI(BASE)s A -> B and C -> D,
797 * construct a MULTI(BASE) (A * C) -> (B, D).
799 * The parameters are assumed to have been aligned.
801 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product_aligned
)(
802 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
809 if (!multi1
|| !multi2
)
812 space
= isl_space_range_product(FN(MULTI(BASE
),get_space
)(multi1
),
813 FN(MULTI(BASE
),get_space
)(multi2
));
814 res
= FN(MULTI(BASE
),alloc
)(space
);
816 n1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
817 n2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
819 for (i
= 0; i
< n1
; ++i
) {
820 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
821 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
824 for (i
= 0; i
< n2
; ++i
) {
825 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
826 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, n1
+ i
, el
);
829 FN(MULTI(BASE
),free
)(multi1
);
830 FN(MULTI(BASE
),free
)(multi2
);
833 FN(MULTI(BASE
),free
)(multi1
);
834 FN(MULTI(BASE
),free
)(multi2
);
838 /* Given two MULTI(BASE)s A -> B and C -> D,
839 * construct a MULTI(BASE) (A * C) -> (B, D).
841 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product
)(
842 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
844 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
845 &FN(MULTI(BASE
),range_product_aligned
));
848 /* Given two MULTI(BASE)s A -> B and C -> D,
849 * construct a MULTI(BASE) [A -> C] -> [B -> D].
851 * The parameters are assumed to have been aligned.
853 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),product_aligned
)(
854 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
860 int in1
, in2
, out1
, out2
;
862 in1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_in
);
863 in2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_in
);
864 out1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
865 out2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
866 space
= isl_space_product(FN(MULTI(BASE
),get_space
)(multi1
),
867 FN(MULTI(BASE
),get_space
)(multi2
));
868 res
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
869 space
= isl_space_domain(space
);
871 for (i
= 0; i
< out1
; ++i
) {
872 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
873 el
= FN(EL
,insert_dims
)(el
, isl_dim_in
, in1
, in2
);
874 el
= FN(EL
,reset_domain_space
)(el
, isl_space_copy(space
));
875 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
878 for (i
= 0; i
< out2
; ++i
) {
879 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
880 el
= FN(EL
,insert_dims
)(el
, isl_dim_in
, 0, in1
);
881 el
= FN(EL
,reset_domain_space
)(el
, isl_space_copy(space
));
882 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, out1
+ i
, el
);
885 isl_space_free(space
);
886 FN(MULTI(BASE
),free
)(multi1
);
887 FN(MULTI(BASE
),free
)(multi2
);
891 /* Given two MULTI(BASE)s A -> B and C -> D,
892 * construct a MULTI(BASE) [A -> C] -> [B -> D].
894 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),product
)(
895 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
897 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
898 &FN(MULTI(BASE
),product_aligned
));
901 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flatten_range
)(
902 __isl_take
MULTI(BASE
) *multi
)
907 if (!multi
->space
->nested
[1])
910 multi
= FN(MULTI(BASE
),cow
)(multi
);
914 multi
->space
= isl_space_flatten_range(multi
->space
);
916 return FN(MULTI(BASE
),free
)(multi
);
921 /* Given two MULTI(BASE)s A -> B and C -> D,
922 * construct a MULTI(BASE) (A * C) -> [B -> D].
924 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flat_range_product
)(
925 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
929 multi
= FN(MULTI(BASE
),range_product
)(multi1
, multi2
);
930 multi
= FN(MULTI(BASE
),flatten_range
)(multi
);
934 /* Given two multi expressions, "multi1"
938 * where B2 starts at position "pos", and "multi2"
942 * return the multi expression
946 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_splice
)(
947 __isl_take
MULTI(BASE
) *multi1
, unsigned pos
,
948 __isl_take
MULTI(BASE
) *multi2
)
953 if (!multi1
|| !multi2
)
956 dim
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
958 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
959 "index out of bounds", goto error
);
961 res
= FN(MULTI(BASE
),copy
)(multi1
);
962 res
= FN(MULTI(BASE
),drop_dims
)(res
, isl_dim_out
, pos
, dim
- pos
);
963 multi1
= FN(MULTI(BASE
),drop_dims
)(multi1
, isl_dim_out
, 0, pos
);
965 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi2
);
966 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi1
);
970 FN(MULTI(BASE
),free
)(multi1
);
971 FN(MULTI(BASE
),free
)(multi2
);
975 /* Given two multi expressions, "multi1"
979 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
984 * return the multi expression
986 * [A1 C A2] -> [B1 D B2]
988 * We first insert input dimensions to obtain
990 * [A1 C A2] -> [B1 B2]
996 * and then apply range_splice.
998 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),splice
)(
999 __isl_take
MULTI(BASE
) *multi1
, unsigned in_pos
, unsigned out_pos
,
1000 __isl_take
MULTI(BASE
) *multi2
)
1005 if (!multi1
|| !multi2
)
1008 n_in1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_in
);
1010 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
1011 "index out of bounds", goto error
);
1013 n_in2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_in
);
1015 multi1
= FN(MULTI(BASE
),insert_dims
)(multi1
, isl_dim_in
, in_pos
, n_in2
);
1016 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, n_in2
,
1018 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, 0, in_pos
);
1020 return FN(MULTI(BASE
),range_splice
)(multi1
, out_pos
, multi2
);
1022 FN(MULTI(BASE
),free
)(multi1
);
1023 FN(MULTI(BASE
),free
)(multi2
);
1027 /* This function is currently only used from isl_aff.c
1029 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
1030 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
1031 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
1032 __attribute__ ((unused
));
1034 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
1035 * return the result.
1037 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
1038 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
1039 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
1044 multi1
= FN(MULTI(BASE
),cow
)(multi1
);
1045 if (!multi1
|| !multi2
)
1048 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
1049 if (!isl_space_is_equal(multi1
->space
, multi2
->space
))
1050 isl_die(ctx
, isl_error_invalid
,
1051 "spaces don't match", goto error
);
1053 for (i
= 0; i
< multi1
->n
; ++i
) {
1054 multi1
->p
[i
] = fn(multi1
->p
[i
], FN(EL
,copy
)(multi2
->p
[i
]));
1059 FN(MULTI(BASE
),free
)(multi2
);
1062 FN(MULTI(BASE
),free
)(multi1
);
1063 FN(MULTI(BASE
),free
)(multi2
);
1067 /* Multiply the elements of "multi" by "v" and return the result.
1069 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_val
)(__isl_take
MULTI(BASE
) *multi
,
1070 __isl_take isl_val
*v
)
1077 if (isl_val_is_one(v
)) {
1082 if (!isl_val_is_rat(v
))
1083 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1084 "expecting rational factor", goto error
);
1086 multi
= FN(MULTI(BASE
),cow
)(multi
);
1090 for (i
= 0; i
< multi
->n
; ++i
) {
1091 multi
->p
[i
] = FN(EL
,scale_val
)(multi
->p
[i
], isl_val_copy(v
));
1100 return FN(MULTI(BASE
),free
)(multi
);
1103 /* Multiply the elements of "multi" by the corresponding element of "mv"
1104 * and return the result.
1106 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_multi_val
)(
1107 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1114 if (!isl_space_tuple_match(multi
->space
, isl_dim_out
,
1115 mv
->space
, isl_dim_set
))
1116 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1117 "spaces don't match", goto error
);
1119 multi
= FN(MULTI(BASE
),cow
)(multi
);
1123 for (i
= 0; i
< multi
->n
; ++i
) {
1126 v
= isl_multi_val_get_val(mv
, i
);
1127 multi
->p
[i
] = FN(EL
,scale_val
)(multi
->p
[i
], v
);
1132 isl_multi_val_free(mv
);
1135 isl_multi_val_free(mv
);
1136 return FN(MULTI(BASE
),free
)(multi
);
1139 /* Divide the elements of "multi" by the corresponding element of "mv"
1140 * and return the result.
1142 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_down_multi_val
)(
1143 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1150 if (!isl_space_tuple_match(multi
->space
, isl_dim_out
,
1151 mv
->space
, isl_dim_set
))
1152 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1153 "spaces don't match", goto error
);
1155 multi
= FN(MULTI(BASE
),cow
)(multi
);
1159 for (i
= 0; i
< multi
->n
; ++i
) {
1162 v
= isl_multi_val_get_val(mv
, i
);
1163 multi
->p
[i
] = FN(EL
,scale_down_val
)(multi
->p
[i
], v
);
1168 isl_multi_val_free(mv
);
1171 isl_multi_val_free(mv
);
1172 return FN(MULTI(BASE
),free
)(multi
);
1175 #ifndef NO_MOVE_DIMS
1176 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1177 * to dimensions of "dst_type" at "dst_pos".
1179 * We only support moving input dimensions to parameters and vice versa.
1181 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),move_dims
)(__isl_take
MULTI(BASE
) *multi
,
1182 enum isl_dim_type dst_type
, unsigned dst_pos
,
1183 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1191 !isl_space_is_named_or_nested(multi
->space
, src_type
) &&
1192 !isl_space_is_named_or_nested(multi
->space
, dst_type
))
1195 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
1196 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1197 "cannot move output/set dimension",
1198 return FN(MULTI(BASE
),free
)(multi
));
1199 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
1200 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1202 return FN(MULTI(BASE
),free
)(multi
));
1203 if (src_pos
+ n
> isl_space_dim(multi
->space
, src_type
))
1204 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1205 "range out of bounds",
1206 return FN(MULTI(BASE
),free
)(multi
));
1207 if (dst_type
== src_type
)
1208 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_unsupported
,
1209 "moving dims within the same type not supported",
1210 return FN(MULTI(BASE
),free
)(multi
));
1212 multi
= FN(MULTI(BASE
),cow
)(multi
);
1216 multi
->space
= isl_space_move_dims(multi
->space
, dst_type
, dst_pos
,
1217 src_type
, src_pos
, n
);
1219 return FN(MULTI(BASE
),free
)(multi
);
1221 for (i
= 0; i
< multi
->n
; ++i
) {
1222 multi
->p
[i
] = FN(EL
,move_dims
)(multi
->p
[i
], dst_type
, dst_pos
,
1223 src_type
, src_pos
, n
);
1225 return FN(MULTI(BASE
),free
)(multi
);
1232 /* Convert a multiple expression defined over a parameter domain
1233 * into one that is defined over a zero-dimensional set.
1235 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),from_range
)(
1236 __isl_take
MULTI(BASE
) *multi
)
1242 if (!isl_space_is_set(multi
->space
))
1243 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1244 "not living in a set space",
1245 return FN(MULTI(BASE
),free
)(multi
));
1247 space
= FN(MULTI(BASE
),get_space
)(multi
);
1248 space
= isl_space_from_range(space
);
1249 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
1254 /* Are "multi1" and "multi2" obviously equal?
1256 int FN(MULTI(BASE
),plain_is_equal
)(__isl_keep
MULTI(BASE
) *multi1
,
1257 __isl_keep
MULTI(BASE
) *multi2
)
1262 if (!multi1
|| !multi2
)
1264 if (multi1
->n
!= multi2
->n
)
1266 equal
= isl_space_is_equal(multi1
->space
, multi2
->space
);
1267 if (equal
< 0 || !equal
)
1270 for (i
= 0; i
< multi1
->n
; ++i
) {
1271 equal
= FN(EL
,plain_is_equal
)(multi1
->p
[i
], multi2
->p
[i
]);
1272 if (equal
< 0 || !equal
)