re-export isl_multi_aff_range_map as isl_space_range_map_multi_aff
[isl.git] / isl_val.c
blobdc6f71495aa5066ab6ee3bc37cb7ac0a5d513a0c
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 EL_BASE
15 #define EL_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 isl_stat isl_val_get_num_isl_int(__isl_keep isl_val *v, isl_int *n)
300 if (!v)
301 return isl_stat_error;
302 if (!isl_val_is_rat(v))
303 isl_die(isl_val_get_ctx(v), isl_error_invalid,
304 "expecting rational value", return isl_stat_error);
305 isl_int_set(*n, v->n);
306 return isl_stat_ok;
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 /* Extract the denominator of a rational value "v" as an isl_val.
328 * If "v" is not a rational value, then the result is undefined.
330 __isl_give isl_val *isl_val_get_den_val(__isl_keep isl_val *v)
332 if (!v)
333 return NULL;
334 if (!isl_val_is_rat(v))
335 isl_die(isl_val_get_ctx(v), isl_error_invalid,
336 "expecting rational value", return NULL);
337 return isl_val_int_from_isl_int(isl_val_get_ctx(v), v->d);
340 /* Return an approximation of "v" as a double.
342 double isl_val_get_d(__isl_keep isl_val *v)
344 if (!v)
345 return 0;
346 if (!isl_val_is_rat(v))
347 isl_die(isl_val_get_ctx(v), isl_error_invalid,
348 "expecting rational value", return 0);
349 return isl_int_get_d(v->n) / isl_int_get_d(v->d);
352 /* Return the isl_ctx to which "val" belongs.
354 isl_ctx *isl_val_get_ctx(__isl_keep isl_val *val)
356 return val ? val->ctx : NULL;
359 /* Return a hash value that digests "val".
361 uint32_t isl_val_get_hash(__isl_keep isl_val *val)
363 uint32_t hash;
365 if (!val)
366 return 0;
368 hash = isl_hash_init();
369 hash = isl_int_hash(val->n, hash);
370 hash = isl_int_hash(val->d, hash);
372 return hash;
375 /* Normalize "v".
377 * In particular, make sure that the denominator of a rational value
378 * is positive and the numerator and denominator do not have any
379 * common divisors.
381 * This function should not be called by an external user
382 * since it will only be given normalized values.
384 __isl_give isl_val *isl_val_normalize(__isl_take isl_val *v)
386 isl_ctx *ctx;
388 if (!v)
389 return NULL;
390 if (isl_val_is_int(v))
391 return v;
392 if (!isl_val_is_rat(v))
393 return v;
394 if (isl_int_is_neg(v->d)) {
395 isl_int_neg(v->d, v->d);
396 isl_int_neg(v->n, v->n);
398 ctx = isl_val_get_ctx(v);
399 isl_int_gcd(ctx->normalize_gcd, v->n, v->d);
400 if (isl_int_is_one(ctx->normalize_gcd))
401 return v;
402 isl_int_divexact(v->n, v->n, ctx->normalize_gcd);
403 isl_int_divexact(v->d, v->d, ctx->normalize_gcd);
404 return v;
407 /* Return the opposite of "v".
409 __isl_give isl_val *isl_val_neg(__isl_take isl_val *v)
411 if (!v)
412 return NULL;
413 if (isl_val_is_nan(v))
414 return v;
415 if (isl_val_is_zero(v))
416 return v;
418 v = isl_val_cow(v);
419 if (!v)
420 return NULL;
421 isl_int_neg(v->n, v->n);
423 return v;
426 /* Return the inverse of "v".
428 __isl_give isl_val *isl_val_inv(__isl_take isl_val *v)
430 if (!v)
431 return NULL;
432 if (isl_val_is_nan(v))
433 return v;
434 if (isl_val_is_zero(v)) {
435 isl_ctx *ctx = isl_val_get_ctx(v);
436 isl_val_free(v);
437 return isl_val_nan(ctx);
439 if (isl_val_is_infty(v) || isl_val_is_neginfty(v)) {
440 isl_ctx *ctx = isl_val_get_ctx(v);
441 isl_val_free(v);
442 return isl_val_zero(ctx);
445 v = isl_val_cow(v);
446 if (!v)
447 return NULL;
448 isl_int_swap(v->n, v->d);
450 return isl_val_normalize(v);
453 /* Return the absolute value of "v".
455 __isl_give isl_val *isl_val_abs(__isl_take isl_val *v)
457 if (!v)
458 return NULL;
459 if (isl_val_is_nan(v))
460 return v;
461 if (isl_val_is_nonneg(v))
462 return v;
463 return isl_val_neg(v);
466 /* Return the "floor" (greatest integer part) of "v".
467 * That is, return the result of rounding towards -infinity.
469 __isl_give isl_val *isl_val_floor(__isl_take isl_val *v)
471 if (!v)
472 return NULL;
473 if (isl_val_is_int(v))
474 return v;
475 if (!isl_val_is_rat(v))
476 return v;
478 v = isl_val_cow(v);
479 if (!v)
480 return NULL;
481 isl_int_fdiv_q(v->n, v->n, v->d);
482 isl_int_set_si(v->d, 1);
484 return v;
487 /* Return the "ceiling" of "v".
488 * That is, return the result of rounding towards +infinity.
490 __isl_give isl_val *isl_val_ceil(__isl_take isl_val *v)
492 if (!v)
493 return NULL;
494 if (isl_val_is_int(v))
495 return v;
496 if (!isl_val_is_rat(v))
497 return v;
499 v = isl_val_cow(v);
500 if (!v)
501 return NULL;
502 isl_int_cdiv_q(v->n, v->n, v->d);
503 isl_int_set_si(v->d, 1);
505 return v;
508 /* Truncate "v".
509 * That is, return the result of rounding towards zero.
511 __isl_give isl_val *isl_val_trunc(__isl_take isl_val *v)
513 if (!v)
514 return NULL;
515 if (isl_val_is_int(v))
516 return v;
517 if (!isl_val_is_rat(v))
518 return v;
520 v = isl_val_cow(v);
521 if (!v)
522 return NULL;
523 isl_int_tdiv_q(v->n, v->n, v->d);
524 isl_int_set_si(v->d, 1);
526 return v;
529 /* Return 2^v, where v is an integer (that is not too large).
531 __isl_give isl_val *isl_val_pow2(__isl_take isl_val *v)
533 unsigned long exp;
534 int neg;
536 v = isl_val_cow(v);
537 if (!v)
538 return NULL;
539 if (!isl_val_is_int(v))
540 isl_die(isl_val_get_ctx(v), isl_error_invalid,
541 "can only compute integer powers",
542 return isl_val_free(v));
543 neg = isl_val_is_neg(v);
544 if (neg)
545 isl_int_neg(v->n, v->n);
546 if (!isl_int_fits_ulong(v->n))
547 isl_die(isl_val_get_ctx(v), isl_error_invalid,
548 "exponent too large", return isl_val_free(v));
549 exp = isl_int_get_ui(v->n);
550 if (neg) {
551 isl_int_mul_2exp(v->d, v->d, exp);
552 isl_int_set_si(v->n, 1);
553 } else {
554 isl_int_mul_2exp(v->n, v->d, exp);
557 return v;
560 /* This is an alternative name for the function above.
562 __isl_give isl_val *isl_val_2exp(__isl_take isl_val *v)
564 return isl_val_pow2(v);
567 /* Return the minimum of "v1" and "v2".
569 __isl_give isl_val *isl_val_min(__isl_take isl_val *v1, __isl_take isl_val *v2)
571 if (!v1 || !v2)
572 goto error;
574 if (isl_val_is_nan(v1)) {
575 isl_val_free(v2);
576 return v1;
578 if (isl_val_is_nan(v2)) {
579 isl_val_free(v1);
580 return v2;
582 if (isl_val_le(v1, v2)) {
583 isl_val_free(v2);
584 return v1;
585 } else {
586 isl_val_free(v1);
587 return v2;
589 error:
590 isl_val_free(v1);
591 isl_val_free(v2);
592 return NULL;
595 /* Return the maximum of "v1" and "v2".
597 __isl_give isl_val *isl_val_max(__isl_take isl_val *v1, __isl_take isl_val *v2)
599 if (!v1 || !v2)
600 goto error;
602 if (isl_val_is_nan(v1)) {
603 isl_val_free(v2);
604 return v1;
606 if (isl_val_is_nan(v2)) {
607 isl_val_free(v1);
608 return v2;
610 if (isl_val_ge(v1, v2)) {
611 isl_val_free(v2);
612 return v1;
613 } else {
614 isl_val_free(v1);
615 return v2;
617 error:
618 isl_val_free(v1);
619 isl_val_free(v2);
620 return NULL;
623 /* Return the sum of "v1" and "v2".
625 __isl_give isl_val *isl_val_add(__isl_take isl_val *v1, __isl_take isl_val *v2)
627 if (!v1 || !v2)
628 goto error;
629 if (isl_val_is_nan(v1)) {
630 isl_val_free(v2);
631 return v1;
633 if (isl_val_is_nan(v2)) {
634 isl_val_free(v1);
635 return v2;
637 if ((isl_val_is_infty(v1) && isl_val_is_neginfty(v2)) ||
638 (isl_val_is_neginfty(v1) && isl_val_is_infty(v2))) {
639 isl_val_free(v2);
640 return isl_val_set_nan(v1);
642 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
643 isl_val_free(v2);
644 return v1;
646 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
647 isl_val_free(v1);
648 return v2;
650 if (isl_val_is_zero(v1)) {
651 isl_val_free(v1);
652 return v2;
654 if (isl_val_is_zero(v2)) {
655 isl_val_free(v2);
656 return v1;
659 v1 = isl_val_cow(v1);
660 if (!v1)
661 goto error;
662 if (isl_val_is_int(v1) && isl_val_is_int(v2))
663 isl_int_add(v1->n, v1->n, v2->n);
664 else {
665 if (isl_int_eq(v1->d, v2->d))
666 isl_int_add(v1->n, v1->n, v2->n);
667 else {
668 isl_int_mul(v1->n, v1->n, v2->d);
669 isl_int_addmul(v1->n, v2->n, v1->d);
670 isl_int_mul(v1->d, v1->d, v2->d);
672 v1 = isl_val_normalize(v1);
674 isl_val_free(v2);
675 return v1;
676 error:
677 isl_val_free(v1);
678 isl_val_free(v2);
679 return NULL;
682 /* Return the sum of "v1" and "v2".
684 __isl_give isl_val *isl_val_add_ui(__isl_take isl_val *v1, unsigned long v2)
686 if (!v1)
687 return NULL;
688 if (!isl_val_is_rat(v1))
689 return v1;
690 if (v2 == 0)
691 return v1;
692 v1 = isl_val_cow(v1);
693 if (!v1)
694 return NULL;
696 isl_int_addmul_ui(v1->n, v1->d, v2);
698 return v1;
701 /* Subtract "v2" from "v1".
703 __isl_give isl_val *isl_val_sub(__isl_take isl_val *v1, __isl_take isl_val *v2)
705 if (!v1 || !v2)
706 goto error;
707 if (isl_val_is_nan(v1)) {
708 isl_val_free(v2);
709 return v1;
711 if (isl_val_is_nan(v2)) {
712 isl_val_free(v1);
713 return v2;
715 if ((isl_val_is_infty(v1) && isl_val_is_infty(v2)) ||
716 (isl_val_is_neginfty(v1) && isl_val_is_neginfty(v2))) {
717 isl_val_free(v2);
718 return isl_val_set_nan(v1);
720 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
721 isl_val_free(v2);
722 return v1;
724 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
725 isl_val_free(v1);
726 return isl_val_neg(v2);
728 if (isl_val_is_zero(v2)) {
729 isl_val_free(v2);
730 return v1;
732 if (isl_val_is_zero(v1)) {
733 isl_val_free(v1);
734 return isl_val_neg(v2);
737 v1 = isl_val_cow(v1);
738 if (!v1)
739 goto error;
740 if (isl_val_is_int(v1) && isl_val_is_int(v2))
741 isl_int_sub(v1->n, v1->n, v2->n);
742 else {
743 if (isl_int_eq(v1->d, v2->d))
744 isl_int_sub(v1->n, v1->n, v2->n);
745 else {
746 isl_int_mul(v1->n, v1->n, v2->d);
747 isl_int_submul(v1->n, v2->n, v1->d);
748 isl_int_mul(v1->d, v1->d, v2->d);
750 v1 = isl_val_normalize(v1);
752 isl_val_free(v2);
753 return v1;
754 error:
755 isl_val_free(v1);
756 isl_val_free(v2);
757 return NULL;
760 /* Subtract "v2" from "v1".
762 __isl_give isl_val *isl_val_sub_ui(__isl_take isl_val *v1, unsigned long v2)
764 if (!v1)
765 return NULL;
766 if (!isl_val_is_rat(v1))
767 return v1;
768 if (v2 == 0)
769 return v1;
770 v1 = isl_val_cow(v1);
771 if (!v1)
772 return NULL;
774 isl_int_submul_ui(v1->n, v1->d, v2);
776 return v1;
779 /* Return the product of "v1" and "v2".
781 __isl_give isl_val *isl_val_mul(__isl_take isl_val *v1, __isl_take isl_val *v2)
783 if (!v1 || !v2)
784 goto error;
785 if (isl_val_is_nan(v1)) {
786 isl_val_free(v2);
787 return v1;
789 if (isl_val_is_nan(v2)) {
790 isl_val_free(v1);
791 return v2;
793 if ((!isl_val_is_rat(v1) && isl_val_is_zero(v2)) ||
794 (isl_val_is_zero(v1) && !isl_val_is_rat(v2))) {
795 isl_val_free(v2);
796 return isl_val_set_nan(v1);
798 if (isl_val_is_zero(v1)) {
799 isl_val_free(v2);
800 return v1;
802 if (isl_val_is_zero(v2)) {
803 isl_val_free(v1);
804 return v2;
806 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
807 if (isl_val_is_neg(v2))
808 v1 = isl_val_neg(v1);
809 isl_val_free(v2);
810 return v1;
812 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
813 if (isl_val_is_neg(v1))
814 v2 = isl_val_neg(v2);
815 isl_val_free(v1);
816 return v2;
819 v1 = isl_val_cow(v1);
820 if (!v1)
821 goto error;
822 if (isl_val_is_int(v1) && isl_val_is_int(v2))
823 isl_int_mul(v1->n, v1->n, v2->n);
824 else {
825 isl_int_mul(v1->n, v1->n, v2->n);
826 isl_int_mul(v1->d, v1->d, v2->d);
827 v1 = isl_val_normalize(v1);
829 isl_val_free(v2);
830 return v1;
831 error:
832 isl_val_free(v1);
833 isl_val_free(v2);
834 return NULL;
837 /* Return the product of "v1" and "v2".
839 * This is a private copy of isl_val_mul for use in the generic
840 * isl_multi_*_scale_val instantiated for isl_val.
842 __isl_give isl_val *isl_val_scale_val(__isl_take isl_val *v1,
843 __isl_take isl_val *v2)
845 return isl_val_mul(v1, v2);
848 /* Return the product of "v1" and "v2".
850 __isl_give isl_val *isl_val_mul_ui(__isl_take isl_val *v1, unsigned long v2)
852 if (!v1)
853 return NULL;
854 if (isl_val_is_nan(v1))
855 return v1;
856 if (!isl_val_is_rat(v1)) {
857 if (v2 == 0)
858 v1 = isl_val_set_nan(v1);
859 return v1;
861 if (v2 == 1)
862 return v1;
863 v1 = isl_val_cow(v1);
864 if (!v1)
865 return NULL;
867 isl_int_mul_ui(v1->n, v1->n, v2);
869 return isl_val_normalize(v1);
872 /* Divide "v1" by "v2".
874 __isl_give isl_val *isl_val_div(__isl_take isl_val *v1, __isl_take isl_val *v2)
876 if (!v1 || !v2)
877 goto error;
878 if (isl_val_is_nan(v1)) {
879 isl_val_free(v2);
880 return v1;
882 if (isl_val_is_nan(v2)) {
883 isl_val_free(v1);
884 return v2;
886 if (isl_val_is_zero(v2) ||
887 (!isl_val_is_rat(v1) && !isl_val_is_rat(v2))) {
888 isl_val_free(v2);
889 return isl_val_set_nan(v1);
891 if (isl_val_is_zero(v1)) {
892 isl_val_free(v2);
893 return v1;
895 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
896 if (isl_val_is_neg(v2))
897 v1 = isl_val_neg(v1);
898 isl_val_free(v2);
899 return v1;
901 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
902 isl_val_free(v2);
903 return isl_val_set_zero(v1);
906 v1 = isl_val_cow(v1);
907 if (!v1)
908 goto error;
909 if (isl_val_is_int(v2)) {
910 isl_int_mul(v1->d, v1->d, v2->n);
911 v1 = isl_val_normalize(v1);
912 } else {
913 isl_int_mul(v1->d, v1->d, v2->n);
914 isl_int_mul(v1->n, v1->n, v2->d);
915 v1 = isl_val_normalize(v1);
917 isl_val_free(v2);
918 return v1;
919 error:
920 isl_val_free(v1);
921 isl_val_free(v2);
922 return NULL;
925 /* Divide "v1" by "v2".
927 __isl_give isl_val *isl_val_div_ui(__isl_take isl_val *v1, unsigned long v2)
929 if (!v1)
930 return NULL;
931 if (isl_val_is_nan(v1))
932 return v1;
933 if (v2 == 0)
934 return isl_val_set_nan(v1);
935 if (v2 == 1)
936 return v1;
937 if (isl_val_is_zero(v1))
938 return v1;
939 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1))
940 return v1;
941 v1 = isl_val_cow(v1);
942 if (!v1)
943 return NULL;
945 isl_int_mul_ui(v1->d, v1->d, v2);
947 return isl_val_normalize(v1);
950 /* Divide "v1" by "v2".
952 * This is a private copy of isl_val_div for use in the generic
953 * isl_multi_*_scale_down_val instantiated for isl_val.
955 __isl_give isl_val *isl_val_scale_down_val(__isl_take isl_val *v1,
956 __isl_take isl_val *v2)
958 return isl_val_div(v1, v2);
961 /* Given two integer values "v1" and "v2", check if "v1" is divisible by "v2".
963 isl_bool isl_val_is_divisible_by(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
965 if (!v1 || !v2)
966 return isl_bool_error;
968 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
969 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
970 "expecting two integers", return isl_bool_error);
972 return isl_bool_ok(isl_int_is_divisible_by(v1->n, v2->n));
975 /* Given two integer values "v1" and "v2", return the residue of "v1"
976 * modulo "v2".
978 __isl_give isl_val *isl_val_mod(__isl_take isl_val *v1, __isl_take isl_val *v2)
980 if (!v1 || !v2)
981 goto error;
982 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
983 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
984 "expecting two integers", goto error);
985 if (isl_val_is_nonneg(v1) && isl_val_lt(v1, v2)) {
986 isl_val_free(v2);
987 return v1;
989 v1 = isl_val_cow(v1);
990 if (!v1)
991 goto error;
992 isl_int_fdiv_r(v1->n, v1->n, v2->n);
993 isl_val_free(v2);
994 return v1;
995 error:
996 isl_val_free(v1);
997 isl_val_free(v2);
998 return NULL;
1001 /* Given two integer values "v1" and "v2", return the residue of "v1"
1002 * modulo "v2".
1004 * This is a private copy of isl_val_mod for use in the generic
1005 * isl_multi_*_mod_multi_val instantiated for isl_val.
1007 __isl_give isl_val *isl_val_mod_val(__isl_take isl_val *v1,
1008 __isl_take isl_val *v2)
1010 return isl_val_mod(v1, v2);
1013 /* Given two integer values, return their greatest common divisor.
1015 __isl_give isl_val *isl_val_gcd(__isl_take isl_val *v1, __isl_take isl_val *v2)
1017 if (!v1 || !v2)
1018 goto error;
1019 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
1020 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
1021 "expecting two integers", goto error);
1022 if (isl_val_eq(v1, v2)) {
1023 isl_val_free(v2);
1024 return v1;
1026 if (isl_val_is_one(v1)) {
1027 isl_val_free(v2);
1028 return v1;
1030 if (isl_val_is_one(v2)) {
1031 isl_val_free(v1);
1032 return v2;
1034 v1 = isl_val_cow(v1);
1035 if (!v1)
1036 goto error;
1037 isl_int_gcd(v1->n, v1->n, v2->n);
1038 isl_val_free(v2);
1039 return v1;
1040 error:
1041 isl_val_free(v1);
1042 isl_val_free(v2);
1043 return NULL;
1046 /* Compute x, y and g such that g = gcd(a,b) and a*x+b*y = g.
1048 static void isl_int_gcdext(isl_int *g, isl_int *x, isl_int *y,
1049 isl_int a, isl_int b)
1051 isl_int d, tmp;
1052 isl_int a_copy, b_copy;
1054 isl_int_init(a_copy);
1055 isl_int_init(b_copy);
1056 isl_int_init(d);
1057 isl_int_init(tmp);
1058 isl_int_set(a_copy, a);
1059 isl_int_set(b_copy, b);
1060 isl_int_abs(*g, a_copy);
1061 isl_int_abs(d, b_copy);
1062 isl_int_set_si(*x, 1);
1063 isl_int_set_si(*y, 0);
1064 while (isl_int_is_pos(d)) {
1065 isl_int_fdiv_q(tmp, *g, d);
1066 isl_int_submul(*x, tmp, *y);
1067 isl_int_submul(*g, tmp, d);
1068 isl_int_swap(*g, d);
1069 isl_int_swap(*x, *y);
1071 if (isl_int_is_zero(a_copy))
1072 isl_int_set_si(*x, 0);
1073 else if (isl_int_is_neg(a_copy))
1074 isl_int_neg(*x, *x);
1075 if (isl_int_is_zero(b_copy))
1076 isl_int_set_si(*y, 0);
1077 else {
1078 isl_int_mul(tmp, a_copy, *x);
1079 isl_int_sub(tmp, *g, tmp);
1080 isl_int_divexact(*y, tmp, b_copy);
1082 isl_int_clear(d);
1083 isl_int_clear(tmp);
1084 isl_int_clear(a_copy);
1085 isl_int_clear(b_copy);
1088 /* Given two integer values v1 and v2, return their greatest common divisor g,
1089 * as well as two integers x and y such that x * v1 + y * v2 = g.
1091 __isl_give isl_val *isl_val_gcdext(__isl_take isl_val *v1,
1092 __isl_take isl_val *v2, __isl_give isl_val **x, __isl_give isl_val **y)
1094 isl_ctx *ctx;
1095 isl_val *a = NULL, *b = NULL;
1097 if (!x && !y)
1098 return isl_val_gcd(v1, v2);
1100 if (!v1 || !v2)
1101 goto error;
1103 ctx = isl_val_get_ctx(v1);
1104 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
1105 isl_die(ctx, isl_error_invalid,
1106 "expecting two integers", goto error);
1108 v1 = isl_val_cow(v1);
1109 a = isl_val_alloc(ctx);
1110 b = isl_val_alloc(ctx);
1111 if (!v1 || !a || !b)
1112 goto error;
1113 isl_int_gcdext(&v1->n, &a->n, &b->n, v1->n, v2->n);
1114 if (x) {
1115 isl_int_set_si(a->d, 1);
1116 *x = a;
1117 } else
1118 isl_val_free(a);
1119 if (y) {
1120 isl_int_set_si(b->d, 1);
1121 *y = b;
1122 } else
1123 isl_val_free(b);
1124 isl_val_free(v2);
1125 return v1;
1126 error:
1127 isl_val_free(v1);
1128 isl_val_free(v2);
1129 isl_val_free(a);
1130 isl_val_free(b);
1131 if (x)
1132 *x = NULL;
1133 if (y)
1134 *y = NULL;
1135 return NULL;
1138 /* Does "v" represent an integer value?
1140 isl_bool isl_val_is_int(__isl_keep isl_val *v)
1142 if (!v)
1143 return isl_bool_error;
1145 return isl_bool_ok(isl_int_is_one(v->d));
1148 /* Does "v" represent a rational value?
1150 isl_bool isl_val_is_rat(__isl_keep isl_val *v)
1152 if (!v)
1153 return isl_bool_error;
1155 return isl_bool_ok(!isl_int_is_zero(v->d));
1158 /* Does "v" represent NaN?
1160 isl_bool isl_val_is_nan(__isl_keep isl_val *v)
1162 if (!v)
1163 return isl_bool_error;
1165 return isl_bool_ok(isl_int_is_zero(v->n) && isl_int_is_zero(v->d));
1168 /* Does "v" represent +infinity?
1170 isl_bool isl_val_is_infty(__isl_keep isl_val *v)
1172 if (!v)
1173 return isl_bool_error;
1175 return isl_bool_ok(isl_int_is_pos(v->n) && isl_int_is_zero(v->d));
1178 /* Does "v" represent -infinity?
1180 isl_bool isl_val_is_neginfty(__isl_keep isl_val *v)
1182 if (!v)
1183 return isl_bool_error;
1185 return isl_bool_ok(isl_int_is_neg(v->n) && isl_int_is_zero(v->d));
1188 /* Does "v" represent the integer zero?
1190 isl_bool isl_val_is_zero(__isl_keep isl_val *v)
1192 if (!v)
1193 return isl_bool_error;
1195 return isl_bool_ok(isl_int_is_zero(v->n) && !isl_int_is_zero(v->d));
1198 /* Does "v" represent the integer one?
1200 isl_bool isl_val_is_one(__isl_keep isl_val *v)
1202 if (!v)
1203 return isl_bool_error;
1205 if (isl_val_is_nan(v))
1206 return isl_bool_false;
1208 return isl_bool_ok(isl_int_eq(v->n, v->d));
1211 /* Does "v" represent the integer negative one?
1213 isl_bool isl_val_is_negone(__isl_keep isl_val *v)
1215 if (!v)
1216 return isl_bool_error;
1218 return isl_bool_ok(isl_int_is_neg(v->n) && isl_int_abs_eq(v->n, v->d));
1221 /* Is "v" (strictly) positive?
1223 isl_bool isl_val_is_pos(__isl_keep isl_val *v)
1225 if (!v)
1226 return isl_bool_error;
1228 return isl_bool_ok(isl_int_is_pos(v->n));
1231 /* Is "v" (strictly) negative?
1233 isl_bool isl_val_is_neg(__isl_keep isl_val *v)
1235 if (!v)
1236 return isl_bool_error;
1238 return isl_bool_ok(isl_int_is_neg(v->n));
1241 /* Is "v" non-negative?
1243 isl_bool isl_val_is_nonneg(__isl_keep isl_val *v)
1245 if (!v)
1246 return isl_bool_error;
1248 if (isl_val_is_nan(v))
1249 return isl_bool_false;
1251 return isl_bool_ok(isl_int_is_nonneg(v->n));
1254 /* Is "v" non-positive?
1256 isl_bool isl_val_is_nonpos(__isl_keep isl_val *v)
1258 if (!v)
1259 return isl_bool_error;
1261 if (isl_val_is_nan(v))
1262 return isl_bool_false;
1264 return isl_bool_ok(isl_int_is_nonpos(v->n));
1267 /* Return the sign of "v".
1269 * The sign of NaN is undefined.
1271 int isl_val_sgn(__isl_keep isl_val *v)
1273 if (!v)
1274 return 0;
1275 if (isl_val_is_zero(v))
1276 return 0;
1277 if (isl_val_is_pos(v))
1278 return 1;
1279 return -1;
1282 /* Is "v1" (strictly) less than "v2"?
1284 isl_bool isl_val_lt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1286 isl_int t;
1287 isl_bool lt;
1289 if (!v1 || !v2)
1290 return isl_bool_error;
1291 if (isl_val_is_int(v1) && isl_val_is_int(v2))
1292 return isl_bool_ok(isl_int_lt(v1->n, v2->n));
1293 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1294 return isl_bool_false;
1295 if (isl_val_eq(v1, v2))
1296 return isl_bool_false;
1297 if (isl_val_is_infty(v2))
1298 return isl_bool_true;
1299 if (isl_val_is_infty(v1))
1300 return isl_bool_false;
1301 if (isl_val_is_neginfty(v1))
1302 return isl_bool_true;
1303 if (isl_val_is_neginfty(v2))
1304 return isl_bool_false;
1306 isl_int_init(t);
1307 isl_int_mul(t, v1->n, v2->d);
1308 isl_int_submul(t, v2->n, v1->d);
1309 lt = isl_bool_ok(isl_int_is_neg(t));
1310 isl_int_clear(t);
1312 return lt;
1315 /* Is "v1" (strictly) greater than "v2"?
1317 isl_bool isl_val_gt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1319 return isl_val_lt(v2, v1);
1322 /* Is "v" (strictly) greater than "i"?
1324 isl_bool isl_val_gt_si(__isl_keep isl_val *v, long i)
1326 isl_val *vi;
1327 isl_bool res;
1329 if (!v)
1330 return isl_bool_error;
1331 if (isl_val_is_int(v))
1332 return isl_bool_ok(isl_int_cmp_si(v->n, i) > 0);
1333 if (isl_val_is_nan(v))
1334 return isl_bool_false;
1335 if (isl_val_is_infty(v))
1336 return isl_bool_true;
1337 if (isl_val_is_neginfty(v))
1338 return isl_bool_false;
1340 vi = isl_val_int_from_si(isl_val_get_ctx(v), i);
1341 res = isl_bool_ok(isl_val_gt(v, vi));
1342 isl_val_free(vi);
1344 return res;
1347 /* Is "v1" less than or equal to "v2"?
1349 isl_bool isl_val_le(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1351 isl_int t;
1352 isl_bool le;
1354 if (!v1 || !v2)
1355 return isl_bool_error;
1356 if (isl_val_is_int(v1) && isl_val_is_int(v2))
1357 return isl_bool_ok(isl_int_le(v1->n, v2->n));
1358 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1359 return isl_bool_false;
1360 if (isl_val_eq(v1, v2))
1361 return isl_bool_true;
1362 if (isl_val_is_infty(v2))
1363 return isl_bool_true;
1364 if (isl_val_is_infty(v1))
1365 return isl_bool_false;
1366 if (isl_val_is_neginfty(v1))
1367 return isl_bool_true;
1368 if (isl_val_is_neginfty(v2))
1369 return isl_bool_false;
1371 isl_int_init(t);
1372 isl_int_mul(t, v1->n, v2->d);
1373 isl_int_submul(t, v2->n, v1->d);
1374 le = isl_bool_ok(isl_int_is_nonpos(t));
1375 isl_int_clear(t);
1377 return le;
1380 /* Is "v1" greater than or equal to "v2"?
1382 isl_bool isl_val_ge(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1384 return isl_val_le(v2, v1);
1387 /* How does "v" compare to "i"?
1389 * Return 1 if v is greater, -1 if v is smaller and 0 if v is equal to i.
1391 * If v is NaN (or NULL), then the result is undefined.
1393 int isl_val_cmp_si(__isl_keep isl_val *v, long i)
1395 isl_int t;
1396 int cmp;
1398 if (!v)
1399 return 0;
1400 if (isl_val_is_int(v))
1401 return isl_int_cmp_si(v->n, i);
1402 if (isl_val_is_nan(v))
1403 return 0;
1404 if (isl_val_is_infty(v))
1405 return 1;
1406 if (isl_val_is_neginfty(v))
1407 return -1;
1409 isl_int_init(t);
1410 isl_int_mul_si(t, v->d, i);
1411 isl_int_sub(t, v->n, t);
1412 cmp = isl_int_sgn(t);
1413 isl_int_clear(t);
1415 return cmp;
1418 /* Is "v1" equal to "v2"?
1420 isl_bool isl_val_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1422 if (!v1 || !v2)
1423 return isl_bool_error;
1424 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1425 return isl_bool_false;
1427 return isl_bool_ok(isl_int_eq(v1->n, v2->n) &&
1428 isl_int_eq(v1->d, v2->d));
1431 /* Is "v" equal to "i"?
1433 isl_bool isl_val_eq_si(__isl_keep isl_val *v, long i)
1435 if (!v)
1436 return isl_bool_error;
1437 if (!isl_val_is_int(v))
1438 return isl_bool_false;
1439 return isl_bool_ok(isl_int_cmp_si(v->n, i) == 0);
1442 /* Is "v1" equal to "v2" in absolute value?
1444 isl_bool isl_val_abs_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1446 if (!v1 || !v2)
1447 return isl_bool_error;
1448 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1449 return isl_bool_false;
1451 return isl_bool_ok(isl_int_abs_eq(v1->n, v2->n) &&
1452 isl_int_eq(v1->d, v2->d));
1455 /* Is "v1" different from "v2"?
1457 isl_bool isl_val_ne(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1459 if (!v1 || !v2)
1460 return isl_bool_error;
1461 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1462 return isl_bool_false;
1464 return isl_bool_ok(isl_int_ne(v1->n, v2->n) ||
1465 isl_int_ne(v1->d, v2->d));
1468 /* Print a textual representation of "v" onto "p".
1470 __isl_give isl_printer *isl_printer_print_val(__isl_take isl_printer *p,
1471 __isl_keep isl_val *v)
1473 int neg;
1475 if (!p || !v)
1476 return isl_printer_free(p);
1478 neg = isl_int_is_neg(v->n);
1479 if (neg) {
1480 p = isl_printer_print_str(p, "-");
1481 isl_int_neg(v->n, v->n);
1483 if (isl_int_is_zero(v->d)) {
1484 int sgn = isl_int_sgn(v->n);
1485 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1486 sgn == 0 ? "NaN" : "infty");
1487 } else
1488 p = isl_printer_print_isl_int(p, v->n);
1489 if (neg)
1490 isl_int_neg(v->n, v->n);
1491 if (!isl_int_is_zero(v->d) && !isl_int_is_one(v->d)) {
1492 p = isl_printer_print_str(p, "/");
1493 p = isl_printer_print_isl_int(p, v->d);
1496 return p;
1499 /* Is "val1" (obviously) equal to "val2"?
1501 * This is a private copy of isl_val_eq for use in the generic
1502 * isl_multi_*_plain_is_equal instantiated for isl_val.
1504 isl_bool isl_val_plain_is_equal(__isl_keep isl_val *val1,
1505 __isl_keep isl_val *val2)
1507 return isl_val_eq(val1, val2);
1510 /* Does "v" have any non-zero coefficients
1511 * for any dimension in the given range?
1513 * This function is only meant to be used in the generic isl_multi_*
1514 * functions which have to deal with base objects that have an associated
1515 * space. Since an isl_val does not have any coefficients, this function
1516 * always returns isl_bool_false.
1518 isl_bool isl_val_involves_dims(__isl_keep isl_val *v, enum isl_dim_type type,
1519 unsigned first, unsigned n)
1521 if (!v)
1522 return isl_bool_error;
1524 return isl_bool_false;
1527 /* Insert "n" dimensions of type "type" at position "first".
1529 * This function is only meant to be used in the generic isl_multi_*
1530 * functions which have to deal with base objects that have an associated
1531 * space. Since an isl_val does not have an associated space, this function
1532 * does not do anything.
1534 __isl_give isl_val *isl_val_insert_dims(__isl_take isl_val *v,
1535 enum isl_dim_type type, unsigned first, unsigned n)
1537 return v;
1540 /* Change the name of the dimension of type "type" at position "pos" to "s".
1542 * This function is only meant to be used in the generic isl_multi_*
1543 * functions which have to deal with base objects that have an associated
1544 * space. Since an isl_val does not have an associated space, this function
1545 * does not do anything.
1547 __isl_give isl_val *isl_val_set_dim_name(__isl_take isl_val *v,
1548 enum isl_dim_type type, unsigned pos, const char *s)
1550 return v;
1553 /* Return an isl_val that is zero on "ls".
1555 * This function is only meant to be used in the generic isl_multi_*
1556 * functions which have to deal with base objects that have an associated
1557 * space. Since an isl_val does not have an associated space, this function
1558 * simply returns a zero isl_val in the same context as "ls".
1560 __isl_give isl_val *isl_val_zero_on_domain(__isl_take isl_local_space *ls)
1562 isl_ctx *ctx;
1564 if (!ls)
1565 return NULL;
1566 ctx = isl_local_space_get_ctx(ls);
1567 isl_local_space_free(ls);
1568 return isl_val_zero(ctx);
1571 #define isl_val_involves_nan isl_val_is_nan
1573 #undef BASE
1574 #define BASE val
1576 #include <isl_multi_no_domain_templ.c>
1577 #include <isl_multi_no_explicit_domain.c>
1578 #include <isl_multi_templ.c>
1579 #include <isl_multi_arith_templ.c>
1580 #include <isl_multi_dim_id_templ.c>
1581 #include <isl_multi_dims.c>
1582 #include <isl_multi_min_max_templ.c>
1583 #include <isl_multi_nan_templ.c>
1584 #include <isl_multi_product_templ.c>
1585 #include <isl_multi_splice_templ.c>
1586 #include <isl_multi_tuple_id_templ.c>
1587 #include <isl_multi_zero_templ.c>
1589 /* Does "mv" consist of only zeros?
1591 isl_bool isl_multi_val_is_zero(__isl_keep isl_multi_val *mv)
1593 return isl_multi_val_every(mv, &isl_val_is_zero);
1596 /* Apply "fn" to each of the elements of "mv" with as second argument "v".
1598 static __isl_give isl_multi_val *isl_multi_val_fn_val(
1599 __isl_take isl_multi_val *mv,
1600 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
1601 __isl_take isl_val *v2),
1602 __isl_take isl_val *v)
1604 int i;
1606 mv = isl_multi_val_cow(mv);
1607 if (!mv || !v)
1608 goto error;
1610 for (i = 0; i < mv->n; ++i) {
1611 mv->u.p[i] = fn(mv->u.p[i], isl_val_copy(v));
1612 if (!mv->u.p[i])
1613 goto error;
1616 isl_val_free(v);
1617 return mv;
1618 error:
1619 isl_val_free(v);
1620 isl_multi_val_free(mv);
1621 return NULL;
1624 /* Add "v" to each of the elements of "mv".
1626 __isl_give isl_multi_val *isl_multi_val_add_val(__isl_take isl_multi_val *mv,
1627 __isl_take isl_val *v)
1629 if (!v)
1630 return isl_multi_val_free(mv);
1631 if (isl_val_is_zero(v)) {
1632 isl_val_free(v);
1633 return mv;
1635 return isl_multi_val_fn_val(mv, &isl_val_add, v);
1638 /* Reduce the elements of "mv" modulo "v".
1640 __isl_give isl_multi_val *isl_multi_val_mod_val(__isl_take isl_multi_val *mv,
1641 __isl_take isl_val *v)
1643 return isl_multi_val_fn_val(mv, &isl_val_mod, v);