[clang-repl] [codegen] Reduce the state in TBAA. NFC for static compilation. (#98138)
[llvm-project.git] / polly / lib / External / isl / isl_val.c
blob8b77622d7fb2ec7eb166dd77368f236f478ef785
1 /*
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
8 */
10 #include <isl_int.h>
11 #include <isl_ctx_private.h>
12 #include <isl_val_private.h>
14 #undef EL_BASE
15 #define EL_BASE val
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)
24 isl_val *v;
26 v = isl_alloc_type(ctx, struct isl_val);
27 if (!v)
28 return NULL;
30 v->ctx = ctx;
31 isl_ctx_ref(ctx);
32 v->ref = 1;
33 isl_int_init(v->n);
34 isl_int_init(v->d);
36 return v;
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)
64 isl_val *v;
66 v = isl_val_alloc(ctx);
67 if (!v)
68 return NULL;
70 isl_int_set_si(v->n, 0);
71 isl_int_set_si(v->d, 0);
73 return v;
76 /* Change "v" into a NaN.
78 __isl_give isl_val *isl_val_set_nan(__isl_take isl_val *v)
80 if (!v)
81 return NULL;
82 if (isl_val_is_nan(v))
83 return v;
84 v = isl_val_cow(v);
85 if (!v)
86 return NULL;
88 isl_int_set_si(v->n, 0);
89 isl_int_set_si(v->d, 0);
91 return v;
94 /* Return a reference to an isl_val representing +infinity.
96 __isl_give isl_val *isl_val_infty(isl_ctx *ctx)
98 isl_val *v;
100 v = isl_val_alloc(ctx);
101 if (!v)
102 return NULL;
104 isl_int_set_si(v->n, 1);
105 isl_int_set_si(v->d, 0);
107 return v;
110 /* Return a reference to an isl_val representing -infinity.
112 __isl_give isl_val *isl_val_neginfty(isl_ctx *ctx)
114 isl_val *v;
116 v = isl_val_alloc(ctx);
117 if (!v)
118 return NULL;
120 isl_int_set_si(v->n, -1);
121 isl_int_set_si(v->d, 0);
123 return v;
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)
130 isl_val *v;
132 v = isl_val_alloc(ctx);
133 if (!v)
134 return NULL;
136 isl_int_set_si(v->n, i);
137 isl_int_set_si(v->d, 1);
139 return v;
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)
146 if (!v)
147 return NULL;
148 if (isl_val_is_int(v) && isl_int_cmp_si(v->n, i) == 0)
149 return v;
150 v = isl_val_cow(v);
151 if (!v)
152 return NULL;
154 isl_int_set_si(v->n, i);
155 isl_int_set_si(v->d, 1);
157 return v;
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)
171 isl_val *v;
173 v = isl_val_alloc(ctx);
174 if (!v)
175 return NULL;
177 isl_int_set_ui(v->n, u);
178 isl_int_set_si(v->d, 1);
180 return v;
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)
187 isl_val *v;
189 v = isl_val_alloc(ctx);
190 if (!v)
191 return NULL;
193 isl_int_set(v->n, n);
194 isl_int_set_si(v->d, 1);
196 return v;
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)
205 isl_val *v;
207 v = isl_val_alloc(ctx);
208 if (!v)
209 return NULL;
211 isl_int_set(v->n, n);
212 isl_int_set(v->d, d);
214 return v;
217 /* Return a new reference to "v".
219 __isl_give isl_val *isl_val_copy(__isl_keep isl_val *v)
221 if (!v)
222 return NULL;
224 v->ref++;
225 return v;
228 /* Return a fresh copy of "val".
230 __isl_give isl_val *isl_val_dup(__isl_keep isl_val *val)
232 isl_val *dup;
234 if (!val)
235 return NULL;
237 dup = isl_val_alloc(isl_val_get_ctx(val));
238 if (!dup)
239 return NULL;
241 isl_int_set(dup->n, val->n);
242 isl_int_set(dup->d, val->d);
244 return dup;
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)
252 if (!val)
253 return NULL;
255 if (val->ref == 1)
256 return val;
257 val->ref--;
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)
265 if (!v)
266 return NULL;
268 if (--v->ref > 0)
269 return NULL;
271 isl_ctx_deref(v->ctx);
272 isl_int_clear(v->n);
273 isl_int_clear(v->d);
274 free(v);
275 return NULL;
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)
284 if (!v)
285 return 0;
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)
301 if (!v)
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);
307 return isl_stat_ok;
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)
316 if (!v)
317 return 0;
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)
333 if (!v)
334 return NULL;
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)
345 if (!v)
346 return 0;
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)
364 uint32_t hash;
366 if (!val)
367 return 0;
369 hash = isl_hash_init();
370 hash = isl_int_hash(val->n, hash);
371 hash = isl_int_hash(val->d, hash);
373 return hash;
376 /* Normalize "v".
378 * In particular, make sure that the denominator of a rational value
379 * is positive and the numerator and denominator do not have any
380 * common divisors.
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)
387 isl_ctx *ctx;
389 if (!v)
390 return NULL;
391 if (isl_val_is_int(v))
392 return v;
393 if (!isl_val_is_rat(v))
394 return 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))
402 return v;
403 isl_int_divexact(v->n, v->n, ctx->normalize_gcd);
404 isl_int_divexact(v->d, v->d, ctx->normalize_gcd);
405 return v;
408 /* Return the opposite of "v".
410 __isl_give isl_val *isl_val_neg(__isl_take isl_val *v)
412 if (!v)
413 return NULL;
414 if (isl_val_is_nan(v))
415 return v;
416 if (isl_val_is_zero(v))
417 return v;
419 v = isl_val_cow(v);
420 if (!v)
421 return NULL;
422 isl_int_neg(v->n, v->n);
424 return v;
427 /* Return the inverse of "v".
429 __isl_give isl_val *isl_val_inv(__isl_take isl_val *v)
431 if (!v)
432 return NULL;
433 if (isl_val_is_nan(v))
434 return v;
435 if (isl_val_is_zero(v)) {
436 isl_ctx *ctx = isl_val_get_ctx(v);
437 isl_val_free(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);
442 isl_val_free(v);
443 return isl_val_zero(ctx);
446 v = isl_val_cow(v);
447 if (!v)
448 return NULL;
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)
458 if (!v)
459 return NULL;
460 if (isl_val_is_nan(v))
461 return v;
462 if (isl_val_is_nonneg(v))
463 return 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)
472 if (!v)
473 return NULL;
474 if (isl_val_is_int(v))
475 return v;
476 if (!isl_val_is_rat(v))
477 return v;
479 v = isl_val_cow(v);
480 if (!v)
481 return NULL;
482 isl_int_fdiv_q(v->n, v->n, v->d);
483 isl_int_set_si(v->d, 1);
485 return v;
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)
493 if (!v)
494 return NULL;
495 if (isl_val_is_int(v))
496 return v;
497 if (!isl_val_is_rat(v))
498 return v;
500 v = isl_val_cow(v);
501 if (!v)
502 return NULL;
503 isl_int_cdiv_q(v->n, v->n, v->d);
504 isl_int_set_si(v->d, 1);
506 return v;
509 /* Truncate "v".
510 * That is, return the result of rounding towards zero.
512 __isl_give isl_val *isl_val_trunc(__isl_take isl_val *v)
514 if (!v)
515 return NULL;
516 if (isl_val_is_int(v))
517 return v;
518 if (!isl_val_is_rat(v))
519 return v;
521 v = isl_val_cow(v);
522 if (!v)
523 return NULL;
524 isl_int_tdiv_q(v->n, v->n, v->d);
525 isl_int_set_si(v->d, 1);
527 return v;
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)
534 unsigned long exp;
535 int neg;
537 v = isl_val_cow(v);
538 if (!v)
539 return NULL;
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);
545 if (neg)
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);
551 if (neg) {
552 isl_int_mul_2exp(v->d, v->d, exp);
553 isl_int_set_si(v->n, 1);
554 } else {
555 isl_int_mul_2exp(v->n, v->d, exp);
558 return v;
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)
572 if (!v1 || !v2)
573 goto error;
575 if (isl_val_is_nan(v1)) {
576 isl_val_free(v2);
577 return v1;
579 if (isl_val_is_nan(v2)) {
580 isl_val_free(v1);
581 return v2;
583 if (isl_val_le(v1, v2)) {
584 isl_val_free(v2);
585 return v1;
586 } else {
587 isl_val_free(v1);
588 return v2;
590 error:
591 isl_val_free(v1);
592 isl_val_free(v2);
593 return NULL;
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)
600 if (!v1 || !v2)
601 goto error;
603 if (isl_val_is_nan(v1)) {
604 isl_val_free(v2);
605 return v1;
607 if (isl_val_is_nan(v2)) {
608 isl_val_free(v1);
609 return v2;
611 if (isl_val_ge(v1, v2)) {
612 isl_val_free(v2);
613 return v1;
614 } else {
615 isl_val_free(v1);
616 return v2;
618 error:
619 isl_val_free(v1);
620 isl_val_free(v2);
621 return NULL;
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)
628 if (!v1 || !v2)
629 goto error;
630 if (isl_val_is_nan(v1)) {
631 isl_val_free(v2);
632 return v1;
634 if (isl_val_is_nan(v2)) {
635 isl_val_free(v1);
636 return v2;
638 if ((isl_val_is_infty(v1) && isl_val_is_neginfty(v2)) ||
639 (isl_val_is_neginfty(v1) && isl_val_is_infty(v2))) {
640 isl_val_free(v2);
641 return isl_val_set_nan(v1);
643 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
644 isl_val_free(v2);
645 return v1;
647 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
648 isl_val_free(v1);
649 return v2;
651 if (isl_val_is_zero(v1)) {
652 isl_val_free(v1);
653 return v2;
655 if (isl_val_is_zero(v2)) {
656 isl_val_free(v2);
657 return v1;
660 v1 = isl_val_cow(v1);
661 if (!v1)
662 goto error;
663 if (isl_val_is_int(v1) && isl_val_is_int(v2))
664 isl_int_add(v1->n, v1->n, v2->n);
665 else {
666 if (isl_int_eq(v1->d, v2->d))
667 isl_int_add(v1->n, v1->n, v2->n);
668 else {
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);
675 isl_val_free(v2);
676 return v1;
677 error:
678 isl_val_free(v1);
679 isl_val_free(v2);
680 return NULL;
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)
687 if (!v1)
688 return NULL;
689 if (!isl_val_is_rat(v1))
690 return v1;
691 if (v2 == 0)
692 return v1;
693 v1 = isl_val_cow(v1);
694 if (!v1)
695 return NULL;
697 isl_int_addmul_ui(v1->n, v1->d, v2);
699 return v1;
702 /* Subtract "v2" from "v1".
704 __isl_give isl_val *isl_val_sub(__isl_take isl_val *v1, __isl_take isl_val *v2)
706 if (!v1 || !v2)
707 goto error;
708 if (isl_val_is_nan(v1)) {
709 isl_val_free(v2);
710 return v1;
712 if (isl_val_is_nan(v2)) {
713 isl_val_free(v1);
714 return v2;
716 if ((isl_val_is_infty(v1) && isl_val_is_infty(v2)) ||
717 (isl_val_is_neginfty(v1) && isl_val_is_neginfty(v2))) {
718 isl_val_free(v2);
719 return isl_val_set_nan(v1);
721 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
722 isl_val_free(v2);
723 return v1;
725 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
726 isl_val_free(v1);
727 return isl_val_neg(v2);
729 if (isl_val_is_zero(v2)) {
730 isl_val_free(v2);
731 return v1;
733 if (isl_val_is_zero(v1)) {
734 isl_val_free(v1);
735 return isl_val_neg(v2);
738 v1 = isl_val_cow(v1);
739 if (!v1)
740 goto error;
741 if (isl_val_is_int(v1) && isl_val_is_int(v2))
742 isl_int_sub(v1->n, v1->n, v2->n);
743 else {
744 if (isl_int_eq(v1->d, v2->d))
745 isl_int_sub(v1->n, v1->n, v2->n);
746 else {
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);
753 isl_val_free(v2);
754 return v1;
755 error:
756 isl_val_free(v1);
757 isl_val_free(v2);
758 return NULL;
761 /* Subtract "v2" from "v1".
763 __isl_give isl_val *isl_val_sub_ui(__isl_take isl_val *v1, unsigned long v2)
765 if (!v1)
766 return NULL;
767 if (!isl_val_is_rat(v1))
768 return v1;
769 if (v2 == 0)
770 return v1;
771 v1 = isl_val_cow(v1);
772 if (!v1)
773 return NULL;
775 isl_int_submul_ui(v1->n, v1->d, v2);
777 return v1;
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)
784 if (!v1 || !v2)
785 goto error;
786 if (isl_val_is_nan(v1)) {
787 isl_val_free(v2);
788 return v1;
790 if (isl_val_is_nan(v2)) {
791 isl_val_free(v1);
792 return v2;
794 if ((!isl_val_is_rat(v1) && isl_val_is_zero(v2)) ||
795 (isl_val_is_zero(v1) && !isl_val_is_rat(v2))) {
796 isl_val_free(v2);
797 return isl_val_set_nan(v1);
799 if (isl_val_is_zero(v1)) {
800 isl_val_free(v2);
801 return v1;
803 if (isl_val_is_zero(v2)) {
804 isl_val_free(v1);
805 return 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);
810 isl_val_free(v2);
811 return 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);
816 isl_val_free(v1);
817 return v2;
820 v1 = isl_val_cow(v1);
821 if (!v1)
822 goto error;
823 if (isl_val_is_int(v1) && isl_val_is_int(v2))
824 isl_int_mul(v1->n, v1->n, v2->n);
825 else {
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);
830 isl_val_free(v2);
831 return v1;
832 error:
833 isl_val_free(v1);
834 isl_val_free(v2);
835 return NULL;
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)
853 if (!v1)
854 return NULL;
855 if (isl_val_is_nan(v1))
856 return v1;
857 if (!isl_val_is_rat(v1)) {
858 if (v2 == 0)
859 v1 = isl_val_set_nan(v1);
860 return v1;
862 if (v2 == 1)
863 return v1;
864 v1 = isl_val_cow(v1);
865 if (!v1)
866 return NULL;
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)
877 if (!v1 || !v2)
878 goto error;
879 if (isl_val_is_nan(v1)) {
880 isl_val_free(v2);
881 return v1;
883 if (isl_val_is_nan(v2)) {
884 isl_val_free(v1);
885 return v2;
887 if (isl_val_is_zero(v2) ||
888 (!isl_val_is_rat(v1) && !isl_val_is_rat(v2))) {
889 isl_val_free(v2);
890 return isl_val_set_nan(v1);
892 if (isl_val_is_zero(v1)) {
893 isl_val_free(v2);
894 return 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);
899 isl_val_free(v2);
900 return v1;
902 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
903 isl_val_free(v2);
904 return isl_val_set_zero(v1);
907 v1 = isl_val_cow(v1);
908 if (!v1)
909 goto error;
910 if (isl_val_is_int(v2)) {
911 isl_int_mul(v1->d, v1->d, v2->n);
912 v1 = isl_val_normalize(v1);
913 } else {
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);
918 isl_val_free(v2);
919 return v1;
920 error:
921 isl_val_free(v1);
922 isl_val_free(v2);
923 return NULL;
926 /* Divide "v1" by "v2".
928 __isl_give isl_val *isl_val_div_ui(__isl_take isl_val *v1, unsigned long v2)
930 if (!v1)
931 return NULL;
932 if (isl_val_is_nan(v1))
933 return v1;
934 if (v2 == 0)
935 return isl_val_set_nan(v1);
936 if (v2 == 1)
937 return v1;
938 if (isl_val_is_zero(v1))
939 return v1;
940 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1))
941 return v1;
942 v1 = isl_val_cow(v1);
943 if (!v1)
944 return NULL;
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)
966 if (!v1 || !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"
977 * modulo "v2".
979 __isl_give isl_val *isl_val_mod(__isl_take isl_val *v1, __isl_take isl_val *v2)
981 if (!v1 || !v2)
982 goto error;
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)) {
987 isl_val_free(v2);
988 return v1;
990 v1 = isl_val_cow(v1);
991 if (!v1)
992 goto error;
993 isl_int_fdiv_r(v1->n, v1->n, v2->n);
994 isl_val_free(v2);
995 return v1;
996 error:
997 isl_val_free(v1);
998 isl_val_free(v2);
999 return NULL;
1002 /* Given two integer values "v1" and "v2", return the residue of "v1"
1003 * modulo "v2".
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)
1018 if (!v1 || !v2)
1019 goto error;
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)) {
1024 isl_val_free(v2);
1025 return v1;
1027 if (isl_val_is_one(v1)) {
1028 isl_val_free(v2);
1029 return v1;
1031 if (isl_val_is_one(v2)) {
1032 isl_val_free(v1);
1033 return v2;
1035 v1 = isl_val_cow(v1);
1036 if (!v1)
1037 goto error;
1038 isl_int_gcd(v1->n, v1->n, v2->n);
1039 isl_val_free(v2);
1040 return v1;
1041 error:
1042 isl_val_free(v1);
1043 isl_val_free(v2);
1044 return NULL;
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)
1052 isl_int d, tmp;
1053 isl_int a_copy, b_copy;
1055 isl_int_init(a_copy);
1056 isl_int_init(b_copy);
1057 isl_int_init(d);
1058 isl_int_init(tmp);
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);
1078 else {
1079 isl_int_mul(tmp, a_copy, *x);
1080 isl_int_sub(tmp, *g, tmp);
1081 isl_int_divexact(*y, tmp, b_copy);
1083 isl_int_clear(d);
1084 isl_int_clear(tmp);
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)
1095 isl_ctx *ctx;
1096 isl_val *a = NULL, *b = NULL;
1098 if (!x && !y)
1099 return isl_val_gcd(v1, v2);
1101 if (!v1 || !v2)
1102 goto error;
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)
1113 goto error;
1114 isl_int_gcdext(&v1->n, &a->n, &b->n, v1->n, v2->n);
1115 if (x) {
1116 isl_int_set_si(a->d, 1);
1117 *x = a;
1118 } else
1119 isl_val_free(a);
1120 if (y) {
1121 isl_int_set_si(b->d, 1);
1122 *y = b;
1123 } else
1124 isl_val_free(b);
1125 isl_val_free(v2);
1126 return v1;
1127 error:
1128 isl_val_free(v1);
1129 isl_val_free(v2);
1130 isl_val_free(a);
1131 isl_val_free(b);
1132 if (x)
1133 *x = NULL;
1134 if (y)
1135 *y = NULL;
1136 return NULL;
1139 /* Does "v" represent an integer value?
1141 isl_bool isl_val_is_int(__isl_keep isl_val *v)
1143 if (!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)
1153 if (!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)
1163 if (!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)
1173 if (!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)
1183 if (!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)
1193 if (!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)
1203 if (!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)
1216 if (!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)
1226 if (!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)
1236 if (!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)
1246 if (!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)
1259 if (!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)
1274 if (!v)
1275 return 0;
1276 if (isl_val_is_zero(v))
1277 return 0;
1278 if (isl_val_is_pos(v))
1279 return 1;
1280 return -1;
1283 /* Is "v1" (strictly) less than "v2"?
1285 isl_bool isl_val_lt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1287 isl_int t;
1288 isl_bool lt;
1290 if (!v1 || !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;
1307 isl_int_init(t);
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));
1311 isl_int_clear(t);
1313 return lt;
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)
1327 isl_val *vi;
1328 isl_bool res;
1330 if (!v)
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));
1343 isl_val_free(vi);
1345 return res;
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)
1352 isl_int t;
1353 isl_bool le;
1355 if (!v1 || !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;
1372 isl_int_init(t);
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));
1376 isl_int_clear(t);
1378 return le;
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)
1396 isl_int t;
1397 int cmp;
1399 if (!v)
1400 return 0;
1401 if (isl_val_is_int(v))
1402 return isl_int_cmp_si(v->n, i);
1403 if (isl_val_is_nan(v))
1404 return 0;
1405 if (isl_val_is_infty(v))
1406 return 1;
1407 if (isl_val_is_neginfty(v))
1408 return -1;
1410 isl_int_init(t);
1411 isl_int_mul_si(t, v->d, i);
1412 isl_int_sub(t, v->n, t);
1413 cmp = isl_int_sgn(t);
1414 isl_int_clear(t);
1416 return cmp;
1419 /* Is "v1" equal to "v2"?
1421 isl_bool isl_val_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1423 if (!v1 || !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)
1436 if (!v)
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)
1447 if (!v1 || !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)
1460 if (!v1 || !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)
1474 int neg;
1476 if (!p || !v)
1477 return isl_printer_free(p);
1479 neg = isl_int_is_neg(v->n);
1480 if (neg) {
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");
1488 } else
1489 p = isl_printer_print_isl_int(p, v->n);
1490 if (neg)
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);
1497 return p;
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)
1522 if (!v)
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)
1538 return v;
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)
1551 return v;
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)
1563 isl_ctx *ctx;
1565 if (!ls)
1566 return NULL;
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
1574 #undef BASE
1575 #define BASE val
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)
1604 if (!v)
1605 return isl_multi_val_free(mv);
1606 if (isl_val_is_zero(v)) {
1607 isl_val_free(v);
1608 return mv;
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);