isl_ast_graft_list_merge: plug memory leak on error paths
[isl.git] / isl_multi_templ.c
blobd584f4569a8ff5974bc2d3d457f95f42acfa0e3b
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),free)(multi));
707 multi->space = isl_space_drop_dims(multi->space, type, first, n);
708 if (!multi->space)
709 return FN(MULTI(BASE),free)(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),free)(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 /* Given a function [B -> C], extract the function C.
874 __isl_give MULTI(BASE) *FN(MULTI(BASE),factor_range)(
875 __isl_take MULTI(BASE) *multi)
877 isl_space *space;
878 int total, keep;
880 if (!multi)
881 return NULL;
882 if (!isl_space_is_wrapping(multi->space))
883 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
884 "not a product", return FN(MULTI(BASE),free)(multi));
886 space = FN(MULTI(BASE),get_space)(multi);
887 total = isl_space_dim(space, isl_dim_out);
888 space = isl_space_factor_range(space);
889 keep = isl_space_dim(space, isl_dim_out);
890 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_out, 0, total - keep);
891 multi = FN(MULTI(BASE),reset_space)(multi, space);
893 return multi;
896 #ifndef NO_PRODUCT
897 /* Given two MULTI(BASE)s A -> B and C -> D,
898 * construct a MULTI(BASE) [A -> C] -> [B -> D].
900 * The parameters are assumed to have been aligned.
902 __isl_give MULTI(BASE) *FN(MULTI(BASE),product_aligned)(
903 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
905 int i;
906 EL *el;
907 isl_space *space;
908 MULTI(BASE) *res;
909 int in1, in2, out1, out2;
911 in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
912 in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
913 out1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
914 out2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
915 space = isl_space_product(FN(MULTI(BASE),get_space)(multi1),
916 FN(MULTI(BASE),get_space)(multi2));
917 res = FN(MULTI(BASE),alloc)(isl_space_copy(space));
918 space = isl_space_domain(space);
920 for (i = 0; i < out1; ++i) {
921 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
922 el = FN(EL,insert_dims)(el, isl_dim_in, in1, in2);
923 el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
924 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
927 for (i = 0; i < out2; ++i) {
928 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
929 el = FN(EL,insert_dims)(el, isl_dim_in, 0, in1);
930 el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
931 res = FN(FN(MULTI(BASE),set),BASE)(res, out1 + i, el);
934 isl_space_free(space);
935 FN(MULTI(BASE),free)(multi1);
936 FN(MULTI(BASE),free)(multi2);
937 return res;
940 /* Given two MULTI(BASE)s A -> B and C -> D,
941 * construct a MULTI(BASE) [A -> C] -> [B -> D].
943 __isl_give MULTI(BASE) *FN(MULTI(BASE),product)(
944 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
946 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
947 &FN(MULTI(BASE),product_aligned));
949 #endif
951 __isl_give MULTI(BASE) *FN(MULTI(BASE),flatten_range)(
952 __isl_take MULTI(BASE) *multi)
954 if (!multi)
955 return NULL;
957 if (!multi->space->nested[1])
958 return multi;
960 multi = FN(MULTI(BASE),cow)(multi);
961 if (!multi)
962 return NULL;
964 multi->space = isl_space_flatten_range(multi->space);
965 if (!multi->space)
966 return FN(MULTI(BASE),free)(multi);
968 return multi;
971 /* Given two MULTI(BASE)s A -> B and C -> D,
972 * construct a MULTI(BASE) (A * C) -> (B, D).
974 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
975 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
977 MULTI(BASE) *multi;
979 multi = FN(MULTI(BASE),range_product)(multi1, multi2);
980 multi = FN(MULTI(BASE),flatten_range)(multi);
981 return multi;
984 /* Given two multi expressions, "multi1"
986 * [A] -> [B1 B2]
988 * where B2 starts at position "pos", and "multi2"
990 * [A] -> [D]
992 * return the multi expression
994 * [A] -> [B1 D B2]
996 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_splice)(
997 __isl_take MULTI(BASE) *multi1, unsigned pos,
998 __isl_take MULTI(BASE) *multi2)
1000 MULTI(BASE) *res;
1001 unsigned dim;
1003 if (!multi1 || !multi2)
1004 goto error;
1006 dim = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
1007 if (pos > dim)
1008 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
1009 "index out of bounds", goto error);
1011 res = FN(MULTI(BASE),copy)(multi1);
1012 res = FN(MULTI(BASE),drop_dims)(res, isl_dim_out, pos, dim - pos);
1013 multi1 = FN(MULTI(BASE),drop_dims)(multi1, isl_dim_out, 0, pos);
1015 res = FN(MULTI(BASE),flat_range_product)(res, multi2);
1016 res = FN(MULTI(BASE),flat_range_product)(res, multi1);
1018 return res;
1019 error:
1020 FN(MULTI(BASE),free)(multi1);
1021 FN(MULTI(BASE),free)(multi2);
1022 return NULL;
1025 #ifndef NO_SPLICE
1026 /* Given two multi expressions, "multi1"
1028 * [A1 A2] -> [B1 B2]
1030 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
1031 * and "multi2"
1033 * [C] -> [D]
1035 * return the multi expression
1037 * [A1 C A2] -> [B1 D B2]
1039 * We first insert input dimensions to obtain
1041 * [A1 C A2] -> [B1 B2]
1043 * and
1045 * [A1 C A2] -> [D]
1047 * and then apply range_splice.
1049 __isl_give MULTI(BASE) *FN(MULTI(BASE),splice)(
1050 __isl_take MULTI(BASE) *multi1, unsigned in_pos, unsigned out_pos,
1051 __isl_take MULTI(BASE) *multi2)
1053 unsigned n_in1;
1054 unsigned n_in2;
1056 if (!multi1 || !multi2)
1057 goto error;
1059 n_in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
1060 if (in_pos > n_in1)
1061 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
1062 "index out of bounds", goto error);
1064 n_in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
1066 multi1 = FN(MULTI(BASE),insert_dims)(multi1, isl_dim_in, in_pos, n_in2);
1067 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, n_in2,
1068 n_in1 - in_pos);
1069 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, 0, in_pos);
1071 return FN(MULTI(BASE),range_splice)(multi1, out_pos, multi2);
1072 error:
1073 FN(MULTI(BASE),free)(multi1);
1074 FN(MULTI(BASE),free)(multi2);
1075 return NULL;
1077 #endif
1079 /* This function is currently only used from isl_aff.c
1081 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
1082 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
1083 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
1084 __attribute__ ((unused));
1086 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
1087 * return the result.
1089 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
1090 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
1091 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
1093 int i;
1094 isl_ctx *ctx;
1096 multi1 = FN(MULTI(BASE),cow)(multi1);
1097 if (!multi1 || !multi2)
1098 goto error;
1100 ctx = FN(MULTI(BASE),get_ctx)(multi1);
1101 if (!isl_space_is_equal(multi1->space, multi2->space))
1102 isl_die(ctx, isl_error_invalid,
1103 "spaces don't match", goto error);
1105 for (i = 0; i < multi1->n; ++i) {
1106 multi1->p[i] = fn(multi1->p[i], FN(EL,copy)(multi2->p[i]));
1107 if (!multi1->p[i])
1108 goto error;
1111 FN(MULTI(BASE),free)(multi2);
1112 return multi1;
1113 error:
1114 FN(MULTI(BASE),free)(multi1);
1115 FN(MULTI(BASE),free)(multi2);
1116 return NULL;
1119 /* Add "multi2" from "multi1" and return the result.
1121 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
1123 static __isl_give MULTI(BASE) *FN(MULTI(BASE),add_aligned)(
1124 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
1126 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,add));
1129 /* Add "multi2" from "multi1" and return the result.
1131 __isl_give MULTI(BASE) *FN(MULTI(BASE),add)(__isl_take MULTI(BASE) *multi1,
1132 __isl_take MULTI(BASE) *multi2)
1134 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
1135 &FN(MULTI(BASE),add_aligned));
1138 /* Subtract "multi2" from "multi1" and return the result.
1140 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
1142 static __isl_give MULTI(BASE) *FN(MULTI(BASE),sub_aligned)(
1143 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
1145 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,sub));
1148 /* Subtract "multi2" from "multi1" and return the result.
1150 __isl_give MULTI(BASE) *FN(MULTI(BASE),sub)(__isl_take MULTI(BASE) *multi1,
1151 __isl_take MULTI(BASE) *multi2)
1153 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
1154 &FN(MULTI(BASE),sub_aligned));
1157 /* Multiply the elements of "multi" by "v" and return the result.
1159 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_val)(__isl_take MULTI(BASE) *multi,
1160 __isl_take isl_val *v)
1162 int i;
1164 if (!multi || !v)
1165 goto error;
1167 if (isl_val_is_one(v)) {
1168 isl_val_free(v);
1169 return multi;
1172 if (!isl_val_is_rat(v))
1173 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1174 "expecting rational factor", goto error);
1176 multi = FN(MULTI(BASE),cow)(multi);
1177 if (!multi)
1178 return NULL;
1180 for (i = 0; i < multi->n; ++i) {
1181 multi->p[i] = FN(EL,scale_val)(multi->p[i], isl_val_copy(v));
1182 if (!multi->p[i])
1183 goto error;
1186 isl_val_free(v);
1187 return multi;
1188 error:
1189 isl_val_free(v);
1190 return FN(MULTI(BASE),free)(multi);
1193 /* Divide the elements of "multi" by "v" and return the result.
1195 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_val)(
1196 __isl_take MULTI(BASE) *multi, __isl_take isl_val *v)
1198 int i;
1200 if (!multi || !v)
1201 goto error;
1203 if (isl_val_is_one(v)) {
1204 isl_val_free(v);
1205 return multi;
1208 if (!isl_val_is_rat(v))
1209 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1210 "expecting rational factor", goto error);
1211 if (isl_val_is_zero(v))
1212 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1213 "cannot scale down by zero", goto error);
1215 multi = FN(MULTI(BASE),cow)(multi);
1216 if (!multi)
1217 return NULL;
1219 for (i = 0; i < multi->n; ++i) {
1220 multi->p[i] = FN(EL,scale_down_val)(multi->p[i],
1221 isl_val_copy(v));
1222 if (!multi->p[i])
1223 goto error;
1226 isl_val_free(v);
1227 return multi;
1228 error:
1229 isl_val_free(v);
1230 return FN(MULTI(BASE),free)(multi);
1233 /* Multiply the elements of "multi" by the corresponding element of "mv"
1234 * and return the result.
1236 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_multi_val)(
1237 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1239 int i;
1241 if (!multi || !mv)
1242 goto error;
1244 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1245 mv->space, isl_dim_set))
1246 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1247 "spaces don't match", goto error);
1249 multi = FN(MULTI(BASE),cow)(multi);
1250 if (!multi)
1251 goto error;
1253 for (i = 0; i < multi->n; ++i) {
1254 isl_val *v;
1256 v = isl_multi_val_get_val(mv, i);
1257 multi->p[i] = FN(EL,scale_val)(multi->p[i], v);
1258 if (!multi->p[i])
1259 goto error;
1262 isl_multi_val_free(mv);
1263 return multi;
1264 error:
1265 isl_multi_val_free(mv);
1266 return FN(MULTI(BASE),free)(multi);
1269 /* Divide the elements of "multi" by the corresponding element of "mv"
1270 * and return the result.
1272 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_multi_val)(
1273 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1275 int i;
1277 if (!multi || !mv)
1278 goto error;
1280 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1281 mv->space, isl_dim_set))
1282 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1283 "spaces don't match", goto error);
1285 multi = FN(MULTI(BASE),cow)(multi);
1286 if (!multi)
1287 return NULL;
1289 for (i = 0; i < multi->n; ++i) {
1290 isl_val *v;
1292 v = isl_multi_val_get_val(mv, i);
1293 multi->p[i] = FN(EL,scale_down_val)(multi->p[i], v);
1294 if (!multi->p[i])
1295 goto error;
1298 isl_multi_val_free(mv);
1299 return multi;
1300 error:
1301 isl_multi_val_free(mv);
1302 return FN(MULTI(BASE),free)(multi);
1305 /* Compute the residues of the elements of "multi" modulo
1306 * the corresponding element of "mv" and return the result.
1308 __isl_give MULTI(BASE) *FN(MULTI(BASE),mod_multi_val)(
1309 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1311 int i;
1313 if (!multi || !mv)
1314 goto error;
1316 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1317 mv->space, isl_dim_set))
1318 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1319 "spaces don't match", goto error);
1321 multi = FN(MULTI(BASE),cow)(multi);
1322 if (!multi)
1323 goto error;
1325 for (i = 0; i < multi->n; ++i) {
1326 isl_val *v;
1328 v = isl_multi_val_get_val(mv, i);
1329 multi->p[i] = FN(EL,mod_val)(multi->p[i], v);
1330 if (!multi->p[i])
1331 goto error;
1334 isl_multi_val_free(mv);
1335 return multi;
1336 error:
1337 isl_multi_val_free(mv);
1338 return FN(MULTI(BASE),free)(multi);
1341 #ifndef NO_MOVE_DIMS
1342 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1343 * to dimensions of "dst_type" at "dst_pos".
1345 * We only support moving input dimensions to parameters and vice versa.
1347 __isl_give MULTI(BASE) *FN(MULTI(BASE),move_dims)(__isl_take MULTI(BASE) *multi,
1348 enum isl_dim_type dst_type, unsigned dst_pos,
1349 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1351 int i;
1353 if (!multi)
1354 return NULL;
1356 if (n == 0 &&
1357 !isl_space_is_named_or_nested(multi->space, src_type) &&
1358 !isl_space_is_named_or_nested(multi->space, dst_type))
1359 return multi;
1361 if (dst_type == isl_dim_out || src_type == isl_dim_out)
1362 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1363 "cannot move output/set dimension",
1364 return FN(MULTI(BASE),free)(multi));
1365 if (dst_type == isl_dim_div || src_type == isl_dim_div)
1366 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1367 "cannot move divs",
1368 return FN(MULTI(BASE),free)(multi));
1369 if (src_pos + n > isl_space_dim(multi->space, src_type))
1370 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1371 "range out of bounds",
1372 return FN(MULTI(BASE),free)(multi));
1373 if (dst_type == src_type)
1374 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_unsupported,
1375 "moving dims within the same type not supported",
1376 return FN(MULTI(BASE),free)(multi));
1378 multi = FN(MULTI(BASE),cow)(multi);
1379 if (!multi)
1380 return NULL;
1382 multi->space = isl_space_move_dims(multi->space, dst_type, dst_pos,
1383 src_type, src_pos, n);
1384 if (!multi->space)
1385 return FN(MULTI(BASE),free)(multi);
1387 for (i = 0; i < multi->n; ++i) {
1388 multi->p[i] = FN(EL,move_dims)(multi->p[i], dst_type, dst_pos,
1389 src_type, src_pos, n);
1390 if (!multi->p[i])
1391 return FN(MULTI(BASE),free)(multi);
1394 return multi;
1396 #endif
1398 /* Convert a multiple expression defined over a parameter domain
1399 * into one that is defined over a zero-dimensional set.
1401 __isl_give MULTI(BASE) *FN(MULTI(BASE),from_range)(
1402 __isl_take MULTI(BASE) *multi)
1404 isl_space *space;
1406 if (!multi)
1407 return NULL;
1408 if (!isl_space_is_set(multi->space))
1409 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1410 "not living in a set space",
1411 return FN(MULTI(BASE),free)(multi));
1413 space = FN(MULTI(BASE),get_space)(multi);
1414 space = isl_space_from_range(space);
1415 multi = FN(MULTI(BASE),reset_space)(multi, space);
1417 return multi;
1420 /* Are "multi1" and "multi2" obviously equal?
1422 isl_bool FN(MULTI(BASE),plain_is_equal)(__isl_keep MULTI(BASE) *multi1,
1423 __isl_keep MULTI(BASE) *multi2)
1425 int i;
1426 isl_bool equal;
1428 if (!multi1 || !multi2)
1429 return isl_bool_error;
1430 if (multi1->n != multi2->n)
1431 return isl_bool_false;
1432 equal = isl_space_is_equal(multi1->space, multi2->space);
1433 if (equal < 0 || !equal)
1434 return equal;
1436 for (i = 0; i < multi1->n; ++i) {
1437 equal = FN(EL,plain_is_equal)(multi1->p[i], multi2->p[i]);
1438 if (equal < 0 || !equal)
1439 return equal;
1442 return isl_bool_true;
1445 #ifndef NO_DOMAIN
1446 /* Return the shared domain of the elements of "multi".
1448 __isl_give isl_set *FN(MULTI(BASE),domain)(__isl_take MULTI(BASE) *multi)
1450 int i;
1451 isl_set *dom;
1453 if (!multi)
1454 return NULL;
1456 dom = isl_set_universe(FN(MULTI(BASE),get_domain_space)(multi));
1457 for (i = 0; i < multi->n; ++i) {
1458 isl_set *dom_i;
1460 dom_i = FN(EL,domain)(FN(FN(MULTI(BASE),get),BASE)(multi, i));
1461 dom = isl_set_intersect(dom, dom_i);
1464 FN(MULTI(BASE),free)(multi);
1465 return dom;
1467 #endif
1469 #ifndef NO_NEG
1470 /* Return the opposite of "multi".
1472 __isl_give MULTI(BASE) *FN(MULTI(BASE),neg)(__isl_take MULTI(BASE) *multi)
1474 int i;
1476 multi = FN(MULTI(BASE),cow)(multi);
1477 if (!multi)
1478 return NULL;
1480 for (i = 0; i < multi->n; ++i) {
1481 multi->p[i] = FN(EL,neg)(multi->p[i]);
1482 if (!multi->p[i])
1483 return FN(MULTI(BASE),free)(multi);
1486 return multi;
1488 #endif