Merge branch 'maint'
[isl.git] / isl_multi_templ.c
bloba60b7d453daaf68dbbdb61ad4b4aad9b986fa261
1 /*
2 * Copyright 2011 Sven Verdoolaege
4 * Use of this software is governed by the GNU LGPLv2.1 license
5 */
7 #define xCAT(A,B) A ## B
8 #define CAT(A,B) xCAT(A,B)
9 #undef EL
10 #define EL CAT(isl_,BASE)
11 #define xFN(TYPE,NAME) TYPE ## _ ## NAME
12 #define FN(TYPE,NAME) xFN(TYPE,NAME)
13 #define xMULTI(BASE) isl_multi_ ## BASE
14 #define MULTI(BASE) xMULTI(BASE)
15 #define MULTI_NAME(BASE) "isl_multi_" #BASE
16 #define xLIST(EL) EL ## _list
17 #define LIST(EL) xLIST(EL)
19 isl_ctx *FN(MULTI(BASE),get_ctx)(__isl_keep MULTI(BASE) *multi)
21 return multi ? isl_space_get_ctx(multi->space) : NULL;
24 __isl_give isl_space *FN(MULTI(BASE),get_space)(__isl_keep MULTI(BASE) *multi)
26 return multi ? isl_space_copy(multi->space) : NULL;
29 __isl_give isl_space *FN(MULTI(BASE),get_domain_space)(
30 __isl_keep MULTI(BASE) *multi)
32 return multi ? isl_space_domain(isl_space_copy(multi->space)) : NULL;
35 __isl_give MULTI(BASE) *FN(MULTI(BASE),alloc)(__isl_take isl_space *space)
37 isl_ctx *ctx;
38 int n;
39 MULTI(BASE) *multi;
41 if (!space)
42 return NULL;
44 ctx = isl_space_get_ctx(space);
45 n = isl_space_dim(space, isl_dim_out);
46 multi = isl_calloc(ctx, MULTI(BASE),
47 sizeof(MULTI(BASE)) + (n - 1) * sizeof(struct EL *));
48 if (!multi)
49 goto error;
51 multi->space = space;
52 multi->n = n;
53 multi->ref = 1;
54 return multi;
55 error:
56 isl_space_free(space);
57 return NULL;
60 __isl_give MULTI(BASE) *FN(MULTI(BASE),dup)(__isl_keep MULTI(BASE) *multi)
62 int i;
63 MULTI(BASE) *dup;
65 if (!multi)
66 return NULL;
68 dup = FN(MULTI(BASE),alloc)(isl_space_copy(multi->space));
69 if (!dup)
70 return NULL;
72 for (i = 0; i < multi->n; ++i)
73 dup = FN(FN(MULTI(BASE),set),BASE)(dup, i,
74 FN(EL,copy)(multi->p[i]));
76 return dup;
79 __isl_give MULTI(BASE) *FN(MULTI(BASE),cow)(__isl_take MULTI(BASE) *multi)
81 if (!multi)
82 return NULL;
84 if (multi->ref == 1)
85 return multi;
87 multi->ref--;
88 return FN(MULTI(BASE),dup)(multi);
91 __isl_give MULTI(BASE) *FN(MULTI(BASE),copy)(__isl_keep MULTI(BASE) *multi)
93 if (!multi)
94 return NULL;
96 multi->ref++;
97 return multi;
100 void *FN(MULTI(BASE),free)(__isl_take MULTI(BASE) *multi)
102 int i;
104 if (!multi)
105 return NULL;
107 if (--multi->ref > 0)
108 return NULL;
110 isl_space_free(multi->space);
111 for (i = 0; i < multi->n; ++i)
112 FN(EL,free)(multi->p[i]);
113 free(multi);
115 return NULL;
118 unsigned FN(MULTI(BASE),dim)(__isl_keep MULTI(BASE) *multi,
119 enum isl_dim_type type)
121 return multi ? isl_space_dim(multi->space, type) : 0;
124 const char *FN(MULTI(BASE),get_tuple_name)(__isl_keep MULTI(BASE) *multi,
125 enum isl_dim_type type)
127 return multi ? isl_space_get_tuple_name(multi->space, type) : NULL;
130 __isl_give EL *FN(FN(MULTI(BASE),get),BASE)(__isl_keep MULTI(BASE) *multi,
131 int pos)
133 isl_ctx *ctx;
135 if (!multi)
136 return NULL;
137 ctx = FN(MULTI(BASE),get_ctx)(multi);
138 if (pos < 0 || pos >= multi->n)
139 isl_die(ctx, isl_error_invalid,
140 "index out of bounds", return NULL);
141 return FN(EL,copy)(multi->p[pos]);
144 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),set),BASE)(
145 __isl_take MULTI(BASE) *multi, int pos, __isl_take EL *el)
147 if (!multi || !el)
148 goto error;
150 FN(EL,free)(multi->p[pos]);
151 multi->p[pos] = el;
153 return multi;
154 error:
155 FN(MULTI(BASE),free)(multi);
156 FN(EL,free)(el);
157 return NULL;
160 /* Reset the space of "multi". This function is called from isl_pw_templ.c
161 * and doesn't know if the space of an element object is represented
162 * directly or through its domain. It therefore passes along both,
163 * which we pass along to the element function since we don't how
164 * that is represented either.
166 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_space_and_domain)(
167 __isl_take MULTI(BASE) *multi, __isl_take isl_space *space,
168 __isl_take isl_space *domain)
170 int i;
172 multi = FN(MULTI(BASE),cow)(multi);
173 if (!multi || !space || !domain)
174 goto error;
176 for (i = 0; i < multi->n; ++i) {
177 multi->p[i] = FN(EL,reset_space_and_domain)(multi->p[i],
178 isl_space_copy(space), isl_space_copy(domain));
179 if (!multi->p[i])
180 goto error;
182 isl_space_free(domain);
183 isl_space_free(multi->space);
184 multi->space = space;
186 return multi;
187 error:
188 isl_space_free(domain);
189 isl_space_free(space);
190 FN(MULTI(BASE),free)(multi);
191 return NULL;
194 __isl_give MULTI(BASE) *FN(MULTI(BASE),reset_domain_space)(
195 __isl_take MULTI(BASE) *multi, __isl_take isl_space *domain)
197 isl_space *space;
199 space = isl_space_extend_domain_with_range(isl_space_copy(domain),
200 isl_space_copy(multi->space));
201 return FN(MULTI(BASE),reset_space_and_domain)(multi, space, domain);
204 __isl_give MULTI(BASE) *FN(MULTI(BASE),realign_domain)(
205 __isl_take MULTI(BASE) *multi, __isl_take isl_reordering *exp)
207 int i;
209 multi = FN(MULTI(BASE),cow)(multi);
210 if (!multi || !exp)
211 return NULL;
213 for (i = 0; i < multi->n; ++i) {
214 multi->p[i] = FN(EL,realign_domain)(multi->p[i],
215 isl_reordering_copy(exp));
216 if (!multi->p[i])
217 goto error;
220 multi = FN(MULTI(BASE),reset_domain_space)(multi,
221 isl_space_copy(exp->dim));
223 isl_reordering_free(exp);
224 return multi;
225 error:
226 isl_reordering_free(exp);
227 FN(MULTI(BASE),free)(multi);
228 return NULL;
231 /* Align the parameters of "multi" to those of "model".
233 __isl_give MULTI(BASE) *FN(MULTI(BASE),align_params)(
234 __isl_take MULTI(BASE) *multi, __isl_take isl_space *model)
236 isl_ctx *ctx;
238 if (!multi || !model)
239 goto error;
241 ctx = isl_space_get_ctx(model);
242 if (!isl_space_has_named_params(model))
243 isl_die(ctx, isl_error_invalid,
244 "model has unnamed parameters", goto error);
245 if (!isl_space_has_named_params(multi->space))
246 isl_die(ctx, isl_error_invalid,
247 "input has unnamed parameters", goto error);
248 if (!isl_space_match(multi->space, isl_dim_param,
249 model, isl_dim_param)) {
250 isl_reordering *exp;
252 model = isl_space_params(model);
253 exp = isl_parameter_alignment_reordering(multi->space, model);
254 exp = isl_reordering_extend_space(exp,
255 FN(MULTI(BASE),get_domain_space)(multi));
256 multi = FN(MULTI(BASE),realign_domain)(multi, exp);
259 isl_space_free(model);
260 return multi;
261 error:
262 isl_space_free(model);
263 FN(MULTI(BASE),free)(multi);
264 return NULL;
267 static __isl_give MULTI(BASE) *align_params_multi_set_and(
268 __isl_take MULTI(BASE) *multi, __isl_take isl_set *set,
269 __isl_give MULTI(BASE) *(*fn)(__isl_take MULTI(BASE) *multi,
270 __isl_take isl_set *set))
272 isl_ctx *ctx;
274 if (!multi || !set)
275 goto error;
276 if (isl_space_match(multi->space, isl_dim_param,
277 set->dim, isl_dim_param))
278 return fn(multi, set);
279 ctx = FN(MULTI(BASE),get_ctx)(multi);
280 if (!isl_space_has_named_params(multi->space) ||
281 !isl_space_has_named_params(set->dim))
282 isl_die(ctx, isl_error_invalid,
283 "unaligned unnamed parameters", goto error);
284 multi = FN(MULTI(BASE),align_params)(multi, isl_set_get_space(set));
285 set = isl_set_align_params(set, FN(MULTI(BASE),get_space)(multi));
286 return fn(multi, set);
287 error:
288 FN(MULTI(BASE),free)(multi);
289 isl_set_free(set);
290 return NULL;
293 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_aligned)(
294 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
296 int i;
298 if (!multi || !context)
299 goto error;
301 for (i = 0; i < multi->n; ++i) {
302 multi->p[i] = FN(EL,gist)(multi->p[i], isl_set_copy(context));
303 if (!multi->p[i])
304 goto error;
307 isl_set_free(context);
308 return multi;
309 error:
310 isl_set_free(context);
311 FN(MULTI(BASE),free)(multi);
312 return NULL;
315 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist)(__isl_take MULTI(BASE) *multi,
316 __isl_take isl_set *context)
318 return align_params_multi_set_and(multi, context,
319 &FN(MULTI(BASE),gist_aligned));
322 __isl_give MULTI(BASE) *FN(MULTI(BASE),gist_params)(
323 __isl_take MULTI(BASE) *multi, __isl_take isl_set *context)
325 isl_space *space = FN(MULTI(BASE),get_domain_space)(multi);
326 isl_set *dom_context = isl_set_universe(space);
327 dom_context = isl_set_intersect_params(dom_context, context);
328 return FN(MULTI(BASE),gist)(multi, dom_context);
331 __isl_give MULTI(BASE) *FN(FN(MULTI(BASE),from),LIST(BASE))(
332 __isl_take isl_space *space, __isl_take LIST(EL) *list)
334 int i;
335 int n;
336 isl_ctx *ctx;
337 MULTI(BASE) *multi;
339 if (!space || !list)
340 goto error;
342 ctx = isl_space_get_ctx(space);
343 n = FN(FN(LIST(EL),n),BASE)(list);
344 if (n != isl_space_dim(space, isl_dim_out))
345 isl_die(ctx, isl_error_invalid,
346 "invalid number of elements in list", goto error);
348 multi = FN(MULTI(BASE),alloc)(isl_space_copy(space));
349 for (i = 0; i < n; ++i) {
350 multi = FN(FN(MULTI(BASE),set),BASE)(multi, i,
351 FN(FN(LIST(EL),get),BASE)(list, i));
354 isl_space_free(space);
355 FN(LIST(EL),free)(list);
356 return multi;
357 error:
358 isl_space_free(space);
359 FN(LIST(EL),free)(list);
360 return NULL;