2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Universiteit Leiden
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
8 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
10 * and Leiden Institute of Advanced Computer Science,
11 * Universiteit Leiden, Niels Bohrweg 1, 2333 CA Leiden, The Netherlands
14 #include <isl_map_private.h>
15 #include <isl_aff_private.h>
16 #include <isl_dim_private.h>
17 #include <isl_local_space_private.h>
18 #include <isl_mat_private.h>
19 #include <isl/constraint.h>
23 __isl_give isl_aff
*isl_aff_alloc_vec(__isl_take isl_local_space
*ls
,
24 __isl_take isl_vec
*v
)
31 aff
= isl_calloc_type(v
->ctx
, struct isl_aff
);
41 isl_local_space_free(ls
);
46 __isl_give isl_aff
*isl_aff_alloc(__isl_take isl_local_space
*ls
)
55 ctx
= isl_local_space_get_ctx(ls
);
56 if (!isl_local_space_divs_known(ls
))
57 isl_die(ctx
, isl_error_invalid
, "local space has unknown divs",
60 total
= isl_local_space_dim(ls
, isl_dim_all
);
61 v
= isl_vec_alloc(ctx
, 1 + 1 + total
);
62 return isl_aff_alloc_vec(ls
, v
);
64 isl_local_space_free(ls
);
68 __isl_give isl_aff
*isl_aff_zero(__isl_take isl_local_space
*ls
)
72 aff
= isl_aff_alloc(ls
);
76 isl_int_set_si(aff
->v
->el
[0], 1);
77 isl_seq_clr(aff
->v
->el
+ 1, aff
->v
->size
- 1);
82 __isl_give isl_aff
*isl_aff_copy(__isl_keep isl_aff
*aff
)
91 __isl_give isl_aff
*isl_aff_dup(__isl_keep isl_aff
*aff
)
96 return isl_aff_alloc_vec(isl_local_space_copy(aff
->ls
),
97 isl_vec_copy(aff
->v
));
100 __isl_give isl_aff
*isl_aff_cow(__isl_take isl_aff
*aff
)
108 return isl_aff_dup(aff
);
111 void *isl_aff_free(__isl_take isl_aff
*aff
)
119 isl_local_space_free(aff
->ls
);
120 isl_vec_free(aff
->v
);
127 isl_ctx
*isl_aff_get_ctx(__isl_keep isl_aff
*aff
)
129 return aff
? isl_local_space_get_ctx(aff
->ls
) : NULL
;
132 int isl_aff_dim(__isl_keep isl_aff
*aff
, enum isl_dim_type type
)
134 return aff
? isl_local_space_dim(aff
->ls
, type
) : 0;
137 __isl_give isl_dim
*isl_aff_get_dim(__isl_keep isl_aff
*aff
)
139 return aff
? isl_local_space_get_dim(aff
->ls
) : NULL
;
142 __isl_give isl_local_space
*isl_aff_get_local_space(__isl_keep isl_aff
*aff
)
144 return aff
? isl_local_space_copy(aff
->ls
) : NULL
;
147 const char *isl_aff_get_dim_name(__isl_keep isl_aff
*aff
,
148 enum isl_dim_type type
, unsigned pos
)
150 return aff
? isl_local_space_get_dim_name(aff
->ls
, type
, pos
) : 0;
153 int isl_aff_plain_is_zero(__isl_keep isl_aff
*aff
)
158 return isl_seq_first_non_zero(aff
->v
->el
+ 1, aff
->v
->size
- 1) < 0;
161 int isl_aff_plain_is_equal(__isl_keep isl_aff
*aff1
, __isl_keep isl_aff
*aff2
)
168 equal
= isl_local_space_is_equal(aff1
->ls
, aff2
->ls
);
169 if (equal
< 0 || !equal
)
172 return isl_vec_is_equal(aff1
->v
, aff2
->v
);
175 int isl_aff_get_denominator(__isl_keep isl_aff
*aff
, isl_int
*v
)
179 isl_int_set(*v
, aff
->v
->el
[0]);
183 int isl_aff_get_constant(__isl_keep isl_aff
*aff
, isl_int
*v
)
187 isl_int_set(*v
, aff
->v
->el
[1]);
191 int isl_aff_get_coefficient(__isl_keep isl_aff
*aff
,
192 enum isl_dim_type type
, int pos
, isl_int
*v
)
197 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
198 isl_die(aff
->v
->ctx
, isl_error_invalid
,
199 "position out of bounds", return -1);
201 pos
+= isl_local_space_offset(aff
->ls
, type
);
202 isl_int_set(*v
, aff
->v
->el
[1 + pos
]);
207 __isl_give isl_aff
*isl_aff_set_denominator(__isl_take isl_aff
*aff
, isl_int v
)
209 aff
= isl_aff_cow(aff
);
213 aff
->v
= isl_vec_cow(aff
->v
);
215 return isl_aff_free(aff
);
217 isl_int_set(aff
->v
->el
[0], v
);
222 __isl_give isl_aff
*isl_aff_set_constant(__isl_take isl_aff
*aff
, isl_int v
)
224 aff
= isl_aff_cow(aff
);
228 aff
->v
= isl_vec_cow(aff
->v
);
230 return isl_aff_free(aff
);
232 isl_int_set(aff
->v
->el
[1], v
);
237 __isl_give isl_aff
*isl_aff_add_constant(__isl_take isl_aff
*aff
, isl_int v
)
239 if (isl_int_is_zero(v
))
242 aff
= isl_aff_cow(aff
);
246 aff
->v
= isl_vec_cow(aff
->v
);
248 return isl_aff_free(aff
);
250 isl_int_addmul(aff
->v
->el
[1], aff
->v
->el
[0], v
);
255 __isl_give isl_aff
*isl_aff_add_constant_si(__isl_take isl_aff
*aff
, int v
)
260 isl_int_set_si(t
, v
);
261 aff
= isl_aff_add_constant(aff
, t
);
267 __isl_give isl_aff
*isl_aff_set_constant_si(__isl_take isl_aff
*aff
, int v
)
269 aff
= isl_aff_cow(aff
);
273 aff
->v
= isl_vec_cow(aff
->v
);
275 return isl_aff_free(aff
);
277 isl_int_set_si(aff
->v
->el
[1], v
);
282 __isl_give isl_aff
*isl_aff_set_coefficient(__isl_take isl_aff
*aff
,
283 enum isl_dim_type type
, int pos
, isl_int v
)
288 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
289 isl_die(aff
->v
->ctx
, isl_error_invalid
,
290 "position out of bounds", return isl_aff_free(aff
));
292 aff
= isl_aff_cow(aff
);
296 aff
->v
= isl_vec_cow(aff
->v
);
298 return isl_aff_free(aff
);
300 pos
+= isl_local_space_offset(aff
->ls
, type
);
301 isl_int_set(aff
->v
->el
[1 + pos
], v
);
306 __isl_give isl_aff
*isl_aff_set_coefficient_si(__isl_take isl_aff
*aff
,
307 enum isl_dim_type type
, int pos
, int v
)
312 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
313 isl_die(aff
->v
->ctx
, isl_error_invalid
,
314 "position out of bounds", return isl_aff_free(aff
));
316 aff
= isl_aff_cow(aff
);
320 aff
->v
= isl_vec_cow(aff
->v
);
322 return isl_aff_free(aff
);
324 pos
+= isl_local_space_offset(aff
->ls
, type
);
325 isl_int_set_si(aff
->v
->el
[1 + pos
], v
);
330 __isl_give isl_aff
*isl_aff_add_coefficient(__isl_take isl_aff
*aff
,
331 enum isl_dim_type type
, int pos
, isl_int v
)
336 if (pos
>= isl_local_space_dim(aff
->ls
, type
))
337 isl_die(aff
->v
->ctx
, isl_error_invalid
,
338 "position out of bounds", return isl_aff_free(aff
));
340 aff
= isl_aff_cow(aff
);
344 aff
->v
= isl_vec_cow(aff
->v
);
346 return isl_aff_free(aff
);
348 pos
+= isl_local_space_offset(aff
->ls
, type
);
349 isl_int_addmul(aff
->v
->el
[1 + pos
], aff
->v
->el
[0], v
);
354 __isl_give isl_aff
*isl_aff_add_coefficient_si(__isl_take isl_aff
*aff
,
355 enum isl_dim_type type
, int pos
, int v
)
360 isl_int_set_si(t
, v
);
361 aff
= isl_aff_add_coefficient(aff
, type
, pos
, t
);
367 __isl_give isl_div
*isl_aff_get_div(__isl_keep isl_aff
*aff
, int pos
)
372 return isl_local_space_get_div(aff
->ls
, pos
);
375 __isl_give isl_aff
*isl_aff_neg(__isl_take isl_aff
*aff
)
377 aff
= isl_aff_cow(aff
);
380 aff
->v
= isl_vec_cow(aff
->v
);
382 return isl_aff_free(aff
);
384 isl_seq_neg(aff
->v
->el
+ 1, aff
->v
->el
+ 1, aff
->v
->size
- 1);
389 /* Given f, return floor(f).
390 * If f is an integer expression, then just return f.
391 * Otherwise, create a new div d = [f] and return the expression d.
393 __isl_give isl_aff
*isl_aff_floor(__isl_take isl_aff
*aff
)
401 if (isl_int_is_one(aff
->v
->el
[0]))
404 aff
= isl_aff_cow(aff
);
408 aff
->ls
= isl_local_space_add_div(aff
->ls
, isl_vec_copy(aff
->v
));
410 return isl_aff_free(aff
);
412 ctx
= isl_aff_get_ctx(aff
);
414 isl_vec_free(aff
->v
);
415 aff
->v
= isl_vec_alloc(ctx
, size
+ 1);
416 aff
->v
= isl_vec_clr(aff
->v
);
418 return isl_aff_free(aff
);
419 isl_int_set_si(aff
->v
->el
[0], 1);
420 isl_int_set_si(aff
->v
->el
[size
], 1);
425 /* Given f, return ceil(f).
426 * If f is an integer expression, then just return f.
427 * Otherwise, create a new div d = [-f] and return the expression -d.
429 __isl_give isl_aff
*isl_aff_ceil(__isl_take isl_aff
*aff
)
434 if (isl_int_is_one(aff
->v
->el
[0]))
437 aff
= isl_aff_neg(aff
);
438 aff
= isl_aff_floor(aff
);
439 aff
= isl_aff_neg(aff
);
444 /* Apply the expansion computed by isl_merge_divs.
445 * The expansion itself is given by "exp" while the resulting
446 * list of divs is given by "div".
448 __isl_give isl_aff
*isl_aff_expand_divs( __isl_take isl_aff
*aff
,
449 __isl_take isl_mat
*div
, int *exp
)
456 aff
= isl_aff_cow(aff
);
460 old_n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
461 new_n_div
= isl_mat_rows(div
);
462 if (new_n_div
< old_n_div
)
463 isl_die(isl_mat_get_ctx(div
), isl_error_invalid
,
464 "not an expansion", goto error
);
466 aff
->v
= isl_vec_extend(aff
->v
, aff
->v
->size
+ new_n_div
- old_n_div
);
470 offset
= 1 + isl_local_space_offset(aff
->ls
, isl_dim_div
);
472 for (i
= new_n_div
- 1; i
>= 0; --i
) {
473 if (j
>= 0 && exp
[j
] == i
) {
475 isl_int_swap(aff
->v
->el
[offset
+ i
],
476 aff
->v
->el
[offset
+ j
]);
479 isl_int_set_si(aff
->v
->el
[offset
+ i
], 0);
482 aff
->ls
= isl_local_space_replace_divs(aff
->ls
, isl_mat_copy(div
));
493 /* Add two affine expressions that live in the same local space.
495 static __isl_give isl_aff
*add_expanded(__isl_take isl_aff
*aff1
,
496 __isl_take isl_aff
*aff2
)
500 aff1
= isl_aff_cow(aff1
);
504 aff1
->v
= isl_vec_cow(aff1
->v
);
510 isl_int_gcd(gcd
, aff1
->v
->el
[0], aff2
->v
->el
[0]);
511 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
512 isl_seq_scale(aff1
->v
->el
+ 1, aff1
->v
->el
+ 1, f
, aff1
->v
->size
- 1);
513 isl_int_divexact(f
, aff1
->v
->el
[0], gcd
);
514 isl_seq_addmul(aff1
->v
->el
+ 1, f
, aff2
->v
->el
+ 1, aff1
->v
->size
- 1);
515 isl_int_divexact(f
, aff2
->v
->el
[0], gcd
);
516 isl_int_mul(aff1
->v
->el
[0], aff1
->v
->el
[0], f
);
528 __isl_give isl_aff
*isl_aff_add(__isl_take isl_aff
*aff1
,
529 __isl_take isl_aff
*aff2
)
539 ctx
= isl_aff_get_ctx(aff1
);
540 if (!isl_dim_equal(aff1
->ls
->dim
, aff2
->ls
->dim
))
541 isl_die(ctx
, isl_error_invalid
,
542 "spaces don't match", goto error
);
544 if (aff1
->ls
->div
->n_row
== 0 && aff2
->ls
->div
->n_row
== 0)
545 return add_expanded(aff1
, aff2
);
547 exp1
= isl_alloc_array(ctx
, int, aff1
->ls
->div
->n_row
);
548 exp2
= isl_alloc_array(ctx
, int, aff2
->ls
->div
->n_row
);
552 div
= isl_merge_divs(aff1
->ls
->div
, aff2
->ls
->div
, exp1
, exp2
);
553 aff1
= isl_aff_expand_divs(aff1
, isl_mat_copy(div
), exp1
);
554 aff2
= isl_aff_expand_divs(aff2
, div
, exp2
);
558 return add_expanded(aff1
, aff2
);
567 __isl_give isl_aff
*isl_aff_sub(__isl_take isl_aff
*aff1
,
568 __isl_take isl_aff
*aff2
)
570 return isl_aff_add(aff1
, isl_aff_neg(aff2
));
573 __isl_give isl_aff
*isl_aff_scale(__isl_take isl_aff
*aff
, isl_int f
)
577 if (isl_int_is_one(f
))
580 aff
= isl_aff_cow(aff
);
583 aff
->v
= isl_vec_cow(aff
->v
);
585 return isl_aff_free(aff
);
588 isl_int_gcd(gcd
, aff
->v
->el
[0], f
);
589 isl_int_divexact(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
590 isl_int_divexact(gcd
, f
, gcd
);
591 isl_seq_scale(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
597 __isl_give isl_aff
*isl_aff_scale_down(__isl_take isl_aff
*aff
, isl_int f
)
601 if (isl_int_is_one(f
))
604 aff
= isl_aff_cow(aff
);
607 aff
->v
= isl_vec_cow(aff
->v
);
609 return isl_aff_free(aff
);
612 isl_seq_gcd(aff
->v
->el
+ 1, aff
->v
->size
- 1, &gcd
);
613 isl_int_gcd(gcd
, gcd
, f
);
614 isl_seq_scale_down(aff
->v
->el
+ 1, aff
->v
->el
+ 1, gcd
, aff
->v
->size
- 1);
615 isl_int_divexact(gcd
, f
, gcd
);
616 isl_int_mul(aff
->v
->el
[0], aff
->v
->el
[0], gcd
);
622 __isl_give isl_aff
*isl_aff_scale_down_ui(__isl_take isl_aff
*aff
, unsigned f
)
630 isl_int_set_ui(v
, f
);
631 aff
= isl_aff_scale_down(aff
, v
);
637 __isl_give isl_aff
*isl_aff_set_dim_name(__isl_take isl_aff
*aff
,
638 enum isl_dim_type type
, unsigned pos
, const char *s
)
640 aff
= isl_aff_cow(aff
);
643 aff
->ls
= isl_local_space_set_dim_name(aff
->ls
, type
, pos
, s
);
645 return isl_aff_free(aff
);
650 /* Exploit the equalities in "eq" to simplify the affine expression
651 * and the expressions of the integer divisions in the local space.
652 * The integer divisions in this local space are assumed to appear
653 * as regular dimensions in "eq".
655 static __isl_give isl_aff
*isl_aff_substitute_equalities(
656 __isl_take isl_aff
*aff
, __isl_take isl_basic_set
*eq
)
665 isl_basic_set_free(eq
);
669 aff
= isl_aff_cow(aff
);
673 aff
->ls
= isl_local_space_substitute_equalities(aff
->ls
,
674 isl_basic_set_copy(eq
));
678 total
= 1 + isl_dim_total(eq
->dim
);
680 for (i
= 0; i
< eq
->n_eq
; ++i
) {
681 j
= isl_seq_last_non_zero(eq
->eq
[i
], total
+ n_div
);
682 if (j
< 0 || j
== 0 || j
>= total
)
685 isl_seq_elim(aff
->v
->el
+ 1, eq
->eq
[i
], j
, total
,
689 isl_basic_set_free(eq
);
692 isl_basic_set_free(eq
);
697 /* Look for equalities among the variables shared by context and aff
698 * and the integer divisions of aff, if any.
699 * The equalities are then used to eliminate coefficients and/or integer
700 * divisions from aff.
702 __isl_give isl_aff
*isl_aff_gist(__isl_take isl_aff
*aff
,
703 __isl_take isl_set
*context
)
710 n_div
= isl_local_space_dim(aff
->ls
, isl_dim_div
);
713 context
= isl_set_add_dims(context
, isl_dim_set
, n_div
);
714 bset
= isl_basic_set_from_local_space(
715 isl_aff_get_local_space(aff
));
716 bset
= isl_basic_set_lift(bset
);
717 bset
= isl_basic_set_flatten(bset
);
718 context
= isl_set_intersect(context
,
719 isl_set_from_basic_set(bset
));
722 hull
= isl_set_affine_hull(context
);
723 return isl_aff_substitute_equalities(aff
, hull
);
726 isl_set_free(context
);
730 /* Return a basic set containing those elements in the space
731 * of aff where it is non-negative.
733 __isl_give isl_basic_set
*isl_aff_nonneg_basic_set(__isl_take isl_aff
*aff
)
735 isl_constraint
*ineq
;
737 ineq
= isl_inequality_from_aff(aff
);
739 return isl_basic_set_from_constraint(ineq
);
742 /* Return a basic set containing those elements in the shared space
743 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
745 __isl_give isl_basic_set
*isl_aff_ge_basic_set(__isl_take isl_aff
*aff1
,
746 __isl_take isl_aff
*aff2
)
748 aff1
= isl_aff_sub(aff1
, aff2
);
750 return isl_aff_nonneg_basic_set(aff1
);
753 __isl_give isl_aff
*isl_aff_add_on_domain(__isl_keep isl_set
*dom
,
754 __isl_take isl_aff
*aff1
, __isl_take isl_aff
*aff2
)
756 aff1
= isl_aff_add(aff1
, aff2
);
757 aff1
= isl_aff_gist(aff1
, isl_set_copy(dom
));
761 int isl_aff_is_empty(__isl_keep isl_aff
*aff
)
769 /* Set active[i] to 1 if the dimension at position i is involved
770 * in the affine expression.
772 static int set_active(__isl_keep isl_aff
*aff
, int *active
)
781 total
= aff
->v
->size
- 2;
782 for (i
= 0; i
< total
; ++i
)
783 active
[i
] = !isl_int_is_zero(aff
->v
->el
[2 + i
]);
785 offset
= isl_local_space_offset(aff
->ls
, isl_dim_div
) - 1;
786 for (i
= aff
->ls
->div
->n_row
- 1; i
>= 0; --i
) {
787 if (!active
[offset
+ i
])
789 for (j
= 0; j
< total
; ++j
)
791 !isl_int_is_zero(aff
->ls
->div
->row
[i
][2 + j
]);
797 /* Check whether the given affine expression has non-zero coefficient
798 * for any dimension in the given range or if any of these dimensions
799 * appear with non-zero coefficients in any of the integer divisions
800 * involved in the affine expression.
802 int isl_aff_involves_dims(__isl_keep isl_aff
*aff
,
803 enum isl_dim_type type
, unsigned first
, unsigned n
)
815 ctx
= isl_aff_get_ctx(aff
);
816 if (first
+ n
> isl_aff_dim(aff
, type
))
817 isl_die(ctx
, isl_error_invalid
,
818 "range out of bounds", return -1);
820 active
= isl_calloc_array(ctx
, int,
821 isl_local_space_dim(aff
->ls
, isl_dim_all
));
822 if (set_active(aff
, active
) < 0)
825 first
+= isl_local_space_offset(aff
->ls
, type
) - 1;
826 for (i
= 0; i
< n
; ++i
)
827 if (active
[first
+ i
]) {
840 __isl_give isl_aff
*isl_aff_drop_dims(__isl_take isl_aff
*aff
,
841 enum isl_dim_type type
, unsigned first
, unsigned n
)
847 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
850 ctx
= isl_aff_get_ctx(aff
);
851 if (first
+ n
> isl_aff_dim(aff
, type
))
852 isl_die(ctx
, isl_error_invalid
, "range out of bounds",
853 return isl_aff_free(aff
));
855 aff
= isl_aff_cow(aff
);
859 aff
->ls
= isl_local_space_drop_dims(aff
->ls
, type
, first
, n
);
861 return isl_aff_free(aff
);
863 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
864 aff
->v
= isl_vec_drop_els(aff
->v
, first
, n
);
866 return isl_aff_free(aff
);
871 __isl_give isl_aff
*isl_aff_insert_dims(__isl_take isl_aff
*aff
,
872 enum isl_dim_type type
, unsigned first
, unsigned n
)
878 if (n
== 0 && !isl_local_space_is_named_or_nested(aff
->ls
, type
))
881 ctx
= isl_aff_get_ctx(aff
);
882 if (first
> isl_aff_dim(aff
, type
))
883 isl_die(ctx
, isl_error_invalid
, "position out of bounds",
884 return isl_aff_free(aff
));
886 aff
= isl_aff_cow(aff
);
890 aff
->ls
= isl_local_space_insert_dims(aff
->ls
, type
, first
, n
);
892 return isl_aff_free(aff
);
894 first
+= 1 + isl_local_space_offset(aff
->ls
, type
);
895 aff
->v
= isl_vec_insert_zero_els(aff
->v
, first
, n
);
897 return isl_aff_free(aff
);
902 __isl_give isl_aff
*isl_aff_add_dims(__isl_take isl_aff
*aff
,
903 enum isl_dim_type type
, unsigned n
)
907 pos
= isl_aff_dim(aff
, type
);
909 return isl_aff_insert_dims(aff
, type
, pos
, n
);
912 __isl_give isl_pw_aff
*isl_pw_aff_add_dims(__isl_take isl_pw_aff
*pwaff
,
913 enum isl_dim_type type
, unsigned n
)
917 pos
= isl_pw_aff_dim(pwaff
, type
);
919 return isl_pw_aff_insert_dims(pwaff
, type
, pos
, n
);
923 #define PW isl_pw_aff
927 #define EL_IS_ZERO is_empty
931 #define IS_ZERO is_empty
943 #include <isl_pw_templ.c>
945 /* Compute a piecewise quasi-affine expression with a domain that
946 * is the union of those of pwaff1 and pwaff2 and such that on each
947 * cell, the quasi-affine expression is the maximum of those of pwaff1
948 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
949 * cell, then the associated expression is the defined one.
951 __isl_give isl_pw_aff
*isl_pw_aff_max(__isl_take isl_pw_aff
*pwaff1
,
952 __isl_take isl_pw_aff
*pwaff2
)
959 if (!pwaff1
|| !pwaff2
)
962 ctx
= isl_dim_get_ctx(pwaff1
->dim
);
963 if (!isl_dim_equal(pwaff1
->dim
, pwaff2
->dim
))
964 isl_die(ctx
, isl_error_invalid
,
965 "arguments should live in same space", goto error
);
967 if (isl_pw_aff_is_empty(pwaff1
)) {
968 isl_pw_aff_free(pwaff1
);
972 if (isl_pw_aff_is_empty(pwaff2
)) {
973 isl_pw_aff_free(pwaff2
);
977 n
= 2 * (pwaff1
->n
+ 1) * (pwaff2
->n
+ 1);
978 res
= isl_pw_aff_alloc_(isl_dim_copy(pwaff1
->dim
), n
);
980 for (i
= 0; i
< pwaff1
->n
; ++i
) {
981 set
= isl_set_copy(pwaff1
->p
[i
].set
);
982 for (j
= 0; j
< pwaff2
->n
; ++j
) {
983 struct isl_set
*common
;
986 common
= isl_set_intersect(
987 isl_set_copy(pwaff1
->p
[i
].set
),
988 isl_set_copy(pwaff2
->p
[j
].set
));
989 ge
= isl_set_from_basic_set(isl_aff_ge_basic_set(
990 isl_aff_copy(pwaff2
->p
[j
].aff
),
991 isl_aff_copy(pwaff1
->p
[i
].aff
)));
992 ge
= isl_set_intersect(common
, ge
);
993 if (isl_set_plain_is_empty(ge
)) {
997 set
= isl_set_subtract(set
, isl_set_copy(ge
));
999 res
= isl_pw_aff_add_piece(res
, ge
,
1000 isl_aff_copy(pwaff2
->p
[j
].aff
));
1002 res
= isl_pw_aff_add_piece(res
, set
,
1003 isl_aff_copy(pwaff1
->p
[i
].aff
));
1006 for (j
= 0; j
< pwaff2
->n
; ++j
) {
1007 set
= isl_set_copy(pwaff2
->p
[j
].set
);
1008 for (i
= 0; i
< pwaff1
->n
; ++i
)
1009 set
= isl_set_subtract(set
,
1010 isl_set_copy(pwaff1
->p
[i
].set
));
1011 res
= isl_pw_aff_add_piece(res
, set
,
1012 isl_aff_copy(pwaff2
->p
[j
].aff
));
1015 isl_pw_aff_free(pwaff1
);
1016 isl_pw_aff_free(pwaff2
);
1020 isl_pw_aff_free(pwaff1
);
1021 isl_pw_aff_free(pwaff2
);
1025 /* Construct a map with as domain the domain of pwaff and
1026 * one-dimensional range corresponding to the affine expressions.
1028 __isl_give isl_map
*isl_map_from_pw_aff(__isl_take isl_pw_aff
*pwaff
)
1037 dim
= isl_pw_aff_get_dim(pwaff
);
1038 dim
= isl_dim_from_domain(dim
);
1039 dim
= isl_dim_add(dim
, isl_dim_out
, 1);
1040 map
= isl_map_empty(dim
);
1042 for (i
= 0; i
< pwaff
->n
; ++i
) {
1043 isl_basic_map
*bmap
;
1046 bmap
= isl_basic_map_from_aff(isl_aff_copy(pwaff
->p
[i
].aff
));
1047 map_i
= isl_map_from_basic_map(bmap
);
1048 map_i
= isl_map_intersect_domain(map_i
,
1049 isl_set_copy(pwaff
->p
[i
].set
));
1050 map
= isl_map_union_disjoint(map
, map_i
);
1053 isl_pw_aff_free(pwaff
);
1058 /* Return a set containing those elements in the domain
1059 * of pwaff where it is non-negative.
1061 __isl_give isl_set
*isl_pw_aff_nonneg_set(__isl_take isl_pw_aff
*pwaff
)
1069 set
= isl_set_empty(isl_pw_aff_get_dim(pwaff
));
1071 for (i
= 0; i
< pwaff
->n
; ++i
) {
1072 isl_basic_set
*bset
;
1075 bset
= isl_aff_nonneg_basic_set(isl_aff_copy(pwaff
->p
[i
].aff
));
1076 set_i
= isl_set_from_basic_set(bset
);
1077 set_i
= isl_set_intersect(set_i
, isl_set_copy(pwaff
->p
[i
].set
));
1078 set
= isl_set_union_disjoint(set
, set_i
);
1081 isl_pw_aff_free(pwaff
);
1086 /* Return a set containing those elements in the shared domain
1087 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
1089 * We compute the difference on the shared domain and then construct
1090 * the set of values where this difference is non-negative.
1091 * If strict is set, we first subtract 1 from the difference.
1093 static __isl_give isl_set
*pw_aff_gte_set(__isl_take isl_pw_aff
*pwaff1
,
1094 __isl_take isl_pw_aff
*pwaff2
, int strict
)
1096 isl_set
*set1
, *set2
;
1098 set1
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff1
));
1099 set2
= isl_pw_aff_domain(isl_pw_aff_copy(pwaff2
));
1100 set1
= isl_set_intersect(set1
, set2
);
1101 pwaff1
= isl_pw_aff_intersect_domain(pwaff1
, isl_set_copy(set1
));
1102 pwaff2
= isl_pw_aff_intersect_domain(pwaff2
, isl_set_copy(set1
));
1103 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_neg(pwaff2
));
1106 isl_dim
*dim
= isl_set_get_dim(set1
);
1108 aff
= isl_aff_zero(isl_local_space_from_dim(dim
));
1109 aff
= isl_aff_add_constant_si(aff
, -1);
1110 pwaff1
= isl_pw_aff_add(pwaff1
, isl_pw_aff_alloc(set1
, aff
));
1114 return isl_pw_aff_nonneg_set(pwaff1
);
1117 /* Return a set containing those elements in the shared domain
1118 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
1120 __isl_give isl_set
*isl_pw_aff_ge_set(__isl_take isl_pw_aff
*pwaff1
,
1121 __isl_take isl_pw_aff
*pwaff2
)
1123 return pw_aff_gte_set(pwaff1
, pwaff2
, 0);
1126 /* Return a set containing those elements in the shared domain
1127 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
1129 __isl_give isl_set
*isl_pw_aff_gt_set(__isl_take isl_pw_aff
*pwaff1
,
1130 __isl_take isl_pw_aff
*pwaff2
)
1132 return pw_aff_gte_set(pwaff1
, pwaff2
, 1);
1135 __isl_give isl_set
*isl_pw_aff_lt_set(__isl_take isl_pw_aff
*pwaff1
,
1136 __isl_take isl_pw_aff
*pwaff2
)
1138 return isl_pw_aff_gt_set(pwaff2
, pwaff1
);