Add test coverage for isl_[union_]set_get_basic_set_list
[isl.git] / isl_multi_templ.c
blobfcd43d7e8a9b54eeae511afa6db82b42fac579c2
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/id.h>
12 #include <isl_space_private.h>
13 #include <isl_val_private.h>
14 #include <isl/set.h>
15 #include <isl_reordering.h>
17 #include <isl_multi_macro.h>
19 #define MULTI_NAME(BASE) "isl_multi_" #BASE
20 #define xLIST(EL) EL ## _list
21 #define LIST(EL) xLIST(EL)
23 isl_ctx *FN(MULTI(BASE),get_ctx)(__isl_keep MULTI(BASE) *multi)
25 return multi ? isl_space_get_ctx(multi->space) : NULL;
28 __isl_give isl_space *FN(MULTI(BASE),get_space)(__isl_keep MULTI(BASE) *multi)
30 return multi ? isl_space_copy(multi->space) : NULL;
33 /* Return the position of the dimension of the given type and name
34 * in "multi".
35 * Return -1 if no such dimension can be found.
37 int FN(MULTI(BASE),find_dim_by_name)(__isl_keep MULTI(BASE) *multi,
38 enum isl_dim_type type, const char *name)
40 if (!multi)
41 return -1;
42 return isl_space_find_dim_by_name(multi->space, type, name);
45 __isl_give isl_space *FN(MULTI(BASE),get_domain_space)(
46 __isl_keep MULTI(BASE) *multi)
48 return multi ? isl_space_domain(isl_space_copy(multi->space)) : NULL;
51 /* Allocate a multi expression living in "space".
53 * If the number of base expressions is zero, then make sure
54 * there is enough room in the structure for the explicit domain,
55 * in case the type supports such an explicit domain.
57 __isl_give MULTI(BASE) *FN(MULTI(BASE),alloc)(__isl_take isl_space *space)
59 isl_ctx *ctx;
60 int n;
61 MULTI(BASE) *multi;
63 if (!space)
64 return NULL;
66 ctx = isl_space_get_ctx(space);
67 n = isl_space_dim(space, isl_dim_out);
68 if (n > 0)
69 multi = isl_calloc(ctx, MULTI(BASE),
70 sizeof(MULTI(BASE)) + (n - 1) * sizeof(struct EL *));
71 else
72 multi = isl_calloc(ctx, MULTI(BASE), sizeof(MULTI(BASE)));
73 if (!multi)
74 goto error;
76 multi->space = space;
77 multi->n = n;
78 multi->ref = 1;
79 if (FN(MULTI(BASE),has_explicit_domain)(multi))
80 multi = FN(MULTI(BASE),init_explicit_domain)(multi);
81 return multi;
82 error:
83 isl_space_free(space);
84 return NULL;
87 __isl_give MULTI(BASE) *FN(MULTI(BASE),dup)(__isl_keep MULTI(BASE) *multi)
89 int i;
90 MULTI(BASE) *dup;
92 if (!multi)
93 return NULL;
95 dup = FN(MULTI(BASE),alloc)(isl_space_copy(multi->space));
96 if (!dup)
97 return NULL;
99 for (i = 0; i < multi->n; ++i)
100 dup = FN(FN(MULTI(BASE),set),BASE)(dup, i,
101 FN(EL,copy)(multi->u.p[i]));
102 if (FN(MULTI(BASE),has_explicit_domain)(multi))
103 dup = FN(MULTI(BASE),copy_explicit_domain)(dup, multi);
105 return dup;
108 __isl_give MULTI(BASE) *FN(MULTI(BASE),cow)(__isl_take MULTI(BASE) *multi)
110 if (!multi)
111 return NULL;
113 if (multi->ref == 1)
114 return multi;
116 multi->ref--;
117 return FN(MULTI(BASE),dup)(multi);
120 __isl_give MULTI(BASE) *FN(MULTI(BASE),copy)(__isl_keep MULTI(BASE) *multi)
122 if (!multi)
123 return NULL;
125 multi->ref++;
126 return multi;
129 __isl_null MULTI(BASE) *FN(MULTI(BASE),free)(__isl_take MULTI(BASE) *multi)
131 int i;
133 if (!multi)
134 return NULL;
136 if (--multi->ref > 0)
137 return NULL;
139 isl_space_free(multi->space);
140 for (i = 0; i < multi->n; ++i)
141 FN(EL,free)(multi->u.p[i]);
142 if (FN(MULTI(BASE),has_explicit_domain)(multi))
143 FN(MULTI(BASE),free_explicit_domain)(multi);
144 free(multi);
146 return NULL;
149 unsigned FN(MULTI(BASE),dim)(__isl_keep MULTI(BASE) *multi,
150 enum isl_dim_type type)
152 return multi ? isl_space_dim(multi->space, type) : 0;
155 /* Return the position of the first dimension of "type" with id "id".
156 * Return -1 if there is no such dimension.
158 int FN(MULTI(BASE),find_dim_by_id)(__isl_keep MULTI(BASE) *multi,
159 enum isl_dim_type type, __isl_keep isl_id *id)
161 if (!multi)
162 return -1;
163 return isl_space_find_dim_by_id(multi->space, type, id);
166 /* Return the id of the given dimension.
168 __isl_give isl_id *FN(MULTI(BASE),get_dim_id)(__isl_keep MULTI(BASE) *multi,
169 enum isl_dim_type type, unsigned pos)
171 return multi ? isl_space_get_dim_id(multi->space, type, pos) : NULL;
174 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_name)(
175 __isl_take MULTI(BASE) *multi,
176 enum isl_dim_type type, unsigned pos, const char *s)
178 int i;
180 multi = FN(MULTI(BASE),cow)(multi);
181 if (!multi)
182 return NULL;
184 multi->space = isl_space_set_dim_name(multi->space, type, pos, s);
185 if (!multi->space)
186 return FN(MULTI(BASE),free)(multi);
188 if (type == isl_dim_out)
189 return multi;
190 for (i = 0; i < multi->n; ++i) {
191 multi->u.p[i] = FN(EL,set_dim_name)(multi->u.p[i],
192 type, pos, s);
193 if (!multi->u.p[i])
194 return FN(MULTI(BASE),free)(multi);
197 return multi;
200 const char *FN(MULTI(BASE),get_tuple_name)(__isl_keep MULTI(BASE) *multi,
201 enum isl_dim_type type)
203 return multi ? isl_space_get_tuple_name(multi->space, type) : NULL;
206 /* Does the specified tuple have an id?
208 isl_bool FN(MULTI(BASE),has_tuple_id)(__isl_keep MULTI(BASE) *multi,
209 enum isl_dim_type type)
211 if (!multi)
212 return isl_bool_error;
213 return isl_space_has_tuple_id(multi->space, type);
216 /* Return the id of the specified tuple.
218 __isl_give isl_id *FN(MULTI(BASE),get_tuple_id)(__isl_keep MULTI(BASE) *multi,
219 enum isl_dim_type type)
221 return multi ? isl_space_get_tuple_id(multi->space, type) : NULL;
224 __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
225 int pos)
227 isl_ctx *ctx;
229 if (!multi)
230 return NULL;
231 ctx = FN(MULTI(BASE),get_ctx)(multi);
232 if (pos < 0 || pos >= multi->n)
233 isl_die(ctx, isl_error_invalid,
234 "index out of bounds", return NULL);
235 return FN(EL,copy)(multi->u.p[pos]);
238 /* Set the element at position "pos" of "multi" to "el",
239 * where the position may be empty if "multi" has only a single reference.
241 static __isl_give MULTI(BASE) *FN(MULTI(BASE),restore)(
242 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
244 multi = FN(MULTI(BASE),cow)(multi);
245 if (!multi || !el)
246 goto error;
248 if (pos < 0 || pos >= multi->n)
249 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
250 "index out of bounds", goto error);
252 FN(EL,free)(multi->u.p[pos]);
253 multi->u.p[pos] = el;
255 return multi;
256 error:
257 FN(MULTI(BASE),free)(multi);
258 FN(EL,free)(el);
259 return NULL;
262 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
263 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
265 isl_space *multi_space = NULL;
266 isl_space *el_space = NULL;
267 isl_bool match;
269 multi_space = FN(MULTI(BASE),get_space)(multi);
270 match = FN(EL,matching_params)(el, multi_space);
271 if (match < 0)
272 goto error;
273 if (!match) {
274 multi = FN(MULTI(BASE),align_params)(multi,
275 FN(EL,get_space)(el));
276 isl_space_free(multi_space);
277 multi_space = FN(MULTI(BASE),get_space)(multi);
278 el = FN(EL,align_params)(el, isl_space_copy(multi_space));
280 if (FN(EL,check_match_domain_space)(el, multi_space) < 0)
281 goto error;
283 multi = FN(MULTI(BASE),restore)(multi, pos, el);
285 isl_space_free(multi_space);
286 isl_space_free(el_space);
288 return multi;
289 error:
290 FN(MULTI(BASE),free)(multi);
291 FN(EL,free)(el);
292 isl_space_free(multi_space);
293 isl_space_free(el_space);
294 return NULL;
297 /* Reset the space of "multi". This function is called from isl_pw_templ.c
298 * and doesn't know if the space of an element object is represented
299 * directly or through its domain. It therefore passes along both,
300 * which we pass along to the element function since we don't know how
301 * that is represented either.
303 * If "multi" has an explicit domain, then the caller is expected
304 * to make sure that any modification that would change the dimensions
305 * of the explicit domain has bee applied before this function is called.
307 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
308 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
309 __isl_take isl_space *domain)
311 int i;
313 multi = FN(MULTI(BASE),cow)(multi);
314 if (!multi || !space || !domain)
315 goto error;
317 for (i = 0; i < multi->n; ++i) {
318 multi->u.p[i] = FN(EL,reset_domain_space)(multi->u.p[i],
319 isl_space_copy(domain));
320 if (!multi->u.p[i])
321 goto error;
323 if (FN(MULTI(BASE),has_explicit_domain)(multi)) {
324 multi = FN(MULTI(BASE),reset_explicit_domain_space)(multi,
325 isl_space_copy(domain));
326 if (!multi)
327 goto error;
329 isl_space_free(domain);
330 isl_space_free(multi->space);
331 multi->space = space;
333 return multi;
334 error:
335 isl_space_free(domain);
336 isl_space_free(space);
337 FN(MULTI(BASE),free)(multi);
338 return NULL;
341 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
342 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
344 isl_space *space;
346 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
347 isl_space_copy(multi->space));
348 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
351 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
352 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
354 isl_space *domain;
356 domain = isl_space_domain(isl_space_copy(space));
357 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
360 /* Set the id of the given dimension of "multi" to "id".
362 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_id)(
363 __isl_take MULTI(BASE) *multi,
364 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
366 isl_space *space;
368 multi = FN(MULTI(BASE),cow)(multi);
369 if (!multi || !id)
370 goto error;
372 space = FN(MULTI(BASE),get_space)(multi);
373 space = isl_space_set_dim_id(space, type, pos, id);
375 return FN(MULTI(BASE),reset_space)(multi, space);
376 error:
377 isl_id_free(id);
378 FN(MULTI(BASE),free)(multi);
379 return NULL;
382 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_name)(
383 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
384 const char *s)
386 isl_space *space;
388 multi = FN(MULTI(BASE),cow)(multi);
389 if (!multi)
390 return NULL;
392 space = FN(MULTI(BASE),get_space)(multi);
393 space = isl_space_set_tuple_name(space, type, s);
395 return FN(MULTI(BASE),reset_space)(multi, space);
398 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_id)(
399 __isl_take MULTI(BASE) *multi, enum isl_dim_type type,
400 __isl_take isl_id *id)
402 isl_space *space;
404 multi = FN(MULTI(BASE),cow)(multi);
405 if (!multi)
406 goto error;
408 space = FN(MULTI(BASE),get_space)(multi);
409 space = isl_space_set_tuple_id(space, type, id);
411 return FN(MULTI(BASE),reset_space)(multi, space);
412 error:
413 isl_id_free(id);
414 return NULL;
417 /* Drop the id on the specified tuple.
419 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_tuple_id)(
420 __isl_take MULTI(BASE) *multi, enum isl_dim_type type)
422 isl_space *space;
424 if (!multi)
425 return NULL;
426 if (!FN(MULTI(BASE),has_tuple_id)(multi, type))
427 return multi;
429 multi = FN(MULTI(BASE),cow)(multi);
430 if (!multi)
431 return NULL;
433 space = FN(MULTI(BASE),get_space)(multi);
434 space = isl_space_reset_tuple_id(space, type);
436 return FN(MULTI(BASE),reset_space)(multi, space);
439 /* Reset the user pointer on all identifiers of parameters and tuples
440 * of the space of "multi".
442 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_user)(
443 __isl_take MULTI(BASE) *multi)
445 isl_space *space;
447 space = FN(MULTI(BASE),get_space)(multi);
448 space = isl_space_reset_user(space);
450 return FN(MULTI(BASE),reset_space)(multi, space);
453 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
454 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
456 int i;
457 isl_space *space;
459 multi = FN(MULTI(BASE),cow)(multi);
460 if (!multi || !exp)
461 goto error;
463 for (i = 0; i < multi->n; ++i) {
464 multi->u.p[i] = FN(EL,realign_domain)(multi->u.p[i],
465 isl_reordering_copy(exp));
466 if (!multi->u.p[i])
467 goto error;
470 space = isl_reordering_get_space(exp);
471 multi = FN(MULTI(BASE),reset_domain_space)(multi, space);
473 isl_reordering_free(exp);
474 return multi;
475 error:
476 isl_reordering_free(exp);
477 FN(MULTI(BASE),free)(multi);
478 return NULL;
481 /* Align the parameters of "multi" to those of "model".
483 * If "multi" has an explicit domain, then align the parameters
484 * of the domain first.
486 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
487 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
489 isl_ctx *ctx;
490 isl_bool equal_params;
491 isl_reordering *exp;
493 if (!multi || !model)
494 goto error;
496 equal_params = isl_space_has_equal_params(multi->space, model);
497 if (equal_params < 0)
498 goto error;
499 if (equal_params) {
500 isl_space_free(model);
501 return multi;
504 ctx = isl_space_get_ctx(model);
505 if (!isl_space_has_named_params(model))
506 isl_die(ctx, isl_error_invalid,
507 "model has unnamed parameters", goto error);
508 if (!isl_space_has_named_params(multi->space))
509 isl_die(ctx, isl_error_invalid,
510 "input has unnamed parameters", goto error);
512 if (FN(MULTI(BASE),has_explicit_domain)(multi)) {
513 multi = FN(MULTI(BASE),align_explicit_domain_params)(multi,
514 isl_space_copy(model));
515 if (!multi)
516 goto error;
518 exp = isl_parameter_alignment_reordering(multi->space, model);
519 exp = isl_reordering_extend_space(exp,
520 FN(MULTI(BASE),get_domain_space)(multi));
521 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
523 isl_space_free(model);
524 return multi;
525 error:
526 isl_space_free(model);
527 FN(MULTI(BASE),free)(multi);
528 return NULL;
531 /* Create a multi expression in the given space with the elements of "list"
532 * as base expressions.
534 * Since isl_multi_*_restore_* assumes that the element and
535 * the multi expression have matching spaces, the alignment
536 * (if any) needs to be performed beforehand.
538 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
539 __isl_take isl_space *space, __isl_take LIST(EL) *list)
541 int i;
542 int n;
543 isl_ctx *ctx;
544 MULTI(BASE) *multi;
546 if (!space || !list)
547 goto error;
549 ctx = isl_space_get_ctx(space);
550 n = FN(FN(LIST(EL),n),BASE)(list);
551 if (n != isl_space_dim(space, isl_dim_out))
552 isl_die(ctx, isl_error_invalid,
553 "invalid number of elements in list", goto error);
555 for (i = 0; i < n; ++i) {
556 EL *el = FN(LIST(EL),peek)(list, i);
557 space = isl_space_align_params(space, FN(EL,get_space)(el));
559 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
560 for (i = 0; i < n; ++i) {
561 EL *el = FN(FN(LIST(EL),get),BASE)(list, i);
562 el = FN(EL,align_params)(el, isl_space_copy(space));
563 multi = FN(MULTI(BASE),restore)(multi, i, el);
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),free)(multi));
705 multi->space = isl_space_drop_dims(multi->space, type, first, n);
706 if (!multi->space)
707 return FN(MULTI(BASE),free)(multi);
709 if (type == isl_dim_out) {
710 for (i = 0; i < n; ++i)
711 FN(EL,free)(multi->u.p[first + i]);
712 for (i = first; i + n < multi->n; ++i)
713 multi->u.p[i] = multi->u.p[i + n];
714 multi->n -= n;
715 if (n > 0 && FN(MULTI(BASE),has_explicit_domain)(multi))
716 multi = FN(MULTI(BASE),init_explicit_domain)(multi);
718 return multi;
721 if (FN(MULTI(BASE),has_explicit_domain)(multi))
722 multi = FN(MULTI(BASE),drop_explicit_domain_dims)(multi,
723 type, first, n);
724 if (!multi)
725 return NULL;
727 for (i = 0; i < multi->n; ++i) {
728 multi->u.p[i] = FN(EL,drop_dims)(multi->u.p[i], type, first, n);
729 if (!multi->u.p[i])
730 return FN(MULTI(BASE),free)(multi);
733 return multi;
736 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
738 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_multi_and)(
739 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
740 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi1,
741 __isl_take MULTI(BASE) *multi2))
743 isl_ctx *ctx;
744 isl_bool equal_params;
746 if (!multi1 || !multi2)
747 goto error;
748 equal_params = isl_space_has_equal_params(multi1->space, multi2->space);
749 if (equal_params < 0)
750 goto error;
751 if (equal_params)
752 return fn(multi1, multi2);
753 ctx = FN(MULTI(BASE),get_ctx)(multi1);
754 if (!isl_space_has_named_params(multi1->space) ||
755 !isl_space_has_named_params(multi2->space))
756 isl_die(ctx, isl_error_invalid,
757 "unaligned unnamed parameters", goto error);
758 multi1 = FN(MULTI(BASE),align_params)(multi1,
759 FN(MULTI(BASE),get_space)(multi2));
760 multi2 = FN(MULTI(BASE),align_params)(multi2,
761 FN(MULTI(BASE),get_space)(multi1));
762 return fn(multi1, multi2);
763 error:
764 FN(MULTI(BASE),free)(multi1);
765 FN(MULTI(BASE),free)(multi2);
766 return NULL;
769 /* Given two MULTI(BASE)s A -> B and C -> D,
770 * construct a MULTI(BASE) (A * C) -> [B -> D].
772 * The parameters are assumed to have been aligned.
774 * If "multi1" and/or "multi2" has an explicit domain, then
775 * intersect the domain of the result with these explicit domains.
777 static __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product_aligned)(
778 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
780 int i, n1, n2;
781 EL *el;
782 isl_space *space;
783 MULTI(BASE) *res;
785 if (!multi1 || !multi2)
786 goto error;
788 space = isl_space_range_product(FN(MULTI(BASE),get_space)(multi1),
789 FN(MULTI(BASE),get_space)(multi2));
790 res = FN(MULTI(BASE),alloc)(space);
792 n1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
793 n2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
795 for (i = 0; i < n1; ++i) {
796 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
797 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
800 for (i = 0; i < n2; ++i) {
801 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
802 res = FN(FN(MULTI(BASE),set),BASE)(res, n1 + i, el);
805 if (FN(MULTI(BASE),has_explicit_domain)(multi1))
806 res = FN(MULTI(BASE),intersect_explicit_domain)(res, multi1);
807 if (FN(MULTI(BASE),has_explicit_domain)(multi2))
808 res = FN(MULTI(BASE),intersect_explicit_domain)(res, multi2);
810 FN(MULTI(BASE),free)(multi1);
811 FN(MULTI(BASE),free)(multi2);
812 return res;
813 error:
814 FN(MULTI(BASE),free)(multi1);
815 FN(MULTI(BASE),free)(multi2);
816 return NULL;
819 /* Given two MULTI(BASE)s A -> B and C -> D,
820 * construct a MULTI(BASE) (A * C) -> [B -> D].
822 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product)(
823 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
825 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
826 &FN(MULTI(BASE),range_product_aligned));
829 /* Is the range of "multi" a wrapped relation?
831 isl_bool FN(MULTI(BASE),range_is_wrapping)(__isl_keep MULTI(BASE) *multi)
833 if (!multi)
834 return isl_bool_error;
835 return isl_space_range_is_wrapping(multi->space);
838 /* Given a function A -> [B -> C], extract the function A -> B.
840 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_domain)(
841 __isl_take MULTI(BASE) *multi)
843 isl_space *space;
844 int total, keep;
846 if (!multi)
847 return NULL;
848 if (!isl_space_range_is_wrapping(multi->space))
849 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
850 "range is not a product",
851 return FN(MULTI(BASE),free)(multi));
853 space = FN(MULTI(BASE),get_space)(multi);
854 total = isl_space_dim(space, isl_dim_out);
855 space = isl_space_range_factor_domain(space);
856 keep = isl_space_dim(space, isl_dim_out);
857 multi = FN(MULTI(BASE),drop_dims)(multi,
858 isl_dim_out, keep, total - keep);
859 multi = FN(MULTI(BASE),reset_space)(multi, space);
861 return multi;
864 /* Given a function A -> [B -> C], extract the function A -> C.
866 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_range)(
867 __isl_take MULTI(BASE) *multi)
869 isl_space *space;
870 int total, keep;
872 if (!multi)
873 return NULL;
874 if (!isl_space_range_is_wrapping(multi->space))
875 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
876 "range is not a product",
877 return FN(MULTI(BASE),free)(multi));
879 space = FN(MULTI(BASE),get_space)(multi);
880 total = isl_space_dim(space, isl_dim_out);
881 space = isl_space_range_factor_range(space);
882 keep = isl_space_dim(space, isl_dim_out);
883 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_out, 0, total - keep);
884 multi = FN(MULTI(BASE),reset_space)(multi, space);
886 return multi;
889 /* Given a function [B -> C], extract the function C.
891 __isl_give MULTI(BASE) *FN(MULTI(BASE),factor_range)(
892 __isl_take MULTI(BASE) *multi)
894 isl_space *space;
895 int total, keep;
897 if (!multi)
898 return NULL;
899 if (!isl_space_is_wrapping(multi->space))
900 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
901 "not a product", return FN(MULTI(BASE),free)(multi));
903 space = FN(MULTI(BASE),get_space)(multi);
904 total = isl_space_dim(space, isl_dim_out);
905 space = isl_space_factor_range(space);
906 keep = isl_space_dim(space, isl_dim_out);
907 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_out, 0, total - keep);
908 multi = FN(MULTI(BASE),reset_space)(multi, space);
910 return multi;
913 #ifndef NO_PRODUCT
914 /* Given two MULTI(BASE)s A -> B and C -> D,
915 * construct a MULTI(BASE) [A -> C] -> [B -> D].
917 * The parameters are assumed to have been aligned.
919 * If "multi1" and/or "multi2" has an explicit domain, then
920 * intersect the domain of the result with these explicit domains.
922 __isl_give MULTI(BASE) *FN(MULTI(BASE),product_aligned)(
923 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
925 int i;
926 EL *el;
927 isl_space *space;
928 MULTI(BASE) *res;
929 int in1, in2, out1, out2;
931 in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
932 in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
933 out1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
934 out2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
935 space = isl_space_product(FN(MULTI(BASE),get_space)(multi1),
936 FN(MULTI(BASE),get_space)(multi2));
937 res = FN(MULTI(BASE),alloc)(isl_space_copy(space));
938 space = isl_space_domain(space);
940 for (i = 0; i < out1; ++i) {
941 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
942 el = FN(EL,insert_dims)(el, isl_dim_in, in1, in2);
943 el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
944 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
947 for (i = 0; i < out2; ++i) {
948 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
949 el = FN(EL,insert_dims)(el, isl_dim_in, 0, in1);
950 el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
951 res = FN(FN(MULTI(BASE),set),BASE)(res, out1 + i, el);
954 if (FN(MULTI(BASE),has_explicit_domain)(multi1) ||
955 FN(MULTI(BASE),has_explicit_domain)(multi2))
956 res = FN(MULTI(BASE),intersect_explicit_domain_product)(res,
957 multi1, multi2);
959 isl_space_free(space);
960 FN(MULTI(BASE),free)(multi1);
961 FN(MULTI(BASE),free)(multi2);
962 return res;
965 /* Given two MULTI(BASE)s A -> B and C -> D,
966 * construct a MULTI(BASE) [A -> C] -> [B -> D].
968 __isl_give MULTI(BASE) *FN(MULTI(BASE),product)(
969 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
971 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
972 &FN(MULTI(BASE),product_aligned));
974 #endif
976 __isl_give MULTI(BASE) *FN(MULTI(BASE),flatten_range)(
977 __isl_take MULTI(BASE) *multi)
979 if (!multi)
980 return NULL;
982 if (!multi->space->nested[1])
983 return multi;
985 multi = FN(MULTI(BASE),cow)(multi);
986 if (!multi)
987 return NULL;
989 multi->space = isl_space_flatten_range(multi->space);
990 if (!multi->space)
991 return FN(MULTI(BASE),free)(multi);
993 return multi;
996 /* Given two MULTI(BASE)s A -> B and C -> D,
997 * construct a MULTI(BASE) (A * C) -> (B, D).
999 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
1000 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
1002 MULTI(BASE) *multi;
1004 multi = FN(MULTI(BASE),range_product)(multi1, multi2);
1005 multi = FN(MULTI(BASE),flatten_range)(multi);
1006 return multi;
1009 /* Given two multi expressions, "multi1"
1011 * [A] -> [B1 B2]
1013 * where B2 starts at position "pos", and "multi2"
1015 * [A] -> [D]
1017 * return the multi expression
1019 * [A] -> [B1 D B2]
1021 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_splice)(
1022 __isl_take MULTI(BASE) *multi1, unsigned pos,
1023 __isl_take MULTI(BASE) *multi2)
1025 MULTI(BASE) *res;
1026 unsigned dim;
1028 if (!multi1 || !multi2)
1029 goto error;
1031 dim = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
1032 if (pos > dim)
1033 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
1034 "index out of bounds", goto error);
1036 res = FN(MULTI(BASE),copy)(multi1);
1037 res = FN(MULTI(BASE),drop_dims)(res, isl_dim_out, pos, dim - pos);
1038 multi1 = FN(MULTI(BASE),drop_dims)(multi1, isl_dim_out, 0, pos);
1040 res = FN(MULTI(BASE),flat_range_product)(res, multi2);
1041 res = FN(MULTI(BASE),flat_range_product)(res, multi1);
1043 return res;
1044 error:
1045 FN(MULTI(BASE),free)(multi1);
1046 FN(MULTI(BASE),free)(multi2);
1047 return NULL;
1050 #ifndef NO_SPLICE
1051 /* Given two multi expressions, "multi1"
1053 * [A1 A2] -> [B1 B2]
1055 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
1056 * and "multi2"
1058 * [C] -> [D]
1060 * return the multi expression
1062 * [A1 C A2] -> [B1 D B2]
1064 * We first insert input dimensions to obtain
1066 * [A1 C A2] -> [B1 B2]
1068 * and
1070 * [A1 C A2] -> [D]
1072 * and then apply range_splice.
1074 __isl_give MULTI(BASE) *FN(MULTI(BASE),splice)(
1075 __isl_take MULTI(BASE) *multi1, unsigned in_pos, unsigned out_pos,
1076 __isl_take MULTI(BASE) *multi2)
1078 unsigned n_in1;
1079 unsigned n_in2;
1081 if (!multi1 || !multi2)
1082 goto error;
1084 n_in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
1085 if (in_pos > n_in1)
1086 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
1087 "index out of bounds", goto error);
1089 n_in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
1091 multi1 = FN(MULTI(BASE),insert_dims)(multi1, isl_dim_in, in_pos, n_in2);
1092 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, n_in2,
1093 n_in1 - in_pos);
1094 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, 0, in_pos);
1096 return FN(MULTI(BASE),range_splice)(multi1, out_pos, multi2);
1097 error:
1098 FN(MULTI(BASE),free)(multi1);
1099 FN(MULTI(BASE),free)(multi2);
1100 return NULL;
1102 #endif
1104 /* Check that "multi1" and "multi2" live in the same space,
1105 * reporting an error if they do not.
1107 static isl_stat FN(MULTI(BASE),check_equal_space)(
1108 __isl_keep MULTI(BASE) *multi1, __isl_keep MULTI(BASE) *multi2)
1110 isl_bool equal;
1112 if (!multi1 || !multi2)
1113 return isl_stat_error;
1115 equal = isl_space_is_equal(multi1->space, multi2->space);
1116 if (equal < 0)
1117 return isl_stat_error;
1118 if (!equal)
1119 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
1120 "spaces don't match", return isl_stat_error);
1122 return isl_stat_ok;
1125 /* This function is currently only used from isl_aff.c
1127 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
1128 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
1129 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
1130 __attribute__ ((unused));
1132 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
1133 * return the result.
1135 * If "multi2" has an explicit domain, then
1136 * intersect the domain of the result with this explicit domain.
1138 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
1139 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
1140 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
1142 int i;
1144 multi1 = FN(MULTI(BASE),cow)(multi1);
1145 if (FN(MULTI(BASE),check_equal_space)(multi1, multi2) < 0)
1146 goto error;
1148 for (i = 0; i < multi1->n; ++i) {
1149 multi1->u.p[i] = fn(multi1->u.p[i],
1150 FN(EL,copy)(multi2->u.p[i]));
1151 if (!multi1->u.p[i])
1152 goto error;
1155 if (FN(MULTI(BASE),has_explicit_domain)(multi2))
1156 multi1 = FN(MULTI(BASE),intersect_explicit_domain)(multi1,
1157 multi2);
1159 FN(MULTI(BASE),free)(multi2);
1160 return multi1;
1161 error:
1162 FN(MULTI(BASE),free)(multi1);
1163 FN(MULTI(BASE),free)(multi2);
1164 return NULL;
1167 /* Add "multi2" from "multi1" and return the result.
1169 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
1171 static __isl_give MULTI(BASE) *FN(MULTI(BASE),add_aligned)(
1172 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
1174 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,add));
1177 /* Add "multi2" from "multi1" and return the result.
1179 __isl_give MULTI(BASE) *FN(MULTI(BASE),add)(__isl_take MULTI(BASE) *multi1,
1180 __isl_take MULTI(BASE) *multi2)
1182 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
1183 &FN(MULTI(BASE),add_aligned));
1186 /* Subtract "multi2" from "multi1" and return the result.
1188 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
1190 static __isl_give MULTI(BASE) *FN(MULTI(BASE),sub_aligned)(
1191 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
1193 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,sub));
1196 /* Subtract "multi2" from "multi1" and return the result.
1198 __isl_give MULTI(BASE) *FN(MULTI(BASE),sub)(__isl_take MULTI(BASE) *multi1,
1199 __isl_take MULTI(BASE) *multi2)
1201 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
1202 &FN(MULTI(BASE),sub_aligned));
1205 /* Multiply the elements of "multi" by "v" and return the result.
1207 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_val)(__isl_take MULTI(BASE) *multi,
1208 __isl_take isl_val *v)
1210 int i;
1212 if (!multi || !v)
1213 goto error;
1215 if (isl_val_is_one(v)) {
1216 isl_val_free(v);
1217 return multi;
1220 if (!isl_val_is_rat(v))
1221 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1222 "expecting rational factor", goto error);
1224 multi = FN(MULTI(BASE),cow)(multi);
1225 if (!multi)
1226 return NULL;
1228 for (i = 0; i < multi->n; ++i) {
1229 multi->u.p[i] = FN(EL,scale_val)(multi->u.p[i],
1230 isl_val_copy(v));
1231 if (!multi->u.p[i])
1232 goto error;
1235 isl_val_free(v);
1236 return multi;
1237 error:
1238 isl_val_free(v);
1239 return FN(MULTI(BASE),free)(multi);
1242 /* Divide the elements of "multi" by "v" and return the result.
1244 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_val)(
1245 __isl_take MULTI(BASE) *multi, __isl_take isl_val *v)
1247 int i;
1249 if (!multi || !v)
1250 goto error;
1252 if (isl_val_is_one(v)) {
1253 isl_val_free(v);
1254 return multi;
1257 if (!isl_val_is_rat(v))
1258 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1259 "expecting rational factor", goto error);
1260 if (isl_val_is_zero(v))
1261 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1262 "cannot scale down by zero", goto error);
1264 multi = FN(MULTI(BASE),cow)(multi);
1265 if (!multi)
1266 return NULL;
1268 for (i = 0; i < multi->n; ++i) {
1269 multi->u.p[i] = FN(EL,scale_down_val)(multi->u.p[i],
1270 isl_val_copy(v));
1271 if (!multi->u.p[i])
1272 goto error;
1275 isl_val_free(v);
1276 return multi;
1277 error:
1278 isl_val_free(v);
1279 return FN(MULTI(BASE),free)(multi);
1282 /* Multiply the elements of "multi" by the corresponding element of "mv"
1283 * and return the result.
1285 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_multi_val)(
1286 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1288 int i;
1290 if (!multi || !mv)
1291 goto error;
1293 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1294 mv->space, isl_dim_set))
1295 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1296 "spaces don't match", goto error);
1298 multi = FN(MULTI(BASE),cow)(multi);
1299 if (!multi)
1300 goto error;
1302 for (i = 0; i < multi->n; ++i) {
1303 isl_val *v;
1305 v = isl_multi_val_get_val(mv, i);
1306 multi->u.p[i] = FN(EL,scale_val)(multi->u.p[i], v);
1307 if (!multi->u.p[i])
1308 goto error;
1311 isl_multi_val_free(mv);
1312 return multi;
1313 error:
1314 isl_multi_val_free(mv);
1315 return FN(MULTI(BASE),free)(multi);
1318 /* Divide the elements of "multi" by the corresponding element of "mv"
1319 * and return the result.
1321 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_multi_val)(
1322 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1324 int i;
1326 if (!multi || !mv)
1327 goto error;
1329 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1330 mv->space, isl_dim_set))
1331 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1332 "spaces don't match", goto error);
1334 multi = FN(MULTI(BASE),cow)(multi);
1335 if (!multi)
1336 return NULL;
1338 for (i = 0; i < multi->n; ++i) {
1339 isl_val *v;
1341 v = isl_multi_val_get_val(mv, i);
1342 multi->u.p[i] = FN(EL,scale_down_val)(multi->u.p[i], v);
1343 if (!multi->u.p[i])
1344 goto error;
1347 isl_multi_val_free(mv);
1348 return multi;
1349 error:
1350 isl_multi_val_free(mv);
1351 return FN(MULTI(BASE),free)(multi);
1354 /* Compute the residues of the elements of "multi" modulo
1355 * the corresponding element of "mv" and return the result.
1357 __isl_give MULTI(BASE) *FN(MULTI(BASE),mod_multi_val)(
1358 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1360 int i;
1362 if (!multi || !mv)
1363 goto error;
1365 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1366 mv->space, isl_dim_set))
1367 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1368 "spaces don't match", goto error);
1370 multi = FN(MULTI(BASE),cow)(multi);
1371 if (!multi)
1372 goto error;
1374 for (i = 0; i < multi->n; ++i) {
1375 isl_val *v;
1377 v = isl_multi_val_get_val(mv, i);
1378 multi->u.p[i] = FN(EL,mod_val)(multi->u.p[i], v);
1379 if (!multi->u.p[i])
1380 goto error;
1383 isl_multi_val_free(mv);
1384 return multi;
1385 error:
1386 isl_multi_val_free(mv);
1387 return FN(MULTI(BASE),free)(multi);
1390 #ifndef NO_MOVE_DIMS
1391 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1392 * to dimensions of "dst_type" at "dst_pos".
1394 * We only support moving input dimensions to parameters and vice versa.
1396 __isl_give MULTI(BASE) *FN(MULTI(BASE),move_dims)(__isl_take MULTI(BASE) *multi,
1397 enum isl_dim_type dst_type, unsigned dst_pos,
1398 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1400 int i;
1402 if (!multi)
1403 return NULL;
1405 if (n == 0 &&
1406 !isl_space_is_named_or_nested(multi->space, src_type) &&
1407 !isl_space_is_named_or_nested(multi->space, dst_type))
1408 return multi;
1410 if (dst_type == isl_dim_out || src_type == isl_dim_out)
1411 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1412 "cannot move output/set dimension",
1413 return FN(MULTI(BASE),free)(multi));
1414 if (dst_type == isl_dim_div || src_type == isl_dim_div)
1415 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1416 "cannot move divs",
1417 return FN(MULTI(BASE),free)(multi));
1418 if (src_pos + n > isl_space_dim(multi->space, src_type))
1419 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1420 "range out of bounds",
1421 return FN(MULTI(BASE),free)(multi));
1422 if (dst_type == src_type)
1423 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_unsupported,
1424 "moving dims within the same type not supported",
1425 return FN(MULTI(BASE),free)(multi));
1427 multi = FN(MULTI(BASE),cow)(multi);
1428 if (!multi)
1429 return NULL;
1431 multi->space = isl_space_move_dims(multi->space, dst_type, dst_pos,
1432 src_type, src_pos, n);
1433 if (!multi->space)
1434 return FN(MULTI(BASE),free)(multi);
1435 if (FN(MULTI(BASE),has_explicit_domain)(multi))
1436 multi = FN(MULTI(BASE),move_explicit_domain_dims)(multi,
1437 dst_type, dst_pos, src_type, src_pos, n);
1438 if (!multi)
1439 return NULL;
1441 for (i = 0; i < multi->n; ++i) {
1442 multi->u.p[i] = FN(EL,move_dims)(multi->u.p[i],
1443 dst_type, dst_pos,
1444 src_type, src_pos, n);
1445 if (!multi->u.p[i])
1446 return FN(MULTI(BASE),free)(multi);
1449 return multi;
1451 #endif
1453 /* Convert a multiple expression defined over a parameter domain
1454 * into one that is defined over a zero-dimensional set.
1456 __isl_give MULTI(BASE) *FN(MULTI(BASE),from_range)(
1457 __isl_take MULTI(BASE) *multi)
1459 isl_space *space;
1461 if (!multi)
1462 return NULL;
1463 if (!isl_space_is_set(multi->space))
1464 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1465 "not living in a set space",
1466 return FN(MULTI(BASE),free)(multi));
1468 space = FN(MULTI(BASE),get_space)(multi);
1469 space = isl_space_from_range(space);
1470 multi = FN(MULTI(BASE),reset_space)(multi, space);
1472 return multi;
1475 /* Are "multi1" and "multi2" obviously equal?
1477 isl_bool FN(MULTI(BASE),plain_is_equal)(__isl_keep MULTI(BASE) *multi1,
1478 __isl_keep MULTI(BASE) *multi2)
1480 int i;
1481 isl_bool equal;
1483 if (!multi1 || !multi2)
1484 return isl_bool_error;
1485 if (multi1->n != multi2->n)
1486 return isl_bool_false;
1487 equal = isl_space_is_equal(multi1->space, multi2->space);
1488 if (equal < 0 || !equal)
1489 return equal;
1491 for (i = 0; i < multi1->n; ++i) {
1492 equal = FN(EL,plain_is_equal)(multi1->u.p[i], multi2->u.p[i]);
1493 if (equal < 0 || !equal)
1494 return equal;
1497 if (FN(MULTI(BASE),has_explicit_domain)(multi1) ||
1498 FN(MULTI(BASE),has_explicit_domain)(multi2)) {
1499 equal = FN(MULTI(BASE),equal_explicit_domain)(multi1, multi2);
1500 if (equal < 0 || !equal)
1501 return equal;
1504 return isl_bool_true;
1507 /* Does "multi" involve any NaNs?
1509 isl_bool FN(MULTI(BASE),involves_nan)(__isl_keep MULTI(BASE) *multi)
1511 int i;
1513 if (!multi)
1514 return isl_bool_error;
1515 if (multi->n == 0)
1516 return isl_bool_false;
1518 for (i = 0; i < multi->n; ++i) {
1519 isl_bool has_nan = FN(EL,involves_nan)(multi->u.p[i]);
1520 if (has_nan < 0 || has_nan)
1521 return has_nan;
1524 return isl_bool_false;
1527 #ifndef NO_DOMAIN
1528 /* Return the shared domain of the elements of "multi".
1530 * If "multi" has an explicit domain, then return this domain.
1532 __isl_give isl_set *FN(MULTI(BASE),domain)(__isl_take MULTI(BASE) *multi)
1534 int i;
1535 isl_set *dom;
1537 if (!multi)
1538 return NULL;
1540 if (FN(MULTI(BASE),has_explicit_domain)(multi)) {
1541 dom = FN(MULTI(BASE),get_explicit_domain)(multi);
1542 FN(MULTI(BASE),free)(multi);
1543 return dom;
1546 dom = isl_set_universe(FN(MULTI(BASE),get_domain_space)(multi));
1547 for (i = 0; i < multi->n; ++i) {
1548 isl_set *dom_i;
1550 dom_i = FN(EL,domain)(FN(FN(MULTI(BASE),get),BASE)(multi, i));
1551 dom = isl_set_intersect(dom, dom_i);
1554 FN(MULTI(BASE),free)(multi);
1555 return dom;
1557 #endif
1559 #ifndef NO_NEG
1560 /* Return the opposite of "multi".
1562 __isl_give MULTI(BASE) *FN(MULTI(BASE),neg)(__isl_take MULTI(BASE) *multi)
1564 int i;
1566 multi = FN(MULTI(BASE),cow)(multi);
1567 if (!multi)
1568 return NULL;
1570 for (i = 0; i < multi->n; ++i) {
1571 multi->u.p[i] = FN(EL,neg)(multi->u.p[i]);
1572 if (!multi->u.p[i])
1573 return FN(MULTI(BASE),free)(multi);
1576 return multi;
1578 #endif