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 negative one.
54 __isl_give isl_val
*isl_val_negone(isl_ctx
*ctx
)
56 return isl_val_int_from_si(ctx
, -1);
59 /* Return a reference to an isl_val representing NaN.
61 __isl_give isl_val
*isl_val_nan(isl_ctx
*ctx
)
65 v
= isl_val_alloc(ctx
);
69 isl_int_set_si(v
->n
, 0);
70 isl_int_set_si(v
->d
, 0);
75 /* Change "v" into a NaN.
77 __isl_give isl_val
*isl_val_set_nan(__isl_take isl_val
*v
)
81 if (isl_val_is_nan(v
))
87 isl_int_set_si(v
->n
, 0);
88 isl_int_set_si(v
->d
, 0);
93 /* Return a reference to an isl_val representing +infinity.
95 __isl_give isl_val
*isl_val_infty(isl_ctx
*ctx
)
99 v
= isl_val_alloc(ctx
);
103 isl_int_set_si(v
->n
, 1);
104 isl_int_set_si(v
->d
, 0);
109 /* Return a reference to an isl_val representing -infinity.
111 __isl_give isl_val
*isl_val_neginfty(isl_ctx
*ctx
)
115 v
= isl_val_alloc(ctx
);
119 isl_int_set_si(v
->n
, -1);
120 isl_int_set_si(v
->d
, 0);
125 /* Return a reference to an isl_val representing the integer "i".
127 __isl_give isl_val
*isl_val_int_from_si(isl_ctx
*ctx
, long i
)
131 v
= isl_val_alloc(ctx
);
135 isl_int_set_si(v
->n
, i
);
136 isl_int_set_si(v
->d
, 1);
141 /* Change the value of "v" to be equal to the integer "i".
143 __isl_give isl_val
*isl_val_set_si(__isl_take isl_val
*v
, long i
)
147 if (isl_val_is_int(v
) && isl_int_cmp_si(v
->n
, i
) == 0)
153 isl_int_set_si(v
->n
, i
);
154 isl_int_set_si(v
->d
, 1);
159 /* Change the value of "v" to be equal to zero.
161 __isl_give isl_val
*isl_val_set_zero(__isl_take isl_val
*v
)
163 return isl_val_set_si(v
, 0);
166 /* Return a reference to an isl_val representing the unsigned integer "u".
168 __isl_give isl_val
*isl_val_int_from_ui(isl_ctx
*ctx
, unsigned long u
)
172 v
= isl_val_alloc(ctx
);
176 isl_int_set_ui(v
->n
, u
);
177 isl_int_set_si(v
->d
, 1);
182 /* Return a reference to an isl_val representing the integer "n".
184 __isl_give isl_val
*isl_val_int_from_isl_int(isl_ctx
*ctx
, isl_int n
)
188 v
= isl_val_alloc(ctx
);
192 isl_int_set(v
->n
, n
);
193 isl_int_set_si(v
->d
, 1);
198 /* Return a reference to an isl_val representing the rational value "n"/"d".
199 * Normalizing the isl_val (if needed) is left to the caller.
201 __isl_give isl_val
*isl_val_rat_from_isl_int(isl_ctx
*ctx
,
202 isl_int n
, isl_int d
)
206 v
= isl_val_alloc(ctx
);
210 isl_int_set(v
->n
, n
);
211 isl_int_set(v
->d
, d
);
216 /* Return a new reference to "v".
218 __isl_give isl_val
*isl_val_copy(__isl_keep isl_val
*v
)
227 /* Return a fresh copy of "val".
229 __isl_give isl_val
*isl_val_dup(__isl_keep isl_val
*val
)
236 dup
= isl_val_alloc(isl_val_get_ctx(val
));
240 isl_int_set(dup
->n
, val
->n
);
241 isl_int_set(dup
->d
, val
->d
);
246 /* Return an isl_val that is equal to "val" and that has only
247 * a single reference.
249 __isl_give isl_val
*isl_val_cow(__isl_take isl_val
*val
)
257 return isl_val_dup(val
);
260 /* Free "v" and return NULL.
262 void *isl_val_free(__isl_take isl_val
*v
)
270 isl_ctx_deref(v
->ctx
);
277 /* Extract the numerator of a rational value "v" as an integer.
279 * If "v" is not a rational value, then the result is undefined.
281 long isl_val_get_num_si(__isl_keep isl_val
*v
)
285 if (!isl_val_is_rat(v
))
286 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
287 "expecting rational value", return 0);
288 if (!isl_int_fits_slong(v
->n
))
289 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
290 "numerator too large", return 0);
291 return isl_int_get_si(v
->n
);
294 /* Extract the numerator of a rational value "v" as an isl_int.
296 * If "v" is not a rational value, then the result is undefined.
298 int isl_val_get_num_isl_int(__isl_keep isl_val
*v
, isl_int
*n
)
302 if (!isl_val_is_rat(v
))
303 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
304 "expecting rational value", return -1);
305 isl_int_set(*n
, v
->n
);
309 /* Extract the denominator of a rational value "v" as an integer.
311 * If "v" is not a rational value, then the result is undefined.
313 long isl_val_get_den_si(__isl_keep isl_val
*v
)
317 if (!isl_val_is_rat(v
))
318 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
319 "expecting rational value", return 0);
320 if (!isl_int_fits_slong(v
->d
))
321 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
322 "denominator too large", return 0);
323 return isl_int_get_si(v
->d
);
326 /* Return an approximation of "v" as a double.
328 double isl_val_get_d(__isl_keep isl_val
*v
)
332 if (!isl_val_is_rat(v
))
333 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
334 "expecting rational value", return 0);
335 return isl_int_get_d(v
->n
) / isl_int_get_d(v
->d
);
338 /* Return the isl_ctx to which "val" belongs.
340 isl_ctx
*isl_val_get_ctx(__isl_keep isl_val
*val
)
342 return val
? val
->ctx
: NULL
;
347 * In particular, make sure that the denominator of a rational value
348 * is positive and the numerator and denominator do not have any
351 * This function should not be called by an external user
352 * since it will only be given normalized values.
354 __isl_give isl_val
*isl_val_normalize(__isl_take isl_val
*v
)
360 if (isl_val_is_int(v
))
362 if (!isl_val_is_rat(v
))
364 if (isl_int_is_neg(v
->d
)) {
365 isl_int_neg(v
->d
, v
->d
);
366 isl_int_neg(v
->n
, v
->n
);
368 ctx
= isl_val_get_ctx(v
);
369 isl_int_gcd(ctx
->normalize_gcd
, v
->n
, v
->d
);
370 if (isl_int_is_one(ctx
->normalize_gcd
))
372 isl_int_divexact(v
->n
, v
->n
, ctx
->normalize_gcd
);
373 isl_int_divexact(v
->d
, v
->d
, ctx
->normalize_gcd
);
377 /* Return the opposite of "v".
379 __isl_give isl_val
*isl_val_neg(__isl_take isl_val
*v
)
383 if (isl_val_is_nan(v
))
385 if (isl_val_is_zero(v
))
391 isl_int_neg(v
->n
, v
->n
);
396 /* Return the absolute value of "v".
398 __isl_give isl_val
*isl_val_abs(__isl_take isl_val
*v
)
402 if (isl_val_is_nan(v
))
404 if (isl_val_is_nonneg(v
))
406 return isl_val_neg(v
);
409 /* Return the "floor" (greatest integer part) of "v".
410 * That is, return the result of rounding towards -infinity.
412 __isl_give isl_val
*isl_val_floor(__isl_take isl_val
*v
)
416 if (isl_val_is_int(v
))
418 if (!isl_val_is_rat(v
))
424 isl_int_fdiv_q(v
->n
, v
->n
, v
->d
);
425 isl_int_set_si(v
->d
, 1);
430 /* Return the "ceiling" of "v".
431 * That is, return the result of rounding towards +infinity.
433 __isl_give isl_val
*isl_val_ceil(__isl_take isl_val
*v
)
437 if (isl_val_is_int(v
))
439 if (!isl_val_is_rat(v
))
445 isl_int_cdiv_q(v
->n
, v
->n
, v
->d
);
446 isl_int_set_si(v
->d
, 1);
452 * That is, return the result of rounding towards zero.
454 __isl_give isl_val
*isl_val_trunc(__isl_take isl_val
*v
)
458 if (isl_val_is_int(v
))
460 if (!isl_val_is_rat(v
))
466 isl_int_tdiv_q(v
->n
, v
->n
, v
->d
);
467 isl_int_set_si(v
->d
, 1);
472 /* Return 2^v, where v is an integer (that is not too large).
474 __isl_give isl_val
*isl_val_2exp(__isl_take isl_val
*v
)
482 if (!isl_val_is_int(v
))
483 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
484 "can only compute integer powers",
485 return isl_val_free(v
));
486 neg
= isl_val_is_neg(v
);
488 isl_int_neg(v
->n
, v
->n
);
489 if (!isl_int_fits_ulong(v
->n
))
490 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
491 "exponent too large", return isl_val_free(v
));
492 exp
= isl_int_get_ui(v
->n
);
494 isl_int_mul_2exp(v
->d
, v
->d
, exp
);
495 isl_int_set_si(v
->n
, 1);
497 isl_int_mul_2exp(v
->n
, v
->d
, exp
);
503 /* Return the minimum of "v1" and "v2".
505 __isl_give isl_val
*isl_val_min(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
510 if (isl_val_is_nan(v1
)) {
514 if (isl_val_is_nan(v2
)) {
518 if (isl_val_le(v1
, v2
)) {
531 /* Return the maximum of "v1" and "v2".
533 __isl_give isl_val
*isl_val_max(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
538 if (isl_val_is_nan(v1
)) {
542 if (isl_val_is_nan(v2
)) {
546 if (isl_val_ge(v1
, v2
)) {
559 /* Return the sum of "v1" and "v2".
561 __isl_give isl_val
*isl_val_add(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
565 if (isl_val_is_nan(v1
)) {
569 if (isl_val_is_nan(v2
)) {
573 if ((isl_val_is_infty(v1
) && isl_val_is_neginfty(v2
)) ||
574 (isl_val_is_neginfty(v1
) && isl_val_is_infty(v2
))) {
576 return isl_val_set_nan(v1
);
578 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
582 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
586 if (isl_val_is_zero(v1
)) {
590 if (isl_val_is_zero(v2
)) {
595 v1
= isl_val_cow(v1
);
598 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
599 isl_int_add(v1
->n
, v1
->n
, v2
->n
);
601 if (isl_int_eq(v1
->d
, v2
->d
))
602 isl_int_add(v1
->n
, v1
->n
, v2
->n
);
604 isl_int_mul(v1
->n
, v1
->n
, v2
->d
);
605 isl_int_addmul(v1
->n
, v2
->n
, v1
->d
);
606 isl_int_mul(v1
->d
, v1
->d
, v2
->d
);
608 v1
= isl_val_normalize(v1
);
618 /* Return the sum of "v1" and "v2".
620 __isl_give isl_val
*isl_val_add_ui(__isl_take isl_val
*v1
, unsigned long v2
)
624 if (!isl_val_is_rat(v1
))
628 v1
= isl_val_cow(v1
);
632 isl_int_addmul_ui(v1
->n
, v1
->d
, v2
);
637 /* Subtract "v2" from "v1".
639 __isl_give isl_val
*isl_val_sub(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
643 if (isl_val_is_nan(v1
)) {
647 if (isl_val_is_nan(v2
)) {
651 if ((isl_val_is_infty(v1
) && isl_val_is_infty(v2
)) ||
652 (isl_val_is_neginfty(v1
) && isl_val_is_neginfty(v2
))) {
654 return isl_val_set_nan(v1
);
656 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
660 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
662 return isl_val_neg(v2
);
664 if (isl_val_is_zero(v2
)) {
668 if (isl_val_is_zero(v1
)) {
670 return isl_val_neg(v2
);
673 v1
= isl_val_cow(v1
);
676 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
677 isl_int_sub(v1
->n
, v1
->n
, v2
->n
);
679 if (isl_int_eq(v1
->d
, v2
->d
))
680 isl_int_sub(v1
->n
, v1
->n
, v2
->n
);
682 isl_int_mul(v1
->n
, v1
->n
, v2
->d
);
683 isl_int_submul(v1
->n
, v2
->n
, v1
->d
);
684 isl_int_mul(v1
->d
, v1
->d
, v2
->d
);
686 v1
= isl_val_normalize(v1
);
696 /* Subtract "v2" from "v1".
698 __isl_give isl_val
*isl_val_sub_ui(__isl_take isl_val
*v1
, unsigned long v2
)
702 if (!isl_val_is_rat(v1
))
706 v1
= isl_val_cow(v1
);
710 isl_int_submul_ui(v1
->n
, v1
->d
, v2
);
715 /* Return the product of "v1" and "v2".
717 __isl_give isl_val
*isl_val_mul(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
721 if (isl_val_is_nan(v1
)) {
725 if (isl_val_is_nan(v2
)) {
729 if ((!isl_val_is_rat(v1
) && isl_val_is_zero(v2
)) ||
730 (isl_val_is_zero(v1
) && !isl_val_is_rat(v2
))) {
732 return isl_val_set_nan(v1
);
734 if (isl_val_is_zero(v1
)) {
738 if (isl_val_is_zero(v2
)) {
742 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
743 if (isl_val_is_neg(v2
))
744 v1
= isl_val_neg(v1
);
748 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
749 if (isl_val_is_neg(v1
))
750 v2
= isl_val_neg(v2
);
755 v1
= isl_val_cow(v1
);
758 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
759 isl_int_mul(v1
->n
, v1
->n
, v2
->n
);
761 isl_int_mul(v1
->n
, v1
->n
, v2
->n
);
762 isl_int_mul(v1
->d
, v1
->d
, v2
->d
);
763 v1
= isl_val_normalize(v1
);
773 /* Return the product of "v1" and "v2".
775 * This is a private copy of isl_val_mul for use in the generic
776 * isl_multi_*_scale_val instantiated for isl_val.
778 __isl_give isl_val
*isl_val_scale_val(__isl_take isl_val
*v1
,
779 __isl_take isl_val
*v2
)
781 return isl_val_mul(v1
, v2
);
784 /* Return the product of "v1" and "v2".
786 __isl_give isl_val
*isl_val_mul_ui(__isl_take isl_val
*v1
, unsigned long v2
)
790 if (isl_val_is_nan(v1
))
792 if (!isl_val_is_rat(v1
)) {
794 v1
= isl_val_set_nan(v1
);
799 v1
= isl_val_cow(v1
);
803 isl_int_mul_ui(v1
->n
, v1
->n
, v2
);
805 return isl_val_normalize(v1
);
808 /* Divide "v1" by "v2".
810 __isl_give isl_val
*isl_val_div(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
814 if (isl_val_is_nan(v1
)) {
818 if (isl_val_is_nan(v2
)) {
822 if (isl_val_is_zero(v2
) ||
823 (!isl_val_is_rat(v1
) && !isl_val_is_rat(v2
))) {
825 return isl_val_set_nan(v1
);
827 if (isl_val_is_zero(v1
)) {
831 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
832 if (isl_val_is_neg(v2
))
833 v1
= isl_val_neg(v1
);
837 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
839 return isl_val_set_zero(v1
);
842 v1
= isl_val_cow(v1
);
845 if (isl_val_is_int(v2
)) {
846 isl_int_mul(v1
->d
, v1
->d
, v2
->n
);
847 v1
= isl_val_normalize(v1
);
849 isl_int_mul(v1
->d
, v1
->d
, v2
->n
);
850 isl_int_mul(v1
->n
, v1
->n
, v2
->d
);
851 v1
= isl_val_normalize(v1
);
861 /* Divide "v1" by "v2".
863 * This is a private copy of isl_val_div for use in the generic
864 * isl_multi_*_scale_down_val instantiated for isl_val.
866 __isl_give isl_val
*isl_val_scale_down_val(__isl_take isl_val
*v1
,
867 __isl_take isl_val
*v2
)
869 return isl_val_div(v1
, v2
);
872 /* Given two integer values "v1" and "v2", check if "v1" is divisible by "v2".
874 int isl_val_is_divisible_by(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
879 if (!isl_val_is_int(v1
) || !isl_val_is_int(v2
))
880 isl_die(isl_val_get_ctx(v1
), isl_error_invalid
,
881 "expecting two integers", return -1);
883 return isl_int_is_divisible_by(v1
->n
, v2
->n
);
886 /* Given two integer values "v1" and "v2", return the residue of "v1"
889 __isl_give isl_val
*isl_val_mod(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
893 if (!isl_val_is_int(v1
) || !isl_val_is_int(v2
))
894 isl_die(isl_val_get_ctx(v1
), isl_error_invalid
,
895 "expecting two integers", goto error
);
896 if (isl_val_is_nonneg(v1
) && isl_val_lt(v1
, v2
)) {
900 v1
= isl_val_cow(v1
);
903 isl_int_fdiv_r(v1
->n
, v1
->n
, v2
->n
);
912 /* Given two integer values, return their greatest common divisor.
914 __isl_give isl_val
*isl_val_gcd(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
918 if (!isl_val_is_int(v1
) || !isl_val_is_int(v2
))
919 isl_die(isl_val_get_ctx(v1
), isl_error_invalid
,
920 "expecting two integers", goto error
);
921 if (isl_val_eq(v1
, v2
)) {
925 if (isl_val_is_one(v1
)) {
929 if (isl_val_is_one(v2
)) {
933 v1
= isl_val_cow(v1
);
936 isl_int_gcd(v1
->n
, v1
->n
, v2
->n
);
945 /* Given two integer values v1 and v2, return their greatest common divisor g,
946 * as well as two integers x and y such that x * v1 + y * v2 = g.
948 __isl_give isl_val
*isl_val_gcdext(__isl_take isl_val
*v1
,
949 __isl_take isl_val
*v2
, __isl_give isl_val
**x
, __isl_give isl_val
**y
)
952 isl_val
*a
= NULL
, *b
= NULL
;
955 return isl_val_gcd(v1
, v2
);
960 ctx
= isl_val_get_ctx(v1
);
961 if (!isl_val_is_int(v1
) || !isl_val_is_int(v2
))
962 isl_die(ctx
, isl_error_invalid
,
963 "expecting two integers", goto error
);
965 v1
= isl_val_cow(v1
);
966 a
= isl_val_alloc(ctx
);
967 b
= isl_val_alloc(ctx
);
970 isl_int_gcdext(v1
->n
, a
->n
, b
->n
, v1
->n
, v2
->n
);
972 isl_int_set_si(a
->d
, 1);
977 isl_int_set_si(b
->d
, 1);
995 /* Does "v" represent an integer value?
997 int isl_val_is_int(__isl_keep isl_val
*v
)
1002 return isl_int_is_one(v
->d
);
1005 /* Does "v" represent a rational value?
1007 int isl_val_is_rat(__isl_keep isl_val
*v
)
1012 return !isl_int_is_zero(v
->d
);
1015 /* Does "v" represent NaN?
1017 int isl_val_is_nan(__isl_keep isl_val
*v
)
1022 return isl_int_is_zero(v
->n
) && isl_int_is_zero(v
->d
);
1025 /* Does "v" represent +infinity?
1027 int isl_val_is_infty(__isl_keep isl_val
*v
)
1032 return isl_int_is_pos(v
->n
) && isl_int_is_zero(v
->d
);
1035 /* Does "v" represent -infinity?
1037 int isl_val_is_neginfty(__isl_keep isl_val
*v
)
1042 return isl_int_is_neg(v
->n
) && isl_int_is_zero(v
->d
);
1045 /* Does "v" represent the integer zero?
1047 int isl_val_is_zero(__isl_keep isl_val
*v
)
1052 return isl_int_is_zero(v
->n
) && !isl_int_is_zero(v
->d
);
1055 /* Does "v" represent the integer one?
1057 int isl_val_is_one(__isl_keep isl_val
*v
)
1062 return isl_int_eq(v
->n
, v
->d
);
1065 /* Does "v" represent the integer negative one?
1067 int isl_val_is_negone(__isl_keep isl_val
*v
)
1072 return isl_int_is_neg(v
->n
) && isl_int_abs_eq(v
->n
, v
->d
);
1075 /* Is "v" (strictly) positive?
1077 int isl_val_is_pos(__isl_keep isl_val
*v
)
1082 return isl_int_is_pos(v
->n
);
1085 /* Is "v" (strictly) negative?
1087 int isl_val_is_neg(__isl_keep isl_val
*v
)
1092 return isl_int_is_neg(v
->n
);
1095 /* Is "v" non-negative?
1097 int isl_val_is_nonneg(__isl_keep isl_val
*v
)
1102 if (isl_val_is_nan(v
))
1105 return isl_int_is_nonneg(v
->n
);
1108 /* Is "v" non-positive?
1110 int isl_val_is_nonpos(__isl_keep isl_val
*v
)
1115 if (isl_val_is_nan(v
))
1118 return isl_int_is_nonpos(v
->n
);
1121 /* Return the sign of "v".
1123 * The sign of NaN is undefined.
1125 int isl_val_sgn(__isl_keep isl_val
*v
)
1129 if (isl_val_is_zero(v
))
1131 if (isl_val_is_pos(v
))
1136 /* Is "v1" (strictly) less than "v2"?
1138 int isl_val_lt(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1145 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
1146 return isl_int_lt(v1
->n
, v2
->n
);
1147 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1149 if (isl_val_eq(v1
, v2
))
1151 if (isl_val_is_infty(v2
))
1153 if (isl_val_is_infty(v1
))
1155 if (isl_val_is_neginfty(v1
))
1157 if (isl_val_is_neginfty(v2
))
1161 isl_int_mul(t
, v1
->n
, v2
->d
);
1162 isl_int_submul(t
, v2
->n
, v1
->d
);
1163 lt
= isl_int_is_neg(t
);
1169 /* Is "v1" (strictly) greater than "v2"?
1171 int isl_val_gt(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1173 return isl_val_lt(v2
, v1
);
1176 /* Is "v1" less than or equal to "v2"?
1178 int isl_val_le(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1185 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
1186 return isl_int_le(v1
->n
, v2
->n
);
1187 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1189 if (isl_val_eq(v1
, v2
))
1191 if (isl_val_is_infty(v2
))
1193 if (isl_val_is_infty(v1
))
1195 if (isl_val_is_neginfty(v1
))
1197 if (isl_val_is_neginfty(v2
))
1201 isl_int_mul(t
, v1
->n
, v2
->d
);
1202 isl_int_submul(t
, v2
->n
, v1
->d
);
1203 le
= isl_int_is_nonpos(t
);
1209 /* Is "v1" greater than or equal to "v2"?
1211 int isl_val_ge(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1213 return isl_val_le(v2
, v1
);
1216 /* How does "v" compare to "i"?
1218 * Return 1 if v is greater, -1 if v is smaller and 0 if v is equal to i.
1220 * If v is NaN (or NULL), then the result is undefined.
1222 int isl_val_cmp_si(__isl_keep isl_val
*v
, long i
)
1229 if (isl_val_is_int(v
))
1230 return isl_int_cmp_si(v
->n
, i
);
1231 if (isl_val_is_nan(v
))
1233 if (isl_val_is_infty(v
))
1235 if (isl_val_is_neginfty(v
))
1239 isl_int_mul_si(t
, v
->d
, i
);
1240 isl_int_sub(t
, v
->n
, t
);
1241 cmp
= isl_int_sgn(t
);
1247 /* Is "v1" equal to "v2"?
1249 int isl_val_eq(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1253 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1256 return isl_int_eq(v1
->n
, v2
->n
) && isl_int_eq(v1
->d
, v2
->d
);
1259 /* Is "v1" different from "v2"?
1261 int isl_val_ne(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1265 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1268 return isl_int_ne(v1
->n
, v2
->n
) || isl_int_ne(v1
->d
, v2
->d
);
1271 /* Print a textual representation of "v" onto "p".
1273 __isl_give isl_printer
*isl_printer_print_val(__isl_take isl_printer
*p
,
1274 __isl_keep isl_val
*v
)
1279 return isl_printer_free(p
);
1281 neg
= isl_int_is_neg(v
->n
);
1283 p
= isl_printer_print_str(p
, "-");
1284 isl_int_neg(v
->n
, v
->n
);
1286 if (isl_int_is_zero(v
->d
)) {
1287 int sgn
= isl_int_sgn(v
->n
);
1288 p
= isl_printer_print_str(p
, sgn
< 0 ? "-infty" :
1289 sgn
== 0 ? "NaN" : "infty");
1291 p
= isl_printer_print_isl_int(p
, v
->n
);
1293 isl_int_neg(v
->n
, v
->n
);
1294 if (!isl_int_is_zero(v
->d
) && !isl_int_is_one(v
->d
)) {
1295 p
= isl_printer_print_str(p
, "/");
1296 p
= isl_printer_print_isl_int(p
, v
->d
);
1302 /* Is "val1" (obviously) equal to "val2"?
1304 * This is a private copy of isl_val_eq for use in the generic
1305 * isl_multi_*_plain_is_equal instantiated for isl_val.
1307 int isl_val_plain_is_equal(__isl_keep isl_val
*val1
, __isl_keep isl_val
*val2
)
1309 return isl_val_eq(val1
, val2
);
1312 /* Does "v" have any non-zero coefficients
1313 * for any dimension in the given range?
1315 * This function is only meant to be used in the generic isl_multi_*
1316 * functions which have to deal with base objects that have an associated
1317 * space. Since an isl_val does not have any coefficients, this function
1320 int isl_val_involves_dims(__isl_keep isl_val
*v
, enum isl_dim_type type
,
1321 unsigned first
, unsigned n
)
1329 /* Insert "n" dimensions of type "type" at position "first".
1331 * This function is only meant to be used in the generic isl_multi_*
1332 * functions which have to deal with base objects that have an associated
1333 * space. Since an isl_val does not have an associated space, this function
1334 * does not do anything.
1336 __isl_give isl_val
*isl_val_insert_dims(__isl_take isl_val
*v
,
1337 enum isl_dim_type type
, unsigned first
, unsigned n
)
1342 /* Drop the the "n" first dimensions of type "type" at position "first".
1344 * This function is only meant to be used in the generic isl_multi_*
1345 * functions which have to deal with base objects that have an associated
1346 * space. Since an isl_val does not have an associated space, this function
1347 * does not do anything.
1349 __isl_give isl_val
*isl_val_drop_dims(__isl_take isl_val
*v
,
1350 enum isl_dim_type type
, unsigned first
, unsigned n
)
1355 /* Change the name of the dimension of type "type" at position "pos" to "s".
1357 * This function is only meant to be used in the generic isl_multi_*
1358 * functions which have to deal with base objects that have an associated
1359 * space. Since an isl_val does not have an associated space, this function
1360 * does not do anything.
1362 __isl_give isl_val
*isl_val_set_dim_name(__isl_take isl_val
*v
,
1363 enum isl_dim_type type
, unsigned pos
, const char *s
)
1368 /* Return the space of "v".
1370 * This function is only meant to be used in the generic isl_multi_*
1371 * functions which have to deal with base objects that have an associated
1372 * space. The conditions surrounding the call to this function make sure
1373 * that this function will never actually get called. We return a valid
1374 * space anyway, just in case.
1376 __isl_give isl_space
*isl_val_get_space(__isl_keep isl_val
*v
)
1381 return isl_space_params_alloc(isl_val_get_ctx(v
), 0);
1384 /* Reset the domain space of "v" to "space".
1386 * This function is only meant to be used in the generic isl_multi_*
1387 * functions which have to deal with base objects that have an associated
1388 * space. Since an isl_val does not have an associated space, this function
1389 * does not do anything, apart from error handling and cleaning up memory.
1391 __isl_give isl_val
*isl_val_reset_domain_space(__isl_take isl_val
*v
,
1392 __isl_take isl_space
*space
)
1395 return isl_val_free(v
);
1396 isl_space_free(space
);
1400 /* Align the parameters of "v" to those of "space".
1402 * This function is only meant to be used in the generic isl_multi_*
1403 * functions which have to deal with base objects that have an associated
1404 * space. Since an isl_val does not have an associated space, this function
1405 * does not do anything, apart from error handling and cleaning up memory.
1406 * Note that the conditions surrounding the call to this function make sure
1407 * that this function will never actually get called.
1409 __isl_give isl_val
*isl_val_align_params(__isl_take isl_val
*v
,
1410 __isl_take isl_space
*space
)
1413 return isl_val_free(v
);
1414 isl_space_free(space
);
1418 /* Reorder the dimensions of the domain of "v" according
1419 * to the given reordering.
1421 * This function is only meant to be used in the generic isl_multi_*
1422 * functions which have to deal with base objects that have an associated
1423 * space. Since an isl_val does not have an associated space, this function
1424 * does not do anything, apart from error handling and cleaning up memory.
1426 __isl_give isl_val
*isl_val_realign_domain(__isl_take isl_val
*v
,
1427 __isl_take isl_reordering
*r
)
1430 return isl_val_free(v
);
1431 isl_reordering_free(r
);
1435 /* Return an isl_val that is zero on "ls".
1437 * This function is only meant to be used in the generic isl_multi_*
1438 * functions which have to deal with base objects that have an associated
1439 * space. Since an isl_val does not have an associated space, this function
1440 * simply returns a zero isl_val in the same context as "ls".
1442 __isl_give isl_val
*isl_val_zero_on_domain(__isl_take isl_local_space
*ls
)
1448 ctx
= isl_local_space_get_ctx(ls
);
1449 isl_local_space_free(ls
);
1450 return isl_val_zero(ctx
);
1453 /* Do the parameters of "v" match those of "space"?
1455 * This function is only meant to be used in the generic isl_multi_*
1456 * functions which have to deal with base objects that have an associated
1457 * space. Since an isl_val does not have an associated space, this function
1458 * simply returns 1, except if "v" or "space" are NULL.
1460 int isl_val_matching_params(__isl_keep isl_val
*v
, __isl_keep isl_space
*space
)
1467 /* Check that the domain space of "v" matches "space".
1469 * Return 0 on success and -1 on error.
1471 * This function is only meant to be used in the generic isl_multi_*
1472 * functions which have to deal with base objects that have an associated
1473 * space. Since an isl_val does not have an associated space, this function
1474 * simply returns 0, except if "v" or "space" are NULL.
1476 int isl_val_check_match_domain_space(__isl_keep isl_val
*v
,
1477 __isl_keep isl_space
*space
)
1488 #define NO_INTERSECT_DOMAIN
1491 #define NO_FROM_BASE
1492 #define NO_MOVE_DIMS
1493 #include <isl_multi_templ.c>
1495 /* Apply "fn" to each of the elements of "mv" with as second argument "v".
1497 static __isl_give isl_multi_val
*isl_multi_val_fn_val(
1498 __isl_take isl_multi_val
*mv
,
1499 __isl_give isl_val
*(*fn
)(__isl_take isl_val
*v1
,
1500 __isl_take isl_val
*v2
),
1501 __isl_take isl_val
*v
)
1505 mv
= isl_multi_val_cow(mv
);
1509 for (i
= 0; i
< mv
->n
; ++i
) {
1510 mv
->p
[i
] = fn(mv
->p
[i
], isl_val_copy(v
));
1519 isl_multi_val_free(mv
);
1523 /* Add "v" to each of the elements of "mv".
1525 __isl_give isl_multi_val
*isl_multi_val_add_val(__isl_take isl_multi_val
*mv
,
1526 __isl_take isl_val
*v
)
1529 return isl_multi_val_free(mv
);
1530 if (isl_val_is_zero(v
)) {
1534 return isl_multi_val_fn_val(mv
, &isl_val_add
, v
);
1537 /* Reduce the elements of "mv" modulo "v".
1539 __isl_give isl_multi_val
*isl_multi_val_mod_val(__isl_take isl_multi_val
*mv
,
1540 __isl_take isl_val
*v
)
1542 return isl_multi_val_fn_val(mv
, &isl_val_mod
, v
);