add isl_set_lower_bound_val and isl_set_upper_bound_val
[isl.git] / isl_val.c
blobc61b4adbe5eaecaa60919f95a652893a5d9c8b12
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 NaN.
54 __isl_give isl_val *isl_val_nan(isl_ctx *ctx)
56 isl_val *v;
58 v = isl_val_alloc(ctx);
59 if (!v)
60 return NULL;
62 isl_int_set_si(v->n, 0);
63 isl_int_set_si(v->d, 0);
65 return v;
68 /* Change "v" into a NaN.
70 __isl_give isl_val *isl_val_set_nan(__isl_take isl_val *v)
72 if (!v)
73 return NULL;
74 if (isl_val_is_nan(v))
75 return v;
76 v = isl_val_cow(v);
77 if (!v)
78 return NULL;
80 isl_int_set_si(v->n, 0);
81 isl_int_set_si(v->d, 0);
83 return v;
86 /* Return a reference to an isl_val representing +infinity.
88 __isl_give isl_val *isl_val_infty(isl_ctx *ctx)
90 isl_val *v;
92 v = isl_val_alloc(ctx);
93 if (!v)
94 return NULL;
96 isl_int_set_si(v->n, 1);
97 isl_int_set_si(v->d, 0);
99 return v;
102 /* Return a reference to an isl_val representing -infinity.
104 __isl_give isl_val *isl_val_neginfty(isl_ctx *ctx)
106 isl_val *v;
108 v = isl_val_alloc(ctx);
109 if (!v)
110 return NULL;
112 isl_int_set_si(v->n, -1);
113 isl_int_set_si(v->d, 0);
115 return v;
118 /* Return a reference to an isl_val representing the integer "i".
120 __isl_give isl_val *isl_val_int_from_si(isl_ctx *ctx, long i)
122 isl_val *v;
124 v = isl_val_alloc(ctx);
125 if (!v)
126 return NULL;
128 isl_int_set_si(v->n, i);
129 isl_int_set_si(v->d, 1);
131 return v;
134 /* Change the value of "v" to be equal to the integer "i".
136 __isl_give isl_val *isl_val_set_si(__isl_take isl_val *v, long i)
138 if (!v)
139 return NULL;
140 if (isl_val_is_int(v) && isl_int_cmp_si(v->n, i) == 0)
141 return v;
142 v = isl_val_cow(v);
143 if (!v)
144 return NULL;
146 isl_int_set_si(v->n, i);
147 isl_int_set_si(v->d, 1);
149 return v;
152 /* Change the value of "v" to be equal to zero.
154 __isl_give isl_val *isl_val_set_zero(__isl_take isl_val *v)
156 return isl_val_set_si(v, 0);
159 /* Return a reference to an isl_val representing the unsigned integer "u".
161 __isl_give isl_val *isl_val_int_from_ui(isl_ctx *ctx, unsigned long u)
163 isl_val *v;
165 v = isl_val_alloc(ctx);
166 if (!v)
167 return NULL;
169 isl_int_set_ui(v->n, u);
170 isl_int_set_si(v->d, 1);
172 return v;
175 /* Return a reference to an isl_val representing the integer "n".
177 __isl_give isl_val *isl_val_int_from_isl_int(isl_ctx *ctx, isl_int n)
179 isl_val *v;
181 v = isl_val_alloc(ctx);
182 if (!v)
183 return NULL;
185 isl_int_set(v->n, n);
186 isl_int_set_si(v->d, 1);
188 return v;
191 /* Return a reference to an isl_val representing the rational value "n"/"d".
192 * Normalizing the isl_val (if needed) is left to the caller.
194 __isl_give isl_val *isl_val_rat_from_isl_int(isl_ctx *ctx,
195 isl_int n, isl_int d)
197 isl_val *v;
199 v = isl_val_alloc(ctx);
200 if (!v)
201 return NULL;
203 isl_int_set(v->n, n);
204 isl_int_set(v->d, d);
206 return v;
209 /* Return a new reference to "v".
211 __isl_give isl_val *isl_val_copy(__isl_keep isl_val *v)
213 if (!v)
214 return NULL;
216 v->ref++;
217 return v;
220 /* Return a fresh copy of "val".
222 __isl_give isl_val *isl_val_dup(__isl_keep isl_val *val)
224 isl_val *dup;
226 if (!val)
227 return NULL;
229 dup = isl_val_alloc(isl_val_get_ctx(val));
230 if (!dup)
231 return NULL;
233 isl_int_set(dup->n, val->n);
234 isl_int_set(dup->d, val->d);
236 return dup;
239 /* Return an isl_val that is equal to "val" and that has only
240 * a single reference.
242 __isl_give isl_val *isl_val_cow(__isl_take isl_val *val)
244 if (!val)
245 return NULL;
247 if (val->ref == 1)
248 return val;
249 val->ref--;
250 return isl_val_dup(val);
253 /* Free "v" and return NULL.
255 void *isl_val_free(__isl_take isl_val *v)
257 if (!v)
258 return NULL;
260 if (--v->ref > 0)
261 return NULL;
263 isl_ctx_deref(v->ctx);
264 isl_int_clear(v->n);
265 isl_int_clear(v->d);
266 free(v);
267 return NULL;
270 /* Extract the numerator of a rational value "v" as an integer.
272 * If "v" is not a rational value, then the result is undefined.
274 long isl_val_get_num_si(__isl_keep isl_val *v)
276 if (!v)
277 return 0;
278 if (!isl_val_is_rat(v))
279 isl_die(isl_val_get_ctx(v), isl_error_invalid,
280 "expecting rational value", return 0);
281 if (!isl_int_fits_slong(v->n))
282 isl_die(isl_val_get_ctx(v), isl_error_invalid,
283 "numerator too large", return 0);
284 return isl_int_get_si(v->n);
287 /* Extract the denominator of a rational value "v" as an integer.
289 * If "v" is not a rational value, then the result is undefined.
291 long isl_val_get_den_si(__isl_keep isl_val *v)
293 if (!v)
294 return 0;
295 if (!isl_val_is_rat(v))
296 isl_die(isl_val_get_ctx(v), isl_error_invalid,
297 "expecting rational value", return 0);
298 if (!isl_int_fits_slong(v->d))
299 isl_die(isl_val_get_ctx(v), isl_error_invalid,
300 "denominator too large", return 0);
301 return isl_int_get_si(v->d);
304 /* Return an approximation of "v" as a double.
306 double isl_val_get_d(__isl_keep isl_val *v)
308 if (!v)
309 return 0;
310 if (!isl_val_is_rat(v))
311 isl_die(isl_val_get_ctx(v), isl_error_invalid,
312 "expecting rational value", return 0);
313 return isl_int_get_d(v->n) / isl_int_get_d(v->d);
316 /* Return the isl_ctx to which "val" belongs.
318 isl_ctx *isl_val_get_ctx(__isl_keep isl_val *val)
320 return val ? val->ctx : NULL;
323 /* Normalize "v".
325 * In particular, make sure that the denominator of a rational value
326 * is positive and the numerator and denominator do not have any
327 * common divisors.
329 * This function should not be called by an external user
330 * since it will only be given normalized values.
332 __isl_give isl_val *isl_val_normalize(__isl_take isl_val *v)
334 isl_ctx *ctx;
336 if (!v)
337 return NULL;
338 if (isl_val_is_int(v))
339 return v;
340 if (!isl_val_is_rat(v))
341 return v;
342 if (isl_int_is_neg(v->d)) {
343 isl_int_neg(v->d, v->d);
344 isl_int_neg(v->n, v->n);
346 ctx = isl_val_get_ctx(v);
347 isl_int_gcd(ctx->normalize_gcd, v->n, v->d);
348 if (isl_int_is_one(ctx->normalize_gcd))
349 return v;
350 isl_int_divexact(v->n, v->n, ctx->normalize_gcd);
351 isl_int_divexact(v->d, v->d, ctx->normalize_gcd);
352 return v;
355 /* Return the opposite of "v".
357 __isl_give isl_val *isl_val_neg(__isl_take isl_val *v)
359 if (!v)
360 return NULL;
361 if (isl_val_is_nan(v))
362 return v;
363 if (isl_val_is_zero(v))
364 return v;
366 v = isl_val_cow(v);
367 if (!v)
368 return NULL;
369 isl_int_neg(v->n, v->n);
371 return v;
374 /* Return the absolute value of "v".
376 __isl_give isl_val *isl_val_abs(__isl_take isl_val *v)
378 if (!v)
379 return NULL;
380 if (isl_val_is_nan(v))
381 return v;
382 if (isl_val_is_nonneg(v))
383 return v;
384 return isl_val_neg(v);
387 /* Return the "floor" (greatest integer part) of "v".
388 * That is, return the result of rounding towards -infinity.
390 __isl_give isl_val *isl_val_floor(__isl_take isl_val *v)
392 if (!v)
393 return NULL;
394 if (isl_val_is_int(v))
395 return v;
396 if (!isl_val_is_rat(v))
397 return v;
399 v = isl_val_cow(v);
400 if (!v)
401 return NULL;
402 isl_int_fdiv_q(v->n, v->n, v->d);
403 isl_int_set_si(v->d, 1);
405 return v;
408 /* Return the "ceiling" of "v".
409 * That is, return the result of rounding towards +infinity.
411 __isl_give isl_val *isl_val_ceil(__isl_take isl_val *v)
413 if (!v)
414 return NULL;
415 if (isl_val_is_int(v))
416 return v;
417 if (!isl_val_is_rat(v))
418 return v;
420 v = isl_val_cow(v);
421 if (!v)
422 return NULL;
423 isl_int_cdiv_q(v->n, v->n, v->d);
424 isl_int_set_si(v->d, 1);
426 return v;
429 /* Truncate "v".
430 * That is, return the result of rounding towards zero.
432 __isl_give isl_val *isl_val_trunc(__isl_take isl_val *v)
434 if (!v)
435 return NULL;
436 if (isl_val_is_int(v))
437 return v;
438 if (!isl_val_is_rat(v))
439 return v;
441 v = isl_val_cow(v);
442 if (!v)
443 return NULL;
444 isl_int_tdiv_q(v->n, v->n, v->d);
445 isl_int_set_si(v->d, 1);
447 return v;
450 /* Return 2^v, where v is an integer (that is not too large).
452 __isl_give isl_val *isl_val_2exp(__isl_take isl_val *v)
454 unsigned long exp;
455 int neg;
457 v = isl_val_cow(v);
458 if (!v)
459 return NULL;
460 if (!isl_val_is_int(v))
461 isl_die(isl_val_get_ctx(v), isl_error_invalid,
462 "can only compute integer powers",
463 return isl_val_free(v));
464 neg = isl_val_is_neg(v);
465 if (neg)
466 isl_int_neg(v->n, v->n);
467 if (!isl_int_fits_ulong(v->n))
468 isl_die(isl_val_get_ctx(v), isl_error_invalid,
469 "exponent too large", return isl_val_free(v));
470 exp = isl_int_get_ui(v->n);
471 if (neg) {
472 isl_int_mul_2exp(v->d, v->d, exp);
473 isl_int_set_si(v->n, 1);
474 } else {
475 isl_int_mul_2exp(v->n, v->d, exp);
478 return v;
481 /* Return the minimum of "v1" and "v2".
483 __isl_give isl_val *isl_val_min(__isl_take isl_val *v1, __isl_take isl_val *v2)
485 if (!v1 || !v2)
486 goto error;
488 if (isl_val_is_nan(v1)) {
489 isl_val_free(v2);
490 return v1;
492 if (isl_val_is_nan(v2)) {
493 isl_val_free(v1);
494 return v2;
496 if (isl_val_le(v1, v2)) {
497 isl_val_free(v2);
498 return v1;
499 } else {
500 isl_val_free(v1);
501 return v2;
503 error:
504 isl_val_free(v1);
505 isl_val_free(v2);
506 return NULL;
509 /* Return the maximum of "v1" and "v2".
511 __isl_give isl_val *isl_val_max(__isl_take isl_val *v1, __isl_take isl_val *v2)
513 if (!v1 || !v2)
514 goto error;
516 if (isl_val_is_nan(v1)) {
517 isl_val_free(v2);
518 return v1;
520 if (isl_val_is_nan(v2)) {
521 isl_val_free(v1);
522 return v2;
524 if (isl_val_ge(v1, v2)) {
525 isl_val_free(v2);
526 return v1;
527 } else {
528 isl_val_free(v1);
529 return v2;
531 error:
532 isl_val_free(v1);
533 isl_val_free(v2);
534 return NULL;
537 /* Return the sum of "v1" and "v2".
539 __isl_give isl_val *isl_val_add(__isl_take isl_val *v1, __isl_take isl_val *v2)
541 if (!v1 || !v2)
542 goto error;
543 if (isl_val_is_nan(v1)) {
544 isl_val_free(v2);
545 return v1;
547 if (isl_val_is_nan(v2)) {
548 isl_val_free(v1);
549 return v2;
551 if ((isl_val_is_infty(v1) && isl_val_is_neginfty(v2)) ||
552 (isl_val_is_neginfty(v1) && isl_val_is_infty(v2))) {
553 isl_val_free(v2);
554 return isl_val_set_nan(v1);
556 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
557 isl_val_free(v2);
558 return v1;
560 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
561 isl_val_free(v1);
562 return v2;
564 if (isl_val_is_zero(v1)) {
565 isl_val_free(v1);
566 return v2;
568 if (isl_val_is_zero(v2)) {
569 isl_val_free(v2);
570 return v1;
573 v1 = isl_val_cow(v1);
574 if (!v1)
575 goto error;
576 if (isl_val_is_int(v1) && isl_val_is_int(v2))
577 isl_int_add(v1->n, v1->n, v2->n);
578 else {
579 if (isl_int_eq(v1->d, v2->d))
580 isl_int_add(v1->n, v1->n, v2->n);
581 else {
582 isl_int_mul(v1->n, v1->n, v2->d);
583 isl_int_addmul(v1->n, v2->n, v1->d);
584 isl_int_mul(v1->d, v1->d, v2->d);
586 v1 = isl_val_normalize(v1);
588 isl_val_free(v2);
589 return v1;
590 error:
591 isl_val_free(v1);
592 isl_val_free(v2);
593 return NULL;
596 /* Return the sum of "v1" and "v2".
598 __isl_give isl_val *isl_val_add_ui(__isl_take isl_val *v1, unsigned long v2)
600 if (!v1)
601 return NULL;
602 if (!isl_val_is_rat(v1))
603 return v1;
604 if (v2 == 0)
605 return v1;
606 v1 = isl_val_cow(v1);
607 if (!v1)
608 return NULL;
610 isl_int_addmul_ui(v1->n, v1->d, v2);
612 return v1;
615 /* Subtract "v2" from "v1".
617 __isl_give isl_val *isl_val_sub(__isl_take isl_val *v1, __isl_take isl_val *v2)
619 if (!v1 || !v2)
620 goto error;
621 if (isl_val_is_nan(v1)) {
622 isl_val_free(v2);
623 return v1;
625 if (isl_val_is_nan(v2)) {
626 isl_val_free(v1);
627 return v2;
629 if ((isl_val_is_infty(v1) && isl_val_is_infty(v2)) ||
630 (isl_val_is_neginfty(v1) && isl_val_is_neginfty(v2))) {
631 isl_val_free(v2);
632 return isl_val_set_nan(v1);
634 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
635 isl_val_free(v2);
636 return v1;
638 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
639 isl_val_free(v1);
640 return isl_val_neg(v2);
642 if (isl_val_is_zero(v2)) {
643 isl_val_free(v2);
644 return v1;
646 if (isl_val_is_zero(v1)) {
647 isl_val_free(v1);
648 return isl_val_neg(v2);
651 v1 = isl_val_cow(v1);
652 if (!v1)
653 goto error;
654 if (isl_val_is_int(v1) && isl_val_is_int(v2))
655 isl_int_sub(v1->n, v1->n, v2->n);
656 else {
657 if (isl_int_eq(v1->d, v2->d))
658 isl_int_sub(v1->n, v1->n, v2->n);
659 else {
660 isl_int_mul(v1->n, v1->n, v2->d);
661 isl_int_submul(v1->n, v2->n, v1->d);
662 isl_int_mul(v1->d, v1->d, v2->d);
664 v1 = isl_val_normalize(v1);
666 isl_val_free(v2);
667 return v1;
668 error:
669 isl_val_free(v1);
670 isl_val_free(v2);
671 return NULL;
674 /* Subtract "v2" from "v1".
676 __isl_give isl_val *isl_val_sub_ui(__isl_take isl_val *v1, unsigned long v2)
678 if (!v1)
679 return NULL;
680 if (!isl_val_is_rat(v1))
681 return v1;
682 if (v2 == 0)
683 return v1;
684 v1 = isl_val_cow(v1);
685 if (!v1)
686 return NULL;
688 isl_int_submul_ui(v1->n, v1->d, v2);
690 return v1;
693 /* Return the product of "v1" and "v2".
695 __isl_give isl_val *isl_val_mul(__isl_take isl_val *v1, __isl_take isl_val *v2)
697 if (!v1 || !v2)
698 goto error;
699 if (isl_val_is_nan(v1)) {
700 isl_val_free(v2);
701 return v1;
703 if (isl_val_is_nan(v2)) {
704 isl_val_free(v1);
705 return v2;
707 if ((!isl_val_is_rat(v1) && isl_val_is_zero(v2)) ||
708 (isl_val_is_zero(v1) && !isl_val_is_rat(v2))) {
709 isl_val_free(v2);
710 return isl_val_set_nan(v1);
712 if (isl_val_is_zero(v1)) {
713 isl_val_free(v2);
714 return v1;
716 if (isl_val_is_zero(v2)) {
717 isl_val_free(v1);
718 return v2;
720 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
721 if (isl_val_is_neg(v2))
722 v1 = isl_val_neg(v1);
723 isl_val_free(v2);
724 return v1;
726 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
727 if (isl_val_is_neg(v1))
728 v2 = isl_val_neg(v2);
729 isl_val_free(v1);
730 return v2;
733 v1 = isl_val_cow(v1);
734 if (!v1)
735 goto error;
736 if (isl_val_is_int(v1) && isl_val_is_int(v2))
737 isl_int_mul(v1->n, v1->n, v2->n);
738 else {
739 isl_int_mul(v1->n, v1->n, v2->n);
740 isl_int_mul(v1->d, v1->d, v2->d);
741 v1 = isl_val_normalize(v1);
743 isl_val_free(v2);
744 return v1;
745 error:
746 isl_val_free(v1);
747 isl_val_free(v2);
748 return NULL;
751 /* Return the product of "v1" and "v2".
753 * This is a private copy of isl_val_mul for use in the generic
754 * isl_multi_*_scale_val instantiated for isl_val.
756 __isl_give isl_val *isl_val_scale_val(__isl_take isl_val *v1,
757 __isl_take isl_val *v2)
759 return isl_val_mul(v1, v2);
762 /* Return the product of "v1" and "v2".
764 __isl_give isl_val *isl_val_mul_ui(__isl_take isl_val *v1, unsigned long v2)
766 if (!v1)
767 return NULL;
768 if (isl_val_is_nan(v1))
769 return v1;
770 if (!isl_val_is_rat(v1)) {
771 if (v2 == 0)
772 v1 = isl_val_set_nan(v1);
773 return v1;
775 if (v2 == 1)
776 return v1;
777 v1 = isl_val_cow(v1);
778 if (!v1)
779 return NULL;
781 isl_int_mul_ui(v1->n, v1->n, v2);
783 return isl_val_normalize(v1);
786 /* Divide "v1" by "v2".
788 __isl_give isl_val *isl_val_div(__isl_take isl_val *v1, __isl_take isl_val *v2)
790 if (!v1 || !v2)
791 goto error;
792 if (isl_val_is_nan(v1)) {
793 isl_val_free(v2);
794 return v1;
796 if (isl_val_is_nan(v2)) {
797 isl_val_free(v1);
798 return v2;
800 if (isl_val_is_zero(v2) ||
801 (!isl_val_is_rat(v1) && !isl_val_is_rat(v2))) {
802 isl_val_free(v2);
803 return isl_val_set_nan(v1);
805 if (isl_val_is_zero(v1)) {
806 isl_val_free(v2);
807 return v1;
809 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
810 if (isl_val_is_neg(v2))
811 v1 = isl_val_neg(v1);
812 isl_val_free(v2);
813 return v1;
815 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
816 isl_val_free(v2);
817 return isl_val_set_zero(v1);
820 v1 = isl_val_cow(v1);
821 if (!v1)
822 goto error;
823 if (isl_val_is_int(v2)) {
824 isl_int_mul(v1->d, v1->d, v2->n);
825 v1 = isl_val_normalize(v1);
826 } else {
827 isl_int_mul(v1->d, v1->d, v2->n);
828 isl_int_mul(v1->n, v1->n, v2->d);
829 v1 = isl_val_normalize(v1);
831 isl_val_free(v2);
832 return v1;
833 error:
834 isl_val_free(v1);
835 isl_val_free(v2);
836 return NULL;
839 /* Given two integer values "v1" and "v2", check if "v1" is divisible by "v2".
841 int isl_val_is_divisible_by(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
843 if (!v1 || !v2)
844 return -1;
846 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
847 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
848 "expecting two integers", return -1);
850 return isl_int_is_divisible_by(v1->n, v2->n);
853 /* Given two integer values "v1" and "v2", return the residue of "v1"
854 * modulo "v2".
856 __isl_give isl_val *isl_val_mod(__isl_take isl_val *v1, __isl_take isl_val *v2)
858 if (!v1 || !v2)
859 goto error;
860 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
861 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
862 "expecting two integers", goto error);
863 if (isl_val_is_nonneg(v1) && isl_val_lt(v1, v2)) {
864 isl_val_free(v2);
865 return v1;
867 v1 = isl_val_cow(v1);
868 if (!v1)
869 goto error;
870 isl_int_fdiv_r(v1->n, v1->n, v2->n);
871 isl_val_free(v2);
872 return v1;
873 error:
874 isl_val_free(v1);
875 isl_val_free(v2);
876 return NULL;
879 /* Given two integer values, return their greatest common divisor.
881 __isl_give isl_val *isl_val_gcd(__isl_take isl_val *v1, __isl_take isl_val *v2)
883 if (!v1 || !v2)
884 goto error;
885 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
886 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
887 "expecting two integers", goto error);
888 if (isl_val_eq(v1, v2)) {
889 isl_val_free(v2);
890 return v1;
892 if (isl_val_is_one(v1)) {
893 isl_val_free(v2);
894 return v1;
896 if (isl_val_is_one(v2)) {
897 isl_val_free(v1);
898 return v2;
900 v1 = isl_val_cow(v1);
901 if (!v1)
902 goto error;
903 isl_int_gcd(v1->n, v1->n, v2->n);
904 isl_val_free(v2);
905 return v1;
906 error:
907 isl_val_free(v1);
908 isl_val_free(v2);
909 return NULL;
912 /* Given two integer values v1 and v2, return their greatest common divisor g,
913 * as well as two integers x and y such that x * v1 + y * v2 = g.
915 __isl_give isl_val *isl_val_gcdext(__isl_take isl_val *v1,
916 __isl_take isl_val *v2, __isl_give isl_val **x, __isl_give isl_val **y)
918 isl_ctx *ctx;
919 isl_val *a = NULL, *b = NULL;
921 if (!x && !y)
922 return isl_val_gcd(v1, v2);
924 if (!v1 || !v2)
925 goto error;
927 ctx = isl_val_get_ctx(v1);
928 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
929 isl_die(ctx, isl_error_invalid,
930 "expecting two integers", goto error);
932 v1 = isl_val_cow(v1);
933 a = isl_val_alloc(ctx);
934 b = isl_val_alloc(ctx);
935 if (!v1 || !a || !b)
936 goto error;
937 isl_int_gcdext(v1->n, a->n, b->n, v1->n, v2->n);
938 if (x) {
939 isl_int_set_si(a->d, 1);
940 *x = a;
941 } else
942 isl_val_free(a);
943 if (y) {
944 isl_int_set_si(b->d, 1);
945 *y = b;
946 } else
947 isl_val_free(b);
948 isl_val_free(v2);
949 return v1;
950 error:
951 isl_val_free(v1);
952 isl_val_free(v2);
953 isl_val_free(a);
954 isl_val_free(b);
955 if (x)
956 *x = NULL;
957 if (y)
958 *y = NULL;
959 return NULL;
962 /* Does "v" represent an integer value?
964 int isl_val_is_int(__isl_keep isl_val *v)
966 if (!v)
967 return -1;
969 return isl_int_is_one(v->d);
972 /* Does "v" represent a rational value?
974 int isl_val_is_rat(__isl_keep isl_val *v)
976 if (!v)
977 return -1;
979 return !isl_int_is_zero(v->d);
982 /* Does "v" represent NaN?
984 int isl_val_is_nan(__isl_keep isl_val *v)
986 if (!v)
987 return -1;
989 return isl_int_is_zero(v->n) && isl_int_is_zero(v->d);
992 /* Does "v" represent +infinity?
994 int isl_val_is_infty(__isl_keep isl_val *v)
996 if (!v)
997 return -1;
999 return isl_int_is_pos(v->n) && isl_int_is_zero(v->d);
1002 /* Does "v" represent -infinity?
1004 int isl_val_is_neginfty(__isl_keep isl_val *v)
1006 if (!v)
1007 return -1;
1009 return isl_int_is_neg(v->n) && isl_int_is_zero(v->d);
1012 /* Does "v" represent the integer zero?
1014 int isl_val_is_zero(__isl_keep isl_val *v)
1016 if (!v)
1017 return -1;
1019 return isl_int_is_zero(v->n) && !isl_int_is_zero(v->d);
1022 /* Does "v" represent the integer one?
1024 int isl_val_is_one(__isl_keep isl_val *v)
1026 if (!v)
1027 return -1;
1029 return isl_int_eq(v->n, v->d);
1032 /* Does "v" represent the integer negative one?
1034 int isl_val_is_negone(__isl_keep isl_val *v)
1036 if (!v)
1037 return -1;
1039 return isl_int_is_neg(v->n) && isl_int_abs_eq(v->n, v->d);
1042 /* Is "v" (strictly) positive?
1044 int isl_val_is_pos(__isl_keep isl_val *v)
1046 if (!v)
1047 return -1;
1049 return isl_int_is_pos(v->n);
1052 /* Is "v" (strictly) negative?
1054 int isl_val_is_neg(__isl_keep isl_val *v)
1056 if (!v)
1057 return -1;
1059 return isl_int_is_neg(v->n);
1062 /* Is "v" non-negative?
1064 int isl_val_is_nonneg(__isl_keep isl_val *v)
1066 if (!v)
1067 return -1;
1069 if (isl_val_is_nan(v))
1070 return 0;
1072 return isl_int_is_nonneg(v->n);
1075 /* Is "v" non-positive?
1077 int isl_val_is_nonpos(__isl_keep isl_val *v)
1079 if (!v)
1080 return -1;
1082 if (isl_val_is_nan(v))
1083 return 0;
1085 return isl_int_is_nonpos(v->n);
1088 /* Return the sign of "v".
1090 * The sign of NaN is undefined.
1092 int isl_val_sgn(__isl_keep isl_val *v)
1094 if (!v)
1095 return 0;
1096 if (isl_val_is_zero(v))
1097 return 0;
1098 if (isl_val_is_pos(v))
1099 return 1;
1100 return -1;
1103 /* Is "v1" (strictly) less than "v2"?
1105 int isl_val_lt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1107 isl_int t;
1108 int lt;
1110 if (!v1 || !v2)
1111 return -1;
1112 if (isl_val_is_int(v1) && isl_val_is_int(v2))
1113 return isl_int_lt(v1->n, v2->n);
1114 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1115 return 0;
1116 if (isl_val_eq(v1, v2))
1117 return 0;
1118 if (isl_val_is_infty(v2))
1119 return 1;
1120 if (isl_val_is_infty(v1))
1121 return 0;
1122 if (isl_val_is_neginfty(v1))
1123 return 1;
1124 if (isl_val_is_neginfty(v2))
1125 return 0;
1127 isl_int_init(t);
1128 isl_int_mul(t, v1->n, v2->d);
1129 isl_int_submul(t, v2->n, v1->d);
1130 lt = isl_int_is_neg(t);
1131 isl_int_clear(t);
1133 return lt;
1136 /* Is "v1" (strictly) greater than "v2"?
1138 int isl_val_gt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1140 return isl_val_lt(v2, v1);
1143 /* Is "v1" less than or equal to "v2"?
1145 int isl_val_le(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1147 isl_int t;
1148 int le;
1150 if (!v1 || !v2)
1151 return -1;
1152 if (isl_val_is_int(v1) && isl_val_is_int(v2))
1153 return isl_int_le(v1->n, v2->n);
1154 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1155 return 0;
1156 if (isl_val_eq(v1, v2))
1157 return 1;
1158 if (isl_val_is_infty(v2))
1159 return 1;
1160 if (isl_val_is_infty(v1))
1161 return 0;
1162 if (isl_val_is_neginfty(v1))
1163 return 1;
1164 if (isl_val_is_neginfty(v2))
1165 return 0;
1167 isl_int_init(t);
1168 isl_int_mul(t, v1->n, v2->d);
1169 isl_int_submul(t, v2->n, v1->d);
1170 le = isl_int_is_nonpos(t);
1171 isl_int_clear(t);
1173 return le;
1176 /* Is "v1" greater than or equal to "v2"?
1178 int isl_val_ge(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1180 return isl_val_le(v2, v1);
1183 /* How does "v" compare to "i"?
1185 * Return 1 if v is greater, -1 if v is smaller and 0 if v is equal to i.
1187 * If v is NaN (or NULL), then the result is undefined.
1189 int isl_val_cmp_si(__isl_keep isl_val *v, long i)
1191 isl_int t;
1192 int cmp;
1194 if (!v)
1195 return 0;
1196 if (isl_val_is_int(v))
1197 return isl_int_cmp_si(v->n, i);
1198 if (isl_val_is_nan(v))
1199 return 0;
1200 if (isl_val_is_infty(v))
1201 return 1;
1202 if (isl_val_is_neginfty(v))
1203 return -1;
1205 isl_int_init(t);
1206 isl_int_mul_si(t, v->d, i);
1207 isl_int_sub(t, v->n, t);
1208 cmp = isl_int_sgn(t);
1209 isl_int_clear(t);
1211 return cmp;
1214 /* Is "v1" equal to "v2"?
1216 int isl_val_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1218 if (!v1 || !v2)
1219 return -1;
1220 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1221 return 0;
1223 return isl_int_eq(v1->n, v2->n) && isl_int_eq(v1->d, v2->d);
1226 /* Is "v1" different from "v2"?
1228 int isl_val_ne(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1230 if (!v1 || !v2)
1231 return -1;
1232 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1233 return 0;
1235 return isl_int_ne(v1->n, v2->n) || isl_int_ne(v1->d, v2->d);
1238 /* Print a textual representation of "v" onto "p".
1240 __isl_give isl_printer *isl_printer_print_val(__isl_take isl_printer *p,
1241 __isl_keep isl_val *v)
1243 int neg;
1245 if (!p || !v)
1246 return isl_printer_free(p);
1248 neg = isl_int_is_neg(v->n);
1249 if (neg) {
1250 p = isl_printer_print_str(p, "-");
1251 isl_int_neg(v->n, v->n);
1253 if (isl_int_is_zero(v->d)) {
1254 int sgn = isl_int_sgn(v->n);
1255 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1256 sgn == 0 ? "NaN" : "infty");
1257 } else
1258 p = isl_printer_print_isl_int(p, v->n);
1259 if (neg)
1260 isl_int_neg(v->n, v->n);
1261 if (!isl_int_is_zero(v->d) && !isl_int_is_one(v->d)) {
1262 p = isl_printer_print_str(p, "/");
1263 p = isl_printer_print_isl_int(p, v->d);
1266 return p;
1269 /* Insert "n" dimensions of type "type" at position "first".
1271 * This function is only meant to be used in the generic isl_multi_*
1272 * functions which have to deal with base objects that have an associated
1273 * space. Since an isl_val does not have an associated space, this function
1274 * does not do anything.
1276 __isl_give isl_val *isl_val_insert_dims(__isl_take isl_val *v,
1277 enum isl_dim_type type, unsigned first, unsigned n)
1279 return v;
1282 /* Drop the the "n" first dimensions of type "type" at position "first".
1284 * This function is only meant to be used in the generic isl_multi_*
1285 * functions which have to deal with base objects that have an associated
1286 * space. Since an isl_val does not have an associated space, this function
1287 * does not do anything.
1289 __isl_give isl_val *isl_val_drop_dims(__isl_take isl_val *v,
1290 enum isl_dim_type type, unsigned first, unsigned n)
1292 return v;
1295 /* Change the name of the dimension of type "type" at position "pos" to "s".
1297 * This function is only meant to be used in the generic isl_multi_*
1298 * functions which have to deal with base objects that have an associated
1299 * space. Since an isl_val does not have an associated space, this function
1300 * does not do anything.
1302 __isl_give isl_val *isl_val_set_dim_name(__isl_take isl_val *v,
1303 enum isl_dim_type type, unsigned pos, const char *s)
1305 return v;
1308 /* Reset the domain space of "v" to "space".
1310 * This function is only meant to be used in the generic isl_multi_*
1311 * functions which have to deal with base objects that have an associated
1312 * space. Since an isl_val does not have an associated space, this function
1313 * does not do anything, apart from error handling and cleaning up memory.
1315 __isl_give isl_val *isl_val_reset_domain_space(__isl_take isl_val *v,
1316 __isl_take isl_space *space)
1318 if (!space)
1319 return isl_val_free(v);
1320 isl_space_free(space);
1321 return v;
1324 /* Reorder the dimensions of the domain of "v" according
1325 * to the given reordering.
1327 * This function is only meant to be used in the generic isl_multi_*
1328 * functions which have to deal with base objects that have an associated
1329 * space. Since an isl_val does not have an associated space, this function
1330 * does not do anything, apart from error handling and cleaning up memory.
1332 __isl_give isl_val *isl_val_realign_domain(__isl_take isl_val *v,
1333 __isl_take isl_reordering *r)
1335 if (!r)
1336 return isl_val_free(v);
1337 isl_reordering_free(r);
1338 return v;
1341 /* Return an isl_val that is zero on "ls".
1343 * This function is only meant to be used in the generic isl_multi_*
1344 * functions which have to deal with base objects that have an associated
1345 * space. Since an isl_val does not have an associated space, this function
1346 * simply returns a zero isl_val in the same context as "ls".
1348 __isl_give isl_val *isl_val_zero_on_domain(__isl_take isl_local_space *ls)
1350 isl_ctx *ctx;
1352 if (!ls)
1353 return NULL;
1354 ctx = isl_local_space_get_ctx(ls);
1355 isl_local_space_free(ls);
1356 return isl_val_zero(ctx);
1359 /* Check that the domain space of "v" matches "space".
1361 * Return 0 on success and -1 on error.
1363 * This function is only meant to be used in the generic isl_multi_*
1364 * functions which have to deal with base objects that have an associated
1365 * space. Since an isl_val does not have an associated space, this function
1366 * simply returns 0, except if "v" or "space" are NULL.
1368 int isl_val_check_match_domain_space(__isl_keep isl_val *v,
1369 __isl_keep isl_space *space)
1371 if (!v || !space)
1372 return -1;
1373 return 0;
1376 #undef BASE
1377 #define BASE val
1379 #define NO_GIST
1380 #define NO_IDENTITY
1381 #define NO_FROM_BASE
1382 #include <isl_multi_templ.c>
1384 /* Apply "fn" to each of the elements of "mv" with as second argument "v".
1386 static __isl_give isl_multi_val *isl_multi_val_fn_val(
1387 __isl_take isl_multi_val *mv,
1388 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
1389 __isl_take isl_val *v2),
1390 __isl_take isl_val *v)
1392 int i;
1394 mv = isl_multi_val_cow(mv);
1395 if (!mv || !v)
1396 goto error;
1398 for (i = 0; i < mv->n; ++i) {
1399 mv->p[i] = fn(mv->p[i], isl_val_copy(v));
1400 if (!mv->p[i])
1401 goto error;
1404 isl_val_free(v);
1405 return mv;
1406 error:
1407 isl_val_free(v);
1408 isl_multi_val_free(mv);
1409 return NULL;
1412 /* Add "v" to each of the elements of "mv".
1414 __isl_give isl_multi_val *isl_multi_val_add_val(__isl_take isl_multi_val *mv,
1415 __isl_take isl_val *v)
1417 if (!v)
1418 return isl_multi_val_free(mv);
1419 if (isl_val_is_zero(v)) {
1420 isl_val_free(v);
1421 return mv;
1423 return isl_multi_val_fn_val(mv, &isl_val_add, v);
1426 /* Reduce the elements of "mv" modulo "v".
1428 __isl_give isl_multi_val *isl_multi_val_mod_val(__isl_take isl_multi_val *mv,
1429 __isl_take isl_val *v)
1431 return isl_multi_val_fn_val(mv, &isl_val_mod, v);