add isl_multi_pw_aff
[isl.git] / isl_multi_templ.c
blob1776b2082ecb33acc0f2fd6700c65ae3cacc7585
1 /*
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
9 */
11 #define xCAT(A,B) A ## B
12 #define CAT(A,B) xCAT(A,B)
13 #undef EL
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)
41 isl_ctx *ctx;
42 int n;
43 MULTI(BASE) *multi;
45 if (!space)
46 return NULL;
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 *));
52 if (!multi)
53 goto error;
55 multi->space = space;
56 multi->n = n;
57 multi->ref = 1;
58 return multi;
59 error:
60 isl_space_free(space);
61 return NULL;
64 __isl_give MULTI(BASE) *FN(MULTI(BASE),dup)(__isl_keep MULTI(BASE) *multi)
66 int i;
67 MULTI(BASE) *dup;
69 if (!multi)
70 return NULL;
72 dup = FN(MULTI(BASE),alloc)(isl_space_copy(multi->space));
73 if (!dup)
74 return NULL;
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]));
80 return dup;
83 __isl_give MULTI(BASE) *FN(MULTI(BASE),cow)(__isl_take MULTI(BASE) *multi)
85 if (!multi)
86 return NULL;
88 if (multi->ref == 1)
89 return multi;
91 multi->ref--;
92 return FN(MULTI(BASE),dup)(multi);
95 __isl_give MULTI(BASE) *FN(MULTI(BASE),copy)(__isl_keep MULTI(BASE) *multi)
97 if (!multi)
98 return NULL;
100 multi->ref++;
101 return multi;
104 void *FN(MULTI(BASE),free)(__isl_take MULTI(BASE) *multi)
106 int i;
108 if (!multi)
109 return NULL;
111 if (--multi->ref > 0)
112 return NULL;
114 isl_space_free(multi->space);
115 for (i = 0; i < multi->n; ++i)
116 FN(EL,free)(multi->p[i]);
117 free(multi);
119 return NULL;
122 unsigned FN(MULTI(BASE),dim)(__isl_keep MULTI(BASE) *multi,
123 enum isl_dim_type type)
125 return multi ? isl_space_dim(multi->space, type) : 0;
128 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_dim_name)(
129 __isl_take MULTI(BASE) *multi,
130 enum isl_dim_type type, unsigned pos, const char *s)
132 int i;
134 multi = FN(MULTI(BASE),cow)(multi);
135 if (!multi)
136 return NULL;
138 multi->space = isl_space_set_dim_name(multi->space, type, pos, s);
139 if (!multi->space)
140 return FN(MULTI(BASE),free)(multi);
142 if (type == isl_dim_out)
143 return multi;
144 for (i = 0; i < multi->n; ++i) {
145 multi->p[i] = FN(EL,set_dim_name)(multi->p[i], type, pos, s);
146 if (!multi->p[i])
147 return FN(MULTI(BASE),free)(multi);
150 return multi;
153 const char *FN(MULTI(BASE),get_tuple_name)(__isl_keep MULTI(BASE) *multi,
154 enum isl_dim_type type)
156 return multi ? isl_space_get_tuple_name(multi->space, type) : NULL;
159 __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
160 int pos)
162 isl_ctx *ctx;
164 if (!multi)
165 return NULL;
166 ctx = FN(MULTI(BASE),get_ctx)(multi);
167 if (pos < 0 || pos >= multi->n)
168 isl_die(ctx, isl_error_invalid,
169 "index out of bounds", return NULL);
170 return FN(EL,copy)(multi->p[pos]);
173 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
174 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
176 isl_space *multi_space = NULL;
177 isl_space *el_space = NULL;
179 multi = FN(MULTI(BASE),cow)(multi);
180 if (!multi || !el)
181 goto error;
183 multi_space = FN(MULTI(BASE),get_space)(multi);
184 el_space = FN(EL,get_space)(el);
186 if (!isl_space_match(multi_space, isl_dim_param,
187 el_space, isl_dim_param))
188 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
189 "parameters don't match", goto error);
190 if (!isl_space_tuple_match(multi_space, isl_dim_in,
191 el_space, isl_dim_in))
192 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
193 "domains don't match", goto error);
195 if (pos < 0 || pos >= multi->n)
196 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
197 "index out of bounds", goto error);
199 FN(EL,free)(multi->p[pos]);
200 multi->p[pos] = el;
202 isl_space_free(multi_space);
203 isl_space_free(el_space);
205 return multi;
206 error:
207 FN(MULTI(BASE),free)(multi);
208 FN(EL,free)(el);
209 isl_space_free(multi_space);
210 isl_space_free(el_space);
211 return NULL;
214 /* Reset the space of "multi". This function is called from isl_pw_templ.c
215 * and doesn't know if the space of an element object is represented
216 * directly or through its domain. It therefore passes along both,
217 * which we pass along to the element function since we don't how
218 * that is represented either.
220 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
221 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
222 __isl_take isl_space *domain)
224 int i;
226 multi = FN(MULTI(BASE),cow)(multi);
227 if (!multi || !space || !domain)
228 goto error;
230 for (i = 0; i < multi->n; ++i) {
231 multi->p[i] = FN(EL,reset_domain_space)(multi->p[i],
232 isl_space_copy(domain));
233 if (!multi->p[i])
234 goto error;
236 isl_space_free(domain);
237 isl_space_free(multi->space);
238 multi->space = space;
240 return multi;
241 error:
242 isl_space_free(domain);
243 isl_space_free(space);
244 FN(MULTI(BASE),free)(multi);
245 return NULL;
248 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
249 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
251 isl_space *space;
253 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
254 isl_space_copy(multi->space));
255 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
258 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space)(
259 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space)
261 isl_space *domain;
263 domain = isl_space_domain(isl_space_copy(space));
264 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
267 __isl_give MULTI(BASE) *FN(MULTI(BASE),set_tuple_id)(
268 __isl_keep MULTI(BASE) *multi, enum isl_dim_type type,
269 __isl_take isl_id *id)
271 isl_space *space;
273 multi = FN(MULTI(BASE),cow)(multi);
274 if (!multi)
275 return isl_id_free(id);
277 space = FN(MULTI(BASE),get_space)(multi);
278 space = isl_space_set_tuple_id(space, type, id);
280 return FN(MULTI(BASE),reset_space)(multi, space);
283 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
284 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
286 int i;
288 multi = FN(MULTI(BASE),cow)(multi);
289 if (!multi || !exp)
290 return NULL;
292 for (i = 0; i < multi->n; ++i) {
293 multi->p[i] = FN(EL,realign_domain)(multi->p[i],
294 isl_reordering_copy(exp));
295 if (!multi->p[i])
296 goto error;
299 multi = FN(MULTI(BASE),reset_domain_space)(multi,
300 isl_space_copy(exp->dim));
302 isl_reordering_free(exp);
303 return multi;
304 error:
305 isl_reordering_free(exp);
306 FN(MULTI(BASE),free)(multi);
307 return NULL;
310 /* Align the parameters of "multi" to those of "model".
312 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
313 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
315 isl_ctx *ctx;
317 if (!multi || !model)
318 goto error;
320 ctx = isl_space_get_ctx(model);
321 if (!isl_space_has_named_params(model))
322 isl_die(ctx, isl_error_invalid,
323 "model has unnamed parameters", goto error);
324 if (!isl_space_has_named_params(multi->space))
325 isl_die(ctx, isl_error_invalid,
326 "input has unnamed parameters", goto error);
327 if (!isl_space_match(multi->space, isl_dim_param,
328 model, isl_dim_param)) {
329 isl_reordering *exp;
331 model = isl_space_params(model);
332 exp = isl_parameter_alignment_reordering(multi->space, model);
333 exp = isl_reordering_extend_space(exp,
334 FN(MULTI(BASE),get_domain_space)(multi));
335 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
338 isl_space_free(model);
339 return multi;
340 error:
341 isl_space_free(model);
342 FN(MULTI(BASE),free)(multi);
343 return NULL;
346 static __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params_multi_set_and)(
347 __isl_take MULTI(BASE) *multi, __isl_take isl_set *set,
348 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi,
349 __isl_take isl_set *set))
351 isl_ctx *ctx;
353 if (!multi || !set)
354 goto error;
355 if (isl_space_match(multi->space, isl_dim_param,
356 set->dim, isl_dim_param))
357 return fn(multi, set);
358 ctx = FN(MULTI(BASE),get_ctx)(multi);
359 if (!isl_space_has_named_params(multi->space) ||
360 !isl_space_has_named_params(set->dim))
361 isl_die(ctx, isl_error_invalid,
362 "unaligned unnamed parameters", goto error);
363 multi = FN(MULTI(BASE),align_params)(multi, isl_set_get_space(set));
364 set = isl_set_align_params(set, FN(MULTI(BASE),get_space)(multi));
365 return fn(multi, set);
366 error:
367 FN(MULTI(BASE),free)(multi);
368 isl_set_free(set);
369 return NULL;
372 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_aligned)(
373 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
375 int i;
377 if (!multi || !context)
378 goto error;
380 for (i = 0; i < multi->n; ++i) {
381 multi->p[i] = FN(EL,gist)(multi->p[i], isl_set_copy(context));
382 if (!multi->p[i])
383 goto error;
386 isl_set_free(context);
387 return multi;
388 error:
389 isl_set_free(context);
390 FN(MULTI(BASE),free)(multi);
391 return NULL;
394 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist)(__isl_take MULTI(BASE) *multi,
395 __isl_take isl_set *context)
397 return FN(MULTI(BASE),align_params_multi_set_and)(multi, context,
398 &FN(MULTI(BASE),gist_aligned));
401 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_params)(
402 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
404 isl_space *space = FN(MULTI(BASE),get_domain_space)(multi);
405 isl_set *dom_context = isl_set_universe(space);
406 dom_context = isl_set_intersect_params(dom_context, context);
407 return FN(MULTI(BASE),gist)(multi, dom_context);
410 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
411 __isl_take isl_space *space, __isl_take LIST(EL) *list)
413 int i;
414 int n;
415 isl_ctx *ctx;
416 MULTI(BASE) *multi;
418 if (!space || !list)
419 goto error;
421 ctx = isl_space_get_ctx(space);
422 n = FN(FN(LIST(EL),n),BASE)(list);
423 if (n != isl_space_dim(space, isl_dim_out))
424 isl_die(ctx, isl_error_invalid,
425 "invalid number of elements in list", goto error);
427 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
428 for (i = 0; i < n; ++i) {
429 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
430 FN(FN(LIST(EL),get),BASE)(list, i));
433 isl_space_free(space);
434 FN(LIST(EL),free)(list);
435 return multi;
436 error:
437 isl_space_free(space);
438 FN(LIST(EL),free)(list);
439 return NULL;
442 /* Create a multi expression in the given space that maps each
443 * input dimension to the corresponding output dimension.
445 __isl_give MULTI(BASE) *FN(MULTI(BASE),identity)(__isl_take isl_space *space)
447 int i, n;
448 isl_local_space *ls;
449 MULTI(BASE) *multi;
451 if (!space)
452 return NULL;
454 if (isl_space_is_set(space))
455 isl_die(isl_space_get_ctx(space), isl_error_invalid,
456 "expecting map space", goto error);
458 n = isl_space_dim(space, isl_dim_out);
459 if (n != isl_space_dim(space, isl_dim_in))
460 isl_die(isl_space_get_ctx(space), isl_error_invalid,
461 "number of input and output dimensions needs to be "
462 "the same", goto error);
464 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
466 if (!n) {
467 isl_space_free(space);
468 return multi;
471 space = isl_space_domain(space);
472 ls = isl_local_space_from_space(space);
474 for (i = 0; i < n; ++i) {
475 EL *el;
476 el = FN(EL,var_on_domain)(isl_local_space_copy(ls),
477 isl_dim_set, i);
478 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i, el);
481 isl_local_space_free(ls);
483 return multi;
484 error:
485 isl_space_free(space);
486 return NULL;
489 /* Construct a multi expression in the given space with value zero in
490 * each of the output dimensions.
492 __isl_give MULTI(BASE) *FN(MULTI(BASE),zero)(__isl_take isl_space *space)
494 int n;
495 MULTI(BASE) *multi;
497 if (!space)
498 return NULL;
500 n = isl_space_dim(space , isl_dim_out);
501 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
503 if (!n)
504 isl_space_free(space);
505 else {
506 int i;
507 isl_local_space *ls;
508 EL *el;
510 space = isl_space_domain(space);
511 ls = isl_local_space_from_space(space);
512 el = FN(EL,zero_on_domain)(ls);
514 for (i = 0; i < n; ++i)
515 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
516 FN(EL,copy)(el));
518 FN(EL,free)(el);
521 return multi;
524 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),BASE)(__isl_take EL *el)
526 MULTI(BASE) *multi;
528 multi = FN(MULTI(BASE),alloc)(FN(EL,get_space)(el));
529 multi = FN(FN(MULTI(BASE),set),BASE)(multi, 0, el);
531 return multi;
534 __isl_give MULTI(BASE) *FN(MULTI(BASE),drop_dims)(
535 __isl_take MULTI(BASE) *multi,
536 enum isl_dim_type type, unsigned first, unsigned n)
538 int i;
539 unsigned dim;
541 multi = FN(MULTI(BASE),cow)(multi);
542 if (!multi)
543 return NULL;
545 dim = FN(MULTI(BASE),dim)(multi, type);
546 if (first + n > dim || first + n < first)
547 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
548 "index out of bounds",
549 return FN(MULTI(BASE),cow)(multi));
551 multi->space = isl_space_drop_dims(multi->space, type, first, n);
552 if (!multi->space)
553 return FN(MULTI(BASE),cow)(multi);
555 if (type == isl_dim_out) {
556 for (i = 0; i < n; ++i)
557 FN(EL,free)(multi->p[first + i]);
558 for (i = first; i + n < multi->n; ++i)
559 multi->p[i] = multi->p[i + n];
560 multi->n -= n;
562 return multi;
565 for (i = 0; i < multi->n; ++i) {
566 multi->p[i] = FN(EL,drop_dims)(multi->p[i], type, first, n);
567 if (!multi->p[i])
568 return FN(MULTI(BASE),cow)(multi);
571 return multi;
574 /* Given two MULTI(BASE)s A -> B and C -> D,
575 * construct a MULTI(BASE) (A * C) -> (B, D).
577 __isl_give MULTI(BASE) *FN(MULTI(BASE),flat_range_product)(
578 __isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
580 int i, n1, n2;
581 EL *el;
582 isl_space *space;
583 MULTI(BASE) *res;
585 if (!multi1 || !multi2)
586 goto error;
588 space = isl_space_range_product(FN(MULTI(BASE),get_space)(multi1),
589 FN(MULTI(BASE),get_space)(multi2));
590 space = isl_space_flatten_range(space);
591 res = FN(MULTI(BASE),alloc)(space);
593 n1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
594 n2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
596 for (i = 0; i < n1; ++i) {
597 el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
598 res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
601 for (i = 0; i < n2; ++i) {
602 el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
603 res = FN(FN(MULTI(BASE),set),BASE)(res, n1 + i, el);
606 FN(MULTI(BASE),free)(multi1);
607 FN(MULTI(BASE),free)(multi2);
608 return res;
609 error:
610 FN(MULTI(BASE),free)(multi1);
611 FN(MULTI(BASE),free)(multi2);
612 return NULL;