polyhedron_sample.c: avoid use of isl_vec internals
[isl.git] / isl_val.c
blobdca322898e060fb609ddff05859046f868e6d0ea
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 numerator of a rational value "v" as an isl_int.
289 * If "v" is not a rational value, then the result is undefined.
291 int isl_val_get_num_isl_int(__isl_keep isl_val *v, isl_int *n)
293 if (!v)
294 return -1;
295 if (!isl_val_is_rat(v))
296 isl_die(isl_val_get_ctx(v), isl_error_invalid,
297 "expecting rational value", return -1);
298 isl_int_set(*n, v->n);
299 return 0;
302 /* Extract the denominator of a rational value "v" as an integer.
304 * If "v" is not a rational value, then the result is undefined.
306 long isl_val_get_den_si(__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 if (!isl_int_fits_slong(v->d))
314 isl_die(isl_val_get_ctx(v), isl_error_invalid,
315 "denominator too large", return 0);
316 return isl_int_get_si(v->d);
319 /* Return an approximation of "v" as a double.
321 double isl_val_get_d(__isl_keep isl_val *v)
323 if (!v)
324 return 0;
325 if (!isl_val_is_rat(v))
326 isl_die(isl_val_get_ctx(v), isl_error_invalid,
327 "expecting rational value", return 0);
328 return isl_int_get_d(v->n) / isl_int_get_d(v->d);
331 /* Return the isl_ctx to which "val" belongs.
333 isl_ctx *isl_val_get_ctx(__isl_keep isl_val *val)
335 return val ? val->ctx : NULL;
338 /* Normalize "v".
340 * In particular, make sure that the denominator of a rational value
341 * is positive and the numerator and denominator do not have any
342 * common divisors.
344 * This function should not be called by an external user
345 * since it will only be given normalized values.
347 __isl_give isl_val *isl_val_normalize(__isl_take isl_val *v)
349 isl_ctx *ctx;
351 if (!v)
352 return NULL;
353 if (isl_val_is_int(v))
354 return v;
355 if (!isl_val_is_rat(v))
356 return v;
357 if (isl_int_is_neg(v->d)) {
358 isl_int_neg(v->d, v->d);
359 isl_int_neg(v->n, v->n);
361 ctx = isl_val_get_ctx(v);
362 isl_int_gcd(ctx->normalize_gcd, v->n, v->d);
363 if (isl_int_is_one(ctx->normalize_gcd))
364 return v;
365 isl_int_divexact(v->n, v->n, ctx->normalize_gcd);
366 isl_int_divexact(v->d, v->d, ctx->normalize_gcd);
367 return v;
370 /* Return the opposite of "v".
372 __isl_give isl_val *isl_val_neg(__isl_take isl_val *v)
374 if (!v)
375 return NULL;
376 if (isl_val_is_nan(v))
377 return v;
378 if (isl_val_is_zero(v))
379 return v;
381 v = isl_val_cow(v);
382 if (!v)
383 return NULL;
384 isl_int_neg(v->n, v->n);
386 return v;
389 /* Return the absolute value of "v".
391 __isl_give isl_val *isl_val_abs(__isl_take isl_val *v)
393 if (!v)
394 return NULL;
395 if (isl_val_is_nan(v))
396 return v;
397 if (isl_val_is_nonneg(v))
398 return v;
399 return isl_val_neg(v);
402 /* Return the "floor" (greatest integer part) of "v".
403 * That is, return the result of rounding towards -infinity.
405 __isl_give isl_val *isl_val_floor(__isl_take isl_val *v)
407 if (!v)
408 return NULL;
409 if (isl_val_is_int(v))
410 return v;
411 if (!isl_val_is_rat(v))
412 return v;
414 v = isl_val_cow(v);
415 if (!v)
416 return NULL;
417 isl_int_fdiv_q(v->n, v->n, v->d);
418 isl_int_set_si(v->d, 1);
420 return v;
423 /* Return the "ceiling" of "v".
424 * That is, return the result of rounding towards +infinity.
426 __isl_give isl_val *isl_val_ceil(__isl_take isl_val *v)
428 if (!v)
429 return NULL;
430 if (isl_val_is_int(v))
431 return v;
432 if (!isl_val_is_rat(v))
433 return v;
435 v = isl_val_cow(v);
436 if (!v)
437 return NULL;
438 isl_int_cdiv_q(v->n, v->n, v->d);
439 isl_int_set_si(v->d, 1);
441 return v;
444 /* Truncate "v".
445 * That is, return the result of rounding towards zero.
447 __isl_give isl_val *isl_val_trunc(__isl_take isl_val *v)
449 if (!v)
450 return NULL;
451 if (isl_val_is_int(v))
452 return v;
453 if (!isl_val_is_rat(v))
454 return v;
456 v = isl_val_cow(v);
457 if (!v)
458 return NULL;
459 isl_int_tdiv_q(v->n, v->n, v->d);
460 isl_int_set_si(v->d, 1);
462 return v;
465 /* Return 2^v, where v is an integer (that is not too large).
467 __isl_give isl_val *isl_val_2exp(__isl_take isl_val *v)
469 unsigned long exp;
470 int neg;
472 v = isl_val_cow(v);
473 if (!v)
474 return NULL;
475 if (!isl_val_is_int(v))
476 isl_die(isl_val_get_ctx(v), isl_error_invalid,
477 "can only compute integer powers",
478 return isl_val_free(v));
479 neg = isl_val_is_neg(v);
480 if (neg)
481 isl_int_neg(v->n, v->n);
482 if (!isl_int_fits_ulong(v->n))
483 isl_die(isl_val_get_ctx(v), isl_error_invalid,
484 "exponent too large", return isl_val_free(v));
485 exp = isl_int_get_ui(v->n);
486 if (neg) {
487 isl_int_mul_2exp(v->d, v->d, exp);
488 isl_int_set_si(v->n, 1);
489 } else {
490 isl_int_mul_2exp(v->n, v->d, exp);
493 return v;
496 /* Return the minimum of "v1" and "v2".
498 __isl_give isl_val *isl_val_min(__isl_take isl_val *v1, __isl_take isl_val *v2)
500 if (!v1 || !v2)
501 goto error;
503 if (isl_val_is_nan(v1)) {
504 isl_val_free(v2);
505 return v1;
507 if (isl_val_is_nan(v2)) {
508 isl_val_free(v1);
509 return v2;
511 if (isl_val_le(v1, v2)) {
512 isl_val_free(v2);
513 return v1;
514 } else {
515 isl_val_free(v1);
516 return v2;
518 error:
519 isl_val_free(v1);
520 isl_val_free(v2);
521 return NULL;
524 /* Return the maximum of "v1" and "v2".
526 __isl_give isl_val *isl_val_max(__isl_take isl_val *v1, __isl_take isl_val *v2)
528 if (!v1 || !v2)
529 goto error;
531 if (isl_val_is_nan(v1)) {
532 isl_val_free(v2);
533 return v1;
535 if (isl_val_is_nan(v2)) {
536 isl_val_free(v1);
537 return v2;
539 if (isl_val_ge(v1, v2)) {
540 isl_val_free(v2);
541 return v1;
542 } else {
543 isl_val_free(v1);
544 return v2;
546 error:
547 isl_val_free(v1);
548 isl_val_free(v2);
549 return NULL;
552 /* Return the sum of "v1" and "v2".
554 __isl_give isl_val *isl_val_add(__isl_take isl_val *v1, __isl_take isl_val *v2)
556 if (!v1 || !v2)
557 goto error;
558 if (isl_val_is_nan(v1)) {
559 isl_val_free(v2);
560 return v1;
562 if (isl_val_is_nan(v2)) {
563 isl_val_free(v1);
564 return v2;
566 if ((isl_val_is_infty(v1) && isl_val_is_neginfty(v2)) ||
567 (isl_val_is_neginfty(v1) && isl_val_is_infty(v2))) {
568 isl_val_free(v2);
569 return isl_val_set_nan(v1);
571 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
572 isl_val_free(v2);
573 return v1;
575 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
576 isl_val_free(v1);
577 return v2;
579 if (isl_val_is_zero(v1)) {
580 isl_val_free(v1);
581 return v2;
583 if (isl_val_is_zero(v2)) {
584 isl_val_free(v2);
585 return v1;
588 v1 = isl_val_cow(v1);
589 if (!v1)
590 goto error;
591 if (isl_val_is_int(v1) && isl_val_is_int(v2))
592 isl_int_add(v1->n, v1->n, v2->n);
593 else {
594 if (isl_int_eq(v1->d, v2->d))
595 isl_int_add(v1->n, v1->n, v2->n);
596 else {
597 isl_int_mul(v1->n, v1->n, v2->d);
598 isl_int_addmul(v1->n, v2->n, v1->d);
599 isl_int_mul(v1->d, v1->d, v2->d);
601 v1 = isl_val_normalize(v1);
603 isl_val_free(v2);
604 return v1;
605 error:
606 isl_val_free(v1);
607 isl_val_free(v2);
608 return NULL;
611 /* Return the sum of "v1" and "v2".
613 __isl_give isl_val *isl_val_add_ui(__isl_take isl_val *v1, unsigned long v2)
615 if (!v1)
616 return NULL;
617 if (!isl_val_is_rat(v1))
618 return v1;
619 if (v2 == 0)
620 return v1;
621 v1 = isl_val_cow(v1);
622 if (!v1)
623 return NULL;
625 isl_int_addmul_ui(v1->n, v1->d, v2);
627 return v1;
630 /* Subtract "v2" from "v1".
632 __isl_give isl_val *isl_val_sub(__isl_take isl_val *v1, __isl_take isl_val *v2)
634 if (!v1 || !v2)
635 goto error;
636 if (isl_val_is_nan(v1)) {
637 isl_val_free(v2);
638 return v1;
640 if (isl_val_is_nan(v2)) {
641 isl_val_free(v1);
642 return v2;
644 if ((isl_val_is_infty(v1) && isl_val_is_infty(v2)) ||
645 (isl_val_is_neginfty(v1) && isl_val_is_neginfty(v2))) {
646 isl_val_free(v2);
647 return isl_val_set_nan(v1);
649 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
650 isl_val_free(v2);
651 return v1;
653 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
654 isl_val_free(v1);
655 return isl_val_neg(v2);
657 if (isl_val_is_zero(v2)) {
658 isl_val_free(v2);
659 return v1;
661 if (isl_val_is_zero(v1)) {
662 isl_val_free(v1);
663 return isl_val_neg(v2);
666 v1 = isl_val_cow(v1);
667 if (!v1)
668 goto error;
669 if (isl_val_is_int(v1) && isl_val_is_int(v2))
670 isl_int_sub(v1->n, v1->n, v2->n);
671 else {
672 if (isl_int_eq(v1->d, v2->d))
673 isl_int_sub(v1->n, v1->n, v2->n);
674 else {
675 isl_int_mul(v1->n, v1->n, v2->d);
676 isl_int_submul(v1->n, v2->n, v1->d);
677 isl_int_mul(v1->d, v1->d, v2->d);
679 v1 = isl_val_normalize(v1);
681 isl_val_free(v2);
682 return v1;
683 error:
684 isl_val_free(v1);
685 isl_val_free(v2);
686 return NULL;
689 /* Subtract "v2" from "v1".
691 __isl_give isl_val *isl_val_sub_ui(__isl_take isl_val *v1, unsigned long v2)
693 if (!v1)
694 return NULL;
695 if (!isl_val_is_rat(v1))
696 return v1;
697 if (v2 == 0)
698 return v1;
699 v1 = isl_val_cow(v1);
700 if (!v1)
701 return NULL;
703 isl_int_submul_ui(v1->n, v1->d, v2);
705 return v1;
708 /* Return the product of "v1" and "v2".
710 __isl_give isl_val *isl_val_mul(__isl_take isl_val *v1, __isl_take isl_val *v2)
712 if (!v1 || !v2)
713 goto error;
714 if (isl_val_is_nan(v1)) {
715 isl_val_free(v2);
716 return v1;
718 if (isl_val_is_nan(v2)) {
719 isl_val_free(v1);
720 return v2;
722 if ((!isl_val_is_rat(v1) && isl_val_is_zero(v2)) ||
723 (isl_val_is_zero(v1) && !isl_val_is_rat(v2))) {
724 isl_val_free(v2);
725 return isl_val_set_nan(v1);
727 if (isl_val_is_zero(v1)) {
728 isl_val_free(v2);
729 return v1;
731 if (isl_val_is_zero(v2)) {
732 isl_val_free(v1);
733 return v2;
735 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
736 if (isl_val_is_neg(v2))
737 v1 = isl_val_neg(v1);
738 isl_val_free(v2);
739 return v1;
741 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
742 if (isl_val_is_neg(v1))
743 v2 = isl_val_neg(v2);
744 isl_val_free(v1);
745 return v2;
748 v1 = isl_val_cow(v1);
749 if (!v1)
750 goto error;
751 if (isl_val_is_int(v1) && isl_val_is_int(v2))
752 isl_int_mul(v1->n, v1->n, v2->n);
753 else {
754 isl_int_mul(v1->n, v1->n, v2->n);
755 isl_int_mul(v1->d, v1->d, v2->d);
756 v1 = isl_val_normalize(v1);
758 isl_val_free(v2);
759 return v1;
760 error:
761 isl_val_free(v1);
762 isl_val_free(v2);
763 return NULL;
766 /* Return the product of "v1" and "v2".
768 * This is a private copy of isl_val_mul for use in the generic
769 * isl_multi_*_scale_val instantiated for isl_val.
771 __isl_give isl_val *isl_val_scale_val(__isl_take isl_val *v1,
772 __isl_take isl_val *v2)
774 return isl_val_mul(v1, v2);
777 /* Return the product of "v1" and "v2".
779 __isl_give isl_val *isl_val_mul_ui(__isl_take isl_val *v1, unsigned long v2)
781 if (!v1)
782 return NULL;
783 if (isl_val_is_nan(v1))
784 return v1;
785 if (!isl_val_is_rat(v1)) {
786 if (v2 == 0)
787 v1 = isl_val_set_nan(v1);
788 return v1;
790 if (v2 == 1)
791 return v1;
792 v1 = isl_val_cow(v1);
793 if (!v1)
794 return NULL;
796 isl_int_mul_ui(v1->n, v1->n, v2);
798 return isl_val_normalize(v1);
801 /* Divide "v1" by "v2".
803 __isl_give isl_val *isl_val_div(__isl_take isl_val *v1, __isl_take isl_val *v2)
805 if (!v1 || !v2)
806 goto error;
807 if (isl_val_is_nan(v1)) {
808 isl_val_free(v2);
809 return v1;
811 if (isl_val_is_nan(v2)) {
812 isl_val_free(v1);
813 return v2;
815 if (isl_val_is_zero(v2) ||
816 (!isl_val_is_rat(v1) && !isl_val_is_rat(v2))) {
817 isl_val_free(v2);
818 return isl_val_set_nan(v1);
820 if (isl_val_is_zero(v1)) {
821 isl_val_free(v2);
822 return v1;
824 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
825 if (isl_val_is_neg(v2))
826 v1 = isl_val_neg(v1);
827 isl_val_free(v2);
828 return v1;
830 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
831 isl_val_free(v2);
832 return isl_val_set_zero(v1);
835 v1 = isl_val_cow(v1);
836 if (!v1)
837 goto error;
838 if (isl_val_is_int(v2)) {
839 isl_int_mul(v1->d, v1->d, v2->n);
840 v1 = isl_val_normalize(v1);
841 } else {
842 isl_int_mul(v1->d, v1->d, v2->n);
843 isl_int_mul(v1->n, v1->n, v2->d);
844 v1 = isl_val_normalize(v1);
846 isl_val_free(v2);
847 return v1;
848 error:
849 isl_val_free(v1);
850 isl_val_free(v2);
851 return NULL;
854 /* Given two integer values "v1" and "v2", check if "v1" is divisible by "v2".
856 int isl_val_is_divisible_by(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
858 if (!v1 || !v2)
859 return -1;
861 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
862 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
863 "expecting two integers", return -1);
865 return isl_int_is_divisible_by(v1->n, v2->n);
868 /* Given two integer values "v1" and "v2", return the residue of "v1"
869 * modulo "v2".
871 __isl_give isl_val *isl_val_mod(__isl_take isl_val *v1, __isl_take isl_val *v2)
873 if (!v1 || !v2)
874 goto error;
875 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
876 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
877 "expecting two integers", goto error);
878 if (isl_val_is_nonneg(v1) && isl_val_lt(v1, v2)) {
879 isl_val_free(v2);
880 return v1;
882 v1 = isl_val_cow(v1);
883 if (!v1)
884 goto error;
885 isl_int_fdiv_r(v1->n, v1->n, v2->n);
886 isl_val_free(v2);
887 return v1;
888 error:
889 isl_val_free(v1);
890 isl_val_free(v2);
891 return NULL;
894 /* Given two integer values, return their greatest common divisor.
896 __isl_give isl_val *isl_val_gcd(__isl_take isl_val *v1, __isl_take isl_val *v2)
898 if (!v1 || !v2)
899 goto error;
900 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
901 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
902 "expecting two integers", goto error);
903 if (isl_val_eq(v1, v2)) {
904 isl_val_free(v2);
905 return v1;
907 if (isl_val_is_one(v1)) {
908 isl_val_free(v2);
909 return v1;
911 if (isl_val_is_one(v2)) {
912 isl_val_free(v1);
913 return v2;
915 v1 = isl_val_cow(v1);
916 if (!v1)
917 goto error;
918 isl_int_gcd(v1->n, v1->n, v2->n);
919 isl_val_free(v2);
920 return v1;
921 error:
922 isl_val_free(v1);
923 isl_val_free(v2);
924 return NULL;
927 /* Given two integer values v1 and v2, return their greatest common divisor g,
928 * as well as two integers x and y such that x * v1 + y * v2 = g.
930 __isl_give isl_val *isl_val_gcdext(__isl_take isl_val *v1,
931 __isl_take isl_val *v2, __isl_give isl_val **x, __isl_give isl_val **y)
933 isl_ctx *ctx;
934 isl_val *a = NULL, *b = NULL;
936 if (!x && !y)
937 return isl_val_gcd(v1, v2);
939 if (!v1 || !v2)
940 goto error;
942 ctx = isl_val_get_ctx(v1);
943 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
944 isl_die(ctx, isl_error_invalid,
945 "expecting two integers", goto error);
947 v1 = isl_val_cow(v1);
948 a = isl_val_alloc(ctx);
949 b = isl_val_alloc(ctx);
950 if (!v1 || !a || !b)
951 goto error;
952 isl_int_gcdext(v1->n, a->n, b->n, v1->n, v2->n);
953 if (x) {
954 isl_int_set_si(a->d, 1);
955 *x = a;
956 } else
957 isl_val_free(a);
958 if (y) {
959 isl_int_set_si(b->d, 1);
960 *y = b;
961 } else
962 isl_val_free(b);
963 isl_val_free(v2);
964 return v1;
965 error:
966 isl_val_free(v1);
967 isl_val_free(v2);
968 isl_val_free(a);
969 isl_val_free(b);
970 if (x)
971 *x = NULL;
972 if (y)
973 *y = NULL;
974 return NULL;
977 /* Does "v" represent an integer value?
979 int isl_val_is_int(__isl_keep isl_val *v)
981 if (!v)
982 return -1;
984 return isl_int_is_one(v->d);
987 /* Does "v" represent a rational value?
989 int isl_val_is_rat(__isl_keep isl_val *v)
991 if (!v)
992 return -1;
994 return !isl_int_is_zero(v->d);
997 /* Does "v" represent NaN?
999 int isl_val_is_nan(__isl_keep isl_val *v)
1001 if (!v)
1002 return -1;
1004 return isl_int_is_zero(v->n) && isl_int_is_zero(v->d);
1007 /* Does "v" represent +infinity?
1009 int isl_val_is_infty(__isl_keep isl_val *v)
1011 if (!v)
1012 return -1;
1014 return isl_int_is_pos(v->n) && isl_int_is_zero(v->d);
1017 /* Does "v" represent -infinity?
1019 int isl_val_is_neginfty(__isl_keep isl_val *v)
1021 if (!v)
1022 return -1;
1024 return isl_int_is_neg(v->n) && isl_int_is_zero(v->d);
1027 /* Does "v" represent the integer zero?
1029 int isl_val_is_zero(__isl_keep isl_val *v)
1031 if (!v)
1032 return -1;
1034 return isl_int_is_zero(v->n) && !isl_int_is_zero(v->d);
1037 /* Does "v" represent the integer one?
1039 int isl_val_is_one(__isl_keep isl_val *v)
1041 if (!v)
1042 return -1;
1044 return isl_int_eq(v->n, v->d);
1047 /* Does "v" represent the integer negative one?
1049 int isl_val_is_negone(__isl_keep isl_val *v)
1051 if (!v)
1052 return -1;
1054 return isl_int_is_neg(v->n) && isl_int_abs_eq(v->n, v->d);
1057 /* Is "v" (strictly) positive?
1059 int isl_val_is_pos(__isl_keep isl_val *v)
1061 if (!v)
1062 return -1;
1064 return isl_int_is_pos(v->n);
1067 /* Is "v" (strictly) negative?
1069 int isl_val_is_neg(__isl_keep isl_val *v)
1071 if (!v)
1072 return -1;
1074 return isl_int_is_neg(v->n);
1077 /* Is "v" non-negative?
1079 int isl_val_is_nonneg(__isl_keep isl_val *v)
1081 if (!v)
1082 return -1;
1084 if (isl_val_is_nan(v))
1085 return 0;
1087 return isl_int_is_nonneg(v->n);
1090 /* Is "v" non-positive?
1092 int isl_val_is_nonpos(__isl_keep isl_val *v)
1094 if (!v)
1095 return -1;
1097 if (isl_val_is_nan(v))
1098 return 0;
1100 return isl_int_is_nonpos(v->n);
1103 /* Return the sign of "v".
1105 * The sign of NaN is undefined.
1107 int isl_val_sgn(__isl_keep isl_val *v)
1109 if (!v)
1110 return 0;
1111 if (isl_val_is_zero(v))
1112 return 0;
1113 if (isl_val_is_pos(v))
1114 return 1;
1115 return -1;
1118 /* Is "v1" (strictly) less than "v2"?
1120 int isl_val_lt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1122 isl_int t;
1123 int lt;
1125 if (!v1 || !v2)
1126 return -1;
1127 if (isl_val_is_int(v1) && isl_val_is_int(v2))
1128 return isl_int_lt(v1->n, v2->n);
1129 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1130 return 0;
1131 if (isl_val_eq(v1, v2))
1132 return 0;
1133 if (isl_val_is_infty(v2))
1134 return 1;
1135 if (isl_val_is_infty(v1))
1136 return 0;
1137 if (isl_val_is_neginfty(v1))
1138 return 1;
1139 if (isl_val_is_neginfty(v2))
1140 return 0;
1142 isl_int_init(t);
1143 isl_int_mul(t, v1->n, v2->d);
1144 isl_int_submul(t, v2->n, v1->d);
1145 lt = isl_int_is_neg(t);
1146 isl_int_clear(t);
1148 return lt;
1151 /* Is "v1" (strictly) greater than "v2"?
1153 int isl_val_gt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1155 return isl_val_lt(v2, v1);
1158 /* Is "v1" less than or equal to "v2"?
1160 int isl_val_le(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1162 isl_int t;
1163 int le;
1165 if (!v1 || !v2)
1166 return -1;
1167 if (isl_val_is_int(v1) && isl_val_is_int(v2))
1168 return isl_int_le(v1->n, v2->n);
1169 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1170 return 0;
1171 if (isl_val_eq(v1, v2))
1172 return 1;
1173 if (isl_val_is_infty(v2))
1174 return 1;
1175 if (isl_val_is_infty(v1))
1176 return 0;
1177 if (isl_val_is_neginfty(v1))
1178 return 1;
1179 if (isl_val_is_neginfty(v2))
1180 return 0;
1182 isl_int_init(t);
1183 isl_int_mul(t, v1->n, v2->d);
1184 isl_int_submul(t, v2->n, v1->d);
1185 le = isl_int_is_nonpos(t);
1186 isl_int_clear(t);
1188 return le;
1191 /* Is "v1" greater than or equal to "v2"?
1193 int isl_val_ge(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1195 return isl_val_le(v2, v1);
1198 /* How does "v" compare to "i"?
1200 * Return 1 if v is greater, -1 if v is smaller and 0 if v is equal to i.
1202 * If v is NaN (or NULL), then the result is undefined.
1204 int isl_val_cmp_si(__isl_keep isl_val *v, long i)
1206 isl_int t;
1207 int cmp;
1209 if (!v)
1210 return 0;
1211 if (isl_val_is_int(v))
1212 return isl_int_cmp_si(v->n, i);
1213 if (isl_val_is_nan(v))
1214 return 0;
1215 if (isl_val_is_infty(v))
1216 return 1;
1217 if (isl_val_is_neginfty(v))
1218 return -1;
1220 isl_int_init(t);
1221 isl_int_mul_si(t, v->d, i);
1222 isl_int_sub(t, v->n, t);
1223 cmp = isl_int_sgn(t);
1224 isl_int_clear(t);
1226 return cmp;
1229 /* Is "v1" equal to "v2"?
1231 int isl_val_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1233 if (!v1 || !v2)
1234 return -1;
1235 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1236 return 0;
1238 return isl_int_eq(v1->n, v2->n) && isl_int_eq(v1->d, v2->d);
1241 /* Is "v1" different from "v2"?
1243 int isl_val_ne(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1245 if (!v1 || !v2)
1246 return -1;
1247 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1248 return 0;
1250 return isl_int_ne(v1->n, v2->n) || isl_int_ne(v1->d, v2->d);
1253 /* Print a textual representation of "v" onto "p".
1255 __isl_give isl_printer *isl_printer_print_val(__isl_take isl_printer *p,
1256 __isl_keep isl_val *v)
1258 int neg;
1260 if (!p || !v)
1261 return isl_printer_free(p);
1263 neg = isl_int_is_neg(v->n);
1264 if (neg) {
1265 p = isl_printer_print_str(p, "-");
1266 isl_int_neg(v->n, v->n);
1268 if (isl_int_is_zero(v->d)) {
1269 int sgn = isl_int_sgn(v->n);
1270 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1271 sgn == 0 ? "NaN" : "infty");
1272 } else
1273 p = isl_printer_print_isl_int(p, v->n);
1274 if (neg)
1275 isl_int_neg(v->n, v->n);
1276 if (!isl_int_is_zero(v->d) && !isl_int_is_one(v->d)) {
1277 p = isl_printer_print_str(p, "/");
1278 p = isl_printer_print_isl_int(p, v->d);
1281 return p;
1284 /* Insert "n" dimensions of type "type" at position "first".
1286 * This function is only meant to be used in the generic isl_multi_*
1287 * functions which have to deal with base objects that have an associated
1288 * space. Since an isl_val does not have an associated space, this function
1289 * does not do anything.
1291 __isl_give isl_val *isl_val_insert_dims(__isl_take isl_val *v,
1292 enum isl_dim_type type, unsigned first, unsigned n)
1294 return v;
1297 /* Drop the the "n" first dimensions of type "type" at position "first".
1299 * This function is only meant to be used in the generic isl_multi_*
1300 * functions which have to deal with base objects that have an associated
1301 * space. Since an isl_val does not have an associated space, this function
1302 * does not do anything.
1304 __isl_give isl_val *isl_val_drop_dims(__isl_take isl_val *v,
1305 enum isl_dim_type type, unsigned first, unsigned n)
1307 return v;
1310 /* Change the name of the dimension of type "type" at position "pos" to "s".
1312 * This function is only meant to be used in the generic isl_multi_*
1313 * functions which have to deal with base objects that have an associated
1314 * space. Since an isl_val does not have an associated space, this function
1315 * does not do anything.
1317 __isl_give isl_val *isl_val_set_dim_name(__isl_take isl_val *v,
1318 enum isl_dim_type type, unsigned pos, const char *s)
1320 return v;
1323 /* Reset the domain space of "v" to "space".
1325 * This function is only meant to be used in the generic isl_multi_*
1326 * functions which have to deal with base objects that have an associated
1327 * space. Since an isl_val does not have an associated space, this function
1328 * does not do anything, apart from error handling and cleaning up memory.
1330 __isl_give isl_val *isl_val_reset_domain_space(__isl_take isl_val *v,
1331 __isl_take isl_space *space)
1333 if (!space)
1334 return isl_val_free(v);
1335 isl_space_free(space);
1336 return v;
1339 /* Reorder the dimensions of the domain of "v" according
1340 * to the given reordering.
1342 * This function is only meant to be used in the generic isl_multi_*
1343 * functions which have to deal with base objects that have an associated
1344 * space. Since an isl_val does not have an associated space, this function
1345 * does not do anything, apart from error handling and cleaning up memory.
1347 __isl_give isl_val *isl_val_realign_domain(__isl_take isl_val *v,
1348 __isl_take isl_reordering *r)
1350 if (!r)
1351 return isl_val_free(v);
1352 isl_reordering_free(r);
1353 return v;
1356 /* Return an isl_val that is zero on "ls".
1358 * This function is only meant to be used in the generic isl_multi_*
1359 * functions which have to deal with base objects that have an associated
1360 * space. Since an isl_val does not have an associated space, this function
1361 * simply returns a zero isl_val in the same context as "ls".
1363 __isl_give isl_val *isl_val_zero_on_domain(__isl_take isl_local_space *ls)
1365 isl_ctx *ctx;
1367 if (!ls)
1368 return NULL;
1369 ctx = isl_local_space_get_ctx(ls);
1370 isl_local_space_free(ls);
1371 return isl_val_zero(ctx);
1374 /* Check that the domain space of "v" matches "space".
1376 * Return 0 on success and -1 on error.
1378 * This function is only meant to be used in the generic isl_multi_*
1379 * functions which have to deal with base objects that have an associated
1380 * space. Since an isl_val does not have an associated space, this function
1381 * simply returns 0, except if "v" or "space" are NULL.
1383 int isl_val_check_match_domain_space(__isl_keep isl_val *v,
1384 __isl_keep isl_space *space)
1386 if (!v || !space)
1387 return -1;
1388 return 0;
1391 #undef BASE
1392 #define BASE val
1394 #define NO_GIST
1395 #define NO_IDENTITY
1396 #define NO_FROM_BASE
1397 #include <isl_multi_templ.c>
1399 /* Apply "fn" to each of the elements of "mv" with as second argument "v".
1401 static __isl_give isl_multi_val *isl_multi_val_fn_val(
1402 __isl_take isl_multi_val *mv,
1403 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
1404 __isl_take isl_val *v2),
1405 __isl_take isl_val *v)
1407 int i;
1409 mv = isl_multi_val_cow(mv);
1410 if (!mv || !v)
1411 goto error;
1413 for (i = 0; i < mv->n; ++i) {
1414 mv->p[i] = fn(mv->p[i], isl_val_copy(v));
1415 if (!mv->p[i])
1416 goto error;
1419 isl_val_free(v);
1420 return mv;
1421 error:
1422 isl_val_free(v);
1423 isl_multi_val_free(mv);
1424 return NULL;
1427 /* Add "v" to each of the elements of "mv".
1429 __isl_give isl_multi_val *isl_multi_val_add_val(__isl_take isl_multi_val *mv,
1430 __isl_take isl_val *v)
1432 if (!v)
1433 return isl_multi_val_free(mv);
1434 if (isl_val_is_zero(v)) {
1435 isl_val_free(v);
1436 return mv;
1438 return isl_multi_val_fn_val(mv, &isl_val_add, v);
1441 /* Reduce the elements of "mv" modulo "v".
1443 __isl_give isl_multi_val *isl_multi_val_mod_val(__isl_take isl_multi_val *mv,
1444 __isl_take isl_val *v)
1446 return isl_multi_val_fn_val(mv, &isl_val_mod, v);