add isl_union_map_range_factor_range
[isl.git] / isl_multi_templ.c
blob69dc93c30075d85dcac6e9e9b15f1d11c442b44e
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 #define xCAT(A,B) A ## B
16 #define CAT(A,B) xCAT(A,B)
17 #undef EL
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 /* Return the position of the dimension of the given type and name
38 * in "multi".
39 * Return -1 if no such dimension can be found.
41 int FN(MULTI(BASE),find_dim_by_name)(__isl_keep MULTI(BASE) *multi,
42 enum isl_dim_type type, const char *name)
44 if (!multi)
45 return -1;
46 return isl_space_find_dim_by_name(multi->space, type, name);
49 __isl_give isl_space *FN(MULTI(BASE),get_domain_space)(
50 __isl_keep MULTI(BASE) *multi)
52 return multi ? isl_space_domain(isl_space_copy(multi->space)) : NULL;
55 __isl_give MULTI(BASE) *FN(MULTI(BASE),alloc)(__isl_take isl_space *space)
57 isl_ctx *ctx;
58 int n;
59 MULTI(BASE) *multi;
61 if (!space)
62 return NULL;
64 ctx = isl_space_get_ctx(space);
65 n = isl_space_dim(space, isl_dim_out);
66 multi = isl_calloc(ctx, MULTI(BASE),
67 sizeof(MULTI(BASE)) + (n - 1) * sizeof(struct EL *));
68 if (!multi)
69 goto error;
71 multi->space = space;
72 multi->n = n;
73 multi->ref = 1;
74 return multi;
75 error:
76 isl_space_free(space);
77 return NULL;
80 __isl_give MULTI(BASE) *FN(MULTI(BASE),dup)(__isl_keep MULTI(BASE) *multi)
82 int i;
83 MULTI(BASE) *dup;
85 if (!multi)
86 return NULL;
88 dup = FN(MULTI(BASE),alloc)(isl_space_copy(multi->space));
89 if (!dup)
90 return NULL;
92 for (i = 0; i < multi->n; ++i)
93 dup = FN(FN(MULTI(BASE),set),BASE)(dup, i,
94 FN(EL,copy)(multi->p[i]));
96 return dup;
99 __isl_give MULTI(BASE) *FN(MULTI(BASE),cow)(__isl_take MULTI(BASE) *multi)
101 if (!multi)
102 return NULL;
104 if (multi->ref == 1)
105 return multi;
107 multi->ref--;
108 return FN(MULTI(BASE),dup)(multi);
111 __isl_give MULTI(BASE) *FN(MULTI(BASE),copy)(__isl_keep MULTI(BASE) *multi)
113 if (!multi)
114 return NULL;
116 multi->ref++;
117 return multi;
120 __isl_null MULTI(BASE) *FN(MULTI(BASE),free)(__isl_take MULTI(BASE) *multi)
122 int i;
124 if (!multi)
125 return NULL;
127 if (--multi->ref > 0)
128 return NULL;
130 isl_space_free(multi->space);
131 for (i = 0; i < multi->n; ++i)
132 FN(EL,free)(multi->p[i]);
133 free(multi);
135 return NULL;
138 /* Check whether "multi" has non-zero coefficients for any dimension
139 * in the given range or if any of these dimensions appear
140 * with non-zero coefficients in any of the integer divisions involved.
142 int FN(MULTI(BASE),involves_dims)(__isl_keep MULTI(BASE) *multi,
143 enum isl_dim_type type, unsigned first, unsigned n)
145 int i;
147 if (!multi)
148 return -1;
149 if (multi->n == 0 || n == 0)
150 return 0;
152 for (i = 0; i < multi->n; ++i) {
153 int involves;
155 involves = FN(EL,involves_dims)(multi->p[i], type, first, n);
156 if (involves < 0 || involves)
157 return involves;
160 return 0;
163 __isl_give MULTI(BASE) *FN(MULTI(BASE),insert_dims)(
164 __isl_take MULTI(BASE) *multi,
165 enum isl_dim_type type, unsigned first, unsigned n)
167 int i;
169 if (!multi)
170 return NULL;
171 if (type == isl_dim_out)
172 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
173 "cannot insert output/set dimensions",
174 return FN(MULTI(BASE),free)(multi));
175 if (n == 0 && !isl_space_is_named_or_nested(multi->space, type))
176 return multi;
178 multi = FN(MULTI(BASE),cow)(multi);
179 if (!multi)
180 return NULL;
182 multi->space = isl_space_insert_dims(multi->space, type, first, n);
183 if (!multi->space)
184 return FN(MULTI(BASE),free)(multi);
186 for (i = 0; i < multi->n; ++i) {
187 multi->p[i] = FN(EL,insert_dims)(multi->p[i], type, first, n);
188 if (!multi->p[i])
189 return FN(MULTI(BASE),free)(multi);
192 return multi;
195 __isl_give MULTI(BASE) *FN(MULTI(BASE),add_dims)(__isl_take MULTI(BASE) *multi,
196 enum isl_dim_type type, unsigned n)
198 unsigned pos;
200 pos = FN(MULTI(BASE),dim)(multi, type);
202 return FN(MULTI(BASE),insert_dims)(multi, type, pos, n);
205 unsigned FN(MULTI(BASE),dim)(__isl_keep MULTI(BASE) *multi,
206 enum isl_dim_type type)
208 return multi ? isl_space_dim(multi->space, type) : 0;
211 /* Return the position of the first dimension of "type" with id "id".
212 * Return -1 if there is no such dimension.
214 int FN(MULTI(BASE),find_dim_by_id)(__isl_keep MULTI(BASE) *multi,
215 enum isl_dim_type type, __isl_keep isl_id *id)
217 if (!multi)
218 return -1;
219 return isl_space_find_dim_by_id(multi->space, type, id);
222 /* Return the id of the given dimension.
224 __isl_give isl_id *FN(MULTI(BASE),get_dim_id)(__isl_keep MULTI(BASE) *multi,
225 enum isl_dim_type type, unsigned pos)
227 return multi ? isl_space_get_dim_id(multi->space, type, pos) : NULL;
230 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_name)(
231 __isl_take MULTI(BASE) *multi,
232 enum isl_dim_type type, unsigned pos, const char *s)
234 int i;
236 multi = FN(MULTI(BASE),cow)(multi);
237 if (!multi)
238 return NULL;
240 multi->space = isl_space_set_dim_name(multi->space, type, pos, s);
241 if (!multi->space)
242 return FN(MULTI(BASE),free)(multi);
244 if (type == isl_dim_out)
245 return multi;
246 for (i = 0; i < multi->n; ++i) {
247 multi->p[i] = FN(EL,set_dim_name)(multi->p[i], type, pos, s);
248 if (!multi->p[i])
249 return FN(MULTI(BASE),free)(multi);
252 return multi;
255 const char *FN(MULTI(BASE),get_tuple_name)(__isl_keep MULTI(BASE) *multi,
256 enum isl_dim_type type)
258 return multi ? isl_space_get_tuple_name(multi->space, type) : NULL;
261 /* Does the specified tuple have an id?
263 int FN(MULTI(BASE),has_tuple_id)(__isl_keep MULTI(BASE) *multi,
264 enum isl_dim_type type)
266 return multi ? isl_space_has_tuple_id(multi->space, type) : -1;
269 /* Return the id of the specified tuple.
271 __isl_give isl_id *FN(MULTI(BASE),get_tuple_id)(__isl_keep MULTI(BASE) *multi,
272 enum isl_dim_type type)
274 return multi ? isl_space_get_tuple_id(multi->space, type) : NULL;
277 __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
278 int pos)
280 isl_ctx *ctx;
282 if (!multi)
283 return NULL;
284 ctx = FN(MULTI(BASE),get_ctx)(multi);
285 if (pos < 0 || pos >= multi->n)
286 isl_die(ctx, isl_error_invalid,
287 "index out of bounds", return NULL);
288 return FN(EL,copy)(multi->p[pos]);
291 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
292 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
294 isl_space *multi_space = NULL;
295 isl_space *el_space = NULL;
296 int match;
298 multi = FN(MULTI(BASE),cow)(multi);
299 if (!multi || !el)
300 goto error;
302 multi_space = FN(MULTI(BASE),get_space)(multi);
303 match = FN(EL,matching_params)(el, multi_space);
304 if (match < 0)
305 goto error;
306 if (!match) {
307 multi = FN(MULTI(BASE),align_params)(multi,
308 FN(EL,get_space)(el));
309 isl_space_free(multi_space);
310 multi_space = FN(MULTI(BASE),get_space)(multi);
311 el = FN(EL,align_params)(el, isl_space_copy(multi_space));
313 if (FN(EL,check_match_domain_space)(el, multi_space) < 0)
314 goto error;
316 if (pos < 0 || pos >= multi->n)
317 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
318 "index out of bounds", goto error);
320 FN(EL,free)(multi->p[pos]);
321 multi->p[pos] = el;
323 isl_space_free(multi_space);
324 isl_space_free(el_space);
326 return multi;
327 error:
328 FN(MULTI(BASE),free)(multi);
329 FN(EL,free)(el);
330 isl_space_free(multi_space);
331 isl_space_free(el_space);
332 return NULL;
335 /* Reset the space of "multi". This function is called from isl_pw_templ.c
336 * and doesn't know if the space of an element object is represented
337 * directly or through its domain. It therefore passes along both,
338 * which we pass along to the element function since we don't how
339 * that is represented either.
341 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
342 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
343 __isl_take isl_space *domain)
345 int i;
347 multi = FN(MULTI(BASE),cow)(multi);
348 if (!multi || !space || !domain)
349 goto error;
351 for (i = 0; i < multi->n; ++i) {
352 multi->p[i] = FN(EL,reset_domain_space)(multi->p[i],
353 isl_space_copy(domain));
354 if (!multi->p[i])
355 goto error;
357 isl_space_free(domain);
358 isl_space_free(multi->space);
359 multi->space = space;
361 return multi;
362 error:
363 isl_space_free(domain);
364 isl_space_free(space);
365 FN(MULTI(BASE),free)(multi);
366 return NULL;
369 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
370 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
372 isl_space *space;
374 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
375 isl_space_copy(multi->space));
376 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
379 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
380 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
382 isl_space *domain;
384 domain = isl_space_domain(isl_space_copy(space));
385 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
388 /* Set the id of the given dimension of "multi" to "id".
390 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_id)(
391 __isl_take MULTI(BASE) *multi,
392 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
394 isl_space *space;
396 multi = FN(MULTI(BASE),cow)(multi);
397 if (!multi || !id)
398 goto error;
400 space = FN(MULTI(BASE),get_space)(multi);
401 space = isl_space_set_dim_id(space, type, pos, id);
403 return FN(MULTI(BASE),reset_space)(multi, space);
404 error:
405 isl_id_free(id);
406 FN(MULTI(BASE),free)(multi);
407 return NULL;
410 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_name)(
411 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
412 const char *s)
414 isl_space *space;
416 multi = FN(MULTI(BASE),cow)(multi);
417 if (!multi)
418 return NULL;
420 space = FN(MULTI(BASE),get_space)(multi);
421 space = isl_space_set_tuple_name(space, type, s);
423 return FN(MULTI(BASE),reset_space)(multi, space);
426 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_id)(
427 __isl_take MULTI(BASE) *multi, enum isl_dim_type type,
428 __isl_take isl_id *id)
430 isl_space *space;
432 multi = FN(MULTI(BASE),cow)(multi);
433 if (!multi)
434 goto error;
436 space = FN(MULTI(BASE),get_space)(multi);
437 space = isl_space_set_tuple_id(space, type, id);
439 return FN(MULTI(BASE),reset_space)(multi, space);
440 error:
441 isl_id_free(id);
442 return NULL;
445 /* Drop the id on the specified tuple.
447 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_tuple_id)(
448 __isl_take MULTI(BASE) *multi, enum isl_dim_type type)
450 isl_space *space;
452 if (!multi)
453 return NULL;
454 if (!FN(MULTI(BASE),has_tuple_id)(multi, type))
455 return multi;
457 multi = FN(MULTI(BASE),cow)(multi);
458 if (!multi)
459 return NULL;
461 space = FN(MULTI(BASE),get_space)(multi);
462 space = isl_space_reset_tuple_id(space, type);
464 return FN(MULTI(BASE),reset_space)(multi, space);
467 /* Reset the user pointer on all identifiers of parameters and tuples
468 * of the space of "multi".
470 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_user)(
471 __isl_take MULTI(BASE) *multi)
473 isl_space *space;
475 space = FN(MULTI(BASE),get_space)(multi);
476 space = isl_space_reset_user(space);
478 return FN(MULTI(BASE),reset_space)(multi, space);
481 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
482 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
484 int i;
486 multi = FN(MULTI(BASE),cow)(multi);
487 if (!multi || !exp)
488 goto error;
490 for (i = 0; i < multi->n; ++i) {
491 multi->p[i] = FN(EL,realign_domain)(multi->p[i],
492 isl_reordering_copy(exp));
493 if (!multi->p[i])
494 goto error;
497 multi = FN(MULTI(BASE),reset_domain_space)(multi,
498 isl_space_copy(exp->dim));
500 isl_reordering_free(exp);
501 return multi;
502 error:
503 isl_reordering_free(exp);
504 FN(MULTI(BASE),free)(multi);
505 return NULL;
508 /* Align the parameters of "multi" to those of "model".
510 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
511 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
513 isl_ctx *ctx;
514 isl_reordering *exp;
516 if (!multi || !model)
517 goto error;
519 if (isl_space_match(multi->space, isl_dim_param,
520 model, isl_dim_param)) {
521 isl_space_free(model);
522 return multi;
525 ctx = isl_space_get_ctx(model);
526 if (!isl_space_has_named_params(model))
527 isl_die(ctx, isl_error_invalid,
528 "model has unnamed parameters", goto error);
529 if (!isl_space_has_named_params(multi->space))
530 isl_die(ctx, isl_error_invalid,
531 "input has unnamed parameters", goto error);
533 model = isl_space_params(model);
534 exp = isl_parameter_alignment_reordering(multi->space, model);
535 exp = isl_reordering_extend_space(exp,
536 FN(MULTI(BASE),get_domain_space)(multi));
537 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
539 isl_space_free(model);
540 return multi;
541 error:
542 isl_space_free(model);
543 FN(MULTI(BASE),free)(multi);
544 return NULL;
547 #if !defined(NO_GIST) || !defined(NO_INTERSECT_DOMAIN)
548 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_set_and)(
549 __isl_take MULTI(BASE) *multi, __isl_take isl_set *set,
550 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi,
551 __isl_take isl_set *set))
553 isl_ctx *ctx;
555 if (!multi || !set)
556 goto error;
557 if (isl_space_match(multi->space, isl_dim_param,
558 set->dim, isl_dim_param))
559 return fn(multi, set);
560 ctx = FN(MULTI(BASE),get_ctx)(multi);
561 if (!isl_space_has_named_params(multi->space) ||
562 !isl_space_has_named_params(set->dim))
563 isl_die(ctx, isl_error_invalid,
564 "unaligned unnamed parameters", goto error);
565 multi = FN(MULTI(BASE),align_params)(multi, isl_set_get_space(set));
566 set = isl_set_align_params(set, FN(MULTI(BASE),get_space)(multi));
567 return fn(multi, set);
568 error:
569 FN(MULTI(BASE),free)(multi);
570 isl_set_free(set);
571 return NULL;
573 #endif
575 #ifndef NO_GIST
576 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_aligned)(
577 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
579 int i;
581 multi = FN(MULTI(BASE),cow)(multi);
582 if (!multi || !context)
583 goto error;
585 for (i = 0; i < multi->n; ++i) {
586 multi->p[i] = FN(EL,gist)(multi->p[i], isl_set_copy(context));
587 if (!multi->p[i])
588 goto error;
591 isl_set_free(context);
592 return multi;
593 error:
594 isl_set_free(context);
595 FN(MULTI(BASE),free)(multi);
596 return NULL;
599 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist)(__isl_take MULTI(BASE) *multi,
600 __isl_take isl_set *context)
602 return FN(MULTI(BASE),align_params_multi_set_and)(multi, context,
603 &FN(MULTI(BASE),gist_aligned));
606 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_params)(
607 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
609 isl_space *space = FN(MULTI(BASE),get_domain_space)(multi);
610 isl_set *dom_context = isl_set_universe(space);
611 dom_context = isl_set_intersect_params(dom_context, context);
612 return FN(MULTI(BASE),gist)(multi, dom_context);
614 #endif
616 #ifndef NO_INTERSECT_DOMAIN
617 /* Transform the domain of "multi" by combining it with "domain"
618 * using "fn".
620 * The parameters of "multi" and "domain" are assumed to have been aligned.
622 __isl_give MULTI(BASE) *FN(MULTI(BASE),intersect_aligned)(
623 __isl_take MULTI(BASE) *multi, __isl_take isl_set *domain,
624 __isl_give EL *(*fn)(EL *el, __isl_take isl_set *set2))
626 int i;
628 if (!multi || !domain)
629 goto error;
631 if (multi->n == 0) {
632 isl_set_free(domain);
633 return multi;
636 multi = FN(MULTI(BASE),cow)(multi);
637 if (!multi)
638 goto error;
640 for (i = 0; i < multi->n; ++i) {
641 multi->p[i] = fn(multi->p[i], isl_set_copy(domain));
642 if (!multi->p[i])
643 goto error;
646 isl_set_free(domain);
647 return multi;
648 error:
649 isl_set_free(domain);
650 FN(MULTI(BASE),free)(multi);
651 return NULL;
654 /* Intersect the domain of "multi" with "domain".
656 * The parameters of "multi" and "domain" are assumed to have been aligned.
658 __isl_give MULTI(BASE) *FN(MULTI(BASE),intersect_domain_aligned)(
659 __isl_take MULTI(BASE) *multi, __isl_take isl_set *domain)
661 return FN(MULTI(BASE),intersect_aligned)(multi, domain,
662 &FN(EL,intersect_domain));
665 /* Intersect the domain of "multi" with "domain".
667 __isl_give MULTI(BASE) *FN(MULTI(BASE),intersect_domain)(
668 __isl_take MULTI(BASE) *multi, __isl_take isl_set *domain)
670 return FN(MULTI(BASE),align_params_multi_set_and)(multi, domain,
671 &FN(MULTI(BASE),intersect_domain_aligned));
674 /* Intersect the parameter domain of "multi" with "domain".
676 * The parameters of "multi" and "domain" are assumed to have been aligned.
678 __isl_give MULTI(BASE) *FN(MULTI(BASE),intersect_params_aligned)(
679 __isl_take MULTI(BASE) *multi, __isl_take isl_set *domain)
681 return FN(MULTI(BASE),intersect_aligned)(multi, domain,
682 &FN(EL,intersect_params));
685 /* Intersect the parameter domain of "multi" with "domain".
687 __isl_give MULTI(BASE) *FN(MULTI(BASE),intersect_params)(
688 __isl_take MULTI(BASE) *multi, __isl_take isl_set *domain)
690 return FN(MULTI(BASE),align_params_multi_set_and)(multi, domain,
691 &FN(MULTI(BASE),intersect_params_aligned));
693 #endif
695 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
696 __isl_take isl_space *space, __isl_take LIST(EL) *list)
698 int i;
699 int n;
700 isl_ctx *ctx;
701 MULTI(BASE) *multi;
703 if (!space || !list)
704 goto error;
706 ctx = isl_space_get_ctx(space);
707 n = FN(FN(LIST(EL),n),BASE)(list);
708 if (n != isl_space_dim(space, isl_dim_out))
709 isl_die(ctx, isl_error_invalid,
710 "invalid number of elements in list", goto error);
712 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
713 for (i = 0; i < n; ++i) {
714 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
715 FN(FN(LIST(EL),get),BASE)(list, i));
718 isl_space_free(space);
719 FN(LIST(EL),free)(list);
720 return multi;
721 error:
722 isl_space_free(space);
723 FN(LIST(EL),free)(list);
724 return NULL;
727 #ifndef NO_IDENTITY
728 /* Create a multi expression in the given space that maps each
729 * input dimension to the corresponding output dimension.
731 __isl_give MULTI(BASE) *FN(MULTI(BASE),identity)(__isl_take isl_space *space)
733 int i, n;
734 isl_local_space *ls;
735 MULTI(BASE) *multi;
737 if (!space)
738 return NULL;
740 if (isl_space_is_set(space))
741 isl_die(isl_space_get_ctx(space), isl_error_invalid,
742 "expecting map space", goto error);
744 n = isl_space_dim(space, isl_dim_out);
745 if (n != isl_space_dim(space, isl_dim_in))
746 isl_die(isl_space_get_ctx(space), isl_error_invalid,
747 "number of input and output dimensions needs to be "
748 "the same", goto error);
750 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
752 if (!n) {
753 isl_space_free(space);
754 return multi;
757 space = isl_space_domain(space);
758 ls = isl_local_space_from_space(space);
760 for (i = 0; i < n; ++i) {
761 EL *el;
762 el = FN(EL,var_on_domain)(isl_local_space_copy(ls),
763 isl_dim_set, i);
764 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i, el);
767 isl_local_space_free(ls);
769 return multi;
770 error:
771 isl_space_free(space);
772 return NULL;
774 #endif
776 /* Construct a multi expression in the given space with value zero in
777 * each of the output dimensions.
779 __isl_give MULTI(BASE) *FN(MULTI(BASE),zero)(__isl_take isl_space *space)
781 int n;
782 MULTI(BASE) *multi;
784 if (!space)
785 return NULL;
787 n = isl_space_dim(space , isl_dim_out);
788 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
790 if (!n)
791 isl_space_free(space);
792 else {
793 int i;
794 isl_local_space *ls;
795 EL *el;
797 space = isl_space_domain(space);
798 ls = isl_local_space_from_space(space);
799 el = FN(EL,zero_on_domain)(ls);
801 for (i = 0; i < n; ++i)
802 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
803 FN(EL,copy)(el));
805 FN(EL,free)(el);
808 return multi;
811 #ifndef NO_FROM_BASE
812 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),BASE)(__isl_take EL *el)
814 MULTI(BASE) *multi;
816 multi = FN(MULTI(BASE),alloc)(FN(EL,get_space)(el));
817 multi = FN(FN(MULTI(BASE),set),BASE)(multi, 0, el);
819 return multi;
821 #endif
823 __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
824 __isl_take MULTI(BASE) *multi,
825 enum isl_dim_type type, unsigned first, unsigned n)
827 int i;
828 unsigned dim;
830 multi = FN(MULTI(BASE),cow)(multi);
831 if (!multi)
832 return NULL;
834 dim = FN(MULTI(BASE),dim)(multi, type);
835 if (first + n > dim || first + n < first)
836 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
837 "index out of bounds",
838 return FN(MULTI(BASE),cow)(multi));
840 multi->space = isl_space_drop_dims(multi->space, type, first, n);
841 if (!multi->space)
842 return FN(MULTI(BASE),cow)(multi);
844 if (type == isl_dim_out) {
845 for (i = 0; i < n; ++i)
846 FN(EL,free)(multi->p[first + i]);
847 for (i = first; i + n < multi->n; ++i)
848 multi->p[i] = multi->p[i + n];
849 multi->n -= n;
851 return multi;
854 for (i = 0; i < multi->n; ++i) {
855 multi->p[i] = FN(EL,drop_dims)(multi->p[i], type, first, n);
856 if (!multi->p[i])
857 return FN(MULTI(BASE),cow)(multi);
860 return multi;
863 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
865 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_multi_and)(
866 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
867 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi1,
868 __isl_take MULTI(BASE) *multi2))
870 isl_ctx *ctx;
872 if (!multi1 || !multi2)
873 goto error;
874 if (isl_space_match(multi1->space, isl_dim_param,
875 multi2->space, isl_dim_param))
876 return fn(multi1, multi2);
877 ctx = FN(MULTI(BASE),get_ctx)(multi1);
878 if (!isl_space_has_named_params(multi1->space) ||
879 !isl_space_has_named_params(multi2->space))
880 isl_die(ctx, isl_error_invalid,
881 "unaligned unnamed parameters", goto error);
882 multi1 = FN(MULTI(BASE),align_params)(multi1,
883 FN(MULTI(BASE),get_space)(multi2));
884 multi2 = FN(MULTI(BASE),align_params)(multi2,
885 FN(MULTI(BASE),get_space)(multi1));
886 return fn(multi1, multi2);
887 error:
888 FN(MULTI(BASE),free)(multi1);
889 FN(MULTI(BASE),free)(multi2);
890 return NULL;
893 /* Given two MULTI(BASE)s A -> B and C -> D,
894 * construct a MULTI(BASE) (A * C) -> [B -> D].
896 * The parameters are assumed to have been aligned.
898 static __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product_aligned)(
899 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
901 int i, n1, n2;
902 EL *el;
903 isl_space *space;
904 MULTI(BASE) *res;
906 if (!multi1 || !multi2)
907 goto error;
909 space = isl_space_range_product(FN(MULTI(BASE),get_space)(multi1),
910 FN(MULTI(BASE),get_space)(multi2));
911 res = FN(MULTI(BASE),alloc)(space);
913 n1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
914 n2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
916 for (i = 0; i < n1; ++i) {
917 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
918 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
921 for (i = 0; i < n2; ++i) {
922 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
923 res = FN(FN(MULTI(BASE),set),BASE)(res, n1 + i, el);
926 FN(MULTI(BASE),free)(multi1);
927 FN(MULTI(BASE),free)(multi2);
928 return res;
929 error:
930 FN(MULTI(BASE),free)(multi1);
931 FN(MULTI(BASE),free)(multi2);
932 return NULL;
935 /* Given two MULTI(BASE)s A -> B and C -> D,
936 * construct a MULTI(BASE) (A * C) -> [B -> D].
938 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product)(
939 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
941 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
942 &FN(MULTI(BASE),range_product_aligned));
945 /* Is the range of "multi" a wrapped relation?
947 int FN(MULTI(BASE),range_is_wrapping)(__isl_keep MULTI(BASE) *multi)
949 if (!multi)
950 return -1;
951 return isl_space_range_is_wrapping(multi->space);
954 /* Given a function A -> [B -> C], extract the function A -> B.
956 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_domain)(
957 __isl_take MULTI(BASE) *multi)
959 isl_space *space;
960 int total, keep;
962 if (!multi)
963 return NULL;
964 if (!isl_space_range_is_wrapping(multi->space))
965 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
966 "range is not a product",
967 return FN(MULTI(BASE),free)(multi));
969 space = FN(MULTI(BASE),get_space)(multi);
970 total = isl_space_dim(space, isl_dim_out);
971 space = isl_space_range_factor_domain(space);
972 keep = isl_space_dim(space, isl_dim_out);
973 multi = FN(MULTI(BASE),drop_dims)(multi,
974 isl_dim_out, keep, total - keep);
975 multi = FN(MULTI(BASE),reset_space)(multi, space);
977 return multi;
980 /* Given a function A -> [B -> C], extract the function A -> C.
982 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_range)(
983 __isl_take MULTI(BASE) *multi)
985 isl_space *space;
986 int total, keep;
988 if (!multi)
989 return NULL;
990 if (!isl_space_range_is_wrapping(multi->space))
991 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
992 "range is not a product",
993 return FN(MULTI(BASE),free)(multi));
995 space = FN(MULTI(BASE),get_space)(multi);
996 total = isl_space_dim(space, isl_dim_out);
997 space = isl_space_range_factor_range(space);
998 keep = isl_space_dim(space, isl_dim_out);
999 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_out, 0, total - keep);
1000 multi = FN(MULTI(BASE),reset_space)(multi, space);
1002 return multi;
1005 /* Given two MULTI(BASE)s A -> B and C -> D,
1006 * construct a MULTI(BASE) [A -> C] -> [B -> D].
1008 * The parameters are assumed to have been aligned.
1010 __isl_give MULTI(BASE) *FN(MULTI(BASE),product_aligned)(
1011 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
1013 int i;
1014 EL *el;
1015 isl_space *space;
1016 MULTI(BASE) *res;
1017 int in1, in2, out1, out2;
1019 in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
1020 in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
1021 out1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
1022 out2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
1023 space = isl_space_product(FN(MULTI(BASE),get_space)(multi1),
1024 FN(MULTI(BASE),get_space)(multi2));
1025 res = FN(MULTI(BASE),alloc)(isl_space_copy(space));
1026 space = isl_space_domain(space);
1028 for (i = 0; i < out1; ++i) {
1029 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
1030 el = FN(EL,insert_dims)(el, isl_dim_in, in1, in2);
1031 el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
1032 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
1035 for (i = 0; i < out2; ++i) {
1036 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
1037 el = FN(EL,insert_dims)(el, isl_dim_in, 0, in1);
1038 el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
1039 res = FN(FN(MULTI(BASE),set),BASE)(res, out1 + i, el);
1042 isl_space_free(space);
1043 FN(MULTI(BASE),free)(multi1);
1044 FN(MULTI(BASE),free)(multi2);
1045 return res;
1048 /* Given two MULTI(BASE)s A -> B and C -> D,
1049 * construct a MULTI(BASE) [A -> C] -> [B -> D].
1051 __isl_give MULTI(BASE) *FN(MULTI(BASE),product)(
1052 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
1054 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
1055 &FN(MULTI(BASE),product_aligned));
1058 __isl_give MULTI(BASE) *FN(MULTI(BASE),flatten_range)(
1059 __isl_take MULTI(BASE) *multi)
1061 if (!multi)
1062 return NULL;
1064 if (!multi->space->nested[1])
1065 return multi;
1067 multi = FN(MULTI(BASE),cow)(multi);
1068 if (!multi)
1069 return NULL;
1071 multi->space = isl_space_flatten_range(multi->space);
1072 if (!multi->space)
1073 return FN(MULTI(BASE),free)(multi);
1075 return multi;
1078 /* Given two MULTI(BASE)s A -> B and C -> D,
1079 * construct a MULTI(BASE) (A * C) -> (B, D).
1081 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
1082 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
1084 MULTI(BASE) *multi;
1086 multi = FN(MULTI(BASE),range_product)(multi1, multi2);
1087 multi = FN(MULTI(BASE),flatten_range)(multi);
1088 return multi;
1091 /* Given two multi expressions, "multi1"
1093 * [A] -> [B1 B2]
1095 * where B2 starts at position "pos", and "multi2"
1097 * [A] -> [D]
1099 * return the multi expression
1101 * [A] -> [B1 D B2]
1103 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_splice)(
1104 __isl_take MULTI(BASE) *multi1, unsigned pos,
1105 __isl_take MULTI(BASE) *multi2)
1107 MULTI(BASE) *res;
1108 unsigned dim;
1110 if (!multi1 || !multi2)
1111 goto error;
1113 dim = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
1114 if (pos > dim)
1115 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
1116 "index out of bounds", goto error);
1118 res = FN(MULTI(BASE),copy)(multi1);
1119 res = FN(MULTI(BASE),drop_dims)(res, isl_dim_out, pos, dim - pos);
1120 multi1 = FN(MULTI(BASE),drop_dims)(multi1, isl_dim_out, 0, pos);
1122 res = FN(MULTI(BASE),flat_range_product)(res, multi2);
1123 res = FN(MULTI(BASE),flat_range_product)(res, multi1);
1125 return res;
1126 error:
1127 FN(MULTI(BASE),free)(multi1);
1128 FN(MULTI(BASE),free)(multi2);
1129 return NULL;
1132 /* Given two multi expressions, "multi1"
1134 * [A1 A2] -> [B1 B2]
1136 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
1137 * and "multi2"
1139 * [C] -> [D]
1141 * return the multi expression
1143 * [A1 C A2] -> [B1 D B2]
1145 * We first insert input dimensions to obtain
1147 * [A1 C A2] -> [B1 B2]
1149 * and
1151 * [A1 C A2] -> [D]
1153 * and then apply range_splice.
1155 __isl_give MULTI(BASE) *FN(MULTI(BASE),splice)(
1156 __isl_take MULTI(BASE) *multi1, unsigned in_pos, unsigned out_pos,
1157 __isl_take MULTI(BASE) *multi2)
1159 unsigned n_in1;
1160 unsigned n_in2;
1162 if (!multi1 || !multi2)
1163 goto error;
1165 n_in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
1166 if (in_pos > n_in1)
1167 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
1168 "index out of bounds", goto error);
1170 n_in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
1172 multi1 = FN(MULTI(BASE),insert_dims)(multi1, isl_dim_in, in_pos, n_in2);
1173 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, n_in2,
1174 n_in1 - in_pos);
1175 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, 0, in_pos);
1177 return FN(MULTI(BASE),range_splice)(multi1, out_pos, multi2);
1178 error:
1179 FN(MULTI(BASE),free)(multi1);
1180 FN(MULTI(BASE),free)(multi2);
1181 return NULL;
1184 /* This function is currently only used from isl_aff.c
1186 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
1187 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
1188 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
1189 __attribute__ ((unused));
1191 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
1192 * return the result.
1194 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
1195 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
1196 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
1198 int i;
1199 isl_ctx *ctx;
1201 multi1 = FN(MULTI(BASE),cow)(multi1);
1202 if (!multi1 || !multi2)
1203 goto error;
1205 ctx = FN(MULTI(BASE),get_ctx)(multi1);
1206 if (!isl_space_is_equal(multi1->space, multi2->space))
1207 isl_die(ctx, isl_error_invalid,
1208 "spaces don't match", goto error);
1210 for (i = 0; i < multi1->n; ++i) {
1211 multi1->p[i] = fn(multi1->p[i], FN(EL,copy)(multi2->p[i]));
1212 if (!multi1->p[i])
1213 goto error;
1216 FN(MULTI(BASE),free)(multi2);
1217 return multi1;
1218 error:
1219 FN(MULTI(BASE),free)(multi1);
1220 FN(MULTI(BASE),free)(multi2);
1221 return NULL;
1224 /* Multiply the elements of "multi" by "v" and return the result.
1226 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_val)(__isl_take MULTI(BASE) *multi,
1227 __isl_take isl_val *v)
1229 int i;
1231 if (!multi || !v)
1232 goto error;
1234 if (isl_val_is_one(v)) {
1235 isl_val_free(v);
1236 return multi;
1239 if (!isl_val_is_rat(v))
1240 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1241 "expecting rational factor", goto error);
1243 multi = FN(MULTI(BASE),cow)(multi);
1244 if (!multi)
1245 return NULL;
1247 for (i = 0; i < multi->n; ++i) {
1248 multi->p[i] = FN(EL,scale_val)(multi->p[i], isl_val_copy(v));
1249 if (!multi->p[i])
1250 goto error;
1253 isl_val_free(v);
1254 return multi;
1255 error:
1256 isl_val_free(v);
1257 return FN(MULTI(BASE),free)(multi);
1260 /* Divide the elements of "multi" by "v" and return the result.
1262 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_val)(
1263 __isl_take MULTI(BASE) *multi, __isl_take isl_val *v)
1265 int i;
1267 if (!multi || !v)
1268 goto error;
1270 if (isl_val_is_one(v)) {
1271 isl_val_free(v);
1272 return multi;
1275 if (!isl_val_is_rat(v))
1276 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1277 "expecting rational factor", goto error);
1278 if (isl_val_is_zero(v))
1279 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1280 "cannot scale down by zero", goto error);
1282 multi = FN(MULTI(BASE),cow)(multi);
1283 if (!multi)
1284 return NULL;
1286 for (i = 0; i < multi->n; ++i) {
1287 multi->p[i] = FN(EL,scale_down_val)(multi->p[i],
1288 isl_val_copy(v));
1289 if (!multi->p[i])
1290 goto error;
1293 isl_val_free(v);
1294 return multi;
1295 error:
1296 isl_val_free(v);
1297 return FN(MULTI(BASE),free)(multi);
1300 /* Multiply the elements of "multi" by the corresponding element of "mv"
1301 * and return the result.
1303 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_multi_val)(
1304 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1306 int i;
1308 if (!multi || !mv)
1309 goto error;
1311 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1312 mv->space, isl_dim_set))
1313 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1314 "spaces don't match", goto error);
1316 multi = FN(MULTI(BASE),cow)(multi);
1317 if (!multi)
1318 return NULL;
1320 for (i = 0; i < multi->n; ++i) {
1321 isl_val *v;
1323 v = isl_multi_val_get_val(mv, i);
1324 multi->p[i] = FN(EL,scale_val)(multi->p[i], v);
1325 if (!multi->p[i])
1326 goto error;
1329 isl_multi_val_free(mv);
1330 return multi;
1331 error:
1332 isl_multi_val_free(mv);
1333 return FN(MULTI(BASE),free)(multi);
1336 /* Divide the elements of "multi" by the corresponding element of "mv"
1337 * and return the result.
1339 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_multi_val)(
1340 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1342 int i;
1344 if (!multi || !mv)
1345 goto error;
1347 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1348 mv->space, isl_dim_set))
1349 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1350 "spaces don't match", goto error);
1352 multi = FN(MULTI(BASE),cow)(multi);
1353 if (!multi)
1354 return NULL;
1356 for (i = 0; i < multi->n; ++i) {
1357 isl_val *v;
1359 v = isl_multi_val_get_val(mv, i);
1360 multi->p[i] = FN(EL,scale_down_val)(multi->p[i], v);
1361 if (!multi->p[i])
1362 goto error;
1365 isl_multi_val_free(mv);
1366 return multi;
1367 error:
1368 isl_multi_val_free(mv);
1369 return FN(MULTI(BASE),free)(multi);
1372 #ifndef NO_MOVE_DIMS
1373 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1374 * to dimensions of "dst_type" at "dst_pos".
1376 * We only support moving input dimensions to parameters and vice versa.
1378 __isl_give MULTI(BASE) *FN(MULTI(BASE),move_dims)(__isl_take MULTI(BASE) *multi,
1379 enum isl_dim_type dst_type, unsigned dst_pos,
1380 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1382 int i;
1384 if (!multi)
1385 return NULL;
1387 if (n == 0 &&
1388 !isl_space_is_named_or_nested(multi->space, src_type) &&
1389 !isl_space_is_named_or_nested(multi->space, dst_type))
1390 return multi;
1392 if (dst_type == isl_dim_out || src_type == isl_dim_out)
1393 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1394 "cannot move output/set dimension",
1395 return FN(MULTI(BASE),free)(multi));
1396 if (dst_type == isl_dim_div || src_type == isl_dim_div)
1397 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1398 "cannot move divs",
1399 return FN(MULTI(BASE),free)(multi));
1400 if (src_pos + n > isl_space_dim(multi->space, src_type))
1401 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1402 "range out of bounds",
1403 return FN(MULTI(BASE),free)(multi));
1404 if (dst_type == src_type)
1405 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_unsupported,
1406 "moving dims within the same type not supported",
1407 return FN(MULTI(BASE),free)(multi));
1409 multi = FN(MULTI(BASE),cow)(multi);
1410 if (!multi)
1411 return NULL;
1413 multi->space = isl_space_move_dims(multi->space, dst_type, dst_pos,
1414 src_type, src_pos, n);
1415 if (!multi->space)
1416 return FN(MULTI(BASE),free)(multi);
1418 for (i = 0; i < multi->n; ++i) {
1419 multi->p[i] = FN(EL,move_dims)(multi->p[i], dst_type, dst_pos,
1420 src_type, src_pos, n);
1421 if (!multi->p[i])
1422 return FN(MULTI(BASE),free)(multi);
1425 return multi;
1427 #endif
1429 /* Convert a multiple expression defined over a parameter domain
1430 * into one that is defined over a zero-dimensional set.
1432 __isl_give MULTI(BASE) *FN(MULTI(BASE),from_range)(
1433 __isl_take MULTI(BASE) *multi)
1435 isl_space *space;
1437 if (!multi)
1438 return NULL;
1439 if (!isl_space_is_set(multi->space))
1440 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1441 "not living in a set space",
1442 return FN(MULTI(BASE),free)(multi));
1444 space = FN(MULTI(BASE),get_space)(multi);
1445 space = isl_space_from_range(space);
1446 multi = FN(MULTI(BASE),reset_space)(multi, space);
1448 return multi;
1451 /* Are "multi1" and "multi2" obviously equal?
1453 int FN(MULTI(BASE),plain_is_equal)(__isl_keep MULTI(BASE) *multi1,
1454 __isl_keep MULTI(BASE) *multi2)
1456 int i;
1457 int equal;
1459 if (!multi1 || !multi2)
1460 return -1;
1461 if (multi1->n != multi2->n)
1462 return 0;
1463 equal = isl_space_is_equal(multi1->space, multi2->space);
1464 if (equal < 0 || !equal)
1465 return equal;
1467 for (i = 0; i < multi1->n; ++i) {
1468 equal = FN(EL,plain_is_equal)(multi1->p[i], multi2->p[i]);
1469 if (equal < 0 || !equal)
1470 return equal;
1473 return 1;
1476 #ifndef NO_DOMAIN
1477 /* Return the shared domain of the elements of "multi".
1479 __isl_give isl_set *FN(MULTI(BASE),domain)(__isl_take MULTI(BASE) *multi)
1481 int i;
1482 isl_set *dom;
1484 if (!multi)
1485 return NULL;
1487 dom = isl_set_universe(FN(MULTI(BASE),get_domain_space)(multi));
1488 for (i = 0; i < multi->n; ++i) {
1489 isl_set *dom_i;
1491 dom_i = FN(EL,domain)(FN(FN(MULTI(BASE),get),BASE)(multi, i));
1492 dom = isl_set_intersect(dom, dom_i);
1495 FN(MULTI(BASE),free)(multi);
1496 return dom;
1498 #endif