isl_ast_graft_list_extract_hoistable_guard: handle call outside band
[isl.git] / isl_val.c
blob2343153068b78b9d5668f5a507f83f53a51e7f83
1 /*
2 * Copyright 2013 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
8 */
10 #include <isl_int.h>
11 #include <isl_ctx_private.h>
12 #include <isl_val_private.h>
14 #undef BASE
15 #define BASE val
17 #include <isl_list_templ.c>
19 /* Allocate an isl_val object with indeterminate value.
21 __isl_give isl_val *isl_val_alloc(isl_ctx *ctx)
23 isl_val *v;
25 v = isl_alloc_type(ctx, struct isl_val);
26 if (!v)
27 return NULL;
29 v->ctx = ctx;
30 isl_ctx_ref(ctx);
31 v->ref = 1;
32 isl_int_init(v->n);
33 isl_int_init(v->d);
35 return v;
38 /* Return a reference to an isl_val representing zero.
40 __isl_give isl_val *isl_val_zero(isl_ctx *ctx)
42 return isl_val_int_from_si(ctx, 0);
45 /* Return a reference to an isl_val representing one.
47 __isl_give isl_val *isl_val_one(isl_ctx *ctx)
49 return isl_val_int_from_si(ctx, 1);
52 /* Return a reference to an isl_val representing negative one.
54 __isl_give isl_val *isl_val_negone(isl_ctx *ctx)
56 return isl_val_int_from_si(ctx, -1);
59 /* Return a reference to an isl_val representing NaN.
61 __isl_give isl_val *isl_val_nan(isl_ctx *ctx)
63 isl_val *v;
65 v = isl_val_alloc(ctx);
66 if (!v)
67 return NULL;
69 isl_int_set_si(v->n, 0);
70 isl_int_set_si(v->d, 0);
72 return v;
75 /* Change "v" into a NaN.
77 __isl_give isl_val *isl_val_set_nan(__isl_take isl_val *v)
79 if (!v)
80 return NULL;
81 if (isl_val_is_nan(v))
82 return v;
83 v = isl_val_cow(v);
84 if (!v)
85 return NULL;
87 isl_int_set_si(v->n, 0);
88 isl_int_set_si(v->d, 0);
90 return v;
93 /* Return a reference to an isl_val representing +infinity.
95 __isl_give isl_val *isl_val_infty(isl_ctx *ctx)
97 isl_val *v;
99 v = isl_val_alloc(ctx);
100 if (!v)
101 return NULL;
103 isl_int_set_si(v->n, 1);
104 isl_int_set_si(v->d, 0);
106 return v;
109 /* Return a reference to an isl_val representing -infinity.
111 __isl_give isl_val *isl_val_neginfty(isl_ctx *ctx)
113 isl_val *v;
115 v = isl_val_alloc(ctx);
116 if (!v)
117 return NULL;
119 isl_int_set_si(v->n, -1);
120 isl_int_set_si(v->d, 0);
122 return v;
125 /* Return a reference to an isl_val representing the integer "i".
127 __isl_give isl_val *isl_val_int_from_si(isl_ctx *ctx, long i)
129 isl_val *v;
131 v = isl_val_alloc(ctx);
132 if (!v)
133 return NULL;
135 isl_int_set_si(v->n, i);
136 isl_int_set_si(v->d, 1);
138 return v;
141 /* Change the value of "v" to be equal to the integer "i".
143 __isl_give isl_val *isl_val_set_si(__isl_take isl_val *v, long i)
145 if (!v)
146 return NULL;
147 if (isl_val_is_int(v) && isl_int_cmp_si(v->n, i) == 0)
148 return v;
149 v = isl_val_cow(v);
150 if (!v)
151 return NULL;
153 isl_int_set_si(v->n, i);
154 isl_int_set_si(v->d, 1);
156 return v;
159 /* Change the value of "v" to be equal to zero.
161 __isl_give isl_val *isl_val_set_zero(__isl_take isl_val *v)
163 return isl_val_set_si(v, 0);
166 /* Return a reference to an isl_val representing the unsigned integer "u".
168 __isl_give isl_val *isl_val_int_from_ui(isl_ctx *ctx, unsigned long u)
170 isl_val *v;
172 v = isl_val_alloc(ctx);
173 if (!v)
174 return NULL;
176 isl_int_set_ui(v->n, u);
177 isl_int_set_si(v->d, 1);
179 return v;
182 /* Return a reference to an isl_val representing the integer "n".
184 __isl_give isl_val *isl_val_int_from_isl_int(isl_ctx *ctx, isl_int n)
186 isl_val *v;
188 v = isl_val_alloc(ctx);
189 if (!v)
190 return NULL;
192 isl_int_set(v->n, n);
193 isl_int_set_si(v->d, 1);
195 return v;
198 /* Return a reference to an isl_val representing the rational value "n"/"d".
199 * Normalizing the isl_val (if needed) is left to the caller.
201 __isl_give isl_val *isl_val_rat_from_isl_int(isl_ctx *ctx,
202 isl_int n, isl_int d)
204 isl_val *v;
206 v = isl_val_alloc(ctx);
207 if (!v)
208 return NULL;
210 isl_int_set(v->n, n);
211 isl_int_set(v->d, d);
213 return v;
216 /* Return a new reference to "v".
218 __isl_give isl_val *isl_val_copy(__isl_keep isl_val *v)
220 if (!v)
221 return NULL;
223 v->ref++;
224 return v;
227 /* Return a fresh copy of "val".
229 __isl_give isl_val *isl_val_dup(__isl_keep isl_val *val)
231 isl_val *dup;
233 if (!val)
234 return NULL;
236 dup = isl_val_alloc(isl_val_get_ctx(val));
237 if (!dup)
238 return NULL;
240 isl_int_set(dup->n, val->n);
241 isl_int_set(dup->d, val->d);
243 return dup;
246 /* Return an isl_val that is equal to "val" and that has only
247 * a single reference.
249 __isl_give isl_val *isl_val_cow(__isl_take isl_val *val)
251 if (!val)
252 return NULL;
254 if (val->ref == 1)
255 return val;
256 val->ref--;
257 return isl_val_dup(val);
260 /* Free "v" and return NULL.
262 __isl_null isl_val *isl_val_free(__isl_take isl_val *v)
264 if (!v)
265 return NULL;
267 if (--v->ref > 0)
268 return NULL;
270 isl_ctx_deref(v->ctx);
271 isl_int_clear(v->n);
272 isl_int_clear(v->d);
273 free(v);
274 return NULL;
277 /* Extract the numerator of a rational value "v" as an integer.
279 * If "v" is not a rational value, then the result is undefined.
281 long isl_val_get_num_si(__isl_keep isl_val *v)
283 if (!v)
284 return 0;
285 if (!isl_val_is_rat(v))
286 isl_die(isl_val_get_ctx(v), isl_error_invalid,
287 "expecting rational value", return 0);
288 if (!isl_int_fits_slong(v->n))
289 isl_die(isl_val_get_ctx(v), isl_error_invalid,
290 "numerator too large", return 0);
291 return isl_int_get_si(v->n);
294 /* Extract the numerator of a rational value "v" as an isl_int.
296 * If "v" is not a rational value, then the result is undefined.
298 int isl_val_get_num_isl_int(__isl_keep isl_val *v, isl_int *n)
300 if (!v)
301 return -1;
302 if (!isl_val_is_rat(v))
303 isl_die(isl_val_get_ctx(v), isl_error_invalid,
304 "expecting rational value", return -1);
305 isl_int_set(*n, v->n);
306 return 0;
309 /* Extract the denominator of a rational value "v" as an integer.
311 * If "v" is not a rational value, then the result is undefined.
313 long isl_val_get_den_si(__isl_keep isl_val *v)
315 if (!v)
316 return 0;
317 if (!isl_val_is_rat(v))
318 isl_die(isl_val_get_ctx(v), isl_error_invalid,
319 "expecting rational value", return 0);
320 if (!isl_int_fits_slong(v->d))
321 isl_die(isl_val_get_ctx(v), isl_error_invalid,
322 "denominator too large", return 0);
323 return isl_int_get_si(v->d);
326 /* Return an approximation of "v" as a double.
328 double isl_val_get_d(__isl_keep isl_val *v)
330 if (!v)
331 return 0;
332 if (!isl_val_is_rat(v))
333 isl_die(isl_val_get_ctx(v), isl_error_invalid,
334 "expecting rational value", return 0);
335 return isl_int_get_d(v->n) / isl_int_get_d(v->d);
338 /* Return the isl_ctx to which "val" belongs.
340 isl_ctx *isl_val_get_ctx(__isl_keep isl_val *val)
342 return val ? val->ctx : NULL;
345 /* Normalize "v".
347 * In particular, make sure that the denominator of a rational value
348 * is positive and the numerator and denominator do not have any
349 * common divisors.
351 * This function should not be called by an external user
352 * since it will only be given normalized values.
354 __isl_give isl_val *isl_val_normalize(__isl_take isl_val *v)
356 isl_ctx *ctx;
358 if (!v)
359 return NULL;
360 if (isl_val_is_int(v))
361 return v;
362 if (!isl_val_is_rat(v))
363 return v;
364 if (isl_int_is_neg(v->d)) {
365 isl_int_neg(v->d, v->d);
366 isl_int_neg(v->n, v->n);
368 ctx = isl_val_get_ctx(v);
369 isl_int_gcd(ctx->normalize_gcd, v->n, v->d);
370 if (isl_int_is_one(ctx->normalize_gcd))
371 return v;
372 isl_int_divexact(v->n, v->n, ctx->normalize_gcd);
373 isl_int_divexact(v->d, v->d, ctx->normalize_gcd);
374 return v;
377 /* Return the opposite of "v".
379 __isl_give isl_val *isl_val_neg(__isl_take isl_val *v)
381 if (!v)
382 return NULL;
383 if (isl_val_is_nan(v))
384 return v;
385 if (isl_val_is_zero(v))
386 return v;
388 v = isl_val_cow(v);
389 if (!v)
390 return NULL;
391 isl_int_neg(v->n, v->n);
393 return v;
396 /* Return the inverse of "v".
398 __isl_give isl_val *isl_val_inv(__isl_take isl_val *v)
400 if (!v)
401 return NULL;
402 if (isl_val_is_nan(v))
403 return v;
404 if (isl_val_is_zero(v)) {
405 isl_ctx *ctx = isl_val_get_ctx(v);
406 isl_val_free(v);
407 return isl_val_nan(ctx);
409 if (isl_val_is_infty(v) || isl_val_is_neginfty(v)) {
410 isl_ctx *ctx = isl_val_get_ctx(v);
411 isl_val_free(v);
412 return isl_val_zero(ctx);
415 v = isl_val_cow(v);
416 if (!v)
417 return NULL;
418 isl_int_swap(v->n, v->d);
420 return isl_val_normalize(v);
423 /* Return the absolute value of "v".
425 __isl_give isl_val *isl_val_abs(__isl_take isl_val *v)
427 if (!v)
428 return NULL;
429 if (isl_val_is_nan(v))
430 return v;
431 if (isl_val_is_nonneg(v))
432 return v;
433 return isl_val_neg(v);
436 /* Return the "floor" (greatest integer part) of "v".
437 * That is, return the result of rounding towards -infinity.
439 __isl_give isl_val *isl_val_floor(__isl_take isl_val *v)
441 if (!v)
442 return NULL;
443 if (isl_val_is_int(v))
444 return v;
445 if (!isl_val_is_rat(v))
446 return v;
448 v = isl_val_cow(v);
449 if (!v)
450 return NULL;
451 isl_int_fdiv_q(v->n, v->n, v->d);
452 isl_int_set_si(v->d, 1);
454 return v;
457 /* Return the "ceiling" of "v".
458 * That is, return the result of rounding towards +infinity.
460 __isl_give isl_val *isl_val_ceil(__isl_take isl_val *v)
462 if (!v)
463 return NULL;
464 if (isl_val_is_int(v))
465 return v;
466 if (!isl_val_is_rat(v))
467 return v;
469 v = isl_val_cow(v);
470 if (!v)
471 return NULL;
472 isl_int_cdiv_q(v->n, v->n, v->d);
473 isl_int_set_si(v->d, 1);
475 return v;
478 /* Truncate "v".
479 * That is, return the result of rounding towards zero.
481 __isl_give isl_val *isl_val_trunc(__isl_take isl_val *v)
483 if (!v)
484 return NULL;
485 if (isl_val_is_int(v))
486 return v;
487 if (!isl_val_is_rat(v))
488 return v;
490 v = isl_val_cow(v);
491 if (!v)
492 return NULL;
493 isl_int_tdiv_q(v->n, v->n, v->d);
494 isl_int_set_si(v->d, 1);
496 return v;
499 /* Return 2^v, where v is an integer (that is not too large).
501 __isl_give isl_val *isl_val_2exp(__isl_take isl_val *v)
503 unsigned long exp;
504 int neg;
506 v = isl_val_cow(v);
507 if (!v)
508 return NULL;
509 if (!isl_val_is_int(v))
510 isl_die(isl_val_get_ctx(v), isl_error_invalid,
511 "can only compute integer powers",
512 return isl_val_free(v));
513 neg = isl_val_is_neg(v);
514 if (neg)
515 isl_int_neg(v->n, v->n);
516 if (!isl_int_fits_ulong(v->n))
517 isl_die(isl_val_get_ctx(v), isl_error_invalid,
518 "exponent too large", return isl_val_free(v));
519 exp = isl_int_get_ui(v->n);
520 if (neg) {
521 isl_int_mul_2exp(v->d, v->d, exp);
522 isl_int_set_si(v->n, 1);
523 } else {
524 isl_int_mul_2exp(v->n, v->d, exp);
527 return v;
530 /* Return the minimum of "v1" and "v2".
532 __isl_give isl_val *isl_val_min(__isl_take isl_val *v1, __isl_take isl_val *v2)
534 if (!v1 || !v2)
535 goto error;
537 if (isl_val_is_nan(v1)) {
538 isl_val_free(v2);
539 return v1;
541 if (isl_val_is_nan(v2)) {
542 isl_val_free(v1);
543 return v2;
545 if (isl_val_le(v1, v2)) {
546 isl_val_free(v2);
547 return v1;
548 } else {
549 isl_val_free(v1);
550 return v2;
552 error:
553 isl_val_free(v1);
554 isl_val_free(v2);
555 return NULL;
558 /* Return the maximum of "v1" and "v2".
560 __isl_give isl_val *isl_val_max(__isl_take isl_val *v1, __isl_take isl_val *v2)
562 if (!v1 || !v2)
563 goto error;
565 if (isl_val_is_nan(v1)) {
566 isl_val_free(v2);
567 return v1;
569 if (isl_val_is_nan(v2)) {
570 isl_val_free(v1);
571 return v2;
573 if (isl_val_ge(v1, v2)) {
574 isl_val_free(v2);
575 return v1;
576 } else {
577 isl_val_free(v1);
578 return v2;
580 error:
581 isl_val_free(v1);
582 isl_val_free(v2);
583 return NULL;
586 /* Return the sum of "v1" and "v2".
588 __isl_give isl_val *isl_val_add(__isl_take isl_val *v1, __isl_take isl_val *v2)
590 if (!v1 || !v2)
591 goto error;
592 if (isl_val_is_nan(v1)) {
593 isl_val_free(v2);
594 return v1;
596 if (isl_val_is_nan(v2)) {
597 isl_val_free(v1);
598 return v2;
600 if ((isl_val_is_infty(v1) && isl_val_is_neginfty(v2)) ||
601 (isl_val_is_neginfty(v1) && isl_val_is_infty(v2))) {
602 isl_val_free(v2);
603 return isl_val_set_nan(v1);
605 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
606 isl_val_free(v2);
607 return v1;
609 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
610 isl_val_free(v1);
611 return v2;
613 if (isl_val_is_zero(v1)) {
614 isl_val_free(v1);
615 return v2;
617 if (isl_val_is_zero(v2)) {
618 isl_val_free(v2);
619 return v1;
622 v1 = isl_val_cow(v1);
623 if (!v1)
624 goto error;
625 if (isl_val_is_int(v1) && isl_val_is_int(v2))
626 isl_int_add(v1->n, v1->n, v2->n);
627 else {
628 if (isl_int_eq(v1->d, v2->d))
629 isl_int_add(v1->n, v1->n, v2->n);
630 else {
631 isl_int_mul(v1->n, v1->n, v2->d);
632 isl_int_addmul(v1->n, v2->n, v1->d);
633 isl_int_mul(v1->d, v1->d, v2->d);
635 v1 = isl_val_normalize(v1);
637 isl_val_free(v2);
638 return v1;
639 error:
640 isl_val_free(v1);
641 isl_val_free(v2);
642 return NULL;
645 /* Return the sum of "v1" and "v2".
647 __isl_give isl_val *isl_val_add_ui(__isl_take isl_val *v1, unsigned long v2)
649 if (!v1)
650 return NULL;
651 if (!isl_val_is_rat(v1))
652 return v1;
653 if (v2 == 0)
654 return v1;
655 v1 = isl_val_cow(v1);
656 if (!v1)
657 return NULL;
659 isl_int_addmul_ui(v1->n, v1->d, v2);
661 return v1;
664 /* Subtract "v2" from "v1".
666 __isl_give isl_val *isl_val_sub(__isl_take isl_val *v1, __isl_take isl_val *v2)
668 if (!v1 || !v2)
669 goto error;
670 if (isl_val_is_nan(v1)) {
671 isl_val_free(v2);
672 return v1;
674 if (isl_val_is_nan(v2)) {
675 isl_val_free(v1);
676 return v2;
678 if ((isl_val_is_infty(v1) && isl_val_is_infty(v2)) ||
679 (isl_val_is_neginfty(v1) && isl_val_is_neginfty(v2))) {
680 isl_val_free(v2);
681 return isl_val_set_nan(v1);
683 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
684 isl_val_free(v2);
685 return v1;
687 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
688 isl_val_free(v1);
689 return isl_val_neg(v2);
691 if (isl_val_is_zero(v2)) {
692 isl_val_free(v2);
693 return v1;
695 if (isl_val_is_zero(v1)) {
696 isl_val_free(v1);
697 return isl_val_neg(v2);
700 v1 = isl_val_cow(v1);
701 if (!v1)
702 goto error;
703 if (isl_val_is_int(v1) && isl_val_is_int(v2))
704 isl_int_sub(v1->n, v1->n, v2->n);
705 else {
706 if (isl_int_eq(v1->d, v2->d))
707 isl_int_sub(v1->n, v1->n, v2->n);
708 else {
709 isl_int_mul(v1->n, v1->n, v2->d);
710 isl_int_submul(v1->n, v2->n, v1->d);
711 isl_int_mul(v1->d, v1->d, v2->d);
713 v1 = isl_val_normalize(v1);
715 isl_val_free(v2);
716 return v1;
717 error:
718 isl_val_free(v1);
719 isl_val_free(v2);
720 return NULL;
723 /* Subtract "v2" from "v1".
725 __isl_give isl_val *isl_val_sub_ui(__isl_take isl_val *v1, unsigned long v2)
727 if (!v1)
728 return NULL;
729 if (!isl_val_is_rat(v1))
730 return v1;
731 if (v2 == 0)
732 return v1;
733 v1 = isl_val_cow(v1);
734 if (!v1)
735 return NULL;
737 isl_int_submul_ui(v1->n, v1->d, v2);
739 return v1;
742 /* Return the product of "v1" and "v2".
744 __isl_give isl_val *isl_val_mul(__isl_take isl_val *v1, __isl_take isl_val *v2)
746 if (!v1 || !v2)
747 goto error;
748 if (isl_val_is_nan(v1)) {
749 isl_val_free(v2);
750 return v1;
752 if (isl_val_is_nan(v2)) {
753 isl_val_free(v1);
754 return v2;
756 if ((!isl_val_is_rat(v1) && isl_val_is_zero(v2)) ||
757 (isl_val_is_zero(v1) && !isl_val_is_rat(v2))) {
758 isl_val_free(v2);
759 return isl_val_set_nan(v1);
761 if (isl_val_is_zero(v1)) {
762 isl_val_free(v2);
763 return v1;
765 if (isl_val_is_zero(v2)) {
766 isl_val_free(v1);
767 return v2;
769 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
770 if (isl_val_is_neg(v2))
771 v1 = isl_val_neg(v1);
772 isl_val_free(v2);
773 return v1;
775 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
776 if (isl_val_is_neg(v1))
777 v2 = isl_val_neg(v2);
778 isl_val_free(v1);
779 return v2;
782 v1 = isl_val_cow(v1);
783 if (!v1)
784 goto error;
785 if (isl_val_is_int(v1) && isl_val_is_int(v2))
786 isl_int_mul(v1->n, v1->n, v2->n);
787 else {
788 isl_int_mul(v1->n, v1->n, v2->n);
789 isl_int_mul(v1->d, v1->d, v2->d);
790 v1 = isl_val_normalize(v1);
792 isl_val_free(v2);
793 return v1;
794 error:
795 isl_val_free(v1);
796 isl_val_free(v2);
797 return NULL;
800 /* Return the product of "v1" and "v2".
802 * This is a private copy of isl_val_mul for use in the generic
803 * isl_multi_*_scale_val instantiated for isl_val.
805 __isl_give isl_val *isl_val_scale_val(__isl_take isl_val *v1,
806 __isl_take isl_val *v2)
808 return isl_val_mul(v1, v2);
811 /* Return the product of "v1" and "v2".
813 __isl_give isl_val *isl_val_mul_ui(__isl_take isl_val *v1, unsigned long v2)
815 if (!v1)
816 return NULL;
817 if (isl_val_is_nan(v1))
818 return v1;
819 if (!isl_val_is_rat(v1)) {
820 if (v2 == 0)
821 v1 = isl_val_set_nan(v1);
822 return v1;
824 if (v2 == 1)
825 return v1;
826 v1 = isl_val_cow(v1);
827 if (!v1)
828 return NULL;
830 isl_int_mul_ui(v1->n, v1->n, v2);
832 return isl_val_normalize(v1);
835 /* Divide "v1" by "v2".
837 __isl_give isl_val *isl_val_div(__isl_take isl_val *v1, __isl_take isl_val *v2)
839 if (!v1 || !v2)
840 goto error;
841 if (isl_val_is_nan(v1)) {
842 isl_val_free(v2);
843 return v1;
845 if (isl_val_is_nan(v2)) {
846 isl_val_free(v1);
847 return v2;
849 if (isl_val_is_zero(v2) ||
850 (!isl_val_is_rat(v1) && !isl_val_is_rat(v2))) {
851 isl_val_free(v2);
852 return isl_val_set_nan(v1);
854 if (isl_val_is_zero(v1)) {
855 isl_val_free(v2);
856 return v1;
858 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
859 if (isl_val_is_neg(v2))
860 v1 = isl_val_neg(v1);
861 isl_val_free(v2);
862 return v1;
864 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
865 isl_val_free(v2);
866 return isl_val_set_zero(v1);
869 v1 = isl_val_cow(v1);
870 if (!v1)
871 goto error;
872 if (isl_val_is_int(v2)) {
873 isl_int_mul(v1->d, v1->d, v2->n);
874 v1 = isl_val_normalize(v1);
875 } else {
876 isl_int_mul(v1->d, v1->d, v2->n);
877 isl_int_mul(v1->n, v1->n, v2->d);
878 v1 = isl_val_normalize(v1);
880 isl_val_free(v2);
881 return v1;
882 error:
883 isl_val_free(v1);
884 isl_val_free(v2);
885 return NULL;
888 /* Divide "v1" by "v2".
890 * This is a private copy of isl_val_div for use in the generic
891 * isl_multi_*_scale_down_val instantiated for isl_val.
893 __isl_give isl_val *isl_val_scale_down_val(__isl_take isl_val *v1,
894 __isl_take isl_val *v2)
896 return isl_val_div(v1, v2);
899 /* Given two integer values "v1" and "v2", check if "v1" is divisible by "v2".
901 int isl_val_is_divisible_by(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
903 if (!v1 || !v2)
904 return -1;
906 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
907 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
908 "expecting two integers", return -1);
910 return isl_int_is_divisible_by(v1->n, v2->n);
913 /* Given two integer values "v1" and "v2", return the residue of "v1"
914 * modulo "v2".
916 __isl_give isl_val *isl_val_mod(__isl_take isl_val *v1, __isl_take isl_val *v2)
918 if (!v1 || !v2)
919 goto error;
920 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
921 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
922 "expecting two integers", goto error);
923 if (isl_val_is_nonneg(v1) && isl_val_lt(v1, v2)) {
924 isl_val_free(v2);
925 return v1;
927 v1 = isl_val_cow(v1);
928 if (!v1)
929 goto error;
930 isl_int_fdiv_r(v1->n, v1->n, v2->n);
931 isl_val_free(v2);
932 return v1;
933 error:
934 isl_val_free(v1);
935 isl_val_free(v2);
936 return NULL;
939 /* Given two integer values "v1" and "v2", return the residue of "v1"
940 * modulo "v2".
942 * This is a private copy of isl_val_mod for use in the generic
943 * isl_multi_*_mod_multi_val instantiated for isl_val.
945 __isl_give isl_val *isl_val_mod_val(__isl_take isl_val *v1,
946 __isl_take isl_val *v2)
948 return isl_val_mod(v1, v2);
951 /* Given two integer values, return their greatest common divisor.
953 __isl_give isl_val *isl_val_gcd(__isl_take isl_val *v1, __isl_take isl_val *v2)
955 if (!v1 || !v2)
956 goto error;
957 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
958 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
959 "expecting two integers", goto error);
960 if (isl_val_eq(v1, v2)) {
961 isl_val_free(v2);
962 return v1;
964 if (isl_val_is_one(v1)) {
965 isl_val_free(v2);
966 return v1;
968 if (isl_val_is_one(v2)) {
969 isl_val_free(v1);
970 return v2;
972 v1 = isl_val_cow(v1);
973 if (!v1)
974 goto error;
975 isl_int_gcd(v1->n, v1->n, v2->n);
976 isl_val_free(v2);
977 return v1;
978 error:
979 isl_val_free(v1);
980 isl_val_free(v2);
981 return NULL;
984 /* Compute x, y and g such that g = gcd(a,b) and a*x+b*y = g.
986 static void isl_int_gcdext(isl_int g, isl_int x, isl_int y,
987 isl_int a, isl_int b)
989 isl_int d, tmp;
990 isl_int a_copy, b_copy;
992 isl_int_init(a_copy);
993 isl_int_init(b_copy);
994 isl_int_init(d);
995 isl_int_init(tmp);
996 isl_int_set(a_copy, a);
997 isl_int_set(b_copy, b);
998 isl_int_abs(g, a_copy);
999 isl_int_abs(d, b_copy);
1000 isl_int_set_si(x, 1);
1001 isl_int_set_si(y, 0);
1002 while (isl_int_is_pos(d)) {
1003 isl_int_fdiv_q(tmp, g, d);
1004 isl_int_submul(x, tmp, y);
1005 isl_int_submul(g, tmp, d);
1006 isl_int_swap(g, d);
1007 isl_int_swap(x, y);
1009 if (isl_int_is_zero(a_copy))
1010 isl_int_set_si(x, 0);
1011 else if (isl_int_is_neg(a_copy))
1012 isl_int_neg(x, x);
1013 if (isl_int_is_zero(b_copy))
1014 isl_int_set_si(y, 0);
1015 else {
1016 isl_int_mul(tmp, a_copy, x);
1017 isl_int_sub(tmp, g, tmp);
1018 isl_int_divexact(y, tmp, b_copy);
1020 isl_int_clear(d);
1021 isl_int_clear(tmp);
1022 isl_int_clear(a_copy);
1023 isl_int_clear(b_copy);
1026 /* Given two integer values v1 and v2, return their greatest common divisor g,
1027 * as well as two integers x and y such that x * v1 + y * v2 = g.
1029 __isl_give isl_val *isl_val_gcdext(__isl_take isl_val *v1,
1030 __isl_take isl_val *v2, __isl_give isl_val **x, __isl_give isl_val **y)
1032 isl_ctx *ctx;
1033 isl_val *a = NULL, *b = NULL;
1035 if (!x && !y)
1036 return isl_val_gcd(v1, v2);
1038 if (!v1 || !v2)
1039 goto error;
1041 ctx = isl_val_get_ctx(v1);
1042 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
1043 isl_die(ctx, isl_error_invalid,
1044 "expecting two integers", goto error);
1046 v1 = isl_val_cow(v1);
1047 a = isl_val_alloc(ctx);
1048 b = isl_val_alloc(ctx);
1049 if (!v1 || !a || !b)
1050 goto error;
1051 isl_int_gcdext(v1->n, a->n, b->n, v1->n, v2->n);
1052 if (x) {
1053 isl_int_set_si(a->d, 1);
1054 *x = a;
1055 } else
1056 isl_val_free(a);
1057 if (y) {
1058 isl_int_set_si(b->d, 1);
1059 *y = b;
1060 } else
1061 isl_val_free(b);
1062 isl_val_free(v2);
1063 return v1;
1064 error:
1065 isl_val_free(v1);
1066 isl_val_free(v2);
1067 isl_val_free(a);
1068 isl_val_free(b);
1069 if (x)
1070 *x = NULL;
1071 if (y)
1072 *y = NULL;
1073 return NULL;
1076 /* Does "v" represent an integer value?
1078 int isl_val_is_int(__isl_keep isl_val *v)
1080 if (!v)
1081 return -1;
1083 return isl_int_is_one(v->d);
1086 /* Does "v" represent a rational value?
1088 int isl_val_is_rat(__isl_keep isl_val *v)
1090 if (!v)
1091 return -1;
1093 return !isl_int_is_zero(v->d);
1096 /* Does "v" represent NaN?
1098 int isl_val_is_nan(__isl_keep isl_val *v)
1100 if (!v)
1101 return -1;
1103 return isl_int_is_zero(v->n) && isl_int_is_zero(v->d);
1106 /* Does "v" represent +infinity?
1108 int isl_val_is_infty(__isl_keep isl_val *v)
1110 if (!v)
1111 return -1;
1113 return isl_int_is_pos(v->n) && isl_int_is_zero(v->d);
1116 /* Does "v" represent -infinity?
1118 int isl_val_is_neginfty(__isl_keep isl_val *v)
1120 if (!v)
1121 return -1;
1123 return isl_int_is_neg(v->n) && isl_int_is_zero(v->d);
1126 /* Does "v" represent the integer zero?
1128 int isl_val_is_zero(__isl_keep isl_val *v)
1130 if (!v)
1131 return -1;
1133 return isl_int_is_zero(v->n) && !isl_int_is_zero(v->d);
1136 /* Does "v" represent the integer one?
1138 int isl_val_is_one(__isl_keep isl_val *v)
1140 if (!v)
1141 return -1;
1143 return isl_int_eq(v->n, v->d);
1146 /* Does "v" represent the integer negative one?
1148 int isl_val_is_negone(__isl_keep isl_val *v)
1150 if (!v)
1151 return -1;
1153 return isl_int_is_neg(v->n) && isl_int_abs_eq(v->n, v->d);
1156 /* Is "v" (strictly) positive?
1158 int isl_val_is_pos(__isl_keep isl_val *v)
1160 if (!v)
1161 return -1;
1163 return isl_int_is_pos(v->n);
1166 /* Is "v" (strictly) negative?
1168 int isl_val_is_neg(__isl_keep isl_val *v)
1170 if (!v)
1171 return -1;
1173 return isl_int_is_neg(v->n);
1176 /* Is "v" non-negative?
1178 int isl_val_is_nonneg(__isl_keep isl_val *v)
1180 if (!v)
1181 return -1;
1183 if (isl_val_is_nan(v))
1184 return 0;
1186 return isl_int_is_nonneg(v->n);
1189 /* Is "v" non-positive?
1191 int isl_val_is_nonpos(__isl_keep isl_val *v)
1193 if (!v)
1194 return -1;
1196 if (isl_val_is_nan(v))
1197 return 0;
1199 return isl_int_is_nonpos(v->n);
1202 /* Return the sign of "v".
1204 * The sign of NaN is undefined.
1206 int isl_val_sgn(__isl_keep isl_val *v)
1208 if (!v)
1209 return 0;
1210 if (isl_val_is_zero(v))
1211 return 0;
1212 if (isl_val_is_pos(v))
1213 return 1;
1214 return -1;
1217 /* Is "v1" (strictly) less than "v2"?
1219 int isl_val_lt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1221 isl_int t;
1222 int lt;
1224 if (!v1 || !v2)
1225 return -1;
1226 if (isl_val_is_int(v1) && isl_val_is_int(v2))
1227 return isl_int_lt(v1->n, v2->n);
1228 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1229 return 0;
1230 if (isl_val_eq(v1, v2))
1231 return 0;
1232 if (isl_val_is_infty(v2))
1233 return 1;
1234 if (isl_val_is_infty(v1))
1235 return 0;
1236 if (isl_val_is_neginfty(v1))
1237 return 1;
1238 if (isl_val_is_neginfty(v2))
1239 return 0;
1241 isl_int_init(t);
1242 isl_int_mul(t, v1->n, v2->d);
1243 isl_int_submul(t, v2->n, v1->d);
1244 lt = isl_int_is_neg(t);
1245 isl_int_clear(t);
1247 return lt;
1250 /* Is "v1" (strictly) greater than "v2"?
1252 int isl_val_gt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1254 return isl_val_lt(v2, v1);
1257 /* Is "v1" less than or equal to "v2"?
1259 int isl_val_le(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1261 isl_int t;
1262 int le;
1264 if (!v1 || !v2)
1265 return -1;
1266 if (isl_val_is_int(v1) && isl_val_is_int(v2))
1267 return isl_int_le(v1->n, v2->n);
1268 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1269 return 0;
1270 if (isl_val_eq(v1, v2))
1271 return 1;
1272 if (isl_val_is_infty(v2))
1273 return 1;
1274 if (isl_val_is_infty(v1))
1275 return 0;
1276 if (isl_val_is_neginfty(v1))
1277 return 1;
1278 if (isl_val_is_neginfty(v2))
1279 return 0;
1281 isl_int_init(t);
1282 isl_int_mul(t, v1->n, v2->d);
1283 isl_int_submul(t, v2->n, v1->d);
1284 le = isl_int_is_nonpos(t);
1285 isl_int_clear(t);
1287 return le;
1290 /* Is "v1" greater than or equal to "v2"?
1292 int isl_val_ge(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1294 return isl_val_le(v2, v1);
1297 /* How does "v" compare to "i"?
1299 * Return 1 if v is greater, -1 if v is smaller and 0 if v is equal to i.
1301 * If v is NaN (or NULL), then the result is undefined.
1303 int isl_val_cmp_si(__isl_keep isl_val *v, long i)
1305 isl_int t;
1306 int cmp;
1308 if (!v)
1309 return 0;
1310 if (isl_val_is_int(v))
1311 return isl_int_cmp_si(v->n, i);
1312 if (isl_val_is_nan(v))
1313 return 0;
1314 if (isl_val_is_infty(v))
1315 return 1;
1316 if (isl_val_is_neginfty(v))
1317 return -1;
1319 isl_int_init(t);
1320 isl_int_mul_si(t, v->d, i);
1321 isl_int_sub(t, v->n, t);
1322 cmp = isl_int_sgn(t);
1323 isl_int_clear(t);
1325 return cmp;
1328 /* Is "v1" equal to "v2"?
1330 int isl_val_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1332 if (!v1 || !v2)
1333 return -1;
1334 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1335 return 0;
1337 return isl_int_eq(v1->n, v2->n) && isl_int_eq(v1->d, v2->d);
1340 /* Is "v1" equal to "v2" in absolute value?
1342 int isl_val_abs_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1344 if (!v1 || !v2)
1345 return -1;
1346 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1347 return 0;
1349 return isl_int_abs_eq(v1->n, v2->n) && isl_int_eq(v1->d, v2->d);
1352 /* Is "v1" different from "v2"?
1354 int isl_val_ne(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1356 if (!v1 || !v2)
1357 return -1;
1358 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1359 return 0;
1361 return isl_int_ne(v1->n, v2->n) || isl_int_ne(v1->d, v2->d);
1364 /* Print a textual representation of "v" onto "p".
1366 __isl_give isl_printer *isl_printer_print_val(__isl_take isl_printer *p,
1367 __isl_keep isl_val *v)
1369 int neg;
1371 if (!p || !v)
1372 return isl_printer_free(p);
1374 neg = isl_int_is_neg(v->n);
1375 if (neg) {
1376 p = isl_printer_print_str(p, "-");
1377 isl_int_neg(v->n, v->n);
1379 if (isl_int_is_zero(v->d)) {
1380 int sgn = isl_int_sgn(v->n);
1381 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1382 sgn == 0 ? "NaN" : "infty");
1383 } else
1384 p = isl_printer_print_isl_int(p, v->n);
1385 if (neg)
1386 isl_int_neg(v->n, v->n);
1387 if (!isl_int_is_zero(v->d) && !isl_int_is_one(v->d)) {
1388 p = isl_printer_print_str(p, "/");
1389 p = isl_printer_print_isl_int(p, v->d);
1392 return p;
1395 /* Is "val1" (obviously) equal to "val2"?
1397 * This is a private copy of isl_val_eq for use in the generic
1398 * isl_multi_*_plain_is_equal instantiated for isl_val.
1400 int isl_val_plain_is_equal(__isl_keep isl_val *val1, __isl_keep isl_val *val2)
1402 return isl_val_eq(val1, val2);
1405 /* Does "v" have any non-zero coefficients
1406 * for any dimension in the given range?
1408 * This function is only meant to be used in the generic isl_multi_*
1409 * functions which have to deal with base objects that have an associated
1410 * space. Since an isl_val does not have any coefficients, this function
1411 * always return 0.
1413 int isl_val_involves_dims(__isl_keep isl_val *v, enum isl_dim_type type,
1414 unsigned first, unsigned n)
1416 if (!v)
1417 return -1;
1419 return 0;
1422 /* Insert "n" dimensions of type "type" at position "first".
1424 * This function is only meant to be used in the generic isl_multi_*
1425 * functions which have to deal with base objects that have an associated
1426 * space. Since an isl_val does not have an associated space, this function
1427 * does not do anything.
1429 __isl_give isl_val *isl_val_insert_dims(__isl_take isl_val *v,
1430 enum isl_dim_type type, unsigned first, unsigned n)
1432 return v;
1435 /* Drop the the "n" first dimensions of type "type" at position "first".
1437 * This function is only meant to be used in the generic isl_multi_*
1438 * functions which have to deal with base objects that have an associated
1439 * space. Since an isl_val does not have an associated space, this function
1440 * does not do anything.
1442 __isl_give isl_val *isl_val_drop_dims(__isl_take isl_val *v,
1443 enum isl_dim_type type, unsigned first, unsigned n)
1445 return v;
1448 /* Change the name of the dimension of type "type" at position "pos" to "s".
1450 * This function is only meant to be used in the generic isl_multi_*
1451 * functions which have to deal with base objects that have an associated
1452 * space. Since an isl_val does not have an associated space, this function
1453 * does not do anything.
1455 __isl_give isl_val *isl_val_set_dim_name(__isl_take isl_val *v,
1456 enum isl_dim_type type, unsigned pos, const char *s)
1458 return v;
1461 /* Return the space of "v".
1463 * This function is only meant to be used in the generic isl_multi_*
1464 * functions which have to deal with base objects that have an associated
1465 * space. The conditions surrounding the call to this function make sure
1466 * that this function will never actually get called. We return a valid
1467 * space anyway, just in case.
1469 __isl_give isl_space *isl_val_get_space(__isl_keep isl_val *v)
1471 if (!v)
1472 return NULL;
1474 return isl_space_params_alloc(isl_val_get_ctx(v), 0);
1477 /* Reset the domain space of "v" to "space".
1479 * This function is only meant to be used in the generic isl_multi_*
1480 * functions which have to deal with base objects that have an associated
1481 * space. Since an isl_val does not have an associated space, this function
1482 * does not do anything, apart from error handling and cleaning up memory.
1484 __isl_give isl_val *isl_val_reset_domain_space(__isl_take isl_val *v,
1485 __isl_take isl_space *space)
1487 if (!space)
1488 return isl_val_free(v);
1489 isl_space_free(space);
1490 return v;
1493 /* Align the parameters of "v" to those of "space".
1495 * This function is only meant to be used in the generic isl_multi_*
1496 * functions which have to deal with base objects that have an associated
1497 * space. Since an isl_val does not have an associated space, this function
1498 * does not do anything, apart from error handling and cleaning up memory.
1499 * Note that the conditions surrounding the call to this function make sure
1500 * that this function will never actually get called.
1502 __isl_give isl_val *isl_val_align_params(__isl_take isl_val *v,
1503 __isl_take isl_space *space)
1505 if (!space)
1506 return isl_val_free(v);
1507 isl_space_free(space);
1508 return v;
1511 /* Reorder the dimensions of the domain of "v" according
1512 * to the given reordering.
1514 * This function is only meant to be used in the generic isl_multi_*
1515 * functions which have to deal with base objects that have an associated
1516 * space. Since an isl_val does not have an associated space, this function
1517 * does not do anything, apart from error handling and cleaning up memory.
1519 __isl_give isl_val *isl_val_realign_domain(__isl_take isl_val *v,
1520 __isl_take isl_reordering *r)
1522 if (!r)
1523 return isl_val_free(v);
1524 isl_reordering_free(r);
1525 return v;
1528 /* Return an isl_val that is zero on "ls".
1530 * This function is only meant to be used in the generic isl_multi_*
1531 * functions which have to deal with base objects that have an associated
1532 * space. Since an isl_val does not have an associated space, this function
1533 * simply returns a zero isl_val in the same context as "ls".
1535 __isl_give isl_val *isl_val_zero_on_domain(__isl_take isl_local_space *ls)
1537 isl_ctx *ctx;
1539 if (!ls)
1540 return NULL;
1541 ctx = isl_local_space_get_ctx(ls);
1542 isl_local_space_free(ls);
1543 return isl_val_zero(ctx);
1546 /* Do the parameters of "v" match those of "space"?
1548 * This function is only meant to be used in the generic isl_multi_*
1549 * functions which have to deal with base objects that have an associated
1550 * space. Since an isl_val does not have an associated space, this function
1551 * simply returns 1, except if "v" or "space" are NULL.
1553 int isl_val_matching_params(__isl_keep isl_val *v, __isl_keep isl_space *space)
1555 if (!v || !space)
1556 return -1;
1557 return 1;
1560 /* Check that the domain space of "v" matches "space".
1562 * Return 0 on success and -1 on error.
1564 * This function is only meant to be used in the generic isl_multi_*
1565 * functions which have to deal with base objects that have an associated
1566 * space. Since an isl_val does not have an associated space, this function
1567 * simply returns 0, except if "v" or "space" are NULL.
1569 int isl_val_check_match_domain_space(__isl_keep isl_val *v,
1570 __isl_keep isl_space *space)
1572 if (!v || !space)
1573 return -1;
1574 return 0;
1577 #undef BASE
1578 #define BASE val
1580 #define NO_DOMAIN
1581 #define NO_IDENTITY
1582 #define NO_FROM_BASE
1583 #define NO_MOVE_DIMS
1584 #include <isl_multi_templ.c>
1586 /* Apply "fn" to each of the elements of "mv" with as second argument "v".
1588 static __isl_give isl_multi_val *isl_multi_val_fn_val(
1589 __isl_take isl_multi_val *mv,
1590 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
1591 __isl_take isl_val *v2),
1592 __isl_take isl_val *v)
1594 int i;
1596 mv = isl_multi_val_cow(mv);
1597 if (!mv || !v)
1598 goto error;
1600 for (i = 0; i < mv->n; ++i) {
1601 mv->p[i] = fn(mv->p[i], isl_val_copy(v));
1602 if (!mv->p[i])
1603 goto error;
1606 isl_val_free(v);
1607 return mv;
1608 error:
1609 isl_val_free(v);
1610 isl_multi_val_free(mv);
1611 return NULL;
1614 /* Add "v" to each of the elements of "mv".
1616 __isl_give isl_multi_val *isl_multi_val_add_val(__isl_take isl_multi_val *mv,
1617 __isl_take isl_val *v)
1619 if (!v)
1620 return isl_multi_val_free(mv);
1621 if (isl_val_is_zero(v)) {
1622 isl_val_free(v);
1623 return mv;
1625 return isl_multi_val_fn_val(mv, &isl_val_add, v);
1628 /* Reduce the elements of "mv" modulo "v".
1630 __isl_give isl_multi_val *isl_multi_val_mod_val(__isl_take isl_multi_val *mv,
1631 __isl_take isl_val *v)
1633 return isl_multi_val_fn_val(mv, &isl_val_mod, v);