Merge branch 'maint'
[isl.git] / isl_multi_templ.c
blob335bdd52e34a031b772a0596742875a6cc9a248f
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 model = isl_space_params(model);
519 exp = isl_parameter_alignment_reordering(multi->space, model);
520 exp = isl_reordering_extend_space(exp,
521 FN(MULTI(BASE),get_domain_space)(multi));
522 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
524 isl_space_free(model);
525 return multi;
526 error:
527 isl_space_free(model);
528 FN(MULTI(BASE),free)(multi);
529 return NULL;
532 /* Create a multi expression in the given space with the elements of "list"
533 * as base expressions.
535 * Since isl_multi_*_restore_* assumes that the element and
536 * the multi expression have matching spaces, the alignment
537 * (if any) needs to be performed beforehand.
539 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
540 __isl_take isl_space *space, __isl_take LIST(EL) *list)
542 int i;
543 int n;
544 isl_ctx *ctx;
545 MULTI(BASE) *multi;
547 if (!space || !list)
548 goto error;
550 ctx = isl_space_get_ctx(space);
551 n = FN(FN(LIST(EL),n),BASE)(list);
552 if (n != isl_space_dim(space, isl_dim_out))
553 isl_die(ctx, isl_error_invalid,
554 "invalid number of elements in list", goto error);
556 for (i = 0; i < n; ++i) {
557 EL *el = FN(LIST(EL),peek)(list, i);
558 space = isl_space_align_params(space, FN(EL,get_space)(el));
560 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
561 for (i = 0; i < n; ++i) {
562 EL *el = FN(FN(LIST(EL),get),BASE)(list, i);
563 el = FN(EL,align_params)(el, isl_space_copy(space));
564 multi = FN(MULTI(BASE),restore)(multi, i, el);
567 isl_space_free(space);
568 FN(LIST(EL),free)(list);
569 return multi;
570 error:
571 isl_space_free(space);
572 FN(LIST(EL),free)(list);
573 return NULL;
576 #ifndef NO_IDENTITY
577 /* Create a multi expression in the given space that maps each
578 * input dimension to the corresponding output dimension.
580 __isl_give MULTI(BASE) *FN(MULTI(BASE),identity)(__isl_take isl_space *space)
582 int i, n;
583 isl_local_space *ls;
584 MULTI(BASE) *multi;
586 if (!space)
587 return NULL;
589 if (isl_space_is_set(space))
590 isl_die(isl_space_get_ctx(space), isl_error_invalid,
591 "expecting map space", goto error);
593 n = isl_space_dim(space, isl_dim_out);
594 if (n != isl_space_dim(space, isl_dim_in))
595 isl_die(isl_space_get_ctx(space), isl_error_invalid,
596 "number of input and output dimensions needs to be "
597 "the same", goto error);
599 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
601 if (!n) {
602 isl_space_free(space);
603 return multi;
606 space = isl_space_domain(space);
607 ls = isl_local_space_from_space(space);
609 for (i = 0; i < n; ++i) {
610 EL *el;
611 el = FN(EL,var_on_domain)(isl_local_space_copy(ls),
612 isl_dim_set, i);
613 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i, el);
616 isl_local_space_free(ls);
618 return multi;
619 error:
620 isl_space_free(space);
621 return NULL;
623 #endif
625 #ifndef NO_ZERO
626 /* Construct a multi expression in the given space with value zero in
627 * each of the output dimensions.
629 __isl_give MULTI(BASE) *FN(MULTI(BASE),zero)(__isl_take isl_space *space)
631 int n;
632 MULTI(BASE) *multi;
634 if (!space)
635 return NULL;
637 n = isl_space_dim(space , isl_dim_out);
638 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
640 if (!n)
641 isl_space_free(space);
642 else {
643 int i;
644 isl_local_space *ls;
645 EL *el;
647 space = isl_space_domain(space);
648 ls = isl_local_space_from_space(space);
649 el = FN(EL,zero_on_domain)(ls);
651 for (i = 0; i < n; ++i)
652 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
653 FN(EL,copy)(el));
655 FN(EL,free)(el);
658 return multi;
660 #endif
662 #ifndef NO_FROM_BASE
663 /* Create a multiple expression with a single output/set dimension
664 * equal to "el".
665 * For most multiple expression types, the base type has a single
666 * output/set dimension and the space of the result is therefore
667 * the same as the space of the input.
668 * In the case of isl_multi_union_pw_aff, however, the base type
669 * lives in a parameter space and we therefore need to add
670 * a single set dimension.
672 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),BASE)(__isl_take EL *el)
674 isl_space *space;
675 MULTI(BASE) *multi;
677 space = FN(EL,get_space(el));
678 if (isl_space_is_params(space)) {
679 space = isl_space_set_from_params(space);
680 space = isl_space_add_dims(space, isl_dim_set, 1);
682 multi = FN(MULTI(BASE),alloc)(space);
683 multi = FN(FN(MULTI(BASE),set),BASE)(multi, 0, el);
685 return multi;
687 #endif
689 __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
690 __isl_take MULTI(BASE) *multi,
691 enum isl_dim_type type, unsigned first, unsigned n)
693 int i;
694 unsigned dim;
696 multi = FN(MULTI(BASE),cow)(multi);
697 if (!multi)
698 return NULL;
700 dim = FN(MULTI(BASE),dim)(multi, type);
701 if (first + n > dim || first + n < first)
702 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
703 "index out of bounds",
704 return FN(MULTI(BASE),free)(multi));
706 multi->space = isl_space_drop_dims(multi->space, type, first, n);
707 if (!multi->space)
708 return FN(MULTI(BASE),free)(multi);
710 if (type == isl_dim_out) {
711 for (i = 0; i < n; ++i)
712 FN(EL,free)(multi->u.p[first + i]);
713 for (i = first; i + n < multi->n; ++i)
714 multi->u.p[i] = multi->u.p[i + n];
715 multi->n -= n;
716 if (n > 0 && FN(MULTI(BASE),has_explicit_domain)(multi))
717 multi = FN(MULTI(BASE),init_explicit_domain)(multi);
719 return multi;
722 if (FN(MULTI(BASE),has_explicit_domain)(multi))
723 multi = FN(MULTI(BASE),drop_explicit_domain_dims)(multi,
724 type, first, n);
725 if (!multi)
726 return NULL;
728 for (i = 0; i < multi->n; ++i) {
729 multi->u.p[i] = FN(EL,drop_dims)(multi->u.p[i], type, first, n);
730 if (!multi->u.p[i])
731 return FN(MULTI(BASE),free)(multi);
734 return multi;
737 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
739 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_multi_and)(
740 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
741 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi1,
742 __isl_take MULTI(BASE) *multi2))
744 isl_ctx *ctx;
745 isl_bool equal_params;
747 if (!multi1 || !multi2)
748 goto error;
749 equal_params = isl_space_has_equal_params(multi1->space, multi2->space);
750 if (equal_params < 0)
751 goto error;
752 if (equal_params)
753 return fn(multi1, multi2);
754 ctx = FN(MULTI(BASE),get_ctx)(multi1);
755 if (!isl_space_has_named_params(multi1->space) ||
756 !isl_space_has_named_params(multi2->space))
757 isl_die(ctx, isl_error_invalid,
758 "unaligned unnamed parameters", goto error);
759 multi1 = FN(MULTI(BASE),align_params)(multi1,
760 FN(MULTI(BASE),get_space)(multi2));
761 multi2 = FN(MULTI(BASE),align_params)(multi2,
762 FN(MULTI(BASE),get_space)(multi1));
763 return fn(multi1, multi2);
764 error:
765 FN(MULTI(BASE),free)(multi1);
766 FN(MULTI(BASE),free)(multi2);
767 return NULL;
770 /* Given two MULTI(BASE)s A -> B and C -> D,
771 * construct a MULTI(BASE) (A * C) -> [B -> D].
773 * The parameters are assumed to have been aligned.
775 * If "multi1" and/or "multi2" has an explicit domain, then
776 * intersect the domain of the result with these explicit domains.
778 static __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product_aligned)(
779 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
781 int i, n1, n2;
782 EL *el;
783 isl_space *space;
784 MULTI(BASE) *res;
786 if (!multi1 || !multi2)
787 goto error;
789 space = isl_space_range_product(FN(MULTI(BASE),get_space)(multi1),
790 FN(MULTI(BASE),get_space)(multi2));
791 res = FN(MULTI(BASE),alloc)(space);
793 n1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
794 n2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
796 for (i = 0; i < n1; ++i) {
797 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
798 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
801 for (i = 0; i < n2; ++i) {
802 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
803 res = FN(FN(MULTI(BASE),set),BASE)(res, n1 + i, el);
806 if (FN(MULTI(BASE),has_explicit_domain)(multi1))
807 res = FN(MULTI(BASE),intersect_explicit_domain)(res, multi1);
808 if (FN(MULTI(BASE),has_explicit_domain)(multi2))
809 res = FN(MULTI(BASE),intersect_explicit_domain)(res, multi2);
811 FN(MULTI(BASE),free)(multi1);
812 FN(MULTI(BASE),free)(multi2);
813 return res;
814 error:
815 FN(MULTI(BASE),free)(multi1);
816 FN(MULTI(BASE),free)(multi2);
817 return NULL;
820 /* Given two MULTI(BASE)s A -> B and C -> D,
821 * construct a MULTI(BASE) (A * C) -> [B -> D].
823 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product)(
824 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
826 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
827 &FN(MULTI(BASE),range_product_aligned));
830 /* Is the range of "multi" a wrapped relation?
832 isl_bool FN(MULTI(BASE),range_is_wrapping)(__isl_keep MULTI(BASE) *multi)
834 if (!multi)
835 return isl_bool_error;
836 return isl_space_range_is_wrapping(multi->space);
839 /* Given a function A -> [B -> C], extract the function A -> B.
841 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_domain)(
842 __isl_take MULTI(BASE) *multi)
844 isl_space *space;
845 int total, keep;
847 if (!multi)
848 return NULL;
849 if (!isl_space_range_is_wrapping(multi->space))
850 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
851 "range is not a product",
852 return FN(MULTI(BASE),free)(multi));
854 space = FN(MULTI(BASE),get_space)(multi);
855 total = isl_space_dim(space, isl_dim_out);
856 space = isl_space_range_factor_domain(space);
857 keep = isl_space_dim(space, isl_dim_out);
858 multi = FN(MULTI(BASE),drop_dims)(multi,
859 isl_dim_out, keep, total - keep);
860 multi = FN(MULTI(BASE),reset_space)(multi, space);
862 return multi;
865 /* Given a function A -> [B -> C], extract the function A -> C.
867 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_range)(
868 __isl_take MULTI(BASE) *multi)
870 isl_space *space;
871 int total, keep;
873 if (!multi)
874 return NULL;
875 if (!isl_space_range_is_wrapping(multi->space))
876 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
877 "range is not a product",
878 return FN(MULTI(BASE),free)(multi));
880 space = FN(MULTI(BASE),get_space)(multi);
881 total = isl_space_dim(space, isl_dim_out);
882 space = isl_space_range_factor_range(space);
883 keep = isl_space_dim(space, isl_dim_out);
884 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_out, 0, total - keep);
885 multi = FN(MULTI(BASE),reset_space)(multi, space);
887 return multi;
890 /* Given a function [B -> C], extract the function C.
892 __isl_give MULTI(BASE) *FN(MULTI(BASE),factor_range)(
893 __isl_take MULTI(BASE) *multi)
895 isl_space *space;
896 int total, keep;
898 if (!multi)
899 return NULL;
900 if (!isl_space_is_wrapping(multi->space))
901 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
902 "not a product", return FN(MULTI(BASE),free)(multi));
904 space = FN(MULTI(BASE),get_space)(multi);
905 total = isl_space_dim(space, isl_dim_out);
906 space = isl_space_factor_range(space);
907 keep = isl_space_dim(space, isl_dim_out);
908 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_out, 0, total - keep);
909 multi = FN(MULTI(BASE),reset_space)(multi, space);
911 return multi;
914 #ifndef NO_PRODUCT
915 /* Given two MULTI(BASE)s A -> B and C -> D,
916 * construct a MULTI(BASE) [A -> C] -> [B -> D].
918 * The parameters are assumed to have been aligned.
920 * If "multi1" and/or "multi2" has an explicit domain, then
921 * intersect the domain of the result with these explicit domains.
923 __isl_give MULTI(BASE) *FN(MULTI(BASE),product_aligned)(
924 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
926 int i;
927 EL *el;
928 isl_space *space;
929 MULTI(BASE) *res;
930 int in1, in2, out1, out2;
932 in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
933 in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
934 out1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
935 out2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
936 space = isl_space_product(FN(MULTI(BASE),get_space)(multi1),
937 FN(MULTI(BASE),get_space)(multi2));
938 res = FN(MULTI(BASE),alloc)(isl_space_copy(space));
939 space = isl_space_domain(space);
941 for (i = 0; i < out1; ++i) {
942 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
943 el = FN(EL,insert_dims)(el, isl_dim_in, in1, in2);
944 el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
945 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
948 for (i = 0; i < out2; ++i) {
949 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
950 el = FN(EL,insert_dims)(el, isl_dim_in, 0, in1);
951 el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
952 res = FN(FN(MULTI(BASE),set),BASE)(res, out1 + i, el);
955 if (FN(MULTI(BASE),has_explicit_domain)(multi1) ||
956 FN(MULTI(BASE),has_explicit_domain)(multi2))
957 res = FN(MULTI(BASE),intersect_explicit_domain_product)(res,
958 multi1, multi2);
960 isl_space_free(space);
961 FN(MULTI(BASE),free)(multi1);
962 FN(MULTI(BASE),free)(multi2);
963 return res;
966 /* Given two MULTI(BASE)s A -> B and C -> D,
967 * construct a MULTI(BASE) [A -> C] -> [B -> D].
969 __isl_give MULTI(BASE) *FN(MULTI(BASE),product)(
970 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
972 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
973 &FN(MULTI(BASE),product_aligned));
975 #endif
977 __isl_give MULTI(BASE) *FN(MULTI(BASE),flatten_range)(
978 __isl_take MULTI(BASE) *multi)
980 if (!multi)
981 return NULL;
983 if (!multi->space->nested[1])
984 return multi;
986 multi = FN(MULTI(BASE),cow)(multi);
987 if (!multi)
988 return NULL;
990 multi->space = isl_space_flatten_range(multi->space);
991 if (!multi->space)
992 return FN(MULTI(BASE),free)(multi);
994 return multi;
997 /* Given two MULTI(BASE)s A -> B and C -> D,
998 * construct a MULTI(BASE) (A * C) -> (B, D).
1000 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
1001 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
1003 MULTI(BASE) *multi;
1005 multi = FN(MULTI(BASE),range_product)(multi1, multi2);
1006 multi = FN(MULTI(BASE),flatten_range)(multi);
1007 return multi;
1010 /* Given two multi expressions, "multi1"
1012 * [A] -> [B1 B2]
1014 * where B2 starts at position "pos", and "multi2"
1016 * [A] -> [D]
1018 * return the multi expression
1020 * [A] -> [B1 D B2]
1022 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_splice)(
1023 __isl_take MULTI(BASE) *multi1, unsigned pos,
1024 __isl_take MULTI(BASE) *multi2)
1026 MULTI(BASE) *res;
1027 unsigned dim;
1029 if (!multi1 || !multi2)
1030 goto error;
1032 dim = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
1033 if (pos > dim)
1034 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
1035 "index out of bounds", goto error);
1037 res = FN(MULTI(BASE),copy)(multi1);
1038 res = FN(MULTI(BASE),drop_dims)(res, isl_dim_out, pos, dim - pos);
1039 multi1 = FN(MULTI(BASE),drop_dims)(multi1, isl_dim_out, 0, pos);
1041 res = FN(MULTI(BASE),flat_range_product)(res, multi2);
1042 res = FN(MULTI(BASE),flat_range_product)(res, multi1);
1044 return res;
1045 error:
1046 FN(MULTI(BASE),free)(multi1);
1047 FN(MULTI(BASE),free)(multi2);
1048 return NULL;
1051 #ifndef NO_SPLICE
1052 /* Given two multi expressions, "multi1"
1054 * [A1 A2] -> [B1 B2]
1056 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
1057 * and "multi2"
1059 * [C] -> [D]
1061 * return the multi expression
1063 * [A1 C A2] -> [B1 D B2]
1065 * We first insert input dimensions to obtain
1067 * [A1 C A2] -> [B1 B2]
1069 * and
1071 * [A1 C A2] -> [D]
1073 * and then apply range_splice.
1075 __isl_give MULTI(BASE) *FN(MULTI(BASE),splice)(
1076 __isl_take MULTI(BASE) *multi1, unsigned in_pos, unsigned out_pos,
1077 __isl_take MULTI(BASE) *multi2)
1079 unsigned n_in1;
1080 unsigned n_in2;
1082 if (!multi1 || !multi2)
1083 goto error;
1085 n_in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
1086 if (in_pos > n_in1)
1087 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
1088 "index out of bounds", goto error);
1090 n_in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
1092 multi1 = FN(MULTI(BASE),insert_dims)(multi1, isl_dim_in, in_pos, n_in2);
1093 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, n_in2,
1094 n_in1 - in_pos);
1095 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, 0, in_pos);
1097 return FN(MULTI(BASE),range_splice)(multi1, out_pos, multi2);
1098 error:
1099 FN(MULTI(BASE),free)(multi1);
1100 FN(MULTI(BASE),free)(multi2);
1101 return NULL;
1103 #endif
1105 /* Check that "multi1" and "multi2" live in the same space,
1106 * reporting an error if they do not.
1108 static isl_stat FN(MULTI(BASE),check_equal_space)(
1109 __isl_keep MULTI(BASE) *multi1, __isl_keep MULTI(BASE) *multi2)
1111 isl_bool equal;
1113 if (!multi1 || !multi2)
1114 return isl_stat_error;
1116 equal = isl_space_is_equal(multi1->space, multi2->space);
1117 if (equal < 0)
1118 return isl_stat_error;
1119 if (!equal)
1120 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
1121 "spaces don't match", return isl_stat_error);
1123 return isl_stat_ok;
1126 /* This function is currently only used from isl_aff.c
1128 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
1129 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
1130 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
1131 __attribute__ ((unused));
1133 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
1134 * return the result.
1136 * If "multi2" has an explicit domain, then
1137 * intersect the domain of the result with this explicit domain.
1139 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
1140 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
1141 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
1143 int i;
1145 multi1 = FN(MULTI(BASE),cow)(multi1);
1146 if (FN(MULTI(BASE),check_equal_space)(multi1, multi2) < 0)
1147 goto error;
1149 for (i = 0; i < multi1->n; ++i) {
1150 multi1->u.p[i] = fn(multi1->u.p[i],
1151 FN(EL,copy)(multi2->u.p[i]));
1152 if (!multi1->u.p[i])
1153 goto error;
1156 if (FN(MULTI(BASE),has_explicit_domain)(multi2))
1157 multi1 = FN(MULTI(BASE),intersect_explicit_domain)(multi1,
1158 multi2);
1160 FN(MULTI(BASE),free)(multi2);
1161 return multi1;
1162 error:
1163 FN(MULTI(BASE),free)(multi1);
1164 FN(MULTI(BASE),free)(multi2);
1165 return NULL;
1168 /* Add "multi2" from "multi1" and return the result.
1170 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
1172 static __isl_give MULTI(BASE) *FN(MULTI(BASE),add_aligned)(
1173 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
1175 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,add));
1178 /* Add "multi2" from "multi1" and return the result.
1180 __isl_give MULTI(BASE) *FN(MULTI(BASE),add)(__isl_take MULTI(BASE) *multi1,
1181 __isl_take MULTI(BASE) *multi2)
1183 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
1184 &FN(MULTI(BASE),add_aligned));
1187 /* Subtract "multi2" from "multi1" and return the result.
1189 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
1191 static __isl_give MULTI(BASE) *FN(MULTI(BASE),sub_aligned)(
1192 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
1194 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,sub));
1197 /* Subtract "multi2" from "multi1" and return the result.
1199 __isl_give MULTI(BASE) *FN(MULTI(BASE),sub)(__isl_take MULTI(BASE) *multi1,
1200 __isl_take MULTI(BASE) *multi2)
1202 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
1203 &FN(MULTI(BASE),sub_aligned));
1206 /* Multiply the elements of "multi" by "v" and return the result.
1208 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_val)(__isl_take MULTI(BASE) *multi,
1209 __isl_take isl_val *v)
1211 int i;
1213 if (!multi || !v)
1214 goto error;
1216 if (isl_val_is_one(v)) {
1217 isl_val_free(v);
1218 return multi;
1221 if (!isl_val_is_rat(v))
1222 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1223 "expecting rational factor", goto error);
1225 multi = FN(MULTI(BASE),cow)(multi);
1226 if (!multi)
1227 return NULL;
1229 for (i = 0; i < multi->n; ++i) {
1230 multi->u.p[i] = FN(EL,scale_val)(multi->u.p[i],
1231 isl_val_copy(v));
1232 if (!multi->u.p[i])
1233 goto error;
1236 isl_val_free(v);
1237 return multi;
1238 error:
1239 isl_val_free(v);
1240 return FN(MULTI(BASE),free)(multi);
1243 /* Divide the elements of "multi" by "v" and return the result.
1245 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_val)(
1246 __isl_take MULTI(BASE) *multi, __isl_take isl_val *v)
1248 int i;
1250 if (!multi || !v)
1251 goto error;
1253 if (isl_val_is_one(v)) {
1254 isl_val_free(v);
1255 return multi;
1258 if (!isl_val_is_rat(v))
1259 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1260 "expecting rational factor", goto error);
1261 if (isl_val_is_zero(v))
1262 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1263 "cannot scale down by zero", goto error);
1265 multi = FN(MULTI(BASE),cow)(multi);
1266 if (!multi)
1267 return NULL;
1269 for (i = 0; i < multi->n; ++i) {
1270 multi->u.p[i] = FN(EL,scale_down_val)(multi->u.p[i],
1271 isl_val_copy(v));
1272 if (!multi->u.p[i])
1273 goto error;
1276 isl_val_free(v);
1277 return multi;
1278 error:
1279 isl_val_free(v);
1280 return FN(MULTI(BASE),free)(multi);
1283 /* Multiply the elements of "multi" by the corresponding element of "mv"
1284 * and return the result.
1286 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_multi_val)(
1287 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1289 int i;
1291 if (!multi || !mv)
1292 goto error;
1294 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1295 mv->space, isl_dim_set))
1296 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1297 "spaces don't match", goto error);
1299 multi = FN(MULTI(BASE),cow)(multi);
1300 if (!multi)
1301 goto error;
1303 for (i = 0; i < multi->n; ++i) {
1304 isl_val *v;
1306 v = isl_multi_val_get_val(mv, i);
1307 multi->u.p[i] = FN(EL,scale_val)(multi->u.p[i], v);
1308 if (!multi->u.p[i])
1309 goto error;
1312 isl_multi_val_free(mv);
1313 return multi;
1314 error:
1315 isl_multi_val_free(mv);
1316 return FN(MULTI(BASE),free)(multi);
1319 /* Divide the elements of "multi" by the corresponding element of "mv"
1320 * and return the result.
1322 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_multi_val)(
1323 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1325 int i;
1327 if (!multi || !mv)
1328 goto error;
1330 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1331 mv->space, isl_dim_set))
1332 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1333 "spaces don't match", goto error);
1335 multi = FN(MULTI(BASE),cow)(multi);
1336 if (!multi)
1337 return NULL;
1339 for (i = 0; i < multi->n; ++i) {
1340 isl_val *v;
1342 v = isl_multi_val_get_val(mv, i);
1343 multi->u.p[i] = FN(EL,scale_down_val)(multi->u.p[i], v);
1344 if (!multi->u.p[i])
1345 goto error;
1348 isl_multi_val_free(mv);
1349 return multi;
1350 error:
1351 isl_multi_val_free(mv);
1352 return FN(MULTI(BASE),free)(multi);
1355 /* Compute the residues of the elements of "multi" modulo
1356 * the corresponding element of "mv" and return the result.
1358 __isl_give MULTI(BASE) *FN(MULTI(BASE),mod_multi_val)(
1359 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1361 int i;
1363 if (!multi || !mv)
1364 goto error;
1366 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1367 mv->space, isl_dim_set))
1368 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1369 "spaces don't match", goto error);
1371 multi = FN(MULTI(BASE),cow)(multi);
1372 if (!multi)
1373 goto error;
1375 for (i = 0; i < multi->n; ++i) {
1376 isl_val *v;
1378 v = isl_multi_val_get_val(mv, i);
1379 multi->u.p[i] = FN(EL,mod_val)(multi->u.p[i], v);
1380 if (!multi->u.p[i])
1381 goto error;
1384 isl_multi_val_free(mv);
1385 return multi;
1386 error:
1387 isl_multi_val_free(mv);
1388 return FN(MULTI(BASE),free)(multi);
1391 #ifndef NO_MOVE_DIMS
1392 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1393 * to dimensions of "dst_type" at "dst_pos".
1395 * We only support moving input dimensions to parameters and vice versa.
1397 __isl_give MULTI(BASE) *FN(MULTI(BASE),move_dims)(__isl_take MULTI(BASE) *multi,
1398 enum isl_dim_type dst_type, unsigned dst_pos,
1399 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1401 int i;
1403 if (!multi)
1404 return NULL;
1406 if (n == 0 &&
1407 !isl_space_is_named_or_nested(multi->space, src_type) &&
1408 !isl_space_is_named_or_nested(multi->space, dst_type))
1409 return multi;
1411 if (dst_type == isl_dim_out || src_type == isl_dim_out)
1412 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1413 "cannot move output/set dimension",
1414 return FN(MULTI(BASE),free)(multi));
1415 if (dst_type == isl_dim_div || src_type == isl_dim_div)
1416 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1417 "cannot move divs",
1418 return FN(MULTI(BASE),free)(multi));
1419 if (src_pos + n > isl_space_dim(multi->space, src_type))
1420 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1421 "range out of bounds",
1422 return FN(MULTI(BASE),free)(multi));
1423 if (dst_type == src_type)
1424 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_unsupported,
1425 "moving dims within the same type not supported",
1426 return FN(MULTI(BASE),free)(multi));
1428 multi = FN(MULTI(BASE),cow)(multi);
1429 if (!multi)
1430 return NULL;
1432 multi->space = isl_space_move_dims(multi->space, dst_type, dst_pos,
1433 src_type, src_pos, n);
1434 if (!multi->space)
1435 return FN(MULTI(BASE),free)(multi);
1436 if (FN(MULTI(BASE),has_explicit_domain)(multi))
1437 multi = FN(MULTI(BASE),move_explicit_domain_dims)(multi,
1438 dst_type, dst_pos, src_type, src_pos, n);
1439 if (!multi)
1440 return NULL;
1442 for (i = 0; i < multi->n; ++i) {
1443 multi->u.p[i] = FN(EL,move_dims)(multi->u.p[i],
1444 dst_type, dst_pos,
1445 src_type, src_pos, n);
1446 if (!multi->u.p[i])
1447 return FN(MULTI(BASE),free)(multi);
1450 return multi;
1452 #endif
1454 /* Convert a multiple expression defined over a parameter domain
1455 * into one that is defined over a zero-dimensional set.
1457 __isl_give MULTI(BASE) *FN(MULTI(BASE),from_range)(
1458 __isl_take MULTI(BASE) *multi)
1460 isl_space *space;
1462 if (!multi)
1463 return NULL;
1464 if (!isl_space_is_set(multi->space))
1465 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1466 "not living in a set space",
1467 return FN(MULTI(BASE),free)(multi));
1469 space = FN(MULTI(BASE),get_space)(multi);
1470 space = isl_space_from_range(space);
1471 multi = FN(MULTI(BASE),reset_space)(multi, space);
1473 return multi;
1476 /* Are "multi1" and "multi2" obviously equal?
1478 isl_bool FN(MULTI(BASE),plain_is_equal)(__isl_keep MULTI(BASE) *multi1,
1479 __isl_keep MULTI(BASE) *multi2)
1481 int i;
1482 isl_bool equal;
1484 if (!multi1 || !multi2)
1485 return isl_bool_error;
1486 if (multi1->n != multi2->n)
1487 return isl_bool_false;
1488 equal = isl_space_is_equal(multi1->space, multi2->space);
1489 if (equal < 0 || !equal)
1490 return equal;
1492 for (i = 0; i < multi1->n; ++i) {
1493 equal = FN(EL,plain_is_equal)(multi1->u.p[i], multi2->u.p[i]);
1494 if (equal < 0 || !equal)
1495 return equal;
1498 if (FN(MULTI(BASE),has_explicit_domain)(multi1) ||
1499 FN(MULTI(BASE),has_explicit_domain)(multi2)) {
1500 equal = FN(MULTI(BASE),equal_explicit_domain)(multi1, multi2);
1501 if (equal < 0 || !equal)
1502 return equal;
1505 return isl_bool_true;
1508 /* Does "multi" involve any NaNs?
1510 isl_bool FN(MULTI(BASE),involves_nan)(__isl_keep MULTI(BASE) *multi)
1512 int i;
1514 if (!multi)
1515 return isl_bool_error;
1516 if (multi->n == 0)
1517 return isl_bool_false;
1519 for (i = 0; i < multi->n; ++i) {
1520 isl_bool has_nan = FN(EL,involves_nan)(multi->u.p[i]);
1521 if (has_nan < 0 || has_nan)
1522 return has_nan;
1525 return isl_bool_false;
1528 #ifndef NO_DOMAIN
1529 /* Return the shared domain of the elements of "multi".
1531 * If "multi" has an explicit domain, then return this domain.
1533 __isl_give isl_set *FN(MULTI(BASE),domain)(__isl_take MULTI(BASE) *multi)
1535 int i;
1536 isl_set *dom;
1538 if (!multi)
1539 return NULL;
1541 if (FN(MULTI(BASE),has_explicit_domain)(multi)) {
1542 dom = FN(MULTI(BASE),get_explicit_domain)(multi);
1543 FN(MULTI(BASE),free)(multi);
1544 return dom;
1547 dom = isl_set_universe(FN(MULTI(BASE),get_domain_space)(multi));
1548 for (i = 0; i < multi->n; ++i) {
1549 isl_set *dom_i;
1551 dom_i = FN(EL,domain)(FN(FN(MULTI(BASE),get),BASE)(multi, i));
1552 dom = isl_set_intersect(dom, dom_i);
1555 FN(MULTI(BASE),free)(multi);
1556 return dom;
1558 #endif
1560 #ifndef NO_NEG
1561 /* Return the opposite of "multi".
1563 __isl_give MULTI(BASE) *FN(MULTI(BASE),neg)(__isl_take MULTI(BASE) *multi)
1565 int i;
1567 multi = FN(MULTI(BASE),cow)(multi);
1568 if (!multi)
1569 return NULL;
1571 for (i = 0; i < multi->n; ++i) {
1572 multi->u.p[i] = FN(EL,neg)(multi->u.p[i]);
1573 if (!multi->u.p[i])
1574 return FN(MULTI(BASE),free)(multi);
1577 return multi;
1579 #endif