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 /* Extract the denominator of a rational value "v" as an isl_val.
328 * If "v" is not a rational value, then the result is undefined.
330 __isl_give isl_val
*isl_val_get_den_val(__isl_keep isl_val
*v
)
334 if (!isl_val_is_rat(v
))
335 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
336 "expecting rational value", return NULL
);
337 return isl_val_int_from_isl_int(isl_val_get_ctx(v
), v
->d
);
340 /* Return an approximation of "v" as a double.
342 double isl_val_get_d(__isl_keep isl_val
*v
)
346 if (!isl_val_is_rat(v
))
347 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
348 "expecting rational value", return 0);
349 return isl_int_get_d(v
->n
) / isl_int_get_d(v
->d
);
352 /* Return the isl_ctx to which "val" belongs.
354 isl_ctx
*isl_val_get_ctx(__isl_keep isl_val
*val
)
356 return val
? val
->ctx
: NULL
;
359 /* Return a hash value that digests "val".
361 uint32_t isl_val_get_hash(__isl_keep isl_val
*val
)
368 hash
= isl_hash_init();
369 hash
= isl_int_hash(val
->n
, hash
);
370 hash
= isl_int_hash(val
->d
, hash
);
377 * In particular, make sure that the denominator of a rational value
378 * is positive and the numerator and denominator do not have any
381 * This function should not be called by an external user
382 * since it will only be given normalized values.
384 __isl_give isl_val
*isl_val_normalize(__isl_take isl_val
*v
)
390 if (isl_val_is_int(v
))
392 if (!isl_val_is_rat(v
))
394 if (isl_int_is_neg(v
->d
)) {
395 isl_int_neg(v
->d
, v
->d
);
396 isl_int_neg(v
->n
, v
->n
);
398 ctx
= isl_val_get_ctx(v
);
399 isl_int_gcd(ctx
->normalize_gcd
, v
->n
, v
->d
);
400 if (isl_int_is_one(ctx
->normalize_gcd
))
402 isl_int_divexact(v
->n
, v
->n
, ctx
->normalize_gcd
);
403 isl_int_divexact(v
->d
, v
->d
, ctx
->normalize_gcd
);
407 /* Return the opposite of "v".
409 __isl_give isl_val
*isl_val_neg(__isl_take isl_val
*v
)
413 if (isl_val_is_nan(v
))
415 if (isl_val_is_zero(v
))
421 isl_int_neg(v
->n
, v
->n
);
426 /* Return the inverse of "v".
428 __isl_give isl_val
*isl_val_inv(__isl_take isl_val
*v
)
432 if (isl_val_is_nan(v
))
434 if (isl_val_is_zero(v
)) {
435 isl_ctx
*ctx
= isl_val_get_ctx(v
);
437 return isl_val_nan(ctx
);
439 if (isl_val_is_infty(v
) || isl_val_is_neginfty(v
)) {
440 isl_ctx
*ctx
= isl_val_get_ctx(v
);
442 return isl_val_zero(ctx
);
448 isl_int_swap(v
->n
, v
->d
);
450 return isl_val_normalize(v
);
453 /* Return the absolute value of "v".
455 __isl_give isl_val
*isl_val_abs(__isl_take isl_val
*v
)
459 if (isl_val_is_nan(v
))
461 if (isl_val_is_nonneg(v
))
463 return isl_val_neg(v
);
466 /* Return the "floor" (greatest integer part) of "v".
467 * That is, return the result of rounding towards -infinity.
469 __isl_give isl_val
*isl_val_floor(__isl_take isl_val
*v
)
473 if (isl_val_is_int(v
))
475 if (!isl_val_is_rat(v
))
481 isl_int_fdiv_q(v
->n
, v
->n
, v
->d
);
482 isl_int_set_si(v
->d
, 1);
487 /* Return the "ceiling" of "v".
488 * That is, return the result of rounding towards +infinity.
490 __isl_give isl_val
*isl_val_ceil(__isl_take isl_val
*v
)
494 if (isl_val_is_int(v
))
496 if (!isl_val_is_rat(v
))
502 isl_int_cdiv_q(v
->n
, v
->n
, v
->d
);
503 isl_int_set_si(v
->d
, 1);
509 * That is, return the result of rounding towards zero.
511 __isl_give isl_val
*isl_val_trunc(__isl_take isl_val
*v
)
515 if (isl_val_is_int(v
))
517 if (!isl_val_is_rat(v
))
523 isl_int_tdiv_q(v
->n
, v
->n
, v
->d
);
524 isl_int_set_si(v
->d
, 1);
529 /* Return 2^v, where v is an integer (that is not too large).
531 __isl_give isl_val
*isl_val_2exp(__isl_take isl_val
*v
)
539 if (!isl_val_is_int(v
))
540 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
541 "can only compute integer powers",
542 return isl_val_free(v
));
543 neg
= isl_val_is_neg(v
);
545 isl_int_neg(v
->n
, v
->n
);
546 if (!isl_int_fits_ulong(v
->n
))
547 isl_die(isl_val_get_ctx(v
), isl_error_invalid
,
548 "exponent too large", return isl_val_free(v
));
549 exp
= isl_int_get_ui(v
->n
);
551 isl_int_mul_2exp(v
->d
, v
->d
, exp
);
552 isl_int_set_si(v
->n
, 1);
554 isl_int_mul_2exp(v
->n
, v
->d
, exp
);
560 /* Return the minimum of "v1" and "v2".
562 __isl_give isl_val
*isl_val_min(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
567 if (isl_val_is_nan(v1
)) {
571 if (isl_val_is_nan(v2
)) {
575 if (isl_val_le(v1
, v2
)) {
588 /* Return the maximum of "v1" and "v2".
590 __isl_give isl_val
*isl_val_max(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
595 if (isl_val_is_nan(v1
)) {
599 if (isl_val_is_nan(v2
)) {
603 if (isl_val_ge(v1
, v2
)) {
616 /* Return the sum of "v1" and "v2".
618 __isl_give isl_val
*isl_val_add(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
622 if (isl_val_is_nan(v1
)) {
626 if (isl_val_is_nan(v2
)) {
630 if ((isl_val_is_infty(v1
) && isl_val_is_neginfty(v2
)) ||
631 (isl_val_is_neginfty(v1
) && isl_val_is_infty(v2
))) {
633 return isl_val_set_nan(v1
);
635 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
639 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
643 if (isl_val_is_zero(v1
)) {
647 if (isl_val_is_zero(v2
)) {
652 v1
= isl_val_cow(v1
);
655 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
656 isl_int_add(v1
->n
, v1
->n
, v2
->n
);
658 if (isl_int_eq(v1
->d
, v2
->d
))
659 isl_int_add(v1
->n
, v1
->n
, v2
->n
);
661 isl_int_mul(v1
->n
, v1
->n
, v2
->d
);
662 isl_int_addmul(v1
->n
, v2
->n
, v1
->d
);
663 isl_int_mul(v1
->d
, v1
->d
, v2
->d
);
665 v1
= isl_val_normalize(v1
);
675 /* Return the sum of "v1" and "v2".
677 __isl_give isl_val
*isl_val_add_ui(__isl_take isl_val
*v1
, unsigned long v2
)
681 if (!isl_val_is_rat(v1
))
685 v1
= isl_val_cow(v1
);
689 isl_int_addmul_ui(v1
->n
, v1
->d
, v2
);
694 /* Subtract "v2" from "v1".
696 __isl_give isl_val
*isl_val_sub(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
700 if (isl_val_is_nan(v1
)) {
704 if (isl_val_is_nan(v2
)) {
708 if ((isl_val_is_infty(v1
) && isl_val_is_infty(v2
)) ||
709 (isl_val_is_neginfty(v1
) && isl_val_is_neginfty(v2
))) {
711 return isl_val_set_nan(v1
);
713 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
717 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
719 return isl_val_neg(v2
);
721 if (isl_val_is_zero(v2
)) {
725 if (isl_val_is_zero(v1
)) {
727 return isl_val_neg(v2
);
730 v1
= isl_val_cow(v1
);
733 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
734 isl_int_sub(v1
->n
, v1
->n
, v2
->n
);
736 if (isl_int_eq(v1
->d
, v2
->d
))
737 isl_int_sub(v1
->n
, v1
->n
, v2
->n
);
739 isl_int_mul(v1
->n
, v1
->n
, v2
->d
);
740 isl_int_submul(v1
->n
, v2
->n
, v1
->d
);
741 isl_int_mul(v1
->d
, v1
->d
, v2
->d
);
743 v1
= isl_val_normalize(v1
);
753 /* Subtract "v2" from "v1".
755 __isl_give isl_val
*isl_val_sub_ui(__isl_take isl_val
*v1
, unsigned long v2
)
759 if (!isl_val_is_rat(v1
))
763 v1
= isl_val_cow(v1
);
767 isl_int_submul_ui(v1
->n
, v1
->d
, v2
);
772 /* Return the product of "v1" and "v2".
774 __isl_give isl_val
*isl_val_mul(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
778 if (isl_val_is_nan(v1
)) {
782 if (isl_val_is_nan(v2
)) {
786 if ((!isl_val_is_rat(v1
) && isl_val_is_zero(v2
)) ||
787 (isl_val_is_zero(v1
) && !isl_val_is_rat(v2
))) {
789 return isl_val_set_nan(v1
);
791 if (isl_val_is_zero(v1
)) {
795 if (isl_val_is_zero(v2
)) {
799 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
800 if (isl_val_is_neg(v2
))
801 v1
= isl_val_neg(v1
);
805 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
806 if (isl_val_is_neg(v1
))
807 v2
= isl_val_neg(v2
);
812 v1
= isl_val_cow(v1
);
815 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
816 isl_int_mul(v1
->n
, v1
->n
, v2
->n
);
818 isl_int_mul(v1
->n
, v1
->n
, v2
->n
);
819 isl_int_mul(v1
->d
, v1
->d
, v2
->d
);
820 v1
= isl_val_normalize(v1
);
830 /* Return the product of "v1" and "v2".
832 * This is a private copy of isl_val_mul for use in the generic
833 * isl_multi_*_scale_val instantiated for isl_val.
835 __isl_give isl_val
*isl_val_scale_val(__isl_take isl_val
*v1
,
836 __isl_take isl_val
*v2
)
838 return isl_val_mul(v1
, v2
);
841 /* Return the product of "v1" and "v2".
843 __isl_give isl_val
*isl_val_mul_ui(__isl_take isl_val
*v1
, unsigned long v2
)
847 if (isl_val_is_nan(v1
))
849 if (!isl_val_is_rat(v1
)) {
851 v1
= isl_val_set_nan(v1
);
856 v1
= isl_val_cow(v1
);
860 isl_int_mul_ui(v1
->n
, v1
->n
, v2
);
862 return isl_val_normalize(v1
);
865 /* Divide "v1" by "v2".
867 __isl_give isl_val
*isl_val_div(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
871 if (isl_val_is_nan(v1
)) {
875 if (isl_val_is_nan(v2
)) {
879 if (isl_val_is_zero(v2
) ||
880 (!isl_val_is_rat(v1
) && !isl_val_is_rat(v2
))) {
882 return isl_val_set_nan(v1
);
884 if (isl_val_is_zero(v1
)) {
888 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
)) {
889 if (isl_val_is_neg(v2
))
890 v1
= isl_val_neg(v1
);
894 if (isl_val_is_infty(v2
) || isl_val_is_neginfty(v2
)) {
896 return isl_val_set_zero(v1
);
899 v1
= isl_val_cow(v1
);
902 if (isl_val_is_int(v2
)) {
903 isl_int_mul(v1
->d
, v1
->d
, v2
->n
);
904 v1
= isl_val_normalize(v1
);
906 isl_int_mul(v1
->d
, v1
->d
, v2
->n
);
907 isl_int_mul(v1
->n
, v1
->n
, v2
->d
);
908 v1
= isl_val_normalize(v1
);
918 /* Divide "v1" by "v2".
920 __isl_give isl_val
*isl_val_div_ui(__isl_take isl_val
*v1
, unsigned long v2
)
924 if (isl_val_is_nan(v1
))
927 return isl_val_set_nan(v1
);
930 if (isl_val_is_zero(v1
))
932 if (isl_val_is_infty(v1
) || isl_val_is_neginfty(v1
))
934 v1
= isl_val_cow(v1
);
938 isl_int_mul_ui(v1
->d
, v1
->d
, v2
);
940 return isl_val_normalize(v1
);
943 /* Divide "v1" by "v2".
945 * This is a private copy of isl_val_div for use in the generic
946 * isl_multi_*_scale_down_val instantiated for isl_val.
948 __isl_give isl_val
*isl_val_scale_down_val(__isl_take isl_val
*v1
,
949 __isl_take isl_val
*v2
)
951 return isl_val_div(v1
, v2
);
954 /* Given two integer values "v1" and "v2", check if "v1" is divisible by "v2".
956 isl_bool
isl_val_is_divisible_by(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
959 return isl_bool_error
;
961 if (!isl_val_is_int(v1
) || !isl_val_is_int(v2
))
962 isl_die(isl_val_get_ctx(v1
), isl_error_invalid
,
963 "expecting two integers", return isl_bool_error
);
965 return isl_int_is_divisible_by(v1
->n
, v2
->n
);
968 /* Given two integer values "v1" and "v2", return the residue of "v1"
971 __isl_give isl_val
*isl_val_mod(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
975 if (!isl_val_is_int(v1
) || !isl_val_is_int(v2
))
976 isl_die(isl_val_get_ctx(v1
), isl_error_invalid
,
977 "expecting two integers", goto error
);
978 if (isl_val_is_nonneg(v1
) && isl_val_lt(v1
, v2
)) {
982 v1
= isl_val_cow(v1
);
985 isl_int_fdiv_r(v1
->n
, v1
->n
, v2
->n
);
994 /* Given two integer values "v1" and "v2", return the residue of "v1"
997 * This is a private copy of isl_val_mod for use in the generic
998 * isl_multi_*_mod_multi_val instantiated for isl_val.
1000 __isl_give isl_val
*isl_val_mod_val(__isl_take isl_val
*v1
,
1001 __isl_take isl_val
*v2
)
1003 return isl_val_mod(v1
, v2
);
1006 /* Given two integer values, return their greatest common divisor.
1008 __isl_give isl_val
*isl_val_gcd(__isl_take isl_val
*v1
, __isl_take isl_val
*v2
)
1012 if (!isl_val_is_int(v1
) || !isl_val_is_int(v2
))
1013 isl_die(isl_val_get_ctx(v1
), isl_error_invalid
,
1014 "expecting two integers", goto error
);
1015 if (isl_val_eq(v1
, v2
)) {
1019 if (isl_val_is_one(v1
)) {
1023 if (isl_val_is_one(v2
)) {
1027 v1
= isl_val_cow(v1
);
1030 isl_int_gcd(v1
->n
, v1
->n
, v2
->n
);
1039 /* Compute x, y and g such that g = gcd(a,b) and a*x+b*y = g.
1041 static void isl_int_gcdext(isl_int
*g
, isl_int
*x
, isl_int
*y
,
1042 isl_int a
, isl_int b
)
1045 isl_int a_copy
, b_copy
;
1047 isl_int_init(a_copy
);
1048 isl_int_init(b_copy
);
1051 isl_int_set(a_copy
, a
);
1052 isl_int_set(b_copy
, b
);
1053 isl_int_abs(*g
, a_copy
);
1054 isl_int_abs(d
, b_copy
);
1055 isl_int_set_si(*x
, 1);
1056 isl_int_set_si(*y
, 0);
1057 while (isl_int_is_pos(d
)) {
1058 isl_int_fdiv_q(tmp
, *g
, d
);
1059 isl_int_submul(*x
, tmp
, *y
);
1060 isl_int_submul(*g
, tmp
, d
);
1061 isl_int_swap(*g
, d
);
1062 isl_int_swap(*x
, *y
);
1064 if (isl_int_is_zero(a_copy
))
1065 isl_int_set_si(*x
, 0);
1066 else if (isl_int_is_neg(a_copy
))
1067 isl_int_neg(*x
, *x
);
1068 if (isl_int_is_zero(b_copy
))
1069 isl_int_set_si(*y
, 0);
1071 isl_int_mul(tmp
, a_copy
, *x
);
1072 isl_int_sub(tmp
, *g
, tmp
);
1073 isl_int_divexact(*y
, tmp
, b_copy
);
1077 isl_int_clear(a_copy
);
1078 isl_int_clear(b_copy
);
1081 /* Given two integer values v1 and v2, return their greatest common divisor g,
1082 * as well as two integers x and y such that x * v1 + y * v2 = g.
1084 __isl_give isl_val
*isl_val_gcdext(__isl_take isl_val
*v1
,
1085 __isl_take isl_val
*v2
, __isl_give isl_val
**x
, __isl_give isl_val
**y
)
1088 isl_val
*a
= NULL
, *b
= NULL
;
1091 return isl_val_gcd(v1
, v2
);
1096 ctx
= isl_val_get_ctx(v1
);
1097 if (!isl_val_is_int(v1
) || !isl_val_is_int(v2
))
1098 isl_die(ctx
, isl_error_invalid
,
1099 "expecting two integers", goto error
);
1101 v1
= isl_val_cow(v1
);
1102 a
= isl_val_alloc(ctx
);
1103 b
= isl_val_alloc(ctx
);
1104 if (!v1
|| !a
|| !b
)
1106 isl_int_gcdext(&v1
->n
, &a
->n
, &b
->n
, v1
->n
, v2
->n
);
1108 isl_int_set_si(a
->d
, 1);
1113 isl_int_set_si(b
->d
, 1);
1131 /* Does "v" represent an integer value?
1133 isl_bool
isl_val_is_int(__isl_keep isl_val
*v
)
1136 return isl_bool_error
;
1138 return isl_int_is_one(v
->d
);
1141 /* Does "v" represent a rational value?
1143 isl_bool
isl_val_is_rat(__isl_keep isl_val
*v
)
1146 return isl_bool_error
;
1148 return !isl_int_is_zero(v
->d
);
1151 /* Does "v" represent NaN?
1153 isl_bool
isl_val_is_nan(__isl_keep isl_val
*v
)
1156 return isl_bool_error
;
1158 return isl_int_is_zero(v
->n
) && isl_int_is_zero(v
->d
);
1161 /* Does "v" represent +infinity?
1163 isl_bool
isl_val_is_infty(__isl_keep isl_val
*v
)
1166 return isl_bool_error
;
1168 return isl_int_is_pos(v
->n
) && isl_int_is_zero(v
->d
);
1171 /* Does "v" represent -infinity?
1173 isl_bool
isl_val_is_neginfty(__isl_keep isl_val
*v
)
1176 return isl_bool_error
;
1178 return isl_int_is_neg(v
->n
) && isl_int_is_zero(v
->d
);
1181 /* Does "v" represent the integer zero?
1183 isl_bool
isl_val_is_zero(__isl_keep isl_val
*v
)
1186 return isl_bool_error
;
1188 return isl_int_is_zero(v
->n
) && !isl_int_is_zero(v
->d
);
1191 /* Does "v" represent the integer one?
1193 isl_bool
isl_val_is_one(__isl_keep isl_val
*v
)
1196 return isl_bool_error
;
1198 if (isl_val_is_nan(v
))
1199 return isl_bool_false
;
1201 return isl_int_eq(v
->n
, v
->d
);
1204 /* Does "v" represent the integer negative one?
1206 isl_bool
isl_val_is_negone(__isl_keep isl_val
*v
)
1209 return isl_bool_error
;
1211 return isl_int_is_neg(v
->n
) && isl_int_abs_eq(v
->n
, v
->d
);
1214 /* Is "v" (strictly) positive?
1216 isl_bool
isl_val_is_pos(__isl_keep isl_val
*v
)
1219 return isl_bool_error
;
1221 return isl_int_is_pos(v
->n
);
1224 /* Is "v" (strictly) negative?
1226 isl_bool
isl_val_is_neg(__isl_keep isl_val
*v
)
1229 return isl_bool_error
;
1231 return isl_int_is_neg(v
->n
);
1234 /* Is "v" non-negative?
1236 isl_bool
isl_val_is_nonneg(__isl_keep isl_val
*v
)
1239 return isl_bool_error
;
1241 if (isl_val_is_nan(v
))
1242 return isl_bool_false
;
1244 return isl_int_is_nonneg(v
->n
);
1247 /* Is "v" non-positive?
1249 isl_bool
isl_val_is_nonpos(__isl_keep isl_val
*v
)
1252 return isl_bool_error
;
1254 if (isl_val_is_nan(v
))
1255 return isl_bool_false
;
1257 return isl_int_is_nonpos(v
->n
);
1260 /* Return the sign of "v".
1262 * The sign of NaN is undefined.
1264 int isl_val_sgn(__isl_keep isl_val
*v
)
1268 if (isl_val_is_zero(v
))
1270 if (isl_val_is_pos(v
))
1275 /* Is "v1" (strictly) less than "v2"?
1277 isl_bool
isl_val_lt(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1283 return isl_bool_error
;
1284 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
1285 return isl_int_lt(v1
->n
, v2
->n
);
1286 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1287 return isl_bool_false
;
1288 if (isl_val_eq(v1
, v2
))
1289 return isl_bool_false
;
1290 if (isl_val_is_infty(v2
))
1291 return isl_bool_true
;
1292 if (isl_val_is_infty(v1
))
1293 return isl_bool_false
;
1294 if (isl_val_is_neginfty(v1
))
1295 return isl_bool_true
;
1296 if (isl_val_is_neginfty(v2
))
1297 return isl_bool_false
;
1300 isl_int_mul(t
, v1
->n
, v2
->d
);
1301 isl_int_submul(t
, v2
->n
, v1
->d
);
1302 lt
= isl_int_is_neg(t
);
1308 /* Is "v1" (strictly) greater than "v2"?
1310 isl_bool
isl_val_gt(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1312 return isl_val_lt(v2
, v1
);
1315 /* Is "v" (strictly) greater than "i"?
1317 isl_bool
isl_val_gt_si(__isl_keep isl_val
*v
, long i
)
1323 return isl_bool_error
;
1324 if (isl_val_is_int(v
))
1325 return isl_int_cmp_si(v
->n
, i
) > 0;
1326 if (isl_val_is_nan(v
))
1327 return isl_bool_false
;
1328 if (isl_val_is_infty(v
))
1329 return isl_bool_true
;
1330 if (isl_val_is_neginfty(v
))
1331 return isl_bool_false
;
1333 vi
= isl_val_int_from_si(isl_val_get_ctx(v
), i
);
1334 res
= isl_val_gt(v
, vi
);
1340 /* Is "v1" less than or equal to "v2"?
1342 isl_bool
isl_val_le(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1348 return isl_bool_error
;
1349 if (isl_val_is_int(v1
) && isl_val_is_int(v2
))
1350 return isl_int_le(v1
->n
, v2
->n
);
1351 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1352 return isl_bool_false
;
1353 if (isl_val_eq(v1
, v2
))
1354 return isl_bool_true
;
1355 if (isl_val_is_infty(v2
))
1356 return isl_bool_true
;
1357 if (isl_val_is_infty(v1
))
1358 return isl_bool_false
;
1359 if (isl_val_is_neginfty(v1
))
1360 return isl_bool_true
;
1361 if (isl_val_is_neginfty(v2
))
1362 return isl_bool_false
;
1365 isl_int_mul(t
, v1
->n
, v2
->d
);
1366 isl_int_submul(t
, v2
->n
, v1
->d
);
1367 le
= isl_int_is_nonpos(t
);
1373 /* Is "v1" greater than or equal to "v2"?
1375 isl_bool
isl_val_ge(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1377 return isl_val_le(v2
, v1
);
1380 /* How does "v" compare to "i"?
1382 * Return 1 if v is greater, -1 if v is smaller and 0 if v is equal to i.
1384 * If v is NaN (or NULL), then the result is undefined.
1386 int isl_val_cmp_si(__isl_keep isl_val
*v
, long i
)
1393 if (isl_val_is_int(v
))
1394 return isl_int_cmp_si(v
->n
, i
);
1395 if (isl_val_is_nan(v
))
1397 if (isl_val_is_infty(v
))
1399 if (isl_val_is_neginfty(v
))
1403 isl_int_mul_si(t
, v
->d
, i
);
1404 isl_int_sub(t
, v
->n
, t
);
1405 cmp
= isl_int_sgn(t
);
1411 /* Is "v1" equal to "v2"?
1413 isl_bool
isl_val_eq(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1416 return isl_bool_error
;
1417 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1418 return isl_bool_false
;
1420 return isl_int_eq(v1
->n
, v2
->n
) && isl_int_eq(v1
->d
, v2
->d
);
1423 /* Is "v1" equal to "v2" in absolute value?
1425 isl_bool
isl_val_abs_eq(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1428 return isl_bool_error
;
1429 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1430 return isl_bool_false
;
1432 return isl_int_abs_eq(v1
->n
, v2
->n
) && isl_int_eq(v1
->d
, v2
->d
);
1435 /* Is "v1" different from "v2"?
1437 isl_bool
isl_val_ne(__isl_keep isl_val
*v1
, __isl_keep isl_val
*v2
)
1440 return isl_bool_error
;
1441 if (isl_val_is_nan(v1
) || isl_val_is_nan(v2
))
1442 return isl_bool_false
;
1444 return isl_int_ne(v1
->n
, v2
->n
) || isl_int_ne(v1
->d
, v2
->d
);
1447 /* Print a textual representation of "v" onto "p".
1449 __isl_give isl_printer
*isl_printer_print_val(__isl_take isl_printer
*p
,
1450 __isl_keep isl_val
*v
)
1455 return isl_printer_free(p
);
1457 neg
= isl_int_is_neg(v
->n
);
1459 p
= isl_printer_print_str(p
, "-");
1460 isl_int_neg(v
->n
, v
->n
);
1462 if (isl_int_is_zero(v
->d
)) {
1463 int sgn
= isl_int_sgn(v
->n
);
1464 p
= isl_printer_print_str(p
, sgn
< 0 ? "-infty" :
1465 sgn
== 0 ? "NaN" : "infty");
1467 p
= isl_printer_print_isl_int(p
, v
->n
);
1469 isl_int_neg(v
->n
, v
->n
);
1470 if (!isl_int_is_zero(v
->d
) && !isl_int_is_one(v
->d
)) {
1471 p
= isl_printer_print_str(p
, "/");
1472 p
= isl_printer_print_isl_int(p
, v
->d
);
1478 /* Is "val1" (obviously) equal to "val2"?
1480 * This is a private copy of isl_val_eq for use in the generic
1481 * isl_multi_*_plain_is_equal instantiated for isl_val.
1483 int isl_val_plain_is_equal(__isl_keep isl_val
*val1
, __isl_keep isl_val
*val2
)
1485 return isl_val_eq(val1
, val2
);
1488 /* Does "v" have any non-zero coefficients
1489 * for any dimension in the given range?
1491 * This function is only meant to be used in the generic isl_multi_*
1492 * functions which have to deal with base objects that have an associated
1493 * space. Since an isl_val does not have any coefficients, this function
1496 int isl_val_involves_dims(__isl_keep isl_val
*v
, enum isl_dim_type type
,
1497 unsigned first
, unsigned n
)
1505 /* Insert "n" dimensions of type "type" at position "first".
1507 * This function is only meant to be used in the generic isl_multi_*
1508 * functions which have to deal with base objects that have an associated
1509 * space. Since an isl_val does not have an associated space, this function
1510 * does not do anything.
1512 __isl_give isl_val
*isl_val_insert_dims(__isl_take isl_val
*v
,
1513 enum isl_dim_type type
, unsigned first
, unsigned n
)
1518 /* Drop the "n" first dimensions of type "type" at position "first".
1520 * This function is only meant to be used in the generic isl_multi_*
1521 * functions which have to deal with base objects that have an associated
1522 * space. Since an isl_val does not have an associated space, this function
1523 * does not do anything.
1525 __isl_give isl_val
*isl_val_drop_dims(__isl_take isl_val
*v
,
1526 enum isl_dim_type type
, unsigned first
, unsigned n
)
1531 /* Change the name of the dimension of type "type" at position "pos" to "s".
1533 * This function is only meant to be used in the generic isl_multi_*
1534 * functions which have to deal with base objects that have an associated
1535 * space. Since an isl_val does not have an associated space, this function
1536 * does not do anything.
1538 __isl_give isl_val
*isl_val_set_dim_name(__isl_take isl_val
*v
,
1539 enum isl_dim_type type
, unsigned pos
, const char *s
)
1544 /* Return the space of "v".
1546 * This function is only meant to be used in the generic isl_multi_*
1547 * functions which have to deal with base objects that have an associated
1548 * space. The conditions surrounding the call to this function make sure
1549 * that this function will never actually get called. We return a valid
1550 * space anyway, just in case.
1552 __isl_give isl_space
*isl_val_get_space(__isl_keep isl_val
*v
)
1557 return isl_space_params_alloc(isl_val_get_ctx(v
), 0);
1560 /* Reset the domain space of "v" to "space".
1562 * This function is only meant to be used in the generic isl_multi_*
1563 * functions which have to deal with base objects that have an associated
1564 * space. Since an isl_val does not have an associated space, this function
1565 * does not do anything, apart from error handling and cleaning up memory.
1567 __isl_give isl_val
*isl_val_reset_domain_space(__isl_take isl_val
*v
,
1568 __isl_take isl_space
*space
)
1571 return isl_val_free(v
);
1572 isl_space_free(space
);
1576 /* Align the parameters of "v" to those of "space".
1578 * This function is only meant to be used in the generic isl_multi_*
1579 * functions which have to deal with base objects that have an associated
1580 * space. Since an isl_val does not have an associated space, this function
1581 * does not do anything, apart from error handling and cleaning up memory.
1582 * Note that the conditions surrounding the call to this function make sure
1583 * that this function will never actually get called.
1585 __isl_give isl_val
*isl_val_align_params(__isl_take isl_val
*v
,
1586 __isl_take isl_space
*space
)
1589 return isl_val_free(v
);
1590 isl_space_free(space
);
1594 /* Reorder the dimensions of the domain of "v" according
1595 * to the given reordering.
1597 * This function is only meant to be used in the generic isl_multi_*
1598 * functions which have to deal with base objects that have an associated
1599 * space. Since an isl_val does not have an associated space, this function
1600 * does not do anything, apart from error handling and cleaning up memory.
1602 __isl_give isl_val
*isl_val_realign_domain(__isl_take isl_val
*v
,
1603 __isl_take isl_reordering
*r
)
1606 return isl_val_free(v
);
1607 isl_reordering_free(r
);
1611 /* Return an isl_val that is zero on "ls".
1613 * This function is only meant to be used in the generic isl_multi_*
1614 * functions which have to deal with base objects that have an associated
1615 * space. Since an isl_val does not have an associated space, this function
1616 * simply returns a zero isl_val in the same context as "ls".
1618 __isl_give isl_val
*isl_val_zero_on_domain(__isl_take isl_local_space
*ls
)
1624 ctx
= isl_local_space_get_ctx(ls
);
1625 isl_local_space_free(ls
);
1626 return isl_val_zero(ctx
);
1629 /* Do the parameters of "v" match those of "space"?
1631 * This function is only meant to be used in the generic isl_multi_*
1632 * functions which have to deal with base objects that have an associated
1633 * space. Since an isl_val does not have an associated space, this function
1634 * simply returns true, except if "v" or "space" are NULL.
1636 isl_bool
isl_val_matching_params(__isl_keep isl_val
*v
,
1637 __isl_keep isl_space
*space
)
1640 return isl_bool_error
;
1641 return isl_bool_true
;
1644 /* Check that the domain space of "v" matches "space".
1646 * This function is only meant to be used in the generic isl_multi_*
1647 * functions which have to deal with base objects that have an associated
1648 * space. Since an isl_val does not have an associated space, this function
1649 * simply returns 0, except if "v" or "space" are NULL.
1651 isl_stat
isl_val_check_match_domain_space(__isl_keep isl_val
*v
,
1652 __isl_keep isl_space
*space
)
1655 return isl_stat_error
;
1659 #define isl_val_involves_nan isl_val_is_nan
1666 #define NO_FROM_BASE
1667 #define NO_MOVE_DIMS
1668 #include <isl_multi_no_explicit_domain.c>
1669 #include <isl_multi_templ.c>
1670 #include <isl_multi_dims.c>
1672 /* Apply "fn" to each of the elements of "mv" with as second argument "v".
1674 static __isl_give isl_multi_val
*isl_multi_val_fn_val(
1675 __isl_take isl_multi_val
*mv
,
1676 __isl_give isl_val
*(*fn
)(__isl_take isl_val
*v1
,
1677 __isl_take isl_val
*v2
),
1678 __isl_take isl_val
*v
)
1682 mv
= isl_multi_val_cow(mv
);
1686 for (i
= 0; i
< mv
->n
; ++i
) {
1687 mv
->u
.p
[i
] = fn(mv
->u
.p
[i
], isl_val_copy(v
));
1696 isl_multi_val_free(mv
);
1700 /* Add "v" to each of the elements of "mv".
1702 __isl_give isl_multi_val
*isl_multi_val_add_val(__isl_take isl_multi_val
*mv
,
1703 __isl_take isl_val
*v
)
1706 return isl_multi_val_free(mv
);
1707 if (isl_val_is_zero(v
)) {
1711 return isl_multi_val_fn_val(mv
, &isl_val_add
, v
);
1714 /* Reduce the elements of "mv" modulo "v".
1716 __isl_give isl_multi_val
*isl_multi_val_mod_val(__isl_take isl_multi_val
*mv
,
1717 __isl_take isl_val
*v
)
1719 return isl_multi_val_fn_val(mv
, &isl_val_mod
, v
);