isl_union_*_drop_dims: use isl_union_*_transform_space
[isl.git] / isl_val.c
blobb63076128676282c990d100a7feb6944ab30cb20
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 BASE
15 #define BASE val
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)
23 isl_val *v;
25 v = isl_alloc_type(ctx, struct isl_val);
26 if (!v)
27 return NULL;
29 v->ctx = ctx;
30 isl_ctx_ref(ctx);
31 v->ref = 1;
32 isl_int_init(v->n);
33 isl_int_init(v->d);
35 return v;
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)
63 isl_val *v;
65 v = isl_val_alloc(ctx);
66 if (!v)
67 return NULL;
69 isl_int_set_si(v->n, 0);
70 isl_int_set_si(v->d, 0);
72 return v;
75 /* Change "v" into a NaN.
77 __isl_give isl_val *isl_val_set_nan(__isl_take isl_val *v)
79 if (!v)
80 return NULL;
81 if (isl_val_is_nan(v))
82 return v;
83 v = isl_val_cow(v);
84 if (!v)
85 return NULL;
87 isl_int_set_si(v->n, 0);
88 isl_int_set_si(v->d, 0);
90 return v;
93 /* Return a reference to an isl_val representing +infinity.
95 __isl_give isl_val *isl_val_infty(isl_ctx *ctx)
97 isl_val *v;
99 v = isl_val_alloc(ctx);
100 if (!v)
101 return NULL;
103 isl_int_set_si(v->n, 1);
104 isl_int_set_si(v->d, 0);
106 return v;
109 /* Return a reference to an isl_val representing -infinity.
111 __isl_give isl_val *isl_val_neginfty(isl_ctx *ctx)
113 isl_val *v;
115 v = isl_val_alloc(ctx);
116 if (!v)
117 return NULL;
119 isl_int_set_si(v->n, -1);
120 isl_int_set_si(v->d, 0);
122 return v;
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)
129 isl_val *v;
131 v = isl_val_alloc(ctx);
132 if (!v)
133 return NULL;
135 isl_int_set_si(v->n, i);
136 isl_int_set_si(v->d, 1);
138 return v;
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)
145 if (!v)
146 return NULL;
147 if (isl_val_is_int(v) && isl_int_cmp_si(v->n, i) == 0)
148 return v;
149 v = isl_val_cow(v);
150 if (!v)
151 return NULL;
153 isl_int_set_si(v->n, i);
154 isl_int_set_si(v->d, 1);
156 return v;
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)
170 isl_val *v;
172 v = isl_val_alloc(ctx);
173 if (!v)
174 return NULL;
176 isl_int_set_ui(v->n, u);
177 isl_int_set_si(v->d, 1);
179 return v;
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)
186 isl_val *v;
188 v = isl_val_alloc(ctx);
189 if (!v)
190 return NULL;
192 isl_int_set(v->n, n);
193 isl_int_set_si(v->d, 1);
195 return v;
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)
204 isl_val *v;
206 v = isl_val_alloc(ctx);
207 if (!v)
208 return NULL;
210 isl_int_set(v->n, n);
211 isl_int_set(v->d, d);
213 return v;
216 /* Return a new reference to "v".
218 __isl_give isl_val *isl_val_copy(__isl_keep isl_val *v)
220 if (!v)
221 return NULL;
223 v->ref++;
224 return v;
227 /* Return a fresh copy of "val".
229 __isl_give isl_val *isl_val_dup(__isl_keep isl_val *val)
231 isl_val *dup;
233 if (!val)
234 return NULL;
236 dup = isl_val_alloc(isl_val_get_ctx(val));
237 if (!dup)
238 return NULL;
240 isl_int_set(dup->n, val->n);
241 isl_int_set(dup->d, val->d);
243 return dup;
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)
251 if (!val)
252 return NULL;
254 if (val->ref == 1)
255 return val;
256 val->ref--;
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)
264 if (!v)
265 return NULL;
267 if (--v->ref > 0)
268 return NULL;
270 isl_ctx_deref(v->ctx);
271 isl_int_clear(v->n);
272 isl_int_clear(v->d);
273 free(v);
274 return NULL;
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)
283 if (!v)
284 return 0;
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)
300 if (!v)
301 return -1;
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);
306 return 0;
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)
315 if (!v)
316 return 0;
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)
332 if (!v)
333 return NULL;
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)
344 if (!v)
345 return 0;
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 /* Normalize "v".
361 * In particular, make sure that the denominator of a rational value
362 * is positive and the numerator and denominator do not have any
363 * common divisors.
365 * This function should not be called by an external user
366 * since it will only be given normalized values.
368 __isl_give isl_val *isl_val_normalize(__isl_take isl_val *v)
370 isl_ctx *ctx;
372 if (!v)
373 return NULL;
374 if (isl_val_is_int(v))
375 return v;
376 if (!isl_val_is_rat(v))
377 return v;
378 if (isl_int_is_neg(v->d)) {
379 isl_int_neg(v->d, v->d);
380 isl_int_neg(v->n, v->n);
382 ctx = isl_val_get_ctx(v);
383 isl_int_gcd(ctx->normalize_gcd, v->n, v->d);
384 if (isl_int_is_one(ctx->normalize_gcd))
385 return v;
386 isl_int_divexact(v->n, v->n, ctx->normalize_gcd);
387 isl_int_divexact(v->d, v->d, ctx->normalize_gcd);
388 return v;
391 /* Return the opposite of "v".
393 __isl_give isl_val *isl_val_neg(__isl_take isl_val *v)
395 if (!v)
396 return NULL;
397 if (isl_val_is_nan(v))
398 return v;
399 if (isl_val_is_zero(v))
400 return v;
402 v = isl_val_cow(v);
403 if (!v)
404 return NULL;
405 isl_int_neg(v->n, v->n);
407 return v;
410 /* Return the inverse of "v".
412 __isl_give isl_val *isl_val_inv(__isl_take isl_val *v)
414 if (!v)
415 return NULL;
416 if (isl_val_is_nan(v))
417 return v;
418 if (isl_val_is_zero(v)) {
419 isl_ctx *ctx = isl_val_get_ctx(v);
420 isl_val_free(v);
421 return isl_val_nan(ctx);
423 if (isl_val_is_infty(v) || isl_val_is_neginfty(v)) {
424 isl_ctx *ctx = isl_val_get_ctx(v);
425 isl_val_free(v);
426 return isl_val_zero(ctx);
429 v = isl_val_cow(v);
430 if (!v)
431 return NULL;
432 isl_int_swap(v->n, v->d);
434 return isl_val_normalize(v);
437 /* Return the absolute value of "v".
439 __isl_give isl_val *isl_val_abs(__isl_take isl_val *v)
441 if (!v)
442 return NULL;
443 if (isl_val_is_nan(v))
444 return v;
445 if (isl_val_is_nonneg(v))
446 return v;
447 return isl_val_neg(v);
450 /* Return the "floor" (greatest integer part) of "v".
451 * That is, return the result of rounding towards -infinity.
453 __isl_give isl_val *isl_val_floor(__isl_take isl_val *v)
455 if (!v)
456 return NULL;
457 if (isl_val_is_int(v))
458 return v;
459 if (!isl_val_is_rat(v))
460 return v;
462 v = isl_val_cow(v);
463 if (!v)
464 return NULL;
465 isl_int_fdiv_q(v->n, v->n, v->d);
466 isl_int_set_si(v->d, 1);
468 return v;
471 /* Return the "ceiling" of "v".
472 * That is, return the result of rounding towards +infinity.
474 __isl_give isl_val *isl_val_ceil(__isl_take isl_val *v)
476 if (!v)
477 return NULL;
478 if (isl_val_is_int(v))
479 return v;
480 if (!isl_val_is_rat(v))
481 return v;
483 v = isl_val_cow(v);
484 if (!v)
485 return NULL;
486 isl_int_cdiv_q(v->n, v->n, v->d);
487 isl_int_set_si(v->d, 1);
489 return v;
492 /* Truncate "v".
493 * That is, return the result of rounding towards zero.
495 __isl_give isl_val *isl_val_trunc(__isl_take isl_val *v)
497 if (!v)
498 return NULL;
499 if (isl_val_is_int(v))
500 return v;
501 if (!isl_val_is_rat(v))
502 return v;
504 v = isl_val_cow(v);
505 if (!v)
506 return NULL;
507 isl_int_tdiv_q(v->n, v->n, v->d);
508 isl_int_set_si(v->d, 1);
510 return v;
513 /* Return 2^v, where v is an integer (that is not too large).
515 __isl_give isl_val *isl_val_2exp(__isl_take isl_val *v)
517 unsigned long exp;
518 int neg;
520 v = isl_val_cow(v);
521 if (!v)
522 return NULL;
523 if (!isl_val_is_int(v))
524 isl_die(isl_val_get_ctx(v), isl_error_invalid,
525 "can only compute integer powers",
526 return isl_val_free(v));
527 neg = isl_val_is_neg(v);
528 if (neg)
529 isl_int_neg(v->n, v->n);
530 if (!isl_int_fits_ulong(v->n))
531 isl_die(isl_val_get_ctx(v), isl_error_invalid,
532 "exponent too large", return isl_val_free(v));
533 exp = isl_int_get_ui(v->n);
534 if (neg) {
535 isl_int_mul_2exp(v->d, v->d, exp);
536 isl_int_set_si(v->n, 1);
537 } else {
538 isl_int_mul_2exp(v->n, v->d, exp);
541 return v;
544 /* Return the minimum of "v1" and "v2".
546 __isl_give isl_val *isl_val_min(__isl_take isl_val *v1, __isl_take isl_val *v2)
548 if (!v1 || !v2)
549 goto error;
551 if (isl_val_is_nan(v1)) {
552 isl_val_free(v2);
553 return v1;
555 if (isl_val_is_nan(v2)) {
556 isl_val_free(v1);
557 return v2;
559 if (isl_val_le(v1, v2)) {
560 isl_val_free(v2);
561 return v1;
562 } else {
563 isl_val_free(v1);
564 return v2;
566 error:
567 isl_val_free(v1);
568 isl_val_free(v2);
569 return NULL;
572 /* Return the maximum of "v1" and "v2".
574 __isl_give isl_val *isl_val_max(__isl_take isl_val *v1, __isl_take isl_val *v2)
576 if (!v1 || !v2)
577 goto error;
579 if (isl_val_is_nan(v1)) {
580 isl_val_free(v2);
581 return v1;
583 if (isl_val_is_nan(v2)) {
584 isl_val_free(v1);
585 return v2;
587 if (isl_val_ge(v1, v2)) {
588 isl_val_free(v2);
589 return v1;
590 } else {
591 isl_val_free(v1);
592 return v2;
594 error:
595 isl_val_free(v1);
596 isl_val_free(v2);
597 return NULL;
600 /* Return the sum of "v1" and "v2".
602 __isl_give isl_val *isl_val_add(__isl_take isl_val *v1, __isl_take isl_val *v2)
604 if (!v1 || !v2)
605 goto error;
606 if (isl_val_is_nan(v1)) {
607 isl_val_free(v2);
608 return v1;
610 if (isl_val_is_nan(v2)) {
611 isl_val_free(v1);
612 return v2;
614 if ((isl_val_is_infty(v1) && isl_val_is_neginfty(v2)) ||
615 (isl_val_is_neginfty(v1) && isl_val_is_infty(v2))) {
616 isl_val_free(v2);
617 return isl_val_set_nan(v1);
619 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
620 isl_val_free(v2);
621 return v1;
623 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
624 isl_val_free(v1);
625 return v2;
627 if (isl_val_is_zero(v1)) {
628 isl_val_free(v1);
629 return v2;
631 if (isl_val_is_zero(v2)) {
632 isl_val_free(v2);
633 return v1;
636 v1 = isl_val_cow(v1);
637 if (!v1)
638 goto error;
639 if (isl_val_is_int(v1) && isl_val_is_int(v2))
640 isl_int_add(v1->n, v1->n, v2->n);
641 else {
642 if (isl_int_eq(v1->d, v2->d))
643 isl_int_add(v1->n, v1->n, v2->n);
644 else {
645 isl_int_mul(v1->n, v1->n, v2->d);
646 isl_int_addmul(v1->n, v2->n, v1->d);
647 isl_int_mul(v1->d, v1->d, v2->d);
649 v1 = isl_val_normalize(v1);
651 isl_val_free(v2);
652 return v1;
653 error:
654 isl_val_free(v1);
655 isl_val_free(v2);
656 return NULL;
659 /* Return the sum of "v1" and "v2".
661 __isl_give isl_val *isl_val_add_ui(__isl_take isl_val *v1, unsigned long v2)
663 if (!v1)
664 return NULL;
665 if (!isl_val_is_rat(v1))
666 return v1;
667 if (v2 == 0)
668 return v1;
669 v1 = isl_val_cow(v1);
670 if (!v1)
671 return NULL;
673 isl_int_addmul_ui(v1->n, v1->d, v2);
675 return v1;
678 /* Subtract "v2" from "v1".
680 __isl_give isl_val *isl_val_sub(__isl_take isl_val *v1, __isl_take isl_val *v2)
682 if (!v1 || !v2)
683 goto error;
684 if (isl_val_is_nan(v1)) {
685 isl_val_free(v2);
686 return v1;
688 if (isl_val_is_nan(v2)) {
689 isl_val_free(v1);
690 return v2;
692 if ((isl_val_is_infty(v1) && isl_val_is_infty(v2)) ||
693 (isl_val_is_neginfty(v1) && isl_val_is_neginfty(v2))) {
694 isl_val_free(v2);
695 return isl_val_set_nan(v1);
697 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
698 isl_val_free(v2);
699 return v1;
701 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
702 isl_val_free(v1);
703 return isl_val_neg(v2);
705 if (isl_val_is_zero(v2)) {
706 isl_val_free(v2);
707 return v1;
709 if (isl_val_is_zero(v1)) {
710 isl_val_free(v1);
711 return isl_val_neg(v2);
714 v1 = isl_val_cow(v1);
715 if (!v1)
716 goto error;
717 if (isl_val_is_int(v1) && isl_val_is_int(v2))
718 isl_int_sub(v1->n, v1->n, v2->n);
719 else {
720 if (isl_int_eq(v1->d, v2->d))
721 isl_int_sub(v1->n, v1->n, v2->n);
722 else {
723 isl_int_mul(v1->n, v1->n, v2->d);
724 isl_int_submul(v1->n, v2->n, v1->d);
725 isl_int_mul(v1->d, v1->d, v2->d);
727 v1 = isl_val_normalize(v1);
729 isl_val_free(v2);
730 return v1;
731 error:
732 isl_val_free(v1);
733 isl_val_free(v2);
734 return NULL;
737 /* Subtract "v2" from "v1".
739 __isl_give isl_val *isl_val_sub_ui(__isl_take isl_val *v1, unsigned long v2)
741 if (!v1)
742 return NULL;
743 if (!isl_val_is_rat(v1))
744 return v1;
745 if (v2 == 0)
746 return v1;
747 v1 = isl_val_cow(v1);
748 if (!v1)
749 return NULL;
751 isl_int_submul_ui(v1->n, v1->d, v2);
753 return v1;
756 /* Return the product of "v1" and "v2".
758 __isl_give isl_val *isl_val_mul(__isl_take isl_val *v1, __isl_take isl_val *v2)
760 if (!v1 || !v2)
761 goto error;
762 if (isl_val_is_nan(v1)) {
763 isl_val_free(v2);
764 return v1;
766 if (isl_val_is_nan(v2)) {
767 isl_val_free(v1);
768 return v2;
770 if ((!isl_val_is_rat(v1) && isl_val_is_zero(v2)) ||
771 (isl_val_is_zero(v1) && !isl_val_is_rat(v2))) {
772 isl_val_free(v2);
773 return isl_val_set_nan(v1);
775 if (isl_val_is_zero(v1)) {
776 isl_val_free(v2);
777 return v1;
779 if (isl_val_is_zero(v2)) {
780 isl_val_free(v1);
781 return v2;
783 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
784 if (isl_val_is_neg(v2))
785 v1 = isl_val_neg(v1);
786 isl_val_free(v2);
787 return v1;
789 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
790 if (isl_val_is_neg(v1))
791 v2 = isl_val_neg(v2);
792 isl_val_free(v1);
793 return v2;
796 v1 = isl_val_cow(v1);
797 if (!v1)
798 goto error;
799 if (isl_val_is_int(v1) && isl_val_is_int(v2))
800 isl_int_mul(v1->n, v1->n, v2->n);
801 else {
802 isl_int_mul(v1->n, v1->n, v2->n);
803 isl_int_mul(v1->d, v1->d, v2->d);
804 v1 = isl_val_normalize(v1);
806 isl_val_free(v2);
807 return v1;
808 error:
809 isl_val_free(v1);
810 isl_val_free(v2);
811 return NULL;
814 /* Return the product of "v1" and "v2".
816 * This is a private copy of isl_val_mul for use in the generic
817 * isl_multi_*_scale_val instantiated for isl_val.
819 __isl_give isl_val *isl_val_scale_val(__isl_take isl_val *v1,
820 __isl_take isl_val *v2)
822 return isl_val_mul(v1, v2);
825 /* Return the product of "v1" and "v2".
827 __isl_give isl_val *isl_val_mul_ui(__isl_take isl_val *v1, unsigned long v2)
829 if (!v1)
830 return NULL;
831 if (isl_val_is_nan(v1))
832 return v1;
833 if (!isl_val_is_rat(v1)) {
834 if (v2 == 0)
835 v1 = isl_val_set_nan(v1);
836 return v1;
838 if (v2 == 1)
839 return v1;
840 v1 = isl_val_cow(v1);
841 if (!v1)
842 return NULL;
844 isl_int_mul_ui(v1->n, v1->n, v2);
846 return isl_val_normalize(v1);
849 /* Divide "v1" by "v2".
851 __isl_give isl_val *isl_val_div(__isl_take isl_val *v1, __isl_take isl_val *v2)
853 if (!v1 || !v2)
854 goto error;
855 if (isl_val_is_nan(v1)) {
856 isl_val_free(v2);
857 return v1;
859 if (isl_val_is_nan(v2)) {
860 isl_val_free(v1);
861 return v2;
863 if (isl_val_is_zero(v2) ||
864 (!isl_val_is_rat(v1) && !isl_val_is_rat(v2))) {
865 isl_val_free(v2);
866 return isl_val_set_nan(v1);
868 if (isl_val_is_zero(v1)) {
869 isl_val_free(v2);
870 return v1;
872 if (isl_val_is_infty(v1) || isl_val_is_neginfty(v1)) {
873 if (isl_val_is_neg(v2))
874 v1 = isl_val_neg(v1);
875 isl_val_free(v2);
876 return v1;
878 if (isl_val_is_infty(v2) || isl_val_is_neginfty(v2)) {
879 isl_val_free(v2);
880 return isl_val_set_zero(v1);
883 v1 = isl_val_cow(v1);
884 if (!v1)
885 goto error;
886 if (isl_val_is_int(v2)) {
887 isl_int_mul(v1->d, v1->d, v2->n);
888 v1 = isl_val_normalize(v1);
889 } else {
890 isl_int_mul(v1->d, v1->d, v2->n);
891 isl_int_mul(v1->n, v1->n, v2->d);
892 v1 = isl_val_normalize(v1);
894 isl_val_free(v2);
895 return v1;
896 error:
897 isl_val_free(v1);
898 isl_val_free(v2);
899 return NULL;
902 /* Divide "v1" by "v2".
904 * This is a private copy of isl_val_div for use in the generic
905 * isl_multi_*_scale_down_val instantiated for isl_val.
907 __isl_give isl_val *isl_val_scale_down_val(__isl_take isl_val *v1,
908 __isl_take isl_val *v2)
910 return isl_val_div(v1, v2);
913 /* Given two integer values "v1" and "v2", check if "v1" is divisible by "v2".
915 isl_bool isl_val_is_divisible_by(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
917 if (!v1 || !v2)
918 return isl_bool_error;
920 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
921 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
922 "expecting two integers", return isl_bool_error);
924 return isl_int_is_divisible_by(v1->n, v2->n);
927 /* Given two integer values "v1" and "v2", return the residue of "v1"
928 * modulo "v2".
930 __isl_give isl_val *isl_val_mod(__isl_take isl_val *v1, __isl_take isl_val *v2)
932 if (!v1 || !v2)
933 goto error;
934 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
935 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
936 "expecting two integers", goto error);
937 if (isl_val_is_nonneg(v1) && isl_val_lt(v1, v2)) {
938 isl_val_free(v2);
939 return v1;
941 v1 = isl_val_cow(v1);
942 if (!v1)
943 goto error;
944 isl_int_fdiv_r(v1->n, v1->n, v2->n);
945 isl_val_free(v2);
946 return v1;
947 error:
948 isl_val_free(v1);
949 isl_val_free(v2);
950 return NULL;
953 /* Given two integer values "v1" and "v2", return the residue of "v1"
954 * modulo "v2".
956 * This is a private copy of isl_val_mod for use in the generic
957 * isl_multi_*_mod_multi_val instantiated for isl_val.
959 __isl_give isl_val *isl_val_mod_val(__isl_take isl_val *v1,
960 __isl_take isl_val *v2)
962 return isl_val_mod(v1, v2);
965 /* Given two integer values, return their greatest common divisor.
967 __isl_give isl_val *isl_val_gcd(__isl_take isl_val *v1, __isl_take isl_val *v2)
969 if (!v1 || !v2)
970 goto error;
971 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
972 isl_die(isl_val_get_ctx(v1), isl_error_invalid,
973 "expecting two integers", goto error);
974 if (isl_val_eq(v1, v2)) {
975 isl_val_free(v2);
976 return v1;
978 if (isl_val_is_one(v1)) {
979 isl_val_free(v2);
980 return v1;
982 if (isl_val_is_one(v2)) {
983 isl_val_free(v1);
984 return v2;
986 v1 = isl_val_cow(v1);
987 if (!v1)
988 goto error;
989 isl_int_gcd(v1->n, v1->n, v2->n);
990 isl_val_free(v2);
991 return v1;
992 error:
993 isl_val_free(v1);
994 isl_val_free(v2);
995 return NULL;
998 /* Compute x, y and g such that g = gcd(a,b) and a*x+b*y = g.
1000 static void isl_int_gcdext(isl_int *g, isl_int *x, isl_int *y,
1001 isl_int a, isl_int b)
1003 isl_int d, tmp;
1004 isl_int a_copy, b_copy;
1006 isl_int_init(a_copy);
1007 isl_int_init(b_copy);
1008 isl_int_init(d);
1009 isl_int_init(tmp);
1010 isl_int_set(a_copy, a);
1011 isl_int_set(b_copy, b);
1012 isl_int_abs(*g, a_copy);
1013 isl_int_abs(d, b_copy);
1014 isl_int_set_si(*x, 1);
1015 isl_int_set_si(*y, 0);
1016 while (isl_int_is_pos(d)) {
1017 isl_int_fdiv_q(tmp, *g, d);
1018 isl_int_submul(*x, tmp, *y);
1019 isl_int_submul(*g, tmp, d);
1020 isl_int_swap(*g, d);
1021 isl_int_swap(*x, *y);
1023 if (isl_int_is_zero(a_copy))
1024 isl_int_set_si(*x, 0);
1025 else if (isl_int_is_neg(a_copy))
1026 isl_int_neg(*x, *x);
1027 if (isl_int_is_zero(b_copy))
1028 isl_int_set_si(*y, 0);
1029 else {
1030 isl_int_mul(tmp, a_copy, *x);
1031 isl_int_sub(tmp, *g, tmp);
1032 isl_int_divexact(*y, tmp, b_copy);
1034 isl_int_clear(d);
1035 isl_int_clear(tmp);
1036 isl_int_clear(a_copy);
1037 isl_int_clear(b_copy);
1040 /* Given two integer values v1 and v2, return their greatest common divisor g,
1041 * as well as two integers x and y such that x * v1 + y * v2 = g.
1043 __isl_give isl_val *isl_val_gcdext(__isl_take isl_val *v1,
1044 __isl_take isl_val *v2, __isl_give isl_val **x, __isl_give isl_val **y)
1046 isl_ctx *ctx;
1047 isl_val *a = NULL, *b = NULL;
1049 if (!x && !y)
1050 return isl_val_gcd(v1, v2);
1052 if (!v1 || !v2)
1053 goto error;
1055 ctx = isl_val_get_ctx(v1);
1056 if (!isl_val_is_int(v1) || !isl_val_is_int(v2))
1057 isl_die(ctx, isl_error_invalid,
1058 "expecting two integers", goto error);
1060 v1 = isl_val_cow(v1);
1061 a = isl_val_alloc(ctx);
1062 b = isl_val_alloc(ctx);
1063 if (!v1 || !a || !b)
1064 goto error;
1065 isl_int_gcdext(&v1->n, &a->n, &b->n, v1->n, v2->n);
1066 if (x) {
1067 isl_int_set_si(a->d, 1);
1068 *x = a;
1069 } else
1070 isl_val_free(a);
1071 if (y) {
1072 isl_int_set_si(b->d, 1);
1073 *y = b;
1074 } else
1075 isl_val_free(b);
1076 isl_val_free(v2);
1077 return v1;
1078 error:
1079 isl_val_free(v1);
1080 isl_val_free(v2);
1081 isl_val_free(a);
1082 isl_val_free(b);
1083 if (x)
1084 *x = NULL;
1085 if (y)
1086 *y = NULL;
1087 return NULL;
1090 /* Does "v" represent an integer value?
1092 isl_bool isl_val_is_int(__isl_keep isl_val *v)
1094 if (!v)
1095 return isl_bool_error;
1097 return isl_int_is_one(v->d);
1100 /* Does "v" represent a rational value?
1102 isl_bool isl_val_is_rat(__isl_keep isl_val *v)
1104 if (!v)
1105 return isl_bool_error;
1107 return !isl_int_is_zero(v->d);
1110 /* Does "v" represent NaN?
1112 isl_bool isl_val_is_nan(__isl_keep isl_val *v)
1114 if (!v)
1115 return isl_bool_error;
1117 return isl_int_is_zero(v->n) && isl_int_is_zero(v->d);
1120 /* Does "v" represent +infinity?
1122 isl_bool isl_val_is_infty(__isl_keep isl_val *v)
1124 if (!v)
1125 return isl_bool_error;
1127 return isl_int_is_pos(v->n) && isl_int_is_zero(v->d);
1130 /* Does "v" represent -infinity?
1132 isl_bool isl_val_is_neginfty(__isl_keep isl_val *v)
1134 if (!v)
1135 return isl_bool_error;
1137 return isl_int_is_neg(v->n) && isl_int_is_zero(v->d);
1140 /* Does "v" represent the integer zero?
1142 isl_bool isl_val_is_zero(__isl_keep isl_val *v)
1144 if (!v)
1145 return isl_bool_error;
1147 return isl_int_is_zero(v->n) && !isl_int_is_zero(v->d);
1150 /* Does "v" represent the integer one?
1152 isl_bool isl_val_is_one(__isl_keep isl_val *v)
1154 if (!v)
1155 return isl_bool_error;
1157 return isl_int_eq(v->n, v->d);
1160 /* Does "v" represent the integer negative one?
1162 isl_bool isl_val_is_negone(__isl_keep isl_val *v)
1164 if (!v)
1165 return isl_bool_error;
1167 return isl_int_is_neg(v->n) && isl_int_abs_eq(v->n, v->d);
1170 /* Is "v" (strictly) positive?
1172 isl_bool isl_val_is_pos(__isl_keep isl_val *v)
1174 if (!v)
1175 return isl_bool_error;
1177 return isl_int_is_pos(v->n);
1180 /* Is "v" (strictly) negative?
1182 isl_bool isl_val_is_neg(__isl_keep isl_val *v)
1184 if (!v)
1185 return isl_bool_error;
1187 return isl_int_is_neg(v->n);
1190 /* Is "v" non-negative?
1192 isl_bool isl_val_is_nonneg(__isl_keep isl_val *v)
1194 if (!v)
1195 return isl_bool_error;
1197 if (isl_val_is_nan(v))
1198 return isl_bool_false;
1200 return isl_int_is_nonneg(v->n);
1203 /* Is "v" non-positive?
1205 isl_bool isl_val_is_nonpos(__isl_keep isl_val *v)
1207 if (!v)
1208 return isl_bool_error;
1210 if (isl_val_is_nan(v))
1211 return isl_bool_false;
1213 return isl_int_is_nonpos(v->n);
1216 /* Return the sign of "v".
1218 * The sign of NaN is undefined.
1220 int isl_val_sgn(__isl_keep isl_val *v)
1222 if (!v)
1223 return 0;
1224 if (isl_val_is_zero(v))
1225 return 0;
1226 if (isl_val_is_pos(v))
1227 return 1;
1228 return -1;
1231 /* Is "v1" (strictly) less than "v2"?
1233 isl_bool isl_val_lt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1235 isl_int t;
1236 isl_bool lt;
1238 if (!v1 || !v2)
1239 return isl_bool_error;
1240 if (isl_val_is_int(v1) && isl_val_is_int(v2))
1241 return isl_int_lt(v1->n, v2->n);
1242 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1243 return isl_bool_false;
1244 if (isl_val_eq(v1, v2))
1245 return isl_bool_false;
1246 if (isl_val_is_infty(v2))
1247 return isl_bool_true;
1248 if (isl_val_is_infty(v1))
1249 return isl_bool_false;
1250 if (isl_val_is_neginfty(v1))
1251 return isl_bool_true;
1252 if (isl_val_is_neginfty(v2))
1253 return isl_bool_false;
1255 isl_int_init(t);
1256 isl_int_mul(t, v1->n, v2->d);
1257 isl_int_submul(t, v2->n, v1->d);
1258 lt = isl_int_is_neg(t);
1259 isl_int_clear(t);
1261 return lt;
1264 /* Is "v1" (strictly) greater than "v2"?
1266 isl_bool isl_val_gt(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1268 return isl_val_lt(v2, v1);
1271 /* Is "v1" less than or equal to "v2"?
1273 isl_bool isl_val_le(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1275 isl_int t;
1276 isl_bool le;
1278 if (!v1 || !v2)
1279 return isl_bool_error;
1280 if (isl_val_is_int(v1) && isl_val_is_int(v2))
1281 return isl_int_le(v1->n, v2->n);
1282 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1283 return isl_bool_false;
1284 if (isl_val_eq(v1, v2))
1285 return isl_bool_true;
1286 if (isl_val_is_infty(v2))
1287 return isl_bool_true;
1288 if (isl_val_is_infty(v1))
1289 return isl_bool_false;
1290 if (isl_val_is_neginfty(v1))
1291 return isl_bool_true;
1292 if (isl_val_is_neginfty(v2))
1293 return isl_bool_false;
1295 isl_int_init(t);
1296 isl_int_mul(t, v1->n, v2->d);
1297 isl_int_submul(t, v2->n, v1->d);
1298 le = isl_int_is_nonpos(t);
1299 isl_int_clear(t);
1301 return le;
1304 /* Is "v1" greater than or equal to "v2"?
1306 isl_bool isl_val_ge(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1308 return isl_val_le(v2, v1);
1311 /* How does "v" compare to "i"?
1313 * Return 1 if v is greater, -1 if v is smaller and 0 if v is equal to i.
1315 * If v is NaN (or NULL), then the result is undefined.
1317 int isl_val_cmp_si(__isl_keep isl_val *v, long i)
1319 isl_int t;
1320 int cmp;
1322 if (!v)
1323 return 0;
1324 if (isl_val_is_int(v))
1325 return isl_int_cmp_si(v->n, i);
1326 if (isl_val_is_nan(v))
1327 return 0;
1328 if (isl_val_is_infty(v))
1329 return 1;
1330 if (isl_val_is_neginfty(v))
1331 return -1;
1333 isl_int_init(t);
1334 isl_int_mul_si(t, v->d, i);
1335 isl_int_sub(t, v->n, t);
1336 cmp = isl_int_sgn(t);
1337 isl_int_clear(t);
1339 return cmp;
1342 /* Is "v1" equal to "v2"?
1344 isl_bool isl_val_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1346 if (!v1 || !v2)
1347 return isl_bool_error;
1348 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1349 return isl_bool_false;
1351 return isl_int_eq(v1->n, v2->n) && isl_int_eq(v1->d, v2->d);
1354 /* Is "v1" equal to "v2" in absolute value?
1356 isl_bool isl_val_abs_eq(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1358 if (!v1 || !v2)
1359 return isl_bool_error;
1360 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1361 return isl_bool_false;
1363 return isl_int_abs_eq(v1->n, v2->n) && isl_int_eq(v1->d, v2->d);
1366 /* Is "v1" different from "v2"?
1368 isl_bool isl_val_ne(__isl_keep isl_val *v1, __isl_keep isl_val *v2)
1370 if (!v1 || !v2)
1371 return isl_bool_error;
1372 if (isl_val_is_nan(v1) || isl_val_is_nan(v2))
1373 return isl_bool_false;
1375 return isl_int_ne(v1->n, v2->n) || isl_int_ne(v1->d, v2->d);
1378 /* Print a textual representation of "v" onto "p".
1380 __isl_give isl_printer *isl_printer_print_val(__isl_take isl_printer *p,
1381 __isl_keep isl_val *v)
1383 int neg;
1385 if (!p || !v)
1386 return isl_printer_free(p);
1388 neg = isl_int_is_neg(v->n);
1389 if (neg) {
1390 p = isl_printer_print_str(p, "-");
1391 isl_int_neg(v->n, v->n);
1393 if (isl_int_is_zero(v->d)) {
1394 int sgn = isl_int_sgn(v->n);
1395 p = isl_printer_print_str(p, sgn < 0 ? "-infty" :
1396 sgn == 0 ? "NaN" : "infty");
1397 } else
1398 p = isl_printer_print_isl_int(p, v->n);
1399 if (neg)
1400 isl_int_neg(v->n, v->n);
1401 if (!isl_int_is_zero(v->d) && !isl_int_is_one(v->d)) {
1402 p = isl_printer_print_str(p, "/");
1403 p = isl_printer_print_isl_int(p, v->d);
1406 return p;
1409 /* Is "val1" (obviously) equal to "val2"?
1411 * This is a private copy of isl_val_eq for use in the generic
1412 * isl_multi_*_plain_is_equal instantiated for isl_val.
1414 int isl_val_plain_is_equal(__isl_keep isl_val *val1, __isl_keep isl_val *val2)
1416 return isl_val_eq(val1, val2);
1419 /* Does "v" have any non-zero coefficients
1420 * for any dimension in the given range?
1422 * This function is only meant to be used in the generic isl_multi_*
1423 * functions which have to deal with base objects that have an associated
1424 * space. Since an isl_val does not have any coefficients, this function
1425 * always return 0.
1427 int isl_val_involves_dims(__isl_keep isl_val *v, enum isl_dim_type type,
1428 unsigned first, unsigned n)
1430 if (!v)
1431 return -1;
1433 return 0;
1436 /* Insert "n" dimensions of type "type" at position "first".
1438 * This function is only meant to be used in the generic isl_multi_*
1439 * functions which have to deal with base objects that have an associated
1440 * space. Since an isl_val does not have an associated space, this function
1441 * does not do anything.
1443 __isl_give isl_val *isl_val_insert_dims(__isl_take isl_val *v,
1444 enum isl_dim_type type, unsigned first, unsigned n)
1446 return v;
1449 /* Drop the the "n" first dimensions of type "type" at position "first".
1451 * This function is only meant to be used in the generic isl_multi_*
1452 * functions which have to deal with base objects that have an associated
1453 * space. Since an isl_val does not have an associated space, this function
1454 * does not do anything.
1456 __isl_give isl_val *isl_val_drop_dims(__isl_take isl_val *v,
1457 enum isl_dim_type type, unsigned first, unsigned n)
1459 return v;
1462 /* Change the name of the dimension of type "type" at position "pos" to "s".
1464 * This function is only meant to be used in the generic isl_multi_*
1465 * functions which have to deal with base objects that have an associated
1466 * space. Since an isl_val does not have an associated space, this function
1467 * does not do anything.
1469 __isl_give isl_val *isl_val_set_dim_name(__isl_take isl_val *v,
1470 enum isl_dim_type type, unsigned pos, const char *s)
1472 return v;
1475 /* Return the space of "v".
1477 * This function is only meant to be used in the generic isl_multi_*
1478 * functions which have to deal with base objects that have an associated
1479 * space. The conditions surrounding the call to this function make sure
1480 * that this function will never actually get called. We return a valid
1481 * space anyway, just in case.
1483 __isl_give isl_space *isl_val_get_space(__isl_keep isl_val *v)
1485 if (!v)
1486 return NULL;
1488 return isl_space_params_alloc(isl_val_get_ctx(v), 0);
1491 /* Reset the domain space of "v" to "space".
1493 * This function is only meant to be used in the generic isl_multi_*
1494 * functions which have to deal with base objects that have an associated
1495 * space. Since an isl_val does not have an associated space, this function
1496 * does not do anything, apart from error handling and cleaning up memory.
1498 __isl_give isl_val *isl_val_reset_domain_space(__isl_take isl_val *v,
1499 __isl_take isl_space *space)
1501 if (!space)
1502 return isl_val_free(v);
1503 isl_space_free(space);
1504 return v;
1507 /* Align the parameters of "v" to those of "space".
1509 * This function is only meant to be used in the generic isl_multi_*
1510 * functions which have to deal with base objects that have an associated
1511 * space. Since an isl_val does not have an associated space, this function
1512 * does not do anything, apart from error handling and cleaning up memory.
1513 * Note that the conditions surrounding the call to this function make sure
1514 * that this function will never actually get called.
1516 __isl_give isl_val *isl_val_align_params(__isl_take isl_val *v,
1517 __isl_take isl_space *space)
1519 if (!space)
1520 return isl_val_free(v);
1521 isl_space_free(space);
1522 return v;
1525 /* Reorder the dimensions of the domain of "v" according
1526 * to the given reordering.
1528 * This function is only meant to be used in the generic isl_multi_*
1529 * functions which have to deal with base objects that have an associated
1530 * space. Since an isl_val does not have an associated space, this function
1531 * does not do anything, apart from error handling and cleaning up memory.
1533 __isl_give isl_val *isl_val_realign_domain(__isl_take isl_val *v,
1534 __isl_take isl_reordering *r)
1536 if (!r)
1537 return isl_val_free(v);
1538 isl_reordering_free(r);
1539 return v;
1542 /* Return an isl_val that is zero on "ls".
1544 * This function is only meant to be used in the generic isl_multi_*
1545 * functions which have to deal with base objects that have an associated
1546 * space. Since an isl_val does not have an associated space, this function
1547 * simply returns a zero isl_val in the same context as "ls".
1549 __isl_give isl_val *isl_val_zero_on_domain(__isl_take isl_local_space *ls)
1551 isl_ctx *ctx;
1553 if (!ls)
1554 return NULL;
1555 ctx = isl_local_space_get_ctx(ls);
1556 isl_local_space_free(ls);
1557 return isl_val_zero(ctx);
1560 /* Do the parameters of "v" match those of "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 * simply returns 1, except if "v" or "space" are NULL.
1567 int isl_val_matching_params(__isl_keep isl_val *v, __isl_keep isl_space *space)
1569 if (!v || !space)
1570 return -1;
1571 return 1;
1574 /* Check that the domain space of "v" matches "space".
1576 * Return 0 on success and -1 on error.
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 * simply returns 0, except if "v" or "space" are NULL.
1583 int isl_val_check_match_domain_space(__isl_keep isl_val *v,
1584 __isl_keep isl_space *space)
1586 if (!v || !space)
1587 return -1;
1588 return 0;
1591 #undef BASE
1592 #define BASE val
1594 #define NO_DOMAIN
1595 #define NO_IDENTITY
1596 #define NO_FROM_BASE
1597 #define NO_MOVE_DIMS
1598 #include <isl_multi_templ.c>
1600 /* Apply "fn" to each of the elements of "mv" with as second argument "v".
1602 static __isl_give isl_multi_val *isl_multi_val_fn_val(
1603 __isl_take isl_multi_val *mv,
1604 __isl_give isl_val *(*fn)(__isl_take isl_val *v1,
1605 __isl_take isl_val *v2),
1606 __isl_take isl_val *v)
1608 int i;
1610 mv = isl_multi_val_cow(mv);
1611 if (!mv || !v)
1612 goto error;
1614 for (i = 0; i < mv->n; ++i) {
1615 mv->p[i] = fn(mv->p[i], isl_val_copy(v));
1616 if (!mv->p[i])
1617 goto error;
1620 isl_val_free(v);
1621 return mv;
1622 error:
1623 isl_val_free(v);
1624 isl_multi_val_free(mv);
1625 return NULL;
1628 /* Add "v" to each of the elements of "mv".
1630 __isl_give isl_multi_val *isl_multi_val_add_val(__isl_take isl_multi_val *mv,
1631 __isl_take isl_val *v)
1633 if (!v)
1634 return isl_multi_val_free(mv);
1635 if (isl_val_is_zero(v)) {
1636 isl_val_free(v);
1637 return mv;
1639 return isl_multi_val_fn_val(mv, &isl_val_add, v);
1642 /* Reduce the elements of "mv" modulo "v".
1644 __isl_give isl_multi_val *isl_multi_val_mod_val(__isl_take isl_multi_val *mv,
1645 __isl_take isl_val *v)
1647 return isl_multi_val_fn_val(mv, &isl_val_mod, v);