2 * Copyright 2011 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 #include <isl_aff_private.h>
12 #include <isl_local_space_private.h>
13 #include <isl_mat_private.h>
16 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
17 __isl_take isl_vec *v)
24 aff = isl_calloc_type(v->ctx, struct isl_aff);
34 isl_local_space_free(ls);
39 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
48 ctx = isl_local_space_get_ctx(ls);
49 if (!isl_local_space_divs_known(ls))
50 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
53 total = isl_local_space_dim(ls, isl_dim_all);
54 v = isl_vec_alloc(ctx, 1 + 1 + total);
55 return isl_aff_alloc_vec(ls, v);
57 isl_local_space_free(ls);
61 __isl_give isl_aff *isl_aff_zero(__isl_take isl_local_space *ls)
65 aff = isl_aff_alloc(ls);
69 isl_int_set_si(aff->v->el[0], 1);
70 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
75 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
84 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
89 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
90 isl_vec_copy(aff->v));
93 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
101 return isl_aff_dup(aff);
104 void *isl_aff_free(__isl_take isl_aff *aff)
112 isl_local_space_free(aff->ls);
113 isl_vec_free(aff->v);
120 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
122 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
125 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
127 return aff ? isl_local_space_dim(aff->ls, type) : 0;
130 __isl_give isl_dim *isl_aff_get_dim(__isl_keep isl_aff *aff)
132 return aff ? isl_local_space_get_dim(aff->ls) : NULL;
135 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
137 return aff ? isl_local_space_copy(aff->ls) : NULL;
140 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
141 enum isl_dim_type type, unsigned pos)
143 return aff ? isl_local_space_get_dim_name(aff->ls, type, pos) : 0;
146 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
150 isl_int_set(*v, aff->v->el[0]);
154 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
158 isl_int_set(*v, aff->v->el[1]);
162 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
163 enum isl_dim_type type, int pos, isl_int *v)
168 if (pos >= isl_local_space_dim(aff->ls, type))
169 isl_die(aff->v->ctx, isl_error_invalid,
170 "position out of bounds", return -1);
172 pos += isl_local_space_offset(aff->ls, type);
173 isl_int_set(*v, aff->v->el[1 + pos]);
178 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
180 aff = isl_aff_cow(aff);
184 aff->v = isl_vec_cow(aff->v);
186 return isl_aff_free(aff);
188 isl_int_set(aff->v->el[0], v);
193 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
195 aff = isl_aff_cow(aff);
199 aff->v = isl_vec_cow(aff->v);
201 return isl_aff_free(aff);
203 isl_int_set(aff->v->el[1], v);
208 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
210 if (isl_int_is_zero(v))
213 aff = isl_aff_cow(aff);
217 aff->v = isl_vec_cow(aff->v);
219 return isl_aff_free(aff);
221 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
226 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
228 aff = isl_aff_cow(aff);
232 aff->v = isl_vec_cow(aff->v);
234 return isl_aff_free(aff);
236 isl_int_set_si(aff->v->el[1], v);
241 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
242 enum isl_dim_type type, int pos, isl_int v)
247 if (pos >= isl_local_space_dim(aff->ls, type))
248 isl_die(aff->v->ctx, isl_error_invalid,
249 "position out of bounds", return isl_aff_free(aff));
251 aff = isl_aff_cow(aff);
255 aff->v = isl_vec_cow(aff->v);
257 return isl_aff_free(aff);
259 pos += isl_local_space_offset(aff->ls, type);
260 isl_int_set(aff->v->el[1 + pos], v);
265 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
266 enum isl_dim_type type, int pos, int v)
271 if (pos >= isl_local_space_dim(aff->ls, type))
272 isl_die(aff->v->ctx, isl_error_invalid,
273 "position out of bounds", return isl_aff_free(aff));
275 aff = isl_aff_cow(aff);
279 aff->v = isl_vec_cow(aff->v);
281 return isl_aff_free(aff);
283 pos += isl_local_space_offset(aff->ls, type);
284 isl_int_set_si(aff->v->el[1 + pos], v);
289 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
290 enum isl_dim_type type, int pos, isl_int v)
295 if (pos >= isl_local_space_dim(aff->ls, type))
296 isl_die(aff->v->ctx, isl_error_invalid,
297 "position out of bounds", return isl_aff_free(aff));
299 aff = isl_aff_cow(aff);
303 aff->v = isl_vec_cow(aff->v);
305 return isl_aff_free(aff);
307 pos += isl_local_space_offset(aff->ls, type);
308 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
313 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
314 enum isl_dim_type type, int pos, int v)
319 isl_int_set_si(t, v);
320 aff = isl_aff_add_coefficient(aff, type, pos, t);
326 __isl_give isl_div *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
331 return isl_local_space_get_div(aff->ls, pos);
334 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
336 aff = isl_aff_cow(aff);
339 aff->v = isl_vec_cow(aff->v);
341 return isl_aff_free(aff);
343 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
348 /* Given f, return ceil(f).
349 * If f is an integer expression, then just return f.
350 * Otherwise, create a new div d = [-f] and return the expression -d.
352 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
360 if (isl_int_is_one(aff->v->el[0]))
363 aff = isl_aff_cow(aff);
367 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
368 aff->ls = isl_local_space_add_div(aff->ls, isl_vec_copy(aff->v));
372 ctx = isl_aff_get_ctx(aff);
374 isl_vec_free(aff->v);
375 aff->v = isl_vec_alloc(ctx, size + 1);
376 aff->v = isl_vec_clr(aff->v);
379 isl_int_set_si(aff->v->el[0], 1);
380 isl_int_set_si(aff->v->el[size], -1);
388 /* Apply the expansion computed by isl_merge_divs.
389 * The expansion itself is given by "exp" while the resulting
390 * list of divs is given by "div".
392 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
393 __isl_take isl_mat *div, int *exp)
400 aff = isl_aff_cow(aff);
404 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
405 new_n_div = isl_mat_rows(div);
406 if (new_n_div < old_n_div)
407 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
408 "not an expansion", goto error);
410 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
414 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
416 for (i = new_n_div - 1; i >= 0; --i) {
417 if (j >= 0 && exp[j] == i) {
419 isl_int_swap(aff->v->el[offset + i],
420 aff->v->el[offset + j]);
423 isl_int_set_si(aff->v->el[offset + i], 0);
426 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
437 /* Add two affine expressions that live in the same local space.
439 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
440 __isl_take isl_aff *aff2)
444 aff1 = isl_aff_cow(aff1);
448 aff1->v = isl_vec_cow(aff1->v);
454 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
455 isl_int_divexact(f, aff2->v->el[0], gcd);
456 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
457 isl_int_divexact(f, aff1->v->el[0], gcd);
458 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
459 isl_int_divexact(f, aff2->v->el[0], gcd);
460 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
472 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
473 __isl_take isl_aff *aff2)
483 ctx = isl_aff_get_ctx(aff1);
484 if (!isl_dim_equal(aff1->ls->dim, aff2->ls->dim))
485 isl_die(ctx, isl_error_invalid,
486 "spaces don't match", goto error);
488 if (aff1->ls->div->n_row == 0 && aff2->ls->div->n_row == 0)
489 return add_expanded(aff1, aff2);
491 exp1 = isl_alloc_array(ctx, int, aff1->ls->div->n_row);
492 exp2 = isl_alloc_array(ctx, int, aff2->ls->div->n_row);
496 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
497 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
498 aff2 = isl_aff_expand_divs(aff2, div, exp2);
502 return add_expanded(aff1, aff2);
511 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
512 __isl_take isl_aff *aff2)
514 return isl_aff_add(aff1, isl_aff_neg(aff2));