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
11 #include <isl_ctx_private.h>
12 #include <isl_val_private.h>
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
)
25 v
= isl_alloc_type(ctx
, struct isl_val
);
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
)
58 v
= isl_val_alloc(ctx
);
62 isl_int_set_si(v
->n
, 0);
63 isl_int_set_si(v
->d
, 0);
68 /* Change "v" into a NaN.
70 __isl_give isl_val
*isl_val_set_nan(__isl_take isl_val
*v
)
74 if (isl_val_is_nan(v
))
80 isl_int_set_si(v
->n
, 0);
81 isl_int_set_si(v
->d
, 0);
86 /* Return a reference to an isl_val representing +infinity.
88 __isl_give isl_val
*isl_val_infty(isl_ctx
*ctx
)
92 v
= isl_val_alloc(ctx
);
96 isl_int_set_si(v
->n
, 1);
97 isl_int_set_si(v
->d
, 0);
102 /* Return a reference to an isl_val representing -infinity.
104 __isl_give isl_val
*isl_val_neginfty(isl_ctx
*ctx
)
108 v
= isl_val_alloc(ctx
);
112 isl_int_set_si(v
->n
, -1);
113 isl_int_set_si(v
->d
, 0);
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
)
124 v
= isl_val_alloc(ctx
);
128 isl_int_set_si(v
->n
, i
);
129 isl_int_set_si(v
->d
, 1);
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
)
140 if (isl_val_is_int(v
) && isl_int_cmp_si(v
->n
, i
) == 0)
146 isl_int_set_si(v
->n
, i
);
147 isl_int_set_si(v
->d
, 1);
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
)
165 v
= isl_val_alloc(ctx
);
169 isl_int_set_ui(v
->n
, u
);
170 isl_int_set_si(v
->d
, 1);
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
)
181 v
= isl_val_alloc(ctx
);
185 isl_int_set(v
->n
, n
);
186 isl_int_set_si(v
->d
, 1);
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
)
199 v
= isl_val_alloc(ctx
);
203 isl_int_set(v
->n
, n
);
204 isl_int_set(v
->d
, d
);
209 /* Return a new reference to "v".
211 __isl_give isl_val
*isl_val_copy(__isl_keep isl_val
*v
)
220 /* Return a fresh copy of "val".
222 __isl_give isl_val
*isl_val_dup(__isl_keep isl_val
*val
)
229 dup
= isl_val_alloc(isl_val_get_ctx(val
));
233 isl_int_set(dup
->n
, val
->n
);
234 isl_int_set(dup
->d
, val
->d
);
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
)
250 return isl_val_dup(val
);
253 /* Free "v" and return NULL.
255 void *isl_val_free(__isl_take isl_val
*v
)
263 isl_ctx_deref(v
->ctx
);
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
)
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
)
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
);
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
)
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
)
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
;
340 * In particular, make sure that the denominator of a rational value
341 * is positive and the numerator and denominator do not have any
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
)
353 if (isl_val_is_int(v
))
355 if (!isl_val_is_rat(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
))
365 isl_int_divexact(v
->n
, v
->n
, ctx
->normalize_gcd
);
366 isl_int_divexact(v
->d
, v
->d
, ctx
->normalize_gcd
);
370 /* Return the opposite of "v".
372 __isl_give isl_val
*isl_val_neg(__isl_take isl_val
*v
)
376 if (isl_val_is_nan(v
))
378 if (isl_val_is_zero(v
))
384 isl_int_neg(v
->n
, v
->n
);
389 /* Return the absolute value of "v".
391 __isl_give isl_val
*isl_val_abs(__isl_take isl_val
*v
)
395 if (isl_val_is_nan(v
))
397 if (isl_val_is_nonneg(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
)
409 if (isl_val_is_int(v
))
411 if (!isl_val_is_rat(v
))
417 isl_int_fdiv_q(v
->n
, v
->n
, v
->d
);
418 isl_int_set_si(v
->d
, 1);
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
)
430 if (isl_val_is_int(v
))
432 if (!isl_val_is_rat(v
))
438 isl_int_cdiv_q(v
->n
, v
->n
, v
->d
);
439 isl_int_set_si(v
->d
, 1);
445 * That is, return the result of rounding towards zero.
447 __isl_give isl_val
*isl_val_trunc(__isl_take isl_val
*v
)
451 if (isl_val_is_int(v
))
453 if (!isl_val_is_rat(v
))
459 isl_int_tdiv_q(v
->n
, v
->n
, v
->d
);
460 isl_int_set_si(v
->d
, 1);
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
)
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
);
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
);
487 isl_int_mul_2exp(v
->d
, v
->d
, exp
);
488 isl_int_set_si(v
->n
, 1);
490 isl_int_mul_2exp(v
->n
, v
->d
, exp
);
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
)
503 if (isl_val_is_nan(v1
)) {
507 if (isl_val_is_nan(v2
)) {
511 if (isl_val_le(v1
, v2
)) {
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
)
531 if (isl_val_is_nan(v1
)) {
535 if (isl_val_is_nan(v2
)) {
539 if (isl_val_ge(v1
, v2
)) {
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
)
558 if (isl_val_is_nan(v1
)) {
562 if (isl_val_is_nan(v2
)) {
566 if ((isl_val_is_infty(v1
) && isl_val_is_neginfty(v2
)) ||
567 (isl_val_is_neginfty(v1
) && isl_val_is_infty(v2
))) {
569 return isl_val_set_nan(v1
);
571 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
575 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
579 if (isl_val_is_zero(v1
)) {
583 if (isl_val_is_zero(v2
)) {
588 v1
= isl_val_cow(v1
);
591 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
592 isl_int_add(v1
->n
, v1
->n
, v2
->n
);
594 if (isl_int_eq(v1
->d
, v2
->d
))
595 isl_int_add(v1
->n
, v1
->n
, v2
->n
);
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
);
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
)
617 if (!isl_val_is_rat(v1
))
621 v1
= isl_val_cow(v1
);
625 isl_int_addmul_ui(v1
->n
, v1
->d
, v2
);
630 /* Subtract "v2" from "v1".
632 __isl_give isl_val
*isl_val_sub(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
636 if (isl_val_is_nan(v1
)) {
640 if (isl_val_is_nan(v2
)) {
644 if ((isl_val_is_infty(v1
) && isl_val_is_infty(v2
)) ||
645 (isl_val_is_neginfty(v1
) && isl_val_is_neginfty(v2
))) {
647 return isl_val_set_nan(v1
);
649 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
653 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
655 return isl_val_neg(v2
);
657 if (isl_val_is_zero(v2
)) {
661 if (isl_val_is_zero(v1
)) {
663 return isl_val_neg(v2
);
666 v1
= isl_val_cow(v1
);
669 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
670 isl_int_sub(v1
->n
, v1
->n
, v2
->n
);
672 if (isl_int_eq(v1
->d
, v2
->d
))
673 isl_int_sub(v1
->n
, v1
->n
, v2
->n
);
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
);
689 /* Subtract "v2" from "v1".
691 __isl_give isl_val
*isl_val_sub_ui(__isl_take isl_val
*v1
, unsigned long v2
)
695 if (!isl_val_is_rat(v1
))
699 v1
= isl_val_cow(v1
);
703 isl_int_submul_ui(v1
->n
, v1
->d
, v2
);
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
)
714 if (isl_val_is_nan(v1
)) {
718 if (isl_val_is_nan(v2
)) {
722 if ((!isl_val_is_rat(v1
) && isl_val_is_zero(v2
)) ||
723 (isl_val_is_zero(v1
) && !isl_val_is_rat(v2
))) {
725 return isl_val_set_nan(v1
);
727 if (isl_val_is_zero(v1
)) {
731 if (isl_val_is_zero(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
);
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
);
748 v1
= isl_val_cow(v1
);
751 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
752 isl_int_mul(v1
->n
, v1
->n
, v2
->n
);
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
);
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
)
783 if (isl_val_is_nan(v1
))
785 if (!isl_val_is_rat(v1
)) {
787 v1
= isl_val_set_nan(v1
);
792 v1
= isl_val_cow(v1
);
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
)
807 if (isl_val_is_nan(v1
)) {
811 if (isl_val_is_nan(v2
)) {
815 if (isl_val_is_zero(v2
) ||
816 (!isl_val_is_rat(v1
) && !isl_val_is_rat(v2
))) {
818 return isl_val_set_nan(v1
);
820 if (isl_val_is_zero(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
);
830 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
832 return isl_val_set_zero(v1
);
835 v1
= isl_val_cow(v1
);
838 if (isl_val_is_int(v2
)) {
839 isl_int_mul(v1
->d
, v1
->d
, v2
->n
);
840 v1
= isl_val_normalize(v1
);
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
);
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
)
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"
871 __isl_give isl_val
*isl_val_mod(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
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
)) {
882 v1
= isl_val_cow(v1
);
885 isl_int_fdiv_r(v1
->n
, v1
->n
, v2
->n
);
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
)
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
)) {
907 if (isl_val_is_one(v1
)) {
911 if (isl_val_is_one(v2
)) {
915 v1
= isl_val_cow(v1
);
918 isl_int_gcd(v1
->n
, v1
->n
, v2
->n
);
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
)
934 isl_val
*a
= NULL
, *b
= NULL
;
937 return isl_val_gcd(v1
, v2
);
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
);
952 isl_int_gcdext(v1
->n
, a
->n
, b
->n
, v1
->n
, v2
->n
);
954 isl_int_set_si(a
->d
, 1);
959 isl_int_set_si(b
->d
, 1);
977 /* Does "v" represent an integer value?
979 int isl_val_is_int(__isl_keep isl_val
*v
)
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
)
994 return !isl_int_is_zero(v
->d
);
997 /* Does "v" represent NaN?
999 int isl_val_is_nan(__isl_keep isl_val
*v
)
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
)
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
)
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
)
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
)
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
)
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
)
1064 return isl_int_is_pos(v
->n
);
1067 /* Is "v" (strictly) negative?
1069 int isl_val_is_neg(__isl_keep isl_val
*v
)
1074 return isl_int_is_neg(v
->n
);
1077 /* Is "v" non-negative?
1079 int isl_val_is_nonneg(__isl_keep isl_val
*v
)
1084 if (isl_val_is_nan(v
))
1087 return isl_int_is_nonneg(v
->n
);
1090 /* Is "v" non-positive?
1092 int isl_val_is_nonpos(__isl_keep isl_val
*v
)
1097 if (isl_val_is_nan(v
))
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
)
1111 if (isl_val_is_zero(v
))
1113 if (isl_val_is_pos(v
))
1118 /* Is "v1" (strictly) less than "v2"?
1120 int isl_val_lt(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
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
))
1131 if (isl_val_eq(v1
, v2
))
1133 if (isl_val_is_infty(v2
))
1135 if (isl_val_is_infty(v1
))
1137 if (isl_val_is_neginfty(v1
))
1139 if (isl_val_is_neginfty(v2
))
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
);
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
)
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
))
1171 if (isl_val_eq(v1
, v2
))
1173 if (isl_val_is_infty(v2
))
1175 if (isl_val_is_infty(v1
))
1177 if (isl_val_is_neginfty(v1
))
1179 if (isl_val_is_neginfty(v2
))
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
);
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
)
1211 if (isl_val_is_int(v
))
1212 return isl_int_cmp_si(v
->n
, i
);
1213 if (isl_val_is_nan(v
))
1215 if (isl_val_is_infty(v
))
1217 if (isl_val_is_neginfty(v
))
1221 isl_int_mul_si(t
, v
->d
, i
);
1222 isl_int_sub(t
, v
->n
, t
);
1223 cmp
= isl_int_sgn(t
);
1229 /* Is "v1" equal to "v2"?
1231 int isl_val_eq(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1235 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
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
)
1247 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
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
)
1261 return isl_printer_free(p
);
1263 neg
= isl_int_is_neg(v
->n
);
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");
1273 p
= isl_printer_print_isl_int(p
, v
->n
);
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
);
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
)
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
)
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
)
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
)
1334 return isl_val_free(v
);
1335 isl_space_free(space
);
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
)
1351 return isl_val_free(v
);
1352 isl_reordering_free(r
);
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
)
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
)
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
)
1409 mv
= isl_multi_val_cow(mv
);
1413 for (i
= 0; i
< mv
->n
; ++i
) {
1414 mv
->p
[i
] = fn(mv
->p
[i
], isl_val_copy(v
));
1423 isl_multi_val_free(mv
);
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
)
1433 return isl_multi_val_free(mv
);
1434 if (isl_val_is_zero(v
)) {
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
);