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