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>
18 #include <isl_list_read_templ.c>
20 /* Allocate an isl_val object with indeterminate value.
22 __isl_give isl_val
*isl_val_alloc(isl_ctx
*ctx
)
26 v
= isl_alloc_type(ctx
, struct isl_val
);
39 /* Return a reference to an isl_val representing zero.
41 __isl_give isl_val
*isl_val_zero(isl_ctx
*ctx
)
43 return isl_val_int_from_si(ctx
, 0);
46 /* Return a reference to an isl_val representing one.
48 __isl_give isl_val
*isl_val_one(isl_ctx
*ctx
)
50 return isl_val_int_from_si(ctx
, 1);
53 /* Return a reference to an isl_val representing negative one.
55 __isl_give isl_val
*isl_val_negone(isl_ctx
*ctx
)
57 return isl_val_int_from_si(ctx
, -1);
60 /* Return a reference to an isl_val representing NaN.
62 __isl_give isl_val
*isl_val_nan(isl_ctx
*ctx
)
66 v
= isl_val_alloc(ctx
);
70 isl_int_set_si(v
->n
, 0);
71 isl_int_set_si(v
->d
, 0);
76 /* Change "v" into a NaN.
78 __isl_give isl_val
*isl_val_set_nan(__isl_take isl_val
*v
)
82 if (isl_val_is_nan(v
))
88 isl_int_set_si(v
->n
, 0);
89 isl_int_set_si(v
->d
, 0);
94 /* Return a reference to an isl_val representing +infinity.
96 __isl_give isl_val
*isl_val_infty(isl_ctx
*ctx
)
100 v
= isl_val_alloc(ctx
);
104 isl_int_set_si(v
->n
, 1);
105 isl_int_set_si(v
->d
, 0);
110 /* Return a reference to an isl_val representing -infinity.
112 __isl_give isl_val
*isl_val_neginfty(isl_ctx
*ctx
)
116 v
= isl_val_alloc(ctx
);
120 isl_int_set_si(v
->n
, -1);
121 isl_int_set_si(v
->d
, 0);
126 /* Return a reference to an isl_val representing the integer "i".
128 __isl_give isl_val
*isl_val_int_from_si(isl_ctx
*ctx
, long i
)
132 v
= isl_val_alloc(ctx
);
136 isl_int_set_si(v
->n
, i
);
137 isl_int_set_si(v
->d
, 1);
142 /* Change the value of "v" to be equal to the integer "i".
144 __isl_give isl_val
*isl_val_set_si(__isl_take isl_val
*v
, long i
)
148 if (isl_val_is_int(v
) && isl_int_cmp_si(v
->n
, i
) == 0)
154 isl_int_set_si(v
->n
, i
);
155 isl_int_set_si(v
->d
, 1);
160 /* Change the value of "v" to be equal to zero.
162 __isl_give isl_val
*isl_val_set_zero(__isl_take isl_val
*v
)
164 return isl_val_set_si(v
, 0);
167 /* Return a reference to an isl_val representing the unsigned integer "u".
169 __isl_give isl_val
*isl_val_int_from_ui(isl_ctx
*ctx
, unsigned long u
)
173 v
= isl_val_alloc(ctx
);
177 isl_int_set_ui(v
->n
, u
);
178 isl_int_set_si(v
->d
, 1);
183 /* Return a reference to an isl_val representing the integer "n".
185 __isl_give isl_val
*isl_val_int_from_isl_int(isl_ctx
*ctx
, isl_int n
)
189 v
= isl_val_alloc(ctx
);
193 isl_int_set(v
->n
, n
);
194 isl_int_set_si(v
->d
, 1);
199 /* Return a reference to an isl_val representing the rational value "n"/"d".
200 * Normalizing the isl_val (if needed) is left to the caller.
202 __isl_give isl_val
*isl_val_rat_from_isl_int(isl_ctx
*ctx
,
203 isl_int n
, isl_int d
)
207 v
= isl_val_alloc(ctx
);
211 isl_int_set(v
->n
, n
);
212 isl_int_set(v
->d
, d
);
217 /* Return a new reference to "v".
219 __isl_give isl_val
*isl_val_copy(__isl_keep isl_val
*v
)
228 /* Return a fresh copy of "val".
230 __isl_give isl_val
*isl_val_dup(__isl_keep isl_val
*val
)
237 dup
= isl_val_alloc(isl_val_get_ctx(val
));
241 isl_int_set(dup
->n
, val
->n
);
242 isl_int_set(dup
->d
, val
->d
);
247 /* Return an isl_val that is equal to "val" and that has only
248 * a single reference.
250 __isl_give isl_val
*isl_val_cow(__isl_take isl_val
*val
)
258 return isl_val_dup(val
);
261 /* Free "v" and return NULL.
263 __isl_null isl_val
*isl_val_free(__isl_take isl_val
*v
)
271 isl_ctx_deref(v
->ctx
);
278 /* Extract the numerator of a rational value "v" as an integer.
280 * If "v" is not a rational value, then the result is undefined.
282 long isl_val_get_num_si(__isl_keep isl_val
*v
)
286 if (!isl_val_is_rat(v
))
287 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
288 "expecting rational value", return 0);
289 if (!isl_int_fits_slong(v
->n
))
290 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
291 "numerator too large", return 0);
292 return isl_int_get_si(v
->n
);
295 /* Extract the numerator of a rational value "v" as an isl_int.
297 * If "v" is not a rational value, then the result is undefined.
299 isl_stat
isl_val_get_num_isl_int(__isl_keep isl_val
*v
, isl_int
*n
)
302 return isl_stat_error
;
303 if (!isl_val_is_rat(v
))
304 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
305 "expecting rational value", return isl_stat_error
);
306 isl_int_set(*n
, v
->n
);
310 /* Extract the denominator of a rational value "v" as an integer.
312 * If "v" is not a rational value, then the result is undefined.
314 long isl_val_get_den_si(__isl_keep isl_val
*v
)
318 if (!isl_val_is_rat(v
))
319 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
320 "expecting rational value", return 0);
321 if (!isl_int_fits_slong(v
->d
))
322 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
323 "denominator too large", return 0);
324 return isl_int_get_si(v
->d
);
327 /* Extract the denominator of a rational value "v" as an isl_val.
329 * If "v" is not a rational value, then the result is undefined.
331 __isl_give isl_val
*isl_val_get_den_val(__isl_keep isl_val
*v
)
335 if (!isl_val_is_rat(v
))
336 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
337 "expecting rational value", return NULL
);
338 return isl_val_int_from_isl_int(isl_val_get_ctx(v
), v
->d
);
341 /* Return an approximation of "v" as a double.
343 double isl_val_get_d(__isl_keep isl_val
*v
)
347 if (!isl_val_is_rat(v
))
348 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
349 "expecting rational value", return 0);
350 return isl_int_get_d(v
->n
) / isl_int_get_d(v
->d
);
353 /* Return the isl_ctx to which "val" belongs.
355 isl_ctx
*isl_val_get_ctx(__isl_keep isl_val
*val
)
357 return val
? val
->ctx
: NULL
;
360 /* Return a hash value that digests "val".
362 uint32_t isl_val_get_hash(__isl_keep isl_val
*val
)
369 hash
= isl_hash_init();
370 hash
= isl_int_hash(val
->n
, hash
);
371 hash
= isl_int_hash(val
->d
, hash
);
378 * In particular, make sure that the denominator of a rational value
379 * is positive and the numerator and denominator do not have any
382 * This function should not be called by an external user
383 * since it will only be given normalized values.
385 __isl_give isl_val
*isl_val_normalize(__isl_take isl_val
*v
)
391 if (isl_val_is_int(v
))
393 if (!isl_val_is_rat(v
))
395 if (isl_int_is_neg(v
->d
)) {
396 isl_int_neg(v
->d
, v
->d
);
397 isl_int_neg(v
->n
, v
->n
);
399 ctx
= isl_val_get_ctx(v
);
400 isl_int_gcd(ctx
->normalize_gcd
, v
->n
, v
->d
);
401 if (isl_int_is_one(ctx
->normalize_gcd
))
403 isl_int_divexact(v
->n
, v
->n
, ctx
->normalize_gcd
);
404 isl_int_divexact(v
->d
, v
->d
, ctx
->normalize_gcd
);
408 /* Return the opposite of "v".
410 __isl_give isl_val
*isl_val_neg(__isl_take isl_val
*v
)
414 if (isl_val_is_nan(v
))
416 if (isl_val_is_zero(v
))
422 isl_int_neg(v
->n
, v
->n
);
427 /* Return the inverse of "v".
429 __isl_give isl_val
*isl_val_inv(__isl_take isl_val
*v
)
433 if (isl_val_is_nan(v
))
435 if (isl_val_is_zero(v
)) {
436 isl_ctx
*ctx
= isl_val_get_ctx(v
);
438 return isl_val_nan(ctx
);
440 if (isl_val_is_infty(v
) || isl_val_is_neginfty(v
)) {
441 isl_ctx
*ctx
= isl_val_get_ctx(v
);
443 return isl_val_zero(ctx
);
449 isl_int_swap(v
->n
, v
->d
);
451 return isl_val_normalize(v
);
454 /* Return the absolute value of "v".
456 __isl_give isl_val
*isl_val_abs(__isl_take isl_val
*v
)
460 if (isl_val_is_nan(v
))
462 if (isl_val_is_nonneg(v
))
464 return isl_val_neg(v
);
467 /* Return the "floor" (greatest integer part) of "v".
468 * That is, return the result of rounding towards -infinity.
470 __isl_give isl_val
*isl_val_floor(__isl_take isl_val
*v
)
474 if (isl_val_is_int(v
))
476 if (!isl_val_is_rat(v
))
482 isl_int_fdiv_q(v
->n
, v
->n
, v
->d
);
483 isl_int_set_si(v
->d
, 1);
488 /* Return the "ceiling" of "v".
489 * That is, return the result of rounding towards +infinity.
491 __isl_give isl_val
*isl_val_ceil(__isl_take isl_val
*v
)
495 if (isl_val_is_int(v
))
497 if (!isl_val_is_rat(v
))
503 isl_int_cdiv_q(v
->n
, v
->n
, v
->d
);
504 isl_int_set_si(v
->d
, 1);
510 * That is, return the result of rounding towards zero.
512 __isl_give isl_val
*isl_val_trunc(__isl_take isl_val
*v
)
516 if (isl_val_is_int(v
))
518 if (!isl_val_is_rat(v
))
524 isl_int_tdiv_q(v
->n
, v
->n
, v
->d
);
525 isl_int_set_si(v
->d
, 1);
530 /* Return 2^v, where v is an integer (that is not too large).
532 __isl_give isl_val
*isl_val_pow2(__isl_take isl_val
*v
)
540 if (!isl_val_is_int(v
))
541 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
542 "can only compute integer powers",
543 return isl_val_free(v
));
544 neg
= isl_val_is_neg(v
);
546 isl_int_neg(v
->n
, v
->n
);
547 if (!isl_int_fits_ulong(v
->n
))
548 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
549 "exponent too large", return isl_val_free(v
));
550 exp
= isl_int_get_ui(v
->n
);
552 isl_int_mul_2exp(v
->d
, v
->d
, exp
);
553 isl_int_set_si(v
->n
, 1);
555 isl_int_mul_2exp(v
->n
, v
->d
, exp
);
561 /* This is an alternative name for the function above.
563 __isl_give isl_val
*isl_val_2exp(__isl_take isl_val
*v
)
565 return isl_val_pow2(v
);
568 /* Return the minimum of "v1" and "v2".
570 __isl_give isl_val
*isl_val_min(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
575 if (isl_val_is_nan(v1
)) {
579 if (isl_val_is_nan(v2
)) {
583 if (isl_val_le(v1
, v2
)) {
596 /* Return the maximum of "v1" and "v2".
598 __isl_give isl_val
*isl_val_max(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
603 if (isl_val_is_nan(v1
)) {
607 if (isl_val_is_nan(v2
)) {
611 if (isl_val_ge(v1
, v2
)) {
624 /* Return the sum of "v1" and "v2".
626 __isl_give isl_val
*isl_val_add(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
630 if (isl_val_is_nan(v1
)) {
634 if (isl_val_is_nan(v2
)) {
638 if ((isl_val_is_infty(v1
) && isl_val_is_neginfty(v2
)) ||
639 (isl_val_is_neginfty(v1
) && isl_val_is_infty(v2
))) {
641 return isl_val_set_nan(v1
);
643 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
647 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
651 if (isl_val_is_zero(v1
)) {
655 if (isl_val_is_zero(v2
)) {
660 v1
= isl_val_cow(v1
);
663 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
664 isl_int_add(v1
->n
, v1
->n
, v2
->n
);
666 if (isl_int_eq(v1
->d
, v2
->d
))
667 isl_int_add(v1
->n
, v1
->n
, v2
->n
);
669 isl_int_mul(v1
->n
, v1
->n
, v2
->d
);
670 isl_int_addmul(v1
->n
, v2
->n
, v1
->d
);
671 isl_int_mul(v1
->d
, v1
->d
, v2
->d
);
673 v1
= isl_val_normalize(v1
);
683 /* Return the sum of "v1" and "v2".
685 __isl_give isl_val
*isl_val_add_ui(__isl_take isl_val
*v1
, unsigned long v2
)
689 if (!isl_val_is_rat(v1
))
693 v1
= isl_val_cow(v1
);
697 isl_int_addmul_ui(v1
->n
, v1
->d
, v2
);
702 /* Subtract "v2" from "v1".
704 __isl_give isl_val
*isl_val_sub(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
708 if (isl_val_is_nan(v1
)) {
712 if (isl_val_is_nan(v2
)) {
716 if ((isl_val_is_infty(v1
) && isl_val_is_infty(v2
)) ||
717 (isl_val_is_neginfty(v1
) && isl_val_is_neginfty(v2
))) {
719 return isl_val_set_nan(v1
);
721 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
725 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
727 return isl_val_neg(v2
);
729 if (isl_val_is_zero(v2
)) {
733 if (isl_val_is_zero(v1
)) {
735 return isl_val_neg(v2
);
738 v1
= isl_val_cow(v1
);
741 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
742 isl_int_sub(v1
->n
, v1
->n
, v2
->n
);
744 if (isl_int_eq(v1
->d
, v2
->d
))
745 isl_int_sub(v1
->n
, v1
->n
, v2
->n
);
747 isl_int_mul(v1
->n
, v1
->n
, v2
->d
);
748 isl_int_submul(v1
->n
, v2
->n
, v1
->d
);
749 isl_int_mul(v1
->d
, v1
->d
, v2
->d
);
751 v1
= isl_val_normalize(v1
);
761 /* Subtract "v2" from "v1".
763 __isl_give isl_val
*isl_val_sub_ui(__isl_take isl_val
*v1
, unsigned long v2
)
767 if (!isl_val_is_rat(v1
))
771 v1
= isl_val_cow(v1
);
775 isl_int_submul_ui(v1
->n
, v1
->d
, v2
);
780 /* Return the product of "v1" and "v2".
782 __isl_give isl_val
*isl_val_mul(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
786 if (isl_val_is_nan(v1
)) {
790 if (isl_val_is_nan(v2
)) {
794 if ((!isl_val_is_rat(v1
) && isl_val_is_zero(v2
)) ||
795 (isl_val_is_zero(v1
) && !isl_val_is_rat(v2
))) {
797 return isl_val_set_nan(v1
);
799 if (isl_val_is_zero(v1
)) {
803 if (isl_val_is_zero(v2
)) {
807 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
808 if (isl_val_is_neg(v2
))
809 v1
= isl_val_neg(v1
);
813 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
814 if (isl_val_is_neg(v1
))
815 v2
= isl_val_neg(v2
);
820 v1
= isl_val_cow(v1
);
823 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
824 isl_int_mul(v1
->n
, v1
->n
, v2
->n
);
826 isl_int_mul(v1
->n
, v1
->n
, v2
->n
);
827 isl_int_mul(v1
->d
, v1
->d
, v2
->d
);
828 v1
= isl_val_normalize(v1
);
838 /* Return the product of "v1" and "v2".
840 * This is a private copy of isl_val_mul for use in the generic
841 * isl_multi_*_scale_val instantiated for isl_val.
843 __isl_give isl_val
*isl_val_scale_val(__isl_take isl_val
*v1
,
844 __isl_take isl_val
*v2
)
846 return isl_val_mul(v1
, v2
);
849 /* Return the product of "v1" and "v2".
851 __isl_give isl_val
*isl_val_mul_ui(__isl_take isl_val
*v1
, unsigned long v2
)
855 if (isl_val_is_nan(v1
))
857 if (!isl_val_is_rat(v1
)) {
859 v1
= isl_val_set_nan(v1
);
864 v1
= isl_val_cow(v1
);
868 isl_int_mul_ui(v1
->n
, v1
->n
, v2
);
870 return isl_val_normalize(v1
);
873 /* Divide "v1" by "v2".
875 __isl_give isl_val
*isl_val_div(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
879 if (isl_val_is_nan(v1
)) {
883 if (isl_val_is_nan(v2
)) {
887 if (isl_val_is_zero(v2
) ||
888 (!isl_val_is_rat(v1
) && !isl_val_is_rat(v2
))) {
890 return isl_val_set_nan(v1
);
892 if (isl_val_is_zero(v1
)) {
896 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
897 if (isl_val_is_neg(v2
))
898 v1
= isl_val_neg(v1
);
902 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
904 return isl_val_set_zero(v1
);
907 v1
= isl_val_cow(v1
);
910 if (isl_val_is_int(v2
)) {
911 isl_int_mul(v1
->d
, v1
->d
, v2
->n
);
912 v1
= isl_val_normalize(v1
);
914 isl_int_mul(v1
->d
, v1
->d
, v2
->n
);
915 isl_int_mul(v1
->n
, v1
->n
, v2
->d
);
916 v1
= isl_val_normalize(v1
);
926 /* Divide "v1" by "v2".
928 __isl_give isl_val
*isl_val_div_ui(__isl_take isl_val
*v1
, unsigned long v2
)
932 if (isl_val_is_nan(v1
))
935 return isl_val_set_nan(v1
);
938 if (isl_val_is_zero(v1
))
940 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
))
942 v1
= isl_val_cow(v1
);
946 isl_int_mul_ui(v1
->d
, v1
->d
, v2
);
948 return isl_val_normalize(v1
);
951 /* Divide "v1" by "v2".
953 * This is a private copy of isl_val_div for use in the generic
954 * isl_multi_*_scale_down_val instantiated for isl_val.
956 __isl_give isl_val
*isl_val_scale_down_val(__isl_take isl_val
*v1
,
957 __isl_take isl_val
*v2
)
959 return isl_val_div(v1
, v2
);
962 /* Given two integer values "v1" and "v2", check if "v1" is divisible by "v2".
964 isl_bool
isl_val_is_divisible_by(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
967 return isl_bool_error
;
969 if (!isl_val_is_int(v1
) || !isl_val_is_int(v2
))
970 isl_die(isl_val_get_ctx(v1
), isl_error_invalid
,
971 "expecting two integers", return isl_bool_error
);
973 return isl_bool_ok(isl_int_is_divisible_by(v1
->n
, v2
->n
));
976 /* Given two integer values "v1" and "v2", return the residue of "v1"
979 __isl_give isl_val
*isl_val_mod(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
983 if (!isl_val_is_int(v1
) || !isl_val_is_int(v2
))
984 isl_die(isl_val_get_ctx(v1
), isl_error_invalid
,
985 "expecting two integers", goto error
);
986 if (isl_val_is_nonneg(v1
) && isl_val_lt(v1
, v2
)) {
990 v1
= isl_val_cow(v1
);
993 isl_int_fdiv_r(v1
->n
, v1
->n
, v2
->n
);
1002 /* Given two integer values "v1" and "v2", return the residue of "v1"
1005 * This is a private copy of isl_val_mod for use in the generic
1006 * isl_multi_*_mod_multi_val instantiated for isl_val.
1008 __isl_give isl_val
*isl_val_mod_val(__isl_take isl_val
*v1
,
1009 __isl_take isl_val
*v2
)
1011 return isl_val_mod(v1
, v2
);
1014 /* Given two integer values, return their greatest common divisor.
1016 __isl_give isl_val
*isl_val_gcd(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
1020 if (!isl_val_is_int(v1
) || !isl_val_is_int(v2
))
1021 isl_die(isl_val_get_ctx(v1
), isl_error_invalid
,
1022 "expecting two integers", goto error
);
1023 if (isl_val_eq(v1
, v2
)) {
1027 if (isl_val_is_one(v1
)) {
1031 if (isl_val_is_one(v2
)) {
1035 v1
= isl_val_cow(v1
);
1038 isl_int_gcd(v1
->n
, v1
->n
, v2
->n
);
1047 /* Compute x, y and g such that g = gcd(a,b) and a*x+b*y = g.
1049 static void isl_int_gcdext(isl_int
*g
, isl_int
*x
, isl_int
*y
,
1050 isl_int a
, isl_int b
)
1053 isl_int a_copy
, b_copy
;
1055 isl_int_init(a_copy
);
1056 isl_int_init(b_copy
);
1059 isl_int_set(a_copy
, a
);
1060 isl_int_set(b_copy
, b
);
1061 isl_int_abs(*g
, a_copy
);
1062 isl_int_abs(d
, b_copy
);
1063 isl_int_set_si(*x
, 1);
1064 isl_int_set_si(*y
, 0);
1065 while (isl_int_is_pos(d
)) {
1066 isl_int_fdiv_q(tmp
, *g
, d
);
1067 isl_int_submul(*x
, tmp
, *y
);
1068 isl_int_submul(*g
, tmp
, d
);
1069 isl_int_swap(*g
, d
);
1070 isl_int_swap(*x
, *y
);
1072 if (isl_int_is_zero(a_copy
))
1073 isl_int_set_si(*x
, 0);
1074 else if (isl_int_is_neg(a_copy
))
1075 isl_int_neg(*x
, *x
);
1076 if (isl_int_is_zero(b_copy
))
1077 isl_int_set_si(*y
, 0);
1079 isl_int_mul(tmp
, a_copy
, *x
);
1080 isl_int_sub(tmp
, *g
, tmp
);
1081 isl_int_divexact(*y
, tmp
, b_copy
);
1085 isl_int_clear(a_copy
);
1086 isl_int_clear(b_copy
);
1089 /* Given two integer values v1 and v2, return their greatest common divisor g,
1090 * as well as two integers x and y such that x * v1 + y * v2 = g.
1092 __isl_give isl_val
*isl_val_gcdext(__isl_take isl_val
*v1
,
1093 __isl_take isl_val
*v2
, __isl_give isl_val
**x
, __isl_give isl_val
**y
)
1096 isl_val
*a
= NULL
, *b
= NULL
;
1099 return isl_val_gcd(v1
, v2
);
1104 ctx
= isl_val_get_ctx(v1
);
1105 if (!isl_val_is_int(v1
) || !isl_val_is_int(v2
))
1106 isl_die(ctx
, isl_error_invalid
,
1107 "expecting two integers", goto error
);
1109 v1
= isl_val_cow(v1
);
1110 a
= isl_val_alloc(ctx
);
1111 b
= isl_val_alloc(ctx
);
1112 if (!v1
|| !a
|| !b
)
1114 isl_int_gcdext(&v1
->n
, &a
->n
, &b
->n
, v1
->n
, v2
->n
);
1116 isl_int_set_si(a
->d
, 1);
1121 isl_int_set_si(b
->d
, 1);
1139 /* Does "v" represent an integer value?
1141 isl_bool
isl_val_is_int(__isl_keep isl_val
*v
)
1144 return isl_bool_error
;
1146 return isl_bool_ok(isl_int_is_one(v
->d
));
1149 /* Does "v" represent a rational value?
1151 isl_bool
isl_val_is_rat(__isl_keep isl_val
*v
)
1154 return isl_bool_error
;
1156 return isl_bool_ok(!isl_int_is_zero(v
->d
));
1159 /* Does "v" represent NaN?
1161 isl_bool
isl_val_is_nan(__isl_keep isl_val
*v
)
1164 return isl_bool_error
;
1166 return isl_bool_ok(isl_int_is_zero(v
->n
) && isl_int_is_zero(v
->d
));
1169 /* Does "v" represent +infinity?
1171 isl_bool
isl_val_is_infty(__isl_keep isl_val
*v
)
1174 return isl_bool_error
;
1176 return isl_bool_ok(isl_int_is_pos(v
->n
) && isl_int_is_zero(v
->d
));
1179 /* Does "v" represent -infinity?
1181 isl_bool
isl_val_is_neginfty(__isl_keep isl_val
*v
)
1184 return isl_bool_error
;
1186 return isl_bool_ok(isl_int_is_neg(v
->n
) && isl_int_is_zero(v
->d
));
1189 /* Does "v" represent the integer zero?
1191 isl_bool
isl_val_is_zero(__isl_keep isl_val
*v
)
1194 return isl_bool_error
;
1196 return isl_bool_ok(isl_int_is_zero(v
->n
) && !isl_int_is_zero(v
->d
));
1199 /* Does "v" represent the integer one?
1201 isl_bool
isl_val_is_one(__isl_keep isl_val
*v
)
1204 return isl_bool_error
;
1206 if (isl_val_is_nan(v
))
1207 return isl_bool_false
;
1209 return isl_bool_ok(isl_int_eq(v
->n
, v
->d
));
1212 /* Does "v" represent the integer negative one?
1214 isl_bool
isl_val_is_negone(__isl_keep isl_val
*v
)
1217 return isl_bool_error
;
1219 return isl_bool_ok(isl_int_is_neg(v
->n
) && isl_int_abs_eq(v
->n
, v
->d
));
1222 /* Is "v" (strictly) positive?
1224 isl_bool
isl_val_is_pos(__isl_keep isl_val
*v
)
1227 return isl_bool_error
;
1229 return isl_bool_ok(isl_int_is_pos(v
->n
));
1232 /* Is "v" (strictly) negative?
1234 isl_bool
isl_val_is_neg(__isl_keep isl_val
*v
)
1237 return isl_bool_error
;
1239 return isl_bool_ok(isl_int_is_neg(v
->n
));
1242 /* Is "v" non-negative?
1244 isl_bool
isl_val_is_nonneg(__isl_keep isl_val
*v
)
1247 return isl_bool_error
;
1249 if (isl_val_is_nan(v
))
1250 return isl_bool_false
;
1252 return isl_bool_ok(isl_int_is_nonneg(v
->n
));
1255 /* Is "v" non-positive?
1257 isl_bool
isl_val_is_nonpos(__isl_keep isl_val
*v
)
1260 return isl_bool_error
;
1262 if (isl_val_is_nan(v
))
1263 return isl_bool_false
;
1265 return isl_bool_ok(isl_int_is_nonpos(v
->n
));
1268 /* Return the sign of "v".
1270 * The sign of NaN is undefined.
1272 int isl_val_sgn(__isl_keep isl_val
*v
)
1276 if (isl_val_is_zero(v
))
1278 if (isl_val_is_pos(v
))
1283 /* Is "v1" (strictly) less than "v2"?
1285 isl_bool
isl_val_lt(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1291 return isl_bool_error
;
1292 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
1293 return isl_bool_ok(isl_int_lt(v1
->n
, v2
->n
));
1294 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1295 return isl_bool_false
;
1296 if (isl_val_eq(v1
, v2
))
1297 return isl_bool_false
;
1298 if (isl_val_is_infty(v2
))
1299 return isl_bool_true
;
1300 if (isl_val_is_infty(v1
))
1301 return isl_bool_false
;
1302 if (isl_val_is_neginfty(v1
))
1303 return isl_bool_true
;
1304 if (isl_val_is_neginfty(v2
))
1305 return isl_bool_false
;
1308 isl_int_mul(t
, v1
->n
, v2
->d
);
1309 isl_int_submul(t
, v2
->n
, v1
->d
);
1310 lt
= isl_bool_ok(isl_int_is_neg(t
));
1316 /* Is "v1" (strictly) greater than "v2"?
1318 isl_bool
isl_val_gt(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1320 return isl_val_lt(v2
, v1
);
1323 /* Is "v" (strictly) greater than "i"?
1325 isl_bool
isl_val_gt_si(__isl_keep isl_val
*v
, long i
)
1331 return isl_bool_error
;
1332 if (isl_val_is_int(v
))
1333 return isl_bool_ok(isl_int_cmp_si(v
->n
, i
) > 0);
1334 if (isl_val_is_nan(v
))
1335 return isl_bool_false
;
1336 if (isl_val_is_infty(v
))
1337 return isl_bool_true
;
1338 if (isl_val_is_neginfty(v
))
1339 return isl_bool_false
;
1341 vi
= isl_val_int_from_si(isl_val_get_ctx(v
), i
);
1342 res
= isl_bool_ok(isl_val_gt(v
, vi
));
1348 /* Is "v1" less than or equal to "v2"?
1350 isl_bool
isl_val_le(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1356 return isl_bool_error
;
1357 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
1358 return isl_bool_ok(isl_int_le(v1
->n
, v2
->n
));
1359 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1360 return isl_bool_false
;
1361 if (isl_val_eq(v1
, v2
))
1362 return isl_bool_true
;
1363 if (isl_val_is_infty(v2
))
1364 return isl_bool_true
;
1365 if (isl_val_is_infty(v1
))
1366 return isl_bool_false
;
1367 if (isl_val_is_neginfty(v1
))
1368 return isl_bool_true
;
1369 if (isl_val_is_neginfty(v2
))
1370 return isl_bool_false
;
1373 isl_int_mul(t
, v1
->n
, v2
->d
);
1374 isl_int_submul(t
, v2
->n
, v1
->d
);
1375 le
= isl_bool_ok(isl_int_is_nonpos(t
));
1381 /* Is "v1" greater than or equal to "v2"?
1383 isl_bool
isl_val_ge(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1385 return isl_val_le(v2
, v1
);
1388 /* How does "v" compare to "i"?
1390 * Return 1 if v is greater, -1 if v is smaller and 0 if v is equal to i.
1392 * If v is NaN (or NULL), then the result is undefined.
1394 int isl_val_cmp_si(__isl_keep isl_val
*v
, long i
)
1401 if (isl_val_is_int(v
))
1402 return isl_int_cmp_si(v
->n
, i
);
1403 if (isl_val_is_nan(v
))
1405 if (isl_val_is_infty(v
))
1407 if (isl_val_is_neginfty(v
))
1411 isl_int_mul_si(t
, v
->d
, i
);
1412 isl_int_sub(t
, v
->n
, t
);
1413 cmp
= isl_int_sgn(t
);
1419 /* Is "v1" equal to "v2"?
1421 isl_bool
isl_val_eq(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1424 return isl_bool_error
;
1425 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1426 return isl_bool_false
;
1428 return isl_bool_ok(isl_int_eq(v1
->n
, v2
->n
) &&
1429 isl_int_eq(v1
->d
, v2
->d
));
1432 /* Is "v" equal to "i"?
1434 isl_bool
isl_val_eq_si(__isl_keep isl_val
*v
, long i
)
1437 return isl_bool_error
;
1438 if (!isl_val_is_int(v
))
1439 return isl_bool_false
;
1440 return isl_bool_ok(isl_int_cmp_si(v
->n
, i
) == 0);
1443 /* Is "v1" equal to "v2" in absolute value?
1445 isl_bool
isl_val_abs_eq(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1448 return isl_bool_error
;
1449 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1450 return isl_bool_false
;
1452 return isl_bool_ok(isl_int_abs_eq(v1
->n
, v2
->n
) &&
1453 isl_int_eq(v1
->d
, v2
->d
));
1456 /* Is "v1" different from "v2"?
1458 isl_bool
isl_val_ne(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1461 return isl_bool_error
;
1462 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1463 return isl_bool_false
;
1465 return isl_bool_ok(isl_int_ne(v1
->n
, v2
->n
) ||
1466 isl_int_ne(v1
->d
, v2
->d
));
1469 /* Print a textual representation of "v" onto "p".
1471 __isl_give isl_printer
*isl_printer_print_val(__isl_take isl_printer
*p
,
1472 __isl_keep isl_val
*v
)
1477 return isl_printer_free(p
);
1479 neg
= isl_int_is_neg(v
->n
);
1481 p
= isl_printer_print_str(p
, "-");
1482 isl_int_neg(v
->n
, v
->n
);
1484 if (isl_int_is_zero(v
->d
)) {
1485 int sgn
= isl_int_sgn(v
->n
);
1486 p
= isl_printer_print_str(p
, sgn
< 0 ? "-infty" :
1487 sgn
== 0 ? "NaN" : "infty");
1489 p
= isl_printer_print_isl_int(p
, v
->n
);
1491 isl_int_neg(v
->n
, v
->n
);
1492 if (!isl_int_is_zero(v
->d
) && !isl_int_is_one(v
->d
)) {
1493 p
= isl_printer_print_str(p
, "/");
1494 p
= isl_printer_print_isl_int(p
, v
->d
);
1500 /* Is "val1" (obviously) equal to "val2"?
1502 * This is a private copy of isl_val_eq for use in the generic
1503 * isl_multi_*_plain_is_equal instantiated for isl_val.
1505 isl_bool
isl_val_plain_is_equal(__isl_keep isl_val
*val1
,
1506 __isl_keep isl_val
*val2
)
1508 return isl_val_eq(val1
, val2
);
1511 /* Does "v" have any non-zero coefficients
1512 * for any dimension in the given range?
1514 * This function is only meant to be used in the generic isl_multi_*
1515 * functions which have to deal with base objects that have an associated
1516 * space. Since an isl_val does not have any coefficients, this function
1517 * always returns isl_bool_false.
1519 isl_bool
isl_val_involves_dims(__isl_keep isl_val
*v
, enum isl_dim_type type
,
1520 unsigned first
, unsigned n
)
1523 return isl_bool_error
;
1525 return isl_bool_false
;
1528 /* Insert "n" dimensions of type "type" at position "first".
1530 * This function is only meant to be used in the generic isl_multi_*
1531 * functions which have to deal with base objects that have an associated
1532 * space. Since an isl_val does not have an associated space, this function
1533 * does not do anything.
1535 __isl_give isl_val
*isl_val_insert_dims(__isl_take isl_val
*v
,
1536 enum isl_dim_type type
, unsigned first
, unsigned n
)
1541 /* Change the name of the dimension of type "type" at position "pos" to "s".
1543 * This function is only meant to be used in the generic isl_multi_*
1544 * functions which have to deal with base objects that have an associated
1545 * space. Since an isl_val does not have an associated space, this function
1546 * does not do anything.
1548 __isl_give isl_val
*isl_val_set_dim_name(__isl_take isl_val
*v
,
1549 enum isl_dim_type type
, unsigned pos
, const char *s
)
1554 /* Return an isl_val that is zero on "ls".
1556 * This function is only meant to be used in the generic isl_multi_*
1557 * functions which have to deal with base objects that have an associated
1558 * space. Since an isl_val does not have an associated space, this function
1559 * simply returns a zero isl_val in the same context as "ls".
1561 __isl_give isl_val
*isl_val_zero_on_domain(__isl_take isl_local_space
*ls
)
1567 ctx
= isl_local_space_get_ctx(ls
);
1568 isl_local_space_free(ls
);
1569 return isl_val_zero(ctx
);
1572 #define isl_val_involves_nan isl_val_is_nan
1577 #include <isl_multi_no_domain_templ.c>
1578 #include <isl_multi_no_explicit_domain.c>
1579 #include <isl_multi_templ.c>
1580 #include <isl_multi_un_op_templ.c>
1581 #include <isl_multi_bin_val_templ.c>
1582 #include <isl_multi_arith_templ.c>
1583 #include <isl_multi_dim_id_templ.c>
1584 #include <isl_multi_dims.c>
1585 #include <isl_multi_min_max_templ.c>
1586 #include <isl_multi_nan_templ.c>
1587 #include <isl_multi_product_templ.c>
1588 #include <isl_multi_splice_templ.c>
1589 #include <isl_multi_tuple_id_templ.c>
1590 #include <isl_multi_zero_templ.c>
1592 /* Does "mv" consist of only zeros?
1594 isl_bool
isl_multi_val_is_zero(__isl_keep isl_multi_val
*mv
)
1596 return isl_multi_val_every(mv
, &isl_val_is_zero
);
1599 /* Add "v" to each of the elements of "mv".
1601 __isl_give isl_multi_val
*isl_multi_val_add_val(__isl_take isl_multi_val
*mv
,
1602 __isl_take isl_val
*v
)
1605 return isl_multi_val_free(mv
);
1606 if (isl_val_is_zero(v
)) {
1610 return isl_multi_val_fn_val(mv
, &isl_val_add
, v
);
1613 /* Reduce the elements of "mv" modulo "v".
1615 __isl_give isl_multi_val
*isl_multi_val_mod_val(__isl_take isl_multi_val
*mv
,
1616 __isl_take isl_val
*v
)
1618 return isl_multi_val_fn_val(mv
, &isl_val_mod
, v
);