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
]);
132 unsigned FN(MULTI(BASE
),dim
)(__isl_keep
MULTI(BASE
) *multi
,
133 enum isl_dim_type type
)
135 return multi
? isl_space_dim(multi
->space
, type
) : 0;
138 /* Return the position of the first dimension of "type" with id "id".
139 * Return -1 if there is no such dimension.
141 int FN(MULTI(BASE
),find_dim_by_id
)(__isl_keep
MULTI(BASE
) *multi
,
142 enum isl_dim_type type
, __isl_keep isl_id
*id
)
146 return isl_space_find_dim_by_id(multi
->space
, type
, id
);
149 /* Return the id of the given dimension.
151 __isl_give isl_id
*FN(MULTI(BASE
),get_dim_id
)(__isl_keep
MULTI(BASE
) *multi
,
152 enum isl_dim_type type
, unsigned pos
)
154 return multi
? isl_space_get_dim_id(multi
->space
, type
, pos
) : NULL
;
157 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_name
)(
158 __isl_take
MULTI(BASE
) *multi
,
159 enum isl_dim_type type
, unsigned pos
, const char *s
)
163 multi
= FN(MULTI(BASE
),cow
)(multi
);
167 multi
->space
= isl_space_set_dim_name(multi
->space
, type
, pos
, s
);
169 return FN(MULTI(BASE
),free
)(multi
);
171 if (type
== isl_dim_out
)
173 for (i
= 0; i
< multi
->n
; ++i
) {
174 multi
->p
[i
] = FN(EL
,set_dim_name
)(multi
->p
[i
], type
, pos
, s
);
176 return FN(MULTI(BASE
),free
)(multi
);
182 const char *FN(MULTI(BASE
),get_tuple_name
)(__isl_keep
MULTI(BASE
) *multi
,
183 enum isl_dim_type type
)
185 return multi
? isl_space_get_tuple_name(multi
->space
, type
) : NULL
;
188 /* Does the specified tuple have an id?
190 isl_bool
FN(MULTI(BASE
),has_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
191 enum isl_dim_type type
)
194 return isl_bool_error
;
195 return isl_space_has_tuple_id(multi
->space
, type
);
198 /* Return the id of the specified tuple.
200 __isl_give isl_id
*FN(MULTI(BASE
),get_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
201 enum isl_dim_type type
)
203 return multi
? isl_space_get_tuple_id(multi
->space
, type
) : NULL
;
206 __isl_give EL
*FN(FN(MULTI(BASE
),get
),BASE
)(__isl_keep
MULTI(BASE
) *multi
,
213 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
214 if (pos
< 0 || pos
>= multi
->n
)
215 isl_die(ctx
, isl_error_invalid
,
216 "index out of bounds", return NULL
);
217 return FN(EL
,copy
)(multi
->p
[pos
]);
220 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),set
),BASE
)(
221 __isl_take
MULTI(BASE
) *multi
, int pos
, __isl_take EL
*el
)
223 isl_space
*multi_space
= NULL
;
224 isl_space
*el_space
= NULL
;
227 multi
= FN(MULTI(BASE
),cow
)(multi
);
231 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
232 match
= FN(EL
,matching_params
)(el
, multi_space
);
236 multi
= FN(MULTI(BASE
),align_params
)(multi
,
237 FN(EL
,get_space
)(el
));
238 isl_space_free(multi_space
);
239 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
240 el
= FN(EL
,align_params
)(el
, isl_space_copy(multi_space
));
242 if (FN(EL
,check_match_domain_space
)(el
, multi_space
) < 0)
245 if (pos
< 0 || pos
>= multi
->n
)
246 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
247 "index out of bounds", goto error
);
249 FN(EL
,free
)(multi
->p
[pos
]);
252 isl_space_free(multi_space
);
253 isl_space_free(el_space
);
257 FN(MULTI(BASE
),free
)(multi
);
259 isl_space_free(multi_space
);
260 isl_space_free(el_space
);
264 /* Reset the space of "multi". This function is called from isl_pw_templ.c
265 * and doesn't know if the space of an element object is represented
266 * directly or through its domain. It therefore passes along both,
267 * which we pass along to the element function since we don't know how
268 * that is represented either.
270 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space_and_domain
)(
271 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
,
272 __isl_take isl_space
*domain
)
276 multi
= FN(MULTI(BASE
),cow
)(multi
);
277 if (!multi
|| !space
|| !domain
)
280 for (i
= 0; i
< multi
->n
; ++i
) {
281 multi
->p
[i
] = FN(EL
,reset_domain_space
)(multi
->p
[i
],
282 isl_space_copy(domain
));
286 isl_space_free(domain
);
287 isl_space_free(multi
->space
);
288 multi
->space
= space
;
292 isl_space_free(domain
);
293 isl_space_free(space
);
294 FN(MULTI(BASE
),free
)(multi
);
298 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_domain_space
)(
299 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*domain
)
303 space
= isl_space_extend_domain_with_range(isl_space_copy(domain
),
304 isl_space_copy(multi
->space
));
305 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
308 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space
)(
309 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
)
313 domain
= isl_space_domain(isl_space_copy(space
));
314 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
317 /* Set the id of the given dimension of "multi" to "id".
319 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_id
)(
320 __isl_take
MULTI(BASE
) *multi
,
321 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
325 multi
= FN(MULTI(BASE
),cow
)(multi
);
329 space
= FN(MULTI(BASE
),get_space
)(multi
);
330 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
332 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
335 FN(MULTI(BASE
),free
)(multi
);
339 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_name
)(
340 __isl_keep
MULTI(BASE
) *multi
, enum isl_dim_type type
,
345 multi
= FN(MULTI(BASE
),cow
)(multi
);
349 space
= FN(MULTI(BASE
),get_space
)(multi
);
350 space
= isl_space_set_tuple_name(space
, type
, s
);
352 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
355 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_id
)(
356 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
,
357 __isl_take isl_id
*id
)
361 multi
= FN(MULTI(BASE
),cow
)(multi
);
365 space
= FN(MULTI(BASE
),get_space
)(multi
);
366 space
= isl_space_set_tuple_id(space
, type
, id
);
368 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
374 /* Drop the id on the specified tuple.
376 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_tuple_id
)(
377 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
)
383 if (!FN(MULTI(BASE
),has_tuple_id
)(multi
, type
))
386 multi
= FN(MULTI(BASE
),cow
)(multi
);
390 space
= FN(MULTI(BASE
),get_space
)(multi
);
391 space
= isl_space_reset_tuple_id(space
, type
);
393 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
396 /* Reset the user pointer on all identifiers of parameters and tuples
397 * of the space of "multi".
399 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_user
)(
400 __isl_take
MULTI(BASE
) *multi
)
404 space
= FN(MULTI(BASE
),get_space
)(multi
);
405 space
= isl_space_reset_user(space
);
407 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
410 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),realign_domain
)(
411 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_reordering
*exp
)
415 multi
= FN(MULTI(BASE
),cow
)(multi
);
419 for (i
= 0; i
< multi
->n
; ++i
) {
420 multi
->p
[i
] = FN(EL
,realign_domain
)(multi
->p
[i
],
421 isl_reordering_copy(exp
));
426 multi
= FN(MULTI(BASE
),reset_domain_space
)(multi
,
427 isl_space_copy(exp
->dim
));
429 isl_reordering_free(exp
);
432 isl_reordering_free(exp
);
433 FN(MULTI(BASE
),free
)(multi
);
437 /* Align the parameters of "multi" to those of "model".
439 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params
)(
440 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*model
)
443 isl_bool equal_params
;
446 if (!multi
|| !model
)
449 equal_params
= isl_space_has_equal_params(multi
->space
, model
);
450 if (equal_params
< 0)
453 isl_space_free(model
);
457 ctx
= isl_space_get_ctx(model
);
458 if (!isl_space_has_named_params(model
))
459 isl_die(ctx
, isl_error_invalid
,
460 "model has unnamed parameters", goto error
);
461 if (!isl_space_has_named_params(multi
->space
))
462 isl_die(ctx
, isl_error_invalid
,
463 "input has unnamed parameters", goto error
);
465 model
= isl_space_params(model
);
466 exp
= isl_parameter_alignment_reordering(multi
->space
, model
);
467 exp
= isl_reordering_extend_space(exp
,
468 FN(MULTI(BASE
),get_domain_space
)(multi
));
469 multi
= FN(MULTI(BASE
),realign_domain
)(multi
, exp
);
471 isl_space_free(model
);
474 isl_space_free(model
);
475 FN(MULTI(BASE
),free
)(multi
);
479 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),LIST(BASE
))(
480 __isl_take isl_space
*space
, __isl_take
LIST(EL
) *list
)
490 ctx
= isl_space_get_ctx(space
);
491 n
= FN(FN(LIST(EL
),n
),BASE
)(list
);
492 if (n
!= isl_space_dim(space
, isl_dim_out
))
493 isl_die(ctx
, isl_error_invalid
,
494 "invalid number of elements in list", goto error
);
496 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
497 for (i
= 0; i
< n
; ++i
) {
498 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
499 FN(FN(LIST(EL
),get
),BASE
)(list
, i
));
502 isl_space_free(space
);
503 FN(LIST(EL
),free
)(list
);
506 isl_space_free(space
);
507 FN(LIST(EL
),free
)(list
);
512 /* Create a multi expression in the given space that maps each
513 * input dimension to the corresponding output dimension.
515 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),identity
)(__isl_take isl_space
*space
)
524 if (isl_space_is_set(space
))
525 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
526 "expecting map space", goto error
);
528 n
= isl_space_dim(space
, isl_dim_out
);
529 if (n
!= isl_space_dim(space
, isl_dim_in
))
530 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
531 "number of input and output dimensions needs to be "
532 "the same", goto error
);
534 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
537 isl_space_free(space
);
541 space
= isl_space_domain(space
);
542 ls
= isl_local_space_from_space(space
);
544 for (i
= 0; i
< n
; ++i
) {
546 el
= FN(EL
,var_on_domain
)(isl_local_space_copy(ls
),
548 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
, el
);
551 isl_local_space_free(ls
);
555 isl_space_free(space
);
561 /* Construct a multi expression in the given space with value zero in
562 * each of the output dimensions.
564 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),zero
)(__isl_take isl_space
*space
)
572 n
= isl_space_dim(space
, isl_dim_out
);
573 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
576 isl_space_free(space
);
582 space
= isl_space_domain(space
);
583 ls
= isl_local_space_from_space(space
);
584 el
= FN(EL
,zero_on_domain
)(ls
);
586 for (i
= 0; i
< n
; ++i
)
587 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
598 /* Create a multiple expression with a single output/set dimension
600 * For most multiple expression types, the base type has a single
601 * output/set dimension and the space of the result is therefore
602 * the same as the space of the input.
603 * In the case of isl_multi_union_pw_aff, however, the base type
604 * lives in a parameter space and we therefore need to add
605 * a single set dimension.
607 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),BASE
)(__isl_take EL
*el
)
612 space
= FN(EL
,get_space(el
));
613 if (isl_space_is_params(space
)) {
614 space
= isl_space_set_from_params(space
);
615 space
= isl_space_add_dims(space
, isl_dim_set
, 1);
617 multi
= FN(MULTI(BASE
),alloc
)(space
);
618 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, 0, el
);
624 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),drop_dims
)(
625 __isl_take
MULTI(BASE
) *multi
,
626 enum isl_dim_type type
, unsigned first
, unsigned n
)
631 multi
= FN(MULTI(BASE
),cow
)(multi
);
635 dim
= FN(MULTI(BASE
),dim
)(multi
, type
);
636 if (first
+ n
> dim
|| first
+ n
< first
)
637 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
638 "index out of bounds",
639 return FN(MULTI(BASE
),free
)(multi
));
641 multi
->space
= isl_space_drop_dims(multi
->space
, type
, first
, n
);
643 return FN(MULTI(BASE
),free
)(multi
);
645 if (type
== isl_dim_out
) {
646 for (i
= 0; i
< n
; ++i
)
647 FN(EL
,free
)(multi
->p
[first
+ i
]);
648 for (i
= first
; i
+ n
< multi
->n
; ++i
)
649 multi
->p
[i
] = multi
->p
[i
+ n
];
655 for (i
= 0; i
< multi
->n
; ++i
) {
656 multi
->p
[i
] = FN(EL
,drop_dims
)(multi
->p
[i
], type
, first
, n
);
658 return FN(MULTI(BASE
),free
)(multi
);
664 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
666 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params_multi_multi_and
)(
667 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
668 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi1
,
669 __isl_take
MULTI(BASE
) *multi2
))
672 isl_bool equal_params
;
674 if (!multi1
|| !multi2
)
676 equal_params
= isl_space_has_equal_params(multi1
->space
, multi2
->space
);
677 if (equal_params
< 0)
680 return fn(multi1
, multi2
);
681 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
682 if (!isl_space_has_named_params(multi1
->space
) ||
683 !isl_space_has_named_params(multi2
->space
))
684 isl_die(ctx
, isl_error_invalid
,
685 "unaligned unnamed parameters", goto error
);
686 multi1
= FN(MULTI(BASE
),align_params
)(multi1
,
687 FN(MULTI(BASE
),get_space
)(multi2
));
688 multi2
= FN(MULTI(BASE
),align_params
)(multi2
,
689 FN(MULTI(BASE
),get_space
)(multi1
));
690 return fn(multi1
, multi2
);
692 FN(MULTI(BASE
),free
)(multi1
);
693 FN(MULTI(BASE
),free
)(multi2
);
697 /* Given two MULTI(BASE)s A -> B and C -> D,
698 * construct a MULTI(BASE) (A * C) -> [B -> D].
700 * The parameters are assumed to have been aligned.
702 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product_aligned
)(
703 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
710 if (!multi1
|| !multi2
)
713 space
= isl_space_range_product(FN(MULTI(BASE
),get_space
)(multi1
),
714 FN(MULTI(BASE
),get_space
)(multi2
));
715 res
= FN(MULTI(BASE
),alloc
)(space
);
717 n1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
718 n2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
720 for (i
= 0; i
< n1
; ++i
) {
721 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
722 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
725 for (i
= 0; i
< n2
; ++i
) {
726 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
727 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, n1
+ i
, el
);
730 FN(MULTI(BASE
),free
)(multi1
);
731 FN(MULTI(BASE
),free
)(multi2
);
734 FN(MULTI(BASE
),free
)(multi1
);
735 FN(MULTI(BASE
),free
)(multi2
);
739 /* Given two MULTI(BASE)s A -> B and C -> D,
740 * construct a MULTI(BASE) (A * C) -> [B -> D].
742 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product
)(
743 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
745 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
746 &FN(MULTI(BASE
),range_product_aligned
));
749 /* Is the range of "multi" a wrapped relation?
751 isl_bool
FN(MULTI(BASE
),range_is_wrapping
)(__isl_keep
MULTI(BASE
) *multi
)
754 return isl_bool_error
;
755 return isl_space_range_is_wrapping(multi
->space
);
758 /* Given a function A -> [B -> C], extract the function A -> B.
760 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_domain
)(
761 __isl_take
MULTI(BASE
) *multi
)
768 if (!isl_space_range_is_wrapping(multi
->space
))
769 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
770 "range is not a product",
771 return FN(MULTI(BASE
),free
)(multi
));
773 space
= FN(MULTI(BASE
),get_space
)(multi
);
774 total
= isl_space_dim(space
, isl_dim_out
);
775 space
= isl_space_range_factor_domain(space
);
776 keep
= isl_space_dim(space
, isl_dim_out
);
777 multi
= FN(MULTI(BASE
),drop_dims
)(multi
,
778 isl_dim_out
, keep
, total
- keep
);
779 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
784 /* Given a function A -> [B -> C], extract the function A -> C.
786 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_range
)(
787 __isl_take
MULTI(BASE
) *multi
)
794 if (!isl_space_range_is_wrapping(multi
->space
))
795 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
796 "range is not a product",
797 return FN(MULTI(BASE
),free
)(multi
));
799 space
= FN(MULTI(BASE
),get_space
)(multi
);
800 total
= isl_space_dim(space
, isl_dim_out
);
801 space
= isl_space_range_factor_range(space
);
802 keep
= isl_space_dim(space
, isl_dim_out
);
803 multi
= FN(MULTI(BASE
),drop_dims
)(multi
, isl_dim_out
, 0, total
- keep
);
804 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
809 /* Given a function [B -> C], extract the function C.
811 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),factor_range
)(
812 __isl_take
MULTI(BASE
) *multi
)
819 if (!isl_space_is_wrapping(multi
->space
))
820 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
821 "not a product", return FN(MULTI(BASE
),free
)(multi
));
823 space
= FN(MULTI(BASE
),get_space
)(multi
);
824 total
= isl_space_dim(space
, isl_dim_out
);
825 space
= isl_space_factor_range(space
);
826 keep
= isl_space_dim(space
, isl_dim_out
);
827 multi
= FN(MULTI(BASE
),drop_dims
)(multi
, isl_dim_out
, 0, total
- keep
);
828 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
834 /* Given two MULTI(BASE)s A -> B and C -> D,
835 * construct a MULTI(BASE) [A -> C] -> [B -> D].
837 * The parameters are assumed to have been aligned.
839 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),product_aligned
)(
840 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
846 int in1
, in2
, out1
, out2
;
848 in1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_in
);
849 in2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_in
);
850 out1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
851 out2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
852 space
= isl_space_product(FN(MULTI(BASE
),get_space
)(multi1
),
853 FN(MULTI(BASE
),get_space
)(multi2
));
854 res
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
855 space
= isl_space_domain(space
);
857 for (i
= 0; i
< out1
; ++i
) {
858 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
859 el
= FN(EL
,insert_dims
)(el
, isl_dim_in
, in1
, in2
);
860 el
= FN(EL
,reset_domain_space
)(el
, isl_space_copy(space
));
861 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
864 for (i
= 0; i
< out2
; ++i
) {
865 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
866 el
= FN(EL
,insert_dims
)(el
, isl_dim_in
, 0, in1
);
867 el
= FN(EL
,reset_domain_space
)(el
, isl_space_copy(space
));
868 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, out1
+ i
, el
);
871 isl_space_free(space
);
872 FN(MULTI(BASE
),free
)(multi1
);
873 FN(MULTI(BASE
),free
)(multi2
);
877 /* Given two MULTI(BASE)s A -> B and C -> D,
878 * construct a MULTI(BASE) [A -> C] -> [B -> D].
880 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),product
)(
881 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
883 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
884 &FN(MULTI(BASE
),product_aligned
));
888 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flatten_range
)(
889 __isl_take
MULTI(BASE
) *multi
)
894 if (!multi
->space
->nested
[1])
897 multi
= FN(MULTI(BASE
),cow
)(multi
);
901 multi
->space
= isl_space_flatten_range(multi
->space
);
903 return FN(MULTI(BASE
),free
)(multi
);
908 /* Given two MULTI(BASE)s A -> B and C -> D,
909 * construct a MULTI(BASE) (A * C) -> (B, D).
911 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flat_range_product
)(
912 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
916 multi
= FN(MULTI(BASE
),range_product
)(multi1
, multi2
);
917 multi
= FN(MULTI(BASE
),flatten_range
)(multi
);
921 /* Given two multi expressions, "multi1"
925 * where B2 starts at position "pos", and "multi2"
929 * return the multi expression
933 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_splice
)(
934 __isl_take
MULTI(BASE
) *multi1
, unsigned pos
,
935 __isl_take
MULTI(BASE
) *multi2
)
940 if (!multi1
|| !multi2
)
943 dim
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
945 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
946 "index out of bounds", goto error
);
948 res
= FN(MULTI(BASE
),copy
)(multi1
);
949 res
= FN(MULTI(BASE
),drop_dims
)(res
, isl_dim_out
, pos
, dim
- pos
);
950 multi1
= FN(MULTI(BASE
),drop_dims
)(multi1
, isl_dim_out
, 0, pos
);
952 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi2
);
953 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi1
);
957 FN(MULTI(BASE
),free
)(multi1
);
958 FN(MULTI(BASE
),free
)(multi2
);
963 /* Given two multi expressions, "multi1"
967 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
972 * return the multi expression
974 * [A1 C A2] -> [B1 D B2]
976 * We first insert input dimensions to obtain
978 * [A1 C A2] -> [B1 B2]
984 * and then apply range_splice.
986 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),splice
)(
987 __isl_take
MULTI(BASE
) *multi1
, unsigned in_pos
, unsigned out_pos
,
988 __isl_take
MULTI(BASE
) *multi2
)
993 if (!multi1
|| !multi2
)
996 n_in1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_in
);
998 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
999 "index out of bounds", goto error
);
1001 n_in2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_in
);
1003 multi1
= FN(MULTI(BASE
),insert_dims
)(multi1
, isl_dim_in
, in_pos
, n_in2
);
1004 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, n_in2
,
1006 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, 0, in_pos
);
1008 return FN(MULTI(BASE
),range_splice
)(multi1
, out_pos
, multi2
);
1010 FN(MULTI(BASE
),free
)(multi1
);
1011 FN(MULTI(BASE
),free
)(multi2
);
1016 /* This function is currently only used from isl_aff.c
1018 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
1019 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
1020 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
1021 __attribute__ ((unused
));
1023 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
1024 * return the result.
1026 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
1027 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
1028 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
1033 multi1
= FN(MULTI(BASE
),cow
)(multi1
);
1034 if (!multi1
|| !multi2
)
1037 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
1038 if (!isl_space_is_equal(multi1
->space
, multi2
->space
))
1039 isl_die(ctx
, isl_error_invalid
,
1040 "spaces don't match", goto error
);
1042 for (i
= 0; i
< multi1
->n
; ++i
) {
1043 multi1
->p
[i
] = fn(multi1
->p
[i
], FN(EL
,copy
)(multi2
->p
[i
]));
1048 FN(MULTI(BASE
),free
)(multi2
);
1051 FN(MULTI(BASE
),free
)(multi1
);
1052 FN(MULTI(BASE
),free
)(multi2
);
1056 /* Add "multi2" from "multi1" and return the result.
1058 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
1060 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),add_aligned
)(
1061 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
1063 return FN(MULTI(BASE
),bin_op
)(multi1
, multi2
, &FN(EL
,add
));
1066 /* Add "multi2" from "multi1" and return the result.
1068 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),add
)(__isl_take
MULTI(BASE
) *multi1
,
1069 __isl_take
MULTI(BASE
) *multi2
)
1071 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
1072 &FN(MULTI(BASE
),add_aligned
));
1075 /* Subtract "multi2" from "multi1" and return the result.
1077 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
1079 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),sub_aligned
)(
1080 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
1082 return FN(MULTI(BASE
),bin_op
)(multi1
, multi2
, &FN(EL
,sub
));
1085 /* Subtract "multi2" from "multi1" and return the result.
1087 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),sub
)(__isl_take
MULTI(BASE
) *multi1
,
1088 __isl_take
MULTI(BASE
) *multi2
)
1090 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
1091 &FN(MULTI(BASE
),sub_aligned
));
1094 /* Multiply the elements of "multi" by "v" and return the result.
1096 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_val
)(__isl_take
MULTI(BASE
) *multi
,
1097 __isl_take isl_val
*v
)
1104 if (isl_val_is_one(v
)) {
1109 if (!isl_val_is_rat(v
))
1110 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1111 "expecting rational factor", goto error
);
1113 multi
= FN(MULTI(BASE
),cow
)(multi
);
1117 for (i
= 0; i
< multi
->n
; ++i
) {
1118 multi
->p
[i
] = FN(EL
,scale_val
)(multi
->p
[i
], isl_val_copy(v
));
1127 return FN(MULTI(BASE
),free
)(multi
);
1130 /* Divide the elements of "multi" by "v" and return the result.
1132 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_down_val
)(
1133 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_val
*v
)
1140 if (isl_val_is_one(v
)) {
1145 if (!isl_val_is_rat(v
))
1146 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1147 "expecting rational factor", goto error
);
1148 if (isl_val_is_zero(v
))
1149 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1150 "cannot scale down by zero", goto error
);
1152 multi
= FN(MULTI(BASE
),cow
)(multi
);
1156 for (i
= 0; i
< multi
->n
; ++i
) {
1157 multi
->p
[i
] = FN(EL
,scale_down_val
)(multi
->p
[i
],
1167 return FN(MULTI(BASE
),free
)(multi
);
1170 /* Multiply the elements of "multi" by the corresponding element of "mv"
1171 * and return the result.
1173 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_multi_val
)(
1174 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1181 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1182 mv
->space
, isl_dim_set
))
1183 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1184 "spaces don't match", goto error
);
1186 multi
= FN(MULTI(BASE
),cow
)(multi
);
1190 for (i
= 0; i
< multi
->n
; ++i
) {
1193 v
= isl_multi_val_get_val(mv
, i
);
1194 multi
->p
[i
] = FN(EL
,scale_val
)(multi
->p
[i
], v
);
1199 isl_multi_val_free(mv
);
1202 isl_multi_val_free(mv
);
1203 return FN(MULTI(BASE
),free
)(multi
);
1206 /* Divide the elements of "multi" by the corresponding element of "mv"
1207 * and return the result.
1209 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_down_multi_val
)(
1210 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1217 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1218 mv
->space
, isl_dim_set
))
1219 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1220 "spaces don't match", goto error
);
1222 multi
= FN(MULTI(BASE
),cow
)(multi
);
1226 for (i
= 0; i
< multi
->n
; ++i
) {
1229 v
= isl_multi_val_get_val(mv
, i
);
1230 multi
->p
[i
] = FN(EL
,scale_down_val
)(multi
->p
[i
], v
);
1235 isl_multi_val_free(mv
);
1238 isl_multi_val_free(mv
);
1239 return FN(MULTI(BASE
),free
)(multi
);
1242 /* Compute the residues of the elements of "multi" modulo
1243 * the corresponding element of "mv" and return the result.
1245 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),mod_multi_val
)(
1246 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1253 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1254 mv
->space
, isl_dim_set
))
1255 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1256 "spaces don't match", goto error
);
1258 multi
= FN(MULTI(BASE
),cow
)(multi
);
1262 for (i
= 0; i
< multi
->n
; ++i
) {
1265 v
= isl_multi_val_get_val(mv
, i
);
1266 multi
->p
[i
] = FN(EL
,mod_val
)(multi
->p
[i
], v
);
1271 isl_multi_val_free(mv
);
1274 isl_multi_val_free(mv
);
1275 return FN(MULTI(BASE
),free
)(multi
);
1278 #ifndef NO_MOVE_DIMS
1279 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1280 * to dimensions of "dst_type" at "dst_pos".
1282 * We only support moving input dimensions to parameters and vice versa.
1284 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),move_dims
)(__isl_take
MULTI(BASE
) *multi
,
1285 enum isl_dim_type dst_type
, unsigned dst_pos
,
1286 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1294 !isl_space_is_named_or_nested(multi
->space
, src_type
) &&
1295 !isl_space_is_named_or_nested(multi
->space
, dst_type
))
1298 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
1299 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1300 "cannot move output/set dimension",
1301 return FN(MULTI(BASE
),free
)(multi
));
1302 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
1303 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1305 return FN(MULTI(BASE
),free
)(multi
));
1306 if (src_pos
+ n
> isl_space_dim(multi
->space
, src_type
))
1307 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1308 "range out of bounds",
1309 return FN(MULTI(BASE
),free
)(multi
));
1310 if (dst_type
== src_type
)
1311 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_unsupported
,
1312 "moving dims within the same type not supported",
1313 return FN(MULTI(BASE
),free
)(multi
));
1315 multi
= FN(MULTI(BASE
),cow
)(multi
);
1319 multi
->space
= isl_space_move_dims(multi
->space
, dst_type
, dst_pos
,
1320 src_type
, src_pos
, n
);
1322 return FN(MULTI(BASE
),free
)(multi
);
1324 for (i
= 0; i
< multi
->n
; ++i
) {
1325 multi
->p
[i
] = FN(EL
,move_dims
)(multi
->p
[i
], dst_type
, dst_pos
,
1326 src_type
, src_pos
, n
);
1328 return FN(MULTI(BASE
),free
)(multi
);
1335 /* Convert a multiple expression defined over a parameter domain
1336 * into one that is defined over a zero-dimensional set.
1338 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),from_range
)(
1339 __isl_take
MULTI(BASE
) *multi
)
1345 if (!isl_space_is_set(multi
->space
))
1346 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1347 "not living in a set space",
1348 return FN(MULTI(BASE
),free
)(multi
));
1350 space
= FN(MULTI(BASE
),get_space
)(multi
);
1351 space
= isl_space_from_range(space
);
1352 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
1357 /* Are "multi1" and "multi2" obviously equal?
1359 isl_bool
FN(MULTI(BASE
),plain_is_equal
)(__isl_keep
MULTI(BASE
) *multi1
,
1360 __isl_keep
MULTI(BASE
) *multi2
)
1365 if (!multi1
|| !multi2
)
1366 return isl_bool_error
;
1367 if (multi1
->n
!= multi2
->n
)
1368 return isl_bool_false
;
1369 equal
= isl_space_is_equal(multi1
->space
, multi2
->space
);
1370 if (equal
< 0 || !equal
)
1373 for (i
= 0; i
< multi1
->n
; ++i
) {
1374 equal
= FN(EL
,plain_is_equal
)(multi1
->p
[i
], multi2
->p
[i
]);
1375 if (equal
< 0 || !equal
)
1379 return isl_bool_true
;
1382 /* Does "multi" involve any NaNs?
1384 isl_bool
FN(MULTI(BASE
),involves_nan
)(__isl_keep
MULTI(BASE
) *multi
)
1389 return isl_bool_error
;
1391 return isl_bool_false
;
1393 for (i
= 0; i
< multi
->n
; ++i
) {
1394 isl_bool has_nan
= FN(EL
,involves_nan
)(multi
->p
[i
]);
1395 if (has_nan
< 0 || has_nan
)
1399 return isl_bool_false
;
1403 /* Return the shared domain of the elements of "multi".
1405 __isl_give isl_set
*FN(MULTI(BASE
),domain
)(__isl_take
MULTI(BASE
) *multi
)
1413 dom
= isl_set_universe(FN(MULTI(BASE
),get_domain_space
)(multi
));
1414 for (i
= 0; i
< multi
->n
; ++i
) {
1417 dom_i
= FN(EL
,domain
)(FN(FN(MULTI(BASE
),get
),BASE
)(multi
, i
));
1418 dom
= isl_set_intersect(dom
, dom_i
);
1421 FN(MULTI(BASE
),free
)(multi
);
1427 /* Return the opposite of "multi".
1429 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),neg
)(__isl_take
MULTI(BASE
) *multi
)
1433 multi
= FN(MULTI(BASE
),cow
)(multi
);
1437 for (i
= 0; i
< multi
->n
; ++i
) {
1438 multi
->p
[i
] = FN(EL
,neg
)(multi
->p
[i
]);
1440 return FN(MULTI(BASE
),free
)(multi
);