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
12 #include <isl_space_private.h>
13 #include <isl_val_private.h>
15 #include <isl_reordering.h>
17 #include <isl_multi_macro.h>
19 #define MULTI_NAME(BASE) "isl_multi_" #BASE
21 isl_ctx
*FN(MULTI(BASE
),get_ctx
)(__isl_keep
MULTI(BASE
) *multi
)
23 return multi
? isl_space_get_ctx(multi
->space
) : NULL
;
26 /* Return the space of "multi".
28 __isl_keep isl_space
*FN(MULTI(BASE
),peek_space
)(__isl_keep
MULTI(BASE
) *multi
)
30 return multi
? multi
->space
: NULL
;
33 __isl_give isl_space
*FN(MULTI(BASE
),get_space
)(__isl_keep
MULTI(BASE
) *multi
)
35 return isl_space_copy(FN(MULTI(BASE
),peek_space
)(multi
));
38 /* Return the position of the dimension of the given type and name
40 * Return -1 if no such dimension can be found.
42 int FN(MULTI(BASE
),find_dim_by_name
)(__isl_keep
MULTI(BASE
) *multi
,
43 enum isl_dim_type type
, const char *name
)
47 return isl_space_find_dim_by_name(multi
->space
, type
, name
);
50 __isl_give isl_space
*FN(MULTI(BASE
),get_domain_space
)(
51 __isl_keep
MULTI(BASE
) *multi
)
53 return multi
? isl_space_domain(isl_space_copy(multi
->space
)) : NULL
;
56 /* Allocate a multi expression living in "space".
58 * If the number of base expressions is zero, then make sure
59 * there is enough room in the structure for the explicit domain,
60 * in case the type supports such an explicit domain.
62 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),alloc
)(__isl_take isl_space
*space
)
68 n
= isl_space_dim(space
, isl_dim_out
);
72 ctx
= isl_space_get_ctx(space
);
74 multi
= isl_calloc(ctx
, MULTI(BASE
),
75 sizeof(MULTI(BASE
)) + (n
- 1) * sizeof(struct EL
*));
77 multi
= isl_calloc(ctx
, MULTI(BASE
), sizeof(MULTI(BASE
)));
84 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
))
85 multi
= FN(MULTI(BASE
),init_explicit_domain
)(multi
);
88 isl_space_free(space
);
92 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),dup
)(__isl_keep
MULTI(BASE
) *multi
)
100 dup
= FN(MULTI(BASE
),alloc
)(isl_space_copy(multi
->space
));
104 for (i
= 0; i
< multi
->n
; ++i
)
105 dup
= FN(FN(MULTI(BASE
),set
),BASE
)(dup
, i
,
106 FN(EL
,copy
)(multi
->u
.p
[i
]));
107 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
))
108 dup
= FN(MULTI(BASE
),copy_explicit_domain
)(dup
, multi
);
113 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),cow
)(__isl_take
MULTI(BASE
) *multi
)
122 return FN(MULTI(BASE
),dup
)(multi
);
125 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),copy
)(__isl_keep
MULTI(BASE
) *multi
)
134 __isl_null
MULTI(BASE
) *FN(MULTI(BASE
),free
)(__isl_take
MULTI(BASE
) *multi
)
141 if (--multi
->ref
> 0)
144 isl_space_free(multi
->space
);
145 for (i
= 0; i
< multi
->n
; ++i
)
146 FN(EL
,free
)(multi
->u
.p
[i
]);
147 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
))
148 FN(MULTI(BASE
),free_explicit_domain
)(multi
);
154 isl_size
FN(MULTI(BASE
),dim
)(__isl_keep
MULTI(BASE
) *multi
,
155 enum isl_dim_type type
)
157 return isl_space_dim(FN(MULTI(BASE
),peek_space
)(multi
), type
);
160 /* Return the position of the first dimension of "type" with id "id".
161 * Return -1 if there is no such dimension.
163 int FN(MULTI(BASE
),find_dim_by_id
)(__isl_keep
MULTI(BASE
) *multi
,
164 enum isl_dim_type type
, __isl_keep isl_id
*id
)
168 return isl_space_find_dim_by_id(multi
->space
, type
, id
);
171 /* Return the id of the given dimension.
173 __isl_give isl_id
*FN(MULTI(BASE
),get_dim_id
)(__isl_keep
MULTI(BASE
) *multi
,
174 enum isl_dim_type type
, unsigned pos
)
176 return multi
? isl_space_get_dim_id(multi
->space
, type
, pos
) : NULL
;
179 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_name
)(
180 __isl_take
MULTI(BASE
) *multi
,
181 enum isl_dim_type type
, unsigned pos
, const char *s
)
185 multi
= FN(MULTI(BASE
),cow
)(multi
);
189 multi
->space
= isl_space_set_dim_name(multi
->space
, type
, pos
, s
);
191 return FN(MULTI(BASE
),free
)(multi
);
193 if (type
== isl_dim_out
)
195 for (i
= 0; i
< multi
->n
; ++i
) {
196 multi
->u
.p
[i
] = FN(EL
,set_dim_name
)(multi
->u
.p
[i
],
199 return FN(MULTI(BASE
),free
)(multi
);
205 const char *FN(MULTI(BASE
),get_tuple_name
)(__isl_keep
MULTI(BASE
) *multi
,
206 enum isl_dim_type type
)
208 return multi
? isl_space_get_tuple_name(multi
->space
, type
) : NULL
;
211 /* Does the specified tuple have an id?
213 isl_bool
FN(MULTI(BASE
),has_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
214 enum isl_dim_type type
)
217 return isl_bool_error
;
218 return isl_space_has_tuple_id(multi
->space
, type
);
221 /* Return the id of the specified tuple.
223 __isl_give isl_id
*FN(MULTI(BASE
),get_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
224 enum isl_dim_type type
)
226 return multi
? isl_space_get_tuple_id(multi
->space
, type
) : NULL
;
230 #define TYPE MULTI(BASE)
232 #include "check_type_range_templ.c"
234 __isl_give EL
*FN(FN(MULTI(BASE
),get
),BASE
)(__isl_keep
MULTI(BASE
) *multi
,
239 if (FN(MULTI(BASE
),check_range
)(multi
, isl_dim_out
, pos
, 1) < 0)
241 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
242 return FN(EL
,copy
)(multi
->u
.p
[pos
]);
245 /* Set the element at position "pos" of "multi" to "el",
246 * where the position may be empty if "multi" has only a single reference.
248 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),restore
)(
249 __isl_take
MULTI(BASE
) *multi
, int pos
, __isl_take EL
*el
)
251 multi
= FN(MULTI(BASE
),cow
)(multi
);
255 if (FN(MULTI(BASE
),check_range
)(multi
, isl_dim_out
, pos
, 1) < 0)
258 FN(EL
,free
)(multi
->u
.p
[pos
]);
259 multi
->u
.p
[pos
] = el
;
263 FN(MULTI(BASE
),free
)(multi
);
268 /* Set the element at position "pos" of "multi" to "el",
269 * where the position may be empty if "multi" has only a single reference.
270 * However, the space of "multi" is available and is checked
271 * for compatibility with "el".
273 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),restore_check_space
)(
274 __isl_take
MULTI(BASE
) *multi
, int pos
, __isl_take EL
*el
)
278 space
= FN(MULTI(BASE
),peek_space
)(multi
);
279 if (FN(EL
,check_match_domain_space
)(el
, space
) < 0)
280 multi
= FN(MULTI(BASE
),free
)(multi
);
281 return FN(MULTI(BASE
),restore
)(multi
, pos
, el
);
284 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),set
),BASE
)(
285 __isl_take
MULTI(BASE
) *multi
, int pos
, __isl_take EL
*el
)
287 isl_space
*multi_space
= NULL
;
288 isl_space
*el_space
= NULL
;
291 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
292 match
= FN(EL
,matching_params
)(el
, multi_space
);
296 multi
= FN(MULTI(BASE
),align_params
)(multi
,
297 FN(EL
,get_space
)(el
));
298 isl_space_free(multi_space
);
299 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
300 el
= FN(EL
,align_params
)(el
, isl_space_copy(multi_space
));
303 multi
= FN(MULTI(BASE
),restore_check_space
)(multi
, pos
, el
);
305 isl_space_free(multi_space
);
306 isl_space_free(el_space
);
310 FN(MULTI(BASE
),free
)(multi
);
312 isl_space_free(multi_space
);
313 isl_space_free(el_space
);
317 /* Reset the space of "multi". This function is called from isl_pw_templ.c
318 * and doesn't know if the space of an element object is represented
319 * directly or through its domain. It therefore passes along both,
320 * which we pass along to the element function since we don't know how
321 * that is represented either.
323 * If "multi" has an explicit domain, then the caller is expected
324 * to make sure that any modification that would change the dimensions
325 * of the explicit domain has bee applied before this function is called.
327 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space_and_domain
)(
328 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
,
329 __isl_take isl_space
*domain
)
333 multi
= FN(MULTI(BASE
),cow
)(multi
);
334 if (!multi
|| !space
|| !domain
)
337 for (i
= 0; i
< multi
->n
; ++i
) {
338 multi
->u
.p
[i
] = FN(EL
,reset_domain_space
)(multi
->u
.p
[i
],
339 isl_space_copy(domain
));
343 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
)) {
344 multi
= FN(MULTI(BASE
),reset_explicit_domain_space
)(multi
,
345 isl_space_copy(domain
));
349 isl_space_free(domain
);
350 isl_space_free(multi
->space
);
351 multi
->space
= space
;
355 isl_space_free(domain
);
356 isl_space_free(space
);
357 FN(MULTI(BASE
),free
)(multi
);
361 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_domain_space
)(
362 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*domain
)
366 space
= isl_space_extend_domain_with_range(isl_space_copy(domain
),
367 isl_space_copy(multi
->space
));
368 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
371 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space
)(
372 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
)
376 domain
= isl_space_domain(isl_space_copy(space
));
377 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
380 /* Set the id of the given dimension of "multi" to "id".
382 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_id
)(
383 __isl_take
MULTI(BASE
) *multi
,
384 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
388 multi
= FN(MULTI(BASE
),cow
)(multi
);
392 space
= FN(MULTI(BASE
),get_space
)(multi
);
393 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
395 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
398 FN(MULTI(BASE
),free
)(multi
);
402 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_name
)(
403 __isl_keep
MULTI(BASE
) *multi
, enum isl_dim_type type
,
408 multi
= FN(MULTI(BASE
),cow
)(multi
);
412 space
= FN(MULTI(BASE
),get_space
)(multi
);
413 space
= isl_space_set_tuple_name(space
, type
, s
);
415 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
418 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_id
)(
419 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
,
420 __isl_take isl_id
*id
)
424 multi
= FN(MULTI(BASE
),cow
)(multi
);
428 space
= FN(MULTI(BASE
),get_space
)(multi
);
429 space
= isl_space_set_tuple_id(space
, type
, id
);
431 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
437 /* Drop the id on the specified tuple.
439 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_tuple_id
)(
440 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
)
446 if (!FN(MULTI(BASE
),has_tuple_id
)(multi
, type
))
449 multi
= FN(MULTI(BASE
),cow
)(multi
);
453 space
= FN(MULTI(BASE
),get_space
)(multi
);
454 space
= isl_space_reset_tuple_id(space
, type
);
456 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
459 /* Reset the user pointer on all identifiers of parameters and tuples
460 * of the space of "multi".
462 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_user
)(
463 __isl_take
MULTI(BASE
) *multi
)
467 space
= FN(MULTI(BASE
),get_space
)(multi
);
468 space
= isl_space_reset_user(space
);
470 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
473 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),realign_domain
)(
474 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_reordering
*exp
)
479 multi
= FN(MULTI(BASE
),cow
)(multi
);
483 for (i
= 0; i
< multi
->n
; ++i
) {
484 multi
->u
.p
[i
] = FN(EL
,realign_domain
)(multi
->u
.p
[i
],
485 isl_reordering_copy(exp
));
490 space
= isl_reordering_get_space(exp
);
491 multi
= FN(MULTI(BASE
),reset_domain_space
)(multi
, space
);
493 isl_reordering_free(exp
);
496 isl_reordering_free(exp
);
497 FN(MULTI(BASE
),free
)(multi
);
501 /* Align the parameters of "multi" to those of "model".
503 * If "multi" has an explicit domain, then align the parameters
504 * of the domain first.
506 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params
)(
507 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*model
)
510 isl_bool equal_params
;
513 if (!multi
|| !model
)
516 equal_params
= isl_space_has_equal_params(multi
->space
, model
);
517 if (equal_params
< 0)
520 isl_space_free(model
);
524 ctx
= isl_space_get_ctx(model
);
525 if (!isl_space_has_named_params(model
))
526 isl_die(ctx
, isl_error_invalid
,
527 "model has unnamed parameters", goto error
);
528 if (!isl_space_has_named_params(multi
->space
))
529 isl_die(ctx
, isl_error_invalid
,
530 "input has unnamed parameters", goto error
);
532 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
)) {
533 multi
= FN(MULTI(BASE
),align_explicit_domain_params
)(multi
,
534 isl_space_copy(model
));
538 exp
= isl_parameter_alignment_reordering(multi
->space
, model
);
539 exp
= isl_reordering_extend_space(exp
,
540 FN(MULTI(BASE
),get_domain_space
)(multi
));
541 multi
= FN(MULTI(BASE
),realign_domain
)(multi
, exp
);
543 isl_space_free(model
);
546 isl_space_free(model
);
547 FN(MULTI(BASE
),free
)(multi
);
551 /* Create a multi expression in the given space with the elements of "list"
552 * as base expressions.
554 * Since isl_multi_*_restore_* assumes that the element and
555 * the multi expression have matching spaces, the alignment
556 * (if any) needs to be performed beforehand.
558 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),LIST(BASE
))(
559 __isl_take isl_space
*space
, __isl_take
LIST(EL
) *list
)
566 dim
= isl_space_dim(space
, isl_dim_out
);
567 n
= FN(FN(LIST(EL
),n
),BASE
)(list
);
568 if (dim
< 0 || n
< 0)
571 ctx
= isl_space_get_ctx(space
);
573 isl_die(ctx
, isl_error_invalid
,
574 "invalid number of elements in list", goto error
);
576 for (i
= 0; i
< n
; ++i
) {
577 EL
*el
= FN(LIST(EL
),peek
)(list
, i
);
578 space
= isl_space_align_params(space
, FN(EL
,get_space
)(el
));
580 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
581 for (i
= 0; i
< n
; ++i
) {
582 EL
*el
= FN(FN(LIST(EL
),get
),BASE
)(list
, i
);
583 el
= FN(EL
,align_params
)(el
, isl_space_copy(space
));
584 multi
= FN(MULTI(BASE
),restore_check_space
)(multi
, i
, el
);
587 isl_space_free(space
);
588 FN(LIST(EL
),free
)(list
);
591 isl_space_free(space
);
592 FN(LIST(EL
),free
)(list
);
596 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),drop_dims
)(
597 __isl_take
MULTI(BASE
) *multi
,
598 enum isl_dim_type type
, unsigned first
, unsigned n
)
602 multi
= FN(MULTI(BASE
),cow
)(multi
);
603 if (FN(MULTI(BASE
),check_range
)(multi
, type
, first
, n
) < 0)
604 return FN(MULTI(BASE
),free
)(multi
);
606 multi
->space
= isl_space_drop_dims(multi
->space
, type
, first
, n
);
608 return FN(MULTI(BASE
),free
)(multi
);
610 if (type
== isl_dim_out
) {
611 for (i
= 0; i
< n
; ++i
)
612 FN(EL
,free
)(multi
->u
.p
[first
+ i
]);
613 for (i
= first
; i
+ n
< multi
->n
; ++i
)
614 multi
->u
.p
[i
] = multi
->u
.p
[i
+ n
];
616 if (n
> 0 && FN(MULTI(BASE
),has_explicit_domain
)(multi
))
617 multi
= FN(MULTI(BASE
),init_explicit_domain
)(multi
);
622 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
))
623 multi
= FN(MULTI(BASE
),drop_explicit_domain_dims
)(multi
,
628 for (i
= 0; i
< multi
->n
; ++i
) {
629 multi
->u
.p
[i
] = FN(EL
,drop_dims
)(multi
->u
.p
[i
], type
, first
, n
);
631 return FN(MULTI(BASE
),free
)(multi
);
637 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
639 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params_multi_multi_and
)(
640 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
641 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi1
,
642 __isl_take
MULTI(BASE
) *multi2
))
645 isl_bool equal_params
;
647 if (!multi1
|| !multi2
)
649 equal_params
= isl_space_has_equal_params(multi1
->space
, multi2
->space
);
650 if (equal_params
< 0)
653 return fn(multi1
, multi2
);
654 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
655 if (!isl_space_has_named_params(multi1
->space
) ||
656 !isl_space_has_named_params(multi2
->space
))
657 isl_die(ctx
, isl_error_invalid
,
658 "unaligned unnamed parameters", goto error
);
659 multi1
= FN(MULTI(BASE
),align_params
)(multi1
,
660 FN(MULTI(BASE
),get_space
)(multi2
));
661 multi2
= FN(MULTI(BASE
),align_params
)(multi2
,
662 FN(MULTI(BASE
),get_space
)(multi1
));
663 return fn(multi1
, multi2
);
665 FN(MULTI(BASE
),free
)(multi1
);
666 FN(MULTI(BASE
),free
)(multi2
);
670 /* Given two MULTI(BASE)s A -> B and C -> D,
671 * construct a MULTI(BASE) (A * C) -> [B -> D].
673 * The parameters are assumed to have been aligned.
675 * If "multi1" and/or "multi2" has an explicit domain, then
676 * intersect the domain of the result with these explicit domains.
678 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product_aligned
)(
679 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
687 n1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
688 n2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
689 if (n1
< 0 || n2
< 0)
692 space
= isl_space_range_product(FN(MULTI(BASE
),get_space
)(multi1
),
693 FN(MULTI(BASE
),get_space
)(multi2
));
694 res
= FN(MULTI(BASE
),alloc
)(space
);
696 for (i
= 0; i
< n1
; ++i
) {
697 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
698 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
701 for (i
= 0; i
< n2
; ++i
) {
702 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
703 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, n1
+ i
, el
);
706 if (FN(MULTI(BASE
),has_explicit_domain
)(multi1
))
707 res
= FN(MULTI(BASE
),intersect_explicit_domain
)(res
, multi1
);
708 if (FN(MULTI(BASE
),has_explicit_domain
)(multi2
))
709 res
= FN(MULTI(BASE
),intersect_explicit_domain
)(res
, multi2
);
711 FN(MULTI(BASE
),free
)(multi1
);
712 FN(MULTI(BASE
),free
)(multi2
);
715 FN(MULTI(BASE
),free
)(multi1
);
716 FN(MULTI(BASE
),free
)(multi2
);
720 /* Given two MULTI(BASE)s A -> B and C -> D,
721 * construct a MULTI(BASE) (A * C) -> [B -> D].
723 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product
)(
724 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
726 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
727 &FN(MULTI(BASE
),range_product_aligned
));
730 /* Is the range of "multi" a wrapped relation?
732 isl_bool
FN(MULTI(BASE
),range_is_wrapping
)(__isl_keep
MULTI(BASE
) *multi
)
735 return isl_bool_error
;
736 return isl_space_range_is_wrapping(multi
->space
);
739 /* Given a function A -> [B -> C], extract the function A -> B.
741 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_domain
)(
742 __isl_take
MULTI(BASE
) *multi
)
745 isl_size total
, keep
;
747 total
= FN(MULTI(BASE
),dim
)(multi
, isl_dim_out
);
749 return FN(MULTI(BASE
),free
)(multi
);
750 if (!isl_space_range_is_wrapping(multi
->space
))
751 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
752 "range is not a product",
753 return FN(MULTI(BASE
),free
)(multi
));
755 space
= FN(MULTI(BASE
),get_space
)(multi
);
756 space
= isl_space_range_factor_domain(space
);
757 keep
= isl_space_dim(space
, isl_dim_out
);
759 multi
= FN(MULTI(BASE
),free
)(multi
);
760 multi
= FN(MULTI(BASE
),drop_dims
)(multi
,
761 isl_dim_out
, keep
, total
- keep
);
762 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
767 /* Given a function A -> [B -> C], extract the function A -> C.
769 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_range
)(
770 __isl_take
MULTI(BASE
) *multi
)
773 isl_size total
, keep
;
775 total
= FN(MULTI(BASE
),dim
)(multi
, isl_dim_out
);
777 return FN(MULTI(BASE
),free
)(multi
);
778 if (!isl_space_range_is_wrapping(multi
->space
))
779 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
780 "range is not a product",
781 return FN(MULTI(BASE
),free
)(multi
));
783 space
= FN(MULTI(BASE
),get_space
)(multi
);
784 space
= isl_space_range_factor_range(space
);
785 keep
= isl_space_dim(space
, isl_dim_out
);
787 multi
= FN(MULTI(BASE
),free
)(multi
);
788 multi
= FN(MULTI(BASE
),drop_dims
)(multi
, isl_dim_out
, 0, total
- keep
);
789 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
794 /* Given a function [B -> C], extract the function C.
796 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),factor_range
)(
797 __isl_take
MULTI(BASE
) *multi
)
800 isl_size total
, keep
;
802 total
= FN(MULTI(BASE
),dim
)(multi
, isl_dim_set
);
804 return FN(MULTI(BASE
),free
)(multi
);
805 if (!isl_space_is_wrapping(multi
->space
))
806 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
807 "not a product", return FN(MULTI(BASE
),free
)(multi
));
809 space
= FN(MULTI(BASE
),get_space
)(multi
);
810 space
= isl_space_factor_range(space
);
811 keep
= isl_space_dim(space
, isl_dim_set
);
813 multi
= FN(MULTI(BASE
),free
)(multi
);
814 multi
= FN(MULTI(BASE
),drop_dims
)(multi
, isl_dim_set
, 0, total
- keep
);
815 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
820 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flatten_range
)(
821 __isl_take
MULTI(BASE
) *multi
)
826 if (!multi
->space
->nested
[1])
829 multi
= FN(MULTI(BASE
),cow
)(multi
);
833 multi
->space
= isl_space_flatten_range(multi
->space
);
835 return FN(MULTI(BASE
),free
)(multi
);
840 /* Given two MULTI(BASE)s A -> B and C -> D,
841 * construct a MULTI(BASE) (A * C) -> (B, D).
843 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flat_range_product
)(
844 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
848 multi
= FN(MULTI(BASE
),range_product
)(multi1
, multi2
);
849 multi
= FN(MULTI(BASE
),flatten_range
)(multi
);
853 /* Given two multi expressions, "multi1"
857 * where B2 starts at position "pos", and "multi2"
861 * return the multi expression
865 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_splice
)(
866 __isl_take
MULTI(BASE
) *multi1
, unsigned pos
,
867 __isl_take
MULTI(BASE
) *multi2
)
872 dim
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
873 if (dim
< 0 || !multi2
)
876 if (FN(MULTI(BASE
),check_range
)(multi1
, isl_dim_out
, pos
, 0) < 0)
879 res
= FN(MULTI(BASE
),copy
)(multi1
);
880 res
= FN(MULTI(BASE
),drop_dims
)(res
, isl_dim_out
, pos
, dim
- pos
);
881 multi1
= FN(MULTI(BASE
),drop_dims
)(multi1
, isl_dim_out
, 0, pos
);
883 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi2
);
884 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi1
);
888 FN(MULTI(BASE
),free
)(multi1
);
889 FN(MULTI(BASE
),free
)(multi2
);
893 /* Check that "multi1" and "multi2" live in the same space,
894 * reporting an error if they do not.
896 static isl_stat
FN(MULTI(BASE
),check_equal_space
)(
897 __isl_keep
MULTI(BASE
) *multi1
, __isl_keep
MULTI(BASE
) *multi2
)
901 if (!multi1
|| !multi2
)
902 return isl_stat_error
;
904 equal
= isl_space_is_equal(multi1
->space
, multi2
->space
);
906 return isl_stat_error
;
908 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
909 "spaces don't match", return isl_stat_error
);
914 /* This function is currently only used from isl_aff.c
916 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
917 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
918 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
919 __attribute__ ((unused
));
921 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
924 * If "multi2" has an explicit domain, then
925 * intersect the domain of the result with this explicit domain.
927 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
928 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
929 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
933 multi1
= FN(MULTI(BASE
),cow
)(multi1
);
934 if (FN(MULTI(BASE
),check_equal_space
)(multi1
, multi2
) < 0)
937 for (i
= 0; i
< multi1
->n
; ++i
) {
938 multi1
->u
.p
[i
] = fn(multi1
->u
.p
[i
],
939 FN(EL
,copy
)(multi2
->u
.p
[i
]));
944 if (FN(MULTI(BASE
),has_explicit_domain
)(multi2
))
945 multi1
= FN(MULTI(BASE
),intersect_explicit_domain
)(multi1
,
948 FN(MULTI(BASE
),free
)(multi2
);
951 FN(MULTI(BASE
),free
)(multi1
);
952 FN(MULTI(BASE
),free
)(multi2
);
956 /* Add "multi2" to "multi1" and return the result.
958 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
960 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),add_aligned
)(
961 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
963 return FN(MULTI(BASE
),bin_op
)(multi1
, multi2
, &FN(EL
,add
));
966 /* Add "multi2" to "multi1" and return the result.
968 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),add
)(__isl_take
MULTI(BASE
) *multi1
,
969 __isl_take
MULTI(BASE
) *multi2
)
971 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
972 &FN(MULTI(BASE
),add_aligned
));
975 /* Subtract "multi2" from "multi1" and return the result.
977 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
979 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),sub_aligned
)(
980 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
982 return FN(MULTI(BASE
),bin_op
)(multi1
, multi2
, &FN(EL
,sub
));
985 /* Subtract "multi2" from "multi1" and return the result.
987 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),sub
)(__isl_take
MULTI(BASE
) *multi1
,
988 __isl_take
MULTI(BASE
) *multi2
)
990 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
991 &FN(MULTI(BASE
),sub_aligned
));
994 /* Multiply the elements of "multi" by "v" and return the result.
996 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_val
)(__isl_take
MULTI(BASE
) *multi
,
997 __isl_take isl_val
*v
)
1004 if (isl_val_is_one(v
)) {
1009 if (!isl_val_is_rat(v
))
1010 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1011 "expecting rational factor", goto error
);
1013 multi
= FN(MULTI(BASE
),cow
)(multi
);
1017 for (i
= 0; i
< multi
->n
; ++i
) {
1018 multi
->u
.p
[i
] = FN(EL
,scale_val
)(multi
->u
.p
[i
],
1028 return FN(MULTI(BASE
),free
)(multi
);
1031 /* Divide the elements of "multi" by "v" and return the result.
1033 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_down_val
)(
1034 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_val
*v
)
1041 if (isl_val_is_one(v
)) {
1046 if (!isl_val_is_rat(v
))
1047 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1048 "expecting rational factor", goto error
);
1049 if (isl_val_is_zero(v
))
1050 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1051 "cannot scale down by zero", goto error
);
1053 multi
= FN(MULTI(BASE
),cow
)(multi
);
1057 for (i
= 0; i
< multi
->n
; ++i
) {
1058 multi
->u
.p
[i
] = FN(EL
,scale_down_val
)(multi
->u
.p
[i
],
1068 return FN(MULTI(BASE
),free
)(multi
);
1071 /* Multiply the elements of "multi" by the corresponding element of "mv"
1072 * and return the result.
1074 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_multi_val
)(
1075 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1082 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1083 mv
->space
, isl_dim_set
))
1084 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1085 "spaces don't match", goto error
);
1087 multi
= FN(MULTI(BASE
),cow
)(multi
);
1091 for (i
= 0; i
< multi
->n
; ++i
) {
1094 v
= isl_multi_val_get_val(mv
, i
);
1095 multi
->u
.p
[i
] = FN(EL
,scale_val
)(multi
->u
.p
[i
], v
);
1100 isl_multi_val_free(mv
);
1103 isl_multi_val_free(mv
);
1104 return FN(MULTI(BASE
),free
)(multi
);
1107 /* Divide the elements of "multi" by the corresponding element of "mv"
1108 * and return the result.
1110 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_down_multi_val
)(
1111 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1118 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1119 mv
->space
, isl_dim_set
))
1120 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1121 "spaces don't match", goto error
);
1123 multi
= FN(MULTI(BASE
),cow
)(multi
);
1127 for (i
= 0; i
< multi
->n
; ++i
) {
1130 v
= isl_multi_val_get_val(mv
, i
);
1131 multi
->u
.p
[i
] = FN(EL
,scale_down_val
)(multi
->u
.p
[i
], v
);
1136 isl_multi_val_free(mv
);
1139 isl_multi_val_free(mv
);
1140 return FN(MULTI(BASE
),free
)(multi
);
1143 /* Compute the residues of the elements of "multi" modulo
1144 * the corresponding element of "mv" and return the result.
1146 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),mod_multi_val
)(
1147 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1154 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1155 mv
->space
, isl_dim_set
))
1156 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1157 "spaces don't match", goto error
);
1159 multi
= FN(MULTI(BASE
),cow
)(multi
);
1163 for (i
= 0; i
< multi
->n
; ++i
) {
1166 v
= isl_multi_val_get_val(mv
, i
);
1167 multi
->u
.p
[i
] = FN(EL
,mod_val
)(multi
->u
.p
[i
], v
);
1172 isl_multi_val_free(mv
);
1175 isl_multi_val_free(mv
);
1176 return FN(MULTI(BASE
),free
)(multi
);
1179 /* Convert a multiple expression defined over a parameter domain
1180 * into one that is defined over a zero-dimensional set.
1182 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),from_range
)(
1183 __isl_take
MULTI(BASE
) *multi
)
1189 if (!isl_space_is_set(multi
->space
))
1190 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1191 "not living in a set space",
1192 return FN(MULTI(BASE
),free
)(multi
));
1194 space
= FN(MULTI(BASE
),get_space
)(multi
);
1195 space
= isl_space_from_range(space
);
1196 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
1201 /* Are "multi1" and "multi2" obviously equal?
1203 isl_bool
FN(MULTI(BASE
),plain_is_equal
)(__isl_keep
MULTI(BASE
) *multi1
,
1204 __isl_keep
MULTI(BASE
) *multi2
)
1209 if (!multi1
|| !multi2
)
1210 return isl_bool_error
;
1211 if (multi1
->n
!= multi2
->n
)
1212 return isl_bool_false
;
1213 equal
= isl_space_is_equal(multi1
->space
, multi2
->space
);
1214 if (equal
< 0 || !equal
)
1217 for (i
= 0; i
< multi1
->n
; ++i
) {
1218 equal
= FN(EL
,plain_is_equal
)(multi1
->u
.p
[i
], multi2
->u
.p
[i
]);
1219 if (equal
< 0 || !equal
)
1223 if (FN(MULTI(BASE
),has_explicit_domain
)(multi1
) ||
1224 FN(MULTI(BASE
),has_explicit_domain
)(multi2
)) {
1225 equal
= FN(MULTI(BASE
),equal_explicit_domain
)(multi1
, multi2
);
1226 if (equal
< 0 || !equal
)
1230 return isl_bool_true
;
1233 /* Does "multi" involve any NaNs?
1235 isl_bool
FN(MULTI(BASE
),involves_nan
)(__isl_keep
MULTI(BASE
) *multi
)
1240 return isl_bool_error
;
1242 return isl_bool_false
;
1244 for (i
= 0; i
< multi
->n
; ++i
) {
1245 isl_bool has_nan
= FN(EL
,involves_nan
)(multi
->u
.p
[i
]);
1246 if (has_nan
< 0 || has_nan
)
1250 return isl_bool_false
;
1253 /* Return the opposite of "multi".
1255 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),neg
)(__isl_take
MULTI(BASE
) *multi
)
1259 multi
= FN(MULTI(BASE
),cow
)(multi
);
1263 for (i
= 0; i
< multi
->n
; ++i
) {
1264 multi
->u
.p
[i
] = FN(EL
,neg
)(multi
->u
.p
[i
]);
1266 return FN(MULTI(BASE
),free
)(multi
);