hide isl_map_add_basic_map
[isl.git] / isl_multi_templ.c
blobb7efbc1bece3e3b395a47e2aa9b099a221bd81de
1 /*
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
9 */
11 #include <isl_space_private.h>
12 #include <isl/set.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
32 * in "multi".
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)
38 if (!multi)
39 return -1;
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)
51 isl_ctx *ctx;
52 int n;
53 MULTI(BASE) *multi;
55 if (!space)
56 return NULL;
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 *));
62 if (!multi)
63 goto error;
65 multi->space = space;
66 multi->n = n;
67 multi->ref = 1;
68 return multi;
69 error:
70 isl_space_free(space);
71 return NULL;
74 __isl_give MULTI(BASE) *FN(MULTI(BASE),dup)(__isl_keep MULTI(BASE) *multi)
76 int i;
77 MULTI(BASE) *dup;
79 if (!multi)
80 return NULL;
82 dup = FN(MULTI(BASE),alloc)(isl_space_copy(multi->space));
83 if (!dup)
84 return NULL;
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]));
90 return dup;
93 __isl_give MULTI(BASE) *FN(MULTI(BASE),cow)(__isl_take MULTI(BASE) *multi)
95 if (!multi)
96 return NULL;
98 if (multi->ref == 1)
99 return multi;
101 multi->ref--;
102 return FN(MULTI(BASE),dup)(multi);
105 __isl_give MULTI(BASE) *FN(MULTI(BASE),copy)(__isl_keep MULTI(BASE) *multi)
107 if (!multi)
108 return NULL;
110 multi->ref++;
111 return multi;
114 __isl_null MULTI(BASE) *FN(MULTI(BASE),free)(__isl_take MULTI(BASE) *multi)
116 int i;
118 if (!multi)
119 return NULL;
121 if (--multi->ref > 0)
122 return NULL;
124 isl_space_free(multi->space);
125 for (i = 0; i < multi->n; ++i)
126 FN(EL,free)(multi->p[i]);
127 free(multi);
129 return NULL;
132 #ifndef NO_DIMS
133 /* Check whether "multi" has non-zero coefficients for any dimension
134 * in the given range or if any of these dimensions appear
135 * with non-zero coefficients in any of the integer divisions involved.
137 isl_bool FN(MULTI(BASE),involves_dims)(__isl_keep MULTI(BASE) *multi,
138 enum isl_dim_type type, unsigned first, unsigned n)
140 int i;
142 if (!multi)
143 return isl_bool_error;
144 if (multi->n == 0 || n == 0)
145 return isl_bool_false;
147 for (i = 0; i < multi->n; ++i) {
148 isl_bool involves;
150 involves = FN(EL,involves_dims)(multi->p[i], type, first, n);
151 if (involves < 0 || involves)
152 return involves;
155 return isl_bool_false;
158 __isl_give MULTI(BASE) *FN(MULTI(BASE),insert_dims)(
159 __isl_take MULTI(BASE) *multi,
160 enum isl_dim_type type, unsigned first, unsigned n)
162 int i;
164 if (!multi)
165 return NULL;
166 if (type == isl_dim_out)
167 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
168 "cannot insert output/set dimensions",
169 return FN(MULTI(BASE),free)(multi));
170 if (n == 0 && !isl_space_is_named_or_nested(multi->space, type))
171 return multi;
173 multi = FN(MULTI(BASE),cow)(multi);
174 if (!multi)
175 return NULL;
177 multi->space = isl_space_insert_dims(multi->space, type, first, n);
178 if (!multi->space)
179 return FN(MULTI(BASE),free)(multi);
181 for (i = 0; i < multi->n; ++i) {
182 multi->p[i] = FN(EL,insert_dims)(multi->p[i], type, first, n);
183 if (!multi->p[i])
184 return FN(MULTI(BASE),free)(multi);
187 return multi;
190 __isl_give MULTI(BASE) *FN(MULTI(BASE),add_dims)(__isl_take MULTI(BASE) *multi,
191 enum isl_dim_type type, unsigned n)
193 unsigned pos;
195 pos = FN(MULTI(BASE),dim)(multi, type);
197 return FN(MULTI(BASE),insert_dims)(multi, type, pos, n);
199 #endif
201 unsigned FN(MULTI(BASE),dim)(__isl_keep MULTI(BASE) *multi,
202 enum isl_dim_type type)
204 return multi ? isl_space_dim(multi->space, type) : 0;
207 /* Return the position of the first dimension of "type" with id "id".
208 * Return -1 if there is no such dimension.
210 int FN(MULTI(BASE),find_dim_by_id)(__isl_keep MULTI(BASE) *multi,
211 enum isl_dim_type type, __isl_keep isl_id *id)
213 if (!multi)
214 return -1;
215 return isl_space_find_dim_by_id(multi->space, type, id);
218 /* Return the id of the given dimension.
220 __isl_give isl_id *FN(MULTI(BASE),get_dim_id)(__isl_keep MULTI(BASE) *multi,
221 enum isl_dim_type type, unsigned pos)
223 return multi ? isl_space_get_dim_id(multi->space, type, pos) : NULL;
226 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_name)(
227 __isl_take MULTI(BASE) *multi,
228 enum isl_dim_type type, unsigned pos, const char *s)
230 int i;
232 multi = FN(MULTI(BASE),cow)(multi);
233 if (!multi)
234 return NULL;
236 multi->space = isl_space_set_dim_name(multi->space, type, pos, s);
237 if (!multi->space)
238 return FN(MULTI(BASE),free)(multi);
240 if (type == isl_dim_out)
241 return multi;
242 for (i = 0; i < multi->n; ++i) {
243 multi->p[i] = FN(EL,set_dim_name)(multi->p[i], type, pos, s);
244 if (!multi->p[i])
245 return FN(MULTI(BASE),free)(multi);
248 return multi;
251 const char *FN(MULTI(BASE),get_tuple_name)(__isl_keep MULTI(BASE) *multi,
252 enum isl_dim_type type)
254 return multi ? isl_space_get_tuple_name(multi->space, type) : NULL;
257 /* Does the specified tuple have an id?
259 isl_bool FN(MULTI(BASE),has_tuple_id)(__isl_keep MULTI(BASE) *multi,
260 enum isl_dim_type type)
262 if (!multi)
263 return isl_bool_error;
264 return isl_space_has_tuple_id(multi->space, type);
267 /* Return the id of the specified tuple.
269 __isl_give isl_id *FN(MULTI(BASE),get_tuple_id)(__isl_keep MULTI(BASE) *multi,
270 enum isl_dim_type type)
272 return multi ? isl_space_get_tuple_id(multi->space, type) : NULL;
275 __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
276 int pos)
278 isl_ctx *ctx;
280 if (!multi)
281 return NULL;
282 ctx = FN(MULTI(BASE),get_ctx)(multi);
283 if (pos < 0 || pos >= multi->n)
284 isl_die(ctx, isl_error_invalid,
285 "index out of bounds", return NULL);
286 return FN(EL,copy)(multi->p[pos]);
289 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
290 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
292 isl_space *multi_space = NULL;
293 isl_space *el_space = NULL;
294 int match;
296 multi = FN(MULTI(BASE),cow)(multi);
297 if (!multi || !el)
298 goto error;
300 multi_space = FN(MULTI(BASE),get_space)(multi);
301 match = FN(EL,matching_params)(el, multi_space);
302 if (match < 0)
303 goto error;
304 if (!match) {
305 multi = FN(MULTI(BASE),align_params)(multi,
306 FN(EL,get_space)(el));
307 isl_space_free(multi_space);
308 multi_space = FN(MULTI(BASE),get_space)(multi);
309 el = FN(EL,align_params)(el, isl_space_copy(multi_space));
311 if (FN(EL,check_match_domain_space)(el, multi_space) < 0)
312 goto error;
314 if (pos < 0 || pos >= multi->n)
315 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
316 "index out of bounds", goto error);
318 FN(EL,free)(multi->p[pos]);
319 multi->p[pos] = el;
321 isl_space_free(multi_space);
322 isl_space_free(el_space);
324 return multi;
325 error:
326 FN(MULTI(BASE),free)(multi);
327 FN(EL,free)(el);
328 isl_space_free(multi_space);
329 isl_space_free(el_space);
330 return NULL;
333 /* Reset the space of "multi". This function is called from isl_pw_templ.c
334 * and doesn't know if the space of an element object is represented
335 * directly or through its domain. It therefore passes along both,
336 * which we pass along to the element function since we don't how
337 * that is represented either.
339 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
340 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
341 __isl_take isl_space *domain)
343 int i;
345 multi = FN(MULTI(BASE),cow)(multi);
346 if (!multi || !space || !domain)
347 goto error;
349 for (i = 0; i < multi->n; ++i) {
350 multi->p[i] = FN(EL,reset_domain_space)(multi->p[i],
351 isl_space_copy(domain));
352 if (!multi->p[i])
353 goto error;
355 isl_space_free(domain);
356 isl_space_free(multi->space);
357 multi->space = space;
359 return multi;
360 error:
361 isl_space_free(domain);
362 isl_space_free(space);
363 FN(MULTI(BASE),free)(multi);
364 return NULL;
367 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
368 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
370 isl_space *space;
372 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
373 isl_space_copy(multi->space));
374 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
377 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
378 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
380 isl_space *domain;
382 domain = isl_space_domain(isl_space_copy(space));
383 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
386 /* Set the id of the given dimension of "multi" to "id".
388 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_id)(
389 __isl_take MULTI(BASE) *multi,
390 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
392 isl_space *space;
394 multi = FN(MULTI(BASE),cow)(multi);
395 if (!multi || !id)
396 goto error;
398 space = FN(MULTI(BASE),get_space)(multi);
399 space = isl_space_set_dim_id(space, type, pos, id);
401 return FN(MULTI(BASE),reset_space)(multi, space);
402 error:
403 isl_id_free(id);
404 FN(MULTI(BASE),free)(multi);
405 return NULL;
408 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_name)(
409 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
410 const char *s)
412 isl_space *space;
414 multi = FN(MULTI(BASE),cow)(multi);
415 if (!multi)
416 return NULL;
418 space = FN(MULTI(BASE),get_space)(multi);
419 space = isl_space_set_tuple_name(space, type, s);
421 return FN(MULTI(BASE),reset_space)(multi, space);
424 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_id)(
425 __isl_take MULTI(BASE) *multi, enum isl_dim_type type,
426 __isl_take isl_id *id)
428 isl_space *space;
430 multi = FN(MULTI(BASE),cow)(multi);
431 if (!multi)
432 goto error;
434 space = FN(MULTI(BASE),get_space)(multi);
435 space = isl_space_set_tuple_id(space, type, id);
437 return FN(MULTI(BASE),reset_space)(multi, space);
438 error:
439 isl_id_free(id);
440 return NULL;
443 /* Drop the id on the specified tuple.
445 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_tuple_id)(
446 __isl_take MULTI(BASE) *multi, enum isl_dim_type type)
448 isl_space *space;
450 if (!multi)
451 return NULL;
452 if (!FN(MULTI(BASE),has_tuple_id)(multi, type))
453 return multi;
455 multi = FN(MULTI(BASE),cow)(multi);
456 if (!multi)
457 return NULL;
459 space = FN(MULTI(BASE),get_space)(multi);
460 space = isl_space_reset_tuple_id(space, type);
462 return FN(MULTI(BASE),reset_space)(multi, space);
465 /* Reset the user pointer on all identifiers of parameters and tuples
466 * of the space of "multi".
468 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_user)(
469 __isl_take MULTI(BASE) *multi)
471 isl_space *space;
473 space = FN(MULTI(BASE),get_space)(multi);
474 space = isl_space_reset_user(space);
476 return FN(MULTI(BASE),reset_space)(multi, space);
479 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
480 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
482 int i;
484 multi = FN(MULTI(BASE),cow)(multi);
485 if (!multi || !exp)
486 goto error;
488 for (i = 0; i < multi->n; ++i) {
489 multi->p[i] = FN(EL,realign_domain)(multi->p[i],
490 isl_reordering_copy(exp));
491 if (!multi->p[i])
492 goto error;
495 multi = FN(MULTI(BASE),reset_domain_space)(multi,
496 isl_space_copy(exp->dim));
498 isl_reordering_free(exp);
499 return multi;
500 error:
501 isl_reordering_free(exp);
502 FN(MULTI(BASE),free)(multi);
503 return NULL;
506 /* Align the parameters of "multi" to those of "model".
508 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
509 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
511 isl_ctx *ctx;
512 isl_reordering *exp;
514 if (!multi || !model)
515 goto error;
517 if (isl_space_match(multi->space, isl_dim_param,
518 model, isl_dim_param)) {
519 isl_space_free(model);
520 return multi;
523 ctx = isl_space_get_ctx(model);
524 if (!isl_space_has_named_params(model))
525 isl_die(ctx, isl_error_invalid,
526 "model has unnamed parameters", goto error);
527 if (!isl_space_has_named_params(multi->space))
528 isl_die(ctx, isl_error_invalid,
529 "input has unnamed parameters", goto error);
531 model = isl_space_params(model);
532 exp = isl_parameter_alignment_reordering(multi->space, model);
533 exp = isl_reordering_extend_space(exp,
534 FN(MULTI(BASE),get_domain_space)(multi));
535 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
537 isl_space_free(model);
538 return multi;
539 error:
540 isl_space_free(model);
541 FN(MULTI(BASE),free)(multi);
542 return NULL;
545 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
546 __isl_take isl_space *space, __isl_take LIST(EL) *list)
548 int i;
549 int n;
550 isl_ctx *ctx;
551 MULTI(BASE) *multi;
553 if (!space || !list)
554 goto error;
556 ctx = isl_space_get_ctx(space);
557 n = FN(FN(LIST(EL),n),BASE)(list);
558 if (n != isl_space_dim(space, isl_dim_out))
559 isl_die(ctx, isl_error_invalid,
560 "invalid number of elements in list", goto error);
562 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
563 for (i = 0; i < n; ++i) {
564 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
565 FN(FN(LIST(EL),get),BASE)(list, i));
568 isl_space_free(space);
569 FN(LIST(EL),free)(list);
570 return multi;
571 error:
572 isl_space_free(space);
573 FN(LIST(EL),free)(list);
574 return NULL;
577 #ifndef NO_IDENTITY
578 /* Create a multi expression in the given space that maps each
579 * input dimension to the corresponding output dimension.
581 __isl_give MULTI(BASE) *FN(MULTI(BASE),identity)(__isl_take isl_space *space)
583 int i, n;
584 isl_local_space *ls;
585 MULTI(BASE) *multi;
587 if (!space)
588 return NULL;
590 if (isl_space_is_set(space))
591 isl_die(isl_space_get_ctx(space), isl_error_invalid,
592 "expecting map space", goto error);
594 n = isl_space_dim(space, isl_dim_out);
595 if (n != isl_space_dim(space, isl_dim_in))
596 isl_die(isl_space_get_ctx(space), isl_error_invalid,
597 "number of input and output dimensions needs to be "
598 "the same", goto error);
600 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
602 if (!n) {
603 isl_space_free(space);
604 return multi;
607 space = isl_space_domain(space);
608 ls = isl_local_space_from_space(space);
610 for (i = 0; i < n; ++i) {
611 EL *el;
612 el = FN(EL,var_on_domain)(isl_local_space_copy(ls),
613 isl_dim_set, i);
614 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i, el);
617 isl_local_space_free(ls);
619 return multi;
620 error:
621 isl_space_free(space);
622 return NULL;
624 #endif
626 #ifndef NO_ZERO
627 /* Construct a multi expression in the given space with value zero in
628 * each of the output dimensions.
630 __isl_give MULTI(BASE) *FN(MULTI(BASE),zero)(__isl_take isl_space *space)
632 int n;
633 MULTI(BASE) *multi;
635 if (!space)
636 return NULL;
638 n = isl_space_dim(space , isl_dim_out);
639 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
641 if (!n)
642 isl_space_free(space);
643 else {
644 int i;
645 isl_local_space *ls;
646 EL *el;
648 space = isl_space_domain(space);
649 ls = isl_local_space_from_space(space);
650 el = FN(EL,zero_on_domain)(ls);
652 for (i = 0; i < n; ++i)
653 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
654 FN(EL,copy)(el));
656 FN(EL,free)(el);
659 return multi;
661 #endif
663 #ifndef NO_FROM_BASE
664 /* Create a multiple expression with a single output/set dimension
665 * equal to "el".
666 * For most multiple expression types, the base type has a single
667 * output/set dimension and the space of the result is therefore
668 * the same as the space of the input.
669 * In the case of isl_multi_union_pw_aff, however, the base type
670 * lives in a parameter space and we therefore need to add
671 * a single set dimension.
673 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),BASE)(__isl_take EL *el)
675 isl_space *space;
676 MULTI(BASE) *multi;
678 space = FN(EL,get_space(el));
679 if (isl_space_is_params(space)) {
680 space = isl_space_set_from_params(space);
681 space = isl_space_add_dims(space, isl_dim_set, 1);
683 multi = FN(MULTI(BASE),alloc)(space);
684 multi = FN(FN(MULTI(BASE),set),BASE)(multi, 0, el);
686 return multi;
688 #endif
690 __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
691 __isl_take MULTI(BASE) *multi,
692 enum isl_dim_type type, unsigned first, unsigned n)
694 int i;
695 unsigned dim;
697 multi = FN(MULTI(BASE),cow)(multi);
698 if (!multi)
699 return NULL;
701 dim = FN(MULTI(BASE),dim)(multi, type);
702 if (first + n > dim || first + n < first)
703 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
704 "index out of bounds",
705 return FN(MULTI(BASE),cow)(multi));
707 multi->space = isl_space_drop_dims(multi->space, type, first, n);
708 if (!multi->space)
709 return FN(MULTI(BASE),cow)(multi);
711 if (type == isl_dim_out) {
712 for (i = 0; i < n; ++i)
713 FN(EL,free)(multi->p[first + i]);
714 for (i = first; i + n < multi->n; ++i)
715 multi->p[i] = multi->p[i + n];
716 multi->n -= n;
718 return multi;
721 for (i = 0; i < multi->n; ++i) {
722 multi->p[i] = FN(EL,drop_dims)(multi->p[i], type, first, n);
723 if (!multi->p[i])
724 return FN(MULTI(BASE),cow)(multi);
727 return multi;
730 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
732 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_multi_and)(
733 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
734 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi1,
735 __isl_take MULTI(BASE) *multi2))
737 isl_ctx *ctx;
739 if (!multi1 || !multi2)
740 goto error;
741 if (isl_space_match(multi1->space, isl_dim_param,
742 multi2->space, isl_dim_param))
743 return fn(multi1, multi2);
744 ctx = FN(MULTI(BASE),get_ctx)(multi1);
745 if (!isl_space_has_named_params(multi1->space) ||
746 !isl_space_has_named_params(multi2->space))
747 isl_die(ctx, isl_error_invalid,
748 "unaligned unnamed parameters", goto error);
749 multi1 = FN(MULTI(BASE),align_params)(multi1,
750 FN(MULTI(BASE),get_space)(multi2));
751 multi2 = FN(MULTI(BASE),align_params)(multi2,
752 FN(MULTI(BASE),get_space)(multi1));
753 return fn(multi1, multi2);
754 error:
755 FN(MULTI(BASE),free)(multi1);
756 FN(MULTI(BASE),free)(multi2);
757 return NULL;
760 /* Given two MULTI(BASE)s A -> B and C -> D,
761 * construct a MULTI(BASE) (A * C) -> [B -> D].
763 * The parameters are assumed to have been aligned.
765 static __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product_aligned)(
766 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
768 int i, n1, n2;
769 EL *el;
770 isl_space *space;
771 MULTI(BASE) *res;
773 if (!multi1 || !multi2)
774 goto error;
776 space = isl_space_range_product(FN(MULTI(BASE),get_space)(multi1),
777 FN(MULTI(BASE),get_space)(multi2));
778 res = FN(MULTI(BASE),alloc)(space);
780 n1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
781 n2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
783 for (i = 0; i < n1; ++i) {
784 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
785 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
788 for (i = 0; i < n2; ++i) {
789 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
790 res = FN(FN(MULTI(BASE),set),BASE)(res, n1 + i, el);
793 FN(MULTI(BASE),free)(multi1);
794 FN(MULTI(BASE),free)(multi2);
795 return res;
796 error:
797 FN(MULTI(BASE),free)(multi1);
798 FN(MULTI(BASE),free)(multi2);
799 return NULL;
802 /* Given two MULTI(BASE)s A -> B and C -> D,
803 * construct a MULTI(BASE) (A * C) -> [B -> D].
805 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product)(
806 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
808 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
809 &FN(MULTI(BASE),range_product_aligned));
812 /* Is the range of "multi" a wrapped relation?
814 isl_bool FN(MULTI(BASE),range_is_wrapping)(__isl_keep MULTI(BASE) *multi)
816 if (!multi)
817 return isl_bool_error;
818 return isl_space_range_is_wrapping(multi->space);
821 /* Given a function A -> [B -> C], extract the function A -> B.
823 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_domain)(
824 __isl_take MULTI(BASE) *multi)
826 isl_space *space;
827 int total, keep;
829 if (!multi)
830 return NULL;
831 if (!isl_space_range_is_wrapping(multi->space))
832 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
833 "range is not a product",
834 return FN(MULTI(BASE),free)(multi));
836 space = FN(MULTI(BASE),get_space)(multi);
837 total = isl_space_dim(space, isl_dim_out);
838 space = isl_space_range_factor_domain(space);
839 keep = isl_space_dim(space, isl_dim_out);
840 multi = FN(MULTI(BASE),drop_dims)(multi,
841 isl_dim_out, keep, total - keep);
842 multi = FN(MULTI(BASE),reset_space)(multi, space);
844 return multi;
847 /* Given a function A -> [B -> C], extract the function A -> C.
849 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_range)(
850 __isl_take MULTI(BASE) *multi)
852 isl_space *space;
853 int total, keep;
855 if (!multi)
856 return NULL;
857 if (!isl_space_range_is_wrapping(multi->space))
858 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
859 "range is not a product",
860 return FN(MULTI(BASE),free)(multi));
862 space = FN(MULTI(BASE),get_space)(multi);
863 total = isl_space_dim(space, isl_dim_out);
864 space = isl_space_range_factor_range(space);
865 keep = isl_space_dim(space, isl_dim_out);
866 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_out, 0, total - keep);
867 multi = FN(MULTI(BASE),reset_space)(multi, space);
869 return multi;
872 #ifndef NO_PRODUCT
873 /* Given two MULTI(BASE)s A -> B and C -> D,
874 * construct a MULTI(BASE) [A -> C] -> [B -> D].
876 * The parameters are assumed to have been aligned.
878 __isl_give MULTI(BASE) *FN(MULTI(BASE),product_aligned)(
879 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
881 int i;
882 EL *el;
883 isl_space *space;
884 MULTI(BASE) *res;
885 int in1, in2, out1, out2;
887 in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
888 in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
889 out1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
890 out2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
891 space = isl_space_product(FN(MULTI(BASE),get_space)(multi1),
892 FN(MULTI(BASE),get_space)(multi2));
893 res = FN(MULTI(BASE),alloc)(isl_space_copy(space));
894 space = isl_space_domain(space);
896 for (i = 0; i < out1; ++i) {
897 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
898 el = FN(EL,insert_dims)(el, isl_dim_in, in1, in2);
899 el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
900 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
903 for (i = 0; i < out2; ++i) {
904 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
905 el = FN(EL,insert_dims)(el, isl_dim_in, 0, in1);
906 el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
907 res = FN(FN(MULTI(BASE),set),BASE)(res, out1 + i, el);
910 isl_space_free(space);
911 FN(MULTI(BASE),free)(multi1);
912 FN(MULTI(BASE),free)(multi2);
913 return res;
916 /* Given two MULTI(BASE)s A -> B and C -> D,
917 * construct a MULTI(BASE) [A -> C] -> [B -> D].
919 __isl_give MULTI(BASE) *FN(MULTI(BASE),product)(
920 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
922 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
923 &FN(MULTI(BASE),product_aligned));
925 #endif
927 __isl_give MULTI(BASE) *FN(MULTI(BASE),flatten_range)(
928 __isl_take MULTI(BASE) *multi)
930 if (!multi)
931 return NULL;
933 if (!multi->space->nested[1])
934 return multi;
936 multi = FN(MULTI(BASE),cow)(multi);
937 if (!multi)
938 return NULL;
940 multi->space = isl_space_flatten_range(multi->space);
941 if (!multi->space)
942 return FN(MULTI(BASE),free)(multi);
944 return multi;
947 /* Given two MULTI(BASE)s A -> B and C -> D,
948 * construct a MULTI(BASE) (A * C) -> (B, D).
950 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
951 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
953 MULTI(BASE) *multi;
955 multi = FN(MULTI(BASE),range_product)(multi1, multi2);
956 multi = FN(MULTI(BASE),flatten_range)(multi);
957 return multi;
960 /* Given two multi expressions, "multi1"
962 * [A] -> [B1 B2]
964 * where B2 starts at position "pos", and "multi2"
966 * [A] -> [D]
968 * return the multi expression
970 * [A] -> [B1 D B2]
972 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_splice)(
973 __isl_take MULTI(BASE) *multi1, unsigned pos,
974 __isl_take MULTI(BASE) *multi2)
976 MULTI(BASE) *res;
977 unsigned dim;
979 if (!multi1 || !multi2)
980 goto error;
982 dim = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
983 if (pos > dim)
984 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
985 "index out of bounds", goto error);
987 res = FN(MULTI(BASE),copy)(multi1);
988 res = FN(MULTI(BASE),drop_dims)(res, isl_dim_out, pos, dim - pos);
989 multi1 = FN(MULTI(BASE),drop_dims)(multi1, isl_dim_out, 0, pos);
991 res = FN(MULTI(BASE),flat_range_product)(res, multi2);
992 res = FN(MULTI(BASE),flat_range_product)(res, multi1);
994 return res;
995 error:
996 FN(MULTI(BASE),free)(multi1);
997 FN(MULTI(BASE),free)(multi2);
998 return NULL;
1001 #ifndef NO_SPLICE
1002 /* Given two multi expressions, "multi1"
1004 * [A1 A2] -> [B1 B2]
1006 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
1007 * and "multi2"
1009 * [C] -> [D]
1011 * return the multi expression
1013 * [A1 C A2] -> [B1 D B2]
1015 * We first insert input dimensions to obtain
1017 * [A1 C A2] -> [B1 B2]
1019 * and
1021 * [A1 C A2] -> [D]
1023 * and then apply range_splice.
1025 __isl_give MULTI(BASE) *FN(MULTI(BASE),splice)(
1026 __isl_take MULTI(BASE) *multi1, unsigned in_pos, unsigned out_pos,
1027 __isl_take MULTI(BASE) *multi2)
1029 unsigned n_in1;
1030 unsigned n_in2;
1032 if (!multi1 || !multi2)
1033 goto error;
1035 n_in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
1036 if (in_pos > n_in1)
1037 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
1038 "index out of bounds", goto error);
1040 n_in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
1042 multi1 = FN(MULTI(BASE),insert_dims)(multi1, isl_dim_in, in_pos, n_in2);
1043 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, n_in2,
1044 n_in1 - in_pos);
1045 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, 0, in_pos);
1047 return FN(MULTI(BASE),range_splice)(multi1, out_pos, multi2);
1048 error:
1049 FN(MULTI(BASE),free)(multi1);
1050 FN(MULTI(BASE),free)(multi2);
1051 return NULL;
1053 #endif
1055 /* This function is currently only used from isl_aff.c
1057 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
1058 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
1059 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
1060 __attribute__ ((unused));
1062 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
1063 * return the result.
1065 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
1066 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
1067 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
1069 int i;
1070 isl_ctx *ctx;
1072 multi1 = FN(MULTI(BASE),cow)(multi1);
1073 if (!multi1 || !multi2)
1074 goto error;
1076 ctx = FN(MULTI(BASE),get_ctx)(multi1);
1077 if (!isl_space_is_equal(multi1->space, multi2->space))
1078 isl_die(ctx, isl_error_invalid,
1079 "spaces don't match", goto error);
1081 for (i = 0; i < multi1->n; ++i) {
1082 multi1->p[i] = fn(multi1->p[i], FN(EL,copy)(multi2->p[i]));
1083 if (!multi1->p[i])
1084 goto error;
1087 FN(MULTI(BASE),free)(multi2);
1088 return multi1;
1089 error:
1090 FN(MULTI(BASE),free)(multi1);
1091 FN(MULTI(BASE),free)(multi2);
1092 return NULL;
1095 /* Subtract "multi2" from "multi1" and return the result.
1097 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
1099 static __isl_give MULTI(BASE) *FN(MULTI(BASE),sub_aligned)(
1100 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
1102 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,sub));
1105 /* Subtract "multi2" from "multi1" and return the result.
1107 __isl_give MULTI(BASE) *FN(MULTI(BASE),sub)(__isl_take MULTI(BASE) *multi1,
1108 __isl_take MULTI(BASE) *multi2)
1110 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
1111 &FN(MULTI(BASE),sub_aligned));
1114 /* Multiply the elements of "multi" by "v" and return the result.
1116 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_val)(__isl_take MULTI(BASE) *multi,
1117 __isl_take isl_val *v)
1119 int i;
1121 if (!multi || !v)
1122 goto error;
1124 if (isl_val_is_one(v)) {
1125 isl_val_free(v);
1126 return multi;
1129 if (!isl_val_is_rat(v))
1130 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1131 "expecting rational factor", goto error);
1133 multi = FN(MULTI(BASE),cow)(multi);
1134 if (!multi)
1135 return NULL;
1137 for (i = 0; i < multi->n; ++i) {
1138 multi->p[i] = FN(EL,scale_val)(multi->p[i], isl_val_copy(v));
1139 if (!multi->p[i])
1140 goto error;
1143 isl_val_free(v);
1144 return multi;
1145 error:
1146 isl_val_free(v);
1147 return FN(MULTI(BASE),free)(multi);
1150 /* Divide the elements of "multi" by "v" and return the result.
1152 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_val)(
1153 __isl_take MULTI(BASE) *multi, __isl_take isl_val *v)
1155 int i;
1157 if (!multi || !v)
1158 goto error;
1160 if (isl_val_is_one(v)) {
1161 isl_val_free(v);
1162 return multi;
1165 if (!isl_val_is_rat(v))
1166 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1167 "expecting rational factor", goto error);
1168 if (isl_val_is_zero(v))
1169 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1170 "cannot scale down by zero", goto error);
1172 multi = FN(MULTI(BASE),cow)(multi);
1173 if (!multi)
1174 return NULL;
1176 for (i = 0; i < multi->n; ++i) {
1177 multi->p[i] = FN(EL,scale_down_val)(multi->p[i],
1178 isl_val_copy(v));
1179 if (!multi->p[i])
1180 goto error;
1183 isl_val_free(v);
1184 return multi;
1185 error:
1186 isl_val_free(v);
1187 return FN(MULTI(BASE),free)(multi);
1190 /* Multiply the elements of "multi" by the corresponding element of "mv"
1191 * and return the result.
1193 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_multi_val)(
1194 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1196 int i;
1198 if (!multi || !mv)
1199 goto error;
1201 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1202 mv->space, isl_dim_set))
1203 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1204 "spaces don't match", goto error);
1206 multi = FN(MULTI(BASE),cow)(multi);
1207 if (!multi)
1208 return NULL;
1210 for (i = 0; i < multi->n; ++i) {
1211 isl_val *v;
1213 v = isl_multi_val_get_val(mv, i);
1214 multi->p[i] = FN(EL,scale_val)(multi->p[i], v);
1215 if (!multi->p[i])
1216 goto error;
1219 isl_multi_val_free(mv);
1220 return multi;
1221 error:
1222 isl_multi_val_free(mv);
1223 return FN(MULTI(BASE),free)(multi);
1226 /* Divide the elements of "multi" by the corresponding element of "mv"
1227 * and return the result.
1229 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_multi_val)(
1230 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1232 int i;
1234 if (!multi || !mv)
1235 goto error;
1237 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1238 mv->space, isl_dim_set))
1239 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1240 "spaces don't match", goto error);
1242 multi = FN(MULTI(BASE),cow)(multi);
1243 if (!multi)
1244 return NULL;
1246 for (i = 0; i < multi->n; ++i) {
1247 isl_val *v;
1249 v = isl_multi_val_get_val(mv, i);
1250 multi->p[i] = FN(EL,scale_down_val)(multi->p[i], v);
1251 if (!multi->p[i])
1252 goto error;
1255 isl_multi_val_free(mv);
1256 return multi;
1257 error:
1258 isl_multi_val_free(mv);
1259 return FN(MULTI(BASE),free)(multi);
1262 /* Compute the residues of the elements of "multi" modulo
1263 * the corresponding element of "mv" and return the result.
1265 __isl_give MULTI(BASE) *FN(MULTI(BASE),mod_multi_val)(
1266 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1268 int i;
1270 if (!multi || !mv)
1271 goto error;
1273 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1274 mv->space, isl_dim_set))
1275 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1276 "spaces don't match", goto error);
1278 multi = FN(MULTI(BASE),cow)(multi);
1279 if (!multi)
1280 return NULL;
1282 for (i = 0; i < multi->n; ++i) {
1283 isl_val *v;
1285 v = isl_multi_val_get_val(mv, i);
1286 multi->p[i] = FN(EL,mod_val)(multi->p[i], v);
1287 if (!multi->p[i])
1288 goto error;
1291 isl_multi_val_free(mv);
1292 return multi;
1293 error:
1294 isl_multi_val_free(mv);
1295 return FN(MULTI(BASE),free)(multi);
1298 #ifndef NO_MOVE_DIMS
1299 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1300 * to dimensions of "dst_type" at "dst_pos".
1302 * We only support moving input dimensions to parameters and vice versa.
1304 __isl_give MULTI(BASE) *FN(MULTI(BASE),move_dims)(__isl_take MULTI(BASE) *multi,
1305 enum isl_dim_type dst_type, unsigned dst_pos,
1306 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1308 int i;
1310 if (!multi)
1311 return NULL;
1313 if (n == 0 &&
1314 !isl_space_is_named_or_nested(multi->space, src_type) &&
1315 !isl_space_is_named_or_nested(multi->space, dst_type))
1316 return multi;
1318 if (dst_type == isl_dim_out || src_type == isl_dim_out)
1319 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1320 "cannot move output/set dimension",
1321 return FN(MULTI(BASE),free)(multi));
1322 if (dst_type == isl_dim_div || src_type == isl_dim_div)
1323 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1324 "cannot move divs",
1325 return FN(MULTI(BASE),free)(multi));
1326 if (src_pos + n > isl_space_dim(multi->space, src_type))
1327 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1328 "range out of bounds",
1329 return FN(MULTI(BASE),free)(multi));
1330 if (dst_type == src_type)
1331 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_unsupported,
1332 "moving dims within the same type not supported",
1333 return FN(MULTI(BASE),free)(multi));
1335 multi = FN(MULTI(BASE),cow)(multi);
1336 if (!multi)
1337 return NULL;
1339 multi->space = isl_space_move_dims(multi->space, dst_type, dst_pos,
1340 src_type, src_pos, n);
1341 if (!multi->space)
1342 return FN(MULTI(BASE),free)(multi);
1344 for (i = 0; i < multi->n; ++i) {
1345 multi->p[i] = FN(EL,move_dims)(multi->p[i], dst_type, dst_pos,
1346 src_type, src_pos, n);
1347 if (!multi->p[i])
1348 return FN(MULTI(BASE),free)(multi);
1351 return multi;
1353 #endif
1355 /* Convert a multiple expression defined over a parameter domain
1356 * into one that is defined over a zero-dimensional set.
1358 __isl_give MULTI(BASE) *FN(MULTI(BASE),from_range)(
1359 __isl_take MULTI(BASE) *multi)
1361 isl_space *space;
1363 if (!multi)
1364 return NULL;
1365 if (!isl_space_is_set(multi->space))
1366 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1367 "not living in a set space",
1368 return FN(MULTI(BASE),free)(multi));
1370 space = FN(MULTI(BASE),get_space)(multi);
1371 space = isl_space_from_range(space);
1372 multi = FN(MULTI(BASE),reset_space)(multi, space);
1374 return multi;
1377 /* Are "multi1" and "multi2" obviously equal?
1379 isl_bool FN(MULTI(BASE),plain_is_equal)(__isl_keep MULTI(BASE) *multi1,
1380 __isl_keep MULTI(BASE) *multi2)
1382 int i;
1383 isl_bool equal;
1385 if (!multi1 || !multi2)
1386 return isl_bool_error;
1387 if (multi1->n != multi2->n)
1388 return isl_bool_false;
1389 equal = isl_space_is_equal(multi1->space, multi2->space);
1390 if (equal < 0 || !equal)
1391 return equal;
1393 for (i = 0; i < multi1->n; ++i) {
1394 equal = FN(EL,plain_is_equal)(multi1->p[i], multi2->p[i]);
1395 if (equal < 0 || !equal)
1396 return equal;
1399 return isl_bool_true;
1402 #ifndef NO_DOMAIN
1403 /* Return the shared domain of the elements of "multi".
1405 __isl_give isl_set *FN(MULTI(BASE),domain)(__isl_take MULTI(BASE) *multi)
1407 int i;
1408 isl_set *dom;
1410 if (!multi)
1411 return NULL;
1413 dom = isl_set_universe(FN(MULTI(BASE),get_domain_space)(multi));
1414 for (i = 0; i < multi->n; ++i) {
1415 isl_set *dom_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);
1422 return dom;
1424 #endif
1426 #ifndef NO_NEG
1427 /* Return the opposite of "multi".
1429 __isl_give MULTI(BASE) *FN(MULTI(BASE),neg)(__isl_take MULTI(BASE) *multi)
1431 int i;
1433 multi = FN(MULTI(BASE),cow)(multi);
1434 if (!multi)
1435 return NULL;
1437 for (i = 0; i < multi->n; ++i) {
1438 multi->p[i] = FN(EL,neg)(multi->p[i]);
1439 if (!multi->p[i])
1440 return FN(MULTI(BASE),free)(multi);
1443 return multi;
1445 #endif