2 * Copyright 2011 Sven Verdoolaege
3 * Copyright 2012 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
11 #define xCAT(A,B) A ## B
12 #define CAT(A,B) xCAT(A,B)
14 #define EL CAT(isl_,BASE)
15 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
16 #define FN(TYPE,NAME) xFN(TYPE,NAME)
17 #define xMULTI(BASE) isl_multi_ ## BASE
18 #define MULTI(BASE) xMULTI(BASE)
19 #define MULTI_NAME(BASE) "isl_multi_" #BASE
20 #define xLIST(EL) EL ## _list
21 #define LIST(EL) xLIST(EL)
23 isl_ctx *FN(MULTI(BASE),get_ctx)(__isl_keep MULTI(BASE) *multi)
25 return multi ? isl_space_get_ctx(multi->space) : NULL;
28 __isl_give isl_space *FN(MULTI(BASE),get_space)(__isl_keep MULTI(BASE) *multi)
30 return multi ? isl_space_copy(multi->space) : NULL;
33 __isl_give isl_space *FN(MULTI(BASE),get_domain_space)(
34 __isl_keep MULTI(BASE) *multi)
36 return multi ? isl_space_domain(isl_space_copy(multi->space)) : NULL;
39 __isl_give MULTI(BASE) *FN(MULTI(BASE),alloc)(__isl_take isl_space *space)
48 ctx = isl_space_get_ctx(space);
49 n = isl_space_dim(space, isl_dim_out);
50 multi = isl_calloc(ctx, MULTI(BASE),
51 sizeof(MULTI(BASE)) + (n - 1) * sizeof(struct EL *));
60 isl_space_free(space);
64 __isl_give MULTI(BASE) *FN(MULTI(BASE),dup)(__isl_keep MULTI(BASE) *multi)
72 dup = FN(MULTI(BASE),alloc)(isl_space_copy(multi->space));
76 for (i = 0; i < multi->n; ++i)
77 dup = FN(FN(MULTI(BASE),set),BASE)(dup, i,
78 FN(EL,copy)(multi->p[i]));
83 __isl_give MULTI(BASE) *FN(MULTI(BASE),cow)(__isl_take MULTI(BASE) *multi)
92 return FN(MULTI(BASE),dup)(multi);
95 __isl_give MULTI(BASE) *FN(MULTI(BASE),copy)(__isl_keep MULTI(BASE) *multi)
104 void *FN(MULTI(BASE),free)(__isl_take MULTI(BASE) *multi)
111 if (--multi->ref > 0)
114 isl_space_free(multi->space);
115 for (i = 0; i < multi->n; ++i)
116 FN(EL,free)(multi->p[i]);
122 __isl_give MULTI(BASE) *FN(MULTI(BASE),insert_dims)(
123 __isl_take MULTI(BASE) *multi,
124 enum isl_dim_type type, unsigned first, unsigned n)
130 if (type == isl_dim_out)
131 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
132 "cannot insert output/set dimensions",
133 return FN(MULTI(BASE),free)(multi));
134 if (n == 0 && !isl_space_is_named_or_nested(multi->space, type))
137 multi = FN(MULTI(BASE),cow)(multi);
141 multi->space = isl_space_insert_dims(multi->space, type, first, n);
143 return FN(MULTI(BASE),free)(multi);
145 for (i = 0; i < multi->n; ++i) {
146 multi->p[i] = FN(EL,insert_dims)(multi->p[i], type, first, n);
148 return FN(MULTI(BASE),free)(multi);
154 unsigned FN(MULTI(BASE),dim)(__isl_keep MULTI(BASE) *multi,
155 enum isl_dim_type type)
157 return multi ? isl_space_dim(multi->space, type) : 0;
160 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_name)(
161 __isl_take MULTI(BASE) *multi,
162 enum isl_dim_type type, unsigned pos, const char *s)
166 multi = FN(MULTI(BASE),cow)(multi);
170 multi->space = isl_space_set_dim_name(multi->space, type, pos, s);
172 return FN(MULTI(BASE),free)(multi);
174 if (type == isl_dim_out)
176 for (i = 0; i < multi->n; ++i) {
177 multi->p[i] = FN(EL,set_dim_name)(multi->p[i], type, pos, s);
179 return FN(MULTI(BASE),free)(multi);
185 const char *FN(MULTI(BASE),get_tuple_name)(__isl_keep MULTI(BASE) *multi,
186 enum isl_dim_type type)
188 return multi ? isl_space_get_tuple_name(multi->space, type) : NULL;
191 __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
198 ctx = FN(MULTI(BASE),get_ctx)(multi);
199 if (pos < 0 || pos >= multi->n)
200 isl_die(ctx, isl_error_invalid,
201 "index out of bounds", return NULL);
202 return FN(EL,copy)(multi->p[pos]);
205 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
206 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
208 isl_space *multi_space = NULL;
209 isl_space *el_space = NULL;
211 multi = FN(MULTI(BASE),cow)(multi);
215 multi_space = FN(MULTI(BASE),get_space)(multi);
216 el_space = FN(EL,get_space)(el);
218 if (!isl_space_match(multi_space, isl_dim_param,
219 el_space, isl_dim_param))
220 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
221 "parameters don't match", goto error);
222 if (!isl_space_tuple_match(multi_space, isl_dim_in,
223 el_space, isl_dim_in))
224 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
225 "domains don't match", goto error);
227 if (pos < 0 || pos >= multi->n)
228 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
229 "index out of bounds", goto error);
231 FN(EL,free)(multi->p[pos]);
234 isl_space_free(multi_space);
235 isl_space_free(el_space);
239 FN(MULTI(BASE),free)(multi);
241 isl_space_free(multi_space);
242 isl_space_free(el_space);
246 /* Reset the space of "multi". This function is called from isl_pw_templ.c
247 * and doesn't know if the space of an element object is represented
248 * directly or through its domain. It therefore passes along both,
249 * which we pass along to the element function since we don't how
250 * that is represented either.
252 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
253 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
254 __isl_take isl_space *domain)
258 multi = FN(MULTI(BASE),cow)(multi);
259 if (!multi || !space || !domain)
262 for (i = 0; i < multi->n; ++i) {
263 multi->p[i] = FN(EL,reset_domain_space)(multi->p[i],
264 isl_space_copy(domain));
268 isl_space_free(domain);
269 isl_space_free(multi->space);
270 multi->space = space;
274 isl_space_free(domain);
275 isl_space_free(space);
276 FN(MULTI(BASE),free)(multi);
280 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
281 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
285 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
286 isl_space_copy(multi->space));
287 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
290 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
291 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
295 domain = isl_space_domain(isl_space_copy(space));
296 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
299 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_id)(
300 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
301 __isl_take isl_id *id)
305 multi = FN(MULTI(BASE),cow)(multi);
307 return isl_id_free(id);
309 space = FN(MULTI(BASE),get_space)(multi);
310 space = isl_space_set_tuple_id(space, type, id);
312 return FN(MULTI(BASE),reset_space)(multi, space);
315 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
316 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
320 multi = FN(MULTI(BASE),cow)(multi);
324 for (i = 0; i < multi->n; ++i) {
325 multi->p[i] = FN(EL,realign_domain)(multi->p[i],
326 isl_reordering_copy(exp));
331 multi = FN(MULTI(BASE),reset_domain_space)(multi,
332 isl_space_copy(exp->dim));
334 isl_reordering_free(exp);
337 isl_reordering_free(exp);
338 FN(MULTI(BASE),free)(multi);
342 /* Align the parameters of "multi" to those of "model".
344 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
345 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
349 if (!multi || !model)
352 ctx = isl_space_get_ctx(model);
353 if (!isl_space_has_named_params(model))
354 isl_die(ctx, isl_error_invalid,
355 "model has unnamed parameters", goto error);
356 if (!isl_space_has_named_params(multi->space))
357 isl_die(ctx, isl_error_invalid,
358 "input has unnamed parameters", goto error);
359 if (!isl_space_match(multi->space, isl_dim_param,
360 model, isl_dim_param)) {
363 model = isl_space_params(model);
364 exp = isl_parameter_alignment_reordering(multi->space, model);
365 exp = isl_reordering_extend_space(exp,
366 FN(MULTI(BASE),get_domain_space)(multi));
367 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
370 isl_space_free(model);
373 isl_space_free(model);
374 FN(MULTI(BASE),free)(multi);
378 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_set_and)(
379 __isl_take MULTI(BASE) *multi, __isl_take isl_set *set,
380 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi,
381 __isl_take isl_set *set))
387 if (isl_space_match(multi->space, isl_dim_param,
388 set->dim, isl_dim_param))
389 return fn(multi, set);
390 ctx = FN(MULTI(BASE),get_ctx)(multi);
391 if (!isl_space_has_named_params(multi->space) ||
392 !isl_space_has_named_params(set->dim))
393 isl_die(ctx, isl_error_invalid,
394 "unaligned unnamed parameters", goto error);
395 multi = FN(MULTI(BASE),align_params)(multi, isl_set_get_space(set));
396 set = isl_set_align_params(set, FN(MULTI(BASE),get_space)(multi));
397 return fn(multi, set);
399 FN(MULTI(BASE),free)(multi);
404 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_aligned)(
405 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
409 if (!multi || !context)
412 for (i = 0; i < multi->n; ++i) {
413 multi->p[i] = FN(EL,gist)(multi->p[i], isl_set_copy(context));
418 isl_set_free(context);
421 isl_set_free(context);
422 FN(MULTI(BASE),free)(multi);
426 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist)(__isl_take MULTI(BASE) *multi,
427 __isl_take isl_set *context)
429 return FN(MULTI(BASE),align_params_multi_set_and)(multi, context,
430 &FN(MULTI(BASE),gist_aligned));
433 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_params)(
434 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
436 isl_space *space = FN(MULTI(BASE),get_domain_space)(multi);
437 isl_set *dom_context = isl_set_universe(space);
438 dom_context = isl_set_intersect_params(dom_context, context);
439 return FN(MULTI(BASE),gist)(multi, dom_context);
442 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
443 __isl_take isl_space *space, __isl_take LIST(EL) *list)
453 ctx = isl_space_get_ctx(space);
454 n = FN(FN(LIST(EL),n),BASE)(list);
455 if (n != isl_space_dim(space, isl_dim_out))
456 isl_die(ctx, isl_error_invalid,
457 "invalid number of elements in list", goto error);
459 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
460 for (i = 0; i < n; ++i) {
461 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
462 FN(FN(LIST(EL),get),BASE)(list, i));
465 isl_space_free(space);
466 FN(LIST(EL),free)(list);
469 isl_space_free(space);
470 FN(LIST(EL),free)(list);
474 /* Create a multi expression in the given space that maps each
475 * input dimension to the corresponding output dimension.
477 __isl_give MULTI(BASE) *FN(MULTI(BASE),identity)(__isl_take isl_space *space)
486 if (isl_space_is_set(space))
487 isl_die(isl_space_get_ctx(space), isl_error_invalid,
488 "expecting map space", goto error);
490 n = isl_space_dim(space, isl_dim_out);
491 if (n != isl_space_dim(space, isl_dim_in))
492 isl_die(isl_space_get_ctx(space), isl_error_invalid,
493 "number of input and output dimensions needs to be "
494 "the same", goto error);
496 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
499 isl_space_free(space);
503 space = isl_space_domain(space);
504 ls = isl_local_space_from_space(space);
506 for (i = 0; i < n; ++i) {
508 el = FN(EL,var_on_domain)(isl_local_space_copy(ls),
510 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i, el);
513 isl_local_space_free(ls);
517 isl_space_free(space);
521 /* Construct a multi expression in the given space with value zero in
522 * each of the output dimensions.
524 __isl_give MULTI(BASE) *FN(MULTI(BASE),zero)(__isl_take isl_space *space)
532 n = isl_space_dim(space , isl_dim_out);
533 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
536 isl_space_free(space);
542 space = isl_space_domain(space);
543 ls = isl_local_space_from_space(space);
544 el = FN(EL,zero_on_domain)(ls);
546 for (i = 0; i < n; ++i)
547 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
556 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),BASE)(__isl_take EL *el)
560 multi = FN(MULTI(BASE),alloc)(FN(EL,get_space)(el));
561 multi = FN(FN(MULTI(BASE),set),BASE)(multi, 0, el);
566 __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
567 __isl_take MULTI(BASE) *multi,
568 enum isl_dim_type type, unsigned first, unsigned n)
573 multi = FN(MULTI(BASE),cow)(multi);
577 dim = FN(MULTI(BASE),dim)(multi, type);
578 if (first + n > dim || first + n < first)
579 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
580 "index out of bounds",
581 return FN(MULTI(BASE),cow)(multi));
583 multi->space = isl_space_drop_dims(multi->space, type, first, n);
585 return FN(MULTI(BASE),cow)(multi);
587 if (type == isl_dim_out) {
588 for (i = 0; i < n; ++i)
589 FN(EL,free)(multi->p[first + i]);
590 for (i = first; i + n < multi->n; ++i)
591 multi->p[i] = multi->p[i + n];
597 for (i = 0; i < multi->n; ++i) {
598 multi->p[i] = FN(EL,drop_dims)(multi->p[i], type, first, n);
600 return FN(MULTI(BASE),cow)(multi);
606 /* Given two MULTI(BASE)s A -> B and C -> D,
607 * construct a MULTI(BASE) (A * C) -> (B, D).
609 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
610 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
617 if (!multi1 || !multi2)
620 space = isl_space_range_product(FN(MULTI(BASE),get_space)(multi1),
621 FN(MULTI(BASE),get_space)(multi2));
622 space = isl_space_flatten_range(space);
623 res = FN(MULTI(BASE),alloc)(space);
625 n1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
626 n2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
628 for (i = 0; i < n1; ++i) {
629 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
630 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
633 for (i = 0; i < n2; ++i) {
634 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
635 res = FN(FN(MULTI(BASE),set),BASE)(res, n1 + i, el);
638 FN(MULTI(BASE),free)(multi1);
639 FN(MULTI(BASE),free)(multi2);
642 FN(MULTI(BASE),free)(multi1);
643 FN(MULTI(BASE),free)(multi2);