2 * Copyright 2011 Sven Verdoolaege
3 * Copyright 2012-2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
11 #include <isl_space_private.h>
13 #include <isl_reordering.h>
15 #define xCAT(A,B) A ## B
16 #define CAT(A,B) xCAT(A,B)
18 #define EL CAT(isl_,BASE)
19 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
20 #define FN(TYPE,NAME) xFN(TYPE,NAME)
21 #define xMULTI(BASE) isl_multi_ ## BASE
22 #define MULTI(BASE) xMULTI(BASE)
23 #define MULTI_NAME(BASE) "isl_multi_" #BASE
24 #define xLIST(EL) EL ## _list
25 #define LIST(EL) xLIST(EL)
27 isl_ctx
*FN(MULTI(BASE
),get_ctx
)(__isl_keep
MULTI(BASE
) *multi
)
29 return multi
? isl_space_get_ctx(multi
->space
) : NULL
;
32 __isl_give isl_space
*FN(MULTI(BASE
),get_space
)(__isl_keep
MULTI(BASE
) *multi
)
34 return multi
? isl_space_copy(multi
->space
) : NULL
;
37 __isl_give isl_space
*FN(MULTI(BASE
),get_domain_space
)(
38 __isl_keep
MULTI(BASE
) *multi
)
40 return multi
? isl_space_domain(isl_space_copy(multi
->space
)) : NULL
;
43 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),alloc
)(__isl_take isl_space
*space
)
52 ctx
= isl_space_get_ctx(space
);
53 n
= isl_space_dim(space
, isl_dim_out
);
54 multi
= isl_calloc(ctx
, MULTI(BASE
),
55 sizeof(MULTI(BASE
)) + (n
- 1) * sizeof(struct EL
*));
64 isl_space_free(space
);
68 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),dup
)(__isl_keep
MULTI(BASE
) *multi
)
76 dup
= FN(MULTI(BASE
),alloc
)(isl_space_copy(multi
->space
));
80 for (i
= 0; i
< multi
->n
; ++i
)
81 dup
= FN(FN(MULTI(BASE
),set
),BASE
)(dup
, i
,
82 FN(EL
,copy
)(multi
->p
[i
]));
87 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),cow
)(__isl_take
MULTI(BASE
) *multi
)
96 return FN(MULTI(BASE
),dup
)(multi
);
99 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),copy
)(__isl_keep
MULTI(BASE
) *multi
)
108 void *FN(MULTI(BASE
),free
)(__isl_take
MULTI(BASE
) *multi
)
115 if (--multi
->ref
> 0)
118 isl_space_free(multi
->space
);
119 for (i
= 0; i
< multi
->n
; ++i
)
120 FN(EL
,free
)(multi
->p
[i
]);
126 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),insert_dims
)(
127 __isl_take
MULTI(BASE
) *multi
,
128 enum isl_dim_type type
, unsigned first
, unsigned n
)
134 if (type
== isl_dim_out
)
135 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
136 "cannot insert output/set dimensions",
137 return FN(MULTI(BASE
),free
)(multi
));
138 if (n
== 0 && !isl_space_is_named_or_nested(multi
->space
, type
))
141 multi
= FN(MULTI(BASE
),cow
)(multi
);
145 multi
->space
= isl_space_insert_dims(multi
->space
, type
, first
, n
);
147 return FN(MULTI(BASE
),free
)(multi
);
149 for (i
= 0; i
< multi
->n
; ++i
) {
150 multi
->p
[i
] = FN(EL
,insert_dims
)(multi
->p
[i
], type
, first
, n
);
152 return FN(MULTI(BASE
),free
)(multi
);
158 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),add_dims
)(__isl_take
MULTI(BASE
) *multi
,
159 enum isl_dim_type type
, unsigned n
)
163 pos
= FN(MULTI(BASE
),dim
)(multi
, type
);
165 return FN(MULTI(BASE
),insert_dims
)(multi
, type
, pos
, n
);
168 unsigned FN(MULTI(BASE
),dim
)(__isl_keep
MULTI(BASE
) *multi
,
169 enum isl_dim_type type
)
171 return multi
? isl_space_dim(multi
->space
, type
) : 0;
174 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_dim_name
)(
175 __isl_take
MULTI(BASE
) *multi
,
176 enum isl_dim_type type
, unsigned pos
, const char *s
)
180 multi
= FN(MULTI(BASE
),cow
)(multi
);
184 multi
->space
= isl_space_set_dim_name(multi
->space
, type
, pos
, s
);
186 return FN(MULTI(BASE
),free
)(multi
);
188 if (type
== isl_dim_out
)
190 for (i
= 0; i
< multi
->n
; ++i
) {
191 multi
->p
[i
] = FN(EL
,set_dim_name
)(multi
->p
[i
], type
, pos
, s
);
193 return FN(MULTI(BASE
),free
)(multi
);
199 const char *FN(MULTI(BASE
),get_tuple_name
)(__isl_keep
MULTI(BASE
) *multi
,
200 enum isl_dim_type type
)
202 return multi
? isl_space_get_tuple_name(multi
->space
, type
) : NULL
;
205 /* Does the specified tuple have an id?
207 int FN(MULTI(BASE
),has_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
208 enum isl_dim_type type
)
210 return multi
? isl_space_has_tuple_id(multi
->space
, type
) : -1;
213 /* Return the id of the specified tuple.
215 __isl_give isl_id
*FN(MULTI(BASE
),get_tuple_id
)(__isl_keep
MULTI(BASE
) *multi
,
216 enum isl_dim_type type
)
218 return multi
? isl_space_get_tuple_id(multi
->space
, type
) : NULL
;
221 __isl_give EL
*FN(FN(MULTI(BASE
),get
),BASE
)(__isl_keep
MULTI(BASE
) *multi
,
228 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
229 if (pos
< 0 || pos
>= multi
->n
)
230 isl_die(ctx
, isl_error_invalid
,
231 "index out of bounds", return NULL
);
232 return FN(EL
,copy
)(multi
->p
[pos
]);
235 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),set
),BASE
)(
236 __isl_take
MULTI(BASE
) *multi
, int pos
, __isl_take EL
*el
)
238 isl_space
*multi_space
= NULL
;
239 isl_space
*el_space
= NULL
;
242 multi
= FN(MULTI(BASE
),cow
)(multi
);
246 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
247 match
= FN(EL
,matching_params
)(el
, multi_space
);
251 multi
= FN(MULTI(BASE
),align_params
)(multi
,
252 FN(EL
,get_space
)(el
));
253 isl_space_free(multi_space
);
254 multi_space
= FN(MULTI(BASE
),get_space
)(multi
);
255 el
= FN(EL
,align_params
)(el
, isl_space_copy(multi_space
));
257 if (FN(EL
,check_match_domain_space
)(el
, multi_space
) < 0)
260 if (pos
< 0 || pos
>= multi
->n
)
261 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
262 "index out of bounds", goto error
);
264 FN(EL
,free
)(multi
->p
[pos
]);
267 isl_space_free(multi_space
);
268 isl_space_free(el_space
);
272 FN(MULTI(BASE
),free
)(multi
);
274 isl_space_free(multi_space
);
275 isl_space_free(el_space
);
279 /* Reset the space of "multi". This function is called from isl_pw_templ.c
280 * and doesn't know if the space of an element object is represented
281 * directly or through its domain. It therefore passes along both,
282 * which we pass along to the element function since we don't how
283 * that is represented either.
285 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space_and_domain
)(
286 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
,
287 __isl_take isl_space
*domain
)
291 multi
= FN(MULTI(BASE
),cow
)(multi
);
292 if (!multi
|| !space
|| !domain
)
295 for (i
= 0; i
< multi
->n
; ++i
) {
296 multi
->p
[i
] = FN(EL
,reset_domain_space
)(multi
->p
[i
],
297 isl_space_copy(domain
));
301 isl_space_free(domain
);
302 isl_space_free(multi
->space
);
303 multi
->space
= space
;
307 isl_space_free(domain
);
308 isl_space_free(space
);
309 FN(MULTI(BASE
),free
)(multi
);
313 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_domain_space
)(
314 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*domain
)
318 space
= isl_space_extend_domain_with_range(isl_space_copy(domain
),
319 isl_space_copy(multi
->space
));
320 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
323 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_space
)(
324 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*space
)
328 domain
= isl_space_domain(isl_space_copy(space
));
329 return FN(MULTI(BASE
),reset_space_and_domain
)(multi
, space
, domain
);
332 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_name
)(
333 __isl_keep
MULTI(BASE
) *multi
, enum isl_dim_type type
,
338 multi
= FN(MULTI(BASE
),cow
)(multi
);
342 space
= FN(MULTI(BASE
),get_space
)(multi
);
343 space
= isl_space_set_tuple_name(space
, type
, s
);
345 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
348 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),set_tuple_id
)(
349 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
,
350 __isl_take isl_id
*id
)
354 multi
= FN(MULTI(BASE
),cow
)(multi
);
356 return isl_id_free(id
);
358 space
= FN(MULTI(BASE
),get_space
)(multi
);
359 space
= isl_space_set_tuple_id(space
, type
, id
);
361 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
364 /* Drop the id on the specified tuple.
366 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),reset_tuple_id
)(
367 __isl_take
MULTI(BASE
) *multi
, enum isl_dim_type type
)
373 if (!FN(MULTI(BASE
),has_tuple_id
)(multi
, type
))
376 multi
= FN(MULTI(BASE
),cow
)(multi
);
380 space
= FN(MULTI(BASE
),get_space
)(multi
);
381 space
= isl_space_reset_tuple_id(space
, type
);
383 return FN(MULTI(BASE
),reset_space
)(multi
, space
);
386 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),realign_domain
)(
387 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_reordering
*exp
)
391 multi
= FN(MULTI(BASE
),cow
)(multi
);
395 for (i
= 0; i
< multi
->n
; ++i
) {
396 multi
->p
[i
] = FN(EL
,realign_domain
)(multi
->p
[i
],
397 isl_reordering_copy(exp
));
402 multi
= FN(MULTI(BASE
),reset_domain_space
)(multi
,
403 isl_space_copy(exp
->dim
));
405 isl_reordering_free(exp
);
408 isl_reordering_free(exp
);
409 FN(MULTI(BASE
),free
)(multi
);
413 /* Align the parameters of "multi" to those of "model".
415 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params
)(
416 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_space
*model
)
420 if (!multi
|| !model
)
423 ctx
= isl_space_get_ctx(model
);
424 if (!isl_space_has_named_params(model
))
425 isl_die(ctx
, isl_error_invalid
,
426 "model has unnamed parameters", goto error
);
427 if (!isl_space_has_named_params(multi
->space
))
428 isl_die(ctx
, isl_error_invalid
,
429 "input has unnamed parameters", goto error
);
430 if (!isl_space_match(multi
->space
, isl_dim_param
,
431 model
, isl_dim_param
)) {
434 model
= isl_space_params(model
);
435 exp
= isl_parameter_alignment_reordering(multi
->space
, model
);
436 exp
= isl_reordering_extend_space(exp
,
437 FN(MULTI(BASE
),get_domain_space
)(multi
));
438 multi
= FN(MULTI(BASE
),realign_domain
)(multi
, exp
);
441 isl_space_free(model
);
444 isl_space_free(model
);
445 FN(MULTI(BASE
),free
)(multi
);
450 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params_multi_set_and
)(
451 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*set
,
452 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi
,
453 __isl_take isl_set
*set
))
459 if (isl_space_match(multi
->space
, isl_dim_param
,
460 set
->dim
, isl_dim_param
))
461 return fn(multi
, set
);
462 ctx
= FN(MULTI(BASE
),get_ctx
)(multi
);
463 if (!isl_space_has_named_params(multi
->space
) ||
464 !isl_space_has_named_params(set
->dim
))
465 isl_die(ctx
, isl_error_invalid
,
466 "unaligned unnamed parameters", goto error
);
467 multi
= FN(MULTI(BASE
),align_params
)(multi
, isl_set_get_space(set
));
468 set
= isl_set_align_params(set
, FN(MULTI(BASE
),get_space
)(multi
));
469 return fn(multi
, set
);
471 FN(MULTI(BASE
),free
)(multi
);
476 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist_aligned
)(
477 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*context
)
481 multi
= FN(MULTI(BASE
),cow
)(multi
);
482 if (!multi
|| !context
)
485 for (i
= 0; i
< multi
->n
; ++i
) {
486 multi
->p
[i
] = FN(EL
,gist
)(multi
->p
[i
], isl_set_copy(context
));
491 isl_set_free(context
);
494 isl_set_free(context
);
495 FN(MULTI(BASE
),free
)(multi
);
499 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist
)(__isl_take
MULTI(BASE
) *multi
,
500 __isl_take isl_set
*context
)
502 return FN(MULTI(BASE
),align_params_multi_set_and
)(multi
, context
,
503 &FN(MULTI(BASE
),gist_aligned
));
506 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),gist_params
)(
507 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_set
*context
)
509 isl_space
*space
= FN(MULTI(BASE
),get_domain_space
)(multi
);
510 isl_set
*dom_context
= isl_set_universe(space
);
511 dom_context
= isl_set_intersect_params(dom_context
, context
);
512 return FN(MULTI(BASE
),gist
)(multi
, dom_context
);
516 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),LIST(BASE
))(
517 __isl_take isl_space
*space
, __isl_take
LIST(EL
) *list
)
527 ctx
= isl_space_get_ctx(space
);
528 n
= FN(FN(LIST(EL
),n
),BASE
)(list
);
529 if (n
!= isl_space_dim(space
, isl_dim_out
))
530 isl_die(ctx
, isl_error_invalid
,
531 "invalid number of elements in list", goto error
);
533 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
534 for (i
= 0; i
< n
; ++i
) {
535 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
536 FN(FN(LIST(EL
),get
),BASE
)(list
, i
));
539 isl_space_free(space
);
540 FN(LIST(EL
),free
)(list
);
543 isl_space_free(space
);
544 FN(LIST(EL
),free
)(list
);
549 /* Create a multi expression in the given space that maps each
550 * input dimension to the corresponding output dimension.
552 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),identity
)(__isl_take isl_space
*space
)
561 if (isl_space_is_set(space
))
562 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
563 "expecting map space", goto error
);
565 n
= isl_space_dim(space
, isl_dim_out
);
566 if (n
!= isl_space_dim(space
, isl_dim_in
))
567 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
568 "number of input and output dimensions needs to be "
569 "the same", goto error
);
571 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
574 isl_space_free(space
);
578 space
= isl_space_domain(space
);
579 ls
= isl_local_space_from_space(space
);
581 for (i
= 0; i
< n
; ++i
) {
583 el
= FN(EL
,var_on_domain
)(isl_local_space_copy(ls
),
585 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
, el
);
588 isl_local_space_free(ls
);
592 isl_space_free(space
);
597 /* Construct a multi expression in the given space with value zero in
598 * each of the output dimensions.
600 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),zero
)(__isl_take isl_space
*space
)
608 n
= isl_space_dim(space
, isl_dim_out
);
609 multi
= FN(MULTI(BASE
),alloc
)(isl_space_copy(space
));
612 isl_space_free(space
);
618 space
= isl_space_domain(space
);
619 ls
= isl_local_space_from_space(space
);
620 el
= FN(EL
,zero_on_domain
)(ls
);
622 for (i
= 0; i
< n
; ++i
)
623 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, i
,
633 __isl_give
MULTI(BASE
) *FN(FN(MULTI(BASE
),from
),BASE
)(__isl_take EL
*el
)
637 multi
= FN(MULTI(BASE
),alloc
)(FN(EL
,get_space
)(el
));
638 multi
= FN(FN(MULTI(BASE
),set
),BASE
)(multi
, 0, el
);
644 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),drop_dims
)(
645 __isl_take
MULTI(BASE
) *multi
,
646 enum isl_dim_type type
, unsigned first
, unsigned n
)
651 multi
= FN(MULTI(BASE
),cow
)(multi
);
655 dim
= FN(MULTI(BASE
),dim
)(multi
, type
);
656 if (first
+ n
> dim
|| first
+ n
< first
)
657 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
658 "index out of bounds",
659 return FN(MULTI(BASE
),cow
)(multi
));
661 multi
->space
= isl_space_drop_dims(multi
->space
, type
, first
, n
);
663 return FN(MULTI(BASE
),cow
)(multi
);
665 if (type
== isl_dim_out
) {
666 for (i
= 0; i
< n
; ++i
)
667 FN(EL
,free
)(multi
->p
[first
+ i
]);
668 for (i
= first
; i
+ n
< multi
->n
; ++i
)
669 multi
->p
[i
] = multi
->p
[i
+ n
];
675 for (i
= 0; i
< multi
->n
; ++i
) {
676 multi
->p
[i
] = FN(EL
,drop_dims
)(multi
->p
[i
], type
, first
, n
);
678 return FN(MULTI(BASE
),cow
)(multi
);
684 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
686 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),align_params_multi_multi_and
)(
687 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
688 __isl_give
MULTI(BASE
) *(*fn
)(__isl_take
MULTI(BASE
) *multi1
,
689 __isl_take
MULTI(BASE
) *multi2
))
693 if (!multi1
|| !multi2
)
695 if (isl_space_match(multi1
->space
, isl_dim_param
,
696 multi2
->space
, isl_dim_param
))
697 return fn(multi1
, multi2
);
698 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
699 if (!isl_space_has_named_params(multi1
->space
) ||
700 !isl_space_has_named_params(multi2
->space
))
701 isl_die(ctx
, isl_error_invalid
,
702 "unaligned unnamed parameters", goto error
);
703 multi1
= FN(MULTI(BASE
),align_params
)(multi1
,
704 FN(MULTI(BASE
),get_space
)(multi2
));
705 multi2
= FN(MULTI(BASE
),align_params
)(multi2
,
706 FN(MULTI(BASE
),get_space
)(multi1
));
707 return fn(multi1
, multi2
);
709 FN(MULTI(BASE
),free
)(multi1
);
710 FN(MULTI(BASE
),free
)(multi2
);
714 /* Given two MULTI(BASE)s A -> B and C -> D,
715 * construct a MULTI(BASE) (A * C) -> (B, D).
717 * The parameters are assumed to have been aligned.
719 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product_aligned
)(
720 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
727 if (!multi1
|| !multi2
)
730 space
= isl_space_range_product(FN(MULTI(BASE
),get_space
)(multi1
),
731 FN(MULTI(BASE
),get_space
)(multi2
));
732 res
= FN(MULTI(BASE
),alloc
)(space
);
734 n1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
735 n2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_out
);
737 for (i
= 0; i
< n1
; ++i
) {
738 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi1
, i
);
739 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, i
, el
);
742 for (i
= 0; i
< n2
; ++i
) {
743 el
= FN(FN(MULTI(BASE
),get
),BASE
)(multi2
, i
);
744 res
= FN(FN(MULTI(BASE
),set
),BASE
)(res
, n1
+ i
, el
);
747 FN(MULTI(BASE
),free
)(multi1
);
748 FN(MULTI(BASE
),free
)(multi2
);
751 FN(MULTI(BASE
),free
)(multi1
);
752 FN(MULTI(BASE
),free
)(multi2
);
756 /* Given two MULTI(BASE)s A -> B and C -> D,
757 * construct a MULTI(BASE) (A * C) -> (B, D).
759 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_product
)(
760 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
762 return FN(MULTI(BASE
),align_params_multi_multi_and
)(multi1
, multi2
,
763 &FN(MULTI(BASE
),range_product_aligned
));
766 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flatten_range
)(
767 __isl_take
MULTI(BASE
) *multi
)
772 if (!multi
->space
->nested
[1])
775 multi
= FN(MULTI(BASE
),cow
)(multi
);
779 multi
->space
= isl_space_flatten_range(multi
->space
);
781 return FN(MULTI(BASE
),free
)(multi
);
786 /* Given two MULTI(BASE)s A -> B and C -> D,
787 * construct a MULTI(BASE) (A * C) -> [B -> D].
789 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),flat_range_product
)(
790 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
)
794 multi
= FN(MULTI(BASE
),range_product
)(multi1
, multi2
);
795 multi
= FN(MULTI(BASE
),flatten_range
)(multi
);
799 /* Given two multi expressions, "multi1"
803 * where B2 starts at position "pos", and "multi2"
807 * return the multi expression
811 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),range_splice
)(
812 __isl_take
MULTI(BASE
) *multi1
, unsigned pos
,
813 __isl_take
MULTI(BASE
) *multi2
)
818 if (!multi1
|| !multi2
)
821 dim
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_out
);
823 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
824 "index out of bounds", goto error
);
826 res
= FN(MULTI(BASE
),copy
)(multi1
);
827 res
= FN(MULTI(BASE
),drop_dims
)(res
, isl_dim_out
, pos
, dim
- pos
);
828 multi1
= FN(MULTI(BASE
),drop_dims
)(multi1
, isl_dim_out
, 0, pos
);
830 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi2
);
831 res
= FN(MULTI(BASE
),flat_range_product
)(res
, multi1
);
835 FN(MULTI(BASE
),free
)(multi1
);
836 FN(MULTI(BASE
),free
)(multi2
);
840 /* Given two multi expressions, "multi1"
844 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
849 * return the multi expression
851 * [A1 C A2] -> [B1 D B2]
853 * We first insert input dimensions to obtain
855 * [A1 C A2] -> [B1 B2]
861 * and then apply range_splice.
863 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),splice
)(
864 __isl_take
MULTI(BASE
) *multi1
, unsigned in_pos
, unsigned out_pos
,
865 __isl_take
MULTI(BASE
) *multi2
)
870 if (!multi1
|| !multi2
)
873 n_in1
= FN(MULTI(BASE
),dim
)(multi1
, isl_dim_in
);
875 isl_die(FN(MULTI(BASE
),get_ctx
)(multi1
), isl_error_invalid
,
876 "index out of bounds", goto error
);
878 n_in2
= FN(MULTI(BASE
),dim
)(multi2
, isl_dim_in
);
880 multi1
= FN(MULTI(BASE
),insert_dims
)(multi1
, isl_dim_in
, in_pos
, n_in2
);
881 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, n_in2
,
883 multi2
= FN(MULTI(BASE
),insert_dims
)(multi2
, isl_dim_in
, 0, in_pos
);
885 return FN(MULTI(BASE
),range_splice
)(multi1
, out_pos
, multi2
);
887 FN(MULTI(BASE
),free
)(multi1
);
888 FN(MULTI(BASE
),free
)(multi2
);
892 /* This function is currently only used from isl_aff.c
894 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
895 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
896 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
897 __attribute__ ((unused
));
899 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
902 static __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),bin_op
)(
903 __isl_take
MULTI(BASE
) *multi1
, __isl_take
MULTI(BASE
) *multi2
,
904 __isl_give EL
*(*fn
)(__isl_take EL
*, __isl_take EL
*))
909 multi1
= FN(MULTI(BASE
),cow
)(multi1
);
910 if (!multi1
|| !multi2
)
913 ctx
= FN(MULTI(BASE
),get_ctx
)(multi1
);
914 if (!isl_space_is_equal(multi1
->space
, multi2
->space
))
915 isl_die(ctx
, isl_error_invalid
,
916 "spaces don't match", goto error
);
918 for (i
= 0; i
< multi1
->n
; ++i
) {
919 multi1
->p
[i
] = fn(multi1
->p
[i
], FN(EL
,copy
)(multi2
->p
[i
]));
924 FN(MULTI(BASE
),free
)(multi2
);
927 FN(MULTI(BASE
),free
)(multi1
);
928 FN(MULTI(BASE
),free
)(multi2
);
932 /* Multiply the elements of "multi" by "v" and return the result.
934 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_val
)(__isl_take
MULTI(BASE
) *multi
,
935 __isl_take isl_val
*v
)
942 if (isl_val_is_one(v
)) {
947 if (!isl_val_is_rat(v
))
948 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
949 "expecting rational factor", goto error
);
951 multi
= FN(MULTI(BASE
),cow
)(multi
);
955 for (i
= 0; i
< multi
->n
; ++i
) {
956 multi
->p
[i
] = FN(EL
,scale_val
)(multi
->p
[i
], isl_val_copy(v
));
965 return FN(MULTI(BASE
),free
)(multi
);
968 /* Multiply the elements of "multi" by the corresponding element of "mv"
969 * and return the result.
971 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),scale_multi_val
)(
972 __isl_take
MULTI(BASE
) *multi
, __isl_take isl_multi_val
*mv
)
979 if (!isl_space_tuple_match(multi
->space
, isl_dim_out
,
980 mv
->space
, isl_dim_set
))
981 isl_die(isl_multi_val_get_ctx(mv
), isl_error_invalid
,
982 "spaces don't match", goto error
);
984 multi
= FN(MULTI(BASE
),cow
)(multi
);
988 for (i
= 0; i
< multi
->n
; ++i
) {
991 v
= isl_multi_val_get_val(mv
, i
);
992 multi
->p
[i
] = FN(EL
,scale_val
)(multi
->p
[i
], v
);
997 isl_multi_val_free(mv
);
1000 isl_multi_val_free(mv
);
1001 return FN(MULTI(BASE
),free
)(multi
);
1004 #ifndef NO_MOVE_DIMS
1005 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1006 * to dimensions of "dst_type" at "dst_pos".
1008 * We only support moving input dimensions to parameters and vice versa.
1010 __isl_give
MULTI(BASE
) *FN(MULTI(BASE
),move_dims
)(__isl_take
MULTI(BASE
) *multi
,
1011 enum isl_dim_type dst_type
, unsigned dst_pos
,
1012 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1020 !isl_space_is_named_or_nested(multi
->space
, src_type
) &&
1021 !isl_space_is_named_or_nested(multi
->space
, dst_type
))
1024 if (dst_type
== isl_dim_out
|| src_type
== isl_dim_out
)
1025 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1026 "cannot move output/set dimension",
1027 return FN(MULTI(BASE
),free
)(multi
));
1028 if (dst_type
== isl_dim_div
|| src_type
== isl_dim_div
)
1029 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1031 return FN(MULTI(BASE
),free
)(multi
));
1032 if (src_pos
+ n
> isl_space_dim(multi
->space
, src_type
))
1033 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_invalid
,
1034 "range out of bounds",
1035 return FN(MULTI(BASE
),free
)(multi
));
1036 if (dst_type
== src_type
)
1037 isl_die(FN(MULTI(BASE
),get_ctx
)(multi
), isl_error_unsupported
,
1038 "moving dims within the same type not supported",
1039 return FN(MULTI(BASE
),free
)(multi
));
1041 multi
= FN(MULTI(BASE
),cow
)(multi
);
1045 multi
->space
= isl_space_move_dims(multi
->space
, dst_type
, dst_pos
,
1046 src_type
, src_pos
, n
);
1048 return FN(MULTI(BASE
),free
)(multi
);
1050 for (i
= 0; i
< multi
->n
; ++i
) {
1051 multi
->p
[i
] = FN(EL
,move_dims
)(multi
->p
[i
], dst_type
, dst_pos
,
1052 src_type
, src_pos
, n
);
1054 return FN(MULTI(BASE
),free
)(multi
);