export isl_multi_aff_{domain,range}_map
[isl.git] / isl_multi_templ.c
blob06e404bc2582b75e9a78d4e1047e5ff2045eb35c
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
21 isl_ctx *FN(MULTI(BASE),get_ctx)(__isl_keep MULTI(BASE) *multi)
23 return multi ? isl_space_get_ctx(multi->space) : NULL;
26 /* Return the space of "multi".
28 __isl_keep isl_space *FN(MULTI(BASE),peek_space)(__isl_keep MULTI(BASE) *multi)
30 return multi ? multi->space : NULL;
33 __isl_give isl_space *FN(MULTI(BASE),get_space)(__isl_keep MULTI(BASE) *multi)
35 return isl_space_copy(FN(MULTI(BASE),peek_space)(multi));
38 /* Return the position of the dimension of the given type and name
39 * in "multi".
40 * Return -1 if no such dimension can be found.
42 int FN(MULTI(BASE),find_dim_by_name)(__isl_keep MULTI(BASE) *multi,
43 enum isl_dim_type type, const char *name)
45 if (!multi)
46 return -1;
47 return isl_space_find_dim_by_name(multi->space, type, name);
50 __isl_give isl_space *FN(MULTI(BASE),get_domain_space)(
51 __isl_keep MULTI(BASE) *multi)
53 return multi ? isl_space_domain(isl_space_copy(multi->space)) : NULL;
56 /* Allocate a multi expression living in "space".
58 * If the number of base expressions is zero, then make sure
59 * there is enough room in the structure for the explicit domain,
60 * in case the type supports such an explicit domain.
62 __isl_give MULTI(BASE) *FN(MULTI(BASE),alloc)(__isl_take isl_space *space)
64 isl_ctx *ctx;
65 isl_size n;
66 MULTI(BASE) *multi;
68 n = isl_space_dim(space, isl_dim_out);
69 if (n < 0)
70 goto error;
72 ctx = isl_space_get_ctx(space);
73 if (n > 0)
74 multi = isl_calloc(ctx, MULTI(BASE),
75 sizeof(MULTI(BASE)) + (n - 1) * sizeof(struct EL *));
76 else
77 multi = isl_calloc(ctx, MULTI(BASE), sizeof(MULTI(BASE)));
78 if (!multi)
79 goto error;
81 multi->space = space;
82 multi->n = n;
83 multi->ref = 1;
84 if (FN(MULTI(BASE),has_explicit_domain)(multi))
85 multi = FN(MULTI(BASE),init_explicit_domain)(multi);
86 return multi;
87 error:
88 isl_space_free(space);
89 return NULL;
92 __isl_give MULTI(BASE) *FN(MULTI(BASE),dup)(__isl_keep MULTI(BASE) *multi)
94 int i;
95 MULTI(BASE) *dup;
97 if (!multi)
98 return NULL;
100 dup = FN(MULTI(BASE),alloc)(isl_space_copy(multi->space));
101 if (!dup)
102 return NULL;
104 for (i = 0; i < multi->n; ++i)
105 dup = FN(FN(MULTI(BASE),set),BASE)(dup, i,
106 FN(EL,copy)(multi->u.p[i]));
107 if (FN(MULTI(BASE),has_explicit_domain)(multi))
108 dup = FN(MULTI(BASE),copy_explicit_domain)(dup, multi);
110 return dup;
113 __isl_give MULTI(BASE) *FN(MULTI(BASE),cow)(__isl_take MULTI(BASE) *multi)
115 if (!multi)
116 return NULL;
118 if (multi->ref == 1)
119 return multi;
121 multi->ref--;
122 return FN(MULTI(BASE),dup)(multi);
125 __isl_give MULTI(BASE) *FN(MULTI(BASE),copy)(__isl_keep MULTI(BASE) *multi)
127 if (!multi)
128 return NULL;
130 multi->ref++;
131 return multi;
134 __isl_null MULTI(BASE) *FN(MULTI(BASE),free)(__isl_take MULTI(BASE) *multi)
136 int i;
138 if (!multi)
139 return NULL;
141 if (--multi->ref > 0)
142 return NULL;
144 isl_space_free(multi->space);
145 for (i = 0; i < multi->n; ++i)
146 FN(EL,free)(multi->u.p[i]);
147 if (FN(MULTI(BASE),has_explicit_domain)(multi))
148 FN(MULTI(BASE),free_explicit_domain)(multi);
149 free(multi);
151 return NULL;
154 isl_size FN(MULTI(BASE),dim)(__isl_keep MULTI(BASE) *multi,
155 enum isl_dim_type type)
157 return isl_space_dim(FN(MULTI(BASE),peek_space)(multi), type);
160 /* Return the position of the first dimension of "type" with id "id".
161 * Return -1 if there is no such dimension.
163 int FN(MULTI(BASE),find_dim_by_id)(__isl_keep MULTI(BASE) *multi,
164 enum isl_dim_type type, __isl_keep isl_id *id)
166 if (!multi)
167 return -1;
168 return isl_space_find_dim_by_id(multi->space, type, id);
171 /* Return the id of the given dimension.
173 __isl_give isl_id *FN(MULTI(BASE),get_dim_id)(__isl_keep MULTI(BASE) *multi,
174 enum isl_dim_type type, unsigned pos)
176 return multi ? isl_space_get_dim_id(multi->space, type, pos) : NULL;
179 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_name)(
180 __isl_take MULTI(BASE) *multi,
181 enum isl_dim_type type, unsigned pos, const char *s)
183 int i;
185 multi = FN(MULTI(BASE),cow)(multi);
186 if (!multi)
187 return NULL;
189 multi->space = isl_space_set_dim_name(multi->space, type, pos, s);
190 if (!multi->space)
191 return FN(MULTI(BASE),free)(multi);
193 if (type == isl_dim_out)
194 return multi;
195 for (i = 0; i < multi->n; ++i) {
196 multi->u.p[i] = FN(EL,set_dim_name)(multi->u.p[i],
197 type, pos, s);
198 if (!multi->u.p[i])
199 return FN(MULTI(BASE),free)(multi);
202 return multi;
205 const char *FN(MULTI(BASE),get_tuple_name)(__isl_keep MULTI(BASE) *multi,
206 enum isl_dim_type type)
208 return multi ? isl_space_get_tuple_name(multi->space, type) : NULL;
211 /* Does the specified tuple have an id?
213 isl_bool FN(MULTI(BASE),has_tuple_id)(__isl_keep MULTI(BASE) *multi,
214 enum isl_dim_type type)
216 if (!multi)
217 return isl_bool_error;
218 return isl_space_has_tuple_id(multi->space, type);
221 /* Return the id of the specified tuple.
223 __isl_give isl_id *FN(MULTI(BASE),get_tuple_id)(__isl_keep MULTI(BASE) *multi,
224 enum isl_dim_type type)
226 return multi ? isl_space_get_tuple_id(multi->space, type) : NULL;
229 #undef TYPE
230 #define TYPE MULTI(BASE)
231 static
232 #include "check_type_range_templ.c"
234 __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
235 int pos)
237 isl_ctx *ctx;
239 if (FN(MULTI(BASE),check_range)(multi, isl_dim_out, pos, 1) < 0)
240 return NULL;
241 ctx = FN(MULTI(BASE),get_ctx)(multi);
242 return FN(EL,copy)(multi->u.p[pos]);
245 /* Set the element at position "pos" of "multi" to "el",
246 * where the position may be empty if "multi" has only a single reference.
248 static __isl_give MULTI(BASE) *FN(MULTI(BASE),restore)(
249 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
251 multi = FN(MULTI(BASE),cow)(multi);
252 if (!multi || !el)
253 goto error;
255 if (FN(MULTI(BASE),check_range)(multi, isl_dim_out, pos, 1) < 0)
256 goto error;
258 FN(EL,free)(multi->u.p[pos]);
259 multi->u.p[pos] = el;
261 return multi;
262 error:
263 FN(MULTI(BASE),free)(multi);
264 FN(EL,free)(el);
265 return NULL;
268 /* Set the element at position "pos" of "multi" to "el",
269 * where the position may be empty if "multi" has only a single reference.
270 * However, the space of "multi" is available and is checked
271 * for compatibility with "el".
273 static __isl_give MULTI(BASE) *FN(MULTI(BASE),restore_check_space)(
274 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
276 isl_space *space;
278 space = FN(MULTI(BASE),peek_space)(multi);
279 if (FN(EL,check_match_domain_space)(el, space) < 0)
280 multi = FN(MULTI(BASE),free)(multi);
281 return FN(MULTI(BASE),restore)(multi, pos, el);
284 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
285 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
287 isl_space *multi_space = NULL;
288 isl_space *el_space = NULL;
289 isl_bool match;
291 multi_space = FN(MULTI(BASE),get_space)(multi);
292 match = FN(EL,matching_params)(el, multi_space);
293 if (match < 0)
294 goto error;
295 if (!match) {
296 multi = FN(MULTI(BASE),align_params)(multi,
297 FN(EL,get_space)(el));
298 isl_space_free(multi_space);
299 multi_space = FN(MULTI(BASE),get_space)(multi);
300 el = FN(EL,align_params)(el, isl_space_copy(multi_space));
303 multi = FN(MULTI(BASE),restore_check_space)(multi, pos, el);
305 isl_space_free(multi_space);
306 isl_space_free(el_space);
308 return multi;
309 error:
310 FN(MULTI(BASE),free)(multi);
311 FN(EL,free)(el);
312 isl_space_free(multi_space);
313 isl_space_free(el_space);
314 return NULL;
317 /* Reset the space of "multi". This function is called from isl_pw_templ.c
318 * and doesn't know if the space of an element object is represented
319 * directly or through its domain. It therefore passes along both,
320 * which we pass along to the element function since we don't know how
321 * that is represented either.
323 * If "multi" has an explicit domain, then the caller is expected
324 * to make sure that any modification that would change the dimensions
325 * of the explicit domain has bee applied before this function is called.
327 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
328 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
329 __isl_take isl_space *domain)
331 int i;
333 multi = FN(MULTI(BASE),cow)(multi);
334 if (!multi || !space || !domain)
335 goto error;
337 for (i = 0; i < multi->n; ++i) {
338 multi->u.p[i] = FN(EL,reset_domain_space)(multi->u.p[i],
339 isl_space_copy(domain));
340 if (!multi->u.p[i])
341 goto error;
343 if (FN(MULTI(BASE),has_explicit_domain)(multi)) {
344 multi = FN(MULTI(BASE),reset_explicit_domain_space)(multi,
345 isl_space_copy(domain));
346 if (!multi)
347 goto error;
349 isl_space_free(domain);
350 isl_space_free(multi->space);
351 multi->space = space;
353 return multi;
354 error:
355 isl_space_free(domain);
356 isl_space_free(space);
357 FN(MULTI(BASE),free)(multi);
358 return NULL;
361 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
362 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
364 isl_space *space;
366 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
367 isl_space_copy(multi->space));
368 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
371 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
372 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
374 isl_space *domain;
376 domain = isl_space_domain(isl_space_copy(space));
377 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
380 /* Set the id of the given dimension of "multi" to "id".
382 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_id)(
383 __isl_take MULTI(BASE) *multi,
384 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
386 isl_space *space;
388 multi = FN(MULTI(BASE),cow)(multi);
389 if (!multi || !id)
390 goto error;
392 space = FN(MULTI(BASE),get_space)(multi);
393 space = isl_space_set_dim_id(space, type, pos, id);
395 return FN(MULTI(BASE),reset_space)(multi, space);
396 error:
397 isl_id_free(id);
398 FN(MULTI(BASE),free)(multi);
399 return NULL;
402 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_name)(
403 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
404 const char *s)
406 isl_space *space;
408 multi = FN(MULTI(BASE),cow)(multi);
409 if (!multi)
410 return NULL;
412 space = FN(MULTI(BASE),get_space)(multi);
413 space = isl_space_set_tuple_name(space, type, s);
415 return FN(MULTI(BASE),reset_space)(multi, space);
418 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_id)(
419 __isl_take MULTI(BASE) *multi, enum isl_dim_type type,
420 __isl_take isl_id *id)
422 isl_space *space;
424 multi = FN(MULTI(BASE),cow)(multi);
425 if (!multi)
426 goto error;
428 space = FN(MULTI(BASE),get_space)(multi);
429 space = isl_space_set_tuple_id(space, type, id);
431 return FN(MULTI(BASE),reset_space)(multi, space);
432 error:
433 isl_id_free(id);
434 return NULL;
437 /* Drop the id on the specified tuple.
439 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_tuple_id)(
440 __isl_take MULTI(BASE) *multi, enum isl_dim_type type)
442 isl_space *space;
444 if (!multi)
445 return NULL;
446 if (!FN(MULTI(BASE),has_tuple_id)(multi, type))
447 return multi;
449 multi = FN(MULTI(BASE),cow)(multi);
450 if (!multi)
451 return NULL;
453 space = FN(MULTI(BASE),get_space)(multi);
454 space = isl_space_reset_tuple_id(space, type);
456 return FN(MULTI(BASE),reset_space)(multi, space);
459 /* Reset the user pointer on all identifiers of parameters and tuples
460 * of the space of "multi".
462 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_user)(
463 __isl_take MULTI(BASE) *multi)
465 isl_space *space;
467 space = FN(MULTI(BASE),get_space)(multi);
468 space = isl_space_reset_user(space);
470 return FN(MULTI(BASE),reset_space)(multi, space);
473 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
474 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
476 int i;
477 isl_space *space;
479 multi = FN(MULTI(BASE),cow)(multi);
480 if (!multi || !exp)
481 goto error;
483 for (i = 0; i < multi->n; ++i) {
484 multi->u.p[i] = FN(EL,realign_domain)(multi->u.p[i],
485 isl_reordering_copy(exp));
486 if (!multi->u.p[i])
487 goto error;
490 space = isl_reordering_get_space(exp);
491 multi = FN(MULTI(BASE),reset_domain_space)(multi, space);
493 isl_reordering_free(exp);
494 return multi;
495 error:
496 isl_reordering_free(exp);
497 FN(MULTI(BASE),free)(multi);
498 return NULL;
501 /* Align the parameters of "multi" to those of "model".
503 * If "multi" has an explicit domain, then align the parameters
504 * of the domain first.
506 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
507 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
509 isl_ctx *ctx;
510 isl_bool equal_params;
511 isl_reordering *exp;
513 if (!multi || !model)
514 goto error;
516 equal_params = isl_space_has_equal_params(multi->space, model);
517 if (equal_params < 0)
518 goto error;
519 if (equal_params) {
520 isl_space_free(model);
521 return multi;
524 ctx = isl_space_get_ctx(model);
525 if (!isl_space_has_named_params(model))
526 isl_die(ctx, isl_error_invalid,
527 "model has unnamed parameters", goto error);
528 if (!isl_space_has_named_params(multi->space))
529 isl_die(ctx, isl_error_invalid,
530 "input has unnamed parameters", goto error);
532 if (FN(MULTI(BASE),has_explicit_domain)(multi)) {
533 multi = FN(MULTI(BASE),align_explicit_domain_params)(multi,
534 isl_space_copy(model));
535 if (!multi)
536 goto error;
538 exp = isl_parameter_alignment_reordering(multi->space, model);
539 exp = isl_reordering_extend_space(exp,
540 FN(MULTI(BASE),get_domain_space)(multi));
541 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
543 isl_space_free(model);
544 return multi;
545 error:
546 isl_space_free(model);
547 FN(MULTI(BASE),free)(multi);
548 return NULL;
551 /* Create a multi expression in the given space with the elements of "list"
552 * as base expressions.
554 * Since isl_multi_*_restore_* assumes that the element and
555 * the multi expression have matching spaces, the alignment
556 * (if any) needs to be performed beforehand.
558 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
559 __isl_take isl_space *space, __isl_take LIST(EL) *list)
561 int i;
562 isl_size n, dim;
563 isl_ctx *ctx;
564 MULTI(BASE) *multi;
566 dim = isl_space_dim(space, isl_dim_out);
567 n = FN(FN(LIST(EL),n),BASE)(list);
568 if (dim < 0 || n < 0)
569 goto error;
571 ctx = isl_space_get_ctx(space);
572 if (n != dim)
573 isl_die(ctx, isl_error_invalid,
574 "invalid number of elements in list", goto error);
576 for (i = 0; i < n; ++i) {
577 EL *el = FN(LIST(EL),peek)(list, i);
578 space = isl_space_align_params(space, FN(EL,get_space)(el));
580 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
581 for (i = 0; i < n; ++i) {
582 EL *el = FN(FN(LIST(EL),get),BASE)(list, i);
583 el = FN(EL,align_params)(el, isl_space_copy(space));
584 multi = FN(MULTI(BASE),restore_check_space)(multi, i, el);
587 isl_space_free(space);
588 FN(LIST(EL),free)(list);
589 return multi;
590 error:
591 isl_space_free(space);
592 FN(LIST(EL),free)(list);
593 return NULL;
596 __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
597 __isl_take MULTI(BASE) *multi,
598 enum isl_dim_type type, unsigned first, unsigned n)
600 int i;
602 multi = FN(MULTI(BASE),cow)(multi);
603 if (FN(MULTI(BASE),check_range)(multi, type, first, n) < 0)
604 return FN(MULTI(BASE),free)(multi);
606 multi->space = isl_space_drop_dims(multi->space, type, first, n);
607 if (!multi->space)
608 return FN(MULTI(BASE),free)(multi);
610 if (type == isl_dim_out) {
611 for (i = 0; i < n; ++i)
612 FN(EL,free)(multi->u.p[first + i]);
613 for (i = first; i + n < multi->n; ++i)
614 multi->u.p[i] = multi->u.p[i + n];
615 multi->n -= n;
616 if (n > 0 && FN(MULTI(BASE),has_explicit_domain)(multi))
617 multi = FN(MULTI(BASE),init_explicit_domain)(multi);
619 return multi;
622 if (FN(MULTI(BASE),has_explicit_domain)(multi))
623 multi = FN(MULTI(BASE),drop_explicit_domain_dims)(multi,
624 type, first, n);
625 if (!multi)
626 return NULL;
628 for (i = 0; i < multi->n; ++i) {
629 multi->u.p[i] = FN(EL,drop_dims)(multi->u.p[i], type, first, n);
630 if (!multi->u.p[i])
631 return FN(MULTI(BASE),free)(multi);
634 return multi;
637 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
639 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_multi_and)(
640 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
641 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi1,
642 __isl_take MULTI(BASE) *multi2))
644 isl_ctx *ctx;
645 isl_bool equal_params;
647 if (!multi1 || !multi2)
648 goto error;
649 equal_params = isl_space_has_equal_params(multi1->space, multi2->space);
650 if (equal_params < 0)
651 goto error;
652 if (equal_params)
653 return fn(multi1, multi2);
654 ctx = FN(MULTI(BASE),get_ctx)(multi1);
655 if (!isl_space_has_named_params(multi1->space) ||
656 !isl_space_has_named_params(multi2->space))
657 isl_die(ctx, isl_error_invalid,
658 "unaligned unnamed parameters", goto error);
659 multi1 = FN(MULTI(BASE),align_params)(multi1,
660 FN(MULTI(BASE),get_space)(multi2));
661 multi2 = FN(MULTI(BASE),align_params)(multi2,
662 FN(MULTI(BASE),get_space)(multi1));
663 return fn(multi1, multi2);
664 error:
665 FN(MULTI(BASE),free)(multi1);
666 FN(MULTI(BASE),free)(multi2);
667 return NULL;
670 /* Given two MULTI(BASE)s A -> B and C -> D,
671 * construct a MULTI(BASE) (A * C) -> [B -> D].
673 * The parameters are assumed to have been aligned.
675 * If "multi1" and/or "multi2" has an explicit domain, then
676 * intersect the domain of the result with these explicit domains.
678 static __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product_aligned)(
679 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
681 int i;
682 isl_size n1, n2;
683 EL *el;
684 isl_space *space;
685 MULTI(BASE) *res;
687 n1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
688 n2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
689 if (n1 < 0 || n2 < 0)
690 goto error;
692 space = isl_space_range_product(FN(MULTI(BASE),get_space)(multi1),
693 FN(MULTI(BASE),get_space)(multi2));
694 res = FN(MULTI(BASE),alloc)(space);
696 for (i = 0; i < n1; ++i) {
697 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
698 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
701 for (i = 0; i < n2; ++i) {
702 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
703 res = FN(FN(MULTI(BASE),set),BASE)(res, n1 + i, el);
706 if (FN(MULTI(BASE),has_explicit_domain)(multi1))
707 res = FN(MULTI(BASE),intersect_explicit_domain)(res, multi1);
708 if (FN(MULTI(BASE),has_explicit_domain)(multi2))
709 res = FN(MULTI(BASE),intersect_explicit_domain)(res, multi2);
711 FN(MULTI(BASE),free)(multi1);
712 FN(MULTI(BASE),free)(multi2);
713 return res;
714 error:
715 FN(MULTI(BASE),free)(multi1);
716 FN(MULTI(BASE),free)(multi2);
717 return NULL;
720 /* Given two MULTI(BASE)s A -> B and C -> D,
721 * construct a MULTI(BASE) (A * C) -> [B -> D].
723 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product)(
724 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
726 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
727 &FN(MULTI(BASE),range_product_aligned));
730 /* Is the range of "multi" a wrapped relation?
732 isl_bool FN(MULTI(BASE),range_is_wrapping)(__isl_keep MULTI(BASE) *multi)
734 if (!multi)
735 return isl_bool_error;
736 return isl_space_range_is_wrapping(multi->space);
739 /* Given a function A -> [B -> C], extract the function A -> B.
741 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_domain)(
742 __isl_take MULTI(BASE) *multi)
744 isl_space *space;
745 isl_size total, keep;
747 total = FN(MULTI(BASE),dim)(multi, isl_dim_out);
748 if (total < 0)
749 return FN(MULTI(BASE),free)(multi);
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 space = isl_space_range_factor_domain(space);
757 keep = isl_space_dim(space, isl_dim_out);
758 if (keep < 0)
759 multi = FN(MULTI(BASE),free)(multi);
760 multi = FN(MULTI(BASE),drop_dims)(multi,
761 isl_dim_out, keep, total - keep);
762 multi = FN(MULTI(BASE),reset_space)(multi, space);
764 return multi;
767 /* Given a function A -> [B -> C], extract the function A -> C.
769 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_factor_range)(
770 __isl_take MULTI(BASE) *multi)
772 isl_space *space;
773 isl_size total, keep;
775 total = FN(MULTI(BASE),dim)(multi, isl_dim_out);
776 if (total < 0)
777 return FN(MULTI(BASE),free)(multi);
778 if (!isl_space_range_is_wrapping(multi->space))
779 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
780 "range is not a product",
781 return FN(MULTI(BASE),free)(multi));
783 space = FN(MULTI(BASE),get_space)(multi);
784 space = isl_space_range_factor_range(space);
785 keep = isl_space_dim(space, isl_dim_out);
786 if (keep < 0)
787 multi = FN(MULTI(BASE),free)(multi);
788 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_out, 0, total - keep);
789 multi = FN(MULTI(BASE),reset_space)(multi, space);
791 return multi;
794 /* Given a function [B -> C], extract the function C.
796 __isl_give MULTI(BASE) *FN(MULTI(BASE),factor_range)(
797 __isl_take MULTI(BASE) *multi)
799 isl_space *space;
800 isl_size total, keep;
802 total = FN(MULTI(BASE),dim)(multi, isl_dim_set);
803 if (total < 0)
804 return FN(MULTI(BASE),free)(multi);
805 if (!isl_space_is_wrapping(multi->space))
806 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
807 "not a product", return FN(MULTI(BASE),free)(multi));
809 space = FN(MULTI(BASE),get_space)(multi);
810 space = isl_space_factor_range(space);
811 keep = isl_space_dim(space, isl_dim_set);
812 if (keep < 0)
813 multi = FN(MULTI(BASE),free)(multi);
814 multi = FN(MULTI(BASE),drop_dims)(multi, isl_dim_set, 0, total - keep);
815 multi = FN(MULTI(BASE),reset_space)(multi, space);
817 return multi;
820 __isl_give MULTI(BASE) *FN(MULTI(BASE),flatten_range)(
821 __isl_take MULTI(BASE) *multi)
823 if (!multi)
824 return NULL;
826 if (!multi->space->nested[1])
827 return multi;
829 multi = FN(MULTI(BASE),cow)(multi);
830 if (!multi)
831 return NULL;
833 multi->space = isl_space_flatten_range(multi->space);
834 if (!multi->space)
835 return FN(MULTI(BASE),free)(multi);
837 return multi;
840 /* Given two MULTI(BASE)s A -> B and C -> D,
841 * construct a MULTI(BASE) (A * C) -> (B, D).
843 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
844 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
846 MULTI(BASE) *multi;
848 multi = FN(MULTI(BASE),range_product)(multi1, multi2);
849 multi = FN(MULTI(BASE),flatten_range)(multi);
850 return multi;
853 /* Given two multi expressions, "multi1"
855 * [A] -> [B1 B2]
857 * where B2 starts at position "pos", and "multi2"
859 * [A] -> [D]
861 * return the multi expression
863 * [A] -> [B1 D B2]
865 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_splice)(
866 __isl_take MULTI(BASE) *multi1, unsigned pos,
867 __isl_take MULTI(BASE) *multi2)
869 MULTI(BASE) *res;
870 isl_size dim;
872 dim = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
873 if (dim < 0 || !multi2)
874 goto error;
876 if (FN(MULTI(BASE),check_range)(multi1, isl_dim_out, pos, 0) < 0)
877 goto error;
879 res = FN(MULTI(BASE),copy)(multi1);
880 res = FN(MULTI(BASE),drop_dims)(res, isl_dim_out, pos, dim - pos);
881 multi1 = FN(MULTI(BASE),drop_dims)(multi1, isl_dim_out, 0, pos);
883 res = FN(MULTI(BASE),flat_range_product)(res, multi2);
884 res = FN(MULTI(BASE),flat_range_product)(res, multi1);
886 return res;
887 error:
888 FN(MULTI(BASE),free)(multi1);
889 FN(MULTI(BASE),free)(multi2);
890 return NULL;
893 /* Check that "multi1" and "multi2" live in the same space,
894 * reporting an error if they do not.
896 static isl_stat FN(MULTI(BASE),check_equal_space)(
897 __isl_keep MULTI(BASE) *multi1, __isl_keep MULTI(BASE) *multi2)
899 isl_bool equal;
901 if (!multi1 || !multi2)
902 return isl_stat_error;
904 equal = isl_space_is_equal(multi1->space, multi2->space);
905 if (equal < 0)
906 return isl_stat_error;
907 if (!equal)
908 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
909 "spaces don't match", return isl_stat_error);
911 return isl_stat_ok;
914 /* This function is currently only used from isl_aff.c
916 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
917 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
918 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
919 __attribute__ ((unused));
921 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
922 * return the result.
924 * If "multi2" has an explicit domain, then
925 * intersect the domain of the result with this explicit domain.
927 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
928 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
929 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
931 int i;
933 multi1 = FN(MULTI(BASE),cow)(multi1);
934 if (FN(MULTI(BASE),check_equal_space)(multi1, multi2) < 0)
935 goto error;
937 for (i = 0; i < multi1->n; ++i) {
938 multi1->u.p[i] = fn(multi1->u.p[i],
939 FN(EL,copy)(multi2->u.p[i]));
940 if (!multi1->u.p[i])
941 goto error;
944 if (FN(MULTI(BASE),has_explicit_domain)(multi2))
945 multi1 = FN(MULTI(BASE),intersect_explicit_domain)(multi1,
946 multi2);
948 FN(MULTI(BASE),free)(multi2);
949 return multi1;
950 error:
951 FN(MULTI(BASE),free)(multi1);
952 FN(MULTI(BASE),free)(multi2);
953 return NULL;
956 /* Add "multi2" to "multi1" and return the result.
958 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
960 static __isl_give MULTI(BASE) *FN(MULTI(BASE),add_aligned)(
961 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
963 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,add));
966 /* Add "multi2" to "multi1" and return the result.
968 __isl_give MULTI(BASE) *FN(MULTI(BASE),add)(__isl_take MULTI(BASE) *multi1,
969 __isl_take MULTI(BASE) *multi2)
971 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
972 &FN(MULTI(BASE),add_aligned));
975 /* Subtract "multi2" from "multi1" and return the result.
977 * The parameters of "multi1" and "multi2" are assumed to have been aligned.
979 static __isl_give MULTI(BASE) *FN(MULTI(BASE),sub_aligned)(
980 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
982 return FN(MULTI(BASE),bin_op)(multi1, multi2, &FN(EL,sub));
985 /* Subtract "multi2" from "multi1" and return the result.
987 __isl_give MULTI(BASE) *FN(MULTI(BASE),sub)(__isl_take MULTI(BASE) *multi1,
988 __isl_take MULTI(BASE) *multi2)
990 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
991 &FN(MULTI(BASE),sub_aligned));
994 /* Multiply the elements of "multi" by "v" and return the result.
996 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_val)(__isl_take MULTI(BASE) *multi,
997 __isl_take isl_val *v)
999 int i;
1001 if (!multi || !v)
1002 goto error;
1004 if (isl_val_is_one(v)) {
1005 isl_val_free(v);
1006 return multi;
1009 if (!isl_val_is_rat(v))
1010 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1011 "expecting rational factor", goto error);
1013 multi = FN(MULTI(BASE),cow)(multi);
1014 if (!multi)
1015 return NULL;
1017 for (i = 0; i < multi->n; ++i) {
1018 multi->u.p[i] = FN(EL,scale_val)(multi->u.p[i],
1019 isl_val_copy(v));
1020 if (!multi->u.p[i])
1021 goto error;
1024 isl_val_free(v);
1025 return multi;
1026 error:
1027 isl_val_free(v);
1028 return FN(MULTI(BASE),free)(multi);
1031 /* Divide the elements of "multi" by "v" and return the result.
1033 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_val)(
1034 __isl_take MULTI(BASE) *multi, __isl_take isl_val *v)
1036 int i;
1038 if (!multi || !v)
1039 goto error;
1041 if (isl_val_is_one(v)) {
1042 isl_val_free(v);
1043 return multi;
1046 if (!isl_val_is_rat(v))
1047 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1048 "expecting rational factor", goto error);
1049 if (isl_val_is_zero(v))
1050 isl_die(isl_val_get_ctx(v), isl_error_invalid,
1051 "cannot scale down by zero", goto error);
1053 multi = FN(MULTI(BASE),cow)(multi);
1054 if (!multi)
1055 return NULL;
1057 for (i = 0; i < multi->n; ++i) {
1058 multi->u.p[i] = FN(EL,scale_down_val)(multi->u.p[i],
1059 isl_val_copy(v));
1060 if (!multi->u.p[i])
1061 goto error;
1064 isl_val_free(v);
1065 return multi;
1066 error:
1067 isl_val_free(v);
1068 return FN(MULTI(BASE),free)(multi);
1071 /* Multiply the elements of "multi" by the corresponding element of "mv"
1072 * and return the result.
1074 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_multi_val)(
1075 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1077 int i;
1079 if (!multi || !mv)
1080 goto error;
1082 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1083 mv->space, isl_dim_set))
1084 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1085 "spaces don't match", goto error);
1087 multi = FN(MULTI(BASE),cow)(multi);
1088 if (!multi)
1089 goto error;
1091 for (i = 0; i < multi->n; ++i) {
1092 isl_val *v;
1094 v = isl_multi_val_get_val(mv, i);
1095 multi->u.p[i] = FN(EL,scale_val)(multi->u.p[i], v);
1096 if (!multi->u.p[i])
1097 goto error;
1100 isl_multi_val_free(mv);
1101 return multi;
1102 error:
1103 isl_multi_val_free(mv);
1104 return FN(MULTI(BASE),free)(multi);
1107 /* Divide the elements of "multi" by the corresponding element of "mv"
1108 * and return the result.
1110 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_multi_val)(
1111 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1113 int i;
1115 if (!multi || !mv)
1116 goto error;
1118 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1119 mv->space, isl_dim_set))
1120 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1121 "spaces don't match", goto error);
1123 multi = FN(MULTI(BASE),cow)(multi);
1124 if (!multi)
1125 return NULL;
1127 for (i = 0; i < multi->n; ++i) {
1128 isl_val *v;
1130 v = isl_multi_val_get_val(mv, i);
1131 multi->u.p[i] = FN(EL,scale_down_val)(multi->u.p[i], v);
1132 if (!multi->u.p[i])
1133 goto error;
1136 isl_multi_val_free(mv);
1137 return multi;
1138 error:
1139 isl_multi_val_free(mv);
1140 return FN(MULTI(BASE),free)(multi);
1143 /* Compute the residues of the elements of "multi" modulo
1144 * the corresponding element of "mv" and return the result.
1146 __isl_give MULTI(BASE) *FN(MULTI(BASE),mod_multi_val)(
1147 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1149 int i;
1151 if (!multi || !mv)
1152 goto error;
1154 if (!isl_space_tuple_is_equal(multi->space, isl_dim_out,
1155 mv->space, isl_dim_set))
1156 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1157 "spaces don't match", goto error);
1159 multi = FN(MULTI(BASE),cow)(multi);
1160 if (!multi)
1161 goto error;
1163 for (i = 0; i < multi->n; ++i) {
1164 isl_val *v;
1166 v = isl_multi_val_get_val(mv, i);
1167 multi->u.p[i] = FN(EL,mod_val)(multi->u.p[i], v);
1168 if (!multi->u.p[i])
1169 goto error;
1172 isl_multi_val_free(mv);
1173 return multi;
1174 error:
1175 isl_multi_val_free(mv);
1176 return FN(MULTI(BASE),free)(multi);
1179 /* Convert a multiple expression defined over a parameter domain
1180 * into one that is defined over a zero-dimensional set.
1182 __isl_give MULTI(BASE) *FN(MULTI(BASE),from_range)(
1183 __isl_take MULTI(BASE) *multi)
1185 isl_space *space;
1187 if (!multi)
1188 return NULL;
1189 if (!isl_space_is_set(multi->space))
1190 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1191 "not living in a set space",
1192 return FN(MULTI(BASE),free)(multi));
1194 space = FN(MULTI(BASE),get_space)(multi);
1195 space = isl_space_from_range(space);
1196 multi = FN(MULTI(BASE),reset_space)(multi, space);
1198 return multi;
1201 /* Are "multi1" and "multi2" obviously equal?
1203 isl_bool FN(MULTI(BASE),plain_is_equal)(__isl_keep MULTI(BASE) *multi1,
1204 __isl_keep MULTI(BASE) *multi2)
1206 int i;
1207 isl_bool equal;
1209 if (!multi1 || !multi2)
1210 return isl_bool_error;
1211 if (multi1->n != multi2->n)
1212 return isl_bool_false;
1213 equal = isl_space_is_equal(multi1->space, multi2->space);
1214 if (equal < 0 || !equal)
1215 return equal;
1217 for (i = 0; i < multi1->n; ++i) {
1218 equal = FN(EL,plain_is_equal)(multi1->u.p[i], multi2->u.p[i]);
1219 if (equal < 0 || !equal)
1220 return equal;
1223 if (FN(MULTI(BASE),has_explicit_domain)(multi1) ||
1224 FN(MULTI(BASE),has_explicit_domain)(multi2)) {
1225 equal = FN(MULTI(BASE),equal_explicit_domain)(multi1, multi2);
1226 if (equal < 0 || !equal)
1227 return equal;
1230 return isl_bool_true;
1233 /* Does "multi" involve any NaNs?
1235 isl_bool FN(MULTI(BASE),involves_nan)(__isl_keep MULTI(BASE) *multi)
1237 int i;
1239 if (!multi)
1240 return isl_bool_error;
1241 if (multi->n == 0)
1242 return isl_bool_false;
1244 for (i = 0; i < multi->n; ++i) {
1245 isl_bool has_nan = FN(EL,involves_nan)(multi->u.p[i]);
1246 if (has_nan < 0 || has_nan)
1247 return has_nan;
1250 return isl_bool_false;
1253 /* Return the opposite of "multi".
1255 __isl_give MULTI(BASE) *FN(MULTI(BASE),neg)(__isl_take MULTI(BASE) *multi)
1257 int i;
1259 multi = FN(MULTI(BASE),cow)(multi);
1260 if (!multi)
1261 return NULL;
1263 for (i = 0; i < multi->n; ++i) {
1264 multi->u.p[i] = FN(EL,neg)(multi->u.p[i]);
1265 if (!multi->u.p[i])
1266 return FN(MULTI(BASE),free)(multi);
1269 return multi;