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
20 #define xLIST(EL) EL ## _list
21 #define LIST(EL) xLIST(EL)
23 isl_ctx
*FN(MULTI(BASE
),get_ctx
)(__isl_keep
MULTI(BASE
) *multi
)
25 return multi
? isl_space_get_ctx(multi
->space
) : NULL
;
28 /* Return the space of "multi".
30 __isl_keep isl_space
*FN(MULTI(BASE
),peek_space
)(__isl_keep
MULTI(BASE
) *multi
)
32 return multi
? multi
->space
: NULL
;
35 __isl_give isl_space
*FN(MULTI(BASE
),get_space
)(__isl_keep
MULTI(BASE
) *multi
)
37 return isl_space_copy(FN(MULTI(BASE
),peek_space
)(multi
));
40 /* Return the position of the dimension of the given type and name
42 * Return -1 if no such dimension can be found.
44 int FN(MULTI(BASE
),find_dim_by_name
)(__isl_keep
MULTI(BASE
) *multi
,
45 enum isl_dim_type type
, const char *name
)
49 return isl_space_find_dim_by_name(multi
->space
, type
, name
);
52 __isl_give isl_space
*FN(MULTI(BASE
),get_domain_space
)(
53 __isl_keep
MULTI(BASE
) *multi
)
55 return multi
? isl_space_domain(isl_space_copy(multi
->space
)) : NULL
;
58 /* Allocate a multi expression living in "space".
60 * If the number of base expressions is zero, then make sure
61 * there is enough room in the structure for the explicit domain,
62 * in case the type supports such an explicit domain.
64 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),alloc
)(__isl_take isl_space
*space
)
73 ctx
= isl_space_get_ctx(space
);
74 n
= isl_space_dim(space
, isl_dim_out
);
76 multi
= isl_calloc(ctx
, MULTI(BASE
),
77 sizeof(MULTI(BASE
)) + (n
- 1) * sizeof(struct EL
*));
79 multi
= isl_calloc(ctx
, MULTI(BASE
), sizeof(MULTI(BASE
)));
86 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
))
87 multi
= FN(MULTI(BASE
),init_explicit_domain
)(multi
);
90 isl_space_free(space
);
94 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),dup
)(__isl_keep
MULTI(BASE
) *multi
)
102 dup
= FN(MULTI(BASE
),alloc
)(isl_space_copy(multi
->space
));
106 for (i
= 0; i
< multi
->n
; ++i
)
107 dup
= FN(FN(MULTI(BASE
),set
),BASE
)(dup
, i
,
108 FN(EL
,copy
)(multi
->u
.p
[i
]));
109 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
))
110 dup
= FN(MULTI(BASE
),copy_explicit_domain
)(dup
, multi
);
115 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),cow
)(__isl_take
MULTI(BASE
) *multi
)
124 return FN(MULTI(BASE
),dup
)(multi
);
127 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),copy
)(__isl_keep
MULTI(BASE
) *multi
)
136 __isl_null
MULTI(BASE
) *FN(MULTI(BASE
),free
)(__isl_take
MULTI(BASE
) *multi
)
143 if (--multi
->ref
> 0)
146 isl_space_free(multi
->space
);
147 for (i
= 0; i
< multi
->n
; ++i
)
148 FN(EL
,free
)(multi
->u
.p
[i
]);
149 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
))
150 FN(MULTI(BASE
),free_explicit_domain
)(multi
);
156 unsigned FN(MULTI(BASE
),dim
)(__isl_keep
MULTI(BASE
) *multi
,
157 enum isl_dim_type type
)
159 return multi
? isl_space_dim(multi
->space
, type
) : 0;
162 /* Return the position of the first dimension of "type" with id "id".
163 * Return -1 if there is no such dimension.
165 int FN(MULTI(BASE
),find_dim_by_id
)(__isl_keep
MULTI(BASE
) *multi
,
166 enum isl_dim_type type
, __isl_keep isl_id
*id
)
170 return isl_space_find_dim_by_id(multi
->space
, type
, id
);
173 /* Return the id of the given dimension.
175 __isl_give isl_id
*FN(MULTI(BASE
),get_dim_id
)(__isl_keep
MULTI(BASE
) *multi
,
176 enum isl_dim_type type
, unsigned pos
)
178 return multi
? isl_space_get_dim_id(multi
->space
, type
, pos
) : NULL
;
181 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_name
)(
182 __isl_take
MULTI(BASE
) *multi
,
183 enum isl_dim_type type
, unsigned pos
, const char *s
)
187 multi
= FN(MULTI(BASE
),cow
)(multi
);
191 multi
->space
= isl_space_set_dim_name(multi
->space
, type
, pos
, s
);
193 return FN(MULTI(BASE
),free
)(multi
);
195 if (type
== isl_dim_out
)
197 for (i
= 0; i
< multi
->n
; ++i
) {
198 multi
->u
.p
[i
] = FN(EL
,set_dim_name
)(multi
->u
.p
[i
],
201 return FN(MULTI(BASE
),free
)(multi
);
207 const char *FN(MULTI(BASE
),get_tuple_name
)(__isl_keep
MULTI(BASE
) *multi
,
208 enum isl_dim_type type
)
210 return multi
? isl_space_get_tuple_name(multi
->space
, type
) : NULL
;
213 /* Does the specified tuple have an id?
215 isl_bool
FN(MULTI(BASE
),has_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
216 enum isl_dim_type type
)
219 return isl_bool_error
;
220 return isl_space_has_tuple_id(multi
->space
, type
);
223 /* Return the id of the specified tuple.
225 __isl_give isl_id
*FN(MULTI(BASE
),get_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
226 enum isl_dim_type type
)
228 return multi
? isl_space_get_tuple_id(multi
->space
, type
) : NULL
;
231 __isl_give EL
*FN(FN(MULTI(BASE
),get
),BASE
)(__isl_keep
MULTI(BASE
) *multi
,
238 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
239 if (pos
< 0 || pos
>= multi
->n
)
240 isl_die(ctx
, isl_error_invalid
,
241 "index out of bounds", return NULL
);
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 (pos
< 0 || pos
>= multi
->n
)
256 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
257 "index out of bounds", goto error
);
259 FN(EL
,free
)(multi
->u
.p
[pos
]);
260 multi
->u
.p
[pos
] = el
;
264 FN(MULTI(BASE
),free
)(multi
);
269 /* Set the element at position "pos" of "multi" to "el",
270 * where the position may be empty if "multi" has only a single reference.
271 * However, the space of "multi" is available and is checked
272 * for compatibility with "el".
274 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),restore_check_space
)(
275 __isl_take
MULTI(BASE
) *multi
, int pos
, __isl_take EL
*el
)
279 space
= FN(MULTI(BASE
),peek_space
)(multi
);
280 if (FN(EL
,check_match_domain_space
)(el
, space
) < 0)
281 multi
= FN(MULTI(BASE
),free
)(multi
);
282 return FN(MULTI(BASE
),restore
)(multi
, pos
, el
);
285 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),set
),BASE
)(
286 __isl_take
MULTI(BASE
) *multi
, int pos
, __isl_take EL
*el
)
288 isl_space
*multi_space
= NULL
;
289 isl_space
*el_space
= NULL
;
292 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
293 match
= FN(EL
,matching_params
)(el
, multi_space
);
297 multi
= FN(MULTI(BASE
),align_params
)(multi
,
298 FN(EL
,get_space
)(el
));
299 isl_space_free(multi_space
);
300 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
301 el
= FN(EL
,align_params
)(el
, isl_space_copy(multi_space
));
304 multi
= FN(MULTI(BASE
),restore_check_space
)(multi
, pos
, el
);
306 isl_space_free(multi_space
);
307 isl_space_free(el_space
);
311 FN(MULTI(BASE
),free
)(multi
);
313 isl_space_free(multi_space
);
314 isl_space_free(el_space
);
318 /* Reset the space of "multi". This function is called from isl_pw_templ.c
319 * and doesn't know if the space of an element object is represented
320 * directly or through its domain. It therefore passes along both,
321 * which we pass along to the element function since we don't know how
322 * that is represented either.
324 * If "multi" has an explicit domain, then the caller is expected
325 * to make sure that any modification that would change the dimensions
326 * of the explicit domain has bee applied before this function is called.
328 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space_and_domain
)(
329 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
,
330 __isl_take isl_space
*domain
)
334 multi
= FN(MULTI(BASE
),cow
)(multi
);
335 if (!multi
|| !space
|| !domain
)
338 for (i
= 0; i
< multi
->n
; ++i
) {
339 multi
->u
.p
[i
] = FN(EL
,reset_domain_space
)(multi
->u
.p
[i
],
340 isl_space_copy(domain
));
344 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
)) {
345 multi
= FN(MULTI(BASE
),reset_explicit_domain_space
)(multi
,
346 isl_space_copy(domain
));
350 isl_space_free(domain
);
351 isl_space_free(multi
->space
);
352 multi
->space
= space
;
356 isl_space_free(domain
);
357 isl_space_free(space
);
358 FN(MULTI(BASE
),free
)(multi
);
362 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_domain_space
)(
363 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*domain
)
367 space
= isl_space_extend_domain_with_range(isl_space_copy(domain
),
368 isl_space_copy(multi
->space
));
369 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
372 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space
)(
373 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
)
377 domain
= isl_space_domain(isl_space_copy(space
));
378 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
381 /* Set the id of the given dimension of "multi" to "id".
383 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_id
)(
384 __isl_take
MULTI(BASE
) *multi
,
385 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
389 multi
= FN(MULTI(BASE
),cow
)(multi
);
393 space
= FN(MULTI(BASE
),get_space
)(multi
);
394 space
= isl_space_set_dim_id(space
, type
, pos
, id
);
396 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
399 FN(MULTI(BASE
),free
)(multi
);
403 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_name
)(
404 __isl_keep
MULTI(BASE
) *multi
, enum isl_dim_type type
,
409 multi
= FN(MULTI(BASE
),cow
)(multi
);
413 space
= FN(MULTI(BASE
),get_space
)(multi
);
414 space
= isl_space_set_tuple_name(space
, type
, s
);
416 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
419 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_id
)(
420 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
,
421 __isl_take isl_id
*id
)
425 multi
= FN(MULTI(BASE
),cow
)(multi
);
429 space
= FN(MULTI(BASE
),get_space
)(multi
);
430 space
= isl_space_set_tuple_id(space
, type
, id
);
432 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
438 /* Drop the id on the specified tuple.
440 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_tuple_id
)(
441 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
)
447 if (!FN(MULTI(BASE
),has_tuple_id
)(multi
, type
))
450 multi
= FN(MULTI(BASE
),cow
)(multi
);
454 space
= FN(MULTI(BASE
),get_space
)(multi
);
455 space
= isl_space_reset_tuple_id(space
, type
);
457 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
460 /* Reset the user pointer on all identifiers of parameters and tuples
461 * of the space of "multi".
463 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_user
)(
464 __isl_take
MULTI(BASE
) *multi
)
468 space
= FN(MULTI(BASE
),get_space
)(multi
);
469 space
= isl_space_reset_user(space
);
471 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
474 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),realign_domain
)(
475 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_reordering
*exp
)
480 multi
= FN(MULTI(BASE
),cow
)(multi
);
484 for (i
= 0; i
< multi
->n
; ++i
) {
485 multi
->u
.p
[i
] = FN(EL
,realign_domain
)(multi
->u
.p
[i
],
486 isl_reordering_copy(exp
));
491 space
= isl_reordering_get_space(exp
);
492 multi
= FN(MULTI(BASE
),reset_domain_space
)(multi
, space
);
494 isl_reordering_free(exp
);
497 isl_reordering_free(exp
);
498 FN(MULTI(BASE
),free
)(multi
);
502 /* Align the parameters of "multi" to those of "model".
504 * If "multi" has an explicit domain, then align the parameters
505 * of the domain first.
507 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params
)(
508 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*model
)
511 isl_bool equal_params
;
514 if (!multi
|| !model
)
517 equal_params
= isl_space_has_equal_params(multi
->space
, model
);
518 if (equal_params
< 0)
521 isl_space_free(model
);
525 ctx
= isl_space_get_ctx(model
);
526 if (!isl_space_has_named_params(model
))
527 isl_die(ctx
, isl_error_invalid
,
528 "model has unnamed parameters", goto error
);
529 if (!isl_space_has_named_params(multi
->space
))
530 isl_die(ctx
, isl_error_invalid
,
531 "input has unnamed parameters", goto error
);
533 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
)) {
534 multi
= FN(MULTI(BASE
),align_explicit_domain_params
)(multi
,
535 isl_space_copy(model
));
539 exp
= isl_parameter_alignment_reordering(multi
->space
, model
);
540 exp
= isl_reordering_extend_space(exp
,
541 FN(MULTI(BASE
),get_domain_space
)(multi
));
542 multi
= FN(MULTI(BASE
),realign_domain
)(multi
, exp
);
544 isl_space_free(model
);
547 isl_space_free(model
);
548 FN(MULTI(BASE
),free
)(multi
);
552 /* Create a multi expression in the given space with the elements of "list"
553 * as base expressions.
555 * Since isl_multi_*_restore_* assumes that the element and
556 * the multi expression have matching spaces, the alignment
557 * (if any) needs to be performed beforehand.
559 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),LIST(BASE
))(
560 __isl_take isl_space
*space
, __isl_take
LIST(EL
) *list
)
570 ctx
= isl_space_get_ctx(space
);
571 n
= FN(FN(LIST(EL
),n
),BASE
)(list
);
572 if (n
!= isl_space_dim(space
, isl_dim_out
))
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
)
603 multi
= FN(MULTI(BASE
),cow
)(multi
);
607 dim
= FN(MULTI(BASE
),dim
)(multi
, type
);
608 if (first
+ n
> dim
|| first
+ n
< first
)
609 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
610 "index out of bounds",
611 return FN(MULTI(BASE
),free
)(multi
));
613 multi
->space
= isl_space_drop_dims(multi
->space
, type
, first
, n
);
615 return FN(MULTI(BASE
),free
)(multi
);
617 if (type
== isl_dim_out
) {
618 for (i
= 0; i
< n
; ++i
)
619 FN(EL
,free
)(multi
->u
.p
[first
+ i
]);
620 for (i
= first
; i
+ n
< multi
->n
; ++i
)
621 multi
->u
.p
[i
] = multi
->u
.p
[i
+ n
];
623 if (n
> 0 && FN(MULTI(BASE
),has_explicit_domain
)(multi
))
624 multi
= FN(MULTI(BASE
),init_explicit_domain
)(multi
);
629 if (FN(MULTI(BASE
),has_explicit_domain
)(multi
))
630 multi
= FN(MULTI(BASE
),drop_explicit_domain_dims
)(multi
,
635 for (i
= 0; i
< multi
->n
; ++i
) {
636 multi
->u
.p
[i
] = FN(EL
,drop_dims
)(multi
->u
.p
[i
], type
, first
, n
);
638 return FN(MULTI(BASE
),free
)(multi
);
644 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
646 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params_multi_multi_and
)(
647 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
648 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi1
,
649 __isl_take
MULTI(BASE
) *multi2
))
652 isl_bool equal_params
;
654 if (!multi1
|| !multi2
)
656 equal_params
= isl_space_has_equal_params(multi1
->space
, multi2
->space
);
657 if (equal_params
< 0)
660 return fn(multi1
, multi2
);
661 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
662 if (!isl_space_has_named_params(multi1
->space
) ||
663 !isl_space_has_named_params(multi2
->space
))
664 isl_die(ctx
, isl_error_invalid
,
665 "unaligned unnamed parameters", goto error
);
666 multi1
= FN(MULTI(BASE
),align_params
)(multi1
,
667 FN(MULTI(BASE
),get_space
)(multi2
));
668 multi2
= FN(MULTI(BASE
),align_params
)(multi2
,
669 FN(MULTI(BASE
),get_space
)(multi1
));
670 return fn(multi1
, multi2
);
672 FN(MULTI(BASE
),free
)(multi1
);
673 FN(MULTI(BASE
),free
)(multi2
);
677 /* Given two MULTI(BASE)s A -> B and C -> D,
678 * construct a MULTI(BASE) (A * C) -> [B -> D].
680 * The parameters are assumed to have been aligned.
682 * If "multi1" and/or "multi2" has an explicit domain, then
683 * intersect the domain of the result with these explicit domains.
685 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product_aligned
)(
686 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
693 if (!multi1
|| !multi2
)
696 space
= isl_space_range_product(FN(MULTI(BASE
),get_space
)(multi1
),
697 FN(MULTI(BASE
),get_space
)(multi2
));
698 res
= FN(MULTI(BASE
),alloc
)(space
);
700 n1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
701 n2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
703 for (i
= 0; i
< n1
; ++i
) {
704 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
705 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
708 for (i
= 0; i
< n2
; ++i
) {
709 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
710 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, n1
+ i
, el
);
713 if (FN(MULTI(BASE
),has_explicit_domain
)(multi1
))
714 res
= FN(MULTI(BASE
),intersect_explicit_domain
)(res
, multi1
);
715 if (FN(MULTI(BASE
),has_explicit_domain
)(multi2
))
716 res
= FN(MULTI(BASE
),intersect_explicit_domain
)(res
, multi2
);
718 FN(MULTI(BASE
),free
)(multi1
);
719 FN(MULTI(BASE
),free
)(multi2
);
722 FN(MULTI(BASE
),free
)(multi1
);
723 FN(MULTI(BASE
),free
)(multi2
);
727 /* Given two MULTI(BASE)s A -> B and C -> D,
728 * construct a MULTI(BASE) (A * C) -> [B -> D].
730 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product
)(
731 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
733 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
734 &FN(MULTI(BASE
),range_product_aligned
));
737 /* Is the range of "multi" a wrapped relation?
739 isl_bool
FN(MULTI(BASE
),range_is_wrapping
)(__isl_keep
MULTI(BASE
) *multi
)
742 return isl_bool_error
;
743 return isl_space_range_is_wrapping(multi
->space
);
746 /* Given a function A -> [B -> C], extract the function A -> B.
748 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_domain
)(
749 __isl_take
MULTI(BASE
) *multi
)
756 if (!isl_space_range_is_wrapping(multi
->space
))
757 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
758 "range is not a product",
759 return FN(MULTI(BASE
),free
)(multi
));
761 space
= FN(MULTI(BASE
),get_space
)(multi
);
762 total
= isl_space_dim(space
, isl_dim_out
);
763 space
= isl_space_range_factor_domain(space
);
764 keep
= isl_space_dim(space
, isl_dim_out
);
765 multi
= FN(MULTI(BASE
),drop_dims
)(multi
,
766 isl_dim_out
, keep
, total
- keep
);
767 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
772 /* Given a function A -> [B -> C], extract the function A -> C.
774 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_factor_range
)(
775 __isl_take
MULTI(BASE
) *multi
)
782 if (!isl_space_range_is_wrapping(multi
->space
))
783 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
784 "range is not a product",
785 return FN(MULTI(BASE
),free
)(multi
));
787 space
= FN(MULTI(BASE
),get_space
)(multi
);
788 total
= isl_space_dim(space
, isl_dim_out
);
789 space
= isl_space_range_factor_range(space
);
790 keep
= isl_space_dim(space
, isl_dim_out
);
791 multi
= FN(MULTI(BASE
),drop_dims
)(multi
, isl_dim_out
, 0, total
- keep
);
792 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
797 /* Given a function [B -> C], extract the function C.
799 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),factor_range
)(
800 __isl_take
MULTI(BASE
) *multi
)
807 if (!isl_space_is_wrapping(multi
->space
))
808 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
809 "not a product", return FN(MULTI(BASE
),free
)(multi
));
811 space
= FN(MULTI(BASE
),get_space
)(multi
);
812 total
= isl_space_dim(space
, isl_dim_out
);
813 space
= isl_space_factor_range(space
);
814 keep
= isl_space_dim(space
, isl_dim_out
);
815 multi
= FN(MULTI(BASE
),drop_dims
)(multi
, isl_dim_out
, 0, total
- keep
);
816 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
821 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flatten_range
)(
822 __isl_take
MULTI(BASE
) *multi
)
827 if (!multi
->space
->nested
[1])
830 multi
= FN(MULTI(BASE
),cow
)(multi
);
834 multi
->space
= isl_space_flatten_range(multi
->space
);
836 return FN(MULTI(BASE
),free
)(multi
);
841 /* Given two MULTI(BASE)s A -> B and C -> D,
842 * construct a MULTI(BASE) (A * C) -> (B, D).
844 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flat_range_product
)(
845 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
849 multi
= FN(MULTI(BASE
),range_product
)(multi1
, multi2
);
850 multi
= FN(MULTI(BASE
),flatten_range
)(multi
);
854 /* Given two multi expressions, "multi1"
858 * where B2 starts at position "pos", and "multi2"
862 * return the multi expression
866 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_splice
)(
867 __isl_take
MULTI(BASE
) *multi1
, unsigned pos
,
868 __isl_take
MULTI(BASE
) *multi2
)
873 if (!multi1
|| !multi2
)
876 dim
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
878 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
879 "index out of bounds", goto error
);
881 res
= FN(MULTI(BASE
),copy
)(multi1
);
882 res
= FN(MULTI(BASE
),drop_dims
)(res
, isl_dim_out
, pos
, dim
- pos
);
883 multi1
= FN(MULTI(BASE
),drop_dims
)(multi1
, isl_dim_out
, 0, pos
);
885 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi2
);
886 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi1
);
890 FN(MULTI(BASE
),free
)(multi1
);
891 FN(MULTI(BASE
),free
)(multi2
);
895 /* Check that "multi1" and "multi2" live in the same space,
896 * reporting an error if they do not.
898 static isl_stat
FN(MULTI(BASE
),check_equal_space
)(
899 __isl_keep
MULTI(BASE
) *multi1
, __isl_keep
MULTI(BASE
) *multi2
)
903 if (!multi1
|| !multi2
)
904 return isl_stat_error
;
906 equal
= isl_space_is_equal(multi1
->space
, multi2
->space
);
908 return isl_stat_error
;
910 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
911 "spaces don't match", return isl_stat_error
);
916 /* This function is currently only used from isl_aff.c
918 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
919 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
920 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
921 __attribute__ ((unused
));
923 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
926 * If "multi2" has an explicit domain, then
927 * intersect the domain of the result with this explicit domain.
929 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
930 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
931 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
935 multi1
= FN(MULTI(BASE
),cow
)(multi1
);
936 if (FN(MULTI(BASE
),check_equal_space
)(multi1
, multi2
) < 0)
939 for (i
= 0; i
< multi1
->n
; ++i
) {
940 multi1
->u
.p
[i
] = fn(multi1
->u
.p
[i
],
941 FN(EL
,copy
)(multi2
->u
.p
[i
]));
946 if (FN(MULTI(BASE
),has_explicit_domain
)(multi2
))
947 multi1
= FN(MULTI(BASE
),intersect_explicit_domain
)(multi1
,
950 FN(MULTI(BASE
),free
)(multi2
);
953 FN(MULTI(BASE
),free
)(multi1
);
954 FN(MULTI(BASE
),free
)(multi2
);
958 /* Add "multi2" to "multi1" and return the result.
960 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
962 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),add_aligned
)(
963 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
965 return FN(MULTI(BASE
),bin_op
)(multi1
, multi2
, &FN(EL
,add
));
968 /* Add "multi2" to "multi1" and return the result.
970 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),add
)(__isl_take
MULTI(BASE
) *multi1
,
971 __isl_take
MULTI(BASE
) *multi2
)
973 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
974 &FN(MULTI(BASE
),add_aligned
));
977 /* Subtract "multi2" from "multi1" and return the result.
979 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
981 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),sub_aligned
)(
982 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
984 return FN(MULTI(BASE
),bin_op
)(multi1
, multi2
, &FN(EL
,sub
));
987 /* Subtract "multi2" from "multi1" and return the result.
989 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),sub
)(__isl_take
MULTI(BASE
) *multi1
,
990 __isl_take
MULTI(BASE
) *multi2
)
992 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
993 &FN(MULTI(BASE
),sub_aligned
));
996 /* Multiply the elements of "multi" by "v" and return the result.
998 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_val
)(__isl_take
MULTI(BASE
) *multi
,
999 __isl_take isl_val
*v
)
1006 if (isl_val_is_one(v
)) {
1011 if (!isl_val_is_rat(v
))
1012 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1013 "expecting rational factor", goto error
);
1015 multi
= FN(MULTI(BASE
),cow
)(multi
);
1019 for (i
= 0; i
< multi
->n
; ++i
) {
1020 multi
->u
.p
[i
] = FN(EL
,scale_val
)(multi
->u
.p
[i
],
1030 return FN(MULTI(BASE
),free
)(multi
);
1033 /* Divide the elements of "multi" by "v" and return the result.
1035 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_down_val
)(
1036 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_val
*v
)
1043 if (isl_val_is_one(v
)) {
1048 if (!isl_val_is_rat(v
))
1049 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1050 "expecting rational factor", goto error
);
1051 if (isl_val_is_zero(v
))
1052 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
1053 "cannot scale down by zero", goto error
);
1055 multi
= FN(MULTI(BASE
),cow
)(multi
);
1059 for (i
= 0; i
< multi
->n
; ++i
) {
1060 multi
->u
.p
[i
] = FN(EL
,scale_down_val
)(multi
->u
.p
[i
],
1070 return FN(MULTI(BASE
),free
)(multi
);
1073 /* Multiply the elements of "multi" by the corresponding element of "mv"
1074 * and return the result.
1076 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_multi_val
)(
1077 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1084 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1085 mv
->space
, isl_dim_set
))
1086 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1087 "spaces don't match", goto error
);
1089 multi
= FN(MULTI(BASE
),cow
)(multi
);
1093 for (i
= 0; i
< multi
->n
; ++i
) {
1096 v
= isl_multi_val_get_val(mv
, i
);
1097 multi
->u
.p
[i
] = FN(EL
,scale_val
)(multi
->u
.p
[i
], v
);
1102 isl_multi_val_free(mv
);
1105 isl_multi_val_free(mv
);
1106 return FN(MULTI(BASE
),free
)(multi
);
1109 /* Divide the elements of "multi" by the corresponding element of "mv"
1110 * and return the result.
1112 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_down_multi_val
)(
1113 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1120 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1121 mv
->space
, isl_dim_set
))
1122 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1123 "spaces don't match", goto error
);
1125 multi
= FN(MULTI(BASE
),cow
)(multi
);
1129 for (i
= 0; i
< multi
->n
; ++i
) {
1132 v
= isl_multi_val_get_val(mv
, i
);
1133 multi
->u
.p
[i
] = FN(EL
,scale_down_val
)(multi
->u
.p
[i
], v
);
1138 isl_multi_val_free(mv
);
1141 isl_multi_val_free(mv
);
1142 return FN(MULTI(BASE
),free
)(multi
);
1145 /* Compute the residues of the elements of "multi" modulo
1146 * the corresponding element of "mv" and return the result.
1148 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),mod_multi_val
)(
1149 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
1156 if (!isl_space_tuple_is_equal(multi
->space
, isl_dim_out
,
1157 mv
->space
, isl_dim_set
))
1158 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
1159 "spaces don't match", goto error
);
1161 multi
= FN(MULTI(BASE
),cow
)(multi
);
1165 for (i
= 0; i
< multi
->n
; ++i
) {
1168 v
= isl_multi_val_get_val(mv
, i
);
1169 multi
->u
.p
[i
] = FN(EL
,mod_val
)(multi
->u
.p
[i
], v
);
1174 isl_multi_val_free(mv
);
1177 isl_multi_val_free(mv
);
1178 return FN(MULTI(BASE
),free
)(multi
);
1181 /* Convert a multiple expression defined over a parameter domain
1182 * into one that is defined over a zero-dimensional set.
1184 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),from_range
)(
1185 __isl_take
MULTI(BASE
) *multi
)
1191 if (!isl_space_is_set(multi
->space
))
1192 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1193 "not living in a set space",
1194 return FN(MULTI(BASE
),free
)(multi
));
1196 space
= FN(MULTI(BASE
),get_space
)(multi
);
1197 space
= isl_space_from_range(space
);
1198 multi
= FN(MULTI(BASE
),reset_space
)(multi
, space
);
1203 /* Are "multi1" and "multi2" obviously equal?
1205 isl_bool
FN(MULTI(BASE
),plain_is_equal
)(__isl_keep
MULTI(BASE
) *multi1
,
1206 __isl_keep
MULTI(BASE
) *multi2
)
1211 if (!multi1
|| !multi2
)
1212 return isl_bool_error
;
1213 if (multi1
->n
!= multi2
->n
)
1214 return isl_bool_false
;
1215 equal
= isl_space_is_equal(multi1
->space
, multi2
->space
);
1216 if (equal
< 0 || !equal
)
1219 for (i
= 0; i
< multi1
->n
; ++i
) {
1220 equal
= FN(EL
,plain_is_equal
)(multi1
->u
.p
[i
], multi2
->u
.p
[i
]);
1221 if (equal
< 0 || !equal
)
1225 if (FN(MULTI(BASE
),has_explicit_domain
)(multi1
) ||
1226 FN(MULTI(BASE
),has_explicit_domain
)(multi2
)) {
1227 equal
= FN(MULTI(BASE
),equal_explicit_domain
)(multi1
, multi2
);
1228 if (equal
< 0 || !equal
)
1232 return isl_bool_true
;
1235 /* Does "multi" involve any NaNs?
1237 isl_bool
FN(MULTI(BASE
),involves_nan
)(__isl_keep
MULTI(BASE
) *multi
)
1242 return isl_bool_error
;
1244 return isl_bool_false
;
1246 for (i
= 0; i
< multi
->n
; ++i
) {
1247 isl_bool has_nan
= FN(EL
,involves_nan
)(multi
->u
.p
[i
]);
1248 if (has_nan
< 0 || has_nan
)
1252 return isl_bool_false
;
1255 /* Return the opposite of "multi".
1257 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),neg
)(__isl_take
MULTI(BASE
) *multi
)
1261 multi
= FN(MULTI(BASE
),cow
)(multi
);
1265 for (i
= 0; i
< multi
->n
; ++i
) {
1266 multi
->u
.p
[i
] = FN(EL
,neg
)(multi
->u
.p
[i
]);
1268 return FN(MULTI(BASE
),free
)(multi
);