isl_pw_qpolynomial_bound: handle combination of wrapping and existentials
[isl.git] / isl_vec.c
blob38a55a7c1c02daf2258030003962d6f3757b85b2
1 /*
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
8 */
10 #include <isl/seq.h>
11 #include <isl/vec.h>
13 struct isl_vec *isl_vec_alloc(struct isl_ctx *ctx, unsigned size)
15 struct isl_vec *vec;
17 vec = isl_alloc_type(ctx, struct isl_vec);
18 if (!vec)
19 return NULL;
21 vec->block = isl_blk_alloc(ctx, size);
22 if (isl_blk_is_error(vec->block))
23 goto error;
25 vec->ctx = ctx;
26 isl_ctx_ref(ctx);
27 vec->ref = 1;
28 vec->size = size;
29 vec->el = vec->block.data;
31 return vec;
32 error:
33 isl_blk_free(ctx, vec->block);
34 return NULL;
37 __isl_give isl_vec *isl_vec_extend(__isl_take isl_vec *vec, unsigned size)
39 if (!vec)
40 return NULL;
41 if (size <= vec->size)
42 return vec;
44 vec = isl_vec_cow(vec);
45 if (!vec)
46 return NULL;
48 vec->block = isl_blk_extend(vec->ctx, vec->block, size);
49 if (!vec->block.data)
50 goto error;
52 vec->size = size;
53 vec->el = vec->block.data;
55 return vec;
56 error:
57 isl_vec_free(vec);
58 return NULL;
61 __isl_give isl_vec *isl_vec_zero_extend(__isl_take isl_vec *vec, unsigned size)
63 int extra;
65 if (!vec)
66 return NULL;
67 if (size <= vec->size)
68 return vec;
70 vec = isl_vec_cow(vec);
71 if (!vec)
72 return NULL;
74 extra = size - vec->size;
75 vec = isl_vec_extend(vec, size);
76 if (!vec)
77 return NULL;
79 isl_seq_clr(vec->el + size - extra, extra);
81 return vec;
84 struct isl_vec *isl_vec_copy(struct isl_vec *vec)
86 if (!vec)
87 return NULL;
89 vec->ref++;
90 return vec;
93 struct isl_vec *isl_vec_dup(struct isl_vec *vec)
95 struct isl_vec *vec2;
97 if (!vec)
98 return NULL;
99 vec2 = isl_vec_alloc(vec->ctx, vec->size);
100 isl_seq_cpy(vec2->el, vec->el, vec->size);
101 return vec2;
104 struct isl_vec *isl_vec_cow(struct isl_vec *vec)
106 struct isl_vec *vec2;
107 if (!vec)
108 return NULL;
110 if (vec->ref == 1)
111 return vec;
113 vec2 = isl_vec_dup(vec);
114 isl_vec_free(vec);
115 return vec2;
118 void isl_vec_free(struct isl_vec *vec)
120 if (!vec)
121 return;
123 if (--vec->ref > 0)
124 return;
126 isl_ctx_deref(vec->ctx);
127 isl_blk_free(vec->ctx, vec->block);
128 free(vec);
131 __isl_give isl_printer *isl_printer_print_vec(__isl_take isl_printer *printer,
132 __isl_keep isl_vec *vec)
134 int i;
136 if (!printer || !vec)
137 goto error;
139 printer = isl_printer_print_str(printer, "[");
140 for (i = 0; i < vec->size; ++i) {
141 if (i)
142 printer = isl_printer_print_str(printer, ",");
143 printer = isl_printer_print_isl_int(printer, vec->el[i]);
145 printer = isl_printer_print_str(printer, "]");
147 return printer;
148 error:
149 isl_printer_free(printer);
150 return NULL;
153 void isl_vec_dump(struct isl_vec *vec)
155 isl_printer *printer;
157 if (!vec)
158 return;
160 printer = isl_printer_to_file(vec->ctx, stderr);
161 printer = isl_printer_print_vec(printer, vec);
162 printer = isl_printer_end_line(printer);
164 isl_printer_free(printer);
167 __isl_give isl_vec *isl_vec_clr(__isl_take isl_vec *vec)
169 vec = isl_vec_cow(vec);
170 if (!vec)
171 return NULL;
172 isl_seq_clr(vec->el, vec->size);
173 return vec;
176 void isl_vec_lcm(struct isl_vec *vec, isl_int *lcm)
178 isl_seq_lcm(vec->block.data, vec->size, lcm);
181 /* Given a rational vector, with the denominator in the first element
182 * of the vector, round up all coordinates.
184 struct isl_vec *isl_vec_ceil(struct isl_vec *vec)
186 vec = isl_vec_cow(vec);
187 if (!vec)
188 return NULL;
190 isl_seq_cdiv_q(vec->el + 1, vec->el + 1, vec->el[0], vec->size - 1);
192 isl_int_set_si(vec->el[0], 1);
194 return vec;
197 struct isl_vec *isl_vec_normalize(struct isl_vec *vec)
199 if (!vec)
200 return NULL;
201 isl_seq_normalize(vec->ctx, vec->el, vec->size);
202 return vec;
205 __isl_give isl_vec *isl_vec_scale(__isl_take isl_vec *vec, isl_int m)
207 if (isl_int_is_one(m))
208 return vec;
209 vec = isl_vec_cow(vec);
210 if (!vec)
211 return NULL;
212 isl_seq_scale(vec->el, vec->el, m, vec->size);
213 return vec;
216 __isl_give isl_vec *isl_vec_add(__isl_take isl_vec *vec1,
217 __isl_take isl_vec *vec2)
219 vec1 = isl_vec_cow(vec1);
220 if (!vec1 || !vec2)
221 goto error;
223 isl_assert(vec1->ctx, vec1->size == vec2->size, goto error);
225 isl_seq_combine(vec1->el, vec1->ctx->one, vec1->el,
226 vec1->ctx->one, vec2->el, vec1->size);
228 isl_vec_free(vec2);
229 return vec1;
230 error:
231 isl_vec_free(vec1);
232 isl_vec_free(vec2);
233 return NULL;