2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
13 struct isl_vec *isl_vec_alloc(struct isl_ctx *ctx, unsigned size)
17 vec = isl_alloc_type(ctx, struct isl_vec);
21 vec->block = isl_blk_alloc(ctx, size);
22 if (isl_blk_is_error(vec->block))
29 vec->el = vec->block.data;
33 isl_blk_free(ctx, vec->block);
37 __isl_give isl_vec *isl_vec_extend(__isl_take isl_vec *vec, unsigned size)
41 if (size <= vec->size)
44 vec = isl_vec_cow(vec);
48 vec->block = isl_blk_extend(vec->ctx, vec->block, size);
53 vec->el = vec->block.data;
61 __isl_give isl_vec *isl_vec_zero_extend(__isl_take isl_vec *vec, unsigned size)
67 if (size <= vec->size)
70 vec = isl_vec_cow(vec);
74 extra = size - vec->size;
75 vec = isl_vec_extend(vec, size);
79 isl_seq_clr(vec->el + size - extra, extra);
84 struct isl_vec *isl_vec_copy(struct isl_vec *vec)
93 struct isl_vec *isl_vec_dup(struct isl_vec *vec)
99 vec2 = isl_vec_alloc(vec->ctx, vec->size);
100 isl_seq_cpy(vec2->el, vec->el, vec->size);
104 struct isl_vec *isl_vec_cow(struct isl_vec *vec)
106 struct isl_vec *vec2;
113 vec2 = isl_vec_dup(vec);
118 void isl_vec_free(struct isl_vec *vec)
126 isl_ctx_deref(vec->ctx);
127 isl_blk_free(vec->ctx, vec->block);
131 void isl_vec_dump(struct isl_vec *vec, FILE *out, int indent)
136 fprintf(out, "%*snull vec\n", indent, "");
140 fprintf(out, "%*s[", indent, "");
141 for (i = 0; i < vec->size; ++i) {
144 isl_int_print(out, vec->el[i], 0);
149 void isl_vec_lcm(struct isl_vec *vec, isl_int *lcm)
151 isl_seq_lcm(vec->block.data, vec->size, lcm);
154 /* Given a rational vector, with the denominator in the first element
155 * of the vector, round up all coordinates.
157 struct isl_vec *isl_vec_ceil(struct isl_vec *vec)
159 vec = isl_vec_cow(vec);
163 isl_seq_cdiv_q(vec->el + 1, vec->el + 1, vec->el[0], vec->size - 1);
165 isl_int_set_si(vec->el[0], 1);
170 struct isl_vec *isl_vec_normalize(struct isl_vec *vec)
174 isl_seq_normalize(vec->ctx, vec->el, vec->size);
178 __isl_give isl_vec *isl_vec_scale(__isl_take isl_vec *vec, isl_int m)
180 if (isl_int_is_one(m))
182 vec = isl_vec_cow(vec);
185 isl_seq_scale(vec->el, vec->el, m, vec->size);
189 __isl_give isl_vec *isl_vec_add(__isl_take isl_vec *vec1,
190 __isl_take isl_vec *vec2)
192 vec1 = isl_vec_cow(vec1);
196 isl_assert(vec1->ctx, vec1->size == vec2->size, goto error);
198 isl_seq_combine(vec1->el, vec1->ctx->one, vec1->el,
199 vec1->ctx->one, vec2->el, vec1->size);