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 /* 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
)
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"
882 __isl_give isl_val
*isl_val_mod(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
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
)) {
893 v1
= isl_val_cow(v1
);
896 isl_int_fdiv_r(v1
->n
, v1
->n
, v2
->n
);
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
)
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
)) {
918 if (isl_val_is_one(v1
)) {
922 if (isl_val_is_one(v2
)) {
926 v1
= isl_val_cow(v1
);
929 isl_int_gcd(v1
->n
, v1
->n
, v2
->n
);
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
)
945 isl_val
*a
= NULL
, *b
= NULL
;
948 return isl_val_gcd(v1
, v2
);
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
);
963 isl_int_gcdext(v1
->n
, a
->n
, b
->n
, v1
->n
, v2
->n
);
965 isl_int_set_si(a
->d
, 1);
970 isl_int_set_si(b
->d
, 1);
988 /* Does "v" represent an integer value?
990 int isl_val_is_int(__isl_keep isl_val
*v
)
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
)
1005 return !isl_int_is_zero(v
->d
);
1008 /* Does "v" represent NaN?
1010 int isl_val_is_nan(__isl_keep isl_val
*v
)
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
)
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
)
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
)
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
)
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
)
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
)
1075 return isl_int_is_pos(v
->n
);
1078 /* Is "v" (strictly) negative?
1080 int isl_val_is_neg(__isl_keep isl_val
*v
)
1085 return isl_int_is_neg(v
->n
);
1088 /* Is "v" non-negative?
1090 int isl_val_is_nonneg(__isl_keep isl_val
*v
)
1095 if (isl_val_is_nan(v
))
1098 return isl_int_is_nonneg(v
->n
);
1101 /* Is "v" non-positive?
1103 int isl_val_is_nonpos(__isl_keep isl_val
*v
)
1108 if (isl_val_is_nan(v
))
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
)
1122 if (isl_val_is_zero(v
))
1124 if (isl_val_is_pos(v
))
1129 /* Is "v1" (strictly) less than "v2"?
1131 int isl_val_lt(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
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
))
1142 if (isl_val_eq(v1
, v2
))
1144 if (isl_val_is_infty(v2
))
1146 if (isl_val_is_infty(v1
))
1148 if (isl_val_is_neginfty(v1
))
1150 if (isl_val_is_neginfty(v2
))
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
);
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
)
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
))
1182 if (isl_val_eq(v1
, v2
))
1184 if (isl_val_is_infty(v2
))
1186 if (isl_val_is_infty(v1
))
1188 if (isl_val_is_neginfty(v1
))
1190 if (isl_val_is_neginfty(v2
))
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
);
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
)
1222 if (isl_val_is_int(v
))
1223 return isl_int_cmp_si(v
->n
, i
);
1224 if (isl_val_is_nan(v
))
1226 if (isl_val_is_infty(v
))
1228 if (isl_val_is_neginfty(v
))
1232 isl_int_mul_si(t
, v
->d
, i
);
1233 isl_int_sub(t
, v
->n
, t
);
1234 cmp
= isl_int_sgn(t
);
1240 /* Is "v1" equal to "v2"?
1242 int isl_val_eq(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1246 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
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
)
1258 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
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
)
1272 return isl_printer_free(p
);
1274 neg
= isl_int_is_neg(v
->n
);
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");
1284 p
= isl_printer_print_isl_int(p
, v
->n
);
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
);
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
)
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
)
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
)
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
)
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
)
1371 return isl_val_free(v
);
1372 isl_space_free(space
);
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
)
1389 return isl_val_free(v
);
1390 isl_space_free(space
);
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
)
1406 return isl_val_free(v
);
1407 isl_reordering_free(r
);
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
)
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
)
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
)
1464 #define NO_INTERSECT_DOMAIN
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
)
1481 mv
= isl_multi_val_cow(mv
);
1485 for (i
= 0; i
< mv
->n
; ++i
) {
1486 mv
->p
[i
] = fn(mv
->p
[i
], isl_val_copy(v
));
1495 isl_multi_val_free(mv
);
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
)
1505 return isl_multi_val_free(mv
);
1506 if (isl_val_is_zero(v
)) {
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
);