add isl_multi_*_reset_tuple_id
[isl.git] / isl_multi_templ.c
blob79ef615c1ba58780e0a39df94f2f0155e611dda5
1 /*
2 * Copyright 2011 Sven Verdoolaege
3 * Copyright 2012-2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege,
8 * Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
9 */
11 #include <isl_space_private.h>
12 #include <isl/set.h>
13 #include <isl_reordering.h>
15 #define xCAT(A,B) A ## B
16 #define CAT(A,B) xCAT(A,B)
17 #undef EL
18 #define EL CAT(isl_,BASE)
19 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
20 #define FN(TYPE,NAME) xFN(TYPE,NAME)
21 #define xMULTI(BASE) isl_multi_ ## BASE
22 #define MULTI(BASE) xMULTI(BASE)
23 #define MULTI_NAME(BASE) "isl_multi_" #BASE
24 #define xLIST(EL) EL ## _list
25 #define LIST(EL) xLIST(EL)
27 isl_ctx *FN(MULTI(BASE),get_ctx)(__isl_keep MULTI(BASE) *multi)
29 return multi ? isl_space_get_ctx(multi->space) : NULL;
32 __isl_give isl_space *FN(MULTI(BASE),get_space)(__isl_keep MULTI(BASE) *multi)
34 return multi ? isl_space_copy(multi->space) : NULL;
37 __isl_give isl_space *FN(MULTI(BASE),get_domain_space)(
38 __isl_keep MULTI(BASE) *multi)
40 return multi ? isl_space_domain(isl_space_copy(multi->space)) : NULL;
43 __isl_give MULTI(BASE) *FN(MULTI(BASE),alloc)(__isl_take isl_space *space)
45 isl_ctx *ctx;
46 int n;
47 MULTI(BASE) *multi;
49 if (!space)
50 return NULL;
52 ctx = isl_space_get_ctx(space);
53 n = isl_space_dim(space, isl_dim_out);
54 multi = isl_calloc(ctx, MULTI(BASE),
55 sizeof(MULTI(BASE)) + (n - 1) * sizeof(struct EL *));
56 if (!multi)
57 goto error;
59 multi->space = space;
60 multi->n = n;
61 multi->ref = 1;
62 return multi;
63 error:
64 isl_space_free(space);
65 return NULL;
68 __isl_give MULTI(BASE) *FN(MULTI(BASE),dup)(__isl_keep MULTI(BASE) *multi)
70 int i;
71 MULTI(BASE) *dup;
73 if (!multi)
74 return NULL;
76 dup = FN(MULTI(BASE),alloc)(isl_space_copy(multi->space));
77 if (!dup)
78 return NULL;
80 for (i = 0; i < multi->n; ++i)
81 dup = FN(FN(MULTI(BASE),set),BASE)(dup, i,
82 FN(EL,copy)(multi->p[i]));
84 return dup;
87 __isl_give MULTI(BASE) *FN(MULTI(BASE),cow)(__isl_take MULTI(BASE) *multi)
89 if (!multi)
90 return NULL;
92 if (multi->ref == 1)
93 return multi;
95 multi->ref--;
96 return FN(MULTI(BASE),dup)(multi);
99 __isl_give MULTI(BASE) *FN(MULTI(BASE),copy)(__isl_keep MULTI(BASE) *multi)
101 if (!multi)
102 return NULL;
104 multi->ref++;
105 return multi;
108 void *FN(MULTI(BASE),free)(__isl_take MULTI(BASE) *multi)
110 int i;
112 if (!multi)
113 return NULL;
115 if (--multi->ref > 0)
116 return NULL;
118 isl_space_free(multi->space);
119 for (i = 0; i < multi->n; ++i)
120 FN(EL,free)(multi->p[i]);
121 free(multi);
123 return NULL;
126 __isl_give MULTI(BASE) *FN(MULTI(BASE),insert_dims)(
127 __isl_take MULTI(BASE) *multi,
128 enum isl_dim_type type, unsigned first, unsigned n)
130 int i;
132 if (!multi)
133 return NULL;
134 if (type == isl_dim_out)
135 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
136 "cannot insert output/set dimensions",
137 return FN(MULTI(BASE),free)(multi));
138 if (n == 0 && !isl_space_is_named_or_nested(multi->space, type))
139 return multi;
141 multi = FN(MULTI(BASE),cow)(multi);
142 if (!multi)
143 return NULL;
145 multi->space = isl_space_insert_dims(multi->space, type, first, n);
146 if (!multi->space)
147 return FN(MULTI(BASE),free)(multi);
149 for (i = 0; i < multi->n; ++i) {
150 multi->p[i] = FN(EL,insert_dims)(multi->p[i], type, first, n);
151 if (!multi->p[i])
152 return FN(MULTI(BASE),free)(multi);
155 return multi;
158 __isl_give MULTI(BASE) *FN(MULTI(BASE),add_dims)(__isl_take MULTI(BASE) *multi,
159 enum isl_dim_type type, unsigned n)
161 unsigned pos;
163 pos = FN(MULTI(BASE),dim)(multi, type);
165 return FN(MULTI(BASE),insert_dims)(multi, type, pos, n);
168 unsigned FN(MULTI(BASE),dim)(__isl_keep MULTI(BASE) *multi,
169 enum isl_dim_type type)
171 return multi ? isl_space_dim(multi->space, type) : 0;
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->p[i] = FN(EL,set_dim_name)(multi->p[i], type, pos, s);
192 if (!multi->p[i])
193 return FN(MULTI(BASE),free)(multi);
196 return multi;
199 const char *FN(MULTI(BASE),get_tuple_name)(__isl_keep MULTI(BASE) *multi,
200 enum isl_dim_type type)
202 return multi ? isl_space_get_tuple_name(multi->space, type) : NULL;
205 /* Does the specified tuple have an id?
207 int FN(MULTI(BASE),has_tuple_id)(__isl_keep MULTI(BASE) *multi,
208 enum isl_dim_type type)
210 return multi ? isl_space_has_tuple_id(multi->space, type) : -1;
213 /* Return the id of the specified tuple.
215 __isl_give isl_id *FN(MULTI(BASE),get_tuple_id)(__isl_keep MULTI(BASE) *multi,
216 enum isl_dim_type type)
218 return multi ? isl_space_get_tuple_id(multi->space, type) : NULL;
221 __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
222 int pos)
224 isl_ctx *ctx;
226 if (!multi)
227 return NULL;
228 ctx = FN(MULTI(BASE),get_ctx)(multi);
229 if (pos < 0 || pos >= multi->n)
230 isl_die(ctx, isl_error_invalid,
231 "index out of bounds", return NULL);
232 return FN(EL,copy)(multi->p[pos]);
235 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
236 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
238 isl_space *multi_space = NULL;
239 isl_space *el_space = NULL;
240 int match;
242 multi = FN(MULTI(BASE),cow)(multi);
243 if (!multi || !el)
244 goto error;
246 multi_space = FN(MULTI(BASE),get_space)(multi);
247 match = FN(EL,matching_params)(el, multi_space);
248 if (match < 0)
249 goto error;
250 if (!match) {
251 multi = FN(MULTI(BASE),align_params)(multi,
252 FN(EL,get_space)(el));
253 isl_space_free(multi_space);
254 multi_space = FN(MULTI(BASE),get_space)(multi);
255 el = FN(EL,align_params)(el, isl_space_copy(multi_space));
257 if (FN(EL,check_match_domain_space)(el, multi_space) < 0)
258 goto error;
260 if (pos < 0 || pos >= multi->n)
261 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
262 "index out of bounds", goto error);
264 FN(EL,free)(multi->p[pos]);
265 multi->p[pos] = el;
267 isl_space_free(multi_space);
268 isl_space_free(el_space);
270 return multi;
271 error:
272 FN(MULTI(BASE),free)(multi);
273 FN(EL,free)(el);
274 isl_space_free(multi_space);
275 isl_space_free(el_space);
276 return NULL;
279 /* Reset the space of "multi". This function is called from isl_pw_templ.c
280 * and doesn't know if the space of an element object is represented
281 * directly or through its domain. It therefore passes along both,
282 * which we pass along to the element function since we don't how
283 * that is represented either.
285 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
286 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
287 __isl_take isl_space *domain)
289 int i;
291 multi = FN(MULTI(BASE),cow)(multi);
292 if (!multi || !space || !domain)
293 goto error;
295 for (i = 0; i < multi->n; ++i) {
296 multi->p[i] = FN(EL,reset_domain_space)(multi->p[i],
297 isl_space_copy(domain));
298 if (!multi->p[i])
299 goto error;
301 isl_space_free(domain);
302 isl_space_free(multi->space);
303 multi->space = space;
305 return multi;
306 error:
307 isl_space_free(domain);
308 isl_space_free(space);
309 FN(MULTI(BASE),free)(multi);
310 return NULL;
313 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
314 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
316 isl_space *space;
318 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
319 isl_space_copy(multi->space));
320 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
323 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
324 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
326 isl_space *domain;
328 domain = isl_space_domain(isl_space_copy(space));
329 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
332 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_name)(
333 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
334 const char *s)
336 isl_space *space;
338 multi = FN(MULTI(BASE),cow)(multi);
339 if (!multi)
340 return NULL;
342 space = FN(MULTI(BASE),get_space)(multi);
343 space = isl_space_set_tuple_name(space, type, s);
345 return FN(MULTI(BASE),reset_space)(multi, space);
348 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_id)(
349 __isl_take MULTI(BASE) *multi, enum isl_dim_type type,
350 __isl_take isl_id *id)
352 isl_space *space;
354 multi = FN(MULTI(BASE),cow)(multi);
355 if (!multi)
356 return isl_id_free(id);
358 space = FN(MULTI(BASE),get_space)(multi);
359 space = isl_space_set_tuple_id(space, type, id);
361 return FN(MULTI(BASE),reset_space)(multi, space);
364 /* Drop the id on the specified tuple.
366 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_tuple_id)(
367 __isl_take MULTI(BASE) *multi, enum isl_dim_type type)
369 isl_space *space;
371 if (!multi)
372 return NULL;
373 if (!FN(MULTI(BASE),has_tuple_id)(multi, type))
374 return multi;
376 multi = FN(MULTI(BASE),cow)(multi);
377 if (!multi)
378 return NULL;
380 space = FN(MULTI(BASE),get_space)(multi);
381 space = isl_space_reset_tuple_id(space, type);
383 return FN(MULTI(BASE),reset_space)(multi, space);
386 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
387 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
389 int i;
391 multi = FN(MULTI(BASE),cow)(multi);
392 if (!multi || !exp)
393 goto error;
395 for (i = 0; i < multi->n; ++i) {
396 multi->p[i] = FN(EL,realign_domain)(multi->p[i],
397 isl_reordering_copy(exp));
398 if (!multi->p[i])
399 goto error;
402 multi = FN(MULTI(BASE),reset_domain_space)(multi,
403 isl_space_copy(exp->dim));
405 isl_reordering_free(exp);
406 return multi;
407 error:
408 isl_reordering_free(exp);
409 FN(MULTI(BASE),free)(multi);
410 return NULL;
413 /* Align the parameters of "multi" to those of "model".
415 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
416 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
418 isl_ctx *ctx;
420 if (!multi || !model)
421 goto error;
423 ctx = isl_space_get_ctx(model);
424 if (!isl_space_has_named_params(model))
425 isl_die(ctx, isl_error_invalid,
426 "model has unnamed parameters", goto error);
427 if (!isl_space_has_named_params(multi->space))
428 isl_die(ctx, isl_error_invalid,
429 "input has unnamed parameters", goto error);
430 if (!isl_space_match(multi->space, isl_dim_param,
431 model, isl_dim_param)) {
432 isl_reordering *exp;
434 model = isl_space_params(model);
435 exp = isl_parameter_alignment_reordering(multi->space, model);
436 exp = isl_reordering_extend_space(exp,
437 FN(MULTI(BASE),get_domain_space)(multi));
438 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
441 isl_space_free(model);
442 return multi;
443 error:
444 isl_space_free(model);
445 FN(MULTI(BASE),free)(multi);
446 return NULL;
449 #ifndef NO_GIST
450 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_set_and)(
451 __isl_take MULTI(BASE) *multi, __isl_take isl_set *set,
452 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi,
453 __isl_take isl_set *set))
455 isl_ctx *ctx;
457 if (!multi || !set)
458 goto error;
459 if (isl_space_match(multi->space, isl_dim_param,
460 set->dim, isl_dim_param))
461 return fn(multi, set);
462 ctx = FN(MULTI(BASE),get_ctx)(multi);
463 if (!isl_space_has_named_params(multi->space) ||
464 !isl_space_has_named_params(set->dim))
465 isl_die(ctx, isl_error_invalid,
466 "unaligned unnamed parameters", goto error);
467 multi = FN(MULTI(BASE),align_params)(multi, isl_set_get_space(set));
468 set = isl_set_align_params(set, FN(MULTI(BASE),get_space)(multi));
469 return fn(multi, set);
470 error:
471 FN(MULTI(BASE),free)(multi);
472 isl_set_free(set);
473 return NULL;
476 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_aligned)(
477 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
479 int i;
481 multi = FN(MULTI(BASE),cow)(multi);
482 if (!multi || !context)
483 goto error;
485 for (i = 0; i < multi->n; ++i) {
486 multi->p[i] = FN(EL,gist)(multi->p[i], isl_set_copy(context));
487 if (!multi->p[i])
488 goto error;
491 isl_set_free(context);
492 return multi;
493 error:
494 isl_set_free(context);
495 FN(MULTI(BASE),free)(multi);
496 return NULL;
499 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist)(__isl_take MULTI(BASE) *multi,
500 __isl_take isl_set *context)
502 return FN(MULTI(BASE),align_params_multi_set_and)(multi, context,
503 &FN(MULTI(BASE),gist_aligned));
506 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_params)(
507 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
509 isl_space *space = FN(MULTI(BASE),get_domain_space)(multi);
510 isl_set *dom_context = isl_set_universe(space);
511 dom_context = isl_set_intersect_params(dom_context, context);
512 return FN(MULTI(BASE),gist)(multi, dom_context);
514 #endif
516 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
517 __isl_take isl_space *space, __isl_take LIST(EL) *list)
519 int i;
520 int n;
521 isl_ctx *ctx;
522 MULTI(BASE) *multi;
524 if (!space || !list)
525 goto error;
527 ctx = isl_space_get_ctx(space);
528 n = FN(FN(LIST(EL),n),BASE)(list);
529 if (n != isl_space_dim(space, isl_dim_out))
530 isl_die(ctx, isl_error_invalid,
531 "invalid number of elements in list", goto error);
533 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
534 for (i = 0; i < n; ++i) {
535 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
536 FN(FN(LIST(EL),get),BASE)(list, i));
539 isl_space_free(space);
540 FN(LIST(EL),free)(list);
541 return multi;
542 error:
543 isl_space_free(space);
544 FN(LIST(EL),free)(list);
545 return NULL;
548 #ifndef NO_IDENTITY
549 /* Create a multi expression in the given space that maps each
550 * input dimension to the corresponding output dimension.
552 __isl_give MULTI(BASE) *FN(MULTI(BASE),identity)(__isl_take isl_space *space)
554 int i, n;
555 isl_local_space *ls;
556 MULTI(BASE) *multi;
558 if (!space)
559 return NULL;
561 if (isl_space_is_set(space))
562 isl_die(isl_space_get_ctx(space), isl_error_invalid,
563 "expecting map space", goto error);
565 n = isl_space_dim(space, isl_dim_out);
566 if (n != isl_space_dim(space, isl_dim_in))
567 isl_die(isl_space_get_ctx(space), isl_error_invalid,
568 "number of input and output dimensions needs to be "
569 "the same", goto error);
571 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
573 if (!n) {
574 isl_space_free(space);
575 return multi;
578 space = isl_space_domain(space);
579 ls = isl_local_space_from_space(space);
581 for (i = 0; i < n; ++i) {
582 EL *el;
583 el = FN(EL,var_on_domain)(isl_local_space_copy(ls),
584 isl_dim_set, i);
585 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i, el);
588 isl_local_space_free(ls);
590 return multi;
591 error:
592 isl_space_free(space);
593 return NULL;
595 #endif
597 /* Construct a multi expression in the given space with value zero in
598 * each of the output dimensions.
600 __isl_give MULTI(BASE) *FN(MULTI(BASE),zero)(__isl_take isl_space *space)
602 int n;
603 MULTI(BASE) *multi;
605 if (!space)
606 return NULL;
608 n = isl_space_dim(space , isl_dim_out);
609 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
611 if (!n)
612 isl_space_free(space);
613 else {
614 int i;
615 isl_local_space *ls;
616 EL *el;
618 space = isl_space_domain(space);
619 ls = isl_local_space_from_space(space);
620 el = FN(EL,zero_on_domain)(ls);
622 for (i = 0; i < n; ++i)
623 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
624 FN(EL,copy)(el));
626 FN(EL,free)(el);
629 return multi;
632 #ifndef NO_FROM_BASE
633 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),BASE)(__isl_take EL *el)
635 MULTI(BASE) *multi;
637 multi = FN(MULTI(BASE),alloc)(FN(EL,get_space)(el));
638 multi = FN(FN(MULTI(BASE),set),BASE)(multi, 0, el);
640 return multi;
642 #endif
644 __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
645 __isl_take MULTI(BASE) *multi,
646 enum isl_dim_type type, unsigned first, unsigned n)
648 int i;
649 unsigned dim;
651 multi = FN(MULTI(BASE),cow)(multi);
652 if (!multi)
653 return NULL;
655 dim = FN(MULTI(BASE),dim)(multi, type);
656 if (first + n > dim || first + n < first)
657 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
658 "index out of bounds",
659 return FN(MULTI(BASE),cow)(multi));
661 multi->space = isl_space_drop_dims(multi->space, type, first, n);
662 if (!multi->space)
663 return FN(MULTI(BASE),cow)(multi);
665 if (type == isl_dim_out) {
666 for (i = 0; i < n; ++i)
667 FN(EL,free)(multi->p[first + i]);
668 for (i = first; i + n < multi->n; ++i)
669 multi->p[i] = multi->p[i + n];
670 multi->n -= n;
672 return multi;
675 for (i = 0; i < multi->n; ++i) {
676 multi->p[i] = FN(EL,drop_dims)(multi->p[i], type, first, n);
677 if (!multi->p[i])
678 return FN(MULTI(BASE),cow)(multi);
681 return multi;
684 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
686 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_multi_and)(
687 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
688 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi1,
689 __isl_take MULTI(BASE) *multi2))
691 isl_ctx *ctx;
693 if (!multi1 || !multi2)
694 goto error;
695 if (isl_space_match(multi1->space, isl_dim_param,
696 multi2->space, isl_dim_param))
697 return fn(multi1, multi2);
698 ctx = FN(MULTI(BASE),get_ctx)(multi1);
699 if (!isl_space_has_named_params(multi1->space) ||
700 !isl_space_has_named_params(multi2->space))
701 isl_die(ctx, isl_error_invalid,
702 "unaligned unnamed parameters", goto error);
703 multi1 = FN(MULTI(BASE),align_params)(multi1,
704 FN(MULTI(BASE),get_space)(multi2));
705 multi2 = FN(MULTI(BASE),align_params)(multi2,
706 FN(MULTI(BASE),get_space)(multi1));
707 return fn(multi1, multi2);
708 error:
709 FN(MULTI(BASE),free)(multi1);
710 FN(MULTI(BASE),free)(multi2);
711 return NULL;
714 /* Given two MULTI(BASE)s A -> B and C -> D,
715 * construct a MULTI(BASE) (A * C) -> (B, D).
717 * The parameters are assumed to have been aligned.
719 static __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product_aligned)(
720 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
722 int i, n1, n2;
723 EL *el;
724 isl_space *space;
725 MULTI(BASE) *res;
727 if (!multi1 || !multi2)
728 goto error;
730 space = isl_space_range_product(FN(MULTI(BASE),get_space)(multi1),
731 FN(MULTI(BASE),get_space)(multi2));
732 res = FN(MULTI(BASE),alloc)(space);
734 n1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
735 n2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
737 for (i = 0; i < n1; ++i) {
738 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
739 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
742 for (i = 0; i < n2; ++i) {
743 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
744 res = FN(FN(MULTI(BASE),set),BASE)(res, n1 + i, el);
747 FN(MULTI(BASE),free)(multi1);
748 FN(MULTI(BASE),free)(multi2);
749 return res;
750 error:
751 FN(MULTI(BASE),free)(multi1);
752 FN(MULTI(BASE),free)(multi2);
753 return NULL;
756 /* Given two MULTI(BASE)s A -> B and C -> D,
757 * construct a MULTI(BASE) (A * C) -> (B, D).
759 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product)(
760 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
762 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
763 &FN(MULTI(BASE),range_product_aligned));
766 __isl_give MULTI(BASE) *FN(MULTI(BASE),flatten_range)(
767 __isl_take MULTI(BASE) *multi)
769 if (!multi)
770 return NULL;
772 if (!multi->space->nested[1])
773 return multi;
775 multi = FN(MULTI(BASE),cow)(multi);
776 if (!multi)
777 return NULL;
779 multi->space = isl_space_flatten_range(multi->space);
780 if (!multi->space)
781 return FN(MULTI(BASE),free)(multi);
783 return multi;
786 /* Given two MULTI(BASE)s A -> B and C -> D,
787 * construct a MULTI(BASE) (A * C) -> [B -> D].
789 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
790 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
792 MULTI(BASE) *multi;
794 multi = FN(MULTI(BASE),range_product)(multi1, multi2);
795 multi = FN(MULTI(BASE),flatten_range)(multi);
796 return multi;
799 /* Given two multi expressions, "multi1"
801 * [A] -> [B1 B2]
803 * where B2 starts at position "pos", and "multi2"
805 * [A] -> [D]
807 * return the multi expression
809 * [A] -> [B1 D B2]
811 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_splice)(
812 __isl_take MULTI(BASE) *multi1, unsigned pos,
813 __isl_take MULTI(BASE) *multi2)
815 MULTI(BASE) *res;
816 unsigned dim;
818 if (!multi1 || !multi2)
819 goto error;
821 dim = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
822 if (pos > dim)
823 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
824 "index out of bounds", goto error);
826 res = FN(MULTI(BASE),copy)(multi1);
827 res = FN(MULTI(BASE),drop_dims)(res, isl_dim_out, pos, dim - pos);
828 multi1 = FN(MULTI(BASE),drop_dims)(multi1, isl_dim_out, 0, pos);
830 res = FN(MULTI(BASE),flat_range_product)(res, multi2);
831 res = FN(MULTI(BASE),flat_range_product)(res, multi1);
833 return res;
834 error:
835 FN(MULTI(BASE),free)(multi1);
836 FN(MULTI(BASE),free)(multi2);
837 return NULL;
840 /* Given two multi expressions, "multi1"
842 * [A1 A2] -> [B1 B2]
844 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
845 * and "multi2"
847 * [C] -> [D]
849 * return the multi expression
851 * [A1 C A2] -> [B1 D B2]
853 * We first insert input dimensions to obtain
855 * [A1 C A2] -> [B1 B2]
857 * and
859 * [A1 C A2] -> [D]
861 * and then apply range_splice.
863 __isl_give MULTI(BASE) *FN(MULTI(BASE),splice)(
864 __isl_take MULTI(BASE) *multi1, unsigned in_pos, unsigned out_pos,
865 __isl_take MULTI(BASE) *multi2)
867 unsigned n_in1;
868 unsigned n_in2;
870 if (!multi1 || !multi2)
871 goto error;
873 n_in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
874 if (in_pos > n_in1)
875 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
876 "index out of bounds", goto error);
878 n_in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
880 multi1 = FN(MULTI(BASE),insert_dims)(multi1, isl_dim_in, in_pos, n_in2);
881 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, n_in2,
882 n_in1 - in_pos);
883 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, 0, in_pos);
885 return FN(MULTI(BASE),range_splice)(multi1, out_pos, multi2);
886 error:
887 FN(MULTI(BASE),free)(multi1);
888 FN(MULTI(BASE),free)(multi2);
889 return NULL;
892 /* This function is currently only used from isl_aff.c
894 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
895 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
896 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
897 __attribute__ ((unused));
899 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
900 * return the result.
902 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
903 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
904 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
906 int i;
907 isl_ctx *ctx;
909 multi1 = FN(MULTI(BASE),cow)(multi1);
910 if (!multi1 || !multi2)
911 goto error;
913 ctx = FN(MULTI(BASE),get_ctx)(multi1);
914 if (!isl_space_is_equal(multi1->space, multi2->space))
915 isl_die(ctx, isl_error_invalid,
916 "spaces don't match", goto error);
918 for (i = 0; i < multi1->n; ++i) {
919 multi1->p[i] = fn(multi1->p[i], FN(EL,copy)(multi2->p[i]));
920 if (!multi1->p[i])
921 goto error;
924 FN(MULTI(BASE),free)(multi2);
925 return multi1;
926 error:
927 FN(MULTI(BASE),free)(multi1);
928 FN(MULTI(BASE),free)(multi2);
929 return NULL;
932 /* Multiply the elements of "multi" by "v" and return the result.
934 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_val)(__isl_take MULTI(BASE) *multi,
935 __isl_take isl_val *v)
937 int i;
939 if (!multi || !v)
940 goto error;
942 if (isl_val_is_one(v)) {
943 isl_val_free(v);
944 return multi;
947 if (!isl_val_is_rat(v))
948 isl_die(isl_val_get_ctx(v), isl_error_invalid,
949 "expecting rational factor", goto error);
951 multi = FN(MULTI(BASE),cow)(multi);
952 if (!multi)
953 return NULL;
955 for (i = 0; i < multi->n; ++i) {
956 multi->p[i] = FN(EL,scale_val)(multi->p[i], isl_val_copy(v));
957 if (!multi->p[i])
958 goto error;
961 isl_val_free(v);
962 return multi;
963 error:
964 isl_val_free(v);
965 return FN(MULTI(BASE),free)(multi);
968 /* Multiply the elements of "multi" by the corresponding element of "mv"
969 * and return the result.
971 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_multi_val)(
972 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
974 int i;
976 if (!multi || !mv)
977 goto error;
979 if (!isl_space_tuple_match(multi->space, isl_dim_out,
980 mv->space, isl_dim_set))
981 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
982 "spaces don't match", goto error);
984 multi = FN(MULTI(BASE),cow)(multi);
985 if (!multi)
986 return NULL;
988 for (i = 0; i < multi->n; ++i) {
989 isl_val *v;
991 v = isl_multi_val_get_val(mv, i);
992 multi->p[i] = FN(EL,scale_val)(multi->p[i], v);
993 if (!multi->p[i])
994 goto error;
997 isl_multi_val_free(mv);
998 return multi;
999 error:
1000 isl_multi_val_free(mv);
1001 return FN(MULTI(BASE),free)(multi);
1004 #ifndef NO_MOVE_DIMS
1005 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1006 * to dimensions of "dst_type" at "dst_pos".
1008 * We only support moving input dimensions to parameters and vice versa.
1010 __isl_give MULTI(BASE) *FN(MULTI(BASE),move_dims)(__isl_take MULTI(BASE) *multi,
1011 enum isl_dim_type dst_type, unsigned dst_pos,
1012 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1014 int i;
1016 if (!multi)
1017 return NULL;
1019 if (n == 0 &&
1020 !isl_space_is_named_or_nested(multi->space, src_type) &&
1021 !isl_space_is_named_or_nested(multi->space, dst_type))
1022 return multi;
1024 if (dst_type == isl_dim_out || src_type == isl_dim_out)
1025 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1026 "cannot move output/set dimension",
1027 return FN(MULTI(BASE),free)(multi));
1028 if (dst_type == isl_dim_div || src_type == isl_dim_div)
1029 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1030 "cannot move divs",
1031 return FN(MULTI(BASE),free)(multi));
1032 if (src_pos + n > isl_space_dim(multi->space, src_type))
1033 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1034 "range out of bounds",
1035 return FN(MULTI(BASE),free)(multi));
1036 if (dst_type == src_type)
1037 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_unsupported,
1038 "moving dims within the same type not supported",
1039 return FN(MULTI(BASE),free)(multi));
1041 multi = FN(MULTI(BASE),cow)(multi);
1042 if (!multi)
1043 return NULL;
1045 multi->space = isl_space_move_dims(multi->space, dst_type, dst_pos,
1046 src_type, src_pos, n);
1047 if (!multi->space)
1048 return FN(MULTI(BASE),free)(multi);
1050 for (i = 0; i < multi->n; ++i) {
1051 multi->p[i] = FN(EL,move_dims)(multi->p[i], dst_type, dst_pos,
1052 src_type, src_pos, n);
1053 if (!multi->p[i])
1054 return FN(MULTI(BASE),free)(multi);
1057 return multi;
1059 #endif