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>
15 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
16 __isl_take isl_vec *v)
23 aff = isl_calloc_type(v->ctx, struct isl_aff);
33 isl_local_space_free(ls);
38 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
47 ctx = isl_local_space_get_ctx(ls);
48 total = isl_local_space_dim(ls, isl_dim_all);
49 v = isl_vec_alloc(ctx, 1 + 1 + total);
50 return isl_aff_alloc_vec(ls, v);
53 __isl_give isl_aff *isl_aff_zero(__isl_take isl_local_space *ls)
57 aff = isl_aff_alloc(ls);
61 isl_int_set_si(aff->v->el[0], 1);
62 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
67 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
76 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
81 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
82 isl_vec_copy(aff->v));
85 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
93 return isl_aff_dup(aff);
96 void *isl_aff_free(__isl_take isl_aff *aff)
104 isl_local_space_free(aff->ls);
105 isl_vec_free(aff->v);
112 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
114 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
117 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
119 return aff ? isl_local_space_dim(aff->ls, type) : 0;
122 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
124 return aff ? isl_local_space_copy(aff->ls) : NULL;
127 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
128 enum isl_dim_type type, unsigned pos)
130 return aff ? isl_local_space_get_dim_name(aff->ls, type, pos) : 0;
133 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
137 isl_int_set(*v, aff->v->el[0]);
141 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
145 isl_int_set(*v, aff->v->el[1]);
149 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
150 enum isl_dim_type type, int pos, isl_int *v)
155 if (pos >= isl_local_space_dim(aff->ls, type))
156 isl_die(aff->v->ctx, isl_error_invalid,
157 "position out of bounds", return -1);
159 pos += isl_local_space_offset(aff->ls, type);
160 isl_int_set(*v, aff->v->el[1 + pos]);
165 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
167 aff = isl_aff_cow(aff);
171 aff->v = isl_vec_cow(aff->v);
173 return isl_aff_free(aff);
175 isl_int_set(aff->v->el[0], v);
180 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
182 aff = isl_aff_cow(aff);
186 aff->v = isl_vec_cow(aff->v);
188 return isl_aff_free(aff);
190 isl_int_set(aff->v->el[1], v);
195 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
197 if (isl_int_is_zero(v))
200 aff = isl_aff_cow(aff);
204 aff->v = isl_vec_cow(aff->v);
206 return isl_aff_free(aff);
208 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
213 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
215 aff = isl_aff_cow(aff);
219 aff->v = isl_vec_cow(aff->v);
221 return isl_aff_free(aff);
223 isl_int_set_si(aff->v->el[1], v);
228 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
229 enum isl_dim_type type, int pos, isl_int v)
234 if (pos >= isl_local_space_dim(aff->ls, type))
235 isl_die(aff->v->ctx, isl_error_invalid,
236 "position out of bounds", return isl_aff_free(aff));
238 aff = isl_aff_cow(aff);
242 aff->v = isl_vec_cow(aff->v);
244 return isl_aff_free(aff);
246 pos += isl_local_space_offset(aff->ls, type);
247 isl_int_set(aff->v->el[1 + pos], v);
252 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
253 enum isl_dim_type type, int pos, int v)
258 if (pos >= isl_local_space_dim(aff->ls, type))
259 isl_die(aff->v->ctx, isl_error_invalid,
260 "position out of bounds", return isl_aff_free(aff));
262 aff = isl_aff_cow(aff);
266 aff->v = isl_vec_cow(aff->v);
268 return isl_aff_free(aff);
270 pos += isl_local_space_offset(aff->ls, type);
271 isl_int_set_si(aff->v->el[1 + pos], v);
276 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
277 enum isl_dim_type type, int pos, isl_int v)
282 if (pos >= isl_local_space_dim(aff->ls, type))
283 isl_die(aff->v->ctx, isl_error_invalid,
284 "position out of bounds", return isl_aff_free(aff));
286 aff = isl_aff_cow(aff);
290 aff->v = isl_vec_cow(aff->v);
292 return isl_aff_free(aff);
294 pos += isl_local_space_offset(aff->ls, type);
295 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
300 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
301 enum isl_dim_type type, int pos, int v)
306 isl_int_set_si(t, v);
307 aff = isl_aff_add_coefficient(aff, type, pos, t);
313 __isl_give isl_div *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
318 return isl_local_space_get_div(aff->ls, pos);
321 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
323 aff = isl_aff_cow(aff);
326 aff->v = isl_vec_cow(aff->v);
328 return isl_aff_free(aff);
330 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
335 /* Given f, return ceil(f).
336 * If f is an integer expression, then just return f.
337 * Otherwise, create a new div d = [-f] and return the expression -d.
339 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
347 if (isl_int_is_one(aff->v->el[0]))
350 aff = isl_aff_cow(aff);
354 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
355 aff->ls = isl_local_space_add_div(aff->ls, isl_vec_copy(aff->v));
359 ctx = isl_aff_get_ctx(aff);
361 isl_vec_free(aff->v);
362 aff->v = isl_vec_alloc(ctx, size + 1);
363 aff->v = isl_vec_clr(aff->v);
366 isl_int_set_si(aff->v->el[0], 1);
367 isl_int_set_si(aff->v->el[size], -1);
375 /* Apply the expansion computed by isl_merge_divs.
376 * The expansion itself is given by "exp" while the resulting
377 * list of divs is given by "div".
379 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
380 __isl_take isl_mat *div, int *exp)
387 aff = isl_aff_cow(aff);
391 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
392 new_n_div = isl_mat_rows(div);
393 if (new_n_div < old_n_div)
394 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
395 "not an expansion", goto error);
397 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
401 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
403 for (i = new_n_div - 1; i >= 0; --i) {
404 if (j >= 0 && exp[j] == i) {
406 isl_int_swap(aff->v->el[offset + i],
407 aff->v->el[offset + j]);
410 isl_int_set_si(aff->v->el[offset + j], 0);
413 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));