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 __isl_null isl_val
*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 inverse of "v".
398 __isl_give isl_val
*isl_val_inv(__isl_take isl_val
*v
)
402 if (isl_val_is_nan(v
))
404 if (isl_val_is_zero(v
)) {
405 isl_ctx
*ctx
= isl_val_get_ctx(v
);
407 return isl_val_nan(ctx
);
409 if (isl_val_is_infty(v
) || isl_val_is_neginfty(v
)) {
410 isl_ctx
*ctx
= isl_val_get_ctx(v
);
412 return isl_val_zero(ctx
);
418 isl_int_swap(v
->n
, v
->d
);
420 return isl_val_normalize(v
);
423 /* Return the absolute value of "v".
425 __isl_give isl_val
*isl_val_abs(__isl_take isl_val
*v
)
429 if (isl_val_is_nan(v
))
431 if (isl_val_is_nonneg(v
))
433 return isl_val_neg(v
);
436 /* Return the "floor" (greatest integer part) of "v".
437 * That is, return the result of rounding towards -infinity.
439 __isl_give isl_val
*isl_val_floor(__isl_take isl_val
*v
)
443 if (isl_val_is_int(v
))
445 if (!isl_val_is_rat(v
))
451 isl_int_fdiv_q(v
->n
, v
->n
, v
->d
);
452 isl_int_set_si(v
->d
, 1);
457 /* Return the "ceiling" of "v".
458 * That is, return the result of rounding towards +infinity.
460 __isl_give isl_val
*isl_val_ceil(__isl_take isl_val
*v
)
464 if (isl_val_is_int(v
))
466 if (!isl_val_is_rat(v
))
472 isl_int_cdiv_q(v
->n
, v
->n
, v
->d
);
473 isl_int_set_si(v
->d
, 1);
479 * That is, return the result of rounding towards zero.
481 __isl_give isl_val
*isl_val_trunc(__isl_take isl_val
*v
)
485 if (isl_val_is_int(v
))
487 if (!isl_val_is_rat(v
))
493 isl_int_tdiv_q(v
->n
, v
->n
, v
->d
);
494 isl_int_set_si(v
->d
, 1);
499 /* Return 2^v, where v is an integer (that is not too large).
501 __isl_give isl_val
*isl_val_2exp(__isl_take isl_val
*v
)
509 if (!isl_val_is_int(v
))
510 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
511 "can only compute integer powers",
512 return isl_val_free(v
));
513 neg
= isl_val_is_neg(v
);
515 isl_int_neg(v
->n
, v
->n
);
516 if (!isl_int_fits_ulong(v
->n
))
517 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
518 "exponent too large", return isl_val_free(v
));
519 exp
= isl_int_get_ui(v
->n
);
521 isl_int_mul_2exp(v
->d
, v
->d
, exp
);
522 isl_int_set_si(v
->n
, 1);
524 isl_int_mul_2exp(v
->n
, v
->d
, exp
);
530 /* Return the minimum of "v1" and "v2".
532 __isl_give isl_val
*isl_val_min(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
537 if (isl_val_is_nan(v1
)) {
541 if (isl_val_is_nan(v2
)) {
545 if (isl_val_le(v1
, v2
)) {
558 /* Return the maximum of "v1" and "v2".
560 __isl_give isl_val
*isl_val_max(__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_ge(v1
, v2
)) {
586 /* Return the sum of "v1" and "v2".
588 __isl_give isl_val
*isl_val_add(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
592 if (isl_val_is_nan(v1
)) {
596 if (isl_val_is_nan(v2
)) {
600 if ((isl_val_is_infty(v1
) && isl_val_is_neginfty(v2
)) ||
601 (isl_val_is_neginfty(v1
) && isl_val_is_infty(v2
))) {
603 return isl_val_set_nan(v1
);
605 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
609 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
613 if (isl_val_is_zero(v1
)) {
617 if (isl_val_is_zero(v2
)) {
622 v1
= isl_val_cow(v1
);
625 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
626 isl_int_add(v1
->n
, v1
->n
, v2
->n
);
628 if (isl_int_eq(v1
->d
, v2
->d
))
629 isl_int_add(v1
->n
, v1
->n
, v2
->n
);
631 isl_int_mul(v1
->n
, v1
->n
, v2
->d
);
632 isl_int_addmul(v1
->n
, v2
->n
, v1
->d
);
633 isl_int_mul(v1
->d
, v1
->d
, v2
->d
);
635 v1
= isl_val_normalize(v1
);
645 /* Return the sum of "v1" and "v2".
647 __isl_give isl_val
*isl_val_add_ui(__isl_take isl_val
*v1
, unsigned long v2
)
651 if (!isl_val_is_rat(v1
))
655 v1
= isl_val_cow(v1
);
659 isl_int_addmul_ui(v1
->n
, v1
->d
, v2
);
664 /* Subtract "v2" from "v1".
666 __isl_give isl_val
*isl_val_sub(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
670 if (isl_val_is_nan(v1
)) {
674 if (isl_val_is_nan(v2
)) {
678 if ((isl_val_is_infty(v1
) && isl_val_is_infty(v2
)) ||
679 (isl_val_is_neginfty(v1
) && isl_val_is_neginfty(v2
))) {
681 return isl_val_set_nan(v1
);
683 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
687 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
689 return isl_val_neg(v2
);
691 if (isl_val_is_zero(v2
)) {
695 if (isl_val_is_zero(v1
)) {
697 return isl_val_neg(v2
);
700 v1
= isl_val_cow(v1
);
703 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
704 isl_int_sub(v1
->n
, v1
->n
, v2
->n
);
706 if (isl_int_eq(v1
->d
, v2
->d
))
707 isl_int_sub(v1
->n
, v1
->n
, v2
->n
);
709 isl_int_mul(v1
->n
, v1
->n
, v2
->d
);
710 isl_int_submul(v1
->n
, v2
->n
, v1
->d
);
711 isl_int_mul(v1
->d
, v1
->d
, v2
->d
);
713 v1
= isl_val_normalize(v1
);
723 /* Subtract "v2" from "v1".
725 __isl_give isl_val
*isl_val_sub_ui(__isl_take isl_val
*v1
, unsigned long v2
)
729 if (!isl_val_is_rat(v1
))
733 v1
= isl_val_cow(v1
);
737 isl_int_submul_ui(v1
->n
, v1
->d
, v2
);
742 /* Return the product of "v1" and "v2".
744 __isl_give isl_val
*isl_val_mul(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
748 if (isl_val_is_nan(v1
)) {
752 if (isl_val_is_nan(v2
)) {
756 if ((!isl_val_is_rat(v1
) && isl_val_is_zero(v2
)) ||
757 (isl_val_is_zero(v1
) && !isl_val_is_rat(v2
))) {
759 return isl_val_set_nan(v1
);
761 if (isl_val_is_zero(v1
)) {
765 if (isl_val_is_zero(v2
)) {
769 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
770 if (isl_val_is_neg(v2
))
771 v1
= isl_val_neg(v1
);
775 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
776 if (isl_val_is_neg(v1
))
777 v2
= isl_val_neg(v2
);
782 v1
= isl_val_cow(v1
);
785 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
786 isl_int_mul(v1
->n
, v1
->n
, v2
->n
);
788 isl_int_mul(v1
->n
, v1
->n
, v2
->n
);
789 isl_int_mul(v1
->d
, v1
->d
, v2
->d
);
790 v1
= isl_val_normalize(v1
);
800 /* Return the product of "v1" and "v2".
802 * This is a private copy of isl_val_mul for use in the generic
803 * isl_multi_*_scale_val instantiated for isl_val.
805 __isl_give isl_val
*isl_val_scale_val(__isl_take isl_val
*v1
,
806 __isl_take isl_val
*v2
)
808 return isl_val_mul(v1
, v2
);
811 /* Return the product of "v1" and "v2".
813 __isl_give isl_val
*isl_val_mul_ui(__isl_take isl_val
*v1
, unsigned long v2
)
817 if (isl_val_is_nan(v1
))
819 if (!isl_val_is_rat(v1
)) {
821 v1
= isl_val_set_nan(v1
);
826 v1
= isl_val_cow(v1
);
830 isl_int_mul_ui(v1
->n
, v1
->n
, v2
);
832 return isl_val_normalize(v1
);
835 /* Divide "v1" by "v2".
837 __isl_give isl_val
*isl_val_div(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
841 if (isl_val_is_nan(v1
)) {
845 if (isl_val_is_nan(v2
)) {
849 if (isl_val_is_zero(v2
) ||
850 (!isl_val_is_rat(v1
) && !isl_val_is_rat(v2
))) {
852 return isl_val_set_nan(v1
);
854 if (isl_val_is_zero(v1
)) {
858 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
859 if (isl_val_is_neg(v2
))
860 v1
= isl_val_neg(v1
);
864 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
866 return isl_val_set_zero(v1
);
869 v1
= isl_val_cow(v1
);
872 if (isl_val_is_int(v2
)) {
873 isl_int_mul(v1
->d
, v1
->d
, v2
->n
);
874 v1
= isl_val_normalize(v1
);
876 isl_int_mul(v1
->d
, v1
->d
, v2
->n
);
877 isl_int_mul(v1
->n
, v1
->n
, v2
->d
);
878 v1
= isl_val_normalize(v1
);
888 /* Divide "v1" by "v2".
890 * This is a private copy of isl_val_div for use in the generic
891 * isl_multi_*_scale_down_val instantiated for isl_val.
893 __isl_give isl_val
*isl_val_scale_down_val(__isl_take isl_val
*v1
,
894 __isl_take isl_val
*v2
)
896 return isl_val_div(v1
, v2
);
899 /* Given two integer values "v1" and "v2", check if "v1" is divisible by "v2".
901 int isl_val_is_divisible_by(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
906 if (!isl_val_is_int(v1
) || !isl_val_is_int(v2
))
907 isl_die(isl_val_get_ctx(v1
), isl_error_invalid
,
908 "expecting two integers", return -1);
910 return isl_int_is_divisible_by(v1
->n
, v2
->n
);
913 /* Given two integer values "v1" and "v2", return the residue of "v1"
916 __isl_give isl_val
*isl_val_mod(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
920 if (!isl_val_is_int(v1
) || !isl_val_is_int(v2
))
921 isl_die(isl_val_get_ctx(v1
), isl_error_invalid
,
922 "expecting two integers", goto error
);
923 if (isl_val_is_nonneg(v1
) && isl_val_lt(v1
, v2
)) {
927 v1
= isl_val_cow(v1
);
930 isl_int_fdiv_r(v1
->n
, v1
->n
, v2
->n
);
939 /* Given two integer values, return their greatest common divisor.
941 __isl_give isl_val
*isl_val_gcd(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
945 if (!isl_val_is_int(v1
) || !isl_val_is_int(v2
))
946 isl_die(isl_val_get_ctx(v1
), isl_error_invalid
,
947 "expecting two integers", goto error
);
948 if (isl_val_eq(v1
, v2
)) {
952 if (isl_val_is_one(v1
)) {
956 if (isl_val_is_one(v2
)) {
960 v1
= isl_val_cow(v1
);
963 isl_int_gcd(v1
->n
, v1
->n
, v2
->n
);
972 /* Compute x, y and g such that g = gcd(a,b) and a*x+b*y = g.
974 static void isl_int_gcdext(isl_int g
, isl_int x
, isl_int y
,
975 isl_int a
, isl_int b
)
978 isl_int a_copy
, b_copy
;
980 isl_int_init(a_copy
);
981 isl_int_init(b_copy
);
984 isl_int_set(a_copy
, a
);
985 isl_int_set(b_copy
, b
);
986 isl_int_abs(g
, a_copy
);
987 isl_int_abs(d
, b_copy
);
988 isl_int_set_si(x
, 1);
989 isl_int_set_si(y
, 0);
990 while (isl_int_is_pos(d
)) {
991 isl_int_fdiv_q(tmp
, g
, d
);
992 isl_int_submul(x
, tmp
, y
);
993 isl_int_submul(g
, tmp
, d
);
997 if (isl_int_is_zero(a_copy
))
998 isl_int_set_si(x
, 0);
999 else if (isl_int_is_neg(a_copy
))
1001 if (isl_int_is_zero(b_copy
))
1002 isl_int_set_si(y
, 0);
1004 isl_int_mul(tmp
, a_copy
, x
);
1005 isl_int_sub(tmp
, g
, tmp
);
1006 isl_int_divexact(y
, tmp
, b_copy
);
1010 isl_int_clear(a_copy
);
1011 isl_int_clear(b_copy
);
1014 /* Given two integer values v1 and v2, return their greatest common divisor g,
1015 * as well as two integers x and y such that x * v1 + y * v2 = g.
1017 __isl_give isl_val
*isl_val_gcdext(__isl_take isl_val
*v1
,
1018 __isl_take isl_val
*v2
, __isl_give isl_val
**x
, __isl_give isl_val
**y
)
1021 isl_val
*a
= NULL
, *b
= NULL
;
1024 return isl_val_gcd(v1
, v2
);
1029 ctx
= isl_val_get_ctx(v1
);
1030 if (!isl_val_is_int(v1
) || !isl_val_is_int(v2
))
1031 isl_die(ctx
, isl_error_invalid
,
1032 "expecting two integers", goto error
);
1034 v1
= isl_val_cow(v1
);
1035 a
= isl_val_alloc(ctx
);
1036 b
= isl_val_alloc(ctx
);
1037 if (!v1
|| !a
|| !b
)
1039 isl_int_gcdext(v1
->n
, a
->n
, b
->n
, v1
->n
, v2
->n
);
1041 isl_int_set_si(a
->d
, 1);
1046 isl_int_set_si(b
->d
, 1);
1064 /* Does "v" represent an integer value?
1066 int isl_val_is_int(__isl_keep isl_val
*v
)
1071 return isl_int_is_one(v
->d
);
1074 /* Does "v" represent a rational value?
1076 int isl_val_is_rat(__isl_keep isl_val
*v
)
1081 return !isl_int_is_zero(v
->d
);
1084 /* Does "v" represent NaN?
1086 int isl_val_is_nan(__isl_keep isl_val
*v
)
1091 return isl_int_is_zero(v
->n
) && isl_int_is_zero(v
->d
);
1094 /* Does "v" represent +infinity?
1096 int isl_val_is_infty(__isl_keep isl_val
*v
)
1101 return isl_int_is_pos(v
->n
) && isl_int_is_zero(v
->d
);
1104 /* Does "v" represent -infinity?
1106 int isl_val_is_neginfty(__isl_keep isl_val
*v
)
1111 return isl_int_is_neg(v
->n
) && isl_int_is_zero(v
->d
);
1114 /* Does "v" represent the integer zero?
1116 int isl_val_is_zero(__isl_keep isl_val
*v
)
1121 return isl_int_is_zero(v
->n
) && !isl_int_is_zero(v
->d
);
1124 /* Does "v" represent the integer one?
1126 int isl_val_is_one(__isl_keep isl_val
*v
)
1131 return isl_int_eq(v
->n
, v
->d
);
1134 /* Does "v" represent the integer negative one?
1136 int isl_val_is_negone(__isl_keep isl_val
*v
)
1141 return isl_int_is_neg(v
->n
) && isl_int_abs_eq(v
->n
, v
->d
);
1144 /* Is "v" (strictly) positive?
1146 int isl_val_is_pos(__isl_keep isl_val
*v
)
1151 return isl_int_is_pos(v
->n
);
1154 /* Is "v" (strictly) negative?
1156 int isl_val_is_neg(__isl_keep isl_val
*v
)
1161 return isl_int_is_neg(v
->n
);
1164 /* Is "v" non-negative?
1166 int isl_val_is_nonneg(__isl_keep isl_val
*v
)
1171 if (isl_val_is_nan(v
))
1174 return isl_int_is_nonneg(v
->n
);
1177 /* Is "v" non-positive?
1179 int isl_val_is_nonpos(__isl_keep isl_val
*v
)
1184 if (isl_val_is_nan(v
))
1187 return isl_int_is_nonpos(v
->n
);
1190 /* Return the sign of "v".
1192 * The sign of NaN is undefined.
1194 int isl_val_sgn(__isl_keep isl_val
*v
)
1198 if (isl_val_is_zero(v
))
1200 if (isl_val_is_pos(v
))
1205 /* Is "v1" (strictly) less than "v2"?
1207 int isl_val_lt(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1214 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
1215 return isl_int_lt(v1
->n
, v2
->n
);
1216 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1218 if (isl_val_eq(v1
, v2
))
1220 if (isl_val_is_infty(v2
))
1222 if (isl_val_is_infty(v1
))
1224 if (isl_val_is_neginfty(v1
))
1226 if (isl_val_is_neginfty(v2
))
1230 isl_int_mul(t
, v1
->n
, v2
->d
);
1231 isl_int_submul(t
, v2
->n
, v1
->d
);
1232 lt
= isl_int_is_neg(t
);
1238 /* Is "v1" (strictly) greater than "v2"?
1240 int isl_val_gt(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1242 return isl_val_lt(v2
, v1
);
1245 /* Is "v1" less than or equal to "v2"?
1247 int isl_val_le(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1254 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
1255 return isl_int_le(v1
->n
, v2
->n
);
1256 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1258 if (isl_val_eq(v1
, v2
))
1260 if (isl_val_is_infty(v2
))
1262 if (isl_val_is_infty(v1
))
1264 if (isl_val_is_neginfty(v1
))
1266 if (isl_val_is_neginfty(v2
))
1270 isl_int_mul(t
, v1
->n
, v2
->d
);
1271 isl_int_submul(t
, v2
->n
, v1
->d
);
1272 le
= isl_int_is_nonpos(t
);
1278 /* Is "v1" greater than or equal to "v2"?
1280 int isl_val_ge(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1282 return isl_val_le(v2
, v1
);
1285 /* How does "v" compare to "i"?
1287 * Return 1 if v is greater, -1 if v is smaller and 0 if v is equal to i.
1289 * If v is NaN (or NULL), then the result is undefined.
1291 int isl_val_cmp_si(__isl_keep isl_val
*v
, long i
)
1298 if (isl_val_is_int(v
))
1299 return isl_int_cmp_si(v
->n
, i
);
1300 if (isl_val_is_nan(v
))
1302 if (isl_val_is_infty(v
))
1304 if (isl_val_is_neginfty(v
))
1308 isl_int_mul_si(t
, v
->d
, i
);
1309 isl_int_sub(t
, v
->n
, t
);
1310 cmp
= isl_int_sgn(t
);
1316 /* Is "v1" equal to "v2"?
1318 int isl_val_eq(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1322 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1325 return isl_int_eq(v1
->n
, v2
->n
) && isl_int_eq(v1
->d
, v2
->d
);
1328 /* Is "v1" equal to "v2" in absolute value?
1330 int isl_val_abs_eq(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1334 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1337 return isl_int_abs_eq(v1
->n
, v2
->n
) && isl_int_eq(v1
->d
, v2
->d
);
1340 /* Is "v1" different from "v2"?
1342 int isl_val_ne(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1346 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1349 return isl_int_ne(v1
->n
, v2
->n
) || isl_int_ne(v1
->d
, v2
->d
);
1352 /* Print a textual representation of "v" onto "p".
1354 __isl_give isl_printer
*isl_printer_print_val(__isl_take isl_printer
*p
,
1355 __isl_keep isl_val
*v
)
1360 return isl_printer_free(p
);
1362 neg
= isl_int_is_neg(v
->n
);
1364 p
= isl_printer_print_str(p
, "-");
1365 isl_int_neg(v
->n
, v
->n
);
1367 if (isl_int_is_zero(v
->d
)) {
1368 int sgn
= isl_int_sgn(v
->n
);
1369 p
= isl_printer_print_str(p
, sgn
< 0 ? "-infty" :
1370 sgn
== 0 ? "NaN" : "infty");
1372 p
= isl_printer_print_isl_int(p
, v
->n
);
1374 isl_int_neg(v
->n
, v
->n
);
1375 if (!isl_int_is_zero(v
->d
) && !isl_int_is_one(v
->d
)) {
1376 p
= isl_printer_print_str(p
, "/");
1377 p
= isl_printer_print_isl_int(p
, v
->d
);
1383 /* Is "val1" (obviously) equal to "val2"?
1385 * This is a private copy of isl_val_eq for use in the generic
1386 * isl_multi_*_plain_is_equal instantiated for isl_val.
1388 int isl_val_plain_is_equal(__isl_keep isl_val
*val1
, __isl_keep isl_val
*val2
)
1390 return isl_val_eq(val1
, val2
);
1393 /* Does "v" have any non-zero coefficients
1394 * for any dimension in the given range?
1396 * This function is only meant to be used in the generic isl_multi_*
1397 * functions which have to deal with base objects that have an associated
1398 * space. Since an isl_val does not have any coefficients, this function
1401 int isl_val_involves_dims(__isl_keep isl_val
*v
, enum isl_dim_type type
,
1402 unsigned first
, unsigned n
)
1410 /* Insert "n" dimensions of type "type" at position "first".
1412 * This function is only meant to be used in the generic isl_multi_*
1413 * functions which have to deal with base objects that have an associated
1414 * space. Since an isl_val does not have an associated space, this function
1415 * does not do anything.
1417 __isl_give isl_val
*isl_val_insert_dims(__isl_take isl_val
*v
,
1418 enum isl_dim_type type
, unsigned first
, unsigned n
)
1423 /* Drop the the "n" first dimensions of type "type" at position "first".
1425 * This function is only meant to be used in the generic isl_multi_*
1426 * functions which have to deal with base objects that have an associated
1427 * space. Since an isl_val does not have an associated space, this function
1428 * does not do anything.
1430 __isl_give isl_val
*isl_val_drop_dims(__isl_take isl_val
*v
,
1431 enum isl_dim_type type
, unsigned first
, unsigned n
)
1436 /* Change the name of the dimension of type "type" at position "pos" to "s".
1438 * This function is only meant to be used in the generic isl_multi_*
1439 * functions which have to deal with base objects that have an associated
1440 * space. Since an isl_val does not have an associated space, this function
1441 * does not do anything.
1443 __isl_give isl_val
*isl_val_set_dim_name(__isl_take isl_val
*v
,
1444 enum isl_dim_type type
, unsigned pos
, const char *s
)
1449 /* Return the space of "v".
1451 * This function is only meant to be used in the generic isl_multi_*
1452 * functions which have to deal with base objects that have an associated
1453 * space. The conditions surrounding the call to this function make sure
1454 * that this function will never actually get called. We return a valid
1455 * space anyway, just in case.
1457 __isl_give isl_space
*isl_val_get_space(__isl_keep isl_val
*v
)
1462 return isl_space_params_alloc(isl_val_get_ctx(v
), 0);
1465 /* Reset the domain space of "v" to "space".
1467 * This function is only meant to be used in the generic isl_multi_*
1468 * functions which have to deal with base objects that have an associated
1469 * space. Since an isl_val does not have an associated space, this function
1470 * does not do anything, apart from error handling and cleaning up memory.
1472 __isl_give isl_val
*isl_val_reset_domain_space(__isl_take isl_val
*v
,
1473 __isl_take isl_space
*space
)
1476 return isl_val_free(v
);
1477 isl_space_free(space
);
1481 /* Align the parameters of "v" to those of "space".
1483 * This function is only meant to be used in the generic isl_multi_*
1484 * functions which have to deal with base objects that have an associated
1485 * space. Since an isl_val does not have an associated space, this function
1486 * does not do anything, apart from error handling and cleaning up memory.
1487 * Note that the conditions surrounding the call to this function make sure
1488 * that this function will never actually get called.
1490 __isl_give isl_val
*isl_val_align_params(__isl_take isl_val
*v
,
1491 __isl_take isl_space
*space
)
1494 return isl_val_free(v
);
1495 isl_space_free(space
);
1499 /* Reorder the dimensions of the domain of "v" according
1500 * to the given reordering.
1502 * This function is only meant to be used in the generic isl_multi_*
1503 * functions which have to deal with base objects that have an associated
1504 * space. Since an isl_val does not have an associated space, this function
1505 * does not do anything, apart from error handling and cleaning up memory.
1507 __isl_give isl_val
*isl_val_realign_domain(__isl_take isl_val
*v
,
1508 __isl_take isl_reordering
*r
)
1511 return isl_val_free(v
);
1512 isl_reordering_free(r
);
1516 /* Return an isl_val that is zero on "ls".
1518 * This function is only meant to be used in the generic isl_multi_*
1519 * functions which have to deal with base objects that have an associated
1520 * space. Since an isl_val does not have an associated space, this function
1521 * simply returns a zero isl_val in the same context as "ls".
1523 __isl_give isl_val
*isl_val_zero_on_domain(__isl_take isl_local_space
*ls
)
1529 ctx
= isl_local_space_get_ctx(ls
);
1530 isl_local_space_free(ls
);
1531 return isl_val_zero(ctx
);
1534 /* Do the parameters of "v" match those of "space"?
1536 * This function is only meant to be used in the generic isl_multi_*
1537 * functions which have to deal with base objects that have an associated
1538 * space. Since an isl_val does not have an associated space, this function
1539 * simply returns 1, except if "v" or "space" are NULL.
1541 int isl_val_matching_params(__isl_keep isl_val
*v
, __isl_keep isl_space
*space
)
1548 /* Check that the domain space of "v" matches "space".
1550 * Return 0 on success and -1 on error.
1552 * This function is only meant to be used in the generic isl_multi_*
1553 * functions which have to deal with base objects that have an associated
1554 * space. Since an isl_val does not have an associated space, this function
1555 * simply returns 0, except if "v" or "space" are NULL.
1557 int isl_val_check_match_domain_space(__isl_keep isl_val
*v
,
1558 __isl_keep isl_space
*space
)
1569 #define NO_INTERSECT_DOMAIN
1572 #define NO_FROM_BASE
1573 #define NO_MOVE_DIMS
1574 #include <isl_multi_templ.c>
1576 /* Apply "fn" to each of the elements of "mv" with as second argument "v".
1578 static __isl_give isl_multi_val
*isl_multi_val_fn_val(
1579 __isl_take isl_multi_val
*mv
,
1580 __isl_give isl_val
*(*fn
)(__isl_take isl_val
*v1
,
1581 __isl_take isl_val
*v2
),
1582 __isl_take isl_val
*v
)
1586 mv
= isl_multi_val_cow(mv
);
1590 for (i
= 0; i
< mv
->n
; ++i
) {
1591 mv
->p
[i
] = fn(mv
->p
[i
], isl_val_copy(v
));
1600 isl_multi_val_free(mv
);
1604 /* Add "v" to each of the elements of "mv".
1606 __isl_give isl_multi_val
*isl_multi_val_add_val(__isl_take isl_multi_val
*mv
,
1607 __isl_take isl_val
*v
)
1610 return isl_multi_val_free(mv
);
1611 if (isl_val_is_zero(v
)) {
1615 return isl_multi_val_fn_val(mv
, &isl_val_add
, v
);
1618 /* Reduce the elements of "mv" modulo "v".
1620 __isl_give isl_multi_val
*isl_multi_val_mod_val(__isl_take isl_multi_val
*mv
,
1621 __isl_take isl_val
*v
)
1623 return isl_multi_val_fn_val(mv
, &isl_val_mod
, v
);