generalize isl_pw_aff_involves_nan
[isl.git] / isl_vec.c
blob2b49a8106adeeee02820ab5519748598948e3700
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2013 Ecole Normale Superieure
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
12 #include <isl_ctx_private.h>
13 #include <isl_seq.h>
14 #include <isl_val_private.h>
15 #include <isl_vec_private.h>
16 #include <isl/deprecated/vec_int.h>
18 isl_ctx *isl_vec_get_ctx(__isl_keep isl_vec *vec)
20 return vec ? vec->ctx : NULL;
23 /* Return a hash value that digests "vec".
25 uint32_t isl_vec_get_hash(__isl_keep isl_vec *vec)
27 if (!vec)
28 return 0;
30 return isl_seq_get_hash(vec->el, vec->size);
33 struct isl_vec *isl_vec_alloc(struct isl_ctx *ctx, unsigned size)
35 struct isl_vec *vec;
37 vec = isl_alloc_type(ctx, struct isl_vec);
38 if (!vec)
39 return NULL;
41 vec->block = isl_blk_alloc(ctx, size);
42 if (isl_blk_is_error(vec->block))
43 goto error;
45 vec->ctx = ctx;
46 isl_ctx_ref(ctx);
47 vec->ref = 1;
48 vec->size = size;
49 vec->el = vec->block.data;
51 return vec;
52 error:
53 isl_blk_free(ctx, vec->block);
54 free(vec);
55 return NULL;
58 __isl_give isl_vec *isl_vec_extend(__isl_take isl_vec *vec, unsigned size)
60 if (!vec)
61 return NULL;
62 if (size <= vec->size)
63 return vec;
65 vec = isl_vec_cow(vec);
66 if (!vec)
67 return NULL;
69 vec->block = isl_blk_extend(vec->ctx, vec->block, size);
70 if (!vec->block.data)
71 goto error;
73 vec->size = size;
74 vec->el = vec->block.data;
76 return vec;
77 error:
78 isl_vec_free(vec);
79 return NULL;
82 /* Apply the expansion specified by "exp" to the "n" elements starting at "pos".
83 * "expanded" it the number of elements that need to replace those "n"
84 * elements. The entries in "exp" have increasing values between
85 * 0 and "expanded".
87 __isl_give isl_vec *isl_vec_expand(__isl_take isl_vec *vec, int pos, int n,
88 int *exp, int expanded)
90 int i, j;
91 int old_size, extra;
93 if (!vec)
94 return NULL;
95 if (expanded < n)
96 isl_die(isl_vec_get_ctx(vec), isl_error_invalid,
97 "not an expansion", isl_vec_free(vec));
98 if (expanded == n)
99 return vec;
100 if (pos < 0 || n < 0 || pos + n > vec->size)
101 isl_die(isl_vec_get_ctx(vec), isl_error_invalid,
102 "position out of bounds", return isl_vec_free(vec));
104 old_size = vec->size;
105 extra = expanded - n;
106 vec = isl_vec_extend(vec, old_size + extra);
107 vec = isl_vec_cow(vec);
108 if (!vec)
109 return NULL;
111 for (i = old_size - 1; i >= pos + n; --i)
112 isl_int_set(vec->el[i + extra], vec->el[i]);
114 j = n - 1;
115 for (i = expanded - 1; i >= 0; --i) {
116 if (j >= 0 && exp[j] == i) {
117 if (i != j)
118 isl_int_swap(vec->el[pos + i],
119 vec->el[pos + j]);
120 j--;
121 } else {
122 isl_int_set_si(vec->el[pos + i], 0);
126 return vec;
129 __isl_give isl_vec *isl_vec_zero_extend(__isl_take isl_vec *vec, unsigned size)
131 int extra;
133 if (!vec)
134 return NULL;
135 if (size <= vec->size)
136 return vec;
138 vec = isl_vec_cow(vec);
139 if (!vec)
140 return NULL;
142 extra = size - vec->size;
143 vec = isl_vec_extend(vec, size);
144 if (!vec)
145 return NULL;
147 isl_seq_clr(vec->el + size - extra, extra);
149 return vec;
152 /* Return a vector containing the elements of "vec1" followed by
153 * those of "vec2".
155 __isl_give isl_vec *isl_vec_concat(__isl_take isl_vec *vec1,
156 __isl_take isl_vec *vec2)
158 if (!vec1 || !vec2)
159 goto error;
161 if (vec2->size == 0) {
162 isl_vec_free(vec2);
163 return vec1;
166 if (vec1->size == 0) {
167 isl_vec_free(vec1);
168 return vec2;
171 vec1 = isl_vec_extend(vec1, vec1->size + vec2->size);
172 if (!vec1)
173 goto error;
175 isl_seq_cpy(vec1->el + vec1->size - vec2->size, vec2->el, vec2->size);
177 isl_vec_free(vec2);
178 return vec1;
179 error:
180 isl_vec_free(vec1);
181 isl_vec_free(vec2);
182 return NULL;
185 struct isl_vec *isl_vec_copy(struct isl_vec *vec)
187 if (!vec)
188 return NULL;
190 vec->ref++;
191 return vec;
194 struct isl_vec *isl_vec_dup(struct isl_vec *vec)
196 struct isl_vec *vec2;
198 if (!vec)
199 return NULL;
200 vec2 = isl_vec_alloc(vec->ctx, vec->size);
201 if (!vec2)
202 return NULL;
203 isl_seq_cpy(vec2->el, vec->el, vec->size);
204 return vec2;
207 struct isl_vec *isl_vec_cow(struct isl_vec *vec)
209 struct isl_vec *vec2;
210 if (!vec)
211 return NULL;
213 if (vec->ref == 1)
214 return vec;
216 vec2 = isl_vec_dup(vec);
217 isl_vec_free(vec);
218 return vec2;
221 __isl_null isl_vec *isl_vec_free(__isl_take isl_vec *vec)
223 if (!vec)
224 return NULL;
226 if (--vec->ref > 0)
227 return NULL;
229 isl_ctx_deref(vec->ctx);
230 isl_blk_free(vec->ctx, vec->block);
231 free(vec);
233 return NULL;
236 int isl_vec_size(__isl_keep isl_vec *vec)
238 return vec ? vec->size : -1;
241 int isl_vec_get_element(__isl_keep isl_vec *vec, int pos, isl_int *v)
243 if (!vec)
244 return -1;
246 if (pos < 0 || pos >= vec->size)
247 isl_die(vec->ctx, isl_error_invalid, "position out of range",
248 return -1);
249 isl_int_set(*v, vec->el[pos]);
250 return 0;
253 /* Extract the element at position "pos" of "vec".
255 __isl_give isl_val *isl_vec_get_element_val(__isl_keep isl_vec *vec, int pos)
257 isl_ctx *ctx;
259 if (!vec)
260 return NULL;
261 ctx = isl_vec_get_ctx(vec);
262 if (pos < 0 || pos >= vec->size)
263 isl_die(ctx, isl_error_invalid, "position out of range",
264 return NULL);
265 return isl_val_int_from_isl_int(ctx, vec->el[pos]);
268 __isl_give isl_vec *isl_vec_set_element(__isl_take isl_vec *vec,
269 int pos, isl_int v)
271 vec = isl_vec_cow(vec);
272 if (!vec)
273 return NULL;
274 if (pos < 0 || pos >= vec->size)
275 isl_die(vec->ctx, isl_error_invalid, "position out of range",
276 goto error);
277 isl_int_set(vec->el[pos], v);
278 return vec;
279 error:
280 isl_vec_free(vec);
281 return NULL;
284 __isl_give isl_vec *isl_vec_set_element_si(__isl_take isl_vec *vec,
285 int pos, int v)
287 vec = isl_vec_cow(vec);
288 if (!vec)
289 return NULL;
290 if (pos < 0 || pos >= vec->size)
291 isl_die(vec->ctx, isl_error_invalid, "position out of range",
292 goto error);
293 isl_int_set_si(vec->el[pos], v);
294 return vec;
295 error:
296 isl_vec_free(vec);
297 return NULL;
300 /* Replace the element at position "pos" of "vec" by "v".
302 __isl_give isl_vec *isl_vec_set_element_val(__isl_take isl_vec *vec,
303 int pos, __isl_take isl_val *v)
305 if (!v)
306 return isl_vec_free(vec);
307 if (!isl_val_is_int(v))
308 isl_die(isl_val_get_ctx(v), isl_error_invalid,
309 "expecting integer value", goto error);
310 vec = isl_vec_set_element(vec, pos, v->n);
311 isl_val_free(v);
312 return vec;
313 error:
314 isl_val_free(v);
315 return isl_vec_free(vec);
318 /* Compare the elements of "vec1" and "vec2" at position "pos".
320 int isl_vec_cmp_element(__isl_keep isl_vec *vec1, __isl_keep isl_vec *vec2,
321 int pos)
323 if (!vec1 || !vec2)
324 return 0;
325 if (pos < 0 || pos >= vec1->size || pos >= vec2->size)
326 isl_die(isl_vec_get_ctx(vec1), isl_error_invalid,
327 "position out of range", return 0);
328 return isl_int_cmp(vec1->el[pos], vec2->el[pos]);
331 isl_bool isl_vec_is_equal(__isl_keep isl_vec *vec1, __isl_keep isl_vec *vec2)
333 if (!vec1 || !vec2)
334 return isl_bool_error;
336 if (vec1->size != vec2->size)
337 return isl_bool_false;
339 return isl_seq_eq(vec1->el, vec2->el, vec1->size);
342 __isl_give isl_printer *isl_printer_print_vec(__isl_take isl_printer *printer,
343 __isl_keep isl_vec *vec)
345 int i;
347 if (!printer || !vec)
348 goto error;
350 printer = isl_printer_print_str(printer, "[");
351 for (i = 0; i < vec->size; ++i) {
352 if (i)
353 printer = isl_printer_print_str(printer, ",");
354 printer = isl_printer_print_isl_int(printer, vec->el[i]);
356 printer = isl_printer_print_str(printer, "]");
358 return printer;
359 error:
360 isl_printer_free(printer);
361 return NULL;
364 void isl_vec_dump(struct isl_vec *vec)
366 isl_printer *printer;
368 if (!vec)
369 return;
371 printer = isl_printer_to_file(vec->ctx, stderr);
372 printer = isl_printer_print_vec(printer, vec);
373 printer = isl_printer_end_line(printer);
375 isl_printer_free(printer);
378 __isl_give isl_vec *isl_vec_set(__isl_take isl_vec *vec, isl_int v)
380 vec = isl_vec_cow(vec);
381 if (!vec)
382 return NULL;
383 isl_seq_set(vec->el, v, vec->size);
384 return vec;
387 __isl_give isl_vec *isl_vec_set_si(__isl_take isl_vec *vec, int v)
389 vec = isl_vec_cow(vec);
390 if (!vec)
391 return NULL;
392 isl_seq_set_si(vec->el, v, vec->size);
393 return vec;
396 /* Replace all elements of "vec" by "v".
398 __isl_give isl_vec *isl_vec_set_val(__isl_take isl_vec *vec,
399 __isl_take isl_val *v)
401 vec = isl_vec_cow(vec);
402 if (!vec || !v)
403 goto error;
404 if (!isl_val_is_int(v))
405 isl_die(isl_val_get_ctx(v), isl_error_invalid,
406 "expecting integer value", goto error);
407 isl_seq_set(vec->el, v->n, vec->size);
408 isl_val_free(v);
409 return vec;
410 error:
411 isl_vec_free(vec);
412 isl_val_free(v);
413 return NULL;
416 __isl_give isl_vec *isl_vec_clr(__isl_take isl_vec *vec)
418 vec = isl_vec_cow(vec);
419 if (!vec)
420 return NULL;
421 isl_seq_clr(vec->el, vec->size);
422 return vec;
425 void isl_vec_lcm(struct isl_vec *vec, isl_int *lcm)
427 isl_seq_lcm(vec->block.data, vec->size, lcm);
430 /* Given a rational vector, with the denominator in the first element
431 * of the vector, round up all coordinates.
433 struct isl_vec *isl_vec_ceil(struct isl_vec *vec)
435 vec = isl_vec_cow(vec);
436 if (!vec)
437 return NULL;
439 isl_seq_cdiv_q(vec->el + 1, vec->el + 1, vec->el[0], vec->size - 1);
441 isl_int_set_si(vec->el[0], 1);
443 return vec;
446 struct isl_vec *isl_vec_normalize(struct isl_vec *vec)
448 if (!vec)
449 return NULL;
450 isl_seq_normalize(vec->ctx, vec->el, vec->size);
451 return vec;
454 __isl_give isl_vec *isl_vec_neg(__isl_take isl_vec *vec)
456 vec = isl_vec_cow(vec);
457 if (!vec)
458 return NULL;
459 isl_seq_neg(vec->el, vec->el, vec->size);
460 return vec;
463 __isl_give isl_vec *isl_vec_scale(__isl_take isl_vec *vec, isl_int m)
465 if (isl_int_is_one(m))
466 return vec;
467 vec = isl_vec_cow(vec);
468 if (!vec)
469 return NULL;
470 isl_seq_scale(vec->el, vec->el, m, vec->size);
471 return vec;
474 /* Reduce the elements of "vec" modulo "m".
476 __isl_give isl_vec *isl_vec_fdiv_r(__isl_take isl_vec *vec, isl_int m)
478 vec = isl_vec_cow(vec);
479 if (!vec)
480 return NULL;
482 isl_seq_fdiv_r(vec->el, vec->el, m, vec->size);
484 return vec;
487 __isl_give isl_vec *isl_vec_add(__isl_take isl_vec *vec1,
488 __isl_take isl_vec *vec2)
490 vec1 = isl_vec_cow(vec1);
491 if (!vec1 || !vec2)
492 goto error;
494 isl_assert(vec1->ctx, vec1->size == vec2->size, goto error);
496 isl_seq_combine(vec1->el, vec1->ctx->one, vec1->el,
497 vec1->ctx->one, vec2->el, vec1->size);
499 isl_vec_free(vec2);
500 return vec1;
501 error:
502 isl_vec_free(vec1);
503 isl_vec_free(vec2);
504 return NULL;
507 static int qsort_int_cmp(const void *p1, const void *p2)
509 const isl_int *i1 = (const isl_int *) p1;
510 const isl_int *i2 = (const isl_int *) p2;
512 return isl_int_cmp(*i1, *i2);
515 __isl_give isl_vec *isl_vec_sort(__isl_take isl_vec *vec)
517 if (!vec)
518 return NULL;
520 qsort(vec->el, vec->size, sizeof(*vec->el), &qsort_int_cmp);
522 return vec;
525 __isl_give isl_vec *isl_vec_drop_els(__isl_take isl_vec *vec,
526 unsigned pos, unsigned n)
528 if (n == 0)
529 return vec;
530 vec = isl_vec_cow(vec);
531 if (!vec)
532 return NULL;
534 if (pos + n > vec->size)
535 isl_die(vec->ctx, isl_error_invalid,
536 "range out of bounds", goto error);
538 if (pos + n != vec->size)
539 isl_seq_cpy(vec->el + pos, vec->el + pos + n,
540 vec->size - pos - n);
542 vec->size -= n;
544 return vec;
545 error:
546 isl_vec_free(vec);
547 return NULL;
550 __isl_give isl_vec *isl_vec_insert_els(__isl_take isl_vec *vec,
551 unsigned pos, unsigned n)
553 isl_vec *ext = NULL;
555 if (n == 0)
556 return vec;
557 if (!vec)
558 return NULL;
560 if (pos > vec->size)
561 isl_die(vec->ctx, isl_error_invalid,
562 "position out of bounds", goto error);
564 ext = isl_vec_alloc(vec->ctx, vec->size + n);
565 if (!ext)
566 goto error;
568 isl_seq_cpy(ext->el, vec->el, pos);
569 isl_seq_cpy(ext->el + pos + n, vec->el + pos, vec->size - pos);
571 isl_vec_free(vec);
572 return ext;
573 error:
574 isl_vec_free(vec);
575 isl_vec_free(ext);
576 return NULL;
579 __isl_give isl_vec *isl_vec_insert_zero_els(__isl_take isl_vec *vec,
580 unsigned pos, unsigned n)
582 vec = isl_vec_insert_els(vec, pos, n);
583 if (!vec)
584 return NULL;
586 isl_seq_clr(vec->el + pos, n);
588 return vec;
591 /* Move the "n" elements starting as "src_pos" of "vec"
592 * to "dst_pos". The elements originally at "dst_pos" are moved
593 * up or down depending on whether "dst_pos" is smaller or greater
594 * than "src_pos".
596 __isl_give isl_vec *isl_vec_move_els(__isl_take isl_vec *vec,
597 unsigned dst_pos, unsigned src_pos, unsigned n)
599 isl_vec *res;
601 if (!vec)
602 return NULL;
604 if (src_pos + n > vec->size)
605 isl_die(vec->ctx, isl_error_invalid,
606 "source range out of bounds", return isl_vec_free(vec));
607 if (dst_pos + n > vec->size)
608 isl_die(vec->ctx, isl_error_invalid,
609 "destination range out of bounds",
610 return isl_vec_free(vec));
612 if (n == 0 || dst_pos == src_pos)
613 return vec;
615 res = isl_vec_alloc(vec->ctx, vec->size);
616 if (!res)
617 return isl_vec_free(vec);
619 if (dst_pos < src_pos) {
620 isl_seq_cpy(res->el, vec->el, dst_pos);
621 isl_seq_cpy(res->el + dst_pos, vec->el + src_pos, n);
622 isl_seq_cpy(res->el + dst_pos + n,
623 vec->el + dst_pos, src_pos - dst_pos);
624 isl_seq_cpy(res->el + src_pos + n,
625 vec->el + src_pos + n, res->size - src_pos - n);
626 } else {
627 isl_seq_cpy(res->el, vec->el, src_pos);
628 isl_seq_cpy(res->el + src_pos,
629 vec->el + src_pos + n, dst_pos - src_pos);
630 isl_seq_cpy(res->el + dst_pos, vec->el + src_pos, n);
631 isl_seq_cpy(res->el + dst_pos + n,
632 vec->el + dst_pos + n, res->size - dst_pos - n);
635 isl_vec_free(vec);
636 return res;