isl_map_simplify.c: div_is_redundant: use isl_basic_map_offset
[isl.git] / isl_multi_templ.c
blob4f70c902e2f4da2fbb341282460f5ccadb6b6a79
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 /* Return the space of "multi".
30 __isl_keep isl_space *FN(MULTI(BASE),peek_space)(__isl_keep MULTI(BASE) *multi)
32 return multi ? multi->space : NULL;
35 __isl_give isl_space *FN(MULTI(BASE),get_space)(__isl_keep MULTI(BASE) *multi)
37 return isl_space_copy(FN(MULTI(BASE),peek_space)(multi));
40 /* Return the position of the dimension of the given type and name
41 * in "multi".
42 * Return -1 if no such dimension can be found.
44 int FN(MULTI(BASE),find_dim_by_name)(__isl_keep MULTI(BASE) *multi,
45 enum isl_dim_type type, const char *name)
47 if (!multi)
48 return -1;
49 return isl_space_find_dim_by_name(multi->space, type, name);
52 __isl_give isl_space *FN(MULTI(BASE),get_domain_space)(
53 __isl_keep MULTI(BASE) *multi)
55 return multi ? isl_space_domain(isl_space_copy(multi->space)) : NULL;
58 /* Allocate a multi expression living in "space".
60 * If the number of base expressions is zero, then make sure
61 * there is enough room in the structure for the explicit domain,
62 * in case the type supports such an explicit domain.
64 __isl_give MULTI(BASE) *FN(MULTI(BASE),alloc)(__isl_take isl_space *space)
66 isl_ctx *ctx;
67 int n;
68 MULTI(BASE) *multi;
70 if (!space)
71 return NULL;
73 ctx = isl_space_get_ctx(space);
74 n = isl_space_dim(space, isl_dim_out);
75 if (n > 0)
76 multi = isl_calloc(ctx, MULTI(BASE),
77 sizeof(MULTI(BASE)) + (n - 1) * sizeof(struct EL *));
78 else
79 multi = isl_calloc(ctx, MULTI(BASE), sizeof(MULTI(BASE)));
80 if (!multi)
81 goto error;
83 multi->space = space;
84 multi->n = n;
85 multi->ref = 1;
86 if (FN(MULTI(BASE),has_explicit_domain)(multi))
87 multi = FN(MULTI(BASE),init_explicit_domain)(multi);
88 return multi;
89 error:
90 isl_space_free(space);
91 return NULL;
94 __isl_give MULTI(BASE) *FN(MULTI(BASE),dup)(__isl_keep MULTI(BASE) *multi)
96 int i;
97 MULTI(BASE) *dup;
99 if (!multi)
100 return NULL;
102 dup = FN(MULTI(BASE),alloc)(isl_space_copy(multi->space));
103 if (!dup)
104 return NULL;
106 for (i = 0; i < multi->n; ++i)
107 dup = FN(FN(MULTI(BASE),set),BASE)(dup, i,
108 FN(EL,copy)(multi->u.p[i]));
109 if (FN(MULTI(BASE),has_explicit_domain)(multi))
110 dup = FN(MULTI(BASE),copy_explicit_domain)(dup, multi);
112 return dup;
115 __isl_give MULTI(BASE) *FN(MULTI(BASE),cow)(__isl_take MULTI(BASE) *multi)
117 if (!multi)
118 return NULL;
120 if (multi->ref == 1)
121 return multi;
123 multi->ref--;
124 return FN(MULTI(BASE),dup)(multi);
127 __isl_give MULTI(BASE) *FN(MULTI(BASE),copy)(__isl_keep MULTI(BASE) *multi)
129 if (!multi)
130 return NULL;
132 multi->ref++;
133 return multi;
136 __isl_null MULTI(BASE) *FN(MULTI(BASE),free)(__isl_take MULTI(BASE) *multi)
138 int i;
140 if (!multi)
141 return NULL;
143 if (--multi->ref > 0)
144 return NULL;
146 isl_space_free(multi->space);
147 for (i = 0; i < multi->n; ++i)
148 FN(EL,free)(multi->u.p[i]);
149 if (FN(MULTI(BASE),has_explicit_domain)(multi))
150 FN(MULTI(BASE),free_explicit_domain)(multi);
151 free(multi);
153 return NULL;
156 unsigned FN(MULTI(BASE),dim)(__isl_keep MULTI(BASE) *multi,
157 enum isl_dim_type type)
159 return multi ? isl_space_dim(multi->space, type) : 0;
162 /* Return the position of the first dimension of "type" with id "id".
163 * Return -1 if there is no such dimension.
165 int FN(MULTI(BASE),find_dim_by_id)(__isl_keep MULTI(BASE) *multi,
166 enum isl_dim_type type, __isl_keep isl_id *id)
168 if (!multi)
169 return -1;
170 return isl_space_find_dim_by_id(multi->space, type, id);
173 /* Return the id of the given dimension.
175 __isl_give isl_id *FN(MULTI(BASE),get_dim_id)(__isl_keep MULTI(BASE) *multi,
176 enum isl_dim_type type, unsigned pos)
178 return multi ? isl_space_get_dim_id(multi->space, type, pos) : NULL;
181 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_name)(
182 __isl_take MULTI(BASE) *multi,
183 enum isl_dim_type type, unsigned pos, const char *s)
185 int i;
187 multi = FN(MULTI(BASE),cow)(multi);
188 if (!multi)
189 return NULL;
191 multi->space = isl_space_set_dim_name(multi->space, type, pos, s);
192 if (!multi->space)
193 return FN(MULTI(BASE),free)(multi);
195 if (type == isl_dim_out)
196 return multi;
197 for (i = 0; i < multi->n; ++i) {
198 multi->u.p[i] = FN(EL,set_dim_name)(multi->u.p[i],
199 type, pos, s);
200 if (!multi->u.p[i])
201 return FN(MULTI(BASE),free)(multi);
204 return multi;
207 const char *FN(MULTI(BASE),get_tuple_name)(__isl_keep MULTI(BASE) *multi,
208 enum isl_dim_type type)
210 return multi ? isl_space_get_tuple_name(multi->space, type) : NULL;
213 /* Does the specified tuple have an id?
215 isl_bool FN(MULTI(BASE),has_tuple_id)(__isl_keep MULTI(BASE) *multi,
216 enum isl_dim_type type)
218 if (!multi)
219 return isl_bool_error;
220 return isl_space_has_tuple_id(multi->space, type);
223 /* Return the id of the specified tuple.
225 __isl_give isl_id *FN(MULTI(BASE),get_tuple_id)(__isl_keep MULTI(BASE) *multi,
226 enum isl_dim_type type)
228 return multi ? isl_space_get_tuple_id(multi->space, type) : NULL;
231 #undef TYPE
232 #define TYPE MULTI(BASE)
233 static
234 #include "check_type_range_templ.c"
236 __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
237 int pos)
239 isl_ctx *ctx;
241 if (FN(MULTI(BASE),check_range)(multi, isl_dim_out, pos, 1) < 0)
242 return NULL;
243 ctx = FN(MULTI(BASE),get_ctx)(multi);
244 return FN(EL,copy)(multi->u.p[pos]);
247 /* Set the element at position "pos" of "multi" to "el",
248 * where the position may be empty if "multi" has only a single reference.
250 static __isl_give MULTI(BASE) *FN(MULTI(BASE),restore)(
251 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
253 multi = FN(MULTI(BASE),cow)(multi);
254 if (!multi || !el)
255 goto error;
257 if (FN(MULTI(BASE),check_range)(multi, isl_dim_out, pos, 1) < 0)
258 goto error;
260 FN(EL,free)(multi->u.p[pos]);
261 multi->u.p[pos] = el;
263 return multi;
264 error:
265 FN(MULTI(BASE),free)(multi);
266 FN(EL,free)(el);
267 return NULL;
270 /* Set the element at position "pos" of "multi" to "el",
271 * where the position may be empty if "multi" has only a single reference.
272 * However, the space of "multi" is available and is checked
273 * for compatibility with "el".
275 static __isl_give MULTI(BASE) *FN(MULTI(BASE),restore_check_space)(
276 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
278 isl_space *space;
280 space = FN(MULTI(BASE),peek_space)(multi);
281 if (FN(EL,check_match_domain_space)(el, space) < 0)
282 multi = FN(MULTI(BASE),free)(multi);
283 return FN(MULTI(BASE),restore)(multi, pos, el);
286 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
287 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
289 isl_space *multi_space = NULL;
290 isl_space *el_space = NULL;
291 isl_bool match;
293 multi_space = FN(MULTI(BASE),get_space)(multi);
294 match = FN(EL,matching_params)(el, multi_space);
295 if (match < 0)
296 goto error;
297 if (!match) {
298 multi = FN(MULTI(BASE),align_params)(multi,
299 FN(EL,get_space)(el));
300 isl_space_free(multi_space);
301 multi_space = FN(MULTI(BASE),get_space)(multi);
302 el = FN(EL,align_params)(el, isl_space_copy(multi_space));
305 multi = FN(MULTI(BASE),restore_check_space)(multi, pos, el);
307 isl_space_free(multi_space);
308 isl_space_free(el_space);
310 return multi;
311 error:
312 FN(MULTI(BASE),free)(multi);
313 FN(EL,free)(el);
314 isl_space_free(multi_space);
315 isl_space_free(el_space);
316 return NULL;
319 /* Reset the space of "multi". This function is called from isl_pw_templ.c
320 * and doesn't know if the space of an element object is represented
321 * directly or through its domain. It therefore passes along both,
322 * which we pass along to the element function since we don't know how
323 * that is represented either.
325 * If "multi" has an explicit domain, then the caller is expected
326 * to make sure that any modification that would change the dimensions
327 * of the explicit domain has bee applied before this function is called.
329 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
330 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
331 __isl_take isl_space *domain)
333 int i;
335 multi = FN(MULTI(BASE),cow)(multi);
336 if (!multi || !space || !domain)
337 goto error;
339 for (i = 0; i < multi->n; ++i) {
340 multi->u.p[i] = FN(EL,reset_domain_space)(multi->u.p[i],
341 isl_space_copy(domain));
342 if (!multi->u.p[i])
343 goto error;
345 if (FN(MULTI(BASE),has_explicit_domain)(multi)) {
346 multi = FN(MULTI(BASE),reset_explicit_domain_space)(multi,
347 isl_space_copy(domain));
348 if (!multi)
349 goto error;
351 isl_space_free(domain);
352 isl_space_free(multi->space);
353 multi->space = space;
355 return multi;
356 error:
357 isl_space_free(domain);
358 isl_space_free(space);
359 FN(MULTI(BASE),free)(multi);
360 return NULL;
363 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
364 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
366 isl_space *space;
368 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
369 isl_space_copy(multi->space));
370 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
373 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
374 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
376 isl_space *domain;
378 domain = isl_space_domain(isl_space_copy(space));
379 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
382 /* Set the id of the given dimension of "multi" to "id".
384 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_id)(
385 __isl_take MULTI(BASE) *multi,
386 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
388 isl_space *space;
390 multi = FN(MULTI(BASE),cow)(multi);
391 if (!multi || !id)
392 goto error;
394 space = FN(MULTI(BASE),get_space)(multi);
395 space = isl_space_set_dim_id(space, type, pos, id);
397 return FN(MULTI(BASE),reset_space)(multi, space);
398 error:
399 isl_id_free(id);
400 FN(MULTI(BASE),free)(multi);
401 return NULL;
404 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_name)(
405 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
406 const char *s)
408 isl_space *space;
410 multi = FN(MULTI(BASE),cow)(multi);
411 if (!multi)
412 return NULL;
414 space = FN(MULTI(BASE),get_space)(multi);
415 space = isl_space_set_tuple_name(space, type, s);
417 return FN(MULTI(BASE),reset_space)(multi, space);
420 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_id)(
421 __isl_take MULTI(BASE) *multi, enum isl_dim_type type,
422 __isl_take isl_id *id)
424 isl_space *space;
426 multi = FN(MULTI(BASE),cow)(multi);
427 if (!multi)
428 goto error;
430 space = FN(MULTI(BASE),get_space)(multi);
431 space = isl_space_set_tuple_id(space, type, id);
433 return FN(MULTI(BASE),reset_space)(multi, space);
434 error:
435 isl_id_free(id);
436 return NULL;
439 /* Drop the id on the specified tuple.
441 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_tuple_id)(
442 __isl_take MULTI(BASE) *multi, enum isl_dim_type type)
444 isl_space *space;
446 if (!multi)
447 return NULL;
448 if (!FN(MULTI(BASE),has_tuple_id)(multi, type))
449 return multi;
451 multi = FN(MULTI(BASE),cow)(multi);
452 if (!multi)
453 return NULL;
455 space = FN(MULTI(BASE),get_space)(multi);
456 space = isl_space_reset_tuple_id(space, type);
458 return FN(MULTI(BASE),reset_space)(multi, space);
461 /* Reset the user pointer on all identifiers of parameters and tuples
462 * of the space of "multi".
464 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_user)(
465 __isl_take MULTI(BASE) *multi)
467 isl_space *space;
469 space = FN(MULTI(BASE),get_space)(multi);
470 space = isl_space_reset_user(space);
472 return FN(MULTI(BASE),reset_space)(multi, space);
475 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
476 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
478 int i;
479 isl_space *space;
481 multi = FN(MULTI(BASE),cow)(multi);
482 if (!multi || !exp)
483 goto error;
485 for (i = 0; i < multi->n; ++i) {
486 multi->u.p[i] = FN(EL,realign_domain)(multi->u.p[i],
487 isl_reordering_copy(exp));
488 if (!multi->u.p[i])
489 goto error;
492 space = isl_reordering_get_space(exp);
493 multi = FN(MULTI(BASE),reset_domain_space)(multi, space);
495 isl_reordering_free(exp);
496 return multi;
497 error:
498 isl_reordering_free(exp);
499 FN(MULTI(BASE),free)(multi);
500 return NULL;
503 /* Align the parameters of "multi" to those of "model".
505 * If "multi" has an explicit domain, then align the parameters
506 * of the domain first.
508 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
509 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
511 isl_ctx *ctx;
512 isl_bool equal_params;
513 isl_reordering *exp;
515 if (!multi || !model)
516 goto error;
518 equal_params = isl_space_has_equal_params(multi->space, model);
519 if (equal_params < 0)
520 goto error;
521 if (equal_params) {
522 isl_space_free(model);
523 return multi;
526 ctx = isl_space_get_ctx(model);
527 if (!isl_space_has_named_params(model))
528 isl_die(ctx, isl_error_invalid,
529 "model has unnamed parameters", goto error);
530 if (!isl_space_has_named_params(multi->space))
531 isl_die(ctx, isl_error_invalid,
532 "input has unnamed parameters", goto error);
534 if (FN(MULTI(BASE),has_explicit_domain)(multi)) {
535 multi = FN(MULTI(BASE),align_explicit_domain_params)(multi,
536 isl_space_copy(model));
537 if (!multi)
538 goto error;
540 exp = isl_parameter_alignment_reordering(multi->space, model);
541 exp = isl_reordering_extend_space(exp,
542 FN(MULTI(BASE),get_domain_space)(multi));
543 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
545 isl_space_free(model);
546 return multi;
547 error:
548 isl_space_free(model);
549 FN(MULTI(BASE),free)(multi);
550 return NULL;
553 /* Create a multi expression in the given space with the elements of "list"
554 * as base expressions.
556 * Since isl_multi_*_restore_* assumes that the element and
557 * the multi expression have matching spaces, the alignment
558 * (if any) needs to be performed beforehand.
560 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
561 __isl_take isl_space *space, __isl_take LIST(EL) *list)
563 int i;
564 int n;
565 isl_ctx *ctx;
566 MULTI(BASE) *multi;
568 if (!space || !list)
569 goto error;
571 ctx = isl_space_get_ctx(space);
572 n = FN(FN(LIST(EL),n),BASE)(list);
573 if (n != isl_space_dim(space, isl_dim_out))
574 isl_die(ctx, isl_error_invalid,
575 "invalid number of elements in list", goto error);
577 for (i = 0; i < n; ++i) {
578 EL *el = FN(LIST(EL),peek)(list, i);
579 space = isl_space_align_params(space, FN(EL,get_space)(el));
581 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
582 for (i = 0; i < n; ++i) {
583 EL *el = FN(FN(LIST(EL),get),BASE)(list, i);
584 el = FN(EL,align_params)(el, isl_space_copy(space));
585 multi = FN(MULTI(BASE),restore_check_space)(multi, i, el);
588 isl_space_free(space);
589 FN(LIST(EL),free)(list);
590 return multi;
591 error:
592 isl_space_free(space);
593 FN(LIST(EL),free)(list);
594 return NULL;
597 __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
598 __isl_take MULTI(BASE) *multi,
599 enum isl_dim_type type, unsigned first, unsigned n)
601 int i;
603 multi = FN(MULTI(BASE),cow)(multi);
604 if (FN(MULTI(BASE),check_range)(multi, type, first, n) < 0)
605 return FN(MULTI(BASE),free)(multi);
607 multi->space = isl_space_drop_dims(multi->space, type, first, n);
608 if (!multi->space)
609 return FN(MULTI(BASE),free)(multi);
611 if (type == isl_dim_out) {
612 for (i = 0; i < n; ++i)
613 FN(EL,free)(multi->u.p[first + i]);
614 for (i = first; i + n < multi->n; ++i)
615 multi->u.p[i] = multi->u.p[i + n];
616 multi->n -= n;
617 if (n > 0 && FN(MULTI(BASE),has_explicit_domain)(multi))
618 multi = FN(MULTI(BASE),init_explicit_domain)(multi);
620 return multi;
623 if (FN(MULTI(BASE),has_explicit_domain)(multi))
624 multi = FN(MULTI(BASE),drop_explicit_domain_dims)(multi,
625 type, first, n);
626 if (!multi)
627 return NULL;
629 for (i = 0; i < multi->n; ++i) {
630 multi->u.p[i] = FN(EL,drop_dims)(multi->u.p[i], type, first, n);
631 if (!multi->u.p[i])
632 return FN(MULTI(BASE),free)(multi);
635 return multi;
638 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
640 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_multi_and)(
641 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
642 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi1,
643 __isl_take MULTI(BASE) *multi2))
645 isl_ctx *ctx;
646 isl_bool equal_params;
648 if (!multi1 || !multi2)
649 goto error;
650 equal_params = isl_space_has_equal_params(multi1->space, multi2->space);
651 if (equal_params < 0)
652 goto error;
653 if (equal_params)
654 return fn(multi1, multi2);
655 ctx = FN(MULTI(BASE),get_ctx)(multi1);
656 if (!isl_space_has_named_params(multi1->space) ||
657 !isl_space_has_named_params(multi2->space))
658 isl_die(ctx, isl_error_invalid,
659 "unaligned unnamed parameters", goto error);
660 multi1 = FN(MULTI(BASE),align_params)(multi1,
661 FN(MULTI(BASE),get_space)(multi2));
662 multi2 = FN(MULTI(BASE),align_params)(multi2,
663 FN(MULTI(BASE),get_space)(multi1));
664 return fn(multi1, multi2);
665 error:
666 FN(MULTI(BASE),free)(multi1);
667 FN(MULTI(BASE),free)(multi2);
668 return NULL;
671 /* Given two MULTI(BASE)s A -> B and C -> D,
672 * construct a MULTI(BASE) (A * C) -> [B -> D].
674 * The parameters are assumed to have been aligned.
676 * If "multi1" and/or "multi2" has an explicit domain, then
677 * intersect the domain of the result with these explicit domains.
679 static __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product_aligned)(
680 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
682 int i, n1, n2;
683 EL *el;
684 isl_space *space;
685 MULTI(BASE) *res;
687 if (!multi1 || !multi2)
688 goto error;
690 space = isl_space_range_product(FN(MULTI(BASE),get_space)(multi1),
691 FN(MULTI(BASE),get_space)(multi2));
692 res = FN(MULTI(BASE),alloc)(space);
694 n1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
695 n2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
697 for (i = 0; i < n1; ++i) {
698 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
699 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
702 for (i = 0; i < n2; ++i) {
703 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
704 res = FN(FN(MULTI(BASE),set),BASE)(res, n1 + i, el);
707 if (FN(MULTI(BASE),has_explicit_domain)(multi1))
708 res = FN(MULTI(BASE),intersect_explicit_domain)(res, multi1);
709 if (FN(MULTI(BASE),has_explicit_domain)(multi2))
710 res = FN(MULTI(BASE),intersect_explicit_domain)(res, multi2);
712 FN(MULTI(BASE),free)(multi1);
713 FN(MULTI(BASE),free)(multi2);
714 return res;
715 error:
716 FN(MULTI(BASE),free)(multi1);
717 FN(MULTI(BASE),free)(multi2);
718 return NULL;
721 /* Given two MULTI(BASE)s A -> B and C -> D,
722 * construct a MULTI(BASE) (A * C) -> [B -> D].
724 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product)(
725 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
727 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
728 &FN(MULTI(BASE),range_product_aligned));
731 /* Is the range of "multi" a wrapped relation?
733 isl_bool FN(MULTI(BASE),range_is_wrapping)(__isl_keep MULTI(BASE) *multi)
735 if (!multi)
736 return isl_bool_error;
737 return isl_space_range_is_wrapping(multi->space);
740 /* Given a function A -> [B -> C], extract the function A -> B.
742 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_domain)(
743 __isl_take MULTI(BASE) *multi)
745 isl_space *space;
746 int total, keep;
748 if (!multi)
749 return NULL;
750 if (!isl_space_range_is_wrapping(multi->space))
751 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
752 "range is not a product",
753 return FN(MULTI(BASE),free)(multi));
755 space = FN(MULTI(BASE),get_space)(multi);
756 total = isl_space_dim(space, isl_dim_out);
757 space = isl_space_range_factor_domain(space);
758 keep = isl_space_dim(space, isl_dim_out);
759 multi = FN(MULTI(BASE),drop_dims)(multi,
760 isl_dim_out, keep, total - keep);
761 multi = FN(MULTI(BASE),reset_space)(multi, space);
763 return multi;
766 /* Given a function A -> [B -> C], extract the function A -> C.
768 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_range)(
769 __isl_take MULTI(BASE) *multi)
771 isl_space *space;
772 int total, keep;
774 if (!multi)
775 return NULL;
776 if (!isl_space_range_is_wrapping(multi->space))
777 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
778 "range is not a product",
779 return FN(MULTI(BASE),free)(multi));
781 space = FN(MULTI(BASE),get_space)(multi);
782 total = isl_space_dim(space, isl_dim_out);
783 space = isl_space_range_factor_range(space);
784 keep = isl_space_dim(space, isl_dim_out);
785 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_out, 0, total - keep);
786 multi = FN(MULTI(BASE),reset_space)(multi, space);
788 return multi;
791 /* Given a function [B -> C], extract the function C.
793 __isl_give MULTI(BASE) *FN(MULTI(BASE),factor_range)(
794 __isl_take MULTI(BASE) *multi)
796 isl_space *space;
797 int total, keep;
799 if (!multi)
800 return NULL;
801 if (!isl_space_is_wrapping(multi->space))
802 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
803 "not a product", return FN(MULTI(BASE),free)(multi));
805 space = FN(MULTI(BASE),get_space)(multi);
806 total = isl_space_dim(space, isl_dim_set);
807 space = isl_space_factor_range(space);
808 keep = isl_space_dim(space, isl_dim_set);
809 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_set, 0, total - keep);
810 multi = FN(MULTI(BASE),reset_space)(multi, space);
812 return multi;
815 __isl_give MULTI(BASE) *FN(MULTI(BASE),flatten_range)(
816 __isl_take MULTI(BASE) *multi)
818 if (!multi)
819 return NULL;
821 if (!multi->space->nested[1])
822 return multi;
824 multi = FN(MULTI(BASE),cow)(multi);
825 if (!multi)
826 return NULL;
828 multi->space = isl_space_flatten_range(multi->space);
829 if (!multi->space)
830 return FN(MULTI(BASE),free)(multi);
832 return multi;
835 /* Given two MULTI(BASE)s A -> B and C -> D,
836 * construct a MULTI(BASE) (A * C) -> (B, D).
838 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
839 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
841 MULTI(BASE) *multi;
843 multi = FN(MULTI(BASE),range_product)(multi1, multi2);
844 multi = FN(MULTI(BASE),flatten_range)(multi);
845 return multi;
848 /* Given two multi expressions, "multi1"
850 * [A] -> [B1 B2]
852 * where B2 starts at position "pos", and "multi2"
854 * [A] -> [D]
856 * return the multi expression
858 * [A] -> [B1 D B2]
860 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_splice)(
861 __isl_take MULTI(BASE) *multi1, unsigned pos,
862 __isl_take MULTI(BASE) *multi2)
864 MULTI(BASE) *res;
865 unsigned dim;
867 if (!multi1 || !multi2)
868 goto error;
870 if (FN(MULTI(BASE),check_range)(multi1, isl_dim_out, pos, 0) < 0)
871 goto error;
872 dim = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
874 res = FN(MULTI(BASE),copy)(multi1);
875 res = FN(MULTI(BASE),drop_dims)(res, isl_dim_out, pos, dim - pos);
876 multi1 = FN(MULTI(BASE),drop_dims)(multi1, isl_dim_out, 0, pos);
878 res = FN(MULTI(BASE),flat_range_product)(res, multi2);
879 res = FN(MULTI(BASE),flat_range_product)(res, multi1);
881 return res;
882 error:
883 FN(MULTI(BASE),free)(multi1);
884 FN(MULTI(BASE),free)(multi2);
885 return NULL;
888 /* Check that "multi1" and "multi2" live in the same space,
889 * reporting an error if they do not.
891 static isl_stat FN(MULTI(BASE),check_equal_space)(
892 __isl_keep MULTI(BASE) *multi1, __isl_keep MULTI(BASE) *multi2)
894 isl_bool equal;
896 if (!multi1 || !multi2)
897 return isl_stat_error;
899 equal = isl_space_is_equal(multi1->space, multi2->space);
900 if (equal < 0)
901 return isl_stat_error;
902 if (!equal)
903 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
904 "spaces don't match", return isl_stat_error);
906 return isl_stat_ok;
909 /* This function is currently only used from isl_aff.c
911 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
912 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
913 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
914 __attribute__ ((unused));
916 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
917 * return the result.
919 * If "multi2" has an explicit domain, then
920 * intersect the domain of the result with this explicit domain.
922 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
923 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
924 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
926 int i;
928 multi1 = FN(MULTI(BASE),cow)(multi1);
929 if (FN(MULTI(BASE),check_equal_space)(multi1, multi2) < 0)
930 goto error;
932 for (i = 0; i < multi1->n; ++i) {
933 multi1->u.p[i] = fn(multi1->u.p[i],
934 FN(EL,copy)(multi2->u.p[i]));
935 if (!multi1->u.p[i])
936 goto error;
939 if (FN(MULTI(BASE),has_explicit_domain)(multi2))
940 multi1 = FN(MULTI(BASE),intersect_explicit_domain)(multi1,
941 multi2);
943 FN(MULTI(BASE),free)(multi2);
944 return multi1;
945 error:
946 FN(MULTI(BASE),free)(multi1);
947 FN(MULTI(BASE),free)(multi2);
948 return NULL;
951 /* Add "multi2" to "multi1" and return the result.
953 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
955 static __isl_give MULTI(BASE) *FN(MULTI(BASE),add_aligned)(
956 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
958 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,add));
961 /* Add "multi2" to "multi1" and return the result.
963 __isl_give MULTI(BASE) *FN(MULTI(BASE),add)(__isl_take MULTI(BASE) *multi1,
964 __isl_take MULTI(BASE) *multi2)
966 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
967 &FN(MULTI(BASE),add_aligned));
970 /* Subtract "multi2" from "multi1" and return the result.
972 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
974 static __isl_give MULTI(BASE) *FN(MULTI(BASE),sub_aligned)(
975 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
977 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,sub));
980 /* Subtract "multi2" from "multi1" and return the result.
982 __isl_give MULTI(BASE) *FN(MULTI(BASE),sub)(__isl_take MULTI(BASE) *multi1,
983 __isl_take MULTI(BASE) *multi2)
985 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
986 &FN(MULTI(BASE),sub_aligned));
989 /* Multiply the elements of "multi" by "v" and return the result.
991 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_val)(__isl_take MULTI(BASE) *multi,
992 __isl_take isl_val *v)
994 int i;
996 if (!multi || !v)
997 goto error;
999 if (isl_val_is_one(v)) {
1000 isl_val_free(v);
1001 return multi;
1004 if (!isl_val_is_rat(v))
1005 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1006 "expecting rational factor", goto error);
1008 multi = FN(MULTI(BASE),cow)(multi);
1009 if (!multi)
1010 return NULL;
1012 for (i = 0; i < multi->n; ++i) {
1013 multi->u.p[i] = FN(EL,scale_val)(multi->u.p[i],
1014 isl_val_copy(v));
1015 if (!multi->u.p[i])
1016 goto error;
1019 isl_val_free(v);
1020 return multi;
1021 error:
1022 isl_val_free(v);
1023 return FN(MULTI(BASE),free)(multi);
1026 /* Divide the elements of "multi" by "v" and return the result.
1028 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_val)(
1029 __isl_take MULTI(BASE) *multi, __isl_take isl_val *v)
1031 int i;
1033 if (!multi || !v)
1034 goto error;
1036 if (isl_val_is_one(v)) {
1037 isl_val_free(v);
1038 return multi;
1041 if (!isl_val_is_rat(v))
1042 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1043 "expecting rational factor", goto error);
1044 if (isl_val_is_zero(v))
1045 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1046 "cannot scale down by zero", goto error);
1048 multi = FN(MULTI(BASE),cow)(multi);
1049 if (!multi)
1050 return NULL;
1052 for (i = 0; i < multi->n; ++i) {
1053 multi->u.p[i] = FN(EL,scale_down_val)(multi->u.p[i],
1054 isl_val_copy(v));
1055 if (!multi->u.p[i])
1056 goto error;
1059 isl_val_free(v);
1060 return multi;
1061 error:
1062 isl_val_free(v);
1063 return FN(MULTI(BASE),free)(multi);
1066 /* Multiply the elements of "multi" by the corresponding element of "mv"
1067 * and return the result.
1069 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_multi_val)(
1070 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1072 int i;
1074 if (!multi || !mv)
1075 goto error;
1077 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1078 mv->space, isl_dim_set))
1079 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1080 "spaces don't match", goto error);
1082 multi = FN(MULTI(BASE),cow)(multi);
1083 if (!multi)
1084 goto error;
1086 for (i = 0; i < multi->n; ++i) {
1087 isl_val *v;
1089 v = isl_multi_val_get_val(mv, i);
1090 multi->u.p[i] = FN(EL,scale_val)(multi->u.p[i], v);
1091 if (!multi->u.p[i])
1092 goto error;
1095 isl_multi_val_free(mv);
1096 return multi;
1097 error:
1098 isl_multi_val_free(mv);
1099 return FN(MULTI(BASE),free)(multi);
1102 /* Divide the elements of "multi" by the corresponding element of "mv"
1103 * and return the result.
1105 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_multi_val)(
1106 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1108 int i;
1110 if (!multi || !mv)
1111 goto error;
1113 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1114 mv->space, isl_dim_set))
1115 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1116 "spaces don't match", goto error);
1118 multi = FN(MULTI(BASE),cow)(multi);
1119 if (!multi)
1120 return NULL;
1122 for (i = 0; i < multi->n; ++i) {
1123 isl_val *v;
1125 v = isl_multi_val_get_val(mv, i);
1126 multi->u.p[i] = FN(EL,scale_down_val)(multi->u.p[i], v);
1127 if (!multi->u.p[i])
1128 goto error;
1131 isl_multi_val_free(mv);
1132 return multi;
1133 error:
1134 isl_multi_val_free(mv);
1135 return FN(MULTI(BASE),free)(multi);
1138 /* Compute the residues of the elements of "multi" modulo
1139 * the corresponding element of "mv" and return the result.
1141 __isl_give MULTI(BASE) *FN(MULTI(BASE),mod_multi_val)(
1142 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1144 int i;
1146 if (!multi || !mv)
1147 goto error;
1149 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1150 mv->space, isl_dim_set))
1151 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1152 "spaces don't match", goto error);
1154 multi = FN(MULTI(BASE),cow)(multi);
1155 if (!multi)
1156 goto error;
1158 for (i = 0; i < multi->n; ++i) {
1159 isl_val *v;
1161 v = isl_multi_val_get_val(mv, i);
1162 multi->u.p[i] = FN(EL,mod_val)(multi->u.p[i], v);
1163 if (!multi->u.p[i])
1164 goto error;
1167 isl_multi_val_free(mv);
1168 return multi;
1169 error:
1170 isl_multi_val_free(mv);
1171 return FN(MULTI(BASE),free)(multi);
1174 /* Convert a multiple expression defined over a parameter domain
1175 * into one that is defined over a zero-dimensional set.
1177 __isl_give MULTI(BASE) *FN(MULTI(BASE),from_range)(
1178 __isl_take MULTI(BASE) *multi)
1180 isl_space *space;
1182 if (!multi)
1183 return NULL;
1184 if (!isl_space_is_set(multi->space))
1185 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1186 "not living in a set space",
1187 return FN(MULTI(BASE),free)(multi));
1189 space = FN(MULTI(BASE),get_space)(multi);
1190 space = isl_space_from_range(space);
1191 multi = FN(MULTI(BASE),reset_space)(multi, space);
1193 return multi;
1196 /* Are "multi1" and "multi2" obviously equal?
1198 isl_bool FN(MULTI(BASE),plain_is_equal)(__isl_keep MULTI(BASE) *multi1,
1199 __isl_keep MULTI(BASE) *multi2)
1201 int i;
1202 isl_bool equal;
1204 if (!multi1 || !multi2)
1205 return isl_bool_error;
1206 if (multi1->n != multi2->n)
1207 return isl_bool_false;
1208 equal = isl_space_is_equal(multi1->space, multi2->space);
1209 if (equal < 0 || !equal)
1210 return equal;
1212 for (i = 0; i < multi1->n; ++i) {
1213 equal = FN(EL,plain_is_equal)(multi1->u.p[i], multi2->u.p[i]);
1214 if (equal < 0 || !equal)
1215 return equal;
1218 if (FN(MULTI(BASE),has_explicit_domain)(multi1) ||
1219 FN(MULTI(BASE),has_explicit_domain)(multi2)) {
1220 equal = FN(MULTI(BASE),equal_explicit_domain)(multi1, multi2);
1221 if (equal < 0 || !equal)
1222 return equal;
1225 return isl_bool_true;
1228 /* Does "multi" involve any NaNs?
1230 isl_bool FN(MULTI(BASE),involves_nan)(__isl_keep MULTI(BASE) *multi)
1232 int i;
1234 if (!multi)
1235 return isl_bool_error;
1236 if (multi->n == 0)
1237 return isl_bool_false;
1239 for (i = 0; i < multi->n; ++i) {
1240 isl_bool has_nan = FN(EL,involves_nan)(multi->u.p[i]);
1241 if (has_nan < 0 || has_nan)
1242 return has_nan;
1245 return isl_bool_false;
1248 /* Return the opposite of "multi".
1250 __isl_give MULTI(BASE) *FN(MULTI(BASE),neg)(__isl_take MULTI(BASE) *multi)
1252 int i;
1254 multi = FN(MULTI(BASE),cow)(multi);
1255 if (!multi)
1256 return NULL;
1258 for (i = 0; i < multi->n; ++i) {
1259 multi->u.p[i] = FN(EL,neg)(multi->u.p[i]);
1260 if (!multi->u.p[i])
1261 return FN(MULTI(BASE),free)(multi);
1264 return multi;