add isl_aff_set_tuple_id
[isl.git] / isl_val.c
blob7c4a9ac981ae78baf90c210a60edc549c2a2fbfc
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 /* Divide "v1" by "v2".
856 * This is a private copy of isl_val_div for use in the generic
857 * isl_multi_*_scale_down_val instantiated for isl_val.
859 __isl_give isl_val *isl_val_scale_down_val(__isl_take isl_val *v1,
860 __isl_take isl_val *v2)
862 return isl_val_div(v1, v2);
865 /* Given two integer values "v1" and "v2", check if "v1" is divisible by "v2".
867 int isl_val_is_divisible_by(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
869 if (!v1 || !v2)
870 return -1;
872 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
873 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
874 "expecting two integers", return -1);
876 return isl_int_is_divisible_by(v1->n, v2->n);
879 /* Given two integer values "v1" and "v2", return the residue of "v1"
880 * modulo "v2".
882 __isl_give isl_val *isl_val_mod(__isl_take isl_val *v1, __isl_take isl_val *v2)
884 if (!v1 || !v2)
885 goto error;
886 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
887 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
888 "expecting two integers", goto error);
889 if (isl_val_is_nonneg(v1) && isl_val_lt(v1, v2)) {
890 isl_val_free(v2);
891 return v1;
893 v1 = isl_val_cow(v1);
894 if (!v1)
895 goto error;
896 isl_int_fdiv_r(v1->n, v1->n, v2->n);
897 isl_val_free(v2);
898 return v1;
899 error:
900 isl_val_free(v1);
901 isl_val_free(v2);
902 return NULL;
905 /* Given two integer values, return their greatest common divisor.
907 __isl_give isl_val *isl_val_gcd(__isl_take isl_val *v1, __isl_take isl_val *v2)
909 if (!v1 || !v2)
910 goto error;
911 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
912 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
913 "expecting two integers", goto error);
914 if (isl_val_eq(v1, v2)) {
915 isl_val_free(v2);
916 return v1;
918 if (isl_val_is_one(v1)) {
919 isl_val_free(v2);
920 return v1;
922 if (isl_val_is_one(v2)) {
923 isl_val_free(v1);
924 return v2;
926 v1 = isl_val_cow(v1);
927 if (!v1)
928 goto error;
929 isl_int_gcd(v1->n, v1->n, v2->n);
930 isl_val_free(v2);
931 return v1;
932 error:
933 isl_val_free(v1);
934 isl_val_free(v2);
935 return NULL;
938 /* Given two integer values v1 and v2, return their greatest common divisor g,
939 * as well as two integers x and y such that x * v1 + y * v2 = g.
941 __isl_give isl_val *isl_val_gcdext(__isl_take isl_val *v1,
942 __isl_take isl_val *v2, __isl_give isl_val **x, __isl_give isl_val **y)
944 isl_ctx *ctx;
945 isl_val *a = NULL, *b = NULL;
947 if (!x && !y)
948 return isl_val_gcd(v1, v2);
950 if (!v1 || !v2)
951 goto error;
953 ctx = isl_val_get_ctx(v1);
954 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
955 isl_die(ctx, isl_error_invalid,
956 "expecting two integers", goto error);
958 v1 = isl_val_cow(v1);
959 a = isl_val_alloc(ctx);
960 b = isl_val_alloc(ctx);
961 if (!v1 || !a || !b)
962 goto error;
963 isl_int_gcdext(v1->n, a->n, b->n, v1->n, v2->n);
964 if (x) {
965 isl_int_set_si(a->d, 1);
966 *x = a;
967 } else
968 isl_val_free(a);
969 if (y) {
970 isl_int_set_si(b->d, 1);
971 *y = b;
972 } else
973 isl_val_free(b);
974 isl_val_free(v2);
975 return v1;
976 error:
977 isl_val_free(v1);
978 isl_val_free(v2);
979 isl_val_free(a);
980 isl_val_free(b);
981 if (x)
982 *x = NULL;
983 if (y)
984 *y = NULL;
985 return NULL;
988 /* Does "v" represent an integer value?
990 int isl_val_is_int(__isl_keep isl_val *v)
992 if (!v)
993 return -1;
995 return isl_int_is_one(v->d);
998 /* Does "v" represent a rational value?
1000 int isl_val_is_rat(__isl_keep isl_val *v)
1002 if (!v)
1003 return -1;
1005 return !isl_int_is_zero(v->d);
1008 /* Does "v" represent NaN?
1010 int isl_val_is_nan(__isl_keep isl_val *v)
1012 if (!v)
1013 return -1;
1015 return isl_int_is_zero(v->n) && isl_int_is_zero(v->d);
1018 /* Does "v" represent +infinity?
1020 int isl_val_is_infty(__isl_keep isl_val *v)
1022 if (!v)
1023 return -1;
1025 return isl_int_is_pos(v->n) && isl_int_is_zero(v->d);
1028 /* Does "v" represent -infinity?
1030 int isl_val_is_neginfty(__isl_keep isl_val *v)
1032 if (!v)
1033 return -1;
1035 return isl_int_is_neg(v->n) && isl_int_is_zero(v->d);
1038 /* Does "v" represent the integer zero?
1040 int isl_val_is_zero(__isl_keep isl_val *v)
1042 if (!v)
1043 return -1;
1045 return isl_int_is_zero(v->n) && !isl_int_is_zero(v->d);
1048 /* Does "v" represent the integer one?
1050 int isl_val_is_one(__isl_keep isl_val *v)
1052 if (!v)
1053 return -1;
1055 return isl_int_eq(v->n, v->d);
1058 /* Does "v" represent the integer negative one?
1060 int isl_val_is_negone(__isl_keep isl_val *v)
1062 if (!v)
1063 return -1;
1065 return isl_int_is_neg(v->n) && isl_int_abs_eq(v->n, v->d);
1068 /* Is "v" (strictly) positive?
1070 int isl_val_is_pos(__isl_keep isl_val *v)
1072 if (!v)
1073 return -1;
1075 return isl_int_is_pos(v->n);
1078 /* Is "v" (strictly) negative?
1080 int isl_val_is_neg(__isl_keep isl_val *v)
1082 if (!v)
1083 return -1;
1085 return isl_int_is_neg(v->n);
1088 /* Is "v" non-negative?
1090 int isl_val_is_nonneg(__isl_keep isl_val *v)
1092 if (!v)
1093 return -1;
1095 if (isl_val_is_nan(v))
1096 return 0;
1098 return isl_int_is_nonneg(v->n);
1101 /* Is "v" non-positive?
1103 int isl_val_is_nonpos(__isl_keep isl_val *v)
1105 if (!v)
1106 return -1;
1108 if (isl_val_is_nan(v))
1109 return 0;
1111 return isl_int_is_nonpos(v->n);
1114 /* Return the sign of "v".
1116 * The sign of NaN is undefined.
1118 int isl_val_sgn(__isl_keep isl_val *v)
1120 if (!v)
1121 return 0;
1122 if (isl_val_is_zero(v))
1123 return 0;
1124 if (isl_val_is_pos(v))
1125 return 1;
1126 return -1;
1129 /* Is "v1" (strictly) less than "v2"?
1131 int isl_val_lt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1133 isl_int t;
1134 int lt;
1136 if (!v1 || !v2)
1137 return -1;
1138 if (isl_val_is_int(v1) && isl_val_is_int(v2))
1139 return isl_int_lt(v1->n, v2->n);
1140 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1141 return 0;
1142 if (isl_val_eq(v1, v2))
1143 return 0;
1144 if (isl_val_is_infty(v2))
1145 return 1;
1146 if (isl_val_is_infty(v1))
1147 return 0;
1148 if (isl_val_is_neginfty(v1))
1149 return 1;
1150 if (isl_val_is_neginfty(v2))
1151 return 0;
1153 isl_int_init(t);
1154 isl_int_mul(t, v1->n, v2->d);
1155 isl_int_submul(t, v2->n, v1->d);
1156 lt = isl_int_is_neg(t);
1157 isl_int_clear(t);
1159 return lt;
1162 /* Is "v1" (strictly) greater than "v2"?
1164 int isl_val_gt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1166 return isl_val_lt(v2, v1);
1169 /* Is "v1" less than or equal to "v2"?
1171 int isl_val_le(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1173 isl_int t;
1174 int le;
1176 if (!v1 || !v2)
1177 return -1;
1178 if (isl_val_is_int(v1) && isl_val_is_int(v2))
1179 return isl_int_le(v1->n, v2->n);
1180 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1181 return 0;
1182 if (isl_val_eq(v1, v2))
1183 return 1;
1184 if (isl_val_is_infty(v2))
1185 return 1;
1186 if (isl_val_is_infty(v1))
1187 return 0;
1188 if (isl_val_is_neginfty(v1))
1189 return 1;
1190 if (isl_val_is_neginfty(v2))
1191 return 0;
1193 isl_int_init(t);
1194 isl_int_mul(t, v1->n, v2->d);
1195 isl_int_submul(t, v2->n, v1->d);
1196 le = isl_int_is_nonpos(t);
1197 isl_int_clear(t);
1199 return le;
1202 /* Is "v1" greater than or equal to "v2"?
1204 int isl_val_ge(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1206 return isl_val_le(v2, v1);
1209 /* How does "v" compare to "i"?
1211 * Return 1 if v is greater, -1 if v is smaller and 0 if v is equal to i.
1213 * If v is NaN (or NULL), then the result is undefined.
1215 int isl_val_cmp_si(__isl_keep isl_val *v, long i)
1217 isl_int t;
1218 int cmp;
1220 if (!v)
1221 return 0;
1222 if (isl_val_is_int(v))
1223 return isl_int_cmp_si(v->n, i);
1224 if (isl_val_is_nan(v))
1225 return 0;
1226 if (isl_val_is_infty(v))
1227 return 1;
1228 if (isl_val_is_neginfty(v))
1229 return -1;
1231 isl_int_init(t);
1232 isl_int_mul_si(t, v->d, i);
1233 isl_int_sub(t, v->n, t);
1234 cmp = isl_int_sgn(t);
1235 isl_int_clear(t);
1237 return cmp;
1240 /* Is "v1" equal to "v2"?
1242 int isl_val_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1244 if (!v1 || !v2)
1245 return -1;
1246 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1247 return 0;
1249 return isl_int_eq(v1->n, v2->n) && isl_int_eq(v1->d, v2->d);
1252 /* Is "v1" different from "v2"?
1254 int isl_val_ne(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1256 if (!v1 || !v2)
1257 return -1;
1258 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1259 return 0;
1261 return isl_int_ne(v1->n, v2->n) || isl_int_ne(v1->d, v2->d);
1264 /* Print a textual representation of "v" onto "p".
1266 __isl_give isl_printer *isl_printer_print_val(__isl_take isl_printer *p,
1267 __isl_keep isl_val *v)
1269 int neg;
1271 if (!p || !v)
1272 return isl_printer_free(p);
1274 neg = isl_int_is_neg(v->n);
1275 if (neg) {
1276 p = isl_printer_print_str(p, "-");
1277 isl_int_neg(v->n, v->n);
1279 if (isl_int_is_zero(v->d)) {
1280 int sgn = isl_int_sgn(v->n);
1281 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1282 sgn == 0 ? "NaN" : "infty");
1283 } else
1284 p = isl_printer_print_isl_int(p, v->n);
1285 if (neg)
1286 isl_int_neg(v->n, v->n);
1287 if (!isl_int_is_zero(v->d) && !isl_int_is_one(v->d)) {
1288 p = isl_printer_print_str(p, "/");
1289 p = isl_printer_print_isl_int(p, v->d);
1292 return p;
1295 /* Is "val1" (obviously) equal to "val2"?
1297 * This is a private copy of isl_val_eq for use in the generic
1298 * isl_multi_*_plain_is_equal instantiated for isl_val.
1300 int isl_val_plain_is_equal(__isl_keep isl_val *val1, __isl_keep isl_val *val2)
1302 return isl_val_eq(val1, val2);
1305 /* Insert "n" dimensions of type "type" at position "first".
1307 * This function is only meant to be used in the generic isl_multi_*
1308 * functions which have to deal with base objects that have an associated
1309 * space. Since an isl_val does not have an associated space, this function
1310 * does not do anything.
1312 __isl_give isl_val *isl_val_insert_dims(__isl_take isl_val *v,
1313 enum isl_dim_type type, unsigned first, unsigned n)
1315 return v;
1318 /* Drop the the "n" first dimensions of type "type" at position "first".
1320 * This function is only meant to be used in the generic isl_multi_*
1321 * functions which have to deal with base objects that have an associated
1322 * space. Since an isl_val does not have an associated space, this function
1323 * does not do anything.
1325 __isl_give isl_val *isl_val_drop_dims(__isl_take isl_val *v,
1326 enum isl_dim_type type, unsigned first, unsigned n)
1328 return v;
1331 /* Change the name of the dimension of type "type" at position "pos" to "s".
1333 * This function is only meant to be used in the generic isl_multi_*
1334 * functions which have to deal with base objects that have an associated
1335 * space. Since an isl_val does not have an associated space, this function
1336 * does not do anything.
1338 __isl_give isl_val *isl_val_set_dim_name(__isl_take isl_val *v,
1339 enum isl_dim_type type, unsigned pos, const char *s)
1341 return v;
1344 /* Return the space of "v".
1346 * This function is only meant to be used in the generic isl_multi_*
1347 * functions which have to deal with base objects that have an associated
1348 * space. The conditions surrounding the call to this function make sure
1349 * that this function will never actually get called. We return a valid
1350 * space anyway, just in case.
1352 __isl_give isl_space *isl_val_get_space(__isl_keep isl_val *v)
1354 if (!v)
1355 return NULL;
1357 return isl_space_params_alloc(isl_val_get_ctx(v), 0);
1360 /* Reset the domain space of "v" to "space".
1362 * This function is only meant to be used in the generic isl_multi_*
1363 * functions which have to deal with base objects that have an associated
1364 * space. Since an isl_val does not have an associated space, this function
1365 * does not do anything, apart from error handling and cleaning up memory.
1367 __isl_give isl_val *isl_val_reset_domain_space(__isl_take isl_val *v,
1368 __isl_take isl_space *space)
1370 if (!space)
1371 return isl_val_free(v);
1372 isl_space_free(space);
1373 return v;
1376 /* Align the parameters of "v" to those of "space".
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 * does not do anything, apart from error handling and cleaning up memory.
1382 * Note that the conditions surrounding the call to this function make sure
1383 * that this function will never actually get called.
1385 __isl_give isl_val *isl_val_align_params(__isl_take isl_val *v,
1386 __isl_take isl_space *space)
1388 if (!space)
1389 return isl_val_free(v);
1390 isl_space_free(space);
1391 return v;
1394 /* Reorder the dimensions of the domain of "v" according
1395 * to the given reordering.
1397 * This function is only meant to be used in the generic isl_multi_*
1398 * functions which have to deal with base objects that have an associated
1399 * space. Since an isl_val does not have an associated space, this function
1400 * does not do anything, apart from error handling and cleaning up memory.
1402 __isl_give isl_val *isl_val_realign_domain(__isl_take isl_val *v,
1403 __isl_take isl_reordering *r)
1405 if (!r)
1406 return isl_val_free(v);
1407 isl_reordering_free(r);
1408 return v;
1411 /* Return an isl_val that is zero on "ls".
1413 * This function is only meant to be used in the generic isl_multi_*
1414 * functions which have to deal with base objects that have an associated
1415 * space. Since an isl_val does not have an associated space, this function
1416 * simply returns a zero isl_val in the same context as "ls".
1418 __isl_give isl_val *isl_val_zero_on_domain(__isl_take isl_local_space *ls)
1420 isl_ctx *ctx;
1422 if (!ls)
1423 return NULL;
1424 ctx = isl_local_space_get_ctx(ls);
1425 isl_local_space_free(ls);
1426 return isl_val_zero(ctx);
1429 /* Do the parameters of "v" match those of "space"?
1431 * This function is only meant to be used in the generic isl_multi_*
1432 * functions which have to deal with base objects that have an associated
1433 * space. Since an isl_val does not have an associated space, this function
1434 * simply returns 1, except if "v" or "space" are NULL.
1436 int isl_val_matching_params(__isl_keep isl_val *v, __isl_keep isl_space *space)
1438 if (!v || !space)
1439 return -1;
1440 return 1;
1443 /* Check that the domain space of "v" matches "space".
1445 * Return 0 on success and -1 on error.
1447 * This function is only meant to be used in the generic isl_multi_*
1448 * functions which have to deal with base objects that have an associated
1449 * space. Since an isl_val does not have an associated space, this function
1450 * simply returns 0, except if "v" or "space" are NULL.
1452 int isl_val_check_match_domain_space(__isl_keep isl_val *v,
1453 __isl_keep isl_space *space)
1455 if (!v || !space)
1456 return -1;
1457 return 0;
1460 #undef BASE
1461 #define BASE val
1463 #define NO_DOMAIN
1464 #define NO_INTERSECT_DOMAIN
1465 #define NO_GIST
1466 #define NO_IDENTITY
1467 #define NO_FROM_BASE
1468 #define NO_MOVE_DIMS
1469 #include <isl_multi_templ.c>
1471 /* Apply "fn" to each of the elements of "mv" with as second argument "v".
1473 static __isl_give isl_multi_val *isl_multi_val_fn_val(
1474 __isl_take isl_multi_val *mv,
1475 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
1476 __isl_take isl_val *v2),
1477 __isl_take isl_val *v)
1479 int i;
1481 mv = isl_multi_val_cow(mv);
1482 if (!mv || !v)
1483 goto error;
1485 for (i = 0; i < mv->n; ++i) {
1486 mv->p[i] = fn(mv->p[i], isl_val_copy(v));
1487 if (!mv->p[i])
1488 goto error;
1491 isl_val_free(v);
1492 return mv;
1493 error:
1494 isl_val_free(v);
1495 isl_multi_val_free(mv);
1496 return NULL;
1499 /* Add "v" to each of the elements of "mv".
1501 __isl_give isl_multi_val *isl_multi_val_add_val(__isl_take isl_multi_val *mv,
1502 __isl_take isl_val *v)
1504 if (!v)
1505 return isl_multi_val_free(mv);
1506 if (isl_val_is_zero(v)) {
1507 isl_val_free(v);
1508 return mv;
1510 return isl_multi_val_fn_val(mv, &isl_val_add, v);
1513 /* Reduce the elements of "mv" modulo "v".
1515 __isl_give isl_multi_val *isl_multi_val_mod_val(__isl_take isl_multi_val *mv,
1516 __isl_take isl_val *v)
1518 return isl_multi_val_fn_val(mv, &isl_val_mod, v);