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 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
)
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
)
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
;
325 * In particular, make sure that the denominator of a rational value
326 * is positive and the numerator and denominator do not have any
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
)
338 if (isl_val_is_int(v
))
340 if (!isl_val_is_rat(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
))
350 isl_int_divexact(v
->n
, v
->n
, ctx
->normalize_gcd
);
351 isl_int_divexact(v
->d
, v
->d
, ctx
->normalize_gcd
);
355 /* Return the opposite of "v".
357 __isl_give isl_val
*isl_val_neg(__isl_take isl_val
*v
)
361 if (isl_val_is_nan(v
))
363 if (isl_val_is_zero(v
))
369 isl_int_neg(v
->n
, v
->n
);
374 /* Return the absolute value of "v".
376 __isl_give isl_val
*isl_val_abs(__isl_take isl_val
*v
)
380 if (isl_val_is_nan(v
))
382 if (isl_val_is_nonneg(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
)
394 if (isl_val_is_int(v
))
396 if (!isl_val_is_rat(v
))
402 isl_int_fdiv_q(v
->n
, v
->n
, v
->d
);
403 isl_int_set_si(v
->d
, 1);
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
)
415 if (isl_val_is_int(v
))
417 if (!isl_val_is_rat(v
))
423 isl_int_cdiv_q(v
->n
, v
->n
, v
->d
);
424 isl_int_set_si(v
->d
, 1);
430 * That is, return the result of rounding towards zero.
432 __isl_give isl_val
*isl_val_trunc(__isl_take isl_val
*v
)
436 if (isl_val_is_int(v
))
438 if (!isl_val_is_rat(v
))
444 isl_int_tdiv_q(v
->n
, v
->n
, v
->d
);
445 isl_int_set_si(v
->d
, 1);
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
)
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
);
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
);
472 isl_int_mul_2exp(v
->d
, v
->d
, exp
);
473 isl_int_set_si(v
->n
, 1);
475 isl_int_mul_2exp(v
->n
, v
->d
, exp
);
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
)
488 if (isl_val_is_nan(v1
)) {
492 if (isl_val_is_nan(v2
)) {
496 if (isl_val_le(v1
, v2
)) {
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
)
516 if (isl_val_is_nan(v1
)) {
520 if (isl_val_is_nan(v2
)) {
524 if (isl_val_ge(v1
, v2
)) {
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
)
543 if (isl_val_is_nan(v1
)) {
547 if (isl_val_is_nan(v2
)) {
551 if ((isl_val_is_infty(v1
) && isl_val_is_neginfty(v2
)) ||
552 (isl_val_is_neginfty(v1
) && isl_val_is_infty(v2
))) {
554 return isl_val_set_nan(v1
);
556 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
560 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
564 if (isl_val_is_zero(v1
)) {
568 if (isl_val_is_zero(v2
)) {
573 v1
= isl_val_cow(v1
);
576 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
577 isl_int_add(v1
->n
, v1
->n
, v2
->n
);
579 if (isl_int_eq(v1
->d
, v2
->d
))
580 isl_int_add(v1
->n
, v1
->n
, v2
->n
);
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
);
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
)
602 if (!isl_val_is_rat(v1
))
606 v1
= isl_val_cow(v1
);
610 isl_int_addmul_ui(v1
->n
, v1
->d
, v2
);
615 /* Subtract "v2" from "v1".
617 __isl_give isl_val
*isl_val_sub(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
621 if (isl_val_is_nan(v1
)) {
625 if (isl_val_is_nan(v2
)) {
629 if ((isl_val_is_infty(v1
) && isl_val_is_infty(v2
)) ||
630 (isl_val_is_neginfty(v1
) && isl_val_is_neginfty(v2
))) {
632 return isl_val_set_nan(v1
);
634 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
638 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
640 return isl_val_neg(v2
);
642 if (isl_val_is_zero(v2
)) {
646 if (isl_val_is_zero(v1
)) {
648 return isl_val_neg(v2
);
651 v1
= isl_val_cow(v1
);
654 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
655 isl_int_sub(v1
->n
, v1
->n
, v2
->n
);
657 if (isl_int_eq(v1
->d
, v2
->d
))
658 isl_int_sub(v1
->n
, v1
->n
, v2
->n
);
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
);
674 /* Subtract "v2" from "v1".
676 __isl_give isl_val
*isl_val_sub_ui(__isl_take isl_val
*v1
, unsigned long v2
)
680 if (!isl_val_is_rat(v1
))
684 v1
= isl_val_cow(v1
);
688 isl_int_submul_ui(v1
->n
, v1
->d
, v2
);
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
)
699 if (isl_val_is_nan(v1
)) {
703 if (isl_val_is_nan(v2
)) {
707 if ((!isl_val_is_rat(v1
) && isl_val_is_zero(v2
)) ||
708 (isl_val_is_zero(v1
) && !isl_val_is_rat(v2
))) {
710 return isl_val_set_nan(v1
);
712 if (isl_val_is_zero(v1
)) {
716 if (isl_val_is_zero(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
);
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
);
733 v1
= isl_val_cow(v1
);
736 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
737 isl_int_mul(v1
->n
, v1
->n
, v2
->n
);
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
);
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
)
768 if (isl_val_is_nan(v1
))
770 if (!isl_val_is_rat(v1
)) {
772 v1
= isl_val_set_nan(v1
);
777 v1
= isl_val_cow(v1
);
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
)
792 if (isl_val_is_nan(v1
)) {
796 if (isl_val_is_nan(v2
)) {
800 if (isl_val_is_zero(v2
) ||
801 (!isl_val_is_rat(v1
) && !isl_val_is_rat(v2
))) {
803 return isl_val_set_nan(v1
);
805 if (isl_val_is_zero(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
);
815 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
817 return isl_val_set_zero(v1
);
820 v1
= isl_val_cow(v1
);
823 if (isl_val_is_int(v2
)) {
824 isl_int_mul(v1
->d
, v1
->d
, v2
->n
);
825 v1
= isl_val_normalize(v1
);
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
);
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
)
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"
856 __isl_give isl_val
*isl_val_mod(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
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
)) {
867 v1
= isl_val_cow(v1
);
870 isl_int_fdiv_r(v1
->n
, v1
->n
, v2
->n
);
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
)
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
)) {
892 if (isl_val_is_one(v1
)) {
896 if (isl_val_is_one(v2
)) {
900 v1
= isl_val_cow(v1
);
903 isl_int_gcd(v1
->n
, v1
->n
, v2
->n
);
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
)
919 isl_val
*a
= NULL
, *b
= NULL
;
922 return isl_val_gcd(v1
, v2
);
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
);
937 isl_int_gcdext(v1
->n
, a
->n
, b
->n
, v1
->n
, v2
->n
);
939 isl_int_set_si(a
->d
, 1);
944 isl_int_set_si(b
->d
, 1);
962 /* Does "v" represent an integer value?
964 int isl_val_is_int(__isl_keep isl_val
*v
)
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
)
979 return !isl_int_is_zero(v
->d
);
982 /* Does "v" represent NaN?
984 int isl_val_is_nan(__isl_keep isl_val
*v
)
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
)
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
)
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
)
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
)
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
)
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
)
1049 return isl_int_is_pos(v
->n
);
1052 /* Is "v" (strictly) negative?
1054 int isl_val_is_neg(__isl_keep isl_val
*v
)
1059 return isl_int_is_neg(v
->n
);
1062 /* Is "v" non-negative?
1064 int isl_val_is_nonneg(__isl_keep isl_val
*v
)
1069 if (isl_val_is_nan(v
))
1072 return isl_int_is_nonneg(v
->n
);
1075 /* Is "v" non-positive?
1077 int isl_val_is_nonpos(__isl_keep isl_val
*v
)
1082 if (isl_val_is_nan(v
))
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
)
1096 if (isl_val_is_zero(v
))
1098 if (isl_val_is_pos(v
))
1103 /* Is "v1" (strictly) less than "v2"?
1105 int isl_val_lt(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
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
))
1116 if (isl_val_eq(v1
, v2
))
1118 if (isl_val_is_infty(v2
))
1120 if (isl_val_is_infty(v1
))
1122 if (isl_val_is_neginfty(v1
))
1124 if (isl_val_is_neginfty(v2
))
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
);
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
)
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
))
1156 if (isl_val_eq(v1
, v2
))
1158 if (isl_val_is_infty(v2
))
1160 if (isl_val_is_infty(v1
))
1162 if (isl_val_is_neginfty(v1
))
1164 if (isl_val_is_neginfty(v2
))
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
);
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
)
1196 if (isl_val_is_int(v
))
1197 return isl_int_cmp_si(v
->n
, i
);
1198 if (isl_val_is_nan(v
))
1200 if (isl_val_is_infty(v
))
1202 if (isl_val_is_neginfty(v
))
1206 isl_int_mul_si(t
, v
->d
, i
);
1207 isl_int_sub(t
, v
->n
, t
);
1208 cmp
= isl_int_sgn(t
);
1214 /* Is "v1" equal to "v2"?
1216 int isl_val_eq(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1220 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
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
)
1232 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
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
)
1246 return isl_printer_free(p
);
1248 neg
= isl_int_is_neg(v
->n
);
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");
1258 p
= isl_printer_print_isl_int(p
, v
->n
);
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
);
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
)
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
)
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
)
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
)
1319 return isl_val_free(v
);
1320 isl_space_free(space
);
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
)
1336 return isl_val_free(v
);
1337 isl_reordering_free(r
);
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
)
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
)
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
)
1394 mv
= isl_multi_val_cow(mv
);
1398 for (i
= 0; i
< mv
->n
; ++i
) {
1399 mv
->p
[i
] = fn(mv
->p
[i
], isl_val_copy(v
));
1408 isl_multi_val_free(mv
);
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
)
1418 return isl_multi_val_free(mv
);
1419 if (isl_val_is_zero(v
)) {
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
);