isl_reordering: rename "dim" field to "space"
[isl.git] / isl_multi_templ.c
blob8c10c4273c8afdbecfcb0b8bf42977b8da75c4b5
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 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
239 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
241 isl_space *multi_space = NULL;
242 isl_space *el_space = NULL;
243 isl_bool match;
245 multi = FN(MULTI(BASE),cow)(multi);
246 if (!multi || !el)
247 goto error;
249 multi_space = FN(MULTI(BASE),get_space)(multi);
250 match = FN(EL,matching_params)(el, multi_space);
251 if (match < 0)
252 goto error;
253 if (!match) {
254 multi = FN(MULTI(BASE),align_params)(multi,
255 FN(EL,get_space)(el));
256 isl_space_free(multi_space);
257 multi_space = FN(MULTI(BASE),get_space)(multi);
258 el = FN(EL,align_params)(el, isl_space_copy(multi_space));
260 if (FN(EL,check_match_domain_space)(el, multi_space) < 0)
261 goto error;
263 if (pos < 0 || pos >= multi->n)
264 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
265 "index out of bounds", goto error);
267 FN(EL,free)(multi->u.p[pos]);
268 multi->u.p[pos] = el;
270 isl_space_free(multi_space);
271 isl_space_free(el_space);
273 return multi;
274 error:
275 FN(MULTI(BASE),free)(multi);
276 FN(EL,free)(el);
277 isl_space_free(multi_space);
278 isl_space_free(el_space);
279 return NULL;
282 /* Reset the space of "multi". This function is called from isl_pw_templ.c
283 * and doesn't know if the space of an element object is represented
284 * directly or through its domain. It therefore passes along both,
285 * which we pass along to the element function since we don't know how
286 * that is represented either.
288 * If "multi" has an explicit domain, then the caller is expected
289 * to make sure that any modification that would change the dimensions
290 * of the explicit domain has bee applied before this function is called.
292 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
293 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
294 __isl_take isl_space *domain)
296 int i;
298 multi = FN(MULTI(BASE),cow)(multi);
299 if (!multi || !space || !domain)
300 goto error;
302 for (i = 0; i < multi->n; ++i) {
303 multi->u.p[i] = FN(EL,reset_domain_space)(multi->u.p[i],
304 isl_space_copy(domain));
305 if (!multi->u.p[i])
306 goto error;
308 if (FN(MULTI(BASE),has_explicit_domain)(multi)) {
309 multi = FN(MULTI(BASE),reset_explicit_domain_space)(multi,
310 isl_space_copy(domain));
311 if (!multi)
312 goto error;
314 isl_space_free(domain);
315 isl_space_free(multi->space);
316 multi->space = space;
318 return multi;
319 error:
320 isl_space_free(domain);
321 isl_space_free(space);
322 FN(MULTI(BASE),free)(multi);
323 return NULL;
326 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
327 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
329 isl_space *space;
331 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
332 isl_space_copy(multi->space));
333 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
336 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
337 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
339 isl_space *domain;
341 domain = isl_space_domain(isl_space_copy(space));
342 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
345 /* Set the id of the given dimension of "multi" to "id".
347 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_id)(
348 __isl_take MULTI(BASE) *multi,
349 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
351 isl_space *space;
353 multi = FN(MULTI(BASE),cow)(multi);
354 if (!multi || !id)
355 goto error;
357 space = FN(MULTI(BASE),get_space)(multi);
358 space = isl_space_set_dim_id(space, type, pos, id);
360 return FN(MULTI(BASE),reset_space)(multi, space);
361 error:
362 isl_id_free(id);
363 FN(MULTI(BASE),free)(multi);
364 return NULL;
367 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_name)(
368 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
369 const char *s)
371 isl_space *space;
373 multi = FN(MULTI(BASE),cow)(multi);
374 if (!multi)
375 return NULL;
377 space = FN(MULTI(BASE),get_space)(multi);
378 space = isl_space_set_tuple_name(space, type, s);
380 return FN(MULTI(BASE),reset_space)(multi, space);
383 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_id)(
384 __isl_take MULTI(BASE) *multi, enum isl_dim_type type,
385 __isl_take isl_id *id)
387 isl_space *space;
389 multi = FN(MULTI(BASE),cow)(multi);
390 if (!multi)
391 goto error;
393 space = FN(MULTI(BASE),get_space)(multi);
394 space = isl_space_set_tuple_id(space, type, id);
396 return FN(MULTI(BASE),reset_space)(multi, space);
397 error:
398 isl_id_free(id);
399 return NULL;
402 /* Drop the id on the specified tuple.
404 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_tuple_id)(
405 __isl_take MULTI(BASE) *multi, enum isl_dim_type type)
407 isl_space *space;
409 if (!multi)
410 return NULL;
411 if (!FN(MULTI(BASE),has_tuple_id)(multi, type))
412 return multi;
414 multi = FN(MULTI(BASE),cow)(multi);
415 if (!multi)
416 return NULL;
418 space = FN(MULTI(BASE),get_space)(multi);
419 space = isl_space_reset_tuple_id(space, type);
421 return FN(MULTI(BASE),reset_space)(multi, space);
424 /* Reset the user pointer on all identifiers of parameters and tuples
425 * of the space of "multi".
427 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_user)(
428 __isl_take MULTI(BASE) *multi)
430 isl_space *space;
432 space = FN(MULTI(BASE),get_space)(multi);
433 space = isl_space_reset_user(space);
435 return FN(MULTI(BASE),reset_space)(multi, space);
438 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
439 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
441 int i;
442 isl_space *space;
444 multi = FN(MULTI(BASE),cow)(multi);
445 if (!multi || !exp)
446 goto error;
448 for (i = 0; i < multi->n; ++i) {
449 multi->u.p[i] = FN(EL,realign_domain)(multi->u.p[i],
450 isl_reordering_copy(exp));
451 if (!multi->u.p[i])
452 goto error;
455 space = isl_reordering_get_space(exp);
456 multi = FN(MULTI(BASE),reset_domain_space)(multi, space);
458 isl_reordering_free(exp);
459 return multi;
460 error:
461 isl_reordering_free(exp);
462 FN(MULTI(BASE),free)(multi);
463 return NULL;
466 /* Align the parameters of "multi" to those of "model".
468 * If "multi" has an explicit domain, then align the parameters
469 * of the domain first.
471 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
472 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
474 isl_ctx *ctx;
475 isl_bool equal_params;
476 isl_reordering *exp;
478 if (!multi || !model)
479 goto error;
481 equal_params = isl_space_has_equal_params(multi->space, model);
482 if (equal_params < 0)
483 goto error;
484 if (equal_params) {
485 isl_space_free(model);
486 return multi;
489 ctx = isl_space_get_ctx(model);
490 if (!isl_space_has_named_params(model))
491 isl_die(ctx, isl_error_invalid,
492 "model has unnamed parameters", goto error);
493 if (!isl_space_has_named_params(multi->space))
494 isl_die(ctx, isl_error_invalid,
495 "input has unnamed parameters", goto error);
497 if (FN(MULTI(BASE),has_explicit_domain)(multi)) {
498 multi = FN(MULTI(BASE),align_explicit_domain_params)(multi,
499 isl_space_copy(model));
500 if (!multi)
501 goto error;
503 model = isl_space_params(model);
504 exp = isl_parameter_alignment_reordering(multi->space, model);
505 exp = isl_reordering_extend_space(exp,
506 FN(MULTI(BASE),get_domain_space)(multi));
507 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
509 isl_space_free(model);
510 return multi;
511 error:
512 isl_space_free(model);
513 FN(MULTI(BASE),free)(multi);
514 return NULL;
517 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
518 __isl_take isl_space *space, __isl_take LIST(EL) *list)
520 int i;
521 int n;
522 isl_ctx *ctx;
523 MULTI(BASE) *multi;
525 if (!space || !list)
526 goto error;
528 ctx = isl_space_get_ctx(space);
529 n = FN(FN(LIST(EL),n),BASE)(list);
530 if (n != isl_space_dim(space, isl_dim_out))
531 isl_die(ctx, isl_error_invalid,
532 "invalid number of elements in list", goto error);
534 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
535 for (i = 0; i < n; ++i) {
536 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
537 FN(FN(LIST(EL),get),BASE)(list, i));
540 isl_space_free(space);
541 FN(LIST(EL),free)(list);
542 return multi;
543 error:
544 isl_space_free(space);
545 FN(LIST(EL),free)(list);
546 return NULL;
549 #ifndef NO_IDENTITY
550 /* Create a multi expression in the given space that maps each
551 * input dimension to the corresponding output dimension.
553 __isl_give MULTI(BASE) *FN(MULTI(BASE),identity)(__isl_take isl_space *space)
555 int i, n;
556 isl_local_space *ls;
557 MULTI(BASE) *multi;
559 if (!space)
560 return NULL;
562 if (isl_space_is_set(space))
563 isl_die(isl_space_get_ctx(space), isl_error_invalid,
564 "expecting map space", goto error);
566 n = isl_space_dim(space, isl_dim_out);
567 if (n != isl_space_dim(space, isl_dim_in))
568 isl_die(isl_space_get_ctx(space), isl_error_invalid,
569 "number of input and output dimensions needs to be "
570 "the same", goto error);
572 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
574 if (!n) {
575 isl_space_free(space);
576 return multi;
579 space = isl_space_domain(space);
580 ls = isl_local_space_from_space(space);
582 for (i = 0; i < n; ++i) {
583 EL *el;
584 el = FN(EL,var_on_domain)(isl_local_space_copy(ls),
585 isl_dim_set, i);
586 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i, el);
589 isl_local_space_free(ls);
591 return multi;
592 error:
593 isl_space_free(space);
594 return NULL;
596 #endif
598 #ifndef NO_ZERO
599 /* Construct a multi expression in the given space with value zero in
600 * each of the output dimensions.
602 __isl_give MULTI(BASE) *FN(MULTI(BASE),zero)(__isl_take isl_space *space)
604 int n;
605 MULTI(BASE) *multi;
607 if (!space)
608 return NULL;
610 n = isl_space_dim(space , isl_dim_out);
611 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
613 if (!n)
614 isl_space_free(space);
615 else {
616 int i;
617 isl_local_space *ls;
618 EL *el;
620 space = isl_space_domain(space);
621 ls = isl_local_space_from_space(space);
622 el = FN(EL,zero_on_domain)(ls);
624 for (i = 0; i < n; ++i)
625 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
626 FN(EL,copy)(el));
628 FN(EL,free)(el);
631 return multi;
633 #endif
635 #ifndef NO_FROM_BASE
636 /* Create a multiple expression with a single output/set dimension
637 * equal to "el".
638 * For most multiple expression types, the base type has a single
639 * output/set dimension and the space of the result is therefore
640 * the same as the space of the input.
641 * In the case of isl_multi_union_pw_aff, however, the base type
642 * lives in a parameter space and we therefore need to add
643 * a single set dimension.
645 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),BASE)(__isl_take EL *el)
647 isl_space *space;
648 MULTI(BASE) *multi;
650 space = FN(EL,get_space(el));
651 if (isl_space_is_params(space)) {
652 space = isl_space_set_from_params(space);
653 space = isl_space_add_dims(space, isl_dim_set, 1);
655 multi = FN(MULTI(BASE),alloc)(space);
656 multi = FN(FN(MULTI(BASE),set),BASE)(multi, 0, el);
658 return multi;
660 #endif
662 __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
663 __isl_take MULTI(BASE) *multi,
664 enum isl_dim_type type, unsigned first, unsigned n)
666 int i;
667 unsigned dim;
669 multi = FN(MULTI(BASE),cow)(multi);
670 if (!multi)
671 return NULL;
673 dim = FN(MULTI(BASE),dim)(multi, type);
674 if (first + n > dim || first + n < first)
675 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
676 "index out of bounds",
677 return FN(MULTI(BASE),free)(multi));
679 multi->space = isl_space_drop_dims(multi->space, type, first, n);
680 if (!multi->space)
681 return FN(MULTI(BASE),free)(multi);
683 if (type == isl_dim_out) {
684 for (i = 0; i < n; ++i)
685 FN(EL,free)(multi->u.p[first + i]);
686 for (i = first; i + n < multi->n; ++i)
687 multi->u.p[i] = multi->u.p[i + n];
688 multi->n -= n;
689 if (n > 0 && FN(MULTI(BASE),has_explicit_domain)(multi))
690 multi = FN(MULTI(BASE),init_explicit_domain)(multi);
692 return multi;
695 if (FN(MULTI(BASE),has_explicit_domain)(multi))
696 multi = FN(MULTI(BASE),drop_explicit_domain_dims)(multi,
697 type, first, n);
698 if (!multi)
699 return NULL;
701 for (i = 0; i < multi->n; ++i) {
702 multi->u.p[i] = FN(EL,drop_dims)(multi->u.p[i], type, first, n);
703 if (!multi->u.p[i])
704 return FN(MULTI(BASE),free)(multi);
707 return multi;
710 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
712 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_multi_and)(
713 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
714 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi1,
715 __isl_take MULTI(BASE) *multi2))
717 isl_ctx *ctx;
718 isl_bool equal_params;
720 if (!multi1 || !multi2)
721 goto error;
722 equal_params = isl_space_has_equal_params(multi1->space, multi2->space);
723 if (equal_params < 0)
724 goto error;
725 if (equal_params)
726 return fn(multi1, multi2);
727 ctx = FN(MULTI(BASE),get_ctx)(multi1);
728 if (!isl_space_has_named_params(multi1->space) ||
729 !isl_space_has_named_params(multi2->space))
730 isl_die(ctx, isl_error_invalid,
731 "unaligned unnamed parameters", goto error);
732 multi1 = FN(MULTI(BASE),align_params)(multi1,
733 FN(MULTI(BASE),get_space)(multi2));
734 multi2 = FN(MULTI(BASE),align_params)(multi2,
735 FN(MULTI(BASE),get_space)(multi1));
736 return fn(multi1, multi2);
737 error:
738 FN(MULTI(BASE),free)(multi1);
739 FN(MULTI(BASE),free)(multi2);
740 return NULL;
743 /* Given two MULTI(BASE)s A -> B and C -> D,
744 * construct a MULTI(BASE) (A * C) -> [B -> D].
746 * The parameters are assumed to have been aligned.
748 * If "multi1" and/or "multi2" has an explicit domain, then
749 * intersect the domain of the result with these explicit domains.
751 static __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product_aligned)(
752 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
754 int i, n1, n2;
755 EL *el;
756 isl_space *space;
757 MULTI(BASE) *res;
759 if (!multi1 || !multi2)
760 goto error;
762 space = isl_space_range_product(FN(MULTI(BASE),get_space)(multi1),
763 FN(MULTI(BASE),get_space)(multi2));
764 res = FN(MULTI(BASE),alloc)(space);
766 n1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
767 n2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
769 for (i = 0; i < n1; ++i) {
770 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
771 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
774 for (i = 0; i < n2; ++i) {
775 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
776 res = FN(FN(MULTI(BASE),set),BASE)(res, n1 + i, el);
779 if (FN(MULTI(BASE),has_explicit_domain)(multi1))
780 res = FN(MULTI(BASE),intersect_explicit_domain)(res, multi1);
781 if (FN(MULTI(BASE),has_explicit_domain)(multi2))
782 res = FN(MULTI(BASE),intersect_explicit_domain)(res, multi2);
784 FN(MULTI(BASE),free)(multi1);
785 FN(MULTI(BASE),free)(multi2);
786 return res;
787 error:
788 FN(MULTI(BASE),free)(multi1);
789 FN(MULTI(BASE),free)(multi2);
790 return NULL;
793 /* Given two MULTI(BASE)s A -> B and C -> D,
794 * construct a MULTI(BASE) (A * C) -> [B -> D].
796 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product)(
797 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
799 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
800 &FN(MULTI(BASE),range_product_aligned));
803 /* Is the range of "multi" a wrapped relation?
805 isl_bool FN(MULTI(BASE),range_is_wrapping)(__isl_keep MULTI(BASE) *multi)
807 if (!multi)
808 return isl_bool_error;
809 return isl_space_range_is_wrapping(multi->space);
812 /* Given a function A -> [B -> C], extract the function A -> B.
814 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_domain)(
815 __isl_take MULTI(BASE) *multi)
817 isl_space *space;
818 int total, keep;
820 if (!multi)
821 return NULL;
822 if (!isl_space_range_is_wrapping(multi->space))
823 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
824 "range is not a product",
825 return FN(MULTI(BASE),free)(multi));
827 space = FN(MULTI(BASE),get_space)(multi);
828 total = isl_space_dim(space, isl_dim_out);
829 space = isl_space_range_factor_domain(space);
830 keep = isl_space_dim(space, isl_dim_out);
831 multi = FN(MULTI(BASE),drop_dims)(multi,
832 isl_dim_out, keep, total - keep);
833 multi = FN(MULTI(BASE),reset_space)(multi, space);
835 return multi;
838 /* Given a function A -> [B -> C], extract the function A -> C.
840 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_range)(
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_range(space);
856 keep = isl_space_dim(space, isl_dim_out);
857 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_out, 0, total - keep);
858 multi = FN(MULTI(BASE),reset_space)(multi, space);
860 return multi;
863 /* Given a function [B -> C], extract the function C.
865 __isl_give MULTI(BASE) *FN(MULTI(BASE),factor_range)(
866 __isl_take MULTI(BASE) *multi)
868 isl_space *space;
869 int total, keep;
871 if (!multi)
872 return NULL;
873 if (!isl_space_is_wrapping(multi->space))
874 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
875 "not a product", return FN(MULTI(BASE),free)(multi));
877 space = FN(MULTI(BASE),get_space)(multi);
878 total = isl_space_dim(space, isl_dim_out);
879 space = isl_space_factor_range(space);
880 keep = isl_space_dim(space, isl_dim_out);
881 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_out, 0, total - keep);
882 multi = FN(MULTI(BASE),reset_space)(multi, space);
884 return multi;
887 #ifndef NO_PRODUCT
888 /* Given two MULTI(BASE)s A -> B and C -> D,
889 * construct a MULTI(BASE) [A -> C] -> [B -> D].
891 * The parameters are assumed to have been aligned.
893 * If "multi1" and/or "multi2" has an explicit domain, then
894 * intersect the domain of the result with these explicit domains.
896 __isl_give MULTI(BASE) *FN(MULTI(BASE),product_aligned)(
897 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
899 int i;
900 EL *el;
901 isl_space *space;
902 MULTI(BASE) *res;
903 int in1, in2, out1, out2;
905 in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
906 in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
907 out1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
908 out2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
909 space = isl_space_product(FN(MULTI(BASE),get_space)(multi1),
910 FN(MULTI(BASE),get_space)(multi2));
911 res = FN(MULTI(BASE),alloc)(isl_space_copy(space));
912 space = isl_space_domain(space);
914 for (i = 0; i < out1; ++i) {
915 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
916 el = FN(EL,insert_dims)(el, isl_dim_in, in1, in2);
917 el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
918 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
921 for (i = 0; i < out2; ++i) {
922 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
923 el = FN(EL,insert_dims)(el, isl_dim_in, 0, in1);
924 el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
925 res = FN(FN(MULTI(BASE),set),BASE)(res, out1 + i, el);
928 if (FN(MULTI(BASE),has_explicit_domain)(multi1) ||
929 FN(MULTI(BASE),has_explicit_domain)(multi2))
930 res = FN(MULTI(BASE),intersect_explicit_domain_product)(res,
931 multi1, multi2);
933 isl_space_free(space);
934 FN(MULTI(BASE),free)(multi1);
935 FN(MULTI(BASE),free)(multi2);
936 return res;
939 /* Given two MULTI(BASE)s A -> B and C -> D,
940 * construct a MULTI(BASE) [A -> C] -> [B -> D].
942 __isl_give MULTI(BASE) *FN(MULTI(BASE),product)(
943 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
945 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
946 &FN(MULTI(BASE),product_aligned));
948 #endif
950 __isl_give MULTI(BASE) *FN(MULTI(BASE),flatten_range)(
951 __isl_take MULTI(BASE) *multi)
953 if (!multi)
954 return NULL;
956 if (!multi->space->nested[1])
957 return multi;
959 multi = FN(MULTI(BASE),cow)(multi);
960 if (!multi)
961 return NULL;
963 multi->space = isl_space_flatten_range(multi->space);
964 if (!multi->space)
965 return FN(MULTI(BASE),free)(multi);
967 return multi;
970 /* Given two MULTI(BASE)s A -> B and C -> D,
971 * construct a MULTI(BASE) (A * C) -> (B, D).
973 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
974 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
976 MULTI(BASE) *multi;
978 multi = FN(MULTI(BASE),range_product)(multi1, multi2);
979 multi = FN(MULTI(BASE),flatten_range)(multi);
980 return multi;
983 /* Given two multi expressions, "multi1"
985 * [A] -> [B1 B2]
987 * where B2 starts at position "pos", and "multi2"
989 * [A] -> [D]
991 * return the multi expression
993 * [A] -> [B1 D B2]
995 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_splice)(
996 __isl_take MULTI(BASE) *multi1, unsigned pos,
997 __isl_take MULTI(BASE) *multi2)
999 MULTI(BASE) *res;
1000 unsigned dim;
1002 if (!multi1 || !multi2)
1003 goto error;
1005 dim = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
1006 if (pos > dim)
1007 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
1008 "index out of bounds", goto error);
1010 res = FN(MULTI(BASE),copy)(multi1);
1011 res = FN(MULTI(BASE),drop_dims)(res, isl_dim_out, pos, dim - pos);
1012 multi1 = FN(MULTI(BASE),drop_dims)(multi1, isl_dim_out, 0, pos);
1014 res = FN(MULTI(BASE),flat_range_product)(res, multi2);
1015 res = FN(MULTI(BASE),flat_range_product)(res, multi1);
1017 return res;
1018 error:
1019 FN(MULTI(BASE),free)(multi1);
1020 FN(MULTI(BASE),free)(multi2);
1021 return NULL;
1024 #ifndef NO_SPLICE
1025 /* Given two multi expressions, "multi1"
1027 * [A1 A2] -> [B1 B2]
1029 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
1030 * and "multi2"
1032 * [C] -> [D]
1034 * return the multi expression
1036 * [A1 C A2] -> [B1 D B2]
1038 * We first insert input dimensions to obtain
1040 * [A1 C A2] -> [B1 B2]
1042 * and
1044 * [A1 C A2] -> [D]
1046 * and then apply range_splice.
1048 __isl_give MULTI(BASE) *FN(MULTI(BASE),splice)(
1049 __isl_take MULTI(BASE) *multi1, unsigned in_pos, unsigned out_pos,
1050 __isl_take MULTI(BASE) *multi2)
1052 unsigned n_in1;
1053 unsigned n_in2;
1055 if (!multi1 || !multi2)
1056 goto error;
1058 n_in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
1059 if (in_pos > n_in1)
1060 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
1061 "index out of bounds", goto error);
1063 n_in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
1065 multi1 = FN(MULTI(BASE),insert_dims)(multi1, isl_dim_in, in_pos, n_in2);
1066 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, n_in2,
1067 n_in1 - in_pos);
1068 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, 0, in_pos);
1070 return FN(MULTI(BASE),range_splice)(multi1, out_pos, multi2);
1071 error:
1072 FN(MULTI(BASE),free)(multi1);
1073 FN(MULTI(BASE),free)(multi2);
1074 return NULL;
1076 #endif
1078 /* Check that "multi1" and "multi2" live in the same space,
1079 * reporting an error if they do not.
1081 static isl_stat FN(MULTI(BASE),check_equal_space)(
1082 __isl_keep MULTI(BASE) *multi1, __isl_keep MULTI(BASE) *multi2)
1084 isl_bool equal;
1086 if (!multi1 || !multi2)
1087 return isl_stat_error;
1089 equal = isl_space_is_equal(multi1->space, multi2->space);
1090 if (equal < 0)
1091 return isl_stat_error;
1092 if (!equal)
1093 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
1094 "spaces don't match", return isl_stat_error);
1096 return isl_stat_ok;
1099 /* This function is currently only used from isl_aff.c
1101 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
1102 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
1103 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
1104 __attribute__ ((unused));
1106 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
1107 * return the result.
1109 * If "multi2" has an explicit domain, then
1110 * intersect the domain of the result with this explicit domain.
1112 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
1113 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
1114 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
1116 int i;
1118 multi1 = FN(MULTI(BASE),cow)(multi1);
1119 if (FN(MULTI(BASE),check_equal_space)(multi1, multi2) < 0)
1120 goto error;
1122 for (i = 0; i < multi1->n; ++i) {
1123 multi1->u.p[i] = fn(multi1->u.p[i],
1124 FN(EL,copy)(multi2->u.p[i]));
1125 if (!multi1->u.p[i])
1126 goto error;
1129 if (FN(MULTI(BASE),has_explicit_domain)(multi2))
1130 multi1 = FN(MULTI(BASE),intersect_explicit_domain)(multi1,
1131 multi2);
1133 FN(MULTI(BASE),free)(multi2);
1134 return multi1;
1135 error:
1136 FN(MULTI(BASE),free)(multi1);
1137 FN(MULTI(BASE),free)(multi2);
1138 return NULL;
1141 /* Add "multi2" from "multi1" and return the result.
1143 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
1145 static __isl_give MULTI(BASE) *FN(MULTI(BASE),add_aligned)(
1146 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
1148 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,add));
1151 /* Add "multi2" from "multi1" and return the result.
1153 __isl_give MULTI(BASE) *FN(MULTI(BASE),add)(__isl_take MULTI(BASE) *multi1,
1154 __isl_take MULTI(BASE) *multi2)
1156 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
1157 &FN(MULTI(BASE),add_aligned));
1160 /* Subtract "multi2" from "multi1" and return the result.
1162 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
1164 static __isl_give MULTI(BASE) *FN(MULTI(BASE),sub_aligned)(
1165 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
1167 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,sub));
1170 /* Subtract "multi2" from "multi1" and return the result.
1172 __isl_give MULTI(BASE) *FN(MULTI(BASE),sub)(__isl_take MULTI(BASE) *multi1,
1173 __isl_take MULTI(BASE) *multi2)
1175 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
1176 &FN(MULTI(BASE),sub_aligned));
1179 /* Multiply the elements of "multi" by "v" and return the result.
1181 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_val)(__isl_take MULTI(BASE) *multi,
1182 __isl_take isl_val *v)
1184 int i;
1186 if (!multi || !v)
1187 goto error;
1189 if (isl_val_is_one(v)) {
1190 isl_val_free(v);
1191 return multi;
1194 if (!isl_val_is_rat(v))
1195 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1196 "expecting rational factor", goto error);
1198 multi = FN(MULTI(BASE),cow)(multi);
1199 if (!multi)
1200 return NULL;
1202 for (i = 0; i < multi->n; ++i) {
1203 multi->u.p[i] = FN(EL,scale_val)(multi->u.p[i],
1204 isl_val_copy(v));
1205 if (!multi->u.p[i])
1206 goto error;
1209 isl_val_free(v);
1210 return multi;
1211 error:
1212 isl_val_free(v);
1213 return FN(MULTI(BASE),free)(multi);
1216 /* Divide the elements of "multi" by "v" and return the result.
1218 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_val)(
1219 __isl_take MULTI(BASE) *multi, __isl_take isl_val *v)
1221 int i;
1223 if (!multi || !v)
1224 goto error;
1226 if (isl_val_is_one(v)) {
1227 isl_val_free(v);
1228 return multi;
1231 if (!isl_val_is_rat(v))
1232 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1233 "expecting rational factor", goto error);
1234 if (isl_val_is_zero(v))
1235 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1236 "cannot scale down by zero", goto error);
1238 multi = FN(MULTI(BASE),cow)(multi);
1239 if (!multi)
1240 return NULL;
1242 for (i = 0; i < multi->n; ++i) {
1243 multi->u.p[i] = FN(EL,scale_down_val)(multi->u.p[i],
1244 isl_val_copy(v));
1245 if (!multi->u.p[i])
1246 goto error;
1249 isl_val_free(v);
1250 return multi;
1251 error:
1252 isl_val_free(v);
1253 return FN(MULTI(BASE),free)(multi);
1256 /* Multiply the elements of "multi" by the corresponding element of "mv"
1257 * and return the result.
1259 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_multi_val)(
1260 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1262 int i;
1264 if (!multi || !mv)
1265 goto error;
1267 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1268 mv->space, isl_dim_set))
1269 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1270 "spaces don't match", goto error);
1272 multi = FN(MULTI(BASE),cow)(multi);
1273 if (!multi)
1274 goto error;
1276 for (i = 0; i < multi->n; ++i) {
1277 isl_val *v;
1279 v = isl_multi_val_get_val(mv, i);
1280 multi->u.p[i] = FN(EL,scale_val)(multi->u.p[i], v);
1281 if (!multi->u.p[i])
1282 goto error;
1285 isl_multi_val_free(mv);
1286 return multi;
1287 error:
1288 isl_multi_val_free(mv);
1289 return FN(MULTI(BASE),free)(multi);
1292 /* Divide the elements of "multi" by the corresponding element of "mv"
1293 * and return the result.
1295 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_multi_val)(
1296 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1298 int i;
1300 if (!multi || !mv)
1301 goto error;
1303 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1304 mv->space, isl_dim_set))
1305 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1306 "spaces don't match", goto error);
1308 multi = FN(MULTI(BASE),cow)(multi);
1309 if (!multi)
1310 return NULL;
1312 for (i = 0; i < multi->n; ++i) {
1313 isl_val *v;
1315 v = isl_multi_val_get_val(mv, i);
1316 multi->u.p[i] = FN(EL,scale_down_val)(multi->u.p[i], v);
1317 if (!multi->u.p[i])
1318 goto error;
1321 isl_multi_val_free(mv);
1322 return multi;
1323 error:
1324 isl_multi_val_free(mv);
1325 return FN(MULTI(BASE),free)(multi);
1328 /* Compute the residues of the elements of "multi" modulo
1329 * the corresponding element of "mv" and return the result.
1331 __isl_give MULTI(BASE) *FN(MULTI(BASE),mod_multi_val)(
1332 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1334 int i;
1336 if (!multi || !mv)
1337 goto error;
1339 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1340 mv->space, isl_dim_set))
1341 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1342 "spaces don't match", goto error);
1344 multi = FN(MULTI(BASE),cow)(multi);
1345 if (!multi)
1346 goto error;
1348 for (i = 0; i < multi->n; ++i) {
1349 isl_val *v;
1351 v = isl_multi_val_get_val(mv, i);
1352 multi->u.p[i] = FN(EL,mod_val)(multi->u.p[i], v);
1353 if (!multi->u.p[i])
1354 goto error;
1357 isl_multi_val_free(mv);
1358 return multi;
1359 error:
1360 isl_multi_val_free(mv);
1361 return FN(MULTI(BASE),free)(multi);
1364 #ifndef NO_MOVE_DIMS
1365 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1366 * to dimensions of "dst_type" at "dst_pos".
1368 * We only support moving input dimensions to parameters and vice versa.
1370 __isl_give MULTI(BASE) *FN(MULTI(BASE),move_dims)(__isl_take MULTI(BASE) *multi,
1371 enum isl_dim_type dst_type, unsigned dst_pos,
1372 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1374 int i;
1376 if (!multi)
1377 return NULL;
1379 if (n == 0 &&
1380 !isl_space_is_named_or_nested(multi->space, src_type) &&
1381 !isl_space_is_named_or_nested(multi->space, dst_type))
1382 return multi;
1384 if (dst_type == isl_dim_out || src_type == isl_dim_out)
1385 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1386 "cannot move output/set dimension",
1387 return FN(MULTI(BASE),free)(multi));
1388 if (dst_type == isl_dim_div || src_type == isl_dim_div)
1389 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1390 "cannot move divs",
1391 return FN(MULTI(BASE),free)(multi));
1392 if (src_pos + n > isl_space_dim(multi->space, src_type))
1393 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1394 "range out of bounds",
1395 return FN(MULTI(BASE),free)(multi));
1396 if (dst_type == src_type)
1397 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_unsupported,
1398 "moving dims within the same type not supported",
1399 return FN(MULTI(BASE),free)(multi));
1401 multi = FN(MULTI(BASE),cow)(multi);
1402 if (!multi)
1403 return NULL;
1405 multi->space = isl_space_move_dims(multi->space, dst_type, dst_pos,
1406 src_type, src_pos, n);
1407 if (!multi->space)
1408 return FN(MULTI(BASE),free)(multi);
1409 if (FN(MULTI(BASE),has_explicit_domain)(multi))
1410 multi = FN(MULTI(BASE),move_explicit_domain_dims)(multi,
1411 dst_type, dst_pos, src_type, src_pos, n);
1412 if (!multi)
1413 return NULL;
1415 for (i = 0; i < multi->n; ++i) {
1416 multi->u.p[i] = FN(EL,move_dims)(multi->u.p[i],
1417 dst_type, dst_pos,
1418 src_type, src_pos, n);
1419 if (!multi->u.p[i])
1420 return FN(MULTI(BASE),free)(multi);
1423 return multi;
1425 #endif
1427 /* Convert a multiple expression defined over a parameter domain
1428 * into one that is defined over a zero-dimensional set.
1430 __isl_give MULTI(BASE) *FN(MULTI(BASE),from_range)(
1431 __isl_take MULTI(BASE) *multi)
1433 isl_space *space;
1435 if (!multi)
1436 return NULL;
1437 if (!isl_space_is_set(multi->space))
1438 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1439 "not living in a set space",
1440 return FN(MULTI(BASE),free)(multi));
1442 space = FN(MULTI(BASE),get_space)(multi);
1443 space = isl_space_from_range(space);
1444 multi = FN(MULTI(BASE),reset_space)(multi, space);
1446 return multi;
1449 /* Are "multi1" and "multi2" obviously equal?
1451 isl_bool FN(MULTI(BASE),plain_is_equal)(__isl_keep MULTI(BASE) *multi1,
1452 __isl_keep MULTI(BASE) *multi2)
1454 int i;
1455 isl_bool equal;
1457 if (!multi1 || !multi2)
1458 return isl_bool_error;
1459 if (multi1->n != multi2->n)
1460 return isl_bool_false;
1461 equal = isl_space_is_equal(multi1->space, multi2->space);
1462 if (equal < 0 || !equal)
1463 return equal;
1465 for (i = 0; i < multi1->n; ++i) {
1466 equal = FN(EL,plain_is_equal)(multi1->u.p[i], multi2->u.p[i]);
1467 if (equal < 0 || !equal)
1468 return equal;
1471 if (FN(MULTI(BASE),has_explicit_domain)(multi1) ||
1472 FN(MULTI(BASE),has_explicit_domain)(multi2)) {
1473 equal = FN(MULTI(BASE),equal_explicit_domain)(multi1, multi2);
1474 if (equal < 0 || !equal)
1475 return equal;
1478 return isl_bool_true;
1481 /* Does "multi" involve any NaNs?
1483 isl_bool FN(MULTI(BASE),involves_nan)(__isl_keep MULTI(BASE) *multi)
1485 int i;
1487 if (!multi)
1488 return isl_bool_error;
1489 if (multi->n == 0)
1490 return isl_bool_false;
1492 for (i = 0; i < multi->n; ++i) {
1493 isl_bool has_nan = FN(EL,involves_nan)(multi->u.p[i]);
1494 if (has_nan < 0 || has_nan)
1495 return has_nan;
1498 return isl_bool_false;
1501 #ifndef NO_DOMAIN
1502 /* Return the shared domain of the elements of "multi".
1504 * If "multi" has an explicit domain, then return this domain.
1506 __isl_give isl_set *FN(MULTI(BASE),domain)(__isl_take MULTI(BASE) *multi)
1508 int i;
1509 isl_set *dom;
1511 if (!multi)
1512 return NULL;
1514 if (FN(MULTI(BASE),has_explicit_domain)(multi)) {
1515 dom = FN(MULTI(BASE),get_explicit_domain)(multi);
1516 FN(MULTI(BASE),free)(multi);
1517 return dom;
1520 dom = isl_set_universe(FN(MULTI(BASE),get_domain_space)(multi));
1521 for (i = 0; i < multi->n; ++i) {
1522 isl_set *dom_i;
1524 dom_i = FN(EL,domain)(FN(FN(MULTI(BASE),get),BASE)(multi, i));
1525 dom = isl_set_intersect(dom, dom_i);
1528 FN(MULTI(BASE),free)(multi);
1529 return dom;
1531 #endif
1533 #ifndef NO_NEG
1534 /* Return the opposite of "multi".
1536 __isl_give MULTI(BASE) *FN(MULTI(BASE),neg)(__isl_take MULTI(BASE) *multi)
1538 int i;
1540 multi = FN(MULTI(BASE),cow)(multi);
1541 if (!multi)
1542 return NULL;
1544 for (i = 0; i < multi->n; ++i) {
1545 multi->u.p[i] = FN(EL,neg)(multi->u.p[i]);
1546 if (!multi->u.p[i])
1547 return FN(MULTI(BASE),free)(multi);
1550 return multi;
1552 #endif