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 #include <isl_multi_macro.h>
17 #define MULTI_NAME(BASE) "isl_multi_" #BASE
18 #define xLIST(EL) EL ## _list
19 #define LIST(EL) xLIST(EL)
21 isl_ctx
*FN(MULTI(BASE
),get_ctx
)(__isl_keep
MULTI(BASE
) *multi
)
23 return multi
? isl_space_get_ctx(multi
->space
) : NULL
;
26 __isl_give isl_space
*FN(MULTI(BASE
),get_space
)(__isl_keep
MULTI(BASE
) *multi
)
28 return multi
? isl_space_copy(multi
->space
) : NULL
;
31 /* Return the position of the dimension of the given type and name
33 * Return -1 if no such dimension can be found.
35 int FN(MULTI(BASE
),find_dim_by_name
)(__isl_keep
MULTI(BASE
) *multi
,
36 enum isl_dim_type type
, const char *name
)
40 return isl_space_find_dim_by_name(multi
->space
, type
, name
);
43 __isl_give isl_space
*FN(MULTI(BASE
),get_domain_space
)(
44 __isl_keep
MULTI(BASE
) *multi
)
46 return multi
? isl_space_domain(isl_space_copy(multi
->space
)) : NULL
;
49 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),alloc
)(__isl_take isl_space
*space
)
58 ctx
= isl_space_get_ctx(space
);
59 n
= isl_space_dim(space
, isl_dim_out
);
60 multi
= isl_calloc(ctx
, MULTI(BASE
),
61 sizeof(MULTI(BASE
)) + (n
- 1) * sizeof(struct EL
*));
70 isl_space_free(space
);
74 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),dup
)(__isl_keep
MULTI(BASE
) *multi
)
82 dup
= FN(MULTI(BASE
),alloc
)(isl_space_copy(multi
->space
));
86 for (i
= 0; i
< multi
->n
; ++i
)
87 dup
= FN(FN(MULTI(BASE
),set
),BASE
)(dup
, i
,
88 FN(EL
,copy
)(multi
->p
[i
]));
93 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),cow
)(__isl_take
MULTI(BASE
) *multi
)
102 return FN(MULTI(BASE
),dup
)(multi
);
105 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),copy
)(__isl_keep
MULTI(BASE
) *multi
)
114 __isl_null
MULTI(BASE
) *FN(MULTI(BASE
),free
)(__isl_take
MULTI(BASE
) *multi
)
121 if (--multi
->ref
> 0)
124 isl_space_free(multi
->space
);
125 for (i
= 0; i
< multi
->n
; ++i
)
126 FN(EL
,free
)(multi
->p
[i
]);
133 /* Check whether "multi" has non-zero coefficients for any dimension
134 * in the given range or if any of these dimensions appear
135 * with non-zero coefficients in any of the integer divisions involved.
137 isl_bool
FN(MULTI(BASE
),involves_dims
)(__isl_keep
MULTI(BASE
) *multi
,
138 enum isl_dim_type type
, unsigned first
, unsigned n
)
143 return isl_bool_error
;
144 if (multi
->n
== 0 || n
== 0)
145 return isl_bool_false
;
147 for (i
= 0; i
< multi
->n
; ++i
) {
150 involves
= FN(EL
,involves_dims
)(multi
->p
[i
], type
, first
, n
);
151 if (involves
< 0 || involves
)
155 return isl_bool_false
;
158 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),insert_dims
)(
159 __isl_take
MULTI(BASE
) *multi
,
160 enum isl_dim_type type
, unsigned first
, unsigned n
)
166 if (type
== isl_dim_out
)
167 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
168 "cannot insert output/set dimensions",
169 return FN(MULTI(BASE
),free
)(multi
));
170 if (n
== 0 && !isl_space_is_named_or_nested(multi
->space
, type
))
173 multi
= FN(MULTI(BASE
),cow
)(multi
);
177 multi
->space
= isl_space_insert_dims(multi
->space
, type
, first
, n
);
179 return FN(MULTI(BASE
),free
)(multi
);
181 for (i
= 0; i
< multi
->n
; ++i
) {
182 multi
->p
[i
] = FN(EL
,insert_dims
)(multi
->p
[i
], type
, first
, n
);
184 return FN(MULTI(BASE
),free
)(multi
);
190 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),add_dims
)(__isl_take
MULTI(BASE
) *multi
,
191 enum isl_dim_type type
, unsigned n
)
195 pos
= FN(MULTI(BASE
),dim
)(multi
, type
);
197 return FN(MULTI(BASE
),insert_dims
)(multi
, type
, pos
, n
);
201 unsigned FN(MULTI(BASE
),dim
)(__isl_keep
MULTI(BASE
) *multi
,
202 enum isl_dim_type type
)
204 return multi
? isl_space_dim(multi
->space
, type
) : 0;
207 /* Return the position of the first dimension of "type" with id "id".
208 * Return -1 if there is no such dimension.
210 int FN(MULTI(BASE
),find_dim_by_id
)(__isl_keep
MULTI(BASE
) *multi
,
211 enum isl_dim_type type
, __isl_keep isl_id
*id
)
215 return isl_space_find_dim_by_id(multi
->space
, type
, id
);
218 /* Return the id of the given dimension.
220 __isl_give isl_id
*FN(MULTI(BASE
),get_dim_id
)(__isl_keep
MULTI(BASE
) *multi
,
221 enum isl_dim_type type
, unsigned pos
)
223 return multi
? isl_space_get_dim_id(multi
->space
, type
, pos
) : NULL
;
226 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_name
)(
227 __isl_take
MULTI(BASE
) *multi
,
228 enum isl_dim_type type
, unsigned pos
, const char *s
)
232 multi
= FN(MULTI(BASE
),cow
)(multi
);
236 multi
->space
= isl_space_set_dim_name(multi
->space
, type
, pos
, s
);
238 return FN(MULTI(BASE
),free
)(multi
);
240 if (type
== isl_dim_out
)
242 for (i
= 0; i
< multi
->n
; ++i
) {
243 multi
->p
[i
] = FN(EL
,set_dim_name
)(multi
->p
[i
], type
, pos
, s
);
245 return FN(MULTI(BASE
),free
)(multi
);
251 const char *FN(MULTI(BASE
),get_tuple_name
)(__isl_keep
MULTI(BASE
) *multi
,
252 enum isl_dim_type type
)
254 return multi
? isl_space_get_tuple_name(multi
->space
, type
) : NULL
;
257 /* Does the specified tuple have an id?
259 isl_bool
FN(MULTI(BASE
),has_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
260 enum isl_dim_type type
)
263 return isl_bool_error
;
264 return isl_space_has_tuple_id(multi
->space
, type
);
267 /* Return the id of the specified tuple.
269 __isl_give isl_id
*FN(MULTI(BASE
),get_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
270 enum isl_dim_type type
)
272 return multi
? isl_space_get_tuple_id(multi
->space
, type
) : NULL
;
275 __isl_give EL
*FN(FN(MULTI(BASE
),get
),BASE
)(__isl_keep
MULTI(BASE
) *multi
,
282 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
283 if (pos
< 0 || pos
>= multi
->n
)
284 isl_die(ctx
, isl_error_invalid
,
285 "index out of bounds", return NULL
);
286 return FN(EL
,copy
)(multi
->p
[pos
]);
289 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),set
),BASE
)(
290 __isl_take
MULTI(BASE
) *multi
, int pos
, __isl_take EL
*el
)
292 isl_space
*multi_space
= NULL
;
293 isl_space
*el_space
= NULL
;
296 multi
= FN(MULTI(BASE
),cow
)(multi
);
300 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
301 match
= FN(EL
,matching_params
)(el
, multi_space
);
305 multi
= FN(MULTI(BASE
),align_params
)(multi
,
306 FN(EL
,get_space
)(el
));
307 isl_space_free(multi_space
);
308 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
309 el
= FN(EL
,align_params
)(el
, isl_space_copy(multi_space
));
311 if (FN(EL
,check_match_domain_space
)(el
, multi_space
) < 0)
314 if (pos
< 0 || pos
>= multi
->n
)
315 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
316 "index out of bounds", goto error
);
318 FN(EL
,free
)(multi
->p
[pos
]);
321 isl_space_free(multi_space
);
322 isl_space_free(el_space
);
326 FN(MULTI(BASE
),free
)(multi
);
328 isl_space_free(multi_space
);
329 isl_space_free(el_space
);
333 /* Reset the space of "multi". This function is called from isl_pw_templ.c
334 * and doesn't know if the space of an element object is represented
335 * directly or through its domain. It therefore passes along both,
336 * which we pass along to the element function since we don't how
337 * that is represented either.
339 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space_and_domain
)(
340 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
,
341 __isl_take isl_space
*domain
)
345 multi
= FN(MULTI(BASE
),cow
)(multi
);
346 if (!multi
|| !space
|| !domain
)
349 for (i
= 0; i
< multi
->n
; ++i
) {
350 multi
->p
[i
] = FN(EL
,reset_domain_space
)(multi
->p
[i
],
351 isl_space_copy(domain
));
355 isl_space_free(domain
);
356 isl_space_free(multi
->space
);
357 multi
->space
= space
;
361 isl_space_free(domain
);
362 isl_space_free(space
);
363 FN(MULTI(BASE
),free
)(multi
);
367 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_domain_space
)(
368 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*domain
)
372 space
= isl_space_extend_domain_with_range(isl_space_copy(domain
),
373 isl_space_copy(multi
->space
));
374 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
377 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space
)(
378 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
)
382 domain
= isl_space_domain(isl_space_copy(space
));
383 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
386 /* Set the id of the given dimension of "multi" to "id".
388 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_id
)(
389 __isl_take
MULTI(BASE
) *multi
,
390 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
394 multi
= FN(MULTI(BASE
),cow
)(multi
);
398 space
= FN(MULTI(BASE
),get_space
)(multi
);
399 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
401 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
404 FN(MULTI(BASE
),free
)(multi
);
408 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_name
)(
409 __isl_keep
MULTI(BASE
) *multi
, enum isl_dim_type type
,
414 multi
= FN(MULTI(BASE
),cow
)(multi
);
418 space
= FN(MULTI(BASE
),get_space
)(multi
);
419 space
= isl_space_set_tuple_name(space
, type
, s
);
421 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
424 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_id
)(
425 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
,
426 __isl_take isl_id
*id
)
430 multi
= FN(MULTI(BASE
),cow
)(multi
);
434 space
= FN(MULTI(BASE
),get_space
)(multi
);
435 space
= isl_space_set_tuple_id(space
, type
, id
);
437 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
443 /* Drop the id on the specified tuple.
445 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_tuple_id
)(
446 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
)
452 if (!FN(MULTI(BASE
),has_tuple_id
)(multi
, type
))
455 multi
= FN(MULTI(BASE
),cow
)(multi
);
459 space
= FN(MULTI(BASE
),get_space
)(multi
);
460 space
= isl_space_reset_tuple_id(space
, type
);
462 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
465 /* Reset the user pointer on all identifiers of parameters and tuples
466 * of the space of "multi".
468 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_user
)(
469 __isl_take
MULTI(BASE
) *multi
)
473 space
= FN(MULTI(BASE
),get_space
)(multi
);
474 space
= isl_space_reset_user(space
);
476 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
479 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),realign_domain
)(
480 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_reordering
*exp
)
484 multi
= FN(MULTI(BASE
),cow
)(multi
);
488 for (i
= 0; i
< multi
->n
; ++i
) {
489 multi
->p
[i
] = FN(EL
,realign_domain
)(multi
->p
[i
],
490 isl_reordering_copy(exp
));
495 multi
= FN(MULTI(BASE
),reset_domain_space
)(multi
,
496 isl_space_copy(exp
->dim
));
498 isl_reordering_free(exp
);
501 isl_reordering_free(exp
);
502 FN(MULTI(BASE
),free
)(multi
);
506 /* Align the parameters of "multi" to those of "model".
508 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params
)(
509 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*model
)
512 isl_bool equal_params
;
515 if (!multi
|| !model
)
518 equal_params
= isl_space_has_equal_params(multi
->space
, model
);
519 if (equal_params
< 0)
522 isl_space_free(model
);
526 ctx
= isl_space_get_ctx(model
);
527 if (!isl_space_has_named_params(model
))
528 isl_die(ctx
, isl_error_invalid
,
529 "model has unnamed parameters", goto error
);
530 if (!isl_space_has_named_params(multi
->space
))
531 isl_die(ctx
, isl_error_invalid
,
532 "input has unnamed parameters", goto error
);
534 model
= isl_space_params(model
);
535 exp
= isl_parameter_alignment_reordering(multi
->space
, model
);
536 exp
= isl_reordering_extend_space(exp
,
537 FN(MULTI(BASE
),get_domain_space
)(multi
));
538 multi
= FN(MULTI(BASE
),realign_domain
)(multi
, exp
);
540 isl_space_free(model
);
543 isl_space_free(model
);
544 FN(MULTI(BASE
),free
)(multi
);
548 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),LIST(BASE
))(
549 __isl_take isl_space
*space
, __isl_take
LIST(EL
) *list
)
559 ctx
= isl_space_get_ctx(space
);
560 n
= FN(FN(LIST(EL
),n
),BASE
)(list
);
561 if (n
!= isl_space_dim(space
, isl_dim_out
))
562 isl_die(ctx
, isl_error_invalid
,
563 "invalid number of elements in list", goto error
);
565 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
566 for (i
= 0; i
< n
; ++i
) {
567 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
568 FN(FN(LIST(EL
),get
),BASE
)(list
, i
));
571 isl_space_free(space
);
572 FN(LIST(EL
),free
)(list
);
575 isl_space_free(space
);
576 FN(LIST(EL
),free
)(list
);
581 /* Create a multi expression in the given space that maps each
582 * input dimension to the corresponding output dimension.
584 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),identity
)(__isl_take isl_space
*space
)
593 if (isl_space_is_set(space
))
594 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
595 "expecting map space", goto error
);
597 n
= isl_space_dim(space
, isl_dim_out
);
598 if (n
!= isl_space_dim(space
, isl_dim_in
))
599 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
600 "number of input and output dimensions needs to be "
601 "the same", goto error
);
603 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
606 isl_space_free(space
);
610 space
= isl_space_domain(space
);
611 ls
= isl_local_space_from_space(space
);
613 for (i
= 0; i
< n
; ++i
) {
615 el
= FN(EL
,var_on_domain
)(isl_local_space_copy(ls
),
617 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
, el
);
620 isl_local_space_free(ls
);
624 isl_space_free(space
);
630 /* Construct a multi expression in the given space with value zero in
631 * each of the output dimensions.
633 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),zero
)(__isl_take isl_space
*space
)
641 n
= isl_space_dim(space
, isl_dim_out
);
642 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
645 isl_space_free(space
);
651 space
= isl_space_domain(space
);
652 ls
= isl_local_space_from_space(space
);
653 el
= FN(EL
,zero_on_domain
)(ls
);
655 for (i
= 0; i
< n
; ++i
)
656 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
667 /* Create a multiple expression with a single output/set dimension
669 * For most multiple expression types, the base type has a single
670 * output/set dimension and the space of the result is therefore
671 * the same as the space of the input.
672 * In the case of isl_multi_union_pw_aff, however, the base type
673 * lives in a parameter space and we therefore need to add
674 * a single set dimension.
676 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),BASE
)(__isl_take EL
*el
)
681 space
= FN(EL
,get_space(el
));
682 if (isl_space_is_params(space
)) {
683 space
= isl_space_set_from_params(space
);
684 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
686 multi
= FN(MULTI(BASE
),alloc
)(space
);
687 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, 0, el
);
693 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),drop_dims
)(
694 __isl_take
MULTI(BASE
) *multi
,
695 enum isl_dim_type type
, unsigned first
, unsigned n
)
700 multi
= FN(MULTI(BASE
),cow
)(multi
);
704 dim
= FN(MULTI(BASE
),dim
)(multi
, type
);
705 if (first
+ n
> dim
|| first
+ n
< first
)
706 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
707 "index out of bounds",
708 return FN(MULTI(BASE
),cow
)(multi
));
710 multi
->space
= isl_space_drop_dims(multi
->space
, type
, first
, n
);
712 return FN(MULTI(BASE
),cow
)(multi
);
714 if (type
== isl_dim_out
) {
715 for (i
= 0; i
< n
; ++i
)
716 FN(EL
,free
)(multi
->p
[first
+ i
]);
717 for (i
= first
; i
+ n
< multi
->n
; ++i
)
718 multi
->p
[i
] = multi
->p
[i
+ n
];
724 for (i
= 0; i
< multi
->n
; ++i
) {
725 multi
->p
[i
] = FN(EL
,drop_dims
)(multi
->p
[i
], type
, first
, n
);
727 return FN(MULTI(BASE
),cow
)(multi
);
733 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
735 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params_multi_multi_and
)(
736 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
737 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi1
,
738 __isl_take
MULTI(BASE
) *multi2
))
741 isl_bool equal_params
;
743 if (!multi1
|| !multi2
)
745 equal_params
= isl_space_has_equal_params(multi1
->space
, multi2
->space
);
746 if (equal_params
< 0)
749 return fn(multi1
, multi2
);
750 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
751 if (!isl_space_has_named_params(multi1
->space
) ||
752 !isl_space_has_named_params(multi2
->space
))
753 isl_die(ctx
, isl_error_invalid
,
754 "unaligned unnamed parameters", goto error
);
755 multi1
= FN(MULTI(BASE
),align_params
)(multi1
,
756 FN(MULTI(BASE
),get_space
)(multi2
));
757 multi2
= FN(MULTI(BASE
),align_params
)(multi2
,
758 FN(MULTI(BASE
),get_space
)(multi1
));
759 return fn(multi1
, multi2
);
761 FN(MULTI(BASE
),free
)(multi1
);
762 FN(MULTI(BASE
),free
)(multi2
);
766 /* Given two MULTI(BASE)s A -> B and C -> D,
767 * construct a MULTI(BASE) (A * C) -> [B -> D].
769 * The parameters are assumed to have been aligned.
771 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product_aligned
)(
772 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
779 if (!multi1
|| !multi2
)
782 space
= isl_space_range_product(FN(MULTI(BASE
),get_space
)(multi1
),
783 FN(MULTI(BASE
),get_space
)(multi2
));
784 res
= FN(MULTI(BASE
),alloc
)(space
);
786 n1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
787 n2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
789 for (i
= 0; i
< n1
; ++i
) {
790 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
791 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
794 for (i
= 0; i
< n2
; ++i
) {
795 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
796 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, n1
+ i
, el
);
799 FN(MULTI(BASE
),free
)(multi1
);
800 FN(MULTI(BASE
),free
)(multi2
);
803 FN(MULTI(BASE
),free
)(multi1
);
804 FN(MULTI(BASE
),free
)(multi2
);
808 /* Given two MULTI(BASE)s A -> B and C -> D,
809 * construct a MULTI(BASE) (A * C) -> [B -> D].
811 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product
)(
812 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
814 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
815 &FN(MULTI(BASE
),range_product_aligned
));
818 /* Is the range of "multi" a wrapped relation?
820 isl_bool
FN(MULTI(BASE
),range_is_wrapping
)(__isl_keep
MULTI(BASE
) *multi
)
823 return isl_bool_error
;
824 return isl_space_range_is_wrapping(multi
->space
);
827 /* Given a function A -> [B -> C], extract the function A -> B.
829 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_domain
)(
830 __isl_take
MULTI(BASE
) *multi
)
837 if (!isl_space_range_is_wrapping(multi
->space
))
838 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
839 "range is not a product",
840 return FN(MULTI(BASE
),free
)(multi
));
842 space
= FN(MULTI(BASE
),get_space
)(multi
);
843 total
= isl_space_dim(space
, isl_dim_out
);
844 space
= isl_space_range_factor_domain(space
);
845 keep
= isl_space_dim(space
, isl_dim_out
);
846 multi
= FN(MULTI(BASE
),drop_dims
)(multi
,
847 isl_dim_out
, keep
, total
- keep
);
848 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
853 /* Given a function A -> [B -> C], extract the function A -> C.
855 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_range
)(
856 __isl_take
MULTI(BASE
) *multi
)
863 if (!isl_space_range_is_wrapping(multi
->space
))
864 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
865 "range is not a product",
866 return FN(MULTI(BASE
),free
)(multi
));
868 space
= FN(MULTI(BASE
),get_space
)(multi
);
869 total
= isl_space_dim(space
, isl_dim_out
);
870 space
= isl_space_range_factor_range(space
);
871 keep
= isl_space_dim(space
, isl_dim_out
);
872 multi
= FN(MULTI(BASE
),drop_dims
)(multi
, isl_dim_out
, 0, total
- keep
);
873 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
878 /* Given a function [B -> C], extract the function C.
880 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),factor_range
)(
881 __isl_take
MULTI(BASE
) *multi
)
888 if (!isl_space_is_wrapping(multi
->space
))
889 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
890 "not a product", return FN(MULTI(BASE
),free
)(multi
));
892 space
= FN(MULTI(BASE
),get_space
)(multi
);
893 total
= isl_space_dim(space
, isl_dim_out
);
894 space
= isl_space_factor_range(space
);
895 keep
= isl_space_dim(space
, isl_dim_out
);
896 multi
= FN(MULTI(BASE
),drop_dims
)(multi
, isl_dim_out
, 0, total
- keep
);
897 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
903 /* Given two MULTI(BASE)s A -> B and C -> D,
904 * construct a MULTI(BASE) [A -> C] -> [B -> D].
906 * The parameters are assumed to have been aligned.
908 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),product_aligned
)(
909 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
915 int in1
, in2
, out1
, out2
;
917 in1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_in
);
918 in2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_in
);
919 out1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
920 out2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
921 space
= isl_space_product(FN(MULTI(BASE
),get_space
)(multi1
),
922 FN(MULTI(BASE
),get_space
)(multi2
));
923 res
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
924 space
= isl_space_domain(space
);
926 for (i
= 0; i
< out1
; ++i
) {
927 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
928 el
= FN(EL
,insert_dims
)(el
, isl_dim_in
, in1
, in2
);
929 el
= FN(EL
,reset_domain_space
)(el
, isl_space_copy(space
));
930 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
933 for (i
= 0; i
< out2
; ++i
) {
934 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
935 el
= FN(EL
,insert_dims
)(el
, isl_dim_in
, 0, in1
);
936 el
= FN(EL
,reset_domain_space
)(el
, isl_space_copy(space
));
937 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, out1
+ i
, el
);
940 isl_space_free(space
);
941 FN(MULTI(BASE
),free
)(multi1
);
942 FN(MULTI(BASE
),free
)(multi2
);
946 /* Given two MULTI(BASE)s A -> B and C -> D,
947 * construct a MULTI(BASE) [A -> C] -> [B -> D].
949 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),product
)(
950 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
952 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
953 &FN(MULTI(BASE
),product_aligned
));
957 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flatten_range
)(
958 __isl_take
MULTI(BASE
) *multi
)
963 if (!multi
->space
->nested
[1])
966 multi
= FN(MULTI(BASE
),cow
)(multi
);
970 multi
->space
= isl_space_flatten_range(multi
->space
);
972 return FN(MULTI(BASE
),free
)(multi
);
977 /* Given two MULTI(BASE)s A -> B and C -> D,
978 * construct a MULTI(BASE) (A * C) -> (B, D).
980 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flat_range_product
)(
981 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
985 multi
= FN(MULTI(BASE
),range_product
)(multi1
, multi2
);
986 multi
= FN(MULTI(BASE
),flatten_range
)(multi
);
990 /* Given two multi expressions, "multi1"
994 * where B2 starts at position "pos", and "multi2"
998 * return the multi expression
1002 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_splice
)(
1003 __isl_take
MULTI(BASE
) *multi1
, unsigned pos
,
1004 __isl_take
MULTI(BASE
) *multi2
)
1009 if (!multi1
|| !multi2
)
1012 dim
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
1014 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
1015 "index out of bounds", goto error
);
1017 res
= FN(MULTI(BASE
),copy
)(multi1
);
1018 res
= FN(MULTI(BASE
),drop_dims
)(res
, isl_dim_out
, pos
, dim
- pos
);
1019 multi1
= FN(MULTI(BASE
),drop_dims
)(multi1
, isl_dim_out
, 0, pos
);
1021 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi2
);
1022 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi1
);
1026 FN(MULTI(BASE
),free
)(multi1
);
1027 FN(MULTI(BASE
),free
)(multi2
);
1032 /* Given two multi expressions, "multi1"
1034 * [A1 A2] -> [B1 B2]
1036 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
1041 * return the multi expression
1043 * [A1 C A2] -> [B1 D B2]
1045 * We first insert input dimensions to obtain
1047 * [A1 C A2] -> [B1 B2]
1053 * and then apply range_splice.
1055 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),splice
)(
1056 __isl_take
MULTI(BASE
) *multi1
, unsigned in_pos
, unsigned out_pos
,
1057 __isl_take
MULTI(BASE
) *multi2
)
1062 if (!multi1
|| !multi2
)
1065 n_in1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_in
);
1067 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
1068 "index out of bounds", goto error
);
1070 n_in2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_in
);
1072 multi1
= FN(MULTI(BASE
),insert_dims
)(multi1
, isl_dim_in
, in_pos
, n_in2
);
1073 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, n_in2
,
1075 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, 0, in_pos
);
1077 return FN(MULTI(BASE
),range_splice
)(multi1
, out_pos
, multi2
);
1079 FN(MULTI(BASE
),free
)(multi1
);
1080 FN(MULTI(BASE
),free
)(multi2
);
1085 /* This function is currently only used from isl_aff.c
1087 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
1088 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
1089 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
1090 __attribute__ ((unused
));
1092 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
1093 * return the result.
1095 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
1096 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
1097 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
1102 multi1
= FN(MULTI(BASE
),cow
)(multi1
);
1103 if (!multi1
|| !multi2
)
1106 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
1107 if (!isl_space_is_equal(multi1
->space
, multi2
->space
))
1108 isl_die(ctx
, isl_error_invalid
,
1109 "spaces don't match", goto error
);
1111 for (i
= 0; i
< multi1
->n
; ++i
) {
1112 multi1
->p
[i
] = fn(multi1
->p
[i
], FN(EL
,copy
)(multi2
->p
[i
]));
1117 FN(MULTI(BASE
),free
)(multi2
);
1120 FN(MULTI(BASE
),free
)(multi1
);
1121 FN(MULTI(BASE
),free
)(multi2
);
1125 /* Add "multi2" from "multi1" and return the result.
1127 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
1129 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),add_aligned
)(
1130 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
1132 return FN(MULTI(BASE
),bin_op
)(multi1
, multi2
, &FN(EL
,add
));
1135 /* Add "multi2" from "multi1" and return the result.
1137 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),add
)(__isl_take
MULTI(BASE
) *multi1
,
1138 __isl_take
MULTI(BASE
) *multi2
)
1140 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
1141 &FN(MULTI(BASE
),add_aligned
));
1144 /* Subtract "multi2" from "multi1" and return the result.
1146 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
1148 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),sub_aligned
)(
1149 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
1151 return FN(MULTI(BASE
),bin_op
)(multi1
, multi2
, &FN(EL
,sub
));
1154 /* Subtract "multi2" from "multi1" and return the result.
1156 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),sub
)(__isl_take
MULTI(BASE
) *multi1
,
1157 __isl_take
MULTI(BASE
) *multi2
)
1159 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
1160 &FN(MULTI(BASE
),sub_aligned
));
1163 /* Multiply the elements of "multi" by "v" and return the result.
1165 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_val
)(__isl_take
MULTI(BASE
) *multi
,
1166 __isl_take isl_val
*v
)
1173 if (isl_val_is_one(v
)) {
1178 if (!isl_val_is_rat(v
))
1179 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1180 "expecting rational factor", goto error
);
1182 multi
= FN(MULTI(BASE
),cow
)(multi
);
1186 for (i
= 0; i
< multi
->n
; ++i
) {
1187 multi
->p
[i
] = FN(EL
,scale_val
)(multi
->p
[i
], isl_val_copy(v
));
1196 return FN(MULTI(BASE
),free
)(multi
);
1199 /* Divide the elements of "multi" by "v" and return the result.
1201 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_down_val
)(
1202 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_val
*v
)
1209 if (isl_val_is_one(v
)) {
1214 if (!isl_val_is_rat(v
))
1215 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1216 "expecting rational factor", goto error
);
1217 if (isl_val_is_zero(v
))
1218 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1219 "cannot scale down by zero", goto error
);
1221 multi
= FN(MULTI(BASE
),cow
)(multi
);
1225 for (i
= 0; i
< multi
->n
; ++i
) {
1226 multi
->p
[i
] = FN(EL
,scale_down_val
)(multi
->p
[i
],
1236 return FN(MULTI(BASE
),free
)(multi
);
1239 /* Multiply the elements of "multi" by the corresponding element of "mv"
1240 * and return the result.
1242 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_multi_val
)(
1243 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1250 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1251 mv
->space
, isl_dim_set
))
1252 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1253 "spaces don't match", goto error
);
1255 multi
= FN(MULTI(BASE
),cow
)(multi
);
1259 for (i
= 0; i
< multi
->n
; ++i
) {
1262 v
= isl_multi_val_get_val(mv
, i
);
1263 multi
->p
[i
] = FN(EL
,scale_val
)(multi
->p
[i
], v
);
1268 isl_multi_val_free(mv
);
1271 isl_multi_val_free(mv
);
1272 return FN(MULTI(BASE
),free
)(multi
);
1275 /* Divide the elements of "multi" by the corresponding element of "mv"
1276 * and return the result.
1278 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_down_multi_val
)(
1279 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1286 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1287 mv
->space
, isl_dim_set
))
1288 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1289 "spaces don't match", goto error
);
1291 multi
= FN(MULTI(BASE
),cow
)(multi
);
1295 for (i
= 0; i
< multi
->n
; ++i
) {
1298 v
= isl_multi_val_get_val(mv
, i
);
1299 multi
->p
[i
] = FN(EL
,scale_down_val
)(multi
->p
[i
], v
);
1304 isl_multi_val_free(mv
);
1307 isl_multi_val_free(mv
);
1308 return FN(MULTI(BASE
),free
)(multi
);
1311 /* Compute the residues of the elements of "multi" modulo
1312 * the corresponding element of "mv" and return the result.
1314 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),mod_multi_val
)(
1315 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1322 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1323 mv
->space
, isl_dim_set
))
1324 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1325 "spaces don't match", goto error
);
1327 multi
= FN(MULTI(BASE
),cow
)(multi
);
1331 for (i
= 0; i
< multi
->n
; ++i
) {
1334 v
= isl_multi_val_get_val(mv
, i
);
1335 multi
->p
[i
] = FN(EL
,mod_val
)(multi
->p
[i
], v
);
1340 isl_multi_val_free(mv
);
1343 isl_multi_val_free(mv
);
1344 return FN(MULTI(BASE
),free
)(multi
);
1347 #ifndef NO_MOVE_DIMS
1348 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1349 * to dimensions of "dst_type" at "dst_pos".
1351 * We only support moving input dimensions to parameters and vice versa.
1353 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),move_dims
)(__isl_take
MULTI(BASE
) *multi
,
1354 enum isl_dim_type dst_type
, unsigned dst_pos
,
1355 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1363 !isl_space_is_named_or_nested(multi
->space
, src_type
) &&
1364 !isl_space_is_named_or_nested(multi
->space
, dst_type
))
1367 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
1368 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1369 "cannot move output/set dimension",
1370 return FN(MULTI(BASE
),free
)(multi
));
1371 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
1372 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1374 return FN(MULTI(BASE
),free
)(multi
));
1375 if (src_pos
+ n
> isl_space_dim(multi
->space
, src_type
))
1376 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1377 "range out of bounds",
1378 return FN(MULTI(BASE
),free
)(multi
));
1379 if (dst_type
== src_type
)
1380 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_unsupported
,
1381 "moving dims within the same type not supported",
1382 return FN(MULTI(BASE
),free
)(multi
));
1384 multi
= FN(MULTI(BASE
),cow
)(multi
);
1388 multi
->space
= isl_space_move_dims(multi
->space
, dst_type
, dst_pos
,
1389 src_type
, src_pos
, n
);
1391 return FN(MULTI(BASE
),free
)(multi
);
1393 for (i
= 0; i
< multi
->n
; ++i
) {
1394 multi
->p
[i
] = FN(EL
,move_dims
)(multi
->p
[i
], dst_type
, dst_pos
,
1395 src_type
, src_pos
, n
);
1397 return FN(MULTI(BASE
),free
)(multi
);
1404 /* Convert a multiple expression defined over a parameter domain
1405 * into one that is defined over a zero-dimensional set.
1407 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),from_range
)(
1408 __isl_take
MULTI(BASE
) *multi
)
1414 if (!isl_space_is_set(multi
->space
))
1415 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1416 "not living in a set space",
1417 return FN(MULTI(BASE
),free
)(multi
));
1419 space
= FN(MULTI(BASE
),get_space
)(multi
);
1420 space
= isl_space_from_range(space
);
1421 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
1426 /* Are "multi1" and "multi2" obviously equal?
1428 isl_bool
FN(MULTI(BASE
),plain_is_equal
)(__isl_keep
MULTI(BASE
) *multi1
,
1429 __isl_keep
MULTI(BASE
) *multi2
)
1434 if (!multi1
|| !multi2
)
1435 return isl_bool_error
;
1436 if (multi1
->n
!= multi2
->n
)
1437 return isl_bool_false
;
1438 equal
= isl_space_is_equal(multi1
->space
, multi2
->space
);
1439 if (equal
< 0 || !equal
)
1442 for (i
= 0; i
< multi1
->n
; ++i
) {
1443 equal
= FN(EL
,plain_is_equal
)(multi1
->p
[i
], multi2
->p
[i
]);
1444 if (equal
< 0 || !equal
)
1448 return isl_bool_true
;
1451 /* Does "multi" involve any NaNs?
1453 isl_bool
FN(MULTI(BASE
),involves_nan
)(__isl_keep
MULTI(BASE
) *multi
)
1458 return isl_bool_error
;
1460 return isl_bool_false
;
1462 for (i
= 0; i
< multi
->n
; ++i
) {
1463 isl_bool has_nan
= FN(EL
,involves_nan
)(multi
->p
[i
]);
1464 if (has_nan
< 0 || has_nan
)
1468 return isl_bool_false
;
1472 /* Return the shared domain of the elements of "multi".
1474 __isl_give isl_set
*FN(MULTI(BASE
),domain
)(__isl_take
MULTI(BASE
) *multi
)
1482 dom
= isl_set_universe(FN(MULTI(BASE
),get_domain_space
)(multi
));
1483 for (i
= 0; i
< multi
->n
; ++i
) {
1486 dom_i
= FN(EL
,domain
)(FN(FN(MULTI(BASE
),get
),BASE
)(multi
, i
));
1487 dom
= isl_set_intersect(dom
, dom_i
);
1490 FN(MULTI(BASE
),free
)(multi
);
1496 /* Return the opposite of "multi".
1498 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),neg
)(__isl_take
MULTI(BASE
) *multi
)
1502 multi
= FN(MULTI(BASE
),cow
)(multi
);
1506 for (i
= 0; i
< multi
->n
; ++i
) {
1507 multi
->p
[i
] = FN(EL
,neg
)(multi
->p
[i
]);
1509 return FN(MULTI(BASE
),free
)(multi
);