declare isl_multi_pw_aff_gist_params
[isl.git] / isl_multi_templ.c
blob020983e027ccbc62062037ecfbc42a64b5f8e962
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 /* Return the position of the first dimension of "type" with id "id".
175 * Return -1 if there is no such dimension.
177 int FN(MULTI(BASE),find_dim_by_id)(__isl_keep MULTI(BASE) *multi,
178 enum isl_dim_type type, __isl_keep isl_id *id)
180 if (!multi)
181 return -1;
182 return isl_space_find_dim_by_id(multi->space, type, id);
185 /* Return the id of the given dimension.
187 __isl_give isl_id *FN(MULTI(BASE),get_dim_id)(__isl_keep MULTI(BASE) *multi,
188 enum isl_dim_type type, unsigned pos)
190 return multi ? isl_space_get_dim_id(multi->space, type, pos) : NULL;
193 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_name)(
194 __isl_take MULTI(BASE) *multi,
195 enum isl_dim_type type, unsigned pos, const char *s)
197 int i;
199 multi = FN(MULTI(BASE),cow)(multi);
200 if (!multi)
201 return NULL;
203 multi->space = isl_space_set_dim_name(multi->space, type, pos, s);
204 if (!multi->space)
205 return FN(MULTI(BASE),free)(multi);
207 if (type == isl_dim_out)
208 return multi;
209 for (i = 0; i < multi->n; ++i) {
210 multi->p[i] = FN(EL,set_dim_name)(multi->p[i], type, pos, s);
211 if (!multi->p[i])
212 return FN(MULTI(BASE),free)(multi);
215 return multi;
218 const char *FN(MULTI(BASE),get_tuple_name)(__isl_keep MULTI(BASE) *multi,
219 enum isl_dim_type type)
221 return multi ? isl_space_get_tuple_name(multi->space, type) : NULL;
224 /* Does the specified tuple have an id?
226 int FN(MULTI(BASE),has_tuple_id)(__isl_keep MULTI(BASE) *multi,
227 enum isl_dim_type type)
229 return multi ? isl_space_has_tuple_id(multi->space, type) : -1;
232 /* Return the id of the specified tuple.
234 __isl_give isl_id *FN(MULTI(BASE),get_tuple_id)(__isl_keep MULTI(BASE) *multi,
235 enum isl_dim_type type)
237 return multi ? isl_space_get_tuple_id(multi->space, type) : NULL;
240 __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
241 int pos)
243 isl_ctx *ctx;
245 if (!multi)
246 return NULL;
247 ctx = FN(MULTI(BASE),get_ctx)(multi);
248 if (pos < 0 || pos >= multi->n)
249 isl_die(ctx, isl_error_invalid,
250 "index out of bounds", return NULL);
251 return FN(EL,copy)(multi->p[pos]);
254 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
255 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
257 isl_space *multi_space = NULL;
258 isl_space *el_space = NULL;
259 int match;
261 multi = FN(MULTI(BASE),cow)(multi);
262 if (!multi || !el)
263 goto error;
265 multi_space = FN(MULTI(BASE),get_space)(multi);
266 match = FN(EL,matching_params)(el, multi_space);
267 if (match < 0)
268 goto error;
269 if (!match) {
270 multi = FN(MULTI(BASE),align_params)(multi,
271 FN(EL,get_space)(el));
272 isl_space_free(multi_space);
273 multi_space = FN(MULTI(BASE),get_space)(multi);
274 el = FN(EL,align_params)(el, isl_space_copy(multi_space));
276 if (FN(EL,check_match_domain_space)(el, multi_space) < 0)
277 goto error;
279 if (pos < 0 || pos >= multi->n)
280 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
281 "index out of bounds", goto error);
283 FN(EL,free)(multi->p[pos]);
284 multi->p[pos] = el;
286 isl_space_free(multi_space);
287 isl_space_free(el_space);
289 return multi;
290 error:
291 FN(MULTI(BASE),free)(multi);
292 FN(EL,free)(el);
293 isl_space_free(multi_space);
294 isl_space_free(el_space);
295 return NULL;
298 /* Reset the space of "multi". This function is called from isl_pw_templ.c
299 * and doesn't know if the space of an element object is represented
300 * directly or through its domain. It therefore passes along both,
301 * which we pass along to the element function since we don't how
302 * that is represented either.
304 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
305 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
306 __isl_take isl_space *domain)
308 int i;
310 multi = FN(MULTI(BASE),cow)(multi);
311 if (!multi || !space || !domain)
312 goto error;
314 for (i = 0; i < multi->n; ++i) {
315 multi->p[i] = FN(EL,reset_domain_space)(multi->p[i],
316 isl_space_copy(domain));
317 if (!multi->p[i])
318 goto error;
320 isl_space_free(domain);
321 isl_space_free(multi->space);
322 multi->space = space;
324 return multi;
325 error:
326 isl_space_free(domain);
327 isl_space_free(space);
328 FN(MULTI(BASE),free)(multi);
329 return NULL;
332 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
333 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
335 isl_space *space;
337 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
338 isl_space_copy(multi->space));
339 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
342 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
343 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
345 isl_space *domain;
347 domain = isl_space_domain(isl_space_copy(space));
348 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
351 /* Set the id of the given dimension of "multi" to "id".
353 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_id)(
354 __isl_take MULTI(BASE) *multi,
355 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
357 isl_space *space;
359 multi = FN(MULTI(BASE),cow)(multi);
360 if (!multi || !id)
361 goto error;
363 space = FN(MULTI(BASE),get_space)(multi);
364 space = isl_space_set_dim_id(space, type, pos, id);
366 return FN(MULTI(BASE),reset_space)(multi, space);
367 error:
368 isl_id_free(id);
369 FN(MULTI(BASE),free)(multi);
370 return NULL;
373 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_name)(
374 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
375 const char *s)
377 isl_space *space;
379 multi = FN(MULTI(BASE),cow)(multi);
380 if (!multi)
381 return NULL;
383 space = FN(MULTI(BASE),get_space)(multi);
384 space = isl_space_set_tuple_name(space, type, s);
386 return FN(MULTI(BASE),reset_space)(multi, space);
389 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_id)(
390 __isl_take MULTI(BASE) *multi, enum isl_dim_type type,
391 __isl_take isl_id *id)
393 isl_space *space;
395 multi = FN(MULTI(BASE),cow)(multi);
396 if (!multi)
397 return isl_id_free(id);
399 space = FN(MULTI(BASE),get_space)(multi);
400 space = isl_space_set_tuple_id(space, type, id);
402 return FN(MULTI(BASE),reset_space)(multi, space);
405 /* Drop the id on the specified tuple.
407 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_tuple_id)(
408 __isl_take MULTI(BASE) *multi, enum isl_dim_type type)
410 isl_space *space;
412 if (!multi)
413 return NULL;
414 if (!FN(MULTI(BASE),has_tuple_id)(multi, type))
415 return multi;
417 multi = FN(MULTI(BASE),cow)(multi);
418 if (!multi)
419 return NULL;
421 space = FN(MULTI(BASE),get_space)(multi);
422 space = isl_space_reset_tuple_id(space, type);
424 return FN(MULTI(BASE),reset_space)(multi, space);
427 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
428 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
430 int i;
432 multi = FN(MULTI(BASE),cow)(multi);
433 if (!multi || !exp)
434 goto error;
436 for (i = 0; i < multi->n; ++i) {
437 multi->p[i] = FN(EL,realign_domain)(multi->p[i],
438 isl_reordering_copy(exp));
439 if (!multi->p[i])
440 goto error;
443 multi = FN(MULTI(BASE),reset_domain_space)(multi,
444 isl_space_copy(exp->dim));
446 isl_reordering_free(exp);
447 return multi;
448 error:
449 isl_reordering_free(exp);
450 FN(MULTI(BASE),free)(multi);
451 return NULL;
454 /* Align the parameters of "multi" to those of "model".
456 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
457 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
459 isl_ctx *ctx;
461 if (!multi || !model)
462 goto error;
464 ctx = isl_space_get_ctx(model);
465 if (!isl_space_has_named_params(model))
466 isl_die(ctx, isl_error_invalid,
467 "model has unnamed parameters", goto error);
468 if (!isl_space_has_named_params(multi->space))
469 isl_die(ctx, isl_error_invalid,
470 "input has unnamed parameters", goto error);
471 if (!isl_space_match(multi->space, isl_dim_param,
472 model, isl_dim_param)) {
473 isl_reordering *exp;
475 model = isl_space_params(model);
476 exp = isl_parameter_alignment_reordering(multi->space, model);
477 exp = isl_reordering_extend_space(exp,
478 FN(MULTI(BASE),get_domain_space)(multi));
479 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
482 isl_space_free(model);
483 return multi;
484 error:
485 isl_space_free(model);
486 FN(MULTI(BASE),free)(multi);
487 return NULL;
490 #ifndef NO_GIST
491 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_set_and)(
492 __isl_take MULTI(BASE) *multi, __isl_take isl_set *set,
493 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi,
494 __isl_take isl_set *set))
496 isl_ctx *ctx;
498 if (!multi || !set)
499 goto error;
500 if (isl_space_match(multi->space, isl_dim_param,
501 set->dim, isl_dim_param))
502 return fn(multi, set);
503 ctx = FN(MULTI(BASE),get_ctx)(multi);
504 if (!isl_space_has_named_params(multi->space) ||
505 !isl_space_has_named_params(set->dim))
506 isl_die(ctx, isl_error_invalid,
507 "unaligned unnamed parameters", goto error);
508 multi = FN(MULTI(BASE),align_params)(multi, isl_set_get_space(set));
509 set = isl_set_align_params(set, FN(MULTI(BASE),get_space)(multi));
510 return fn(multi, set);
511 error:
512 FN(MULTI(BASE),free)(multi);
513 isl_set_free(set);
514 return NULL;
517 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_aligned)(
518 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
520 int i;
522 multi = FN(MULTI(BASE),cow)(multi);
523 if (!multi || !context)
524 goto error;
526 for (i = 0; i < multi->n; ++i) {
527 multi->p[i] = FN(EL,gist)(multi->p[i], isl_set_copy(context));
528 if (!multi->p[i])
529 goto error;
532 isl_set_free(context);
533 return multi;
534 error:
535 isl_set_free(context);
536 FN(MULTI(BASE),free)(multi);
537 return NULL;
540 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist)(__isl_take MULTI(BASE) *multi,
541 __isl_take isl_set *context)
543 return FN(MULTI(BASE),align_params_multi_set_and)(multi, context,
544 &FN(MULTI(BASE),gist_aligned));
547 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_params)(
548 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
550 isl_space *space = FN(MULTI(BASE),get_domain_space)(multi);
551 isl_set *dom_context = isl_set_universe(space);
552 dom_context = isl_set_intersect_params(dom_context, context);
553 return FN(MULTI(BASE),gist)(multi, dom_context);
555 #endif
557 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
558 __isl_take isl_space *space, __isl_take LIST(EL) *list)
560 int i;
561 int n;
562 isl_ctx *ctx;
563 MULTI(BASE) *multi;
565 if (!space || !list)
566 goto error;
568 ctx = isl_space_get_ctx(space);
569 n = FN(FN(LIST(EL),n),BASE)(list);
570 if (n != isl_space_dim(space, isl_dim_out))
571 isl_die(ctx, isl_error_invalid,
572 "invalid number of elements in list", goto error);
574 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
575 for (i = 0; i < n; ++i) {
576 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
577 FN(FN(LIST(EL),get),BASE)(list, i));
580 isl_space_free(space);
581 FN(LIST(EL),free)(list);
582 return multi;
583 error:
584 isl_space_free(space);
585 FN(LIST(EL),free)(list);
586 return NULL;
589 #ifndef NO_IDENTITY
590 /* Create a multi expression in the given space that maps each
591 * input dimension to the corresponding output dimension.
593 __isl_give MULTI(BASE) *FN(MULTI(BASE),identity)(__isl_take isl_space *space)
595 int i, n;
596 isl_local_space *ls;
597 MULTI(BASE) *multi;
599 if (!space)
600 return NULL;
602 if (isl_space_is_set(space))
603 isl_die(isl_space_get_ctx(space), isl_error_invalid,
604 "expecting map space", goto error);
606 n = isl_space_dim(space, isl_dim_out);
607 if (n != isl_space_dim(space, isl_dim_in))
608 isl_die(isl_space_get_ctx(space), isl_error_invalid,
609 "number of input and output dimensions needs to be "
610 "the same", goto error);
612 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
614 if (!n) {
615 isl_space_free(space);
616 return multi;
619 space = isl_space_domain(space);
620 ls = isl_local_space_from_space(space);
622 for (i = 0; i < n; ++i) {
623 EL *el;
624 el = FN(EL,var_on_domain)(isl_local_space_copy(ls),
625 isl_dim_set, i);
626 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i, el);
629 isl_local_space_free(ls);
631 return multi;
632 error:
633 isl_space_free(space);
634 return NULL;
636 #endif
638 /* Construct a multi expression in the given space with value zero in
639 * each of the output dimensions.
641 __isl_give MULTI(BASE) *FN(MULTI(BASE),zero)(__isl_take isl_space *space)
643 int n;
644 MULTI(BASE) *multi;
646 if (!space)
647 return NULL;
649 n = isl_space_dim(space , isl_dim_out);
650 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
652 if (!n)
653 isl_space_free(space);
654 else {
655 int i;
656 isl_local_space *ls;
657 EL *el;
659 space = isl_space_domain(space);
660 ls = isl_local_space_from_space(space);
661 el = FN(EL,zero_on_domain)(ls);
663 for (i = 0; i < n; ++i)
664 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
665 FN(EL,copy)(el));
667 FN(EL,free)(el);
670 return multi;
673 #ifndef NO_FROM_BASE
674 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),BASE)(__isl_take EL *el)
676 MULTI(BASE) *multi;
678 multi = FN(MULTI(BASE),alloc)(FN(EL,get_space)(el));
679 multi = FN(FN(MULTI(BASE),set),BASE)(multi, 0, el);
681 return multi;
683 #endif
685 __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
686 __isl_take MULTI(BASE) *multi,
687 enum isl_dim_type type, unsigned first, unsigned n)
689 int i;
690 unsigned dim;
692 multi = FN(MULTI(BASE),cow)(multi);
693 if (!multi)
694 return NULL;
696 dim = FN(MULTI(BASE),dim)(multi, type);
697 if (first + n > dim || first + n < first)
698 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
699 "index out of bounds",
700 return FN(MULTI(BASE),cow)(multi));
702 multi->space = isl_space_drop_dims(multi->space, type, first, n);
703 if (!multi->space)
704 return FN(MULTI(BASE),cow)(multi);
706 if (type == isl_dim_out) {
707 for (i = 0; i < n; ++i)
708 FN(EL,free)(multi->p[first + i]);
709 for (i = first; i + n < multi->n; ++i)
710 multi->p[i] = multi->p[i + n];
711 multi->n -= n;
713 return multi;
716 for (i = 0; i < multi->n; ++i) {
717 multi->p[i] = FN(EL,drop_dims)(multi->p[i], type, first, n);
718 if (!multi->p[i])
719 return FN(MULTI(BASE),cow)(multi);
722 return multi;
725 /* Align the parameters of "multi1" and "multi2" (if needed) and call "fn".
727 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_multi_and)(
728 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
729 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi1,
730 __isl_take MULTI(BASE) *multi2))
732 isl_ctx *ctx;
734 if (!multi1 || !multi2)
735 goto error;
736 if (isl_space_match(multi1->space, isl_dim_param,
737 multi2->space, isl_dim_param))
738 return fn(multi1, multi2);
739 ctx = FN(MULTI(BASE),get_ctx)(multi1);
740 if (!isl_space_has_named_params(multi1->space) ||
741 !isl_space_has_named_params(multi2->space))
742 isl_die(ctx, isl_error_invalid,
743 "unaligned unnamed parameters", goto error);
744 multi1 = FN(MULTI(BASE),align_params)(multi1,
745 FN(MULTI(BASE),get_space)(multi2));
746 multi2 = FN(MULTI(BASE),align_params)(multi2,
747 FN(MULTI(BASE),get_space)(multi1));
748 return fn(multi1, multi2);
749 error:
750 FN(MULTI(BASE),free)(multi1);
751 FN(MULTI(BASE),free)(multi2);
752 return NULL;
755 /* Given two MULTI(BASE)s A -> B and C -> D,
756 * construct a MULTI(BASE) (A * C) -> (B, D).
758 * The parameters are assumed to have been aligned.
760 static __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product_aligned)(
761 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
763 int i, n1, n2;
764 EL *el;
765 isl_space *space;
766 MULTI(BASE) *res;
768 if (!multi1 || !multi2)
769 goto error;
771 space = isl_space_range_product(FN(MULTI(BASE),get_space)(multi1),
772 FN(MULTI(BASE),get_space)(multi2));
773 res = FN(MULTI(BASE),alloc)(space);
775 n1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
776 n2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
778 for (i = 0; i < n1; ++i) {
779 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
780 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
783 for (i = 0; i < n2; ++i) {
784 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
785 res = FN(FN(MULTI(BASE),set),BASE)(res, n1 + i, el);
788 FN(MULTI(BASE),free)(multi1);
789 FN(MULTI(BASE),free)(multi2);
790 return res;
791 error:
792 FN(MULTI(BASE),free)(multi1);
793 FN(MULTI(BASE),free)(multi2);
794 return NULL;
797 /* Given two MULTI(BASE)s A -> B and C -> D,
798 * construct a MULTI(BASE) (A * C) -> (B, D).
800 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_product)(
801 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
803 return FN(MULTI(BASE),align_params_multi_multi_and)(multi1, multi2,
804 &FN(MULTI(BASE),range_product_aligned));
807 __isl_give MULTI(BASE) *FN(MULTI(BASE),flatten_range)(
808 __isl_take MULTI(BASE) *multi)
810 if (!multi)
811 return NULL;
813 if (!multi->space->nested[1])
814 return multi;
816 multi = FN(MULTI(BASE),cow)(multi);
817 if (!multi)
818 return NULL;
820 multi->space = isl_space_flatten_range(multi->space);
821 if (!multi->space)
822 return FN(MULTI(BASE),free)(multi);
824 return multi;
827 /* Given two MULTI(BASE)s A -> B and C -> D,
828 * construct a MULTI(BASE) (A * C) -> [B -> D].
830 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
831 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
833 MULTI(BASE) *multi;
835 multi = FN(MULTI(BASE),range_product)(multi1, multi2);
836 multi = FN(MULTI(BASE),flatten_range)(multi);
837 return multi;
840 /* Given two multi expressions, "multi1"
842 * [A] -> [B1 B2]
844 * where B2 starts at position "pos", and "multi2"
846 * [A] -> [D]
848 * return the multi expression
850 * [A] -> [B1 D B2]
852 __isl_give MULTI(BASE) *FN(MULTI(BASE),range_splice)(
853 __isl_take MULTI(BASE) *multi1, unsigned pos,
854 __isl_take MULTI(BASE) *multi2)
856 MULTI(BASE) *res;
857 unsigned dim;
859 if (!multi1 || !multi2)
860 goto error;
862 dim = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
863 if (pos > dim)
864 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
865 "index out of bounds", goto error);
867 res = FN(MULTI(BASE),copy)(multi1);
868 res = FN(MULTI(BASE),drop_dims)(res, isl_dim_out, pos, dim - pos);
869 multi1 = FN(MULTI(BASE),drop_dims)(multi1, isl_dim_out, 0, pos);
871 res = FN(MULTI(BASE),flat_range_product)(res, multi2);
872 res = FN(MULTI(BASE),flat_range_product)(res, multi1);
874 return res;
875 error:
876 FN(MULTI(BASE),free)(multi1);
877 FN(MULTI(BASE),free)(multi2);
878 return NULL;
881 /* Given two multi expressions, "multi1"
883 * [A1 A2] -> [B1 B2]
885 * where A2 starts at position "in_pos" and B2 starts at position "out_pos",
886 * and "multi2"
888 * [C] -> [D]
890 * return the multi expression
892 * [A1 C A2] -> [B1 D B2]
894 * We first insert input dimensions to obtain
896 * [A1 C A2] -> [B1 B2]
898 * and
900 * [A1 C A2] -> [D]
902 * and then apply range_splice.
904 __isl_give MULTI(BASE) *FN(MULTI(BASE),splice)(
905 __isl_take MULTI(BASE) *multi1, unsigned in_pos, unsigned out_pos,
906 __isl_take MULTI(BASE) *multi2)
908 unsigned n_in1;
909 unsigned n_in2;
911 if (!multi1 || !multi2)
912 goto error;
914 n_in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
915 if (in_pos > n_in1)
916 isl_die(FN(MULTI(BASE),get_ctx)(multi1), isl_error_invalid,
917 "index out of bounds", goto error);
919 n_in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
921 multi1 = FN(MULTI(BASE),insert_dims)(multi1, isl_dim_in, in_pos, n_in2);
922 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, n_in2,
923 n_in1 - in_pos);
924 multi2 = FN(MULTI(BASE),insert_dims)(multi2, isl_dim_in, 0, in_pos);
926 return FN(MULTI(BASE),range_splice)(multi1, out_pos, multi2);
927 error:
928 FN(MULTI(BASE),free)(multi1);
929 FN(MULTI(BASE),free)(multi2);
930 return NULL;
933 /* This function is currently only used from isl_aff.c
935 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
936 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
937 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
938 __attribute__ ((unused));
940 /* Pairwise perform "fn" to the elements of "multi1" and "multi2" and
941 * return the result.
943 static __isl_give MULTI(BASE) *FN(MULTI(BASE),bin_op)(
944 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2,
945 __isl_give EL *(*fn)(__isl_take EL *, __isl_take EL *))
947 int i;
948 isl_ctx *ctx;
950 multi1 = FN(MULTI(BASE),cow)(multi1);
951 if (!multi1 || !multi2)
952 goto error;
954 ctx = FN(MULTI(BASE),get_ctx)(multi1);
955 if (!isl_space_is_equal(multi1->space, multi2->space))
956 isl_die(ctx, isl_error_invalid,
957 "spaces don't match", goto error);
959 for (i = 0; i < multi1->n; ++i) {
960 multi1->p[i] = fn(multi1->p[i], FN(EL,copy)(multi2->p[i]));
961 if (!multi1->p[i])
962 goto error;
965 FN(MULTI(BASE),free)(multi2);
966 return multi1;
967 error:
968 FN(MULTI(BASE),free)(multi1);
969 FN(MULTI(BASE),free)(multi2);
970 return NULL;
973 /* Multiply the elements of "multi" by "v" and return the result.
975 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_val)(__isl_take MULTI(BASE) *multi,
976 __isl_take isl_val *v)
978 int i;
980 if (!multi || !v)
981 goto error;
983 if (isl_val_is_one(v)) {
984 isl_val_free(v);
985 return multi;
988 if (!isl_val_is_rat(v))
989 isl_die(isl_val_get_ctx(v), isl_error_invalid,
990 "expecting rational factor", goto error);
992 multi = FN(MULTI(BASE),cow)(multi);
993 if (!multi)
994 return NULL;
996 for (i = 0; i < multi->n; ++i) {
997 multi->p[i] = FN(EL,scale_val)(multi->p[i], isl_val_copy(v));
998 if (!multi->p[i])
999 goto error;
1002 isl_val_free(v);
1003 return multi;
1004 error:
1005 isl_val_free(v);
1006 return FN(MULTI(BASE),free)(multi);
1009 /* Multiply the elements of "multi" by the corresponding element of "mv"
1010 * and return the result.
1012 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_multi_val)(
1013 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1015 int i;
1017 if (!multi || !mv)
1018 goto error;
1020 if (!isl_space_tuple_match(multi->space, isl_dim_out,
1021 mv->space, isl_dim_set))
1022 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1023 "spaces don't match", goto error);
1025 multi = FN(MULTI(BASE),cow)(multi);
1026 if (!multi)
1027 return NULL;
1029 for (i = 0; i < multi->n; ++i) {
1030 isl_val *v;
1032 v = isl_multi_val_get_val(mv, i);
1033 multi->p[i] = FN(EL,scale_val)(multi->p[i], v);
1034 if (!multi->p[i])
1035 goto error;
1038 isl_multi_val_free(mv);
1039 return multi;
1040 error:
1041 isl_multi_val_free(mv);
1042 return FN(MULTI(BASE),free)(multi);
1045 /* Divide the elements of "multi" by the corresponding element of "mv"
1046 * and return the result.
1048 __isl_give MULTI(BASE) *FN(MULTI(BASE),scale_down_multi_val)(
1049 __isl_take MULTI(BASE) *multi, __isl_take isl_multi_val *mv)
1051 int i;
1053 if (!multi || !mv)
1054 goto error;
1056 if (!isl_space_tuple_match(multi->space, isl_dim_out,
1057 mv->space, isl_dim_set))
1058 isl_die(isl_multi_val_get_ctx(mv), isl_error_invalid,
1059 "spaces don't match", goto error);
1061 multi = FN(MULTI(BASE),cow)(multi);
1062 if (!multi)
1063 return NULL;
1065 for (i = 0; i < multi->n; ++i) {
1066 isl_val *v;
1068 v = isl_multi_val_get_val(mv, i);
1069 multi->p[i] = FN(EL,scale_down_val)(multi->p[i], v);
1070 if (!multi->p[i])
1071 goto error;
1074 isl_multi_val_free(mv);
1075 return multi;
1076 error:
1077 isl_multi_val_free(mv);
1078 return FN(MULTI(BASE),free)(multi);
1081 #ifndef NO_MOVE_DIMS
1082 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
1083 * to dimensions of "dst_type" at "dst_pos".
1085 * We only support moving input dimensions to parameters and vice versa.
1087 __isl_give MULTI(BASE) *FN(MULTI(BASE),move_dims)(__isl_take MULTI(BASE) *multi,
1088 enum isl_dim_type dst_type, unsigned dst_pos,
1089 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1091 int i;
1093 if (!multi)
1094 return NULL;
1096 if (n == 0 &&
1097 !isl_space_is_named_or_nested(multi->space, src_type) &&
1098 !isl_space_is_named_or_nested(multi->space, dst_type))
1099 return multi;
1101 if (dst_type == isl_dim_out || src_type == isl_dim_out)
1102 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1103 "cannot move output/set dimension",
1104 return FN(MULTI(BASE),free)(multi));
1105 if (dst_type == isl_dim_div || src_type == isl_dim_div)
1106 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1107 "cannot move divs",
1108 return FN(MULTI(BASE),free)(multi));
1109 if (src_pos + n > isl_space_dim(multi->space, src_type))
1110 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1111 "range out of bounds",
1112 return FN(MULTI(BASE),free)(multi));
1113 if (dst_type == src_type)
1114 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_unsupported,
1115 "moving dims within the same type not supported",
1116 return FN(MULTI(BASE),free)(multi));
1118 multi = FN(MULTI(BASE),cow)(multi);
1119 if (!multi)
1120 return NULL;
1122 multi->space = isl_space_move_dims(multi->space, dst_type, dst_pos,
1123 src_type, src_pos, n);
1124 if (!multi->space)
1125 return FN(MULTI(BASE),free)(multi);
1127 for (i = 0; i < multi->n; ++i) {
1128 multi->p[i] = FN(EL,move_dims)(multi->p[i], dst_type, dst_pos,
1129 src_type, src_pos, n);
1130 if (!multi->p[i])
1131 return FN(MULTI(BASE),free)(multi);
1134 return multi;
1136 #endif
1138 /* Convert a multiple expression defined over a parameter domain
1139 * into one that is defined over a zero-dimensional set.
1141 __isl_give MULTI(BASE) *FN(MULTI(BASE),from_range)(
1142 __isl_take MULTI(BASE) *multi)
1144 isl_space *space;
1146 if (!multi)
1147 return NULL;
1148 if (!isl_space_is_set(multi->space))
1149 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
1150 "not living in a set space",
1151 return FN(MULTI(BASE),free)(multi));
1153 space = FN(MULTI(BASE),get_space)(multi);
1154 space = isl_space_from_range(space);
1155 multi = FN(MULTI(BASE),reset_space)(multi, space);
1157 return multi;