isl_schedule_node.c: collect_filter_prefix: allow caller to initialize filter
[isl.git] / isl_multi_templ.c
blobc60aff0b309e5e8476b407330ec31e5cffe687aa
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 int 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 -1;
144 if (multi->n == 0 || n == 0)
145 return 0;
147 for (i = 0; i < multi->n; ++i) {
148 int involves;
150 involves = FN(EL,involves_dims)(multi->p[i], type, first, n);
151 if (involves < 0 || involves)
152 return involves;
155 return 0;
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 int FN(MULTI(BASE),has_tuple_id)(__isl_keep MULTI(BASE) *multi,
260 enum isl_dim_type type)
262 return multi ? isl_space_has_tuple_id(multi->space, type) : -1;
265 /* Return the id of the specified tuple.
267 __isl_give isl_id *FN(MULTI(BASE),get_tuple_id)(__isl_keep MULTI(BASE) *multi,
268 enum isl_dim_type type)
270 return multi ? isl_space_get_tuple_id(multi->space, type) : NULL;
273 __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
274 int pos)
276 isl_ctx *ctx;
278 if (!multi)
279 return NULL;
280 ctx = FN(MULTI(BASE),get_ctx)(multi);
281 if (pos < 0 || pos >= multi->n)
282 isl_die(ctx, isl_error_invalid,
283 "index out of bounds", return NULL);
284 return FN(EL,copy)(multi->p[pos]);
287 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
288 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
290 isl_space *multi_space = NULL;
291 isl_space *el_space = NULL;
292 int match;
294 multi = FN(MULTI(BASE),cow)(multi);
295 if (!multi || !el)
296 goto error;
298 multi_space = FN(MULTI(BASE),get_space)(multi);
299 match = FN(EL,matching_params)(el, multi_space);
300 if (match < 0)
301 goto error;
302 if (!match) {
303 multi = FN(MULTI(BASE),align_params)(multi,
304 FN(EL,get_space)(el));
305 isl_space_free(multi_space);
306 multi_space = FN(MULTI(BASE),get_space)(multi);
307 el = FN(EL,align_params)(el, isl_space_copy(multi_space));
309 if (FN(EL,check_match_domain_space)(el, multi_space) < 0)
310 goto error;
312 if (pos < 0 || pos >= multi->n)
313 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
314 "index out of bounds", goto error);
316 FN(EL,free)(multi->p[pos]);
317 multi->p[pos] = el;
319 isl_space_free(multi_space);
320 isl_space_free(el_space);
322 return multi;
323 error:
324 FN(MULTI(BASE),free)(multi);
325 FN(EL,free)(el);
326 isl_space_free(multi_space);
327 isl_space_free(el_space);
328 return NULL;
331 /* Reset the space of "multi". This function is called from isl_pw_templ.c
332 * and doesn't know if the space of an element object is represented
333 * directly or through its domain. It therefore passes along both,
334 * which we pass along to the element function since we don't how
335 * that is represented either.
337 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
338 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
339 __isl_take isl_space *domain)
341 int i;
343 multi = FN(MULTI(BASE),cow)(multi);
344 if (!multi || !space || !domain)
345 goto error;
347 for (i = 0; i < multi->n; ++i) {
348 multi->p[i] = FN(EL,reset_domain_space)(multi->p[i],
349 isl_space_copy(domain));
350 if (!multi->p[i])
351 goto error;
353 isl_space_free(domain);
354 isl_space_free(multi->space);
355 multi->space = space;
357 return multi;
358 error:
359 isl_space_free(domain);
360 isl_space_free(space);
361 FN(MULTI(BASE),free)(multi);
362 return NULL;
365 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
366 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
368 isl_space *space;
370 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
371 isl_space_copy(multi->space));
372 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
375 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
376 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
378 isl_space *domain;
380 domain = isl_space_domain(isl_space_copy(space));
381 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
384 /* Set the id of the given dimension of "multi" to "id".
386 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_id)(
387 __isl_take MULTI(BASE) *multi,
388 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
390 isl_space *space;
392 multi = FN(MULTI(BASE),cow)(multi);
393 if (!multi || !id)
394 goto error;
396 space = FN(MULTI(BASE),get_space)(multi);
397 space = isl_space_set_dim_id(space, type, pos, id);
399 return FN(MULTI(BASE),reset_space)(multi, space);
400 error:
401 isl_id_free(id);
402 FN(MULTI(BASE),free)(multi);
403 return NULL;
406 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_name)(
407 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
408 const char *s)
410 isl_space *space;
412 multi = FN(MULTI(BASE),cow)(multi);
413 if (!multi)
414 return NULL;
416 space = FN(MULTI(BASE),get_space)(multi);
417 space = isl_space_set_tuple_name(space, type, s);
419 return FN(MULTI(BASE),reset_space)(multi, space);
422 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_id)(
423 __isl_take MULTI(BASE) *multi, enum isl_dim_type type,
424 __isl_take isl_id *id)
426 isl_space *space;
428 multi = FN(MULTI(BASE),cow)(multi);
429 if (!multi)
430 goto error;
432 space = FN(MULTI(BASE),get_space)(multi);
433 space = isl_space_set_tuple_id(space, type, id);
435 return FN(MULTI(BASE),reset_space)(multi, space);
436 error:
437 isl_id_free(id);
438 return NULL;
441 /* Drop the id on the specified tuple.
443 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_tuple_id)(
444 __isl_take MULTI(BASE) *multi, enum isl_dim_type type)
446 isl_space *space;
448 if (!multi)
449 return NULL;
450 if (!FN(MULTI(BASE),has_tuple_id)(multi, type))
451 return multi;
453 multi = FN(MULTI(BASE),cow)(multi);
454 if (!multi)
455 return NULL;
457 space = FN(MULTI(BASE),get_space)(multi);
458 space = isl_space_reset_tuple_id(space, type);
460 return FN(MULTI(BASE),reset_space)(multi, space);
463 /* Reset the user pointer on all identifiers of parameters and tuples
464 * of the space of "multi".
466 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_user)(
467 __isl_take MULTI(BASE) *multi)
469 isl_space *space;
471 space = FN(MULTI(BASE),get_space)(multi);
472 space = isl_space_reset_user(space);
474 return FN(MULTI(BASE),reset_space)(multi, space);
477 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
478 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
480 int i;
482 multi = FN(MULTI(BASE),cow)(multi);
483 if (!multi || !exp)
484 goto error;
486 for (i = 0; i < multi->n; ++i) {
487 multi->p[i] = FN(EL,realign_domain)(multi->p[i],
488 isl_reordering_copy(exp));
489 if (!multi->p[i])
490 goto error;
493 multi = FN(MULTI(BASE),reset_domain_space)(multi,
494 isl_space_copy(exp->dim));
496 isl_reordering_free(exp);
497 return multi;
498 error:
499 isl_reordering_free(exp);
500 FN(MULTI(BASE),free)(multi);
501 return NULL;
504 /* Align the parameters of "multi" to those of "model".
506 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
507 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
509 isl_ctx *ctx;
510 isl_reordering *exp;
512 if (!multi || !model)
513 goto error;
515 if (isl_space_match(multi->space, isl_dim_param,
516 model, isl_dim_param)) {
517 isl_space_free(model);
518 return multi;
521 ctx = isl_space_get_ctx(model);
522 if (!isl_space_has_named_params(model))
523 isl_die(ctx, isl_error_invalid,
524 "model has unnamed parameters", goto error);
525 if (!isl_space_has_named_params(multi->space))
526 isl_die(ctx, isl_error_invalid,
527 "input has unnamed parameters", goto error);
529 model = isl_space_params(model);
530 exp = isl_parameter_alignment_reordering(multi->space, model);
531 exp = isl_reordering_extend_space(exp,
532 FN(MULTI(BASE),get_domain_space)(multi));
533 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
535 isl_space_free(model);
536 return multi;
537 error:
538 isl_space_free(model);
539 FN(MULTI(BASE),free)(multi);
540 return NULL;
543 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
544 __isl_take isl_space *space, __isl_take LIST(EL) *list)
546 int i;
547 int n;
548 isl_ctx *ctx;
549 MULTI(BASE) *multi;
551 if (!space || !list)
552 goto error;
554 ctx = isl_space_get_ctx(space);
555 n = FN(FN(LIST(EL),n),BASE)(list);
556 if (n != isl_space_dim(space, isl_dim_out))
557 isl_die(ctx, isl_error_invalid,
558 "invalid number of elements in list", goto error);
560 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
561 for (i = 0; i < n; ++i) {
562 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
563 FN(FN(LIST(EL),get),BASE)(list, i));
566 isl_space_free(space);
567 FN(LIST(EL),free)(list);
568 return multi;
569 error:
570 isl_space_free(space);
571 FN(LIST(EL),free)(list);
572 return NULL;
575 #ifndef NO_IDENTITY
576 /* Create a multi expression in the given space that maps each
577 * input dimension to the corresponding output dimension.
579 __isl_give MULTI(BASE) *FN(MULTI(BASE),identity)(__isl_take isl_space *space)
581 int i, n;
582 isl_local_space *ls;
583 MULTI(BASE) *multi;
585 if (!space)
586 return NULL;
588 if (isl_space_is_set(space))
589 isl_die(isl_space_get_ctx(space), isl_error_invalid,
590 "expecting map space", goto error);
592 n = isl_space_dim(space, isl_dim_out);
593 if (n != isl_space_dim(space, isl_dim_in))
594 isl_die(isl_space_get_ctx(space), isl_error_invalid,
595 "number of input and output dimensions needs to be "
596 "the same", goto error);
598 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
600 if (!n) {
601 isl_space_free(space);
602 return multi;
605 space = isl_space_domain(space);
606 ls = isl_local_space_from_space(space);
608 for (i = 0; i < n; ++i) {
609 EL *el;
610 el = FN(EL,var_on_domain)(isl_local_space_copy(ls),
611 isl_dim_set, i);
612 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i, el);
615 isl_local_space_free(ls);
617 return multi;
618 error:
619 isl_space_free(space);
620 return NULL;
622 #endif
624 #ifndef NO_ZERO
625 /* Construct a multi expression in the given space with value zero in
626 * each of the output dimensions.
628 __isl_give MULTI(BASE) *FN(MULTI(BASE),zero)(__isl_take isl_space *space)
630 int n;
631 MULTI(BASE) *multi;
633 if (!space)
634 return NULL;
636 n = isl_space_dim(space , isl_dim_out);
637 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
639 if (!n)
640 isl_space_free(space);
641 else {
642 int i;
643 isl_local_space *ls;
644 EL *el;
646 space = isl_space_domain(space);
647 ls = isl_local_space_from_space(space);
648 el = FN(EL,zero_on_domain)(ls);
650 for (i = 0; i < n; ++i)
651 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
652 FN(EL,copy)(el));
654 FN(EL,free)(el);
657 return multi;
659 #endif
661 #ifndef NO_FROM_BASE
662 /* Create a multiple expression with a single output/set dimension
663 * equal to "el".
664 * For most multiple expression types, the base type has a single
665 * output/set dimension and the space of the result is therefore
666 * the same as the space of the input.
667 * In the case of isl_multi_union_pw_aff, however, the base type
668 * lives in a parameter space and we therefore need to add
669 * a single set dimension.
671 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),BASE)(__isl_take EL *el)
673 isl_space *space;
674 MULTI(BASE) *multi;
676 space = FN(EL,get_space(el));
677 if (isl_space_is_params(space)) {
678 space = isl_space_set_from_params(space);
679 space = isl_space_add_dims(space, isl_dim_set, 1);
681 multi = FN(MULTI(BASE),alloc)(space);
682 multi = FN(FN(MULTI(BASE),set),BASE)(multi, 0, el);
684 return multi;
686 #endif
688 __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
689 __isl_take MULTI(BASE) *multi,
690 enum isl_dim_type type, unsigned first, unsigned n)
692 int i;
693 unsigned dim;
695 multi = FN(MULTI(BASE),cow)(multi);
696 if (!multi)
697 return NULL;
699 dim = FN(MULTI(BASE),dim)(multi, type);
700 if (first + n > dim || first + n < first)
701 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
702 "index out of bounds",
703 return FN(MULTI(BASE),cow)(multi));
705 multi->space = isl_space_drop_dims(multi->space, type, first, n);
706 if (!multi->space)
707 return FN(MULTI(BASE),cow)(multi);
709 if (type == isl_dim_out) {
710 for (i = 0; i < n; ++i)
711 FN(EL,free)(multi->p[first + i]);
712 for (i = first; i + n < multi->n; ++i)
713 multi->p[i] = multi->p[i + n];
714 multi->n -= n;
716 return multi;
719 for (i = 0; i < multi->n; ++i) {
720 multi->p[i] = FN(EL,drop_dims)(multi->p[i], type, first, n);
721 if (!multi->p[i])
722 return FN(MULTI(BASE),cow)(multi);
725 return multi;
728 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
730 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_multi_and)(
731 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
732 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi1,
733 __isl_take MULTI(BASE) *multi2))
735 isl_ctx *ctx;
737 if (!multi1 || !multi2)
738 goto error;
739 if (isl_space_match(multi1->space, isl_dim_param,
740 multi2->space, isl_dim_param))
741 return fn(multi1, multi2);
742 ctx = FN(MULTI(BASE),get_ctx)(multi1);
743 if (!isl_space_has_named_params(multi1->space) ||
744 !isl_space_has_named_params(multi2->space))
745 isl_die(ctx, isl_error_invalid,
746 "unaligned unnamed parameters", goto error);
747 multi1 = FN(MULTI(BASE),align_params)(multi1,
748 FN(MULTI(BASE),get_space)(multi2));
749 multi2 = FN(MULTI(BASE),align_params)(multi2,
750 FN(MULTI(BASE),get_space)(multi1));
751 return fn(multi1, multi2);
752 error:
753 FN(MULTI(BASE),free)(multi1);
754 FN(MULTI(BASE),free)(multi2);
755 return NULL;
758 /* Given two MULTI(BASE)s A -> B and C -> D,
759 * construct a MULTI(BASE) (A * C) -> [B -> D].
761 * The parameters are assumed to have been aligned.
763 static __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product_aligned)(
764 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
766 int i, n1, n2;
767 EL *el;
768 isl_space *space;
769 MULTI(BASE) *res;
771 if (!multi1 || !multi2)
772 goto error;
774 space = isl_space_range_product(FN(MULTI(BASE),get_space)(multi1),
775 FN(MULTI(BASE),get_space)(multi2));
776 res = FN(MULTI(BASE),alloc)(space);
778 n1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
779 n2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
781 for (i = 0; i < n1; ++i) {
782 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
783 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
786 for (i = 0; i < n2; ++i) {
787 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
788 res = FN(FN(MULTI(BASE),set),BASE)(res, n1 + i, el);
791 FN(MULTI(BASE),free)(multi1);
792 FN(MULTI(BASE),free)(multi2);
793 return res;
794 error:
795 FN(MULTI(BASE),free)(multi1);
796 FN(MULTI(BASE),free)(multi2);
797 return NULL;
800 /* Given two MULTI(BASE)s A -> B and C -> D,
801 * construct a MULTI(BASE) (A * C) -> [B -> D].
803 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product)(
804 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
806 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
807 &FN(MULTI(BASE),range_product_aligned));
810 /* Is the range of "multi" a wrapped relation?
812 int FN(MULTI(BASE),range_is_wrapping)(__isl_keep MULTI(BASE) *multi)
814 if (!multi)
815 return -1;
816 return isl_space_range_is_wrapping(multi->space);
819 /* Given a function A -> [B -> C], extract the function A -> B.
821 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_domain)(
822 __isl_take MULTI(BASE) *multi)
824 isl_space *space;
825 int total, keep;
827 if (!multi)
828 return NULL;
829 if (!isl_space_range_is_wrapping(multi->space))
830 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
831 "range is not a product",
832 return FN(MULTI(BASE),free)(multi));
834 space = FN(MULTI(BASE),get_space)(multi);
835 total = isl_space_dim(space, isl_dim_out);
836 space = isl_space_range_factor_domain(space);
837 keep = isl_space_dim(space, isl_dim_out);
838 multi = FN(MULTI(BASE),drop_dims)(multi,
839 isl_dim_out, keep, total - keep);
840 multi = FN(MULTI(BASE),reset_space)(multi, space);
842 return multi;
845 /* Given a function A -> [B -> C], extract the function A -> C.
847 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_range)(
848 __isl_take MULTI(BASE) *multi)
850 isl_space *space;
851 int total, keep;
853 if (!multi)
854 return NULL;
855 if (!isl_space_range_is_wrapping(multi->space))
856 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
857 "range is not a product",
858 return FN(MULTI(BASE),free)(multi));
860 space = FN(MULTI(BASE),get_space)(multi);
861 total = isl_space_dim(space, isl_dim_out);
862 space = isl_space_range_factor_range(space);
863 keep = isl_space_dim(space, isl_dim_out);
864 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_out, 0, total - keep);
865 multi = FN(MULTI(BASE),reset_space)(multi, space);
867 return multi;
870 #ifndef NO_PRODUCT
871 /* Given two MULTI(BASE)s A -> B and C -> D,
872 * construct a MULTI(BASE) [A -> C] -> [B -> D].
874 * The parameters are assumed to have been aligned.
876 __isl_give MULTI(BASE) *FN(MULTI(BASE),product_aligned)(
877 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
879 int i;
880 EL *el;
881 isl_space *space;
882 MULTI(BASE) *res;
883 int in1, in2, out1, out2;
885 in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
886 in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
887 out1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
888 out2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
889 space = isl_space_product(FN(MULTI(BASE),get_space)(multi1),
890 FN(MULTI(BASE),get_space)(multi2));
891 res = FN(MULTI(BASE),alloc)(isl_space_copy(space));
892 space = isl_space_domain(space);
894 for (i = 0; i < out1; ++i) {
895 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
896 el = FN(EL,insert_dims)(el, isl_dim_in, in1, in2);
897 el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
898 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
901 for (i = 0; i < out2; ++i) {
902 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
903 el = FN(EL,insert_dims)(el, isl_dim_in, 0, in1);
904 el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
905 res = FN(FN(MULTI(BASE),set),BASE)(res, out1 + i, el);
908 isl_space_free(space);
909 FN(MULTI(BASE),free)(multi1);
910 FN(MULTI(BASE),free)(multi2);
911 return res;
914 /* Given two MULTI(BASE)s A -> B and C -> D,
915 * construct a MULTI(BASE) [A -> C] -> [B -> D].
917 __isl_give MULTI(BASE) *FN(MULTI(BASE),product)(
918 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
920 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
921 &FN(MULTI(BASE),product_aligned));
923 #endif
925 __isl_give MULTI(BASE) *FN(MULTI(BASE),flatten_range)(
926 __isl_take MULTI(BASE) *multi)
928 if (!multi)
929 return NULL;
931 if (!multi->space->nested[1])
932 return multi;
934 multi = FN(MULTI(BASE),cow)(multi);
935 if (!multi)
936 return NULL;
938 multi->space = isl_space_flatten_range(multi->space);
939 if (!multi->space)
940 return FN(MULTI(BASE),free)(multi);
942 return multi;
945 /* Given two MULTI(BASE)s A -> B and C -> D,
946 * construct a MULTI(BASE) (A * C) -> (B, D).
948 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
949 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
951 MULTI(BASE) *multi;
953 multi = FN(MULTI(BASE),range_product)(multi1, multi2);
954 multi = FN(MULTI(BASE),flatten_range)(multi);
955 return multi;
958 /* Given two multi expressions, "multi1"
960 * [A] -> [B1 B2]
962 * where B2 starts at position "pos", and "multi2"
964 * [A] -> [D]
966 * return the multi expression
968 * [A] -> [B1 D B2]
970 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_splice)(
971 __isl_take MULTI(BASE) *multi1, unsigned pos,
972 __isl_take MULTI(BASE) *multi2)
974 MULTI(BASE) *res;
975 unsigned dim;
977 if (!multi1 || !multi2)
978 goto error;
980 dim = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
981 if (pos > dim)
982 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
983 "index out of bounds", goto error);
985 res = FN(MULTI(BASE),copy)(multi1);
986 res = FN(MULTI(BASE),drop_dims)(res, isl_dim_out, pos, dim - pos);
987 multi1 = FN(MULTI(BASE),drop_dims)(multi1, isl_dim_out, 0, pos);
989 res = FN(MULTI(BASE),flat_range_product)(res, multi2);
990 res = FN(MULTI(BASE),flat_range_product)(res, multi1);
992 return res;
993 error:
994 FN(MULTI(BASE),free)(multi1);
995 FN(MULTI(BASE),free)(multi2);
996 return NULL;
999 #ifndef NO_SPLICE
1000 /* Given two multi expressions, "multi1"
1002 * [A1 A2] -> [B1 B2]
1004 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
1005 * and "multi2"
1007 * [C] -> [D]
1009 * return the multi expression
1011 * [A1 C A2] -> [B1 D B2]
1013 * We first insert input dimensions to obtain
1015 * [A1 C A2] -> [B1 B2]
1017 * and
1019 * [A1 C A2] -> [D]
1021 * and then apply range_splice.
1023 __isl_give MULTI(BASE) *FN(MULTI(BASE),splice)(
1024 __isl_take MULTI(BASE) *multi1, unsigned in_pos, unsigned out_pos,
1025 __isl_take MULTI(BASE) *multi2)
1027 unsigned n_in1;
1028 unsigned n_in2;
1030 if (!multi1 || !multi2)
1031 goto error;
1033 n_in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
1034 if (in_pos > n_in1)
1035 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
1036 "index out of bounds", goto error);
1038 n_in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
1040 multi1 = FN(MULTI(BASE),insert_dims)(multi1, isl_dim_in, in_pos, n_in2);
1041 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, n_in2,
1042 n_in1 - in_pos);
1043 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, 0, in_pos);
1045 return FN(MULTI(BASE),range_splice)(multi1, out_pos, multi2);
1046 error:
1047 FN(MULTI(BASE),free)(multi1);
1048 FN(MULTI(BASE),free)(multi2);
1049 return NULL;
1051 #endif
1053 /* This function is currently only used from isl_aff.c
1055 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
1056 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
1057 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
1058 __attribute__ ((unused));
1060 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
1061 * return the result.
1063 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
1064 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
1065 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
1067 int i;
1068 isl_ctx *ctx;
1070 multi1 = FN(MULTI(BASE),cow)(multi1);
1071 if (!multi1 || !multi2)
1072 goto error;
1074 ctx = FN(MULTI(BASE),get_ctx)(multi1);
1075 if (!isl_space_is_equal(multi1->space, multi2->space))
1076 isl_die(ctx, isl_error_invalid,
1077 "spaces don't match", goto error);
1079 for (i = 0; i < multi1->n; ++i) {
1080 multi1->p[i] = fn(multi1->p[i], FN(EL,copy)(multi2->p[i]));
1081 if (!multi1->p[i])
1082 goto error;
1085 FN(MULTI(BASE),free)(multi2);
1086 return multi1;
1087 error:
1088 FN(MULTI(BASE),free)(multi1);
1089 FN(MULTI(BASE),free)(multi2);
1090 return NULL;
1093 /* Subtract "multi2" from "multi1" and return the result.
1095 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
1097 static __isl_give MULTI(BASE) *FN(MULTI(BASE),sub_aligned)(
1098 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
1100 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,sub));
1103 /* Subtract "multi2" from "multi1" and return the result.
1105 __isl_give MULTI(BASE) *FN(MULTI(BASE),sub)(__isl_take MULTI(BASE) *multi1,
1106 __isl_take MULTI(BASE) *multi2)
1108 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
1109 &FN(MULTI(BASE),sub_aligned));
1112 /* Multiply the elements of "multi" by "v" and return the result.
1114 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_val)(__isl_take MULTI(BASE) *multi,
1115 __isl_take isl_val *v)
1117 int i;
1119 if (!multi || !v)
1120 goto error;
1122 if (isl_val_is_one(v)) {
1123 isl_val_free(v);
1124 return multi;
1127 if (!isl_val_is_rat(v))
1128 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1129 "expecting rational factor", goto error);
1131 multi = FN(MULTI(BASE),cow)(multi);
1132 if (!multi)
1133 return NULL;
1135 for (i = 0; i < multi->n; ++i) {
1136 multi->p[i] = FN(EL,scale_val)(multi->p[i], isl_val_copy(v));
1137 if (!multi->p[i])
1138 goto error;
1141 isl_val_free(v);
1142 return multi;
1143 error:
1144 isl_val_free(v);
1145 return FN(MULTI(BASE),free)(multi);
1148 /* Divide the elements of "multi" by "v" and return the result.
1150 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_val)(
1151 __isl_take MULTI(BASE) *multi, __isl_take isl_val *v)
1153 int i;
1155 if (!multi || !v)
1156 goto error;
1158 if (isl_val_is_one(v)) {
1159 isl_val_free(v);
1160 return multi;
1163 if (!isl_val_is_rat(v))
1164 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1165 "expecting rational factor", goto error);
1166 if (isl_val_is_zero(v))
1167 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1168 "cannot scale down by zero", goto error);
1170 multi = FN(MULTI(BASE),cow)(multi);
1171 if (!multi)
1172 return NULL;
1174 for (i = 0; i < multi->n; ++i) {
1175 multi->p[i] = FN(EL,scale_down_val)(multi->p[i],
1176 isl_val_copy(v));
1177 if (!multi->p[i])
1178 goto error;
1181 isl_val_free(v);
1182 return multi;
1183 error:
1184 isl_val_free(v);
1185 return FN(MULTI(BASE),free)(multi);
1188 /* Multiply the elements of "multi" by the corresponding element of "mv"
1189 * and return the result.
1191 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_multi_val)(
1192 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1194 int i;
1196 if (!multi || !mv)
1197 goto error;
1199 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1200 mv->space, isl_dim_set))
1201 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1202 "spaces don't match", goto error);
1204 multi = FN(MULTI(BASE),cow)(multi);
1205 if (!multi)
1206 return NULL;
1208 for (i = 0; i < multi->n; ++i) {
1209 isl_val *v;
1211 v = isl_multi_val_get_val(mv, i);
1212 multi->p[i] = FN(EL,scale_val)(multi->p[i], v);
1213 if (!multi->p[i])
1214 goto error;
1217 isl_multi_val_free(mv);
1218 return multi;
1219 error:
1220 isl_multi_val_free(mv);
1221 return FN(MULTI(BASE),free)(multi);
1224 /* Divide the elements of "multi" by the corresponding element of "mv"
1225 * and return the result.
1227 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_multi_val)(
1228 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1230 int i;
1232 if (!multi || !mv)
1233 goto error;
1235 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1236 mv->space, isl_dim_set))
1237 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1238 "spaces don't match", goto error);
1240 multi = FN(MULTI(BASE),cow)(multi);
1241 if (!multi)
1242 return NULL;
1244 for (i = 0; i < multi->n; ++i) {
1245 isl_val *v;
1247 v = isl_multi_val_get_val(mv, i);
1248 multi->p[i] = FN(EL,scale_down_val)(multi->p[i], v);
1249 if (!multi->p[i])
1250 goto error;
1253 isl_multi_val_free(mv);
1254 return multi;
1255 error:
1256 isl_multi_val_free(mv);
1257 return FN(MULTI(BASE),free)(multi);
1260 /* Compute the residues of the elements of "multi" modulo
1261 * the corresponding element of "mv" and return the result.
1263 __isl_give MULTI(BASE) *FN(MULTI(BASE),mod_multi_val)(
1264 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1266 int i;
1268 if (!multi || !mv)
1269 goto error;
1271 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1272 mv->space, isl_dim_set))
1273 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1274 "spaces don't match", goto error);
1276 multi = FN(MULTI(BASE),cow)(multi);
1277 if (!multi)
1278 return NULL;
1280 for (i = 0; i < multi->n; ++i) {
1281 isl_val *v;
1283 v = isl_multi_val_get_val(mv, i);
1284 multi->p[i] = FN(EL,mod_val)(multi->p[i], v);
1285 if (!multi->p[i])
1286 goto error;
1289 isl_multi_val_free(mv);
1290 return multi;
1291 error:
1292 isl_multi_val_free(mv);
1293 return FN(MULTI(BASE),free)(multi);
1296 #ifndef NO_MOVE_DIMS
1297 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1298 * to dimensions of "dst_type" at "dst_pos".
1300 * We only support moving input dimensions to parameters and vice versa.
1302 __isl_give MULTI(BASE) *FN(MULTI(BASE),move_dims)(__isl_take MULTI(BASE) *multi,
1303 enum isl_dim_type dst_type, unsigned dst_pos,
1304 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1306 int i;
1308 if (!multi)
1309 return NULL;
1311 if (n == 0 &&
1312 !isl_space_is_named_or_nested(multi->space, src_type) &&
1313 !isl_space_is_named_or_nested(multi->space, dst_type))
1314 return multi;
1316 if (dst_type == isl_dim_out || src_type == isl_dim_out)
1317 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1318 "cannot move output/set dimension",
1319 return FN(MULTI(BASE),free)(multi));
1320 if (dst_type == isl_dim_div || src_type == isl_dim_div)
1321 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1322 "cannot move divs",
1323 return FN(MULTI(BASE),free)(multi));
1324 if (src_pos + n > isl_space_dim(multi->space, src_type))
1325 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1326 "range out of bounds",
1327 return FN(MULTI(BASE),free)(multi));
1328 if (dst_type == src_type)
1329 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_unsupported,
1330 "moving dims within the same type not supported",
1331 return FN(MULTI(BASE),free)(multi));
1333 multi = FN(MULTI(BASE),cow)(multi);
1334 if (!multi)
1335 return NULL;
1337 multi->space = isl_space_move_dims(multi->space, dst_type, dst_pos,
1338 src_type, src_pos, n);
1339 if (!multi->space)
1340 return FN(MULTI(BASE),free)(multi);
1342 for (i = 0; i < multi->n; ++i) {
1343 multi->p[i] = FN(EL,move_dims)(multi->p[i], dst_type, dst_pos,
1344 src_type, src_pos, n);
1345 if (!multi->p[i])
1346 return FN(MULTI(BASE),free)(multi);
1349 return multi;
1351 #endif
1353 /* Convert a multiple expression defined over a parameter domain
1354 * into one that is defined over a zero-dimensional set.
1356 __isl_give MULTI(BASE) *FN(MULTI(BASE),from_range)(
1357 __isl_take MULTI(BASE) *multi)
1359 isl_space *space;
1361 if (!multi)
1362 return NULL;
1363 if (!isl_space_is_set(multi->space))
1364 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1365 "not living in a set space",
1366 return FN(MULTI(BASE),free)(multi));
1368 space = FN(MULTI(BASE),get_space)(multi);
1369 space = isl_space_from_range(space);
1370 multi = FN(MULTI(BASE),reset_space)(multi, space);
1372 return multi;
1375 /* Are "multi1" and "multi2" obviously equal?
1377 int FN(MULTI(BASE),plain_is_equal)(__isl_keep MULTI(BASE) *multi1,
1378 __isl_keep MULTI(BASE) *multi2)
1380 int i;
1381 int equal;
1383 if (!multi1 || !multi2)
1384 return -1;
1385 if (multi1->n != multi2->n)
1386 return 0;
1387 equal = isl_space_is_equal(multi1->space, multi2->space);
1388 if (equal < 0 || !equal)
1389 return equal;
1391 for (i = 0; i < multi1->n; ++i) {
1392 equal = FN(EL,plain_is_equal)(multi1->p[i], multi2->p[i]);
1393 if (equal < 0 || !equal)
1394 return equal;
1397 return 1;
1400 #ifndef NO_DOMAIN
1401 /* Return the shared domain of the elements of "multi".
1403 __isl_give isl_set *FN(MULTI(BASE),domain)(__isl_take MULTI(BASE) *multi)
1405 int i;
1406 isl_set *dom;
1408 if (!multi)
1409 return NULL;
1411 dom = isl_set_universe(FN(MULTI(BASE),get_domain_space)(multi));
1412 for (i = 0; i < multi->n; ++i) {
1413 isl_set *dom_i;
1415 dom_i = FN(EL,domain)(FN(FN(MULTI(BASE),get),BASE)(multi, i));
1416 dom = isl_set_intersect(dom, dom_i);
1419 FN(MULTI(BASE),free)(multi);
1420 return dom;
1422 #endif
1424 #ifndef NO_NEG
1425 /* Return the opposite of "multi".
1427 __isl_give MULTI(BASE) *FN(MULTI(BASE),neg)(__isl_take MULTI(BASE) *multi)
1429 int i;
1431 multi = FN(MULTI(BASE),cow)(multi);
1432 if (!multi)
1433 return NULL;
1435 for (i = 0; i < multi->n; ++i) {
1436 multi->p[i] = FN(EL,neg)(multi->p[i]);
1437 if (!multi->p[i])
1438 return FN(MULTI(BASE),free)(multi);
1441 return multi;
1443 #endif