isl_set_from_pw_multi_aff: explicitly convert result to set
[isl.git] / isl_aff.c
blob7aa0862aedb57851a8cfe3ae486fa8b8094c5f72
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl_union_map_private.h>
20 #include <isl_aff_private.h>
21 #include <isl_space_private.h>
22 #include <isl_local_space_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_mat_private.h>
25 #include <isl/id.h>
26 #include <isl/constraint.h>
27 #include <isl_seq.h>
28 #include <isl/set.h>
29 #include <isl_val_private.h>
30 #include <isl_point_private.h>
31 #include <isl_config.h>
33 #undef BASE
34 #define BASE aff
36 #include <isl_list_templ.c>
38 #undef BASE
39 #define BASE pw_aff
41 #include <isl_list_templ.c>
43 #undef BASE
44 #define BASE pw_multi_aff
46 #include <isl_list_templ.c>
48 #undef BASE
49 #define BASE union_pw_aff
51 #include <isl_list_templ.c>
53 #undef BASE
54 #define BASE union_pw_multi_aff
56 #include <isl_list_templ.c>
58 #include <set_from_map.c>
60 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
61 __isl_take isl_vec *v)
63 isl_aff *aff;
65 if (!ls || !v)
66 goto error;
68 aff = isl_calloc_type(v->ctx, struct isl_aff);
69 if (!aff)
70 goto error;
72 aff->ref = 1;
73 aff->ls = ls;
74 aff->v = v;
76 return aff;
77 error:
78 isl_local_space_free(ls);
79 isl_vec_free(v);
80 return NULL;
83 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
85 isl_ctx *ctx;
86 isl_vec *v;
87 unsigned total;
89 if (!ls)
90 return NULL;
92 ctx = isl_local_space_get_ctx(ls);
93 if (!isl_local_space_divs_known(ls))
94 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
95 goto error);
96 if (!isl_local_space_is_set(ls))
97 isl_die(ctx, isl_error_invalid,
98 "domain of affine expression should be a set",
99 goto error);
101 total = isl_local_space_dim(ls, isl_dim_all);
102 v = isl_vec_alloc(ctx, 1 + 1 + total);
103 return isl_aff_alloc_vec(ls, v);
104 error:
105 isl_local_space_free(ls);
106 return NULL;
109 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
111 isl_aff *aff;
113 aff = isl_aff_alloc(ls);
114 if (!aff)
115 return NULL;
117 isl_int_set_si(aff->v->el[0], 1);
118 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
120 return aff;
123 /* Return a piecewise affine expression defined on the specified domain
124 * that is equal to zero.
126 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
128 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
131 /* Return an affine expression defined on the specified domain
132 * that represents NaN.
134 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
136 isl_aff *aff;
138 aff = isl_aff_alloc(ls);
139 if (!aff)
140 return NULL;
142 isl_seq_clr(aff->v->el, aff->v->size);
144 return aff;
147 /* Return a piecewise affine expression defined on the specified domain
148 * that represents NaN.
150 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
152 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
155 /* Return an affine expression that is equal to "val" on
156 * domain local space "ls".
158 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
159 __isl_take isl_val *val)
161 isl_aff *aff;
163 if (!ls || !val)
164 goto error;
165 if (!isl_val_is_rat(val))
166 isl_die(isl_val_get_ctx(val), isl_error_invalid,
167 "expecting rational value", goto error);
169 aff = isl_aff_alloc(isl_local_space_copy(ls));
170 if (!aff)
171 goto error;
173 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
174 isl_int_set(aff->v->el[1], val->n);
175 isl_int_set(aff->v->el[0], val->d);
177 isl_local_space_free(ls);
178 isl_val_free(val);
179 return aff;
180 error:
181 isl_local_space_free(ls);
182 isl_val_free(val);
183 return NULL;
186 /* Return an affine expression that is equal to the specified dimension
187 * in "ls".
189 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
190 enum isl_dim_type type, unsigned pos)
192 isl_space *space;
193 isl_aff *aff;
195 if (!ls)
196 return NULL;
198 space = isl_local_space_get_space(ls);
199 if (!space)
200 goto error;
201 if (isl_space_is_map(space))
202 isl_die(isl_space_get_ctx(space), isl_error_invalid,
203 "expecting (parameter) set space", goto error);
204 if (pos >= isl_local_space_dim(ls, type))
205 isl_die(isl_space_get_ctx(space), isl_error_invalid,
206 "position out of bounds", goto error);
208 isl_space_free(space);
209 aff = isl_aff_alloc(ls);
210 if (!aff)
211 return NULL;
213 pos += isl_local_space_offset(aff->ls, type);
215 isl_int_set_si(aff->v->el[0], 1);
216 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
217 isl_int_set_si(aff->v->el[1 + pos], 1);
219 return aff;
220 error:
221 isl_local_space_free(ls);
222 isl_space_free(space);
223 return NULL;
226 /* Return a piecewise affine expression that is equal to
227 * the specified dimension in "ls".
229 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
230 enum isl_dim_type type, unsigned pos)
232 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
235 /* Return an affine expression that is equal to the parameter
236 * in the domain space "space" with identifier "id".
238 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
239 __isl_take isl_space *space, __isl_take isl_id *id)
241 int pos;
242 isl_local_space *ls;
244 if (!space || !id)
245 goto error;
246 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
247 if (pos < 0)
248 isl_die(isl_space_get_ctx(space), isl_error_invalid,
249 "parameter not found in space", goto error);
250 isl_id_free(id);
251 ls = isl_local_space_from_space(space);
252 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
253 error:
254 isl_space_free(space);
255 isl_id_free(id);
256 return NULL;
259 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
261 if (!aff)
262 return NULL;
264 aff->ref++;
265 return aff;
268 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
270 if (!aff)
271 return NULL;
273 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
274 isl_vec_copy(aff->v));
277 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
279 if (!aff)
280 return NULL;
282 if (aff->ref == 1)
283 return aff;
284 aff->ref--;
285 return isl_aff_dup(aff);
288 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
290 if (!aff)
291 return NULL;
293 if (--aff->ref > 0)
294 return NULL;
296 isl_local_space_free(aff->ls);
297 isl_vec_free(aff->v);
299 free(aff);
301 return NULL;
304 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
306 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
309 /* Return a hash value that digests "aff".
311 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
313 uint32_t hash, ls_hash, v_hash;
315 if (!aff)
316 return 0;
318 hash = isl_hash_init();
319 ls_hash = isl_local_space_get_hash(aff->ls);
320 isl_hash_hash(hash, ls_hash);
321 v_hash = isl_vec_get_hash(aff->v);
322 isl_hash_hash(hash, v_hash);
324 return hash;
327 /* Externally, an isl_aff has a map space, but internally, the
328 * ls field corresponds to the domain of that space.
330 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
332 if (!aff)
333 return 0;
334 if (type == isl_dim_out)
335 return 1;
336 if (type == isl_dim_in)
337 type = isl_dim_set;
338 return isl_local_space_dim(aff->ls, type);
341 /* Return the position of the dimension of the given type and name
342 * in "aff".
343 * Return -1 if no such dimension can be found.
345 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
346 const char *name)
348 if (!aff)
349 return -1;
350 if (type == isl_dim_out)
351 return -1;
352 if (type == isl_dim_in)
353 type = isl_dim_set;
354 return isl_local_space_find_dim_by_name(aff->ls, type, name);
357 /* Return the domain space of "aff".
359 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
361 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
364 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
366 return isl_space_copy(isl_aff_peek_domain_space(aff));
369 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
371 isl_space *space;
372 if (!aff)
373 return NULL;
374 space = isl_local_space_get_space(aff->ls);
375 space = isl_space_from_domain(space);
376 space = isl_space_add_dims(space, isl_dim_out, 1);
377 return space;
380 __isl_give isl_local_space *isl_aff_get_domain_local_space(
381 __isl_keep isl_aff *aff)
383 return aff ? isl_local_space_copy(aff->ls) : NULL;
386 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
388 isl_local_space *ls;
389 if (!aff)
390 return NULL;
391 ls = isl_local_space_copy(aff->ls);
392 ls = isl_local_space_from_domain(ls);
393 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
394 return ls;
397 /* Return the local space of the domain of "aff".
398 * This may be either a copy or the local space itself
399 * if there is only one reference to "aff".
400 * This allows the local space to be modified inplace
401 * if both the expression and its local space have only a single reference.
402 * The caller is not allowed to modify "aff" between this call and
403 * a subsequent call to isl_aff_restore_domain_local_space.
404 * The only exception is that isl_aff_free can be called instead.
406 __isl_give isl_local_space *isl_aff_take_domain_local_space(
407 __isl_keep isl_aff *aff)
409 isl_local_space *ls;
411 if (!aff)
412 return NULL;
413 if (aff->ref != 1)
414 return isl_aff_get_domain_local_space(aff);
415 ls = aff->ls;
416 aff->ls = NULL;
417 return ls;
420 /* Set the local space of the domain of "aff" to "ls",
421 * where the local space of "aff" may be missing
422 * due to a preceding call to isl_aff_take_domain_local_space.
423 * However, in this case, "aff" only has a single reference and
424 * then the call to isl_aff_cow has no effect.
426 __isl_give isl_aff *isl_aff_restore_domain_local_space(
427 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
429 if (!aff || !ls)
430 goto error;
432 if (aff->ls == ls) {
433 isl_local_space_free(ls);
434 return aff;
437 aff = isl_aff_cow(aff);
438 if (!aff)
439 goto error;
440 isl_local_space_free(aff->ls);
441 aff->ls = ls;
443 return aff;
444 error:
445 isl_aff_free(aff);
446 isl_local_space_free(ls);
447 return NULL;
450 /* Externally, an isl_aff has a map space, but internally, the
451 * ls field corresponds to the domain of that space.
453 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
454 enum isl_dim_type type, unsigned pos)
456 if (!aff)
457 return NULL;
458 if (type == isl_dim_out)
459 return NULL;
460 if (type == isl_dim_in)
461 type = isl_dim_set;
462 return isl_local_space_get_dim_name(aff->ls, type, pos);
465 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
466 __isl_take isl_space *dim)
468 aff = isl_aff_cow(aff);
469 if (!aff || !dim)
470 goto error;
472 aff->ls = isl_local_space_reset_space(aff->ls, dim);
473 if (!aff->ls)
474 return isl_aff_free(aff);
476 return aff;
477 error:
478 isl_aff_free(aff);
479 isl_space_free(dim);
480 return NULL;
483 /* Reset the space of "aff". This function is called from isl_pw_templ.c
484 * and doesn't know if the space of an element object is represented
485 * directly or through its domain. It therefore passes along both.
487 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
488 __isl_take isl_space *space, __isl_take isl_space *domain)
490 isl_space_free(space);
491 return isl_aff_reset_domain_space(aff, domain);
494 /* Reorder the coefficients of the affine expression based
495 * on the given reordering.
496 * The reordering r is assumed to have been extended with the local
497 * variables.
499 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
500 __isl_take isl_reordering *r, int n_div)
502 isl_space *space;
503 isl_vec *res;
504 int i;
506 if (!vec || !r)
507 goto error;
509 space = isl_reordering_peek_space(r);
510 res = isl_vec_alloc(vec->ctx,
511 2 + isl_space_dim(space, isl_dim_all) + n_div);
512 if (!res)
513 goto error;
514 isl_seq_cpy(res->el, vec->el, 2);
515 isl_seq_clr(res->el + 2, res->size - 2);
516 for (i = 0; i < r->len; ++i)
517 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
519 isl_reordering_free(r);
520 isl_vec_free(vec);
521 return res;
522 error:
523 isl_vec_free(vec);
524 isl_reordering_free(r);
525 return NULL;
528 /* Reorder the dimensions of the domain of "aff" according
529 * to the given reordering.
531 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
532 __isl_take isl_reordering *r)
534 aff = isl_aff_cow(aff);
535 if (!aff)
536 goto error;
538 r = isl_reordering_extend(r, aff->ls->div->n_row);
539 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
540 aff->ls->div->n_row);
541 aff->ls = isl_local_space_realign(aff->ls, r);
543 if (!aff->v || !aff->ls)
544 return isl_aff_free(aff);
546 return aff;
547 error:
548 isl_aff_free(aff);
549 isl_reordering_free(r);
550 return NULL;
553 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
554 __isl_take isl_space *model)
556 isl_bool equal_params;
558 if (!aff || !model)
559 goto error;
561 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
562 if (equal_params < 0)
563 goto error;
564 if (!equal_params) {
565 isl_reordering *exp;
567 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
568 exp = isl_reordering_extend_space(exp,
569 isl_aff_get_domain_space(aff));
570 aff = isl_aff_realign_domain(aff, exp);
573 isl_space_free(model);
574 return aff;
575 error:
576 isl_space_free(model);
577 isl_aff_free(aff);
578 return NULL;
581 /* Is "aff" obviously equal to zero?
583 * If the denominator is zero, then "aff" is not equal to zero.
585 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
587 if (!aff)
588 return isl_bool_error;
590 if (isl_int_is_zero(aff->v->el[0]))
591 return isl_bool_false;
592 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
595 /* Does "aff" represent NaN?
597 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
599 if (!aff)
600 return isl_bool_error;
602 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
605 /* Are "aff1" and "aff2" obviously equal?
607 * NaN is not equal to anything, not even to another NaN.
609 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
610 __isl_keep isl_aff *aff2)
612 isl_bool equal;
614 if (!aff1 || !aff2)
615 return isl_bool_error;
617 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
618 return isl_bool_false;
620 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
621 if (equal < 0 || !equal)
622 return equal;
624 return isl_vec_is_equal(aff1->v, aff2->v);
627 /* Return the common denominator of "aff" in "v".
629 * We cannot return anything meaningful in case of a NaN.
631 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
633 if (!aff)
634 return isl_stat_error;
635 if (isl_aff_is_nan(aff))
636 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
637 "cannot get denominator of NaN", return isl_stat_error);
638 isl_int_set(*v, aff->v->el[0]);
639 return isl_stat_ok;
642 /* Return the common denominator of "aff".
644 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
646 isl_ctx *ctx;
648 if (!aff)
649 return NULL;
651 ctx = isl_aff_get_ctx(aff);
652 if (isl_aff_is_nan(aff))
653 return isl_val_nan(ctx);
654 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
657 /* Return the constant term of "aff".
659 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
661 isl_ctx *ctx;
662 isl_val *v;
664 if (!aff)
665 return NULL;
667 ctx = isl_aff_get_ctx(aff);
668 if (isl_aff_is_nan(aff))
669 return isl_val_nan(ctx);
670 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
671 return isl_val_normalize(v);
674 /* Return the coefficient of the variable of type "type" at position "pos"
675 * of "aff".
677 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
678 enum isl_dim_type type, int pos)
680 isl_ctx *ctx;
681 isl_val *v;
683 if (!aff)
684 return NULL;
686 ctx = isl_aff_get_ctx(aff);
687 if (type == isl_dim_out)
688 isl_die(ctx, isl_error_invalid,
689 "output/set dimension does not have a coefficient",
690 return NULL);
691 if (type == isl_dim_in)
692 type = isl_dim_set;
694 if (pos >= isl_local_space_dim(aff->ls, type))
695 isl_die(ctx, isl_error_invalid,
696 "position out of bounds", return NULL);
698 if (isl_aff_is_nan(aff))
699 return isl_val_nan(ctx);
700 pos += isl_local_space_offset(aff->ls, type);
701 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
702 return isl_val_normalize(v);
705 /* Return the sign of the coefficient of the variable of type "type"
706 * at position "pos" of "aff".
708 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
709 int pos)
711 isl_ctx *ctx;
713 if (!aff)
714 return 0;
716 ctx = isl_aff_get_ctx(aff);
717 if (type == isl_dim_out)
718 isl_die(ctx, isl_error_invalid,
719 "output/set dimension does not have a coefficient",
720 return 0);
721 if (type == isl_dim_in)
722 type = isl_dim_set;
724 if (pos >= isl_local_space_dim(aff->ls, type))
725 isl_die(ctx, isl_error_invalid,
726 "position out of bounds", return 0);
728 pos += isl_local_space_offset(aff->ls, type);
729 return isl_int_sgn(aff->v->el[1 + pos]);
732 /* Replace the numerator of the constant term of "aff" by "v".
734 * A NaN is unaffected by this operation.
736 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
738 if (!aff)
739 return NULL;
740 if (isl_aff_is_nan(aff))
741 return aff;
742 aff = isl_aff_cow(aff);
743 if (!aff)
744 return NULL;
746 aff->v = isl_vec_cow(aff->v);
747 if (!aff->v)
748 return isl_aff_free(aff);
750 isl_int_set(aff->v->el[1], v);
752 return aff;
755 /* Replace the constant term of "aff" by "v".
757 * A NaN is unaffected by this operation.
759 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
760 __isl_take isl_val *v)
762 if (!aff || !v)
763 goto error;
765 if (isl_aff_is_nan(aff)) {
766 isl_val_free(v);
767 return aff;
770 if (!isl_val_is_rat(v))
771 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
772 "expecting rational value", goto error);
774 if (isl_int_eq(aff->v->el[1], v->n) &&
775 isl_int_eq(aff->v->el[0], v->d)) {
776 isl_val_free(v);
777 return aff;
780 aff = isl_aff_cow(aff);
781 if (!aff)
782 goto error;
783 aff->v = isl_vec_cow(aff->v);
784 if (!aff->v)
785 goto error;
787 if (isl_int_eq(aff->v->el[0], v->d)) {
788 isl_int_set(aff->v->el[1], v->n);
789 } else if (isl_int_is_one(v->d)) {
790 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
791 } else {
792 isl_seq_scale(aff->v->el + 1,
793 aff->v->el + 1, v->d, aff->v->size - 1);
794 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
795 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
796 aff->v = isl_vec_normalize(aff->v);
797 if (!aff->v)
798 goto error;
801 isl_val_free(v);
802 return aff;
803 error:
804 isl_aff_free(aff);
805 isl_val_free(v);
806 return NULL;
809 /* Add "v" to the constant term of "aff".
811 * A NaN is unaffected by this operation.
813 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
815 if (isl_int_is_zero(v))
816 return aff;
818 if (!aff)
819 return NULL;
820 if (isl_aff_is_nan(aff))
821 return aff;
822 aff = isl_aff_cow(aff);
823 if (!aff)
824 return NULL;
826 aff->v = isl_vec_cow(aff->v);
827 if (!aff->v)
828 return isl_aff_free(aff);
830 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
832 return aff;
835 /* Add "v" to the constant term of "aff".
837 * A NaN is unaffected by this operation.
839 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
840 __isl_take isl_val *v)
842 if (!aff || !v)
843 goto error;
845 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
846 isl_val_free(v);
847 return aff;
850 if (!isl_val_is_rat(v))
851 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
852 "expecting rational value", goto error);
854 aff = isl_aff_cow(aff);
855 if (!aff)
856 goto error;
858 aff->v = isl_vec_cow(aff->v);
859 if (!aff->v)
860 goto error;
862 if (isl_int_is_one(v->d)) {
863 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
864 } else if (isl_int_eq(aff->v->el[0], v->d)) {
865 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
866 aff->v = isl_vec_normalize(aff->v);
867 if (!aff->v)
868 goto error;
869 } else {
870 isl_seq_scale(aff->v->el + 1,
871 aff->v->el + 1, v->d, aff->v->size - 1);
872 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
873 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
874 aff->v = isl_vec_normalize(aff->v);
875 if (!aff->v)
876 goto error;
879 isl_val_free(v);
880 return aff;
881 error:
882 isl_aff_free(aff);
883 isl_val_free(v);
884 return NULL;
887 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
889 isl_int t;
891 isl_int_init(t);
892 isl_int_set_si(t, v);
893 aff = isl_aff_add_constant(aff, t);
894 isl_int_clear(t);
896 return aff;
899 /* Add "v" to the numerator of the constant term of "aff".
901 * A NaN is unaffected by this operation.
903 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
905 if (isl_int_is_zero(v))
906 return aff;
908 if (!aff)
909 return NULL;
910 if (isl_aff_is_nan(aff))
911 return aff;
912 aff = isl_aff_cow(aff);
913 if (!aff)
914 return NULL;
916 aff->v = isl_vec_cow(aff->v);
917 if (!aff->v)
918 return isl_aff_free(aff);
920 isl_int_add(aff->v->el[1], aff->v->el[1], v);
922 return aff;
925 /* Add "v" to the numerator of the constant term of "aff".
927 * A NaN is unaffected by this operation.
929 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
931 isl_int t;
933 if (v == 0)
934 return aff;
936 isl_int_init(t);
937 isl_int_set_si(t, v);
938 aff = isl_aff_add_constant_num(aff, t);
939 isl_int_clear(t);
941 return aff;
944 /* Replace the numerator of the constant term of "aff" by "v".
946 * A NaN is unaffected by this operation.
948 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
950 if (!aff)
951 return NULL;
952 if (isl_aff_is_nan(aff))
953 return aff;
954 aff = isl_aff_cow(aff);
955 if (!aff)
956 return NULL;
958 aff->v = isl_vec_cow(aff->v);
959 if (!aff->v)
960 return isl_aff_free(aff);
962 isl_int_set_si(aff->v->el[1], v);
964 return aff;
967 /* Replace the numerator of the coefficient of the variable of type "type"
968 * at position "pos" of "aff" by "v".
970 * A NaN is unaffected by this operation.
972 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
973 enum isl_dim_type type, int pos, isl_int v)
975 if (!aff)
976 return NULL;
978 if (type == isl_dim_out)
979 isl_die(aff->v->ctx, isl_error_invalid,
980 "output/set dimension does not have a coefficient",
981 return isl_aff_free(aff));
982 if (type == isl_dim_in)
983 type = isl_dim_set;
985 if (pos >= isl_local_space_dim(aff->ls, type))
986 isl_die(aff->v->ctx, isl_error_invalid,
987 "position out of bounds", return isl_aff_free(aff));
989 if (isl_aff_is_nan(aff))
990 return aff;
991 aff = isl_aff_cow(aff);
992 if (!aff)
993 return NULL;
995 aff->v = isl_vec_cow(aff->v);
996 if (!aff->v)
997 return isl_aff_free(aff);
999 pos += isl_local_space_offset(aff->ls, type);
1000 isl_int_set(aff->v->el[1 + pos], v);
1002 return aff;
1005 /* Replace the numerator of the coefficient of the variable of type "type"
1006 * at position "pos" of "aff" by "v".
1008 * A NaN is unaffected by this operation.
1010 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1011 enum isl_dim_type type, int pos, int v)
1013 if (!aff)
1014 return NULL;
1016 if (type == isl_dim_out)
1017 isl_die(aff->v->ctx, isl_error_invalid,
1018 "output/set dimension does not have a coefficient",
1019 return isl_aff_free(aff));
1020 if (type == isl_dim_in)
1021 type = isl_dim_set;
1023 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1024 isl_die(aff->v->ctx, isl_error_invalid,
1025 "position out of bounds", return isl_aff_free(aff));
1027 if (isl_aff_is_nan(aff))
1028 return aff;
1029 pos += isl_local_space_offset(aff->ls, type);
1030 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1031 return aff;
1033 aff = isl_aff_cow(aff);
1034 if (!aff)
1035 return NULL;
1037 aff->v = isl_vec_cow(aff->v);
1038 if (!aff->v)
1039 return isl_aff_free(aff);
1041 isl_int_set_si(aff->v->el[1 + pos], v);
1043 return aff;
1046 /* Replace the coefficient of the variable of type "type" at position "pos"
1047 * of "aff" by "v".
1049 * A NaN is unaffected by this operation.
1051 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1052 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1054 if (!aff || !v)
1055 goto error;
1057 if (type == isl_dim_out)
1058 isl_die(aff->v->ctx, isl_error_invalid,
1059 "output/set dimension does not have a coefficient",
1060 goto error);
1061 if (type == isl_dim_in)
1062 type = isl_dim_set;
1064 if (pos >= isl_local_space_dim(aff->ls, type))
1065 isl_die(aff->v->ctx, isl_error_invalid,
1066 "position out of bounds", goto error);
1068 if (isl_aff_is_nan(aff)) {
1069 isl_val_free(v);
1070 return aff;
1072 if (!isl_val_is_rat(v))
1073 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1074 "expecting rational value", goto error);
1076 pos += isl_local_space_offset(aff->ls, type);
1077 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1078 isl_int_eq(aff->v->el[0], v->d)) {
1079 isl_val_free(v);
1080 return aff;
1083 aff = isl_aff_cow(aff);
1084 if (!aff)
1085 goto error;
1086 aff->v = isl_vec_cow(aff->v);
1087 if (!aff->v)
1088 goto error;
1090 if (isl_int_eq(aff->v->el[0], v->d)) {
1091 isl_int_set(aff->v->el[1 + pos], v->n);
1092 } else if (isl_int_is_one(v->d)) {
1093 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1094 } else {
1095 isl_seq_scale(aff->v->el + 1,
1096 aff->v->el + 1, v->d, aff->v->size - 1);
1097 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1098 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1099 aff->v = isl_vec_normalize(aff->v);
1100 if (!aff->v)
1101 goto error;
1104 isl_val_free(v);
1105 return aff;
1106 error:
1107 isl_aff_free(aff);
1108 isl_val_free(v);
1109 return NULL;
1112 /* Add "v" to the coefficient of the variable of type "type"
1113 * at position "pos" of "aff".
1115 * A NaN is unaffected by this operation.
1117 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1118 enum isl_dim_type type, int pos, isl_int v)
1120 if (!aff)
1121 return NULL;
1123 if (type == isl_dim_out)
1124 isl_die(aff->v->ctx, isl_error_invalid,
1125 "output/set dimension does not have a coefficient",
1126 return isl_aff_free(aff));
1127 if (type == isl_dim_in)
1128 type = isl_dim_set;
1130 if (pos >= isl_local_space_dim(aff->ls, type))
1131 isl_die(aff->v->ctx, isl_error_invalid,
1132 "position out of bounds", return isl_aff_free(aff));
1134 if (isl_aff_is_nan(aff))
1135 return aff;
1136 aff = isl_aff_cow(aff);
1137 if (!aff)
1138 return NULL;
1140 aff->v = isl_vec_cow(aff->v);
1141 if (!aff->v)
1142 return isl_aff_free(aff);
1144 pos += isl_local_space_offset(aff->ls, type);
1145 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1147 return aff;
1150 /* Add "v" to the coefficient of the variable of type "type"
1151 * at position "pos" of "aff".
1153 * A NaN is unaffected by this operation.
1155 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1156 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1158 if (!aff || !v)
1159 goto error;
1161 if (isl_val_is_zero(v)) {
1162 isl_val_free(v);
1163 return aff;
1166 if (type == isl_dim_out)
1167 isl_die(aff->v->ctx, isl_error_invalid,
1168 "output/set dimension does not have a coefficient",
1169 goto error);
1170 if (type == isl_dim_in)
1171 type = isl_dim_set;
1173 if (pos >= isl_local_space_dim(aff->ls, type))
1174 isl_die(aff->v->ctx, isl_error_invalid,
1175 "position out of bounds", goto error);
1177 if (isl_aff_is_nan(aff)) {
1178 isl_val_free(v);
1179 return aff;
1181 if (!isl_val_is_rat(v))
1182 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1183 "expecting rational value", goto error);
1185 aff = isl_aff_cow(aff);
1186 if (!aff)
1187 goto error;
1189 aff->v = isl_vec_cow(aff->v);
1190 if (!aff->v)
1191 goto error;
1193 pos += isl_local_space_offset(aff->ls, type);
1194 if (isl_int_is_one(v->d)) {
1195 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1196 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1197 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1198 aff->v = isl_vec_normalize(aff->v);
1199 if (!aff->v)
1200 goto error;
1201 } else {
1202 isl_seq_scale(aff->v->el + 1,
1203 aff->v->el + 1, v->d, aff->v->size - 1);
1204 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1205 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1206 aff->v = isl_vec_normalize(aff->v);
1207 if (!aff->v)
1208 goto error;
1211 isl_val_free(v);
1212 return aff;
1213 error:
1214 isl_aff_free(aff);
1215 isl_val_free(v);
1216 return NULL;
1219 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1220 enum isl_dim_type type, int pos, int v)
1222 isl_int t;
1224 isl_int_init(t);
1225 isl_int_set_si(t, v);
1226 aff = isl_aff_add_coefficient(aff, type, pos, t);
1227 isl_int_clear(t);
1229 return aff;
1232 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1234 if (!aff)
1235 return NULL;
1237 return isl_local_space_get_div(aff->ls, pos);
1240 /* Return the negation of "aff".
1242 * As a special case, -NaN = NaN.
1244 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1246 if (!aff)
1247 return NULL;
1248 if (isl_aff_is_nan(aff))
1249 return aff;
1250 aff = isl_aff_cow(aff);
1251 if (!aff)
1252 return NULL;
1253 aff->v = isl_vec_cow(aff->v);
1254 if (!aff->v)
1255 return isl_aff_free(aff);
1257 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1259 return aff;
1262 /* Remove divs from the local space that do not appear in the affine
1263 * expression.
1264 * We currently only remove divs at the end.
1265 * Some intermediate divs may also not appear directly in the affine
1266 * expression, but we would also need to check that no other divs are
1267 * defined in terms of them.
1269 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1271 int pos;
1272 int off;
1273 int n;
1275 if (!aff)
1276 return NULL;
1278 n = isl_local_space_dim(aff->ls, isl_dim_div);
1279 off = isl_local_space_offset(aff->ls, isl_dim_div);
1281 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1282 if (pos == n)
1283 return aff;
1285 aff = isl_aff_cow(aff);
1286 if (!aff)
1287 return NULL;
1289 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1290 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1291 if (!aff->ls || !aff->v)
1292 return isl_aff_free(aff);
1294 return aff;
1297 /* Look for any divs in the aff->ls with a denominator equal to one
1298 * and plug them into the affine expression and any subsequent divs
1299 * that may reference the div.
1301 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1303 int i, n;
1304 int len;
1305 isl_int v;
1306 isl_vec *vec;
1307 isl_local_space *ls;
1308 unsigned pos;
1310 if (!aff)
1311 return NULL;
1313 n = isl_local_space_dim(aff->ls, isl_dim_div);
1314 len = aff->v->size;
1315 for (i = 0; i < n; ++i) {
1316 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1317 continue;
1318 ls = isl_local_space_copy(aff->ls);
1319 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1320 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1321 vec = isl_vec_copy(aff->v);
1322 vec = isl_vec_cow(vec);
1323 if (!ls || !vec)
1324 goto error;
1326 isl_int_init(v);
1328 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1329 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1330 len, len, v);
1332 isl_int_clear(v);
1334 isl_vec_free(aff->v);
1335 aff->v = vec;
1336 isl_local_space_free(aff->ls);
1337 aff->ls = ls;
1340 return aff;
1341 error:
1342 isl_vec_free(vec);
1343 isl_local_space_free(ls);
1344 return isl_aff_free(aff);
1347 /* Look for any divs j that appear with a unit coefficient inside
1348 * the definitions of other divs i and plug them into the definitions
1349 * of the divs i.
1351 * In particular, an expression of the form
1353 * floor((f(..) + floor(g(..)/n))/m)
1355 * is simplified to
1357 * floor((n * f(..) + g(..))/(n * m))
1359 * This simplification is correct because we can move the expression
1360 * f(..) into the inner floor in the original expression to obtain
1362 * floor(floor((n * f(..) + g(..))/n)/m)
1364 * from which we can derive the simplified expression.
1366 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1368 int i, j, n;
1369 int off;
1371 if (!aff)
1372 return NULL;
1374 n = isl_local_space_dim(aff->ls, isl_dim_div);
1375 off = isl_local_space_offset(aff->ls, isl_dim_div);
1376 for (i = 1; i < n; ++i) {
1377 for (j = 0; j < i; ++j) {
1378 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1379 continue;
1380 aff->ls = isl_local_space_substitute_seq(aff->ls,
1381 isl_dim_div, j, aff->ls->div->row[j],
1382 aff->v->size, i, 1);
1383 if (!aff->ls)
1384 return isl_aff_free(aff);
1388 return aff;
1391 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1393 * Even though this function is only called on isl_affs with a single
1394 * reference, we are careful to only change aff->v and aff->ls together.
1396 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1398 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1399 isl_local_space *ls;
1400 isl_vec *v;
1402 ls = isl_local_space_copy(aff->ls);
1403 ls = isl_local_space_swap_div(ls, a, b);
1404 v = isl_vec_copy(aff->v);
1405 v = isl_vec_cow(v);
1406 if (!ls || !v)
1407 goto error;
1409 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1410 isl_vec_free(aff->v);
1411 aff->v = v;
1412 isl_local_space_free(aff->ls);
1413 aff->ls = ls;
1415 return aff;
1416 error:
1417 isl_vec_free(v);
1418 isl_local_space_free(ls);
1419 return isl_aff_free(aff);
1422 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1424 * We currently do not actually remove div "b", but simply add its
1425 * coefficient to that of "a" and then zero it out.
1427 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1429 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1431 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1432 return aff;
1434 aff->v = isl_vec_cow(aff->v);
1435 if (!aff->v)
1436 return isl_aff_free(aff);
1438 isl_int_add(aff->v->el[1 + off + a],
1439 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1440 isl_int_set_si(aff->v->el[1 + off + b], 0);
1442 return aff;
1445 /* Sort the divs in the local space of "aff" according to
1446 * the comparison function "cmp_row" in isl_local_space.c,
1447 * combining the coefficients of identical divs.
1449 * Reordering divs does not change the semantics of "aff",
1450 * so there is no need to call isl_aff_cow.
1451 * Moreover, this function is currently only called on isl_affs
1452 * with a single reference.
1454 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1456 int i, j, n;
1458 if (!aff)
1459 return NULL;
1461 n = isl_aff_dim(aff, isl_dim_div);
1462 for (i = 1; i < n; ++i) {
1463 for (j = i - 1; j >= 0; --j) {
1464 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1465 if (cmp < 0)
1466 break;
1467 if (cmp == 0)
1468 aff = merge_divs(aff, j, j + 1);
1469 else
1470 aff = swap_div(aff, j, j + 1);
1471 if (!aff)
1472 return NULL;
1476 return aff;
1479 /* Normalize the representation of "aff".
1481 * This function should only be called of "new" isl_affs, i.e.,
1482 * with only a single reference. We therefore do not need to
1483 * worry about affecting other instances.
1485 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1487 if (!aff)
1488 return NULL;
1489 aff->v = isl_vec_normalize(aff->v);
1490 if (!aff->v)
1491 return isl_aff_free(aff);
1492 aff = plug_in_integral_divs(aff);
1493 aff = plug_in_unit_divs(aff);
1494 aff = sort_divs(aff);
1495 aff = isl_aff_remove_unused_divs(aff);
1496 return aff;
1499 /* Given f, return floor(f).
1500 * If f is an integer expression, then just return f.
1501 * If f is a constant, then return the constant floor(f).
1502 * Otherwise, if f = g/m, write g = q m + r,
1503 * create a new div d = [r/m] and return the expression q + d.
1504 * The coefficients in r are taken to lie between -m/2 and m/2.
1506 * reduce_div_coefficients performs the same normalization.
1508 * As a special case, floor(NaN) = NaN.
1510 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1512 int i;
1513 int size;
1514 isl_ctx *ctx;
1515 isl_vec *div;
1517 if (!aff)
1518 return NULL;
1520 if (isl_aff_is_nan(aff))
1521 return aff;
1522 if (isl_int_is_one(aff->v->el[0]))
1523 return aff;
1525 aff = isl_aff_cow(aff);
1526 if (!aff)
1527 return NULL;
1529 aff->v = isl_vec_cow(aff->v);
1530 if (!aff->v)
1531 return isl_aff_free(aff);
1533 if (isl_aff_is_cst(aff)) {
1534 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1535 isl_int_set_si(aff->v->el[0], 1);
1536 return aff;
1539 div = isl_vec_copy(aff->v);
1540 div = isl_vec_cow(div);
1541 if (!div)
1542 return isl_aff_free(aff);
1544 ctx = isl_aff_get_ctx(aff);
1545 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1546 for (i = 1; i < aff->v->size; ++i) {
1547 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1548 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1549 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1550 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1551 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1555 aff->ls = isl_local_space_add_div(aff->ls, div);
1556 if (!aff->ls)
1557 return isl_aff_free(aff);
1559 size = aff->v->size;
1560 aff->v = isl_vec_extend(aff->v, size + 1);
1561 if (!aff->v)
1562 return isl_aff_free(aff);
1563 isl_int_set_si(aff->v->el[0], 1);
1564 isl_int_set_si(aff->v->el[size], 1);
1566 aff = isl_aff_normalize(aff);
1568 return aff;
1571 /* Compute
1573 * aff mod m = aff - m * floor(aff/m)
1575 * with m an integer value.
1577 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1578 __isl_take isl_val *m)
1580 isl_aff *res;
1582 if (!aff || !m)
1583 goto error;
1585 if (!isl_val_is_int(m))
1586 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1587 "expecting integer modulo", goto error);
1589 res = isl_aff_copy(aff);
1590 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1591 aff = isl_aff_floor(aff);
1592 aff = isl_aff_scale_val(aff, m);
1593 res = isl_aff_sub(res, aff);
1595 return res;
1596 error:
1597 isl_aff_free(aff);
1598 isl_val_free(m);
1599 return NULL;
1602 /* Compute
1604 * pwaff mod m = pwaff - m * floor(pwaff/m)
1606 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1608 isl_pw_aff *res;
1610 res = isl_pw_aff_copy(pwaff);
1611 pwaff = isl_pw_aff_scale_down(pwaff, m);
1612 pwaff = isl_pw_aff_floor(pwaff);
1613 pwaff = isl_pw_aff_scale(pwaff, m);
1614 res = isl_pw_aff_sub(res, pwaff);
1616 return res;
1619 /* Compute
1621 * pa mod m = pa - m * floor(pa/m)
1623 * with m an integer value.
1625 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1626 __isl_take isl_val *m)
1628 if (!pa || !m)
1629 goto error;
1630 if (!isl_val_is_int(m))
1631 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1632 "expecting integer modulo", goto error);
1633 pa = isl_pw_aff_mod(pa, m->n);
1634 isl_val_free(m);
1635 return pa;
1636 error:
1637 isl_pw_aff_free(pa);
1638 isl_val_free(m);
1639 return NULL;
1642 /* Given f, return ceil(f).
1643 * If f is an integer expression, then just return f.
1644 * Otherwise, let f be the expression
1646 * e/m
1648 * then return
1650 * floor((e + m - 1)/m)
1652 * As a special case, ceil(NaN) = NaN.
1654 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1656 if (!aff)
1657 return NULL;
1659 if (isl_aff_is_nan(aff))
1660 return aff;
1661 if (isl_int_is_one(aff->v->el[0]))
1662 return aff;
1664 aff = isl_aff_cow(aff);
1665 if (!aff)
1666 return NULL;
1667 aff->v = isl_vec_cow(aff->v);
1668 if (!aff->v)
1669 return isl_aff_free(aff);
1671 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1672 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1673 aff = isl_aff_floor(aff);
1675 return aff;
1678 /* Apply the expansion computed by isl_merge_divs.
1679 * The expansion itself is given by "exp" while the resulting
1680 * list of divs is given by "div".
1682 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1683 __isl_take isl_mat *div, int *exp)
1685 int old_n_div;
1686 int new_n_div;
1687 int offset;
1689 aff = isl_aff_cow(aff);
1690 if (!aff || !div)
1691 goto error;
1693 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1694 new_n_div = isl_mat_rows(div);
1695 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1697 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1698 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1699 if (!aff->v || !aff->ls)
1700 return isl_aff_free(aff);
1701 return aff;
1702 error:
1703 isl_aff_free(aff);
1704 isl_mat_free(div);
1705 return NULL;
1708 /* Add two affine expressions that live in the same local space.
1710 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1711 __isl_take isl_aff *aff2)
1713 isl_int gcd, f;
1715 aff1 = isl_aff_cow(aff1);
1716 if (!aff1 || !aff2)
1717 goto error;
1719 aff1->v = isl_vec_cow(aff1->v);
1720 if (!aff1->v)
1721 goto error;
1723 isl_int_init(gcd);
1724 isl_int_init(f);
1725 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1726 isl_int_divexact(f, aff2->v->el[0], gcd);
1727 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1728 isl_int_divexact(f, aff1->v->el[0], gcd);
1729 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1730 isl_int_divexact(f, aff2->v->el[0], gcd);
1731 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1732 isl_int_clear(f);
1733 isl_int_clear(gcd);
1735 isl_aff_free(aff2);
1736 return aff1;
1737 error:
1738 isl_aff_free(aff1);
1739 isl_aff_free(aff2);
1740 return NULL;
1743 /* Return the sum of "aff1" and "aff2".
1745 * If either of the two is NaN, then the result is NaN.
1747 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1748 __isl_take isl_aff *aff2)
1750 isl_ctx *ctx;
1751 int *exp1 = NULL;
1752 int *exp2 = NULL;
1753 isl_mat *div;
1754 int n_div1, n_div2;
1756 if (!aff1 || !aff2)
1757 goto error;
1759 ctx = isl_aff_get_ctx(aff1);
1760 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1761 isl_die(ctx, isl_error_invalid,
1762 "spaces don't match", goto error);
1764 if (isl_aff_is_nan(aff1)) {
1765 isl_aff_free(aff2);
1766 return aff1;
1768 if (isl_aff_is_nan(aff2)) {
1769 isl_aff_free(aff1);
1770 return aff2;
1773 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1774 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1775 if (n_div1 == 0 && n_div2 == 0)
1776 return add_expanded(aff1, aff2);
1778 exp1 = isl_alloc_array(ctx, int, n_div1);
1779 exp2 = isl_alloc_array(ctx, int, n_div2);
1780 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1781 goto error;
1783 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1784 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1785 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1786 free(exp1);
1787 free(exp2);
1789 return add_expanded(aff1, aff2);
1790 error:
1791 free(exp1);
1792 free(exp2);
1793 isl_aff_free(aff1);
1794 isl_aff_free(aff2);
1795 return NULL;
1798 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1799 __isl_take isl_aff *aff2)
1801 return isl_aff_add(aff1, isl_aff_neg(aff2));
1804 /* Return the result of scaling "aff" by a factor of "f".
1806 * As a special case, f * NaN = NaN.
1808 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1810 isl_int gcd;
1812 if (!aff)
1813 return NULL;
1814 if (isl_aff_is_nan(aff))
1815 return aff;
1817 if (isl_int_is_one(f))
1818 return aff;
1820 aff = isl_aff_cow(aff);
1821 if (!aff)
1822 return NULL;
1823 aff->v = isl_vec_cow(aff->v);
1824 if (!aff->v)
1825 return isl_aff_free(aff);
1827 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1828 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1829 return aff;
1832 isl_int_init(gcd);
1833 isl_int_gcd(gcd, aff->v->el[0], f);
1834 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1835 isl_int_divexact(gcd, f, gcd);
1836 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1837 isl_int_clear(gcd);
1839 return aff;
1842 /* Multiple "aff" by "v".
1844 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1845 __isl_take isl_val *v)
1847 if (!aff || !v)
1848 goto error;
1850 if (isl_val_is_one(v)) {
1851 isl_val_free(v);
1852 return aff;
1855 if (!isl_val_is_rat(v))
1856 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1857 "expecting rational factor", goto error);
1859 aff = isl_aff_scale(aff, v->n);
1860 aff = isl_aff_scale_down(aff, v->d);
1862 isl_val_free(v);
1863 return aff;
1864 error:
1865 isl_aff_free(aff);
1866 isl_val_free(v);
1867 return NULL;
1870 /* Return the result of scaling "aff" down by a factor of "f".
1872 * As a special case, NaN/f = NaN.
1874 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1876 isl_int gcd;
1878 if (!aff)
1879 return NULL;
1880 if (isl_aff_is_nan(aff))
1881 return aff;
1883 if (isl_int_is_one(f))
1884 return aff;
1886 aff = isl_aff_cow(aff);
1887 if (!aff)
1888 return NULL;
1890 if (isl_int_is_zero(f))
1891 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1892 "cannot scale down by zero", return isl_aff_free(aff));
1894 aff->v = isl_vec_cow(aff->v);
1895 if (!aff->v)
1896 return isl_aff_free(aff);
1898 isl_int_init(gcd);
1899 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1900 isl_int_gcd(gcd, gcd, f);
1901 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1902 isl_int_divexact(gcd, f, gcd);
1903 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1904 isl_int_clear(gcd);
1906 return aff;
1909 /* Divide "aff" by "v".
1911 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1912 __isl_take isl_val *v)
1914 if (!aff || !v)
1915 goto error;
1917 if (isl_val_is_one(v)) {
1918 isl_val_free(v);
1919 return aff;
1922 if (!isl_val_is_rat(v))
1923 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1924 "expecting rational factor", goto error);
1925 if (!isl_val_is_pos(v))
1926 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1927 "factor needs to be positive", goto error);
1929 aff = isl_aff_scale(aff, v->d);
1930 aff = isl_aff_scale_down(aff, v->n);
1932 isl_val_free(v);
1933 return aff;
1934 error:
1935 isl_aff_free(aff);
1936 isl_val_free(v);
1937 return NULL;
1940 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1942 isl_int v;
1944 if (f == 1)
1945 return aff;
1947 isl_int_init(v);
1948 isl_int_set_ui(v, f);
1949 aff = isl_aff_scale_down(aff, v);
1950 isl_int_clear(v);
1952 return aff;
1955 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1956 enum isl_dim_type type, unsigned pos, const char *s)
1958 aff = isl_aff_cow(aff);
1959 if (!aff)
1960 return NULL;
1961 if (type == isl_dim_out)
1962 isl_die(aff->v->ctx, isl_error_invalid,
1963 "cannot set name of output/set dimension",
1964 return isl_aff_free(aff));
1965 if (type == isl_dim_in)
1966 type = isl_dim_set;
1967 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1968 if (!aff->ls)
1969 return isl_aff_free(aff);
1971 return aff;
1974 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1975 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1977 aff = isl_aff_cow(aff);
1978 if (!aff)
1979 goto error;
1980 if (type == isl_dim_out)
1981 isl_die(aff->v->ctx, isl_error_invalid,
1982 "cannot set name of output/set dimension",
1983 goto error);
1984 if (type == isl_dim_in)
1985 type = isl_dim_set;
1986 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1987 if (!aff->ls)
1988 return isl_aff_free(aff);
1990 return aff;
1991 error:
1992 isl_id_free(id);
1993 isl_aff_free(aff);
1994 return NULL;
1997 /* Replace the identifier of the input tuple of "aff" by "id".
1998 * type is currently required to be equal to isl_dim_in
2000 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2001 enum isl_dim_type type, __isl_take isl_id *id)
2003 aff = isl_aff_cow(aff);
2004 if (!aff)
2005 goto error;
2006 if (type != isl_dim_in)
2007 isl_die(aff->v->ctx, isl_error_invalid,
2008 "cannot only set id of input tuple", goto error);
2009 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2010 if (!aff->ls)
2011 return isl_aff_free(aff);
2013 return aff;
2014 error:
2015 isl_id_free(id);
2016 isl_aff_free(aff);
2017 return NULL;
2020 /* Exploit the equalities in "eq" to simplify the affine expression
2021 * and the expressions of the integer divisions in the local space.
2022 * The integer divisions in this local space are assumed to appear
2023 * as regular dimensions in "eq".
2025 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2026 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2028 int i, j;
2029 unsigned total;
2030 unsigned n_div;
2032 if (!eq)
2033 goto error;
2034 if (eq->n_eq == 0) {
2035 isl_basic_set_free(eq);
2036 return aff;
2039 aff = isl_aff_cow(aff);
2040 if (!aff)
2041 goto error;
2043 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2044 isl_basic_set_copy(eq));
2045 aff->v = isl_vec_cow(aff->v);
2046 if (!aff->ls || !aff->v)
2047 goto error;
2049 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2050 n_div = eq->n_div;
2051 for (i = 0; i < eq->n_eq; ++i) {
2052 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2053 if (j < 0 || j == 0 || j >= total)
2054 continue;
2056 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2057 &aff->v->el[0]);
2060 isl_basic_set_free(eq);
2061 aff = isl_aff_normalize(aff);
2062 return aff;
2063 error:
2064 isl_basic_set_free(eq);
2065 isl_aff_free(aff);
2066 return NULL;
2069 /* Exploit the equalities in "eq" to simplify the affine expression
2070 * and the expressions of the integer divisions in the local space.
2072 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2073 __isl_take isl_basic_set *eq)
2075 int n_div;
2077 if (!aff || !eq)
2078 goto error;
2079 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2080 if (n_div > 0)
2081 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2082 return isl_aff_substitute_equalities_lifted(aff, eq);
2083 error:
2084 isl_basic_set_free(eq);
2085 isl_aff_free(aff);
2086 return NULL;
2089 /* Look for equalities among the variables shared by context and aff
2090 * and the integer divisions of aff, if any.
2091 * The equalities are then used to eliminate coefficients and/or integer
2092 * divisions from aff.
2094 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2095 __isl_take isl_set *context)
2097 isl_basic_set *hull;
2098 int n_div;
2100 if (!aff)
2101 goto error;
2102 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2103 if (n_div > 0) {
2104 isl_basic_set *bset;
2105 isl_local_space *ls;
2106 context = isl_set_add_dims(context, isl_dim_set, n_div);
2107 ls = isl_aff_get_domain_local_space(aff);
2108 bset = isl_basic_set_from_local_space(ls);
2109 bset = isl_basic_set_lift(bset);
2110 bset = isl_basic_set_flatten(bset);
2111 context = isl_set_intersect(context,
2112 isl_set_from_basic_set(bset));
2115 hull = isl_set_affine_hull(context);
2116 return isl_aff_substitute_equalities_lifted(aff, hull);
2117 error:
2118 isl_aff_free(aff);
2119 isl_set_free(context);
2120 return NULL;
2123 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2124 __isl_take isl_set *context)
2126 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2127 dom_context = isl_set_intersect_params(dom_context, context);
2128 return isl_aff_gist(aff, dom_context);
2131 /* Return a basic set containing those elements in the space
2132 * of aff where it is positive. "rational" should not be set.
2134 * If "aff" is NaN, then it is not positive.
2136 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2137 int rational)
2139 isl_constraint *ineq;
2140 isl_basic_set *bset;
2141 isl_val *c;
2143 if (!aff)
2144 return NULL;
2145 if (isl_aff_is_nan(aff)) {
2146 isl_space *space = isl_aff_get_domain_space(aff);
2147 isl_aff_free(aff);
2148 return isl_basic_set_empty(space);
2150 if (rational)
2151 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2152 "rational sets not supported", goto error);
2154 ineq = isl_inequality_from_aff(aff);
2155 c = isl_constraint_get_constant_val(ineq);
2156 c = isl_val_sub_ui(c, 1);
2157 ineq = isl_constraint_set_constant_val(ineq, c);
2159 bset = isl_basic_set_from_constraint(ineq);
2160 bset = isl_basic_set_simplify(bset);
2161 return bset;
2162 error:
2163 isl_aff_free(aff);
2164 return NULL;
2167 /* Return a basic set containing those elements in the space
2168 * of aff where it is non-negative.
2169 * If "rational" is set, then return a rational basic set.
2171 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2173 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2174 __isl_take isl_aff *aff, int rational)
2176 isl_constraint *ineq;
2177 isl_basic_set *bset;
2179 if (!aff)
2180 return NULL;
2181 if (isl_aff_is_nan(aff)) {
2182 isl_space *space = isl_aff_get_domain_space(aff);
2183 isl_aff_free(aff);
2184 return isl_basic_set_empty(space);
2187 ineq = isl_inequality_from_aff(aff);
2189 bset = isl_basic_set_from_constraint(ineq);
2190 if (rational)
2191 bset = isl_basic_set_set_rational(bset);
2192 bset = isl_basic_set_simplify(bset);
2193 return bset;
2196 /* Return a basic set containing those elements in the space
2197 * of aff where it is non-negative.
2199 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2201 return aff_nonneg_basic_set(aff, 0);
2204 /* Return a basic set containing those elements in the domain space
2205 * of "aff" where it is positive.
2207 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2209 aff = isl_aff_add_constant_num_si(aff, -1);
2210 return isl_aff_nonneg_basic_set(aff);
2213 /* Return a basic set containing those elements in the domain space
2214 * of aff where it is negative.
2216 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2218 aff = isl_aff_neg(aff);
2219 return isl_aff_pos_basic_set(aff);
2222 /* Return a basic set containing those elements in the space
2223 * of aff where it is zero.
2224 * If "rational" is set, then return a rational basic set.
2226 * If "aff" is NaN, then it is not zero.
2228 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2229 int rational)
2231 isl_constraint *ineq;
2232 isl_basic_set *bset;
2234 if (!aff)
2235 return NULL;
2236 if (isl_aff_is_nan(aff)) {
2237 isl_space *space = isl_aff_get_domain_space(aff);
2238 isl_aff_free(aff);
2239 return isl_basic_set_empty(space);
2242 ineq = isl_equality_from_aff(aff);
2244 bset = isl_basic_set_from_constraint(ineq);
2245 if (rational)
2246 bset = isl_basic_set_set_rational(bset);
2247 bset = isl_basic_set_simplify(bset);
2248 return bset;
2251 /* Return a basic set containing those elements in the space
2252 * of aff where it is zero.
2254 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2256 return aff_zero_basic_set(aff, 0);
2259 /* Return a basic set containing those elements in the shared space
2260 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2262 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2263 __isl_take isl_aff *aff2)
2265 aff1 = isl_aff_sub(aff1, aff2);
2267 return isl_aff_nonneg_basic_set(aff1);
2270 /* Return a basic set containing those elements in the shared domain space
2271 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2273 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2274 __isl_take isl_aff *aff2)
2276 aff1 = isl_aff_sub(aff1, aff2);
2278 return isl_aff_pos_basic_set(aff1);
2281 /* Return a set containing those elements in the shared space
2282 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2284 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2285 __isl_take isl_aff *aff2)
2287 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2290 /* Return a set containing those elements in the shared domain space
2291 * of aff1 and aff2 where aff1 is greater than aff2.
2293 * If either of the two inputs is NaN, then the result is empty,
2294 * as comparisons with NaN always return false.
2296 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2297 __isl_take isl_aff *aff2)
2299 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2302 /* Return a basic set containing those elements in the shared space
2303 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2305 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2306 __isl_take isl_aff *aff2)
2308 return isl_aff_ge_basic_set(aff2, aff1);
2311 /* Return a basic set containing those elements in the shared domain space
2312 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2314 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2315 __isl_take isl_aff *aff2)
2317 return isl_aff_gt_basic_set(aff2, aff1);
2320 /* Return a set containing those elements in the shared space
2321 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2323 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2324 __isl_take isl_aff *aff2)
2326 return isl_aff_ge_set(aff2, aff1);
2329 /* Return a set containing those elements in the shared domain space
2330 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2332 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2333 __isl_take isl_aff *aff2)
2335 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2338 /* Return a basic set containing those elements in the shared space
2339 * of aff1 and aff2 where aff1 and aff2 are equal.
2341 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2342 __isl_take isl_aff *aff2)
2344 aff1 = isl_aff_sub(aff1, aff2);
2346 return isl_aff_zero_basic_set(aff1);
2349 /* Return a set containing those elements in the shared space
2350 * of aff1 and aff2 where aff1 and aff2 are equal.
2352 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2353 __isl_take isl_aff *aff2)
2355 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2358 /* Return a set containing those elements in the shared domain space
2359 * of aff1 and aff2 where aff1 and aff2 are not equal.
2361 * If either of the two inputs is NaN, then the result is empty,
2362 * as comparisons with NaN always return false.
2364 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2365 __isl_take isl_aff *aff2)
2367 isl_set *set_lt, *set_gt;
2369 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2370 isl_aff_copy(aff2));
2371 set_gt = isl_aff_gt_set(aff1, aff2);
2372 return isl_set_union_disjoint(set_lt, set_gt);
2375 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2376 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2378 aff1 = isl_aff_add(aff1, aff2);
2379 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2380 return aff1;
2383 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2385 if (!aff)
2386 return -1;
2388 return 0;
2391 /* Check whether the given affine expression has non-zero coefficient
2392 * for any dimension in the given range or if any of these dimensions
2393 * appear with non-zero coefficients in any of the integer divisions
2394 * involved in the affine expression.
2396 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2397 enum isl_dim_type type, unsigned first, unsigned n)
2399 int i;
2400 isl_ctx *ctx;
2401 int *active = NULL;
2402 isl_bool involves = isl_bool_false;
2404 if (!aff)
2405 return isl_bool_error;
2406 if (n == 0)
2407 return isl_bool_false;
2409 ctx = isl_aff_get_ctx(aff);
2410 if (first + n > isl_aff_dim(aff, type))
2411 isl_die(ctx, isl_error_invalid,
2412 "range out of bounds", return isl_bool_error);
2414 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2415 if (!active)
2416 goto error;
2418 first += isl_local_space_offset(aff->ls, type) - 1;
2419 for (i = 0; i < n; ++i)
2420 if (active[first + i]) {
2421 involves = isl_bool_true;
2422 break;
2425 free(active);
2427 return involves;
2428 error:
2429 free(active);
2430 return isl_bool_error;
2433 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2434 enum isl_dim_type type, unsigned first, unsigned n)
2436 isl_ctx *ctx;
2438 if (!aff)
2439 return NULL;
2440 if (type == isl_dim_out)
2441 isl_die(aff->v->ctx, isl_error_invalid,
2442 "cannot drop output/set dimension",
2443 return isl_aff_free(aff));
2444 if (type == isl_dim_in)
2445 type = isl_dim_set;
2446 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2447 return aff;
2449 ctx = isl_aff_get_ctx(aff);
2450 if (first + n > isl_local_space_dim(aff->ls, type))
2451 isl_die(ctx, isl_error_invalid, "range out of bounds",
2452 return isl_aff_free(aff));
2454 aff = isl_aff_cow(aff);
2455 if (!aff)
2456 return NULL;
2458 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2459 if (!aff->ls)
2460 return isl_aff_free(aff);
2462 first += 1 + isl_local_space_offset(aff->ls, type);
2463 aff->v = isl_vec_drop_els(aff->v, first, n);
2464 if (!aff->v)
2465 return isl_aff_free(aff);
2467 return aff;
2470 /* Drop the "n" domain dimensions starting at "first" from "aff",
2471 * after checking that they do not appear in the affine expression.
2473 static __isl_give isl_aff *drop_domain(__isl_take isl_aff *aff, unsigned first,
2474 unsigned n)
2476 isl_bool involves;
2478 involves = isl_aff_involves_dims(aff, isl_dim_in, first, n);
2479 if (involves < 0)
2480 return isl_aff_free(aff);
2481 if (involves)
2482 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2483 "affine expression involves some of the domain dimensions",
2484 return isl_aff_free(aff));
2485 return isl_aff_drop_dims(aff, isl_dim_in, first, n);
2488 /* Project the domain of the affine expression onto its parameter space.
2489 * The affine expression may not involve any of the domain dimensions.
2491 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2493 isl_space *space;
2494 unsigned n;
2496 n = isl_aff_dim(aff, isl_dim_in);
2497 aff = drop_domain(aff, 0, n);
2498 space = isl_aff_get_domain_space(aff);
2499 space = isl_space_params(space);
2500 aff = isl_aff_reset_domain_space(aff, space);
2501 return aff;
2504 /* Check that the domain of "aff" is a product.
2506 static isl_stat check_domain_product(__isl_keep isl_aff *aff)
2508 isl_bool is_product;
2510 is_product = isl_space_is_product(isl_aff_peek_domain_space(aff));
2511 if (is_product < 0)
2512 return isl_stat_error;
2513 if (!is_product)
2514 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2515 "domain is not a product", return isl_stat_error);
2516 return isl_stat_ok;
2519 /* Given an affine function with a domain of the form [A -> B] that
2520 * does not depend on B, return the same function on domain A.
2522 __isl_give isl_aff *isl_aff_domain_factor_domain(__isl_take isl_aff *aff)
2524 isl_space *space;
2525 int n, n_in;
2527 if (check_domain_product(aff) < 0)
2528 return isl_aff_free(aff);
2529 space = isl_aff_get_domain_space(aff);
2530 n = isl_space_dim(space, isl_dim_set);
2531 space = isl_space_factor_domain(space);
2532 n_in = isl_space_dim(space, isl_dim_set);
2533 aff = drop_domain(aff, n_in, n - n_in);
2534 aff = isl_aff_reset_domain_space(aff, space);
2535 return aff;
2538 /* Convert an affine expression defined over a parameter domain
2539 * into one that is defined over a zero-dimensional set.
2541 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2543 isl_local_space *ls;
2545 ls = isl_aff_take_domain_local_space(aff);
2546 ls = isl_local_space_set_from_params(ls);
2547 aff = isl_aff_restore_domain_local_space(aff, ls);
2549 return aff;
2552 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2553 enum isl_dim_type type, unsigned first, unsigned n)
2555 isl_ctx *ctx;
2557 if (!aff)
2558 return NULL;
2559 if (type == isl_dim_out)
2560 isl_die(aff->v->ctx, isl_error_invalid,
2561 "cannot insert output/set dimensions",
2562 return isl_aff_free(aff));
2563 if (type == isl_dim_in)
2564 type = isl_dim_set;
2565 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2566 return aff;
2568 ctx = isl_aff_get_ctx(aff);
2569 if (first > isl_local_space_dim(aff->ls, type))
2570 isl_die(ctx, isl_error_invalid, "position out of bounds",
2571 return isl_aff_free(aff));
2573 aff = isl_aff_cow(aff);
2574 if (!aff)
2575 return NULL;
2577 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2578 if (!aff->ls)
2579 return isl_aff_free(aff);
2581 first += 1 + isl_local_space_offset(aff->ls, type);
2582 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2583 if (!aff->v)
2584 return isl_aff_free(aff);
2586 return aff;
2589 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2590 enum isl_dim_type type, unsigned n)
2592 unsigned pos;
2594 pos = isl_aff_dim(aff, type);
2596 return isl_aff_insert_dims(aff, type, pos, n);
2599 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2600 enum isl_dim_type type, unsigned n)
2602 unsigned pos;
2604 pos = isl_pw_aff_dim(pwaff, type);
2606 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2609 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2610 * to dimensions of "dst_type" at "dst_pos".
2612 * We only support moving input dimensions to parameters and vice versa.
2614 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2615 enum isl_dim_type dst_type, unsigned dst_pos,
2616 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2618 unsigned g_dst_pos;
2619 unsigned g_src_pos;
2621 if (!aff)
2622 return NULL;
2623 if (n == 0 &&
2624 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2625 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2626 return aff;
2628 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2629 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2630 "cannot move output/set dimension",
2631 return isl_aff_free(aff));
2632 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2633 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2634 "cannot move divs", return isl_aff_free(aff));
2635 if (dst_type == isl_dim_in)
2636 dst_type = isl_dim_set;
2637 if (src_type == isl_dim_in)
2638 src_type = isl_dim_set;
2640 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2641 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2642 "range out of bounds", return isl_aff_free(aff));
2643 if (dst_type == src_type)
2644 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2645 "moving dims within the same type not supported",
2646 return isl_aff_free(aff));
2648 aff = isl_aff_cow(aff);
2649 if (!aff)
2650 return NULL;
2652 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2653 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2654 if (dst_type > src_type)
2655 g_dst_pos -= n;
2657 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2658 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2659 src_type, src_pos, n);
2660 if (!aff->v || !aff->ls)
2661 return isl_aff_free(aff);
2663 aff = sort_divs(aff);
2665 return aff;
2668 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2670 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2671 return isl_pw_aff_alloc(dom, aff);
2674 #define isl_aff_involves_nan isl_aff_is_nan
2676 #undef PW
2677 #define PW isl_pw_aff
2678 #undef EL
2679 #define EL isl_aff
2680 #undef EL_IS_ZERO
2681 #define EL_IS_ZERO is_empty
2682 #undef ZERO
2683 #define ZERO empty
2684 #undef IS_ZERO
2685 #define IS_ZERO is_empty
2686 #undef FIELD
2687 #define FIELD aff
2688 #undef DEFAULT_IS_ZERO
2689 #define DEFAULT_IS_ZERO 0
2691 #define NO_OPT
2692 #define NO_LIFT
2693 #define NO_MORPH
2695 #include <isl_pw_templ.c>
2696 #include <isl_pw_eval.c>
2697 #include <isl_pw_hash.c>
2698 #include <isl_pw_union_opt.c>
2700 #undef BASE
2701 #define BASE pw_aff
2703 #include <isl_union_single.c>
2704 #include <isl_union_neg.c>
2706 static __isl_give isl_set *align_params_pw_pw_set_and(
2707 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2708 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2709 __isl_take isl_pw_aff *pwaff2))
2711 isl_bool equal_params;
2713 if (!pwaff1 || !pwaff2)
2714 goto error;
2715 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2716 if (equal_params < 0)
2717 goto error;
2718 if (equal_params)
2719 return fn(pwaff1, pwaff2);
2720 if (isl_pw_aff_check_named_params(pwaff1) < 0 ||
2721 isl_pw_aff_check_named_params(pwaff2) < 0)
2722 goto error;
2723 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2724 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2725 return fn(pwaff1, pwaff2);
2726 error:
2727 isl_pw_aff_free(pwaff1);
2728 isl_pw_aff_free(pwaff2);
2729 return NULL;
2732 /* Align the parameters of the to isl_pw_aff arguments and
2733 * then apply a function "fn" on them that returns an isl_map.
2735 static __isl_give isl_map *align_params_pw_pw_map_and(
2736 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2737 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2738 __isl_take isl_pw_aff *pa2))
2740 isl_bool equal_params;
2742 if (!pa1 || !pa2)
2743 goto error;
2744 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2745 if (equal_params < 0)
2746 goto error;
2747 if (equal_params)
2748 return fn(pa1, pa2);
2749 if (isl_pw_aff_check_named_params(pa1) < 0 ||
2750 isl_pw_aff_check_named_params(pa2) < 0)
2751 goto error;
2752 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2753 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2754 return fn(pa1, pa2);
2755 error:
2756 isl_pw_aff_free(pa1);
2757 isl_pw_aff_free(pa2);
2758 return NULL;
2761 /* Compute a piecewise quasi-affine expression with a domain that
2762 * is the union of those of pwaff1 and pwaff2 and such that on each
2763 * cell, the quasi-affine expression is the maximum of those of pwaff1
2764 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2765 * cell, then the associated expression is the defined one.
2767 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2768 __isl_take isl_pw_aff *pwaff2)
2770 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2773 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2774 __isl_take isl_pw_aff *pwaff2)
2776 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2777 &pw_aff_union_max);
2780 /* Compute a piecewise quasi-affine expression with a domain that
2781 * is the union of those of pwaff1 and pwaff2 and such that on each
2782 * cell, the quasi-affine expression is the minimum of those of pwaff1
2783 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2784 * cell, then the associated expression is the defined one.
2786 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2787 __isl_take isl_pw_aff *pwaff2)
2789 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2792 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2793 __isl_take isl_pw_aff *pwaff2)
2795 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2796 &pw_aff_union_min);
2799 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2800 __isl_take isl_pw_aff *pwaff2, int max)
2802 if (max)
2803 return isl_pw_aff_union_max(pwaff1, pwaff2);
2804 else
2805 return isl_pw_aff_union_min(pwaff1, pwaff2);
2808 /* Construct a map with as domain the domain of pwaff and
2809 * one-dimensional range corresponding to the affine expressions.
2811 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2813 int i;
2814 isl_space *space;
2815 isl_map *map;
2817 if (!pwaff)
2818 return NULL;
2820 space = isl_pw_aff_get_space(pwaff);
2821 map = isl_map_empty(space);
2823 for (i = 0; i < pwaff->n; ++i) {
2824 isl_basic_map *bmap;
2825 isl_map *map_i;
2827 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2828 map_i = isl_map_from_basic_map(bmap);
2829 map_i = isl_map_intersect_domain(map_i,
2830 isl_set_copy(pwaff->p[i].set));
2831 map = isl_map_union_disjoint(map, map_i);
2834 isl_pw_aff_free(pwaff);
2836 return map;
2839 /* Construct a map with as domain the domain of pwaff and
2840 * one-dimensional range corresponding to the affine expressions.
2842 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2844 if (!pwaff)
2845 return NULL;
2846 if (isl_space_is_set(pwaff->dim))
2847 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2848 "space of input is not a map", goto error);
2849 return map_from_pw_aff(pwaff);
2850 error:
2851 isl_pw_aff_free(pwaff);
2852 return NULL;
2855 /* Construct a one-dimensional set with as parameter domain
2856 * the domain of pwaff and the single set dimension
2857 * corresponding to the affine expressions.
2859 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2861 if (!pwaff)
2862 return NULL;
2863 if (!isl_space_is_set(pwaff->dim))
2864 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2865 "space of input is not a set", goto error);
2866 return set_from_map(map_from_pw_aff(pwaff));
2867 error:
2868 isl_pw_aff_free(pwaff);
2869 return NULL;
2872 /* Return a set containing those elements in the domain
2873 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2874 * does not satisfy "fn" (if complement is 1).
2876 * The pieces with a NaN never belong to the result since
2877 * NaN does not satisfy any property.
2879 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2880 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2881 int complement)
2883 int i;
2884 isl_set *set;
2886 if (!pwaff)
2887 return NULL;
2889 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2891 for (i = 0; i < pwaff->n; ++i) {
2892 isl_basic_set *bset;
2893 isl_set *set_i, *locus;
2894 isl_bool rational;
2896 if (isl_aff_is_nan(pwaff->p[i].aff))
2897 continue;
2899 rational = isl_set_has_rational(pwaff->p[i].set);
2900 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2901 locus = isl_set_from_basic_set(bset);
2902 set_i = isl_set_copy(pwaff->p[i].set);
2903 if (complement)
2904 set_i = isl_set_subtract(set_i, locus);
2905 else
2906 set_i = isl_set_intersect(set_i, locus);
2907 set = isl_set_union_disjoint(set, set_i);
2910 isl_pw_aff_free(pwaff);
2912 return set;
2915 /* Return a set containing those elements in the domain
2916 * of "pa" where it is positive.
2918 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2920 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2923 /* Return a set containing those elements in the domain
2924 * of pwaff where it is non-negative.
2926 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2928 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2931 /* Return a set containing those elements in the domain
2932 * of pwaff where it is zero.
2934 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2936 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2939 /* Return a set containing those elements in the domain
2940 * of pwaff where it is not zero.
2942 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2944 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2947 /* Return a set containing those elements in the shared domain
2948 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2950 * We compute the difference on the shared domain and then construct
2951 * the set of values where this difference is non-negative.
2952 * If strict is set, we first subtract 1 from the difference.
2953 * If equal is set, we only return the elements where pwaff1 and pwaff2
2954 * are equal.
2956 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2957 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2959 isl_set *set1, *set2;
2961 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2962 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2963 set1 = isl_set_intersect(set1, set2);
2964 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2965 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2966 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2968 if (strict) {
2969 isl_space *dim = isl_set_get_space(set1);
2970 isl_aff *aff;
2971 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2972 aff = isl_aff_add_constant_si(aff, -1);
2973 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2974 } else
2975 isl_set_free(set1);
2977 if (equal)
2978 return isl_pw_aff_zero_set(pwaff1);
2979 return isl_pw_aff_nonneg_set(pwaff1);
2982 /* Return a set containing those elements in the shared domain
2983 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2985 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2986 __isl_take isl_pw_aff *pwaff2)
2988 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2991 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2992 __isl_take isl_pw_aff *pwaff2)
2994 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2997 /* Return a set containing those elements in the shared domain
2998 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3000 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3001 __isl_take isl_pw_aff *pwaff2)
3003 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3006 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3007 __isl_take isl_pw_aff *pwaff2)
3009 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
3012 /* Return a set containing those elements in the shared domain
3013 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3015 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3016 __isl_take isl_pw_aff *pwaff2)
3018 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3021 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3022 __isl_take isl_pw_aff *pwaff2)
3024 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
3027 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3028 __isl_take isl_pw_aff *pwaff2)
3030 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3033 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3034 __isl_take isl_pw_aff *pwaff2)
3036 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3039 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3040 * where the function values are ordered in the same way as "order",
3041 * which returns a set in the shared domain of its two arguments.
3042 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3044 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3045 * We first pull back the two functions such that they are defined on
3046 * the domain [A -> B]. Then we apply "order", resulting in a set
3047 * in the space [A -> B]. Finally, we unwrap this set to obtain
3048 * a map in the space A -> B.
3050 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3051 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3052 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3053 __isl_take isl_pw_aff *pa2))
3055 isl_space *space1, *space2;
3056 isl_multi_aff *ma;
3057 isl_set *set;
3059 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3060 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3061 space1 = isl_space_map_from_domain_and_range(space1, space2);
3062 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3063 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3064 ma = isl_multi_aff_range_map(space1);
3065 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3066 set = order(pa1, pa2);
3068 return isl_set_unwrap(set);
3071 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3072 * where the function values are equal.
3073 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3075 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3076 __isl_take isl_pw_aff *pa2)
3078 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3081 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3082 * where the function values are equal.
3084 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3085 __isl_take isl_pw_aff *pa2)
3087 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3090 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3091 * where the function value of "pa1" is less than the function value of "pa2".
3092 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3094 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3095 __isl_take isl_pw_aff *pa2)
3097 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3100 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3101 * where the function value of "pa1" is less than the function value of "pa2".
3103 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3104 __isl_take isl_pw_aff *pa2)
3106 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3109 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3110 * where the function value of "pa1" is greater than the function value
3111 * of "pa2".
3112 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3114 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3115 __isl_take isl_pw_aff *pa2)
3117 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3120 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3121 * where the function value of "pa1" is greater than the function value
3122 * of "pa2".
3124 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3125 __isl_take isl_pw_aff *pa2)
3127 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3130 /* Return a set containing those elements in the shared domain
3131 * of the elements of list1 and list2 where each element in list1
3132 * has the relation specified by "fn" with each element in list2.
3134 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3135 __isl_take isl_pw_aff_list *list2,
3136 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3137 __isl_take isl_pw_aff *pwaff2))
3139 int i, j;
3140 isl_ctx *ctx;
3141 isl_set *set;
3143 if (!list1 || !list2)
3144 goto error;
3146 ctx = isl_pw_aff_list_get_ctx(list1);
3147 if (list1->n < 1 || list2->n < 1)
3148 isl_die(ctx, isl_error_invalid,
3149 "list should contain at least one element", goto error);
3151 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3152 for (i = 0; i < list1->n; ++i)
3153 for (j = 0; j < list2->n; ++j) {
3154 isl_set *set_ij;
3156 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3157 isl_pw_aff_copy(list2->p[j]));
3158 set = isl_set_intersect(set, set_ij);
3161 isl_pw_aff_list_free(list1);
3162 isl_pw_aff_list_free(list2);
3163 return set;
3164 error:
3165 isl_pw_aff_list_free(list1);
3166 isl_pw_aff_list_free(list2);
3167 return NULL;
3170 /* Return a set containing those elements in the shared domain
3171 * of the elements of list1 and list2 where each element in list1
3172 * is equal to each element in list2.
3174 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3175 __isl_take isl_pw_aff_list *list2)
3177 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3180 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3181 __isl_take isl_pw_aff_list *list2)
3183 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3186 /* Return a set containing those elements in the shared domain
3187 * of the elements of list1 and list2 where each element in list1
3188 * is less than or equal to each element in list2.
3190 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3191 __isl_take isl_pw_aff_list *list2)
3193 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3196 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3197 __isl_take isl_pw_aff_list *list2)
3199 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3202 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3203 __isl_take isl_pw_aff_list *list2)
3205 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3208 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3209 __isl_take isl_pw_aff_list *list2)
3211 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3215 /* Return a set containing those elements in the shared domain
3216 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3218 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3219 __isl_take isl_pw_aff *pwaff2)
3221 isl_set *set_lt, *set_gt;
3223 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3224 isl_pw_aff_copy(pwaff2));
3225 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3226 return isl_set_union_disjoint(set_lt, set_gt);
3229 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3230 __isl_take isl_pw_aff *pwaff2)
3232 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3235 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3236 isl_int v)
3238 int i;
3240 if (isl_int_is_one(v))
3241 return pwaff;
3242 if (!isl_int_is_pos(v))
3243 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3244 "factor needs to be positive",
3245 return isl_pw_aff_free(pwaff));
3246 pwaff = isl_pw_aff_cow(pwaff);
3247 if (!pwaff)
3248 return NULL;
3249 if (pwaff->n == 0)
3250 return pwaff;
3252 for (i = 0; i < pwaff->n; ++i) {
3253 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3254 if (!pwaff->p[i].aff)
3255 return isl_pw_aff_free(pwaff);
3258 return pwaff;
3261 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3263 int i;
3265 pwaff = isl_pw_aff_cow(pwaff);
3266 if (!pwaff)
3267 return NULL;
3268 if (pwaff->n == 0)
3269 return pwaff;
3271 for (i = 0; i < pwaff->n; ++i) {
3272 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3273 if (!pwaff->p[i].aff)
3274 return isl_pw_aff_free(pwaff);
3277 return pwaff;
3280 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3282 int i;
3284 pwaff = isl_pw_aff_cow(pwaff);
3285 if (!pwaff)
3286 return NULL;
3287 if (pwaff->n == 0)
3288 return pwaff;
3290 for (i = 0; i < pwaff->n; ++i) {
3291 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3292 if (!pwaff->p[i].aff)
3293 return isl_pw_aff_free(pwaff);
3296 return pwaff;
3299 /* Assuming that "cond1" and "cond2" are disjoint,
3300 * return an affine expression that is equal to pwaff1 on cond1
3301 * and to pwaff2 on cond2.
3303 static __isl_give isl_pw_aff *isl_pw_aff_select(
3304 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3305 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3307 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3308 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3310 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3313 /* Return an affine expression that is equal to pwaff_true for elements
3314 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3315 * is zero.
3316 * That is, return cond ? pwaff_true : pwaff_false;
3318 * If "cond" involves and NaN, then we conservatively return a NaN
3319 * on its entire domain. In principle, we could consider the pieces
3320 * where it is NaN separately from those where it is not.
3322 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3323 * then only use the domain of "cond" to restrict the domain.
3325 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3326 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3328 isl_set *cond_true, *cond_false;
3329 isl_bool equal;
3331 if (!cond)
3332 goto error;
3333 if (isl_pw_aff_involves_nan(cond)) {
3334 isl_space *space = isl_pw_aff_get_domain_space(cond);
3335 isl_local_space *ls = isl_local_space_from_space(space);
3336 isl_pw_aff_free(cond);
3337 isl_pw_aff_free(pwaff_true);
3338 isl_pw_aff_free(pwaff_false);
3339 return isl_pw_aff_nan_on_domain(ls);
3342 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3343 isl_pw_aff_get_space(pwaff_false));
3344 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3345 isl_pw_aff_get_space(pwaff_true));
3346 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3347 if (equal < 0)
3348 goto error;
3349 if (equal) {
3350 isl_set *dom;
3352 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3353 isl_pw_aff_free(pwaff_false);
3354 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3357 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3358 cond_false = isl_pw_aff_zero_set(cond);
3359 return isl_pw_aff_select(cond_true, pwaff_true,
3360 cond_false, pwaff_false);
3361 error:
3362 isl_pw_aff_free(cond);
3363 isl_pw_aff_free(pwaff_true);
3364 isl_pw_aff_free(pwaff_false);
3365 return NULL;
3368 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3370 if (!aff)
3371 return isl_bool_error;
3373 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3376 /* Check whether pwaff is a piecewise constant.
3378 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3380 int i;
3382 if (!pwaff)
3383 return isl_bool_error;
3385 for (i = 0; i < pwaff->n; ++i) {
3386 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3387 if (is_cst < 0 || !is_cst)
3388 return is_cst;
3391 return isl_bool_true;
3394 /* Are all elements of "mpa" piecewise constants?
3396 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3398 int i;
3400 if (!mpa)
3401 return isl_bool_error;
3403 for (i = 0; i < mpa->n; ++i) {
3404 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3405 if (is_cst < 0 || !is_cst)
3406 return is_cst;
3409 return isl_bool_true;
3412 /* Return the product of "aff1" and "aff2".
3414 * If either of the two is NaN, then the result is NaN.
3416 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3418 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3419 __isl_take isl_aff *aff2)
3421 if (!aff1 || !aff2)
3422 goto error;
3424 if (isl_aff_is_nan(aff1)) {
3425 isl_aff_free(aff2);
3426 return aff1;
3428 if (isl_aff_is_nan(aff2)) {
3429 isl_aff_free(aff1);
3430 return aff2;
3433 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3434 return isl_aff_mul(aff2, aff1);
3436 if (!isl_aff_is_cst(aff2))
3437 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3438 "at least one affine expression should be constant",
3439 goto error);
3441 aff1 = isl_aff_cow(aff1);
3442 if (!aff1 || !aff2)
3443 goto error;
3445 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3446 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3448 isl_aff_free(aff2);
3449 return aff1;
3450 error:
3451 isl_aff_free(aff1);
3452 isl_aff_free(aff2);
3453 return NULL;
3456 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3458 * If either of the two is NaN, then the result is NaN.
3460 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3461 __isl_take isl_aff *aff2)
3463 int is_cst;
3464 int neg;
3466 if (!aff1 || !aff2)
3467 goto error;
3469 if (isl_aff_is_nan(aff1)) {
3470 isl_aff_free(aff2);
3471 return aff1;
3473 if (isl_aff_is_nan(aff2)) {
3474 isl_aff_free(aff1);
3475 return aff2;
3478 is_cst = isl_aff_is_cst(aff2);
3479 if (is_cst < 0)
3480 goto error;
3481 if (!is_cst)
3482 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3483 "second argument should be a constant", goto error);
3485 if (!aff2)
3486 goto error;
3488 neg = isl_int_is_neg(aff2->v->el[1]);
3489 if (neg) {
3490 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3491 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3494 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3495 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3497 if (neg) {
3498 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3499 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3502 isl_aff_free(aff2);
3503 return aff1;
3504 error:
3505 isl_aff_free(aff1);
3506 isl_aff_free(aff2);
3507 return NULL;
3510 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3511 __isl_take isl_pw_aff *pwaff2)
3513 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3516 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3517 __isl_take isl_pw_aff *pwaff2)
3519 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3522 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3523 __isl_take isl_pw_aff *pwaff2)
3525 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3528 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3529 __isl_take isl_pw_aff *pwaff2)
3531 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3534 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3535 __isl_take isl_pw_aff *pwaff2)
3537 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3540 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3541 __isl_take isl_pw_aff *pa2)
3543 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3546 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3548 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3549 __isl_take isl_pw_aff *pa2)
3551 int is_cst;
3553 is_cst = isl_pw_aff_is_cst(pa2);
3554 if (is_cst < 0)
3555 goto error;
3556 if (!is_cst)
3557 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3558 "second argument should be a piecewise constant",
3559 goto error);
3560 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3561 error:
3562 isl_pw_aff_free(pa1);
3563 isl_pw_aff_free(pa2);
3564 return NULL;
3567 /* Compute the quotient of the integer division of "pa1" by "pa2"
3568 * with rounding towards zero.
3569 * "pa2" is assumed to be a piecewise constant.
3571 * In particular, return
3573 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3576 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3577 __isl_take isl_pw_aff *pa2)
3579 int is_cst;
3580 isl_set *cond;
3581 isl_pw_aff *f, *c;
3583 is_cst = isl_pw_aff_is_cst(pa2);
3584 if (is_cst < 0)
3585 goto error;
3586 if (!is_cst)
3587 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3588 "second argument should be a piecewise constant",
3589 goto error);
3591 pa1 = isl_pw_aff_div(pa1, pa2);
3593 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3594 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3595 c = isl_pw_aff_ceil(pa1);
3596 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3597 error:
3598 isl_pw_aff_free(pa1);
3599 isl_pw_aff_free(pa2);
3600 return NULL;
3603 /* Compute the remainder of the integer division of "pa1" by "pa2"
3604 * with rounding towards zero.
3605 * "pa2" is assumed to be a piecewise constant.
3607 * In particular, return
3609 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3612 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3613 __isl_take isl_pw_aff *pa2)
3615 int is_cst;
3616 isl_pw_aff *res;
3618 is_cst = isl_pw_aff_is_cst(pa2);
3619 if (is_cst < 0)
3620 goto error;
3621 if (!is_cst)
3622 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3623 "second argument should be a piecewise constant",
3624 goto error);
3625 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3626 res = isl_pw_aff_mul(pa2, res);
3627 res = isl_pw_aff_sub(pa1, res);
3628 return res;
3629 error:
3630 isl_pw_aff_free(pa1);
3631 isl_pw_aff_free(pa2);
3632 return NULL;
3635 /* Does either of "pa1" or "pa2" involve any NaN2?
3637 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3638 __isl_keep isl_pw_aff *pa2)
3640 isl_bool has_nan;
3642 has_nan = isl_pw_aff_involves_nan(pa1);
3643 if (has_nan < 0 || has_nan)
3644 return has_nan;
3645 return isl_pw_aff_involves_nan(pa2);
3648 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3649 * by a NaN on their shared domain.
3651 * In principle, the result could be refined to only being NaN
3652 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3654 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3655 __isl_take isl_pw_aff *pa2)
3657 isl_local_space *ls;
3658 isl_set *dom;
3659 isl_pw_aff *pa;
3661 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3662 ls = isl_local_space_from_space(isl_set_get_space(dom));
3663 pa = isl_pw_aff_nan_on_domain(ls);
3664 pa = isl_pw_aff_intersect_domain(pa, dom);
3666 return pa;
3669 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3670 __isl_take isl_pw_aff *pwaff2)
3672 isl_set *le;
3673 isl_set *dom;
3675 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3676 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3677 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3678 isl_pw_aff_copy(pwaff2));
3679 dom = isl_set_subtract(dom, isl_set_copy(le));
3680 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3683 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3684 __isl_take isl_pw_aff *pwaff2)
3686 isl_set *ge;
3687 isl_set *dom;
3689 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3690 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3691 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3692 isl_pw_aff_copy(pwaff2));
3693 dom = isl_set_subtract(dom, isl_set_copy(ge));
3694 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3697 /* Return an expression for the minimum (if "max" is not set) or
3698 * the maximum (if "max" is set) of "pa1" and "pa2".
3699 * If either expression involves any NaN, then return a NaN
3700 * on the shared domain as result.
3702 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3703 __isl_take isl_pw_aff *pa2, int max)
3705 isl_bool has_nan;
3707 has_nan = either_involves_nan(pa1, pa2);
3708 if (has_nan < 0)
3709 pa1 = isl_pw_aff_free(pa1);
3710 else if (has_nan)
3711 return replace_by_nan(pa1, pa2);
3713 if (max)
3714 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3715 else
3716 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3719 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3721 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3722 __isl_take isl_pw_aff *pwaff2)
3724 return pw_aff_min_max(pwaff1, pwaff2, 0);
3727 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3729 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3730 __isl_take isl_pw_aff *pwaff2)
3732 return pw_aff_min_max(pwaff1, pwaff2, 1);
3735 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3736 __isl_take isl_pw_aff_list *list,
3737 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3738 __isl_take isl_pw_aff *pwaff2))
3740 int i;
3741 isl_ctx *ctx;
3742 isl_pw_aff *res;
3744 if (!list)
3745 return NULL;
3747 ctx = isl_pw_aff_list_get_ctx(list);
3748 if (list->n < 1)
3749 isl_die(ctx, isl_error_invalid,
3750 "list should contain at least one element", goto error);
3752 res = isl_pw_aff_copy(list->p[0]);
3753 for (i = 1; i < list->n; ++i)
3754 res = fn(res, isl_pw_aff_copy(list->p[i]));
3756 isl_pw_aff_list_free(list);
3757 return res;
3758 error:
3759 isl_pw_aff_list_free(list);
3760 return NULL;
3763 /* Return an isl_pw_aff that maps each element in the intersection of the
3764 * domains of the elements of list to the minimal corresponding affine
3765 * expression.
3767 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3769 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3772 /* Return an isl_pw_aff that maps each element in the intersection of the
3773 * domains of the elements of list to the maximal corresponding affine
3774 * expression.
3776 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3778 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3781 /* Mark the domains of "pwaff" as rational.
3783 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3785 int i;
3787 pwaff = isl_pw_aff_cow(pwaff);
3788 if (!pwaff)
3789 return NULL;
3790 if (pwaff->n == 0)
3791 return pwaff;
3793 for (i = 0; i < pwaff->n; ++i) {
3794 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3795 if (!pwaff->p[i].set)
3796 return isl_pw_aff_free(pwaff);
3799 return pwaff;
3802 /* Mark the domains of the elements of "list" as rational.
3804 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3805 __isl_take isl_pw_aff_list *list)
3807 int i, n;
3809 if (!list)
3810 return NULL;
3811 if (list->n == 0)
3812 return list;
3814 n = list->n;
3815 for (i = 0; i < n; ++i) {
3816 isl_pw_aff *pa;
3818 pa = isl_pw_aff_list_get_pw_aff(list, i);
3819 pa = isl_pw_aff_set_rational(pa);
3820 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3823 return list;
3826 /* Do the parameters of "aff" match those of "space"?
3828 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3829 __isl_keep isl_space *space)
3831 isl_space *aff_space;
3832 isl_bool match;
3834 if (!aff || !space)
3835 return isl_bool_error;
3837 aff_space = isl_aff_get_domain_space(aff);
3839 match = isl_space_has_equal_params(space, aff_space);
3841 isl_space_free(aff_space);
3842 return match;
3845 /* Check that the domain space of "aff" matches "space".
3847 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3848 __isl_keep isl_space *space)
3850 isl_space *aff_space;
3851 isl_bool match;
3853 if (!aff || !space)
3854 return isl_stat_error;
3856 aff_space = isl_aff_get_domain_space(aff);
3858 match = isl_space_has_equal_params(space, aff_space);
3859 if (match < 0)
3860 goto error;
3861 if (!match)
3862 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3863 "parameters don't match", goto error);
3864 match = isl_space_tuple_is_equal(space, isl_dim_in,
3865 aff_space, isl_dim_set);
3866 if (match < 0)
3867 goto error;
3868 if (!match)
3869 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3870 "domains don't match", goto error);
3871 isl_space_free(aff_space);
3872 return isl_stat_ok;
3873 error:
3874 isl_space_free(aff_space);
3875 return isl_stat_error;
3878 #undef BASE
3879 #define BASE aff
3880 #undef DOMBASE
3881 #define DOMBASE set
3882 #define NO_DOMAIN
3884 #include <isl_multi_no_explicit_domain.c>
3885 #include <isl_multi_templ.c>
3886 #include <isl_multi_apply_set.c>
3887 #include <isl_multi_cmp.c>
3888 #include <isl_multi_dims.c>
3889 #include <isl_multi_floor.c>
3890 #include <isl_multi_gist.c>
3892 #undef NO_DOMAIN
3894 /* Construct an isl_multi_aff living in "space" that corresponds
3895 * to the affine transformation matrix "mat".
3897 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3898 __isl_take isl_space *space, __isl_take isl_mat *mat)
3900 isl_ctx *ctx;
3901 isl_local_space *ls = NULL;
3902 isl_multi_aff *ma = NULL;
3903 int n_row, n_col, n_out, total;
3904 int i;
3906 if (!space || !mat)
3907 goto error;
3909 ctx = isl_mat_get_ctx(mat);
3911 n_row = isl_mat_rows(mat);
3912 n_col = isl_mat_cols(mat);
3913 if (n_row < 1)
3914 isl_die(ctx, isl_error_invalid,
3915 "insufficient number of rows", goto error);
3916 if (n_col < 1)
3917 isl_die(ctx, isl_error_invalid,
3918 "insufficient number of columns", goto error);
3919 n_out = isl_space_dim(space, isl_dim_out);
3920 total = isl_space_dim(space, isl_dim_all);
3921 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3922 isl_die(ctx, isl_error_invalid,
3923 "dimension mismatch", goto error);
3925 ma = isl_multi_aff_zero(isl_space_copy(space));
3926 ls = isl_local_space_from_space(isl_space_domain(space));
3928 for (i = 0; i < n_row - 1; ++i) {
3929 isl_vec *v;
3930 isl_aff *aff;
3932 v = isl_vec_alloc(ctx, 1 + n_col);
3933 if (!v)
3934 goto error;
3935 isl_int_set(v->el[0], mat->row[0][0]);
3936 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3937 v = isl_vec_normalize(v);
3938 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3939 ma = isl_multi_aff_set_aff(ma, i, aff);
3942 isl_local_space_free(ls);
3943 isl_mat_free(mat);
3944 return ma;
3945 error:
3946 isl_local_space_free(ls);
3947 isl_mat_free(mat);
3948 isl_multi_aff_free(ma);
3949 return NULL;
3952 /* Remove any internal structure of the domain of "ma".
3953 * If there is any such internal structure in the input,
3954 * then the name of the corresponding space is also removed.
3956 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3957 __isl_take isl_multi_aff *ma)
3959 isl_space *space;
3961 if (!ma)
3962 return NULL;
3964 if (!ma->space->nested[0])
3965 return ma;
3967 space = isl_multi_aff_get_space(ma);
3968 space = isl_space_flatten_domain(space);
3969 ma = isl_multi_aff_reset_space(ma, space);
3971 return ma;
3974 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3975 * of the space to its domain.
3977 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3979 int i, n_in;
3980 isl_local_space *ls;
3981 isl_multi_aff *ma;
3983 if (!space)
3984 return NULL;
3985 if (!isl_space_is_map(space))
3986 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3987 "not a map space", goto error);
3989 n_in = isl_space_dim(space, isl_dim_in);
3990 space = isl_space_domain_map(space);
3992 ma = isl_multi_aff_alloc(isl_space_copy(space));
3993 if (n_in == 0) {
3994 isl_space_free(space);
3995 return ma;
3998 space = isl_space_domain(space);
3999 ls = isl_local_space_from_space(space);
4000 for (i = 0; i < n_in; ++i) {
4001 isl_aff *aff;
4003 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4004 isl_dim_set, i);
4005 ma = isl_multi_aff_set_aff(ma, i, aff);
4007 isl_local_space_free(ls);
4008 return ma;
4009 error:
4010 isl_space_free(space);
4011 return NULL;
4014 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4015 * of the space to its range.
4017 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4019 int i, n_in, n_out;
4020 isl_local_space *ls;
4021 isl_multi_aff *ma;
4023 if (!space)
4024 return NULL;
4025 if (!isl_space_is_map(space))
4026 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4027 "not a map space", goto error);
4029 n_in = isl_space_dim(space, isl_dim_in);
4030 n_out = isl_space_dim(space, isl_dim_out);
4031 space = isl_space_range_map(space);
4033 ma = isl_multi_aff_alloc(isl_space_copy(space));
4034 if (n_out == 0) {
4035 isl_space_free(space);
4036 return ma;
4039 space = isl_space_domain(space);
4040 ls = isl_local_space_from_space(space);
4041 for (i = 0; i < n_out; ++i) {
4042 isl_aff *aff;
4044 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4045 isl_dim_set, n_in + i);
4046 ma = isl_multi_aff_set_aff(ma, i, aff);
4048 isl_local_space_free(ls);
4049 return ma;
4050 error:
4051 isl_space_free(space);
4052 return NULL;
4055 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4056 * of the space to its range.
4058 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4059 __isl_take isl_space *space)
4061 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4064 /* Given the space of a set and a range of set dimensions,
4065 * construct an isl_multi_aff that projects out those dimensions.
4067 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4068 __isl_take isl_space *space, enum isl_dim_type type,
4069 unsigned first, unsigned n)
4071 int i, dim;
4072 isl_local_space *ls;
4073 isl_multi_aff *ma;
4075 if (!space)
4076 return NULL;
4077 if (!isl_space_is_set(space))
4078 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4079 "expecting set space", goto error);
4080 if (type != isl_dim_set)
4081 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4082 "only set dimensions can be projected out", goto error);
4084 dim = isl_space_dim(space, isl_dim_set);
4085 if (first + n > dim)
4086 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4087 "range out of bounds", goto error);
4089 space = isl_space_from_domain(space);
4090 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4092 if (dim == n)
4093 return isl_multi_aff_alloc(space);
4095 ma = isl_multi_aff_alloc(isl_space_copy(space));
4096 space = isl_space_domain(space);
4097 ls = isl_local_space_from_space(space);
4099 for (i = 0; i < first; ++i) {
4100 isl_aff *aff;
4102 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4103 isl_dim_set, i);
4104 ma = isl_multi_aff_set_aff(ma, i, aff);
4107 for (i = 0; i < dim - (first + n); ++i) {
4108 isl_aff *aff;
4110 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4111 isl_dim_set, first + n + i);
4112 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4115 isl_local_space_free(ls);
4116 return ma;
4117 error:
4118 isl_space_free(space);
4119 return NULL;
4122 /* Given the space of a set and a range of set dimensions,
4123 * construct an isl_pw_multi_aff that projects out those dimensions.
4125 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4126 __isl_take isl_space *space, enum isl_dim_type type,
4127 unsigned first, unsigned n)
4129 isl_multi_aff *ma;
4131 ma = isl_multi_aff_project_out_map(space, type, first, n);
4132 return isl_pw_multi_aff_from_multi_aff(ma);
4135 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4136 * domain.
4138 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4139 __isl_take isl_multi_aff *ma)
4141 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4142 return isl_pw_multi_aff_alloc(dom, ma);
4145 /* Create a piecewise multi-affine expression in the given space that maps each
4146 * input dimension to the corresponding output dimension.
4148 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4149 __isl_take isl_space *space)
4151 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4154 /* Exploit the equalities in "eq" to simplify the affine expressions.
4156 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4157 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4159 int i;
4161 maff = isl_multi_aff_cow(maff);
4162 if (!maff || !eq)
4163 goto error;
4165 for (i = 0; i < maff->n; ++i) {
4166 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4167 isl_basic_set_copy(eq));
4168 if (!maff->u.p[i])
4169 goto error;
4172 isl_basic_set_free(eq);
4173 return maff;
4174 error:
4175 isl_basic_set_free(eq);
4176 isl_multi_aff_free(maff);
4177 return NULL;
4180 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4181 isl_int f)
4183 int i;
4185 maff = isl_multi_aff_cow(maff);
4186 if (!maff)
4187 return NULL;
4189 for (i = 0; i < maff->n; ++i) {
4190 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4191 if (!maff->u.p[i])
4192 return isl_multi_aff_free(maff);
4195 return maff;
4198 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4199 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4201 maff1 = isl_multi_aff_add(maff1, maff2);
4202 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4203 return maff1;
4206 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4208 if (!maff)
4209 return -1;
4211 return 0;
4214 /* Return the set of domain elements where "ma1" is lexicographically
4215 * smaller than or equal to "ma2".
4217 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4218 __isl_take isl_multi_aff *ma2)
4220 return isl_multi_aff_lex_ge_set(ma2, ma1);
4223 /* Return the set of domain elements where "ma1" is lexicographically
4224 * smaller than "ma2".
4226 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4227 __isl_take isl_multi_aff *ma2)
4229 return isl_multi_aff_lex_gt_set(ma2, ma1);
4232 /* Return the set of domain elements where "ma1" and "ma2"
4233 * satisfy "order".
4235 static __isl_give isl_set *isl_multi_aff_order_set(
4236 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4237 __isl_give isl_map *order(__isl_take isl_space *set_space))
4239 isl_space *space;
4240 isl_map *map1, *map2;
4241 isl_map *map, *ge;
4243 map1 = isl_map_from_multi_aff(ma1);
4244 map2 = isl_map_from_multi_aff(ma2);
4245 map = isl_map_range_product(map1, map2);
4246 space = isl_space_range(isl_map_get_space(map));
4247 space = isl_space_domain(isl_space_unwrap(space));
4248 ge = order(space);
4249 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4251 return isl_map_domain(map);
4254 /* Return the set of domain elements where "ma1" is lexicographically
4255 * greater than or equal to "ma2".
4257 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4258 __isl_take isl_multi_aff *ma2)
4260 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4263 /* Return the set of domain elements where "ma1" is lexicographically
4264 * greater than "ma2".
4266 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4267 __isl_take isl_multi_aff *ma2)
4269 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4272 #undef PW
4273 #define PW isl_pw_multi_aff
4274 #undef EL
4275 #define EL isl_multi_aff
4276 #undef EL_IS_ZERO
4277 #define EL_IS_ZERO is_empty
4278 #undef ZERO
4279 #define ZERO empty
4280 #undef IS_ZERO
4281 #define IS_ZERO is_empty
4282 #undef FIELD
4283 #define FIELD maff
4284 #undef DEFAULT_IS_ZERO
4285 #define DEFAULT_IS_ZERO 0
4287 #define NO_SUB
4288 #define NO_OPT
4289 #define NO_INSERT_DIMS
4290 #define NO_LIFT
4291 #define NO_MORPH
4293 #include <isl_pw_templ.c>
4294 #include <isl_pw_union_opt.c>
4296 #undef NO_SUB
4298 #undef BASE
4299 #define BASE pw_multi_aff
4301 #include <isl_union_multi.c>
4302 #include <isl_union_neg.c>
4304 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4305 __isl_take isl_pw_multi_aff *pma1,
4306 __isl_take isl_pw_multi_aff *pma2)
4308 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4309 &isl_multi_aff_lex_ge_set);
4312 /* Given two piecewise multi affine expressions, return a piecewise
4313 * multi-affine expression defined on the union of the definition domains
4314 * of the inputs that is equal to the lexicographic maximum of the two
4315 * inputs on each cell. If only one of the two inputs is defined on
4316 * a given cell, then it is considered to be the maximum.
4318 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4319 __isl_take isl_pw_multi_aff *pma1,
4320 __isl_take isl_pw_multi_aff *pma2)
4322 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4323 &pw_multi_aff_union_lexmax);
4326 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4327 __isl_take isl_pw_multi_aff *pma1,
4328 __isl_take isl_pw_multi_aff *pma2)
4330 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4331 &isl_multi_aff_lex_le_set);
4334 /* Given two piecewise multi affine expressions, return a piecewise
4335 * multi-affine expression defined on the union of the definition domains
4336 * of the inputs that is equal to the lexicographic minimum of the two
4337 * inputs on each cell. If only one of the two inputs is defined on
4338 * a given cell, then it is considered to be the minimum.
4340 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4341 __isl_take isl_pw_multi_aff *pma1,
4342 __isl_take isl_pw_multi_aff *pma2)
4344 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4345 &pw_multi_aff_union_lexmin);
4348 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4349 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4351 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4352 &isl_multi_aff_add);
4355 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4356 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4358 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4359 &pw_multi_aff_add);
4362 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4363 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4365 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4366 &isl_multi_aff_sub);
4369 /* Subtract "pma2" from "pma1" and return the result.
4371 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4372 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4374 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4375 &pw_multi_aff_sub);
4378 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4379 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4381 return isl_pw_multi_aff_union_add_(pma1, pma2);
4384 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4385 * with the actual sum on the shared domain and
4386 * the defined expression on the symmetric difference of the domains.
4388 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4389 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4391 return isl_union_pw_aff_union_add_(upa1, upa2);
4394 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4395 * with the actual sum on the shared domain and
4396 * the defined expression on the symmetric difference of the domains.
4398 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4399 __isl_take isl_union_pw_multi_aff *upma1,
4400 __isl_take isl_union_pw_multi_aff *upma2)
4402 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4405 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4406 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4408 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4409 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4411 int i, j, n;
4412 isl_space *space;
4413 isl_pw_multi_aff *res;
4415 if (!pma1 || !pma2)
4416 goto error;
4418 n = pma1->n * pma2->n;
4419 space = isl_space_product(isl_space_copy(pma1->dim),
4420 isl_space_copy(pma2->dim));
4421 res = isl_pw_multi_aff_alloc_size(space, n);
4423 for (i = 0; i < pma1->n; ++i) {
4424 for (j = 0; j < pma2->n; ++j) {
4425 isl_set *domain;
4426 isl_multi_aff *ma;
4428 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4429 isl_set_copy(pma2->p[j].set));
4430 ma = isl_multi_aff_product(
4431 isl_multi_aff_copy(pma1->p[i].maff),
4432 isl_multi_aff_copy(pma2->p[j].maff));
4433 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4437 isl_pw_multi_aff_free(pma1);
4438 isl_pw_multi_aff_free(pma2);
4439 return res;
4440 error:
4441 isl_pw_multi_aff_free(pma1);
4442 isl_pw_multi_aff_free(pma2);
4443 return NULL;
4446 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4447 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4449 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4450 &pw_multi_aff_product);
4453 /* Construct a map mapping the domain of the piecewise multi-affine expression
4454 * to its range, with each dimension in the range equated to the
4455 * corresponding affine expression on its cell.
4457 * If the domain of "pma" is rational, then so is the constructed "map".
4459 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4461 int i;
4462 isl_map *map;
4464 if (!pma)
4465 return NULL;
4467 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4469 for (i = 0; i < pma->n; ++i) {
4470 isl_bool rational;
4471 isl_multi_aff *maff;
4472 isl_basic_map *bmap;
4473 isl_map *map_i;
4475 rational = isl_set_is_rational(pma->p[i].set);
4476 if (rational < 0)
4477 map = isl_map_free(map);
4478 maff = isl_multi_aff_copy(pma->p[i].maff);
4479 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4480 map_i = isl_map_from_basic_map(bmap);
4481 map_i = isl_map_intersect_domain(map_i,
4482 isl_set_copy(pma->p[i].set));
4483 map = isl_map_union_disjoint(map, map_i);
4486 isl_pw_multi_aff_free(pma);
4487 return map;
4490 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4492 if (!pma)
4493 return NULL;
4495 if (!isl_space_is_set(pma->dim))
4496 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4497 "isl_pw_multi_aff cannot be converted into an isl_set",
4498 goto error);
4500 return set_from_map(isl_map_from_pw_multi_aff(pma));
4501 error:
4502 isl_pw_multi_aff_free(pma);
4503 return NULL;
4506 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4507 * denominator "denom".
4508 * "denom" is allowed to be negative, in which case the actual denominator
4509 * is -denom and the expressions are added instead.
4511 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4512 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4514 int i, first;
4515 int sign;
4516 isl_int d;
4518 first = isl_seq_first_non_zero(c, n);
4519 if (first == -1)
4520 return aff;
4522 sign = isl_int_sgn(denom);
4523 isl_int_init(d);
4524 isl_int_abs(d, denom);
4525 for (i = first; i < n; ++i) {
4526 isl_aff *aff_i;
4528 if (isl_int_is_zero(c[i]))
4529 continue;
4530 aff_i = isl_multi_aff_get_aff(ma, i);
4531 aff_i = isl_aff_scale(aff_i, c[i]);
4532 aff_i = isl_aff_scale_down(aff_i, d);
4533 if (sign >= 0)
4534 aff = isl_aff_sub(aff, aff_i);
4535 else
4536 aff = isl_aff_add(aff, aff_i);
4538 isl_int_clear(d);
4540 return aff;
4543 /* Extract an affine expression that expresses the output dimension "pos"
4544 * of "bmap" in terms of the parameters and input dimensions from
4545 * equality "eq".
4546 * Note that this expression may involve integer divisions defined
4547 * in terms of parameters and input dimensions.
4548 * The equality may also involve references to earlier (but not later)
4549 * output dimensions. These are replaced by the corresponding elements
4550 * in "ma".
4552 * If the equality is of the form
4554 * f(i) + h(j) + a x + g(i) = 0,
4556 * with f(i) a linear combinations of the parameters and input dimensions,
4557 * g(i) a linear combination of integer divisions defined in terms of the same
4558 * and h(j) a linear combinations of earlier output dimensions,
4559 * then the affine expression is
4561 * (-f(i) - g(i))/a - h(j)/a
4563 * If the equality is of the form
4565 * f(i) + h(j) - a x + g(i) = 0,
4567 * then the affine expression is
4569 * (f(i) + g(i))/a - h(j)/(-a)
4572 * If "div" refers to an integer division (i.e., it is smaller than
4573 * the number of integer divisions), then the equality constraint
4574 * does involve an integer division (the one at position "div") that
4575 * is defined in terms of output dimensions. However, this integer
4576 * division can be eliminated by exploiting a pair of constraints
4577 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4578 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4579 * -l + x >= 0.
4580 * In particular, let
4582 * x = e(i) + m floor(...)
4584 * with e(i) the expression derived above and floor(...) the integer
4585 * division involving output dimensions.
4586 * From
4588 * l <= x <= l + n,
4590 * we have
4592 * 0 <= x - l <= n
4594 * This means
4596 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4597 * = (e(i) - l) mod m
4599 * Therefore,
4601 * x - l = (e(i) - l) mod m
4603 * or
4605 * x = ((e(i) - l) mod m) + l
4607 * The variable "shift" below contains the expression -l, which may
4608 * also involve a linear combination of earlier output dimensions.
4610 static __isl_give isl_aff *extract_aff_from_equality(
4611 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4612 __isl_keep isl_multi_aff *ma)
4614 unsigned o_out;
4615 unsigned n_div, n_out;
4616 isl_ctx *ctx;
4617 isl_local_space *ls;
4618 isl_aff *aff, *shift;
4619 isl_val *mod;
4621 ctx = isl_basic_map_get_ctx(bmap);
4622 ls = isl_basic_map_get_local_space(bmap);
4623 ls = isl_local_space_domain(ls);
4624 aff = isl_aff_alloc(isl_local_space_copy(ls));
4625 if (!aff)
4626 goto error;
4627 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4628 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4629 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4630 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4631 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4632 isl_seq_cpy(aff->v->el + 1 + o_out,
4633 bmap->eq[eq] + o_out + n_out, n_div);
4634 } else {
4635 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4636 isl_seq_neg(aff->v->el + 1 + o_out,
4637 bmap->eq[eq] + o_out + n_out, n_div);
4639 if (div < n_div)
4640 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4641 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4642 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4643 bmap->eq[eq][o_out + pos]);
4644 if (div < n_div) {
4645 shift = isl_aff_alloc(isl_local_space_copy(ls));
4646 if (!shift)
4647 goto error;
4648 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4649 isl_seq_cpy(shift->v->el + 1 + o_out,
4650 bmap->ineq[ineq] + o_out + n_out, n_div);
4651 isl_int_set_si(shift->v->el[0], 1);
4652 shift = subtract_initial(shift, ma, pos,
4653 bmap->ineq[ineq] + o_out, ctx->negone);
4654 aff = isl_aff_add(aff, isl_aff_copy(shift));
4655 mod = isl_val_int_from_isl_int(ctx,
4656 bmap->eq[eq][o_out + n_out + div]);
4657 mod = isl_val_abs(mod);
4658 aff = isl_aff_mod_val(aff, mod);
4659 aff = isl_aff_sub(aff, shift);
4662 isl_local_space_free(ls);
4663 return aff;
4664 error:
4665 isl_local_space_free(ls);
4666 isl_aff_free(aff);
4667 return NULL;
4670 /* Given a basic map with output dimensions defined
4671 * in terms of the parameters input dimensions and earlier
4672 * output dimensions using an equality (and possibly a pair on inequalities),
4673 * extract an isl_aff that expresses output dimension "pos" in terms
4674 * of the parameters and input dimensions.
4675 * Note that this expression may involve integer divisions defined
4676 * in terms of parameters and input dimensions.
4677 * "ma" contains the expressions corresponding to earlier output dimensions.
4679 * This function shares some similarities with
4680 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4682 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4683 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4685 int eq, div, ineq;
4686 isl_aff *aff;
4688 if (!bmap)
4689 return NULL;
4690 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4691 if (eq >= bmap->n_eq)
4692 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4693 "unable to find suitable equality", return NULL);
4694 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4696 aff = isl_aff_remove_unused_divs(aff);
4697 return aff;
4700 /* Given a basic map where each output dimension is defined
4701 * in terms of the parameters and input dimensions using an equality,
4702 * extract an isl_multi_aff that expresses the output dimensions in terms
4703 * of the parameters and input dimensions.
4705 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4706 __isl_take isl_basic_map *bmap)
4708 int i;
4709 unsigned n_out;
4710 isl_multi_aff *ma;
4712 if (!bmap)
4713 return NULL;
4715 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4716 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4718 for (i = 0; i < n_out; ++i) {
4719 isl_aff *aff;
4721 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4722 ma = isl_multi_aff_set_aff(ma, i, aff);
4725 isl_basic_map_free(bmap);
4727 return ma;
4730 /* Given a basic set where each set dimension is defined
4731 * in terms of the parameters using an equality,
4732 * extract an isl_multi_aff that expresses the set dimensions in terms
4733 * of the parameters.
4735 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4736 __isl_take isl_basic_set *bset)
4738 return extract_isl_multi_aff_from_basic_map(bset);
4741 /* Create an isl_pw_multi_aff that is equivalent to
4742 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4743 * The given basic map is such that each output dimension is defined
4744 * in terms of the parameters and input dimensions using an equality.
4746 * Since some applications expect the result of isl_pw_multi_aff_from_map
4747 * to only contain integer affine expressions, we compute the floor
4748 * of the expression before returning.
4750 * Remove all constraints involving local variables without
4751 * an explicit representation (resulting in the removal of those
4752 * local variables) prior to the actual extraction to ensure
4753 * that the local spaces in which the resulting affine expressions
4754 * are created do not contain any unknown local variables.
4755 * Removing such constraints is safe because constraints involving
4756 * unknown local variables are not used to determine whether
4757 * a basic map is obviously single-valued.
4759 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4760 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4762 isl_multi_aff *ma;
4764 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4765 ma = extract_isl_multi_aff_from_basic_map(bmap);
4766 ma = isl_multi_aff_floor(ma);
4767 return isl_pw_multi_aff_alloc(domain, ma);
4770 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4771 * This obviously only works if the input "map" is single-valued.
4772 * If so, we compute the lexicographic minimum of the image in the form
4773 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4774 * to its lexicographic minimum.
4775 * If the input is not single-valued, we produce an error.
4777 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4778 __isl_take isl_map *map)
4780 int i;
4781 int sv;
4782 isl_pw_multi_aff *pma;
4784 sv = isl_map_is_single_valued(map);
4785 if (sv < 0)
4786 goto error;
4787 if (!sv)
4788 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4789 "map is not single-valued", goto error);
4790 map = isl_map_make_disjoint(map);
4791 if (!map)
4792 return NULL;
4794 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4796 for (i = 0; i < map->n; ++i) {
4797 isl_pw_multi_aff *pma_i;
4798 isl_basic_map *bmap;
4799 bmap = isl_basic_map_copy(map->p[i]);
4800 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4801 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4804 isl_map_free(map);
4805 return pma;
4806 error:
4807 isl_map_free(map);
4808 return NULL;
4811 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4812 * taking into account that the output dimension at position "d"
4813 * can be represented as
4815 * x = floor((e(...) + c1) / m)
4817 * given that constraint "i" is of the form
4819 * e(...) + c1 - m x >= 0
4822 * Let "map" be of the form
4824 * A -> B
4826 * We construct a mapping
4828 * A -> [A -> x = floor(...)]
4830 * apply that to the map, obtaining
4832 * [A -> x = floor(...)] -> B
4834 * and equate dimension "d" to x.
4835 * We then compute a isl_pw_multi_aff representation of the resulting map
4836 * and plug in the mapping above.
4838 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4839 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4841 isl_ctx *ctx;
4842 isl_space *space;
4843 isl_local_space *ls;
4844 isl_multi_aff *ma;
4845 isl_aff *aff;
4846 isl_vec *v;
4847 isl_map *insert;
4848 int offset;
4849 int n;
4850 int n_in;
4851 isl_pw_multi_aff *pma;
4852 isl_bool is_set;
4854 is_set = isl_map_is_set(map);
4855 if (is_set < 0)
4856 goto error;
4858 offset = isl_basic_map_offset(hull, isl_dim_out);
4859 ctx = isl_map_get_ctx(map);
4860 space = isl_space_domain(isl_map_get_space(map));
4861 n_in = isl_space_dim(space, isl_dim_set);
4862 n = isl_space_dim(space, isl_dim_all);
4864 v = isl_vec_alloc(ctx, 1 + 1 + n);
4865 if (v) {
4866 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4867 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4869 isl_basic_map_free(hull);
4871 ls = isl_local_space_from_space(isl_space_copy(space));
4872 aff = isl_aff_alloc_vec(ls, v);
4873 aff = isl_aff_floor(aff);
4874 if (is_set) {
4875 isl_space_free(space);
4876 ma = isl_multi_aff_from_aff(aff);
4877 } else {
4878 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4879 ma = isl_multi_aff_range_product(ma,
4880 isl_multi_aff_from_aff(aff));
4883 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4884 map = isl_map_apply_domain(map, insert);
4885 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4886 pma = isl_pw_multi_aff_from_map(map);
4887 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4889 return pma;
4890 error:
4891 isl_map_free(map);
4892 isl_basic_map_free(hull);
4893 return NULL;
4896 /* Is constraint "c" of the form
4898 * e(...) + c1 - m x >= 0
4900 * or
4902 * -e(...) + c2 + m x >= 0
4904 * where m > 1 and e only depends on parameters and input dimemnsions?
4906 * "offset" is the offset of the output dimensions
4907 * "pos" is the position of output dimension x.
4909 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4911 if (isl_int_is_zero(c[offset + d]))
4912 return 0;
4913 if (isl_int_is_one(c[offset + d]))
4914 return 0;
4915 if (isl_int_is_negone(c[offset + d]))
4916 return 0;
4917 if (isl_seq_first_non_zero(c + offset, d) != -1)
4918 return 0;
4919 if (isl_seq_first_non_zero(c + offset + d + 1,
4920 total - (offset + d + 1)) != -1)
4921 return 0;
4922 return 1;
4925 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4927 * As a special case, we first check if there is any pair of constraints,
4928 * shared by all the basic maps in "map" that force a given dimension
4929 * to be equal to the floor of some affine combination of the input dimensions.
4931 * In particular, if we can find two constraints
4933 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4935 * and
4937 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4939 * where m > 1 and e only depends on parameters and input dimemnsions,
4940 * and such that
4942 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4944 * then we know that we can take
4946 * x = floor((e(...) + c1) / m)
4948 * without having to perform any computation.
4950 * Note that we know that
4952 * c1 + c2 >= 1
4954 * If c1 + c2 were 0, then we would have detected an equality during
4955 * simplification. If c1 + c2 were negative, then we would have detected
4956 * a contradiction.
4958 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4959 __isl_take isl_map *map)
4961 int d, dim;
4962 int i, j, n;
4963 int offset, total;
4964 isl_int sum;
4965 isl_basic_map *hull;
4967 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4968 if (!hull)
4969 goto error;
4971 isl_int_init(sum);
4972 dim = isl_map_dim(map, isl_dim_out);
4973 offset = isl_basic_map_offset(hull, isl_dim_out);
4974 total = 1 + isl_basic_map_total_dim(hull);
4975 n = hull->n_ineq;
4976 for (d = 0; d < dim; ++d) {
4977 for (i = 0; i < n; ++i) {
4978 if (!is_potential_div_constraint(hull->ineq[i],
4979 offset, d, total))
4980 continue;
4981 for (j = i + 1; j < n; ++j) {
4982 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4983 hull->ineq[j] + 1, total - 1))
4984 continue;
4985 isl_int_add(sum, hull->ineq[i][0],
4986 hull->ineq[j][0]);
4987 if (isl_int_abs_lt(sum,
4988 hull->ineq[i][offset + d]))
4989 break;
4992 if (j >= n)
4993 continue;
4994 isl_int_clear(sum);
4995 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4996 j = i;
4997 return pw_multi_aff_from_map_div(map, hull, d, j);
5000 isl_int_clear(sum);
5001 isl_basic_map_free(hull);
5002 return pw_multi_aff_from_map_base(map);
5003 error:
5004 isl_map_free(map);
5005 isl_basic_map_free(hull);
5006 return NULL;
5009 /* Given an affine expression
5011 * [A -> B] -> f(A,B)
5013 * construct an isl_multi_aff
5015 * [A -> B] -> B'
5017 * such that dimension "d" in B' is set to "aff" and the remaining
5018 * dimensions are set equal to the corresponding dimensions in B.
5019 * "n_in" is the dimension of the space A.
5020 * "n_out" is the dimension of the space B.
5022 * If "is_set" is set, then the affine expression is of the form
5024 * [B] -> f(B)
5026 * and we construct an isl_multi_aff
5028 * B -> B'
5030 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5031 unsigned n_in, unsigned n_out, int is_set)
5033 int i;
5034 isl_multi_aff *ma;
5035 isl_space *space, *space2;
5036 isl_local_space *ls;
5038 space = isl_aff_get_domain_space(aff);
5039 ls = isl_local_space_from_space(isl_space_copy(space));
5040 space2 = isl_space_copy(space);
5041 if (!is_set)
5042 space2 = isl_space_range(isl_space_unwrap(space2));
5043 space = isl_space_map_from_domain_and_range(space, space2);
5044 ma = isl_multi_aff_alloc(space);
5045 ma = isl_multi_aff_set_aff(ma, d, aff);
5047 for (i = 0; i < n_out; ++i) {
5048 if (i == d)
5049 continue;
5050 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5051 isl_dim_set, n_in + i);
5052 ma = isl_multi_aff_set_aff(ma, i, aff);
5055 isl_local_space_free(ls);
5057 return ma;
5060 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5061 * taking into account that the dimension at position "d" can be written as
5063 * x = m a + f(..) (1)
5065 * where m is equal to "gcd".
5066 * "i" is the index of the equality in "hull" that defines f(..).
5067 * In particular, the equality is of the form
5069 * f(..) - x + m g(existentials) = 0
5071 * or
5073 * -f(..) + x + m g(existentials) = 0
5075 * We basically plug (1) into "map", resulting in a map with "a"
5076 * in the range instead of "x". The corresponding isl_pw_multi_aff
5077 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5079 * Specifically, given the input map
5081 * A -> B
5083 * We first wrap it into a set
5085 * [A -> B]
5087 * and define (1) on top of the corresponding space, resulting in "aff".
5088 * We use this to create an isl_multi_aff that maps the output position "d"
5089 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5090 * We plug this into the wrapped map, unwrap the result and compute the
5091 * corresponding isl_pw_multi_aff.
5092 * The result is an expression
5094 * A -> T(A)
5096 * We adjust that to
5098 * A -> [A -> T(A)]
5100 * so that we can plug that into "aff", after extending the latter to
5101 * a mapping
5103 * [A -> B] -> B'
5106 * If "map" is actually a set, then there is no "A" space, meaning
5107 * that we do not need to perform any wrapping, and that the result
5108 * of the recursive call is of the form
5110 * [T]
5112 * which is plugged into a mapping of the form
5114 * B -> B'
5116 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5117 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5118 isl_int gcd)
5120 isl_set *set;
5121 isl_space *space;
5122 isl_local_space *ls;
5123 isl_aff *aff;
5124 isl_multi_aff *ma;
5125 isl_pw_multi_aff *pma, *id;
5126 unsigned n_in;
5127 unsigned o_out;
5128 unsigned n_out;
5129 isl_bool is_set;
5131 is_set = isl_map_is_set(map);
5132 if (is_set < 0)
5133 goto error;
5135 n_in = isl_basic_map_dim(hull, isl_dim_in);
5136 n_out = isl_basic_map_dim(hull, isl_dim_out);
5137 o_out = isl_basic_map_offset(hull, isl_dim_out);
5139 if (is_set)
5140 set = map;
5141 else
5142 set = isl_map_wrap(map);
5143 space = isl_space_map_from_set(isl_set_get_space(set));
5144 ma = isl_multi_aff_identity(space);
5145 ls = isl_local_space_from_space(isl_set_get_space(set));
5146 aff = isl_aff_alloc(ls);
5147 if (aff) {
5148 isl_int_set_si(aff->v->el[0], 1);
5149 if (isl_int_is_one(hull->eq[i][o_out + d]))
5150 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5151 aff->v->size - 1);
5152 else
5153 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5154 aff->v->size - 1);
5155 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5157 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5158 set = isl_set_preimage_multi_aff(set, ma);
5160 ma = range_map(aff, d, n_in, n_out, is_set);
5162 if (is_set)
5163 map = set;
5164 else
5165 map = isl_set_unwrap(set);
5166 pma = isl_pw_multi_aff_from_map(map);
5168 if (!is_set) {
5169 space = isl_pw_multi_aff_get_domain_space(pma);
5170 space = isl_space_map_from_set(space);
5171 id = isl_pw_multi_aff_identity(space);
5172 pma = isl_pw_multi_aff_range_product(id, pma);
5174 id = isl_pw_multi_aff_from_multi_aff(ma);
5175 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5177 isl_basic_map_free(hull);
5178 return pma;
5179 error:
5180 isl_map_free(map);
5181 isl_basic_map_free(hull);
5182 return NULL;
5185 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5186 * "hull" contains the equalities valid for "map".
5188 * Check if any of the output dimensions is "strided".
5189 * That is, we check if it can be written as
5191 * x = m a + f(..)
5193 * with m greater than 1, a some combination of existentially quantified
5194 * variables and f an expression in the parameters and input dimensions.
5195 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5197 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5198 * special case.
5200 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5201 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5203 int i, j;
5204 unsigned n_out;
5205 unsigned o_out;
5206 unsigned n_div;
5207 unsigned o_div;
5208 isl_int gcd;
5210 n_div = isl_basic_map_dim(hull, isl_dim_div);
5211 o_div = isl_basic_map_offset(hull, isl_dim_div);
5213 if (n_div == 0) {
5214 isl_basic_map_free(hull);
5215 return pw_multi_aff_from_map_check_div(map);
5218 isl_int_init(gcd);
5220 n_out = isl_basic_map_dim(hull, isl_dim_out);
5221 o_out = isl_basic_map_offset(hull, isl_dim_out);
5223 for (i = 0; i < n_out; ++i) {
5224 for (j = 0; j < hull->n_eq; ++j) {
5225 isl_int *eq = hull->eq[j];
5226 isl_pw_multi_aff *res;
5228 if (!isl_int_is_one(eq[o_out + i]) &&
5229 !isl_int_is_negone(eq[o_out + i]))
5230 continue;
5231 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5232 continue;
5233 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5234 n_out - (i + 1)) != -1)
5235 continue;
5236 isl_seq_gcd(eq + o_div, n_div, &gcd);
5237 if (isl_int_is_zero(gcd))
5238 continue;
5239 if (isl_int_is_one(gcd))
5240 continue;
5242 res = pw_multi_aff_from_map_stride(map, hull,
5243 i, j, gcd);
5244 isl_int_clear(gcd);
5245 return res;
5249 isl_int_clear(gcd);
5250 isl_basic_map_free(hull);
5251 return pw_multi_aff_from_map_check_div(map);
5254 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5256 * As a special case, we first check if all output dimensions are uniquely
5257 * defined in terms of the parameters and input dimensions over the entire
5258 * domain. If so, we extract the desired isl_pw_multi_aff directly
5259 * from the affine hull of "map" and its domain.
5261 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5262 * special cases.
5264 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5266 isl_bool sv;
5267 isl_basic_map *hull;
5269 if (!map)
5270 return NULL;
5272 if (isl_map_n_basic_map(map) == 1) {
5273 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5274 hull = isl_basic_map_plain_affine_hull(hull);
5275 sv = isl_basic_map_plain_is_single_valued(hull);
5276 if (sv >= 0 && sv)
5277 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5278 hull);
5279 isl_basic_map_free(hull);
5281 map = isl_map_detect_equalities(map);
5282 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5283 sv = isl_basic_map_plain_is_single_valued(hull);
5284 if (sv >= 0 && sv)
5285 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5286 if (sv >= 0)
5287 return pw_multi_aff_from_map_check_strides(map, hull);
5288 isl_basic_map_free(hull);
5289 isl_map_free(map);
5290 return NULL;
5293 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5295 return isl_pw_multi_aff_from_map(set);
5298 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5299 * add it to *user.
5301 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5303 isl_union_pw_multi_aff **upma = user;
5304 isl_pw_multi_aff *pma;
5306 pma = isl_pw_multi_aff_from_map(map);
5307 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5309 return *upma ? isl_stat_ok : isl_stat_error;
5312 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5313 * domain.
5315 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5316 __isl_take isl_aff *aff)
5318 isl_multi_aff *ma;
5319 isl_pw_multi_aff *pma;
5321 ma = isl_multi_aff_from_aff(aff);
5322 pma = isl_pw_multi_aff_from_multi_aff(ma);
5323 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5326 /* Try and create an isl_union_pw_multi_aff that is equivalent
5327 * to the given isl_union_map.
5328 * The isl_union_map is required to be single-valued in each space.
5329 * Otherwise, an error is produced.
5331 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5332 __isl_take isl_union_map *umap)
5334 isl_space *space;
5335 isl_union_pw_multi_aff *upma;
5337 space = isl_union_map_get_space(umap);
5338 upma = isl_union_pw_multi_aff_empty(space);
5339 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5340 upma = isl_union_pw_multi_aff_free(upma);
5341 isl_union_map_free(umap);
5343 return upma;
5346 /* Try and create an isl_union_pw_multi_aff that is equivalent
5347 * to the given isl_union_set.
5348 * The isl_union_set is required to be a singleton in each space.
5349 * Otherwise, an error is produced.
5351 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5352 __isl_take isl_union_set *uset)
5354 return isl_union_pw_multi_aff_from_union_map(uset);
5357 /* Return the piecewise affine expression "set ? 1 : 0".
5359 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5361 isl_pw_aff *pa;
5362 isl_space *space = isl_set_get_space(set);
5363 isl_local_space *ls = isl_local_space_from_space(space);
5364 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5365 isl_aff *one = isl_aff_zero_on_domain(ls);
5367 one = isl_aff_add_constant_si(one, 1);
5368 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5369 set = isl_set_complement(set);
5370 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5372 return pa;
5375 /* Plug in "subs" for dimension "type", "pos" of "aff".
5377 * Let i be the dimension to replace and let "subs" be of the form
5379 * f/d
5381 * and "aff" of the form
5383 * (a i + g)/m
5385 * The result is
5387 * (a f + d g')/(m d)
5389 * where g' is the result of plugging in "subs" in each of the integer
5390 * divisions in g.
5392 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5393 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5395 isl_ctx *ctx;
5396 isl_int v;
5398 aff = isl_aff_cow(aff);
5399 if (!aff || !subs)
5400 return isl_aff_free(aff);
5402 ctx = isl_aff_get_ctx(aff);
5403 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5404 isl_die(ctx, isl_error_invalid,
5405 "spaces don't match", return isl_aff_free(aff));
5406 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5407 isl_die(ctx, isl_error_unsupported,
5408 "cannot handle divs yet", return isl_aff_free(aff));
5410 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5411 if (!aff->ls)
5412 return isl_aff_free(aff);
5414 aff->v = isl_vec_cow(aff->v);
5415 if (!aff->v)
5416 return isl_aff_free(aff);
5418 pos += isl_local_space_offset(aff->ls, type);
5420 isl_int_init(v);
5421 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5422 aff->v->size, subs->v->size, v);
5423 isl_int_clear(v);
5425 return aff;
5428 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5429 * expressions in "maff".
5431 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5432 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5433 __isl_keep isl_aff *subs)
5435 int i;
5437 maff = isl_multi_aff_cow(maff);
5438 if (!maff || !subs)
5439 return isl_multi_aff_free(maff);
5441 if (type == isl_dim_in)
5442 type = isl_dim_set;
5444 for (i = 0; i < maff->n; ++i) {
5445 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5446 type, pos, subs);
5447 if (!maff->u.p[i])
5448 return isl_multi_aff_free(maff);
5451 return maff;
5454 /* Plug in "subs" for dimension "type", "pos" of "pma".
5456 * pma is of the form
5458 * A_i(v) -> M_i(v)
5460 * while subs is of the form
5462 * v' = B_j(v) -> S_j
5464 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5465 * has a contribution in the result, in particular
5467 * C_ij(S_j) -> M_i(S_j)
5469 * Note that plugging in S_j in C_ij may also result in an empty set
5470 * and this contribution should simply be discarded.
5472 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5473 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5474 __isl_keep isl_pw_aff *subs)
5476 int i, j, n;
5477 isl_pw_multi_aff *res;
5479 if (!pma || !subs)
5480 return isl_pw_multi_aff_free(pma);
5482 n = pma->n * subs->n;
5483 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5485 for (i = 0; i < pma->n; ++i) {
5486 for (j = 0; j < subs->n; ++j) {
5487 isl_set *common;
5488 isl_multi_aff *res_ij;
5489 int empty;
5491 common = isl_set_intersect(
5492 isl_set_copy(pma->p[i].set),
5493 isl_set_copy(subs->p[j].set));
5494 common = isl_set_substitute(common,
5495 type, pos, subs->p[j].aff);
5496 empty = isl_set_plain_is_empty(common);
5497 if (empty < 0 || empty) {
5498 isl_set_free(common);
5499 if (empty < 0)
5500 goto error;
5501 continue;
5504 res_ij = isl_multi_aff_substitute(
5505 isl_multi_aff_copy(pma->p[i].maff),
5506 type, pos, subs->p[j].aff);
5508 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5512 isl_pw_multi_aff_free(pma);
5513 return res;
5514 error:
5515 isl_pw_multi_aff_free(pma);
5516 isl_pw_multi_aff_free(res);
5517 return NULL;
5520 /* Compute the preimage of a range of dimensions in the affine expression "src"
5521 * under "ma" and put the result in "dst". The number of dimensions in "src"
5522 * that precede the range is given by "n_before". The number of dimensions
5523 * in the range is given by the number of output dimensions of "ma".
5524 * The number of dimensions that follow the range is given by "n_after".
5525 * If "has_denom" is set (to one),
5526 * then "src" and "dst" have an extra initial denominator.
5527 * "n_div_ma" is the number of existentials in "ma"
5528 * "n_div_bset" is the number of existentials in "src"
5529 * The resulting "dst" (which is assumed to have been allocated by
5530 * the caller) contains coefficients for both sets of existentials,
5531 * first those in "ma" and then those in "src".
5532 * f, c1, c2 and g are temporary objects that have been initialized
5533 * by the caller.
5535 * Let src represent the expression
5537 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5539 * and let ma represent the expressions
5541 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5543 * We start out with the following expression for dst:
5545 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5547 * with the multiplication factor f initially equal to 1
5548 * and f \sum_i b_i v_i kept separately.
5549 * For each x_i that we substitute, we multiply the numerator
5550 * (and denominator) of dst by c_1 = m_i and add the numerator
5551 * of the x_i expression multiplied by c_2 = f b_i,
5552 * after removing the common factors of c_1 and c_2.
5553 * The multiplication factor f also needs to be multiplied by c_1
5554 * for the next x_j, j > i.
5556 void isl_seq_preimage(isl_int *dst, isl_int *src,
5557 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5558 int n_div_ma, int n_div_bmap,
5559 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5561 int i;
5562 int n_param, n_in, n_out;
5563 int o_dst, o_src;
5565 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5566 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5567 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5569 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5570 o_dst = o_src = has_denom + 1 + n_param + n_before;
5571 isl_seq_clr(dst + o_dst, n_in);
5572 o_dst += n_in;
5573 o_src += n_out;
5574 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5575 o_dst += n_after;
5576 o_src += n_after;
5577 isl_seq_clr(dst + o_dst, n_div_ma);
5578 o_dst += n_div_ma;
5579 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5581 isl_int_set_si(f, 1);
5583 for (i = 0; i < n_out; ++i) {
5584 int offset = has_denom + 1 + n_param + n_before + i;
5586 if (isl_int_is_zero(src[offset]))
5587 continue;
5588 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5589 isl_int_mul(c2, f, src[offset]);
5590 isl_int_gcd(g, c1, c2);
5591 isl_int_divexact(c1, c1, g);
5592 isl_int_divexact(c2, c2, g);
5594 isl_int_mul(f, f, c1);
5595 o_dst = has_denom;
5596 o_src = 1;
5597 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5598 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5599 o_dst += 1 + n_param;
5600 o_src += 1 + n_param;
5601 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5602 o_dst += n_before;
5603 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5604 c2, ma->u.p[i]->v->el + o_src, n_in);
5605 o_dst += n_in;
5606 o_src += n_in;
5607 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5608 o_dst += n_after;
5609 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5610 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5611 o_dst += n_div_ma;
5612 o_src += n_div_ma;
5613 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5614 if (has_denom)
5615 isl_int_mul(dst[0], dst[0], c1);
5619 /* Compute the pullback of "aff" by the function represented by "ma".
5620 * In other words, plug in "ma" in "aff". The result is an affine expression
5621 * defined over the domain space of "ma".
5623 * If "aff" is represented by
5625 * (a(p) + b x + c(divs))/d
5627 * and ma is represented by
5629 * x = D(p) + F(y) + G(divs')
5631 * then the result is
5633 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5635 * The divs in the local space of the input are similarly adjusted
5636 * through a call to isl_local_space_preimage_multi_aff.
5638 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5639 __isl_take isl_multi_aff *ma)
5641 isl_aff *res = NULL;
5642 isl_local_space *ls;
5643 int n_div_aff, n_div_ma;
5644 isl_int f, c1, c2, g;
5646 ma = isl_multi_aff_align_divs(ma);
5647 if (!aff || !ma)
5648 goto error;
5650 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5651 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5653 ls = isl_aff_get_domain_local_space(aff);
5654 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5655 res = isl_aff_alloc(ls);
5656 if (!res)
5657 goto error;
5659 isl_int_init(f);
5660 isl_int_init(c1);
5661 isl_int_init(c2);
5662 isl_int_init(g);
5664 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5665 f, c1, c2, g, 1);
5667 isl_int_clear(f);
5668 isl_int_clear(c1);
5669 isl_int_clear(c2);
5670 isl_int_clear(g);
5672 isl_aff_free(aff);
5673 isl_multi_aff_free(ma);
5674 res = isl_aff_normalize(res);
5675 return res;
5676 error:
5677 isl_aff_free(aff);
5678 isl_multi_aff_free(ma);
5679 isl_aff_free(res);
5680 return NULL;
5683 /* Compute the pullback of "aff1" by the function represented by "aff2".
5684 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5685 * defined over the domain space of "aff1".
5687 * The domain of "aff1" should match the range of "aff2", which means
5688 * that it should be single-dimensional.
5690 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5691 __isl_take isl_aff *aff2)
5693 isl_multi_aff *ma;
5695 ma = isl_multi_aff_from_aff(aff2);
5696 return isl_aff_pullback_multi_aff(aff1, ma);
5699 /* Compute the pullback of "ma1" by the function represented by "ma2".
5700 * In other words, plug in "ma2" in "ma1".
5702 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5704 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5705 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5707 int i;
5708 isl_space *space = NULL;
5710 ma2 = isl_multi_aff_align_divs(ma2);
5711 ma1 = isl_multi_aff_cow(ma1);
5712 if (!ma1 || !ma2)
5713 goto error;
5715 space = isl_space_join(isl_multi_aff_get_space(ma2),
5716 isl_multi_aff_get_space(ma1));
5718 for (i = 0; i < ma1->n; ++i) {
5719 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5720 isl_multi_aff_copy(ma2));
5721 if (!ma1->u.p[i])
5722 goto error;
5725 ma1 = isl_multi_aff_reset_space(ma1, space);
5726 isl_multi_aff_free(ma2);
5727 return ma1;
5728 error:
5729 isl_space_free(space);
5730 isl_multi_aff_free(ma2);
5731 isl_multi_aff_free(ma1);
5732 return NULL;
5735 /* Compute the pullback of "ma1" by the function represented by "ma2".
5736 * In other words, plug in "ma2" in "ma1".
5738 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5739 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5741 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5742 &isl_multi_aff_pullback_multi_aff_aligned);
5745 /* Extend the local space of "dst" to include the divs
5746 * in the local space of "src".
5748 * If "src" does not have any divs or if the local spaces of "dst" and
5749 * "src" are the same, then no extension is required.
5751 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5752 __isl_keep isl_aff *src)
5754 isl_ctx *ctx;
5755 int src_n_div, dst_n_div;
5756 int *exp1 = NULL;
5757 int *exp2 = NULL;
5758 isl_bool equal;
5759 isl_mat *div;
5761 if (!src || !dst)
5762 return isl_aff_free(dst);
5764 ctx = isl_aff_get_ctx(src);
5765 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5766 if (equal < 0)
5767 return isl_aff_free(dst);
5768 if (!equal)
5769 isl_die(ctx, isl_error_invalid,
5770 "spaces don't match", goto error);
5772 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5773 if (src_n_div == 0)
5774 return dst;
5775 equal = isl_local_space_is_equal(src->ls, dst->ls);
5776 if (equal < 0)
5777 return isl_aff_free(dst);
5778 if (equal)
5779 return dst;
5781 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5782 exp1 = isl_alloc_array(ctx, int, src_n_div);
5783 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5784 if (!exp1 || (dst_n_div && !exp2))
5785 goto error;
5787 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5788 dst = isl_aff_expand_divs(dst, div, exp2);
5789 free(exp1);
5790 free(exp2);
5792 return dst;
5793 error:
5794 free(exp1);
5795 free(exp2);
5796 return isl_aff_free(dst);
5799 /* Adjust the local spaces of the affine expressions in "maff"
5800 * such that they all have the save divs.
5802 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5803 __isl_take isl_multi_aff *maff)
5805 int i;
5807 if (!maff)
5808 return NULL;
5809 if (maff->n == 0)
5810 return maff;
5811 maff = isl_multi_aff_cow(maff);
5812 if (!maff)
5813 return NULL;
5815 for (i = 1; i < maff->n; ++i)
5816 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5817 for (i = 1; i < maff->n; ++i) {
5818 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5819 if (!maff->u.p[i])
5820 return isl_multi_aff_free(maff);
5823 return maff;
5826 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5828 aff = isl_aff_cow(aff);
5829 if (!aff)
5830 return NULL;
5832 aff->ls = isl_local_space_lift(aff->ls);
5833 if (!aff->ls)
5834 return isl_aff_free(aff);
5836 return aff;
5839 /* Lift "maff" to a space with extra dimensions such that the result
5840 * has no more existentially quantified variables.
5841 * If "ls" is not NULL, then *ls is assigned the local space that lies
5842 * at the basis of the lifting applied to "maff".
5844 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5845 __isl_give isl_local_space **ls)
5847 int i;
5848 isl_space *space;
5849 unsigned n_div;
5851 if (ls)
5852 *ls = NULL;
5854 if (!maff)
5855 return NULL;
5857 if (maff->n == 0) {
5858 if (ls) {
5859 isl_space *space = isl_multi_aff_get_domain_space(maff);
5860 *ls = isl_local_space_from_space(space);
5861 if (!*ls)
5862 return isl_multi_aff_free(maff);
5864 return maff;
5867 maff = isl_multi_aff_cow(maff);
5868 maff = isl_multi_aff_align_divs(maff);
5869 if (!maff)
5870 return NULL;
5872 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5873 space = isl_multi_aff_get_space(maff);
5874 space = isl_space_lift(isl_space_domain(space), n_div);
5875 space = isl_space_extend_domain_with_range(space,
5876 isl_multi_aff_get_space(maff));
5877 if (!space)
5878 return isl_multi_aff_free(maff);
5879 isl_space_free(maff->space);
5880 maff->space = space;
5882 if (ls) {
5883 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5884 if (!*ls)
5885 return isl_multi_aff_free(maff);
5888 for (i = 0; i < maff->n; ++i) {
5889 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5890 if (!maff->u.p[i])
5891 goto error;
5894 return maff;
5895 error:
5896 if (ls)
5897 isl_local_space_free(*ls);
5898 return isl_multi_aff_free(maff);
5902 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5904 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5905 __isl_keep isl_pw_multi_aff *pma, int pos)
5907 int i;
5908 int n_out;
5909 isl_space *space;
5910 isl_pw_aff *pa;
5912 if (!pma)
5913 return NULL;
5915 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5916 if (pos < 0 || pos >= n_out)
5917 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5918 "index out of bounds", return NULL);
5920 space = isl_pw_multi_aff_get_space(pma);
5921 space = isl_space_drop_dims(space, isl_dim_out,
5922 pos + 1, n_out - pos - 1);
5923 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5925 pa = isl_pw_aff_alloc_size(space, pma->n);
5926 for (i = 0; i < pma->n; ++i) {
5927 isl_aff *aff;
5928 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5929 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5932 return pa;
5935 /* Return an isl_pw_multi_aff with the given "set" as domain and
5936 * an unnamed zero-dimensional range.
5938 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5939 __isl_take isl_set *set)
5941 isl_multi_aff *ma;
5942 isl_space *space;
5944 space = isl_set_get_space(set);
5945 space = isl_space_from_domain(space);
5946 ma = isl_multi_aff_zero(space);
5947 return isl_pw_multi_aff_alloc(set, ma);
5950 /* Add an isl_pw_multi_aff with the given "set" as domain and
5951 * an unnamed zero-dimensional range to *user.
5953 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5954 void *user)
5956 isl_union_pw_multi_aff **upma = user;
5957 isl_pw_multi_aff *pma;
5959 pma = isl_pw_multi_aff_from_domain(set);
5960 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5962 return isl_stat_ok;
5965 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5966 * an unnamed zero-dimensional range.
5968 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5969 __isl_take isl_union_set *uset)
5971 isl_space *space;
5972 isl_union_pw_multi_aff *upma;
5974 if (!uset)
5975 return NULL;
5977 space = isl_union_set_get_space(uset);
5978 upma = isl_union_pw_multi_aff_empty(space);
5980 if (isl_union_set_foreach_set(uset,
5981 &add_pw_multi_aff_from_domain, &upma) < 0)
5982 goto error;
5984 isl_union_set_free(uset);
5985 return upma;
5986 error:
5987 isl_union_set_free(uset);
5988 isl_union_pw_multi_aff_free(upma);
5989 return NULL;
5992 /* Convert "pma" to an isl_map and add it to *umap.
5994 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5995 void *user)
5997 isl_union_map **umap = user;
5998 isl_map *map;
6000 map = isl_map_from_pw_multi_aff(pma);
6001 *umap = isl_union_map_add_map(*umap, map);
6003 return isl_stat_ok;
6006 /* Construct a union map mapping the domain of the union
6007 * piecewise multi-affine expression to its range, with each dimension
6008 * in the range equated to the corresponding affine expression on its cell.
6010 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
6011 __isl_take isl_union_pw_multi_aff *upma)
6013 isl_space *space;
6014 isl_union_map *umap;
6016 if (!upma)
6017 return NULL;
6019 space = isl_union_pw_multi_aff_get_space(upma);
6020 umap = isl_union_map_empty(space);
6022 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
6023 &map_from_pw_multi_aff, &umap) < 0)
6024 goto error;
6026 isl_union_pw_multi_aff_free(upma);
6027 return umap;
6028 error:
6029 isl_union_pw_multi_aff_free(upma);
6030 isl_union_map_free(umap);
6031 return NULL;
6034 /* Local data for bin_entry and the callback "fn".
6036 struct isl_union_pw_multi_aff_bin_data {
6037 isl_union_pw_multi_aff *upma2;
6038 isl_union_pw_multi_aff *res;
6039 isl_pw_multi_aff *pma;
6040 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6043 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6044 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6046 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6048 struct isl_union_pw_multi_aff_bin_data *data = user;
6049 isl_stat r;
6051 data->pma = pma;
6052 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6053 data->fn, data);
6054 isl_pw_multi_aff_free(pma);
6056 return r;
6059 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6060 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6061 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6062 * as *entry. The callback should adjust data->res if desired.
6064 static __isl_give isl_union_pw_multi_aff *bin_op(
6065 __isl_take isl_union_pw_multi_aff *upma1,
6066 __isl_take isl_union_pw_multi_aff *upma2,
6067 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6069 isl_space *space;
6070 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6072 space = isl_union_pw_multi_aff_get_space(upma2);
6073 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6074 space = isl_union_pw_multi_aff_get_space(upma1);
6075 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6077 if (!upma1 || !upma2)
6078 goto error;
6080 data.upma2 = upma2;
6081 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6082 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6083 &bin_entry, &data) < 0)
6084 goto error;
6086 isl_union_pw_multi_aff_free(upma1);
6087 isl_union_pw_multi_aff_free(upma2);
6088 return data.res;
6089 error:
6090 isl_union_pw_multi_aff_free(upma1);
6091 isl_union_pw_multi_aff_free(upma2);
6092 isl_union_pw_multi_aff_free(data.res);
6093 return NULL;
6096 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6097 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6099 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
6100 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6102 isl_space *space;
6104 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6105 isl_pw_multi_aff_get_space(pma2));
6106 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6107 &isl_multi_aff_range_product);
6110 /* Given two isl_pw_multi_affs A -> B and C -> D,
6111 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6113 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6114 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6116 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6117 &pw_multi_aff_range_product);
6120 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6121 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6123 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6124 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6126 isl_space *space;
6128 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6129 isl_pw_multi_aff_get_space(pma2));
6130 space = isl_space_flatten_range(space);
6131 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6132 &isl_multi_aff_flat_range_product);
6135 /* Given two isl_pw_multi_affs A -> B and C -> D,
6136 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6138 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6139 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6141 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6142 &pw_multi_aff_flat_range_product);
6145 /* If data->pma and "pma2" have the same domain space, then compute
6146 * their flat range product and the result to data->res.
6148 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6149 void *user)
6151 struct isl_union_pw_multi_aff_bin_data *data = user;
6153 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6154 pma2->dim, isl_dim_in)) {
6155 isl_pw_multi_aff_free(pma2);
6156 return isl_stat_ok;
6159 pma2 = isl_pw_multi_aff_flat_range_product(
6160 isl_pw_multi_aff_copy(data->pma), pma2);
6162 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6164 return isl_stat_ok;
6167 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6168 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6170 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6171 __isl_take isl_union_pw_multi_aff *upma1,
6172 __isl_take isl_union_pw_multi_aff *upma2)
6174 return bin_op(upma1, upma2, &flat_range_product_entry);
6177 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6178 * The parameters are assumed to have been aligned.
6180 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6181 * except that it works on two different isl_pw_* types.
6183 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6184 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6185 __isl_take isl_pw_aff *pa)
6187 int i, j, n;
6188 isl_pw_multi_aff *res = NULL;
6190 if (!pma || !pa)
6191 goto error;
6193 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6194 pa->dim, isl_dim_in))
6195 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6196 "domains don't match", goto error);
6197 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6198 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6199 "index out of bounds", goto error);
6201 n = pma->n * pa->n;
6202 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6204 for (i = 0; i < pma->n; ++i) {
6205 for (j = 0; j < pa->n; ++j) {
6206 isl_set *common;
6207 isl_multi_aff *res_ij;
6208 int empty;
6210 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6211 isl_set_copy(pa->p[j].set));
6212 empty = isl_set_plain_is_empty(common);
6213 if (empty < 0 || empty) {
6214 isl_set_free(common);
6215 if (empty < 0)
6216 goto error;
6217 continue;
6220 res_ij = isl_multi_aff_set_aff(
6221 isl_multi_aff_copy(pma->p[i].maff), pos,
6222 isl_aff_copy(pa->p[j].aff));
6223 res_ij = isl_multi_aff_gist(res_ij,
6224 isl_set_copy(common));
6226 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6230 isl_pw_multi_aff_free(pma);
6231 isl_pw_aff_free(pa);
6232 return res;
6233 error:
6234 isl_pw_multi_aff_free(pma);
6235 isl_pw_aff_free(pa);
6236 return isl_pw_multi_aff_free(res);
6239 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6241 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6242 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6243 __isl_take isl_pw_aff *pa)
6245 isl_bool equal_params;
6247 if (!pma || !pa)
6248 goto error;
6249 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6250 if (equal_params < 0)
6251 goto error;
6252 if (equal_params)
6253 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6254 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6255 isl_pw_aff_check_named_params(pa) < 0)
6256 goto error;
6257 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6258 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6259 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6260 error:
6261 isl_pw_multi_aff_free(pma);
6262 isl_pw_aff_free(pa);
6263 return NULL;
6266 /* Do the parameters of "pa" match those of "space"?
6268 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6269 __isl_keep isl_space *space)
6271 isl_space *pa_space;
6272 isl_bool match;
6274 if (!pa || !space)
6275 return isl_bool_error;
6277 pa_space = isl_pw_aff_get_space(pa);
6279 match = isl_space_has_equal_params(space, pa_space);
6281 isl_space_free(pa_space);
6282 return match;
6285 /* Check that the domain space of "pa" matches "space".
6287 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6288 __isl_keep isl_space *space)
6290 isl_space *pa_space;
6291 isl_bool match;
6293 if (!pa || !space)
6294 return isl_stat_error;
6296 pa_space = isl_pw_aff_get_space(pa);
6298 match = isl_space_has_equal_params(space, pa_space);
6299 if (match < 0)
6300 goto error;
6301 if (!match)
6302 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6303 "parameters don't match", goto error);
6304 match = isl_space_tuple_is_equal(space, isl_dim_in,
6305 pa_space, isl_dim_in);
6306 if (match < 0)
6307 goto error;
6308 if (!match)
6309 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6310 "domains don't match", goto error);
6311 isl_space_free(pa_space);
6312 return isl_stat_ok;
6313 error:
6314 isl_space_free(pa_space);
6315 return isl_stat_error;
6318 #undef BASE
6319 #define BASE pw_aff
6320 #undef DOMBASE
6321 #define DOMBASE set
6323 #include <isl_multi_explicit_domain.c>
6324 #include <isl_multi_pw_aff_explicit_domain.c>
6325 #include <isl_multi_templ.c>
6326 #include <isl_multi_apply_set.c>
6327 #include <isl_multi_coalesce.c>
6328 #include <isl_multi_dims.c>
6329 #include <isl_multi_gist.c>
6330 #include <isl_multi_hash.c>
6331 #include <isl_multi_align_set.c>
6332 #include <isl_multi_intersect.c>
6334 /* Does "mpa" have a non-trivial explicit domain?
6336 * The explicit domain, if present, is trivial if it represents
6337 * an (obviously) universe set.
6339 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6340 __isl_keep isl_multi_pw_aff *mpa)
6342 if (!mpa)
6343 return isl_bool_error;
6344 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6345 return isl_bool_false;
6346 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6349 /* Scale the elements of "pma" by the corresponding elements of "mv".
6351 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6352 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6354 int i;
6355 isl_bool equal_params;
6357 pma = isl_pw_multi_aff_cow(pma);
6358 if (!pma || !mv)
6359 goto error;
6360 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6361 mv->space, isl_dim_set))
6362 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6363 "spaces don't match", goto error);
6364 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6365 if (equal_params < 0)
6366 goto error;
6367 if (!equal_params) {
6368 pma = isl_pw_multi_aff_align_params(pma,
6369 isl_multi_val_get_space(mv));
6370 mv = isl_multi_val_align_params(mv,
6371 isl_pw_multi_aff_get_space(pma));
6372 if (!pma || !mv)
6373 goto error;
6376 for (i = 0; i < pma->n; ++i) {
6377 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6378 isl_multi_val_copy(mv));
6379 if (!pma->p[i].maff)
6380 goto error;
6383 isl_multi_val_free(mv);
6384 return pma;
6385 error:
6386 isl_multi_val_free(mv);
6387 isl_pw_multi_aff_free(pma);
6388 return NULL;
6391 /* This function is called for each entry of an isl_union_pw_multi_aff.
6392 * If the space of the entry matches that of data->mv,
6393 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6394 * Otherwise, return an empty isl_pw_multi_aff.
6396 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6397 __isl_take isl_pw_multi_aff *pma, void *user)
6399 isl_multi_val *mv = user;
6401 if (!pma)
6402 return NULL;
6403 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6404 mv->space, isl_dim_set)) {
6405 isl_space *space = isl_pw_multi_aff_get_space(pma);
6406 isl_pw_multi_aff_free(pma);
6407 return isl_pw_multi_aff_empty(space);
6410 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6413 /* Scale the elements of "upma" by the corresponding elements of "mv",
6414 * for those entries that match the space of "mv".
6416 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6417 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6419 upma = isl_union_pw_multi_aff_align_params(upma,
6420 isl_multi_val_get_space(mv));
6421 mv = isl_multi_val_align_params(mv,
6422 isl_union_pw_multi_aff_get_space(upma));
6423 if (!upma || !mv)
6424 goto error;
6426 return isl_union_pw_multi_aff_transform(upma,
6427 &union_pw_multi_aff_scale_multi_val_entry, mv);
6429 isl_multi_val_free(mv);
6430 return upma;
6431 error:
6432 isl_multi_val_free(mv);
6433 isl_union_pw_multi_aff_free(upma);
6434 return NULL;
6437 /* Construct and return a piecewise multi affine expression
6438 * in the given space with value zero in each of the output dimensions and
6439 * a universe domain.
6441 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6443 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6446 /* Construct and return a piecewise multi affine expression
6447 * that is equal to the given piecewise affine expression.
6449 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6450 __isl_take isl_pw_aff *pa)
6452 int i;
6453 isl_space *space;
6454 isl_pw_multi_aff *pma;
6456 if (!pa)
6457 return NULL;
6459 space = isl_pw_aff_get_space(pa);
6460 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6462 for (i = 0; i < pa->n; ++i) {
6463 isl_set *set;
6464 isl_multi_aff *ma;
6466 set = isl_set_copy(pa->p[i].set);
6467 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6468 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6471 isl_pw_aff_free(pa);
6472 return pma;
6475 /* Construct a set or map mapping the shared (parameter) domain
6476 * of the piecewise affine expressions to the range of "mpa"
6477 * with each dimension in the range equated to the
6478 * corresponding piecewise affine expression.
6480 static __isl_give isl_map *map_from_multi_pw_aff(
6481 __isl_take isl_multi_pw_aff *mpa)
6483 int i;
6484 isl_space *space;
6485 isl_map *map;
6487 if (!mpa)
6488 return NULL;
6490 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6491 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6492 "invalid space", goto error);
6494 space = isl_multi_pw_aff_get_domain_space(mpa);
6495 map = isl_map_universe(isl_space_from_domain(space));
6497 for (i = 0; i < mpa->n; ++i) {
6498 isl_pw_aff *pa;
6499 isl_map *map_i;
6501 pa = isl_pw_aff_copy(mpa->u.p[i]);
6502 map_i = map_from_pw_aff(pa);
6504 map = isl_map_flat_range_product(map, map_i);
6507 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6509 isl_multi_pw_aff_free(mpa);
6510 return map;
6511 error:
6512 isl_multi_pw_aff_free(mpa);
6513 return NULL;
6516 /* Construct a map mapping the shared domain
6517 * of the piecewise affine expressions to the range of "mpa"
6518 * with each dimension in the range equated to the
6519 * corresponding piecewise affine expression.
6521 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6523 if (!mpa)
6524 return NULL;
6525 if (isl_space_is_set(mpa->space))
6526 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6527 "space of input is not a map", goto error);
6529 return map_from_multi_pw_aff(mpa);
6530 error:
6531 isl_multi_pw_aff_free(mpa);
6532 return NULL;
6535 /* Construct a set mapping the shared parameter domain
6536 * of the piecewise affine expressions to the space of "mpa"
6537 * with each dimension in the range equated to the
6538 * corresponding piecewise affine expression.
6540 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6542 if (!mpa)
6543 return NULL;
6544 if (!isl_space_is_set(mpa->space))
6545 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6546 "space of input is not a set", goto error);
6548 return map_from_multi_pw_aff(mpa);
6549 error:
6550 isl_multi_pw_aff_free(mpa);
6551 return NULL;
6554 /* Construct and return a piecewise multi affine expression
6555 * that is equal to the given multi piecewise affine expression
6556 * on the shared domain of the piecewise affine expressions,
6557 * in the special case of a 0D multi piecewise affine expression.
6559 * Create a piecewise multi affine expression with the explicit domain of
6560 * the 0D multi piecewise affine expression as domain.
6562 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6563 __isl_take isl_multi_pw_aff *mpa)
6565 isl_space *space;
6566 isl_set *dom;
6567 isl_multi_aff *ma;
6569 space = isl_multi_pw_aff_get_space(mpa);
6570 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6571 isl_multi_pw_aff_free(mpa);
6573 ma = isl_multi_aff_zero(space);
6574 return isl_pw_multi_aff_alloc(dom, ma);
6577 /* Construct and return a piecewise multi affine expression
6578 * that is equal to the given multi piecewise affine expression
6579 * on the shared domain of the piecewise affine expressions.
6581 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6582 __isl_take isl_multi_pw_aff *mpa)
6584 int i;
6585 isl_space *space;
6586 isl_pw_aff *pa;
6587 isl_pw_multi_aff *pma;
6589 if (!mpa)
6590 return NULL;
6592 if (mpa->n == 0)
6593 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6595 space = isl_multi_pw_aff_get_space(mpa);
6596 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6597 pma = isl_pw_multi_aff_from_pw_aff(pa);
6599 for (i = 1; i < mpa->n; ++i) {
6600 isl_pw_multi_aff *pma_i;
6602 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6603 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6604 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6607 pma = isl_pw_multi_aff_reset_space(pma, space);
6609 isl_multi_pw_aff_free(mpa);
6610 return pma;
6613 /* Construct and return a multi piecewise affine expression
6614 * that is equal to the given multi affine expression.
6616 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6617 __isl_take isl_multi_aff *ma)
6619 int i, n;
6620 isl_multi_pw_aff *mpa;
6622 if (!ma)
6623 return NULL;
6625 n = isl_multi_aff_dim(ma, isl_dim_out);
6626 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6628 for (i = 0; i < n; ++i) {
6629 isl_pw_aff *pa;
6631 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6632 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6635 isl_multi_aff_free(ma);
6636 return mpa;
6639 /* Construct and return a multi piecewise affine expression
6640 * that is equal to the given piecewise multi affine expression.
6642 * If the resulting multi piecewise affine expression has
6643 * an explicit domain, then assign it the domain of the input.
6644 * In other cases, the domain is stored in the individual elements.
6646 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6647 __isl_take isl_pw_multi_aff *pma)
6649 int i, n;
6650 isl_space *space;
6651 isl_multi_pw_aff *mpa;
6653 if (!pma)
6654 return NULL;
6656 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6657 space = isl_pw_multi_aff_get_space(pma);
6658 mpa = isl_multi_pw_aff_alloc(space);
6660 for (i = 0; i < n; ++i) {
6661 isl_pw_aff *pa;
6663 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6664 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6666 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6667 isl_set *dom;
6669 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6670 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6673 isl_pw_multi_aff_free(pma);
6674 return mpa;
6677 /* Do "pa1" and "pa2" represent the same function?
6679 * We first check if they are obviously equal.
6680 * If not, we convert them to maps and check if those are equal.
6682 * If "pa1" or "pa2" contain any NaNs, then they are considered
6683 * not to be the same. A NaN is not equal to anything, not even
6684 * to another NaN.
6686 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6687 __isl_keep isl_pw_aff *pa2)
6689 isl_bool equal;
6690 isl_bool has_nan;
6691 isl_map *map1, *map2;
6693 if (!pa1 || !pa2)
6694 return isl_bool_error;
6696 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6697 if (equal < 0 || equal)
6698 return equal;
6699 has_nan = either_involves_nan(pa1, pa2);
6700 if (has_nan < 0)
6701 return isl_bool_error;
6702 if (has_nan)
6703 return isl_bool_false;
6705 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6706 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6707 equal = isl_map_is_equal(map1, map2);
6708 isl_map_free(map1);
6709 isl_map_free(map2);
6711 return equal;
6714 /* Do "mpa1" and "mpa2" represent the same function?
6716 * Note that we cannot convert the entire isl_multi_pw_aff
6717 * to a map because the domains of the piecewise affine expressions
6718 * may not be the same.
6720 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6721 __isl_keep isl_multi_pw_aff *mpa2)
6723 int i;
6724 isl_bool equal, equal_params;
6726 if (!mpa1 || !mpa2)
6727 return isl_bool_error;
6729 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6730 if (equal_params < 0)
6731 return isl_bool_error;
6732 if (!equal_params) {
6733 if (!isl_space_has_named_params(mpa1->space))
6734 return isl_bool_false;
6735 if (!isl_space_has_named_params(mpa2->space))
6736 return isl_bool_false;
6737 mpa1 = isl_multi_pw_aff_copy(mpa1);
6738 mpa2 = isl_multi_pw_aff_copy(mpa2);
6739 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6740 isl_multi_pw_aff_get_space(mpa2));
6741 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6742 isl_multi_pw_aff_get_space(mpa1));
6743 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6744 isl_multi_pw_aff_free(mpa1);
6745 isl_multi_pw_aff_free(mpa2);
6746 return equal;
6749 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6750 if (equal < 0 || !equal)
6751 return equal;
6753 for (i = 0; i < mpa1->n; ++i) {
6754 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6755 if (equal < 0 || !equal)
6756 return equal;
6759 return isl_bool_true;
6762 /* Do "pma1" and "pma2" represent the same function?
6764 * First check if they are obviously equal.
6765 * If not, then convert them to maps and check if those are equal.
6767 * If "pa1" or "pa2" contain any NaNs, then they are considered
6768 * not to be the same. A NaN is not equal to anything, not even
6769 * to another NaN.
6771 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6772 __isl_keep isl_pw_multi_aff *pma2)
6774 isl_bool equal;
6775 isl_bool has_nan;
6776 isl_map *map1, *map2;
6778 if (!pma1 || !pma2)
6779 return isl_bool_error;
6781 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6782 if (equal < 0 || equal)
6783 return equal;
6784 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6785 if (has_nan >= 0 && !has_nan)
6786 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6787 if (has_nan < 0 || has_nan)
6788 return isl_bool_not(has_nan);
6790 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6791 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6792 equal = isl_map_is_equal(map1, map2);
6793 isl_map_free(map1);
6794 isl_map_free(map2);
6796 return equal;
6799 /* Compute the pullback of "mpa" by the function represented by "ma".
6800 * In other words, plug in "ma" in "mpa".
6802 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6804 * If "mpa" has an explicit domain, then it is this domain
6805 * that needs to undergo a pullback, i.e., a preimage.
6807 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6808 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6810 int i;
6811 isl_space *space = NULL;
6813 mpa = isl_multi_pw_aff_cow(mpa);
6814 if (!mpa || !ma)
6815 goto error;
6817 space = isl_space_join(isl_multi_aff_get_space(ma),
6818 isl_multi_pw_aff_get_space(mpa));
6819 if (!space)
6820 goto error;
6822 for (i = 0; i < mpa->n; ++i) {
6823 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6824 isl_multi_aff_copy(ma));
6825 if (!mpa->u.p[i])
6826 goto error;
6828 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6829 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6830 isl_multi_aff_copy(ma));
6831 if (!mpa->u.dom)
6832 goto error;
6835 isl_multi_aff_free(ma);
6836 isl_space_free(mpa->space);
6837 mpa->space = space;
6838 return mpa;
6839 error:
6840 isl_space_free(space);
6841 isl_multi_pw_aff_free(mpa);
6842 isl_multi_aff_free(ma);
6843 return NULL;
6846 /* Compute the pullback of "mpa" by the function represented by "ma".
6847 * In other words, plug in "ma" in "mpa".
6849 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6850 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6852 isl_bool equal_params;
6854 if (!mpa || !ma)
6855 goto error;
6856 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6857 if (equal_params < 0)
6858 goto error;
6859 if (equal_params)
6860 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6861 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6862 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6863 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6864 error:
6865 isl_multi_pw_aff_free(mpa);
6866 isl_multi_aff_free(ma);
6867 return NULL;
6870 /* Compute the pullback of "mpa" by the function represented by "pma".
6871 * In other words, plug in "pma" in "mpa".
6873 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6875 * If "mpa" has an explicit domain, then it is this domain
6876 * that needs to undergo a pullback, i.e., a preimage.
6878 static __isl_give isl_multi_pw_aff *
6879 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6880 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6882 int i;
6883 isl_space *space = NULL;
6885 mpa = isl_multi_pw_aff_cow(mpa);
6886 if (!mpa || !pma)
6887 goto error;
6889 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6890 isl_multi_pw_aff_get_space(mpa));
6892 for (i = 0; i < mpa->n; ++i) {
6893 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6894 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6895 if (!mpa->u.p[i])
6896 goto error;
6898 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6899 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6900 isl_pw_multi_aff_copy(pma));
6901 if (!mpa->u.dom)
6902 goto error;
6905 isl_pw_multi_aff_free(pma);
6906 isl_space_free(mpa->space);
6907 mpa->space = space;
6908 return mpa;
6909 error:
6910 isl_space_free(space);
6911 isl_multi_pw_aff_free(mpa);
6912 isl_pw_multi_aff_free(pma);
6913 return NULL;
6916 /* Compute the pullback of "mpa" by the function represented by "pma".
6917 * In other words, plug in "pma" in "mpa".
6919 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6920 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6922 isl_bool equal_params;
6924 if (!mpa || !pma)
6925 goto error;
6926 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6927 if (equal_params < 0)
6928 goto error;
6929 if (equal_params)
6930 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6931 mpa = isl_multi_pw_aff_align_params(mpa,
6932 isl_pw_multi_aff_get_space(pma));
6933 pma = isl_pw_multi_aff_align_params(pma,
6934 isl_multi_pw_aff_get_space(mpa));
6935 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6936 error:
6937 isl_multi_pw_aff_free(mpa);
6938 isl_pw_multi_aff_free(pma);
6939 return NULL;
6942 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6943 * with the domain of "aff". The domain of the result is the same
6944 * as that of "mpa".
6945 * "mpa" and "aff" are assumed to have been aligned.
6947 * We first extract the parametric constant from "aff", defined
6948 * over the correct domain.
6949 * Then we add the appropriate combinations of the members of "mpa".
6950 * Finally, we add the integer divisions through recursive calls.
6952 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6953 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6955 int i, n_in, n_div;
6956 isl_space *space;
6957 isl_val *v;
6958 isl_pw_aff *pa;
6959 isl_aff *tmp;
6961 n_in = isl_aff_dim(aff, isl_dim_in);
6962 n_div = isl_aff_dim(aff, isl_dim_div);
6964 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6965 tmp = isl_aff_copy(aff);
6966 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6967 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6968 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6969 isl_space_dim(space, isl_dim_set));
6970 tmp = isl_aff_reset_domain_space(tmp, space);
6971 pa = isl_pw_aff_from_aff(tmp);
6973 for (i = 0; i < n_in; ++i) {
6974 isl_pw_aff *pa_i;
6976 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6977 continue;
6978 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6979 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6980 pa_i = isl_pw_aff_scale_val(pa_i, v);
6981 pa = isl_pw_aff_add(pa, pa_i);
6984 for (i = 0; i < n_div; ++i) {
6985 isl_aff *div;
6986 isl_pw_aff *pa_i;
6988 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6989 continue;
6990 div = isl_aff_get_div(aff, i);
6991 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6992 isl_multi_pw_aff_copy(mpa), div);
6993 pa_i = isl_pw_aff_floor(pa_i);
6994 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6995 pa_i = isl_pw_aff_scale_val(pa_i, v);
6996 pa = isl_pw_aff_add(pa, pa_i);
6999 isl_multi_pw_aff_free(mpa);
7000 isl_aff_free(aff);
7002 return pa;
7005 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7006 * with the domain of "aff". The domain of the result is the same
7007 * as that of "mpa".
7009 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7010 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7012 isl_bool equal_params;
7014 if (!aff || !mpa)
7015 goto error;
7016 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7017 if (equal_params < 0)
7018 goto error;
7019 if (equal_params)
7020 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7022 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7023 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7025 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7026 error:
7027 isl_aff_free(aff);
7028 isl_multi_pw_aff_free(mpa);
7029 return NULL;
7032 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7033 * with the domain of "pa". The domain of the result is the same
7034 * as that of "mpa".
7035 * "mpa" and "pa" are assumed to have been aligned.
7037 * We consider each piece in turn. Note that the domains of the
7038 * pieces are assumed to be disjoint and they remain disjoint
7039 * after taking the preimage (over the same function).
7041 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7042 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7044 isl_space *space;
7045 isl_pw_aff *res;
7046 int i;
7048 if (!mpa || !pa)
7049 goto error;
7051 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7052 isl_pw_aff_get_space(pa));
7053 res = isl_pw_aff_empty(space);
7055 for (i = 0; i < pa->n; ++i) {
7056 isl_pw_aff *pa_i;
7057 isl_set *domain;
7059 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7060 isl_multi_pw_aff_copy(mpa),
7061 isl_aff_copy(pa->p[i].aff));
7062 domain = isl_set_copy(pa->p[i].set);
7063 domain = isl_set_preimage_multi_pw_aff(domain,
7064 isl_multi_pw_aff_copy(mpa));
7065 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7066 res = isl_pw_aff_add_disjoint(res, pa_i);
7069 isl_pw_aff_free(pa);
7070 isl_multi_pw_aff_free(mpa);
7071 return res;
7072 error:
7073 isl_pw_aff_free(pa);
7074 isl_multi_pw_aff_free(mpa);
7075 return NULL;
7078 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7079 * with the domain of "pa". The domain of the result is the same
7080 * as that of "mpa".
7082 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7083 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7085 isl_bool equal_params;
7087 if (!pa || !mpa)
7088 goto error;
7089 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7090 if (equal_params < 0)
7091 goto error;
7092 if (equal_params)
7093 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7095 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7096 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7098 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7099 error:
7100 isl_pw_aff_free(pa);
7101 isl_multi_pw_aff_free(mpa);
7102 return NULL;
7105 /* Compute the pullback of "pa" by the function represented by "mpa".
7106 * In other words, plug in "mpa" in "pa".
7107 * "pa" and "mpa" are assumed to have been aligned.
7109 * The pullback is computed by applying "pa" to "mpa".
7111 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7112 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7114 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7117 /* Compute the pullback of "pa" by the function represented by "mpa".
7118 * In other words, plug in "mpa" in "pa".
7120 * The pullback is computed by applying "pa" to "mpa".
7122 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7123 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7125 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7128 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7129 * In other words, plug in "mpa2" in "mpa1".
7131 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7133 * We pullback each member of "mpa1" in turn.
7135 * If "mpa1" has an explicit domain, then it is this domain
7136 * that needs to undergo a pullback instead, i.e., a preimage.
7138 static __isl_give isl_multi_pw_aff *
7139 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7140 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7142 int i;
7143 isl_space *space = NULL;
7145 mpa1 = isl_multi_pw_aff_cow(mpa1);
7146 if (!mpa1 || !mpa2)
7147 goto error;
7149 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7150 isl_multi_pw_aff_get_space(mpa1));
7152 for (i = 0; i < mpa1->n; ++i) {
7153 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7154 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7155 if (!mpa1->u.p[i])
7156 goto error;
7159 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7160 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7161 isl_multi_pw_aff_copy(mpa2));
7162 if (!mpa1->u.dom)
7163 goto error;
7165 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7167 isl_multi_pw_aff_free(mpa2);
7168 return mpa1;
7169 error:
7170 isl_space_free(space);
7171 isl_multi_pw_aff_free(mpa1);
7172 isl_multi_pw_aff_free(mpa2);
7173 return NULL;
7176 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7177 * In other words, plug in "mpa2" in "mpa1".
7179 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7180 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7182 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7183 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7186 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7187 * of "mpa1" and "mpa2" live in the same space, construct map space
7188 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7189 * with this map space as extract argument.
7191 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7192 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7193 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7194 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7196 int match;
7197 isl_space *space1, *space2;
7198 isl_map *res;
7200 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7201 isl_multi_pw_aff_get_space(mpa2));
7202 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7203 isl_multi_pw_aff_get_space(mpa1));
7204 if (!mpa1 || !mpa2)
7205 goto error;
7206 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7207 mpa2->space, isl_dim_out);
7208 if (match < 0)
7209 goto error;
7210 if (!match)
7211 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7212 "range spaces don't match", goto error);
7213 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7214 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7215 space1 = isl_space_map_from_domain_and_range(space1, space2);
7217 res = order(mpa1, mpa2, space1);
7218 isl_multi_pw_aff_free(mpa1);
7219 isl_multi_pw_aff_free(mpa2);
7220 return res;
7221 error:
7222 isl_multi_pw_aff_free(mpa1);
7223 isl_multi_pw_aff_free(mpa2);
7224 return NULL;
7227 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7228 * where the function values are equal. "space" is the space of the result.
7229 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7231 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7232 * in the sequences are equal.
7234 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7235 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7236 __isl_take isl_space *space)
7238 int i, n;
7239 isl_map *res;
7241 res = isl_map_universe(space);
7243 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7244 for (i = 0; i < n; ++i) {
7245 isl_pw_aff *pa1, *pa2;
7246 isl_map *map;
7248 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7249 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7250 map = isl_pw_aff_eq_map(pa1, pa2);
7251 res = isl_map_intersect(res, map);
7254 return res;
7257 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7258 * where the function values are equal.
7260 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7261 __isl_take isl_multi_pw_aff *mpa2)
7263 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7264 &isl_multi_pw_aff_eq_map_on_space);
7267 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7268 * where the function values of "mpa1" is lexicographically satisfies "base"
7269 * compared to that of "mpa2". "space" is the space of the result.
7270 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7272 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7273 * if its i-th element satisfies "base" when compared to
7274 * the i-th element of "mpa2" while all previous elements are
7275 * pairwise equal.
7277 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7278 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7279 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7280 __isl_take isl_pw_aff *pa2),
7281 __isl_take isl_space *space)
7283 int i, n;
7284 isl_map *res, *rest;
7286 res = isl_map_empty(isl_space_copy(space));
7287 rest = isl_map_universe(space);
7289 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7290 for (i = 0; i < n; ++i) {
7291 isl_pw_aff *pa1, *pa2;
7292 isl_map *map;
7294 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7295 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7296 map = base(pa1, pa2);
7297 map = isl_map_intersect(map, isl_map_copy(rest));
7298 res = isl_map_union(res, map);
7300 if (i == n - 1)
7301 continue;
7303 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7304 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7305 map = isl_pw_aff_eq_map(pa1, pa2);
7306 rest = isl_map_intersect(rest, map);
7309 isl_map_free(rest);
7310 return res;
7313 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7314 * where the function value of "mpa1" is lexicographically less than that
7315 * of "mpa2". "space" is the space of the result.
7316 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7318 * "mpa1" is less than "mpa2" if its i-th element is smaller
7319 * than the i-th element of "mpa2" while all previous elements are
7320 * pairwise equal.
7322 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7323 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7324 __isl_take isl_space *space)
7326 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7327 &isl_pw_aff_lt_map, space);
7330 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7331 * where the function value of "mpa1" is lexicographically less than that
7332 * of "mpa2".
7334 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7335 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7337 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7338 &isl_multi_pw_aff_lex_lt_map_on_space);
7341 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7342 * where the function value of "mpa1" is lexicographically greater than that
7343 * of "mpa2". "space" is the space of the result.
7344 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7346 * "mpa1" is greater than "mpa2" if its i-th element is greater
7347 * than the i-th element of "mpa2" while all previous elements are
7348 * pairwise equal.
7350 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7351 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7352 __isl_take isl_space *space)
7354 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7355 &isl_pw_aff_gt_map, space);
7358 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7359 * where the function value of "mpa1" is lexicographically greater than that
7360 * of "mpa2".
7362 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7363 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7365 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7366 &isl_multi_pw_aff_lex_gt_map_on_space);
7369 /* Compare two isl_affs.
7371 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7372 * than "aff2" and 0 if they are equal.
7374 * The order is fairly arbitrary. We do consider expressions that only involve
7375 * earlier dimensions as "smaller".
7377 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7379 int cmp;
7380 int last1, last2;
7382 if (aff1 == aff2)
7383 return 0;
7385 if (!aff1)
7386 return -1;
7387 if (!aff2)
7388 return 1;
7390 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7391 if (cmp != 0)
7392 return cmp;
7394 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7395 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7396 if (last1 != last2)
7397 return last1 - last2;
7399 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7402 /* Compare two isl_pw_affs.
7404 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7405 * than "pa2" and 0 if they are equal.
7407 * The order is fairly arbitrary. We do consider expressions that only involve
7408 * earlier dimensions as "smaller".
7410 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7411 __isl_keep isl_pw_aff *pa2)
7413 int i;
7414 int cmp;
7416 if (pa1 == pa2)
7417 return 0;
7419 if (!pa1)
7420 return -1;
7421 if (!pa2)
7422 return 1;
7424 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7425 if (cmp != 0)
7426 return cmp;
7428 if (pa1->n != pa2->n)
7429 return pa1->n - pa2->n;
7431 for (i = 0; i < pa1->n; ++i) {
7432 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7433 if (cmp != 0)
7434 return cmp;
7435 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7436 if (cmp != 0)
7437 return cmp;
7440 return 0;
7443 /* Return a piecewise affine expression that is equal to "v" on "domain".
7445 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7446 __isl_take isl_val *v)
7448 isl_space *space;
7449 isl_local_space *ls;
7450 isl_aff *aff;
7452 space = isl_set_get_space(domain);
7453 ls = isl_local_space_from_space(space);
7454 aff = isl_aff_val_on_domain(ls, v);
7456 return isl_pw_aff_alloc(domain, aff);
7459 /* Return a multi affine expression that is equal to "mv" on domain
7460 * space "space".
7462 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7463 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7465 int i, n;
7466 isl_space *space2;
7467 isl_local_space *ls;
7468 isl_multi_aff *ma;
7470 if (!space || !mv)
7471 goto error;
7473 n = isl_multi_val_dim(mv, isl_dim_set);
7474 space2 = isl_multi_val_get_space(mv);
7475 space2 = isl_space_align_params(space2, isl_space_copy(space));
7476 space = isl_space_align_params(space, isl_space_copy(space2));
7477 space = isl_space_map_from_domain_and_range(space, space2);
7478 ma = isl_multi_aff_alloc(isl_space_copy(space));
7479 ls = isl_local_space_from_space(isl_space_domain(space));
7480 for (i = 0; i < n; ++i) {
7481 isl_val *v;
7482 isl_aff *aff;
7484 v = isl_multi_val_get_val(mv, i);
7485 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7486 ma = isl_multi_aff_set_aff(ma, i, aff);
7488 isl_local_space_free(ls);
7490 isl_multi_val_free(mv);
7491 return ma;
7492 error:
7493 isl_space_free(space);
7494 isl_multi_val_free(mv);
7495 return NULL;
7498 /* Return a piecewise multi-affine expression
7499 * that is equal to "mv" on "domain".
7501 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7502 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7504 isl_space *space;
7505 isl_multi_aff *ma;
7507 space = isl_set_get_space(domain);
7508 ma = isl_multi_aff_multi_val_on_space(space, mv);
7510 return isl_pw_multi_aff_alloc(domain, ma);
7513 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7514 * mv is the value that should be attained on each domain set
7515 * res collects the results
7517 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7518 isl_multi_val *mv;
7519 isl_union_pw_multi_aff *res;
7522 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7523 * and add it to data->res.
7525 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7526 void *user)
7528 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7529 isl_pw_multi_aff *pma;
7530 isl_multi_val *mv;
7532 mv = isl_multi_val_copy(data->mv);
7533 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7534 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7536 return data->res ? isl_stat_ok : isl_stat_error;
7539 /* Return a union piecewise multi-affine expression
7540 * that is equal to "mv" on "domain".
7542 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7543 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7545 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7546 isl_space *space;
7548 space = isl_union_set_get_space(domain);
7549 data.res = isl_union_pw_multi_aff_empty(space);
7550 data.mv = mv;
7551 if (isl_union_set_foreach_set(domain,
7552 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7553 data.res = isl_union_pw_multi_aff_free(data.res);
7554 isl_union_set_free(domain);
7555 isl_multi_val_free(mv);
7556 return data.res;
7559 /* Compute the pullback of data->pma by the function represented by "pma2",
7560 * provided the spaces match, and add the results to data->res.
7562 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7564 struct isl_union_pw_multi_aff_bin_data *data = user;
7566 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7567 pma2->dim, isl_dim_out)) {
7568 isl_pw_multi_aff_free(pma2);
7569 return isl_stat_ok;
7572 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7573 isl_pw_multi_aff_copy(data->pma), pma2);
7575 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7576 if (!data->res)
7577 return isl_stat_error;
7579 return isl_stat_ok;
7582 /* Compute the pullback of "upma1" by the function represented by "upma2".
7584 __isl_give isl_union_pw_multi_aff *
7585 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7586 __isl_take isl_union_pw_multi_aff *upma1,
7587 __isl_take isl_union_pw_multi_aff *upma2)
7589 return bin_op(upma1, upma2, &pullback_entry);
7592 /* Check that the domain space of "upa" matches "space".
7594 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7595 * can in principle never fail since the space "space" is that
7596 * of the isl_multi_union_pw_aff and is a set space such that
7597 * there is no domain space to match.
7599 * We check the parameters and double-check that "space" is
7600 * indeed that of a set.
7602 static isl_stat isl_union_pw_aff_check_match_domain_space(
7603 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7605 isl_space *upa_space;
7606 isl_bool match;
7608 if (!upa || !space)
7609 return isl_stat_error;
7611 match = isl_space_is_set(space);
7612 if (match < 0)
7613 return isl_stat_error;
7614 if (!match)
7615 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7616 "expecting set space", return isl_stat_error);
7618 upa_space = isl_union_pw_aff_get_space(upa);
7619 match = isl_space_has_equal_params(space, upa_space);
7620 if (match < 0)
7621 goto error;
7622 if (!match)
7623 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7624 "parameters don't match", goto error);
7626 isl_space_free(upa_space);
7627 return isl_stat_ok;
7628 error:
7629 isl_space_free(upa_space);
7630 return isl_stat_error;
7633 /* Do the parameters of "upa" match those of "space"?
7635 static isl_bool isl_union_pw_aff_matching_params(
7636 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7638 isl_space *upa_space;
7639 isl_bool match;
7641 if (!upa || !space)
7642 return isl_bool_error;
7644 upa_space = isl_union_pw_aff_get_space(upa);
7646 match = isl_space_has_equal_params(space, upa_space);
7648 isl_space_free(upa_space);
7649 return match;
7652 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7653 * space represents the new parameters.
7654 * res collects the results.
7656 struct isl_union_pw_aff_reset_params_data {
7657 isl_space *space;
7658 isl_union_pw_aff *res;
7661 /* Replace the parameters of "pa" by data->space and
7662 * add the result to data->res.
7664 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7666 struct isl_union_pw_aff_reset_params_data *data = user;
7667 isl_space *space;
7669 space = isl_pw_aff_get_space(pa);
7670 space = isl_space_replace_params(space, data->space);
7671 pa = isl_pw_aff_reset_space(pa, space);
7672 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7674 return data->res ? isl_stat_ok : isl_stat_error;
7677 /* Replace the domain space of "upa" by "space".
7678 * Since a union expression does not have a (single) domain space,
7679 * "space" is necessarily a parameter space.
7681 * Since the order and the names of the parameters determine
7682 * the hash value, we need to create a new hash table.
7684 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7685 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7687 struct isl_union_pw_aff_reset_params_data data = { space };
7688 isl_bool match;
7690 match = isl_union_pw_aff_matching_params(upa, space);
7691 if (match < 0)
7692 upa = isl_union_pw_aff_free(upa);
7693 else if (match) {
7694 isl_space_free(space);
7695 return upa;
7698 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7699 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7700 data.res = isl_union_pw_aff_free(data.res);
7702 isl_union_pw_aff_free(upa);
7703 isl_space_free(space);
7704 return data.res;
7707 /* Return the floor of "pa".
7709 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7711 return isl_pw_aff_floor(pa);
7714 /* Given f, return floor(f).
7716 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7717 __isl_take isl_union_pw_aff *upa)
7719 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7722 /* Compute
7724 * upa mod m = upa - m * floor(upa/m)
7726 * with m an integer value.
7728 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7729 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7731 isl_union_pw_aff *res;
7733 if (!upa || !m)
7734 goto error;
7736 if (!isl_val_is_int(m))
7737 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7738 "expecting integer modulo", goto error);
7739 if (!isl_val_is_pos(m))
7740 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7741 "expecting positive modulo", goto error);
7743 res = isl_union_pw_aff_copy(upa);
7744 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7745 upa = isl_union_pw_aff_floor(upa);
7746 upa = isl_union_pw_aff_scale_val(upa, m);
7747 res = isl_union_pw_aff_sub(res, upa);
7749 return res;
7750 error:
7751 isl_val_free(m);
7752 isl_union_pw_aff_free(upa);
7753 return NULL;
7756 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7757 * pos is the output position that needs to be extracted.
7758 * res collects the results.
7760 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7761 int pos;
7762 isl_union_pw_aff *res;
7765 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7766 * (assuming it has such a dimension) and add it to data->res.
7768 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7770 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7771 int n_out;
7772 isl_pw_aff *pa;
7774 if (!pma)
7775 return isl_stat_error;
7777 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7778 if (data->pos >= n_out) {
7779 isl_pw_multi_aff_free(pma);
7780 return isl_stat_ok;
7783 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7784 isl_pw_multi_aff_free(pma);
7786 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7788 return data->res ? isl_stat_ok : isl_stat_error;
7791 /* Extract an isl_union_pw_aff corresponding to
7792 * output dimension "pos" of "upma".
7794 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7795 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7797 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7798 isl_space *space;
7800 if (!upma)
7801 return NULL;
7803 if (pos < 0)
7804 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7805 "cannot extract at negative position", return NULL);
7807 space = isl_union_pw_multi_aff_get_space(upma);
7808 data.res = isl_union_pw_aff_empty(space);
7809 data.pos = pos;
7810 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7811 &get_union_pw_aff, &data) < 0)
7812 data.res = isl_union_pw_aff_free(data.res);
7814 return data.res;
7817 /* Return a union piecewise affine expression
7818 * that is equal to "aff" on "domain".
7820 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7821 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7823 isl_pw_aff *pa;
7825 pa = isl_pw_aff_from_aff(aff);
7826 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7829 /* Return a union piecewise affine expression
7830 * that is equal to the parameter identified by "id" on "domain".
7832 * Make sure the parameter appears in the space passed to
7833 * isl_aff_param_on_domain_space_id.
7835 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7836 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7838 isl_space *space;
7839 isl_aff *aff;
7841 space = isl_union_set_get_space(domain);
7842 space = isl_space_add_param_id(space, isl_id_copy(id));
7843 aff = isl_aff_param_on_domain_space_id(space, id);
7844 return isl_union_pw_aff_aff_on_domain(domain, aff);
7847 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7848 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7849 * needs to attain.
7850 * "res" collects the results.
7852 struct isl_union_pw_aff_pw_aff_on_domain_data {
7853 isl_pw_aff *pa;
7854 isl_union_pw_aff *res;
7857 /* Construct a piecewise affine expression that is equal to data->pa
7858 * on "domain" and add the result to data->res.
7860 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7862 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7863 isl_pw_aff *pa;
7864 int dim;
7866 pa = isl_pw_aff_copy(data->pa);
7867 dim = isl_set_dim(domain, isl_dim_set);
7868 pa = isl_pw_aff_from_range(pa);
7869 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7870 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7871 pa = isl_pw_aff_intersect_domain(pa, domain);
7872 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7874 return data->res ? isl_stat_ok : isl_stat_error;
7877 /* Return a union piecewise affine expression
7878 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7879 * have been aligned.
7881 * Construct an isl_pw_aff on each of the sets in "domain" and
7882 * collect the results.
7884 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7885 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7887 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7888 isl_space *space;
7890 space = isl_union_set_get_space(domain);
7891 data.res = isl_union_pw_aff_empty(space);
7892 data.pa = pa;
7893 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7894 data.res = isl_union_pw_aff_free(data.res);
7895 isl_union_set_free(domain);
7896 isl_pw_aff_free(pa);
7897 return data.res;
7900 /* Return a union piecewise affine expression
7901 * that is equal to "pa" on "domain".
7903 * Check that "pa" is a parametric expression,
7904 * align the parameters if needed and call
7905 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7907 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7908 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7910 isl_bool is_set;
7911 isl_bool equal_params;
7912 isl_space *domain_space, *pa_space;
7914 pa_space = isl_pw_aff_peek_space(pa);
7915 is_set = isl_space_is_set(pa_space);
7916 if (is_set < 0)
7917 goto error;
7918 if (!is_set)
7919 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7920 "expecting parametric expression", goto error);
7922 domain_space = isl_union_set_get_space(domain);
7923 pa_space = isl_pw_aff_get_space(pa);
7924 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7925 if (equal_params >= 0 && !equal_params) {
7926 isl_space *space;
7928 space = isl_space_align_params(domain_space, pa_space);
7929 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7930 domain = isl_union_set_align_params(domain, space);
7931 } else {
7932 isl_space_free(domain_space);
7933 isl_space_free(pa_space);
7936 if (equal_params < 0)
7937 goto error;
7938 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7939 error:
7940 isl_union_set_free(domain);
7941 isl_pw_aff_free(pa);
7942 return NULL;
7945 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7946 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7947 * "res" collects the results.
7949 struct isl_union_pw_aff_val_on_domain_data {
7950 isl_val *v;
7951 isl_union_pw_aff *res;
7954 /* Construct a piecewise affine expression that is equal to data->v
7955 * on "domain" and add the result to data->res.
7957 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7959 struct isl_union_pw_aff_val_on_domain_data *data = user;
7960 isl_pw_aff *pa;
7961 isl_val *v;
7963 v = isl_val_copy(data->v);
7964 pa = isl_pw_aff_val_on_domain(domain, v);
7965 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7967 return data->res ? isl_stat_ok : isl_stat_error;
7970 /* Return a union piecewise affine expression
7971 * that is equal to "v" on "domain".
7973 * Construct an isl_pw_aff on each of the sets in "domain" and
7974 * collect the results.
7976 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7977 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7979 struct isl_union_pw_aff_val_on_domain_data data;
7980 isl_space *space;
7982 space = isl_union_set_get_space(domain);
7983 data.res = isl_union_pw_aff_empty(space);
7984 data.v = v;
7985 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7986 data.res = isl_union_pw_aff_free(data.res);
7987 isl_union_set_free(domain);
7988 isl_val_free(v);
7989 return data.res;
7992 /* Construct a piecewise multi affine expression
7993 * that is equal to "pa" and add it to upma.
7995 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7996 void *user)
7998 isl_union_pw_multi_aff **upma = user;
7999 isl_pw_multi_aff *pma;
8001 pma = isl_pw_multi_aff_from_pw_aff(pa);
8002 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8004 return *upma ? isl_stat_ok : isl_stat_error;
8007 /* Construct and return a union piecewise multi affine expression
8008 * that is equal to the given union piecewise affine expression.
8010 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8011 __isl_take isl_union_pw_aff *upa)
8013 isl_space *space;
8014 isl_union_pw_multi_aff *upma;
8016 if (!upa)
8017 return NULL;
8019 space = isl_union_pw_aff_get_space(upa);
8020 upma = isl_union_pw_multi_aff_empty(space);
8022 if (isl_union_pw_aff_foreach_pw_aff(upa,
8023 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8024 upma = isl_union_pw_multi_aff_free(upma);
8026 isl_union_pw_aff_free(upa);
8027 return upma;
8030 /* Compute the set of elements in the domain of "pa" where it is zero and
8031 * add this set to "uset".
8033 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8035 isl_union_set **uset = (isl_union_set **)user;
8037 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8039 return *uset ? isl_stat_ok : isl_stat_error;
8042 /* Return a union set containing those elements in the domain
8043 * of "upa" where it is zero.
8045 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8046 __isl_take isl_union_pw_aff *upa)
8048 isl_union_set *zero;
8050 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8051 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8052 zero = isl_union_set_free(zero);
8054 isl_union_pw_aff_free(upa);
8055 return zero;
8058 /* Convert "pa" to an isl_map and add it to *umap.
8060 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
8062 isl_union_map **umap = user;
8063 isl_map *map;
8065 map = isl_map_from_pw_aff(pa);
8066 *umap = isl_union_map_add_map(*umap, map);
8068 return *umap ? isl_stat_ok : isl_stat_error;
8071 /* Construct a union map mapping the domain of the union
8072 * piecewise affine expression to its range, with the single output dimension
8073 * equated to the corresponding affine expressions on their cells.
8075 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
8076 __isl_take isl_union_pw_aff *upa)
8078 isl_space *space;
8079 isl_union_map *umap;
8081 if (!upa)
8082 return NULL;
8084 space = isl_union_pw_aff_get_space(upa);
8085 umap = isl_union_map_empty(space);
8087 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
8088 &umap) < 0)
8089 umap = isl_union_map_free(umap);
8091 isl_union_pw_aff_free(upa);
8092 return umap;
8095 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8096 * upma is the function that is plugged in.
8097 * pa is the current part of the function in which upma is plugged in.
8098 * res collects the results.
8100 struct isl_union_pw_aff_pullback_upma_data {
8101 isl_union_pw_multi_aff *upma;
8102 isl_pw_aff *pa;
8103 isl_union_pw_aff *res;
8106 /* Check if "pma" can be plugged into data->pa.
8107 * If so, perform the pullback and add the result to data->res.
8109 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8111 struct isl_union_pw_aff_pullback_upma_data *data = user;
8112 isl_pw_aff *pa;
8114 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8115 pma->dim, isl_dim_out)) {
8116 isl_pw_multi_aff_free(pma);
8117 return isl_stat_ok;
8120 pa = isl_pw_aff_copy(data->pa);
8121 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8123 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8125 return data->res ? isl_stat_ok : isl_stat_error;
8128 /* Check if any of the elements of data->upma can be plugged into pa,
8129 * add if so add the result to data->res.
8131 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8133 struct isl_union_pw_aff_pullback_upma_data *data = user;
8134 isl_stat r;
8136 data->pa = pa;
8137 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8138 &pa_pb_pma, data);
8139 isl_pw_aff_free(pa);
8141 return r;
8144 /* Compute the pullback of "upa" by the function represented by "upma".
8145 * In other words, plug in "upma" in "upa". The result contains
8146 * expressions defined over the domain space of "upma".
8148 * Run over all pairs of elements in "upa" and "upma", perform
8149 * the pullback when appropriate and collect the results.
8150 * If the hash value were based on the domain space rather than
8151 * the function space, then we could run through all elements
8152 * of "upma" and directly pick out the corresponding element of "upa".
8154 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8155 __isl_take isl_union_pw_aff *upa,
8156 __isl_take isl_union_pw_multi_aff *upma)
8158 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8159 isl_space *space;
8161 space = isl_union_pw_multi_aff_get_space(upma);
8162 upa = isl_union_pw_aff_align_params(upa, space);
8163 space = isl_union_pw_aff_get_space(upa);
8164 upma = isl_union_pw_multi_aff_align_params(upma, space);
8166 if (!upa || !upma)
8167 goto error;
8169 data.upma = upma;
8170 data.res = isl_union_pw_aff_alloc_same_size(upa);
8171 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8172 data.res = isl_union_pw_aff_free(data.res);
8174 isl_union_pw_aff_free(upa);
8175 isl_union_pw_multi_aff_free(upma);
8176 return data.res;
8177 error:
8178 isl_union_pw_aff_free(upa);
8179 isl_union_pw_multi_aff_free(upma);
8180 return NULL;
8183 #undef BASE
8184 #define BASE union_pw_aff
8185 #undef DOMBASE
8186 #define DOMBASE union_set
8188 #define NO_MOVE_DIMS
8189 #define NO_DOMAIN
8190 #define NO_PRODUCT
8191 #define NO_SPLICE
8192 #define NO_ZERO
8193 #define NO_IDENTITY
8195 #include <isl_multi_explicit_domain.c>
8196 #include <isl_multi_union_pw_aff_explicit_domain.c>
8197 #include <isl_multi_templ.c>
8198 #include <isl_multi_apply_set.c>
8199 #include <isl_multi_apply_union_set.c>
8200 #include <isl_multi_coalesce.c>
8201 #include <isl_multi_floor.c>
8202 #include <isl_multi_gist.c>
8203 #include <isl_multi_align_set.c>
8204 #include <isl_multi_align_union_set.c>
8205 #include <isl_multi_intersect.c>
8207 /* Does "mupa" have a non-trivial explicit domain?
8209 * The explicit domain, if present, is trivial if it represents
8210 * an (obviously) universe parameter set.
8212 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8213 __isl_keep isl_multi_union_pw_aff *mupa)
8215 isl_bool is_params, trivial;
8216 isl_set *set;
8218 if (!mupa)
8219 return isl_bool_error;
8220 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8221 return isl_bool_false;
8222 is_params = isl_union_set_is_params(mupa->u.dom);
8223 if (is_params < 0 || !is_params)
8224 return isl_bool_not(is_params);
8225 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8226 trivial = isl_set_plain_is_universe(set);
8227 isl_set_free(set);
8228 return isl_bool_not(trivial);
8231 /* Construct a multiple union piecewise affine expression
8232 * in the given space with value zero in each of the output dimensions.
8234 * Since there is no canonical zero value for
8235 * a union piecewise affine expression, we can only construct
8236 * a zero-dimensional "zero" value.
8238 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8239 __isl_take isl_space *space)
8241 isl_bool params;
8243 if (!space)
8244 return NULL;
8246 params = isl_space_is_params(space);
8247 if (params < 0)
8248 goto error;
8249 if (params)
8250 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8251 "expecting proper set space", goto error);
8252 if (!isl_space_is_set(space))
8253 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8254 "expecting set space", goto error);
8255 if (isl_space_dim(space , isl_dim_out) != 0)
8256 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8257 "expecting 0D space", goto error);
8259 return isl_multi_union_pw_aff_alloc(space);
8260 error:
8261 isl_space_free(space);
8262 return NULL;
8265 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8266 * with the actual sum on the shared domain and
8267 * the defined expression on the symmetric difference of the domains.
8269 * We simply iterate over the elements in both arguments and
8270 * call isl_union_pw_aff_union_add on each of them, if there is
8271 * at least one element.
8273 * Otherwise, the two expressions have an explicit domain and
8274 * the union of these explicit domains is computed.
8275 * This assumes that the explicit domains are either both in terms
8276 * of specific domains elements or both in terms of parameters.
8277 * However, if one of the expressions does not have any constraints
8278 * on its explicit domain, then this is allowed as well and the result
8279 * is the expression with no constraints on its explicit domain.
8281 static __isl_give isl_multi_union_pw_aff *
8282 isl_multi_union_pw_aff_union_add_aligned(
8283 __isl_take isl_multi_union_pw_aff *mupa1,
8284 __isl_take isl_multi_union_pw_aff *mupa2)
8286 isl_bool has_domain, is_params1, is_params2;
8288 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8289 goto error;
8290 if (mupa1->n > 0)
8291 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8292 &isl_union_pw_aff_union_add);
8293 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8294 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8295 goto error;
8297 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8298 if (has_domain < 0)
8299 goto error;
8300 if (!has_domain) {
8301 isl_multi_union_pw_aff_free(mupa2);
8302 return mupa1;
8304 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8305 if (has_domain < 0)
8306 goto error;
8307 if (!has_domain) {
8308 isl_multi_union_pw_aff_free(mupa1);
8309 return mupa2;
8312 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8313 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8314 if (is_params1 < 0 || is_params2 < 0)
8315 goto error;
8316 if (is_params1 != is_params2)
8317 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8318 isl_error_invalid,
8319 "cannot compute union of concrete domain and "
8320 "parameter constraints", goto error);
8321 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8322 if (!mupa1)
8323 goto error;
8324 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8325 isl_union_set_copy(mupa2->u.dom));
8326 if (!mupa1->u.dom)
8327 goto error;
8328 isl_multi_union_pw_aff_free(mupa2);
8329 return mupa1;
8330 error:
8331 isl_multi_union_pw_aff_free(mupa1);
8332 isl_multi_union_pw_aff_free(mupa2);
8333 return NULL;
8336 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8337 * with the actual sum on the shared domain and
8338 * the defined expression on the symmetric difference of the domains.
8340 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8341 __isl_take isl_multi_union_pw_aff *mupa1,
8342 __isl_take isl_multi_union_pw_aff *mupa2)
8344 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8345 &isl_multi_union_pw_aff_union_add_aligned);
8348 /* Construct and return a multi union piecewise affine expression
8349 * that is equal to the given multi affine expression.
8351 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8352 __isl_take isl_multi_aff *ma)
8354 isl_multi_pw_aff *mpa;
8356 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8357 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8360 /* Construct and return a multi union piecewise affine expression
8361 * that is equal to the given multi piecewise affine expression.
8363 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8364 __isl_take isl_multi_pw_aff *mpa)
8366 int i, n;
8367 isl_space *space;
8368 isl_multi_union_pw_aff *mupa;
8370 if (!mpa)
8371 return NULL;
8373 space = isl_multi_pw_aff_get_space(mpa);
8374 space = isl_space_range(space);
8375 mupa = isl_multi_union_pw_aff_alloc(space);
8377 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8378 for (i = 0; i < n; ++i) {
8379 isl_pw_aff *pa;
8380 isl_union_pw_aff *upa;
8382 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8383 upa = isl_union_pw_aff_from_pw_aff(pa);
8384 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8387 isl_multi_pw_aff_free(mpa);
8389 return mupa;
8392 /* Extract the range space of "pma" and assign it to *space.
8393 * If *space has already been set (through a previous call to this function),
8394 * then check that the range space is the same.
8396 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8398 isl_space **space = user;
8399 isl_space *pma_space;
8400 isl_bool equal;
8402 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8403 isl_pw_multi_aff_free(pma);
8405 if (!pma_space)
8406 return isl_stat_error;
8407 if (!*space) {
8408 *space = pma_space;
8409 return isl_stat_ok;
8412 equal = isl_space_is_equal(pma_space, *space);
8413 isl_space_free(pma_space);
8415 if (equal < 0)
8416 return isl_stat_error;
8417 if (!equal)
8418 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8419 "range spaces not the same", return isl_stat_error);
8420 return isl_stat_ok;
8423 /* Construct and return a multi union piecewise affine expression
8424 * that is equal to the given union piecewise multi affine expression.
8426 * In order to be able to perform the conversion, the input
8427 * needs to be non-empty and may only involve a single range space.
8429 * If the resulting multi union piecewise affine expression has
8430 * an explicit domain, then assign it the domain of the input.
8431 * In other cases, the domain is stored in the individual elements.
8433 __isl_give isl_multi_union_pw_aff *
8434 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8435 __isl_take isl_union_pw_multi_aff *upma)
8437 isl_space *space = NULL;
8438 isl_multi_union_pw_aff *mupa;
8439 int i, n;
8441 if (!upma)
8442 return NULL;
8443 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8444 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8445 "cannot extract range space from empty input",
8446 goto error);
8447 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8448 &space) < 0)
8449 goto error;
8451 if (!space)
8452 goto error;
8454 n = isl_space_dim(space, isl_dim_set);
8455 mupa = isl_multi_union_pw_aff_alloc(space);
8457 for (i = 0; i < n; ++i) {
8458 isl_union_pw_aff *upa;
8460 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8461 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8463 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8464 isl_union_set *dom;
8465 isl_union_pw_multi_aff *copy;
8467 copy = isl_union_pw_multi_aff_copy(upma);
8468 dom = isl_union_pw_multi_aff_domain(copy);
8469 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8472 isl_union_pw_multi_aff_free(upma);
8473 return mupa;
8474 error:
8475 isl_space_free(space);
8476 isl_union_pw_multi_aff_free(upma);
8477 return NULL;
8480 /* Try and create an isl_multi_union_pw_aff that is equivalent
8481 * to the given isl_union_map.
8482 * The isl_union_map is required to be single-valued in each space.
8483 * Moreover, it cannot be empty and all range spaces need to be the same.
8484 * Otherwise, an error is produced.
8486 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8487 __isl_take isl_union_map *umap)
8489 isl_union_pw_multi_aff *upma;
8491 upma = isl_union_pw_multi_aff_from_union_map(umap);
8492 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8495 /* Return a multiple union piecewise affine expression
8496 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8497 * have been aligned.
8499 * If the resulting multi union piecewise affine expression has
8500 * an explicit domain, then assign it the input domain.
8501 * In other cases, the domain is stored in the individual elements.
8503 static __isl_give isl_multi_union_pw_aff *
8504 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8505 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8507 int i, n;
8508 isl_space *space;
8509 isl_multi_union_pw_aff *mupa;
8511 if (!domain || !mv)
8512 goto error;
8514 n = isl_multi_val_dim(mv, isl_dim_set);
8515 space = isl_multi_val_get_space(mv);
8516 mupa = isl_multi_union_pw_aff_alloc(space);
8517 for (i = 0; i < n; ++i) {
8518 isl_val *v;
8519 isl_union_pw_aff *upa;
8521 v = isl_multi_val_get_val(mv, i);
8522 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8524 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8526 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8527 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8528 isl_union_set_copy(domain));
8530 isl_union_set_free(domain);
8531 isl_multi_val_free(mv);
8532 return mupa;
8533 error:
8534 isl_union_set_free(domain);
8535 isl_multi_val_free(mv);
8536 return NULL;
8539 /* Return a multiple union piecewise affine expression
8540 * that is equal to "mv" on "domain".
8542 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8543 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8545 isl_bool equal_params;
8547 if (!domain || !mv)
8548 goto error;
8549 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8550 if (equal_params < 0)
8551 goto error;
8552 if (equal_params)
8553 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8554 domain, mv);
8555 domain = isl_union_set_align_params(domain,
8556 isl_multi_val_get_space(mv));
8557 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8558 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8559 error:
8560 isl_union_set_free(domain);
8561 isl_multi_val_free(mv);
8562 return NULL;
8565 /* Return a multiple union piecewise affine expression
8566 * that is equal to "ma" on "domain".
8568 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8569 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8571 isl_pw_multi_aff *pma;
8573 pma = isl_pw_multi_aff_from_multi_aff(ma);
8574 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8577 /* Return a multiple union piecewise affine expression
8578 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8579 * have been aligned.
8581 * If the resulting multi union piecewise affine expression has
8582 * an explicit domain, then assign it the input domain.
8583 * In other cases, the domain is stored in the individual elements.
8585 static __isl_give isl_multi_union_pw_aff *
8586 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8587 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8589 int i, n;
8590 isl_space *space;
8591 isl_multi_union_pw_aff *mupa;
8593 if (!domain || !pma)
8594 goto error;
8596 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8597 space = isl_pw_multi_aff_get_space(pma);
8598 mupa = isl_multi_union_pw_aff_alloc(space);
8599 for (i = 0; i < n; ++i) {
8600 isl_pw_aff *pa;
8601 isl_union_pw_aff *upa;
8603 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8604 upa = isl_union_pw_aff_pw_aff_on_domain(
8605 isl_union_set_copy(domain), pa);
8606 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8608 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8609 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8610 isl_union_set_copy(domain));
8612 isl_union_set_free(domain);
8613 isl_pw_multi_aff_free(pma);
8614 return mupa;
8615 error:
8616 isl_union_set_free(domain);
8617 isl_pw_multi_aff_free(pma);
8618 return NULL;
8621 /* Return a multiple union piecewise affine expression
8622 * that is equal to "pma" on "domain".
8624 __isl_give isl_multi_union_pw_aff *
8625 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8626 __isl_take isl_pw_multi_aff *pma)
8628 isl_bool equal_params;
8629 isl_space *space;
8631 space = isl_pw_multi_aff_peek_space(pma);
8632 equal_params = isl_union_set_space_has_equal_params(domain, space);
8633 if (equal_params < 0)
8634 goto error;
8635 if (equal_params)
8636 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8637 domain, pma);
8638 domain = isl_union_set_align_params(domain,
8639 isl_pw_multi_aff_get_space(pma));
8640 pma = isl_pw_multi_aff_align_params(pma,
8641 isl_union_set_get_space(domain));
8642 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8643 pma);
8644 error:
8645 isl_union_set_free(domain);
8646 isl_pw_multi_aff_free(pma);
8647 return NULL;
8650 /* Return a union set containing those elements in the domains
8651 * of the elements of "mupa" where they are all zero.
8653 * If there are no elements, then simply return the entire domain.
8655 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8656 __isl_take isl_multi_union_pw_aff *mupa)
8658 int i, n;
8659 isl_union_pw_aff *upa;
8660 isl_union_set *zero;
8662 if (!mupa)
8663 return NULL;
8665 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8666 if (n == 0)
8667 return isl_multi_union_pw_aff_domain(mupa);
8669 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8670 zero = isl_union_pw_aff_zero_union_set(upa);
8672 for (i = 1; i < n; ++i) {
8673 isl_union_set *zero_i;
8675 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8676 zero_i = isl_union_pw_aff_zero_union_set(upa);
8678 zero = isl_union_set_intersect(zero, zero_i);
8681 isl_multi_union_pw_aff_free(mupa);
8682 return zero;
8685 /* Construct a union map mapping the shared domain
8686 * of the union piecewise affine expressions to the range of "mupa"
8687 * in the special case of a 0D multi union piecewise affine expression.
8689 * Construct a map between the explicit domain of "mupa" and
8690 * the range space.
8691 * Note that this assumes that the domain consists of explicit elements.
8693 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8694 __isl_take isl_multi_union_pw_aff *mupa)
8696 isl_bool is_params;
8697 isl_space *space;
8698 isl_union_set *dom, *ran;
8700 space = isl_multi_union_pw_aff_get_space(mupa);
8701 dom = isl_multi_union_pw_aff_domain(mupa);
8702 ran = isl_union_set_from_set(isl_set_universe(space));
8704 is_params = isl_union_set_is_params(dom);
8705 if (is_params < 0)
8706 dom = isl_union_set_free(dom);
8707 else if (is_params)
8708 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8709 "cannot create union map from expression without "
8710 "explicit domain elements",
8711 dom = isl_union_set_free(dom));
8713 return isl_union_map_from_domain_and_range(dom, ran);
8716 /* Construct a union map mapping the shared domain
8717 * of the union piecewise affine expressions to the range of "mupa"
8718 * with each dimension in the range equated to the
8719 * corresponding union piecewise affine expression.
8721 * If the input is zero-dimensional, then construct a mapping
8722 * from its explicit domain.
8724 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8725 __isl_take isl_multi_union_pw_aff *mupa)
8727 int i, n;
8728 isl_space *space;
8729 isl_union_map *umap;
8730 isl_union_pw_aff *upa;
8732 if (!mupa)
8733 return NULL;
8735 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8736 if (n == 0)
8737 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8739 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8740 umap = isl_union_map_from_union_pw_aff(upa);
8742 for (i = 1; i < n; ++i) {
8743 isl_union_map *umap_i;
8745 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8746 umap_i = isl_union_map_from_union_pw_aff(upa);
8747 umap = isl_union_map_flat_range_product(umap, umap_i);
8750 space = isl_multi_union_pw_aff_get_space(mupa);
8751 umap = isl_union_map_reset_range_space(umap, space);
8753 isl_multi_union_pw_aff_free(mupa);
8754 return umap;
8757 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8758 * "range" is the space from which to set the range space.
8759 * "res" collects the results.
8761 struct isl_union_pw_multi_aff_reset_range_space_data {
8762 isl_space *range;
8763 isl_union_pw_multi_aff *res;
8766 /* Replace the range space of "pma" by the range space of data->range and
8767 * add the result to data->res.
8769 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8771 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8772 isl_space *space;
8774 space = isl_pw_multi_aff_get_space(pma);
8775 space = isl_space_domain(space);
8776 space = isl_space_extend_domain_with_range(space,
8777 isl_space_copy(data->range));
8778 pma = isl_pw_multi_aff_reset_space(pma, space);
8779 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8781 return data->res ? isl_stat_ok : isl_stat_error;
8784 /* Replace the range space of all the piecewise affine expressions in "upma" by
8785 * the range space of "space".
8787 * This assumes that all these expressions have the same output dimension.
8789 * Since the spaces of the expressions change, so do their hash values.
8790 * We therefore need to create a new isl_union_pw_multi_aff.
8791 * Note that the hash value is currently computed based on the entire
8792 * space even though there can only be a single expression with a given
8793 * domain space.
8795 static __isl_give isl_union_pw_multi_aff *
8796 isl_union_pw_multi_aff_reset_range_space(
8797 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8799 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8800 isl_space *space_upma;
8802 space_upma = isl_union_pw_multi_aff_get_space(upma);
8803 data.res = isl_union_pw_multi_aff_empty(space_upma);
8804 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8805 &reset_range_space, &data) < 0)
8806 data.res = isl_union_pw_multi_aff_free(data.res);
8808 isl_space_free(space);
8809 isl_union_pw_multi_aff_free(upma);
8810 return data.res;
8813 /* Construct and return a union piecewise multi affine expression
8814 * that is equal to the given multi union piecewise affine expression,
8815 * in the special case of a 0D multi union piecewise affine expression.
8817 * Construct a union piecewise multi affine expression
8818 * on top of the explicit domain of the input.
8820 __isl_give isl_union_pw_multi_aff *
8821 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8822 __isl_take isl_multi_union_pw_aff *mupa)
8824 isl_space *space;
8825 isl_multi_val *mv;
8826 isl_union_set *domain;
8828 space = isl_multi_union_pw_aff_get_space(mupa);
8829 mv = isl_multi_val_zero(space);
8830 domain = isl_multi_union_pw_aff_domain(mupa);
8831 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8834 /* Construct and return a union piecewise multi affine expression
8835 * that is equal to the given multi union piecewise affine expression.
8837 * If the input is zero-dimensional, then
8838 * construct a union piecewise multi affine expression
8839 * on top of the explicit domain of the input.
8841 __isl_give isl_union_pw_multi_aff *
8842 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8843 __isl_take isl_multi_union_pw_aff *mupa)
8845 int i, n;
8846 isl_space *space;
8847 isl_union_pw_multi_aff *upma;
8848 isl_union_pw_aff *upa;
8850 if (!mupa)
8851 return NULL;
8853 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8854 if (n == 0)
8855 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8857 space = isl_multi_union_pw_aff_get_space(mupa);
8858 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8859 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8861 for (i = 1; i < n; ++i) {
8862 isl_union_pw_multi_aff *upma_i;
8864 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8865 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8866 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8869 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8871 isl_multi_union_pw_aff_free(mupa);
8872 return upma;
8875 /* Intersect the range of "mupa" with "range",
8876 * in the special case where "mupa" is 0D.
8878 * Intersect the domain of "mupa" with the constraints on the parameters
8879 * of "range".
8881 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8882 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8884 range = isl_set_params(range);
8885 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8886 return mupa;
8889 /* Intersect the range of "mupa" with "range".
8890 * That is, keep only those domain elements that have a function value
8891 * in "range".
8893 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8894 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8896 isl_union_pw_multi_aff *upma;
8897 isl_union_set *domain;
8898 isl_space *space;
8899 int n;
8900 int match;
8902 if (!mupa || !range)
8903 goto error;
8905 space = isl_set_get_space(range);
8906 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8907 space, isl_dim_set);
8908 isl_space_free(space);
8909 if (match < 0)
8910 goto error;
8911 if (!match)
8912 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8913 "space don't match", goto error);
8914 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8915 if (n == 0)
8916 return mupa_intersect_range_0D(mupa, range);
8918 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8919 isl_multi_union_pw_aff_copy(mupa));
8920 domain = isl_union_set_from_set(range);
8921 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8922 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8924 return mupa;
8925 error:
8926 isl_multi_union_pw_aff_free(mupa);
8927 isl_set_free(range);
8928 return NULL;
8931 /* Return the shared domain of the elements of "mupa",
8932 * in the special case where "mupa" is zero-dimensional.
8934 * Return the explicit domain of "mupa".
8935 * Note that this domain may be a parameter set, either
8936 * because "mupa" is meant to live in a set space or
8937 * because no explicit domain has been set.
8939 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8940 __isl_take isl_multi_union_pw_aff *mupa)
8942 isl_union_set *dom;
8944 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8945 isl_multi_union_pw_aff_free(mupa);
8947 return dom;
8950 /* Return the shared domain of the elements of "mupa".
8952 * If "mupa" is zero-dimensional, then return its explicit domain.
8954 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8955 __isl_take isl_multi_union_pw_aff *mupa)
8957 int i, n;
8958 isl_union_pw_aff *upa;
8959 isl_union_set *dom;
8961 if (!mupa)
8962 return NULL;
8964 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8965 if (n == 0)
8966 return isl_multi_union_pw_aff_domain_0D(mupa);
8968 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8969 dom = isl_union_pw_aff_domain(upa);
8970 for (i = 1; i < n; ++i) {
8971 isl_union_set *dom_i;
8973 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8974 dom_i = isl_union_pw_aff_domain(upa);
8975 dom = isl_union_set_intersect(dom, dom_i);
8978 isl_multi_union_pw_aff_free(mupa);
8979 return dom;
8982 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8983 * In particular, the spaces have been aligned.
8984 * The result is defined over the shared domain of the elements of "mupa"
8986 * We first extract the parametric constant part of "aff" and
8987 * define that over the shared domain.
8988 * Then we iterate over all input dimensions of "aff" and add the corresponding
8989 * multiples of the elements of "mupa".
8990 * Finally, we consider the integer divisions, calling the function
8991 * recursively to obtain an isl_union_pw_aff corresponding to the
8992 * integer division argument.
8994 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8995 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8997 int i, n_in, n_div;
8998 isl_union_pw_aff *upa;
8999 isl_union_set *uset;
9000 isl_val *v;
9001 isl_aff *cst;
9003 n_in = isl_aff_dim(aff, isl_dim_in);
9004 n_div = isl_aff_dim(aff, isl_dim_div);
9006 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9007 cst = isl_aff_copy(aff);
9008 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9009 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9010 cst = isl_aff_project_domain_on_params(cst);
9011 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9013 for (i = 0; i < n_in; ++i) {
9014 isl_union_pw_aff *upa_i;
9016 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9017 continue;
9018 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9019 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9020 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9021 upa = isl_union_pw_aff_add(upa, upa_i);
9024 for (i = 0; i < n_div; ++i) {
9025 isl_aff *div;
9026 isl_union_pw_aff *upa_i;
9028 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9029 continue;
9030 div = isl_aff_get_div(aff, i);
9031 upa_i = multi_union_pw_aff_apply_aff(
9032 isl_multi_union_pw_aff_copy(mupa), div);
9033 upa_i = isl_union_pw_aff_floor(upa_i);
9034 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9035 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9036 upa = isl_union_pw_aff_add(upa, upa_i);
9039 isl_multi_union_pw_aff_free(mupa);
9040 isl_aff_free(aff);
9042 return upa;
9045 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9046 * with the domain of "aff".
9047 * Furthermore, the dimension of this space needs to be greater than zero.
9048 * The result is defined over the shared domain of the elements of "mupa"
9050 * We perform these checks and then hand over control to
9051 * multi_union_pw_aff_apply_aff.
9053 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9054 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9056 isl_space *space1, *space2;
9057 int equal;
9059 mupa = isl_multi_union_pw_aff_align_params(mupa,
9060 isl_aff_get_space(aff));
9061 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9062 if (!mupa || !aff)
9063 goto error;
9065 space1 = isl_multi_union_pw_aff_get_space(mupa);
9066 space2 = isl_aff_get_domain_space(aff);
9067 equal = isl_space_is_equal(space1, space2);
9068 isl_space_free(space1);
9069 isl_space_free(space2);
9070 if (equal < 0)
9071 goto error;
9072 if (!equal)
9073 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9074 "spaces don't match", goto error);
9075 if (isl_aff_dim(aff, isl_dim_in) == 0)
9076 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9077 "cannot determine domains", goto error);
9079 return multi_union_pw_aff_apply_aff(mupa, aff);
9080 error:
9081 isl_multi_union_pw_aff_free(mupa);
9082 isl_aff_free(aff);
9083 return NULL;
9086 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9087 * The space of "mupa" is known to be compatible with the domain of "ma".
9089 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9090 * on the domain of "mupa".
9092 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9093 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9095 isl_union_set *dom;
9097 dom = isl_multi_union_pw_aff_domain(mupa);
9098 ma = isl_multi_aff_project_domain_on_params(ma);
9100 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9103 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9104 * with the domain of "ma".
9105 * The result is defined over the shared domain of the elements of "mupa"
9107 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9108 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9110 isl_space *space1, *space2;
9111 isl_multi_union_pw_aff *res;
9112 int equal;
9113 int i, n_out;
9115 mupa = isl_multi_union_pw_aff_align_params(mupa,
9116 isl_multi_aff_get_space(ma));
9117 ma = isl_multi_aff_align_params(ma,
9118 isl_multi_union_pw_aff_get_space(mupa));
9119 if (!mupa || !ma)
9120 goto error;
9122 space1 = isl_multi_union_pw_aff_get_space(mupa);
9123 space2 = isl_multi_aff_get_domain_space(ma);
9124 equal = isl_space_is_equal(space1, space2);
9125 isl_space_free(space1);
9126 isl_space_free(space2);
9127 if (equal < 0)
9128 goto error;
9129 if (!equal)
9130 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9131 "spaces don't match", goto error);
9132 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9133 if (isl_multi_aff_dim(ma, isl_dim_in) == 0)
9134 return mupa_apply_multi_aff_0D(mupa, ma);
9136 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9137 res = isl_multi_union_pw_aff_alloc(space1);
9139 for (i = 0; i < n_out; ++i) {
9140 isl_aff *aff;
9141 isl_union_pw_aff *upa;
9143 aff = isl_multi_aff_get_aff(ma, i);
9144 upa = multi_union_pw_aff_apply_aff(
9145 isl_multi_union_pw_aff_copy(mupa), aff);
9146 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9149 isl_multi_aff_free(ma);
9150 isl_multi_union_pw_aff_free(mupa);
9151 return res;
9152 error:
9153 isl_multi_union_pw_aff_free(mupa);
9154 isl_multi_aff_free(ma);
9155 return NULL;
9158 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9159 * The space of "mupa" is known to be compatible with the domain of "pa".
9161 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9162 * on the domain of "mupa".
9164 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9165 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9167 isl_union_set *dom;
9169 dom = isl_multi_union_pw_aff_domain(mupa);
9170 pa = isl_pw_aff_project_domain_on_params(pa);
9172 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9175 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9176 * with the domain of "pa".
9177 * Furthermore, the dimension of this space needs to be greater than zero.
9178 * The result is defined over the shared domain of the elements of "mupa"
9180 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9181 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9183 int i;
9184 int equal;
9185 isl_space *space, *space2;
9186 isl_union_pw_aff *upa;
9188 mupa = isl_multi_union_pw_aff_align_params(mupa,
9189 isl_pw_aff_get_space(pa));
9190 pa = isl_pw_aff_align_params(pa,
9191 isl_multi_union_pw_aff_get_space(mupa));
9192 if (!mupa || !pa)
9193 goto error;
9195 space = isl_multi_union_pw_aff_get_space(mupa);
9196 space2 = isl_pw_aff_get_domain_space(pa);
9197 equal = isl_space_is_equal(space, space2);
9198 isl_space_free(space);
9199 isl_space_free(space2);
9200 if (equal < 0)
9201 goto error;
9202 if (!equal)
9203 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9204 "spaces don't match", goto error);
9205 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
9206 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9208 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9209 upa = isl_union_pw_aff_empty(space);
9211 for (i = 0; i < pa->n; ++i) {
9212 isl_aff *aff;
9213 isl_set *domain;
9214 isl_multi_union_pw_aff *mupa_i;
9215 isl_union_pw_aff *upa_i;
9217 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9218 domain = isl_set_copy(pa->p[i].set);
9219 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9220 aff = isl_aff_copy(pa->p[i].aff);
9221 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9222 upa = isl_union_pw_aff_union_add(upa, upa_i);
9225 isl_multi_union_pw_aff_free(mupa);
9226 isl_pw_aff_free(pa);
9227 return upa;
9228 error:
9229 isl_multi_union_pw_aff_free(mupa);
9230 isl_pw_aff_free(pa);
9231 return NULL;
9234 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9235 * The space of "mupa" is known to be compatible with the domain of "pma".
9237 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9238 * on the domain of "mupa".
9240 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9241 __isl_take isl_multi_union_pw_aff *mupa,
9242 __isl_take isl_pw_multi_aff *pma)
9244 isl_union_set *dom;
9246 dom = isl_multi_union_pw_aff_domain(mupa);
9247 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9249 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9252 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9253 * with the domain of "pma".
9254 * The result is defined over the shared domain of the elements of "mupa"
9256 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9257 __isl_take isl_multi_union_pw_aff *mupa,
9258 __isl_take isl_pw_multi_aff *pma)
9260 isl_space *space1, *space2;
9261 isl_multi_union_pw_aff *res;
9262 int equal;
9263 int i, n_out;
9265 mupa = isl_multi_union_pw_aff_align_params(mupa,
9266 isl_pw_multi_aff_get_space(pma));
9267 pma = isl_pw_multi_aff_align_params(pma,
9268 isl_multi_union_pw_aff_get_space(mupa));
9269 if (!mupa || !pma)
9270 goto error;
9272 space1 = isl_multi_union_pw_aff_get_space(mupa);
9273 space2 = isl_pw_multi_aff_get_domain_space(pma);
9274 equal = isl_space_is_equal(space1, space2);
9275 isl_space_free(space1);
9276 isl_space_free(space2);
9277 if (equal < 0)
9278 goto error;
9279 if (!equal)
9280 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9281 "spaces don't match", goto error);
9282 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9283 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0)
9284 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9286 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9287 res = isl_multi_union_pw_aff_alloc(space1);
9289 for (i = 0; i < n_out; ++i) {
9290 isl_pw_aff *pa;
9291 isl_union_pw_aff *upa;
9293 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9294 upa = isl_multi_union_pw_aff_apply_pw_aff(
9295 isl_multi_union_pw_aff_copy(mupa), pa);
9296 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9299 isl_pw_multi_aff_free(pma);
9300 isl_multi_union_pw_aff_free(mupa);
9301 return res;
9302 error:
9303 isl_multi_union_pw_aff_free(mupa);
9304 isl_pw_multi_aff_free(pma);
9305 return NULL;
9308 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9309 * If the explicit domain only keeps track of constraints on the parameters,
9310 * then only update those constraints.
9312 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9313 __isl_take isl_multi_union_pw_aff *mupa,
9314 __isl_keep isl_union_pw_multi_aff *upma)
9316 isl_bool is_params;
9318 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9319 return isl_multi_union_pw_aff_free(mupa);
9321 mupa = isl_multi_union_pw_aff_cow(mupa);
9322 if (!mupa)
9323 return NULL;
9325 is_params = isl_union_set_is_params(mupa->u.dom);
9326 if (is_params < 0)
9327 return isl_multi_union_pw_aff_free(mupa);
9329 upma = isl_union_pw_multi_aff_copy(upma);
9330 if (is_params)
9331 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9332 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9333 else
9334 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9335 mupa->u.dom, upma);
9336 if (!mupa->u.dom)
9337 return isl_multi_union_pw_aff_free(mupa);
9338 return mupa;
9341 /* Compute the pullback of "mupa" by the function represented by "upma".
9342 * In other words, plug in "upma" in "mupa". The result contains
9343 * expressions defined over the domain space of "upma".
9345 * Run over all elements of "mupa" and plug in "upma" in each of them.
9347 * If "mupa" has an explicit domain, then it is this domain
9348 * that needs to undergo a pullback instead, i.e., a preimage.
9350 __isl_give isl_multi_union_pw_aff *
9351 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9352 __isl_take isl_multi_union_pw_aff *mupa,
9353 __isl_take isl_union_pw_multi_aff *upma)
9355 int i, n;
9357 mupa = isl_multi_union_pw_aff_align_params(mupa,
9358 isl_union_pw_multi_aff_get_space(upma));
9359 upma = isl_union_pw_multi_aff_align_params(upma,
9360 isl_multi_union_pw_aff_get_space(mupa));
9361 mupa = isl_multi_union_pw_aff_cow(mupa);
9362 if (!mupa || !upma)
9363 goto error;
9365 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9366 for (i = 0; i < n; ++i) {
9367 isl_union_pw_aff *upa;
9369 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9370 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9371 isl_union_pw_multi_aff_copy(upma));
9372 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9375 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9376 mupa = preimage_explicit_domain(mupa, upma);
9378 isl_union_pw_multi_aff_free(upma);
9379 return mupa;
9380 error:
9381 isl_multi_union_pw_aff_free(mupa);
9382 isl_union_pw_multi_aff_free(upma);
9383 return NULL;
9386 /* Extract the sequence of elements in "mupa" with domain space "space"
9387 * (ignoring parameters).
9389 * For the elements of "mupa" that are not defined on the specified space,
9390 * the corresponding element in the result is empty.
9392 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9393 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9395 int i, n;
9396 isl_space *space_mpa;
9397 isl_multi_pw_aff *mpa;
9399 if (!mupa || !space)
9400 goto error;
9402 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9403 space = isl_space_replace_params(space, space_mpa);
9404 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9405 space_mpa);
9406 mpa = isl_multi_pw_aff_alloc(space_mpa);
9408 space = isl_space_from_domain(space);
9409 space = isl_space_add_dims(space, isl_dim_out, 1);
9410 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9411 for (i = 0; i < n; ++i) {
9412 isl_union_pw_aff *upa;
9413 isl_pw_aff *pa;
9415 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9416 pa = isl_union_pw_aff_extract_pw_aff(upa,
9417 isl_space_copy(space));
9418 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9419 isl_union_pw_aff_free(upa);
9422 isl_space_free(space);
9423 return mpa;
9424 error:
9425 isl_space_free(space);
9426 return NULL;
9429 /* Evaluate the affine function "aff" in the void point "pnt".
9430 * In particular, return the value NaN.
9432 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9433 __isl_take isl_point *pnt)
9435 isl_ctx *ctx;
9437 ctx = isl_point_get_ctx(pnt);
9438 isl_aff_free(aff);
9439 isl_point_free(pnt);
9440 return isl_val_nan(ctx);
9443 /* Evaluate the affine expression "aff"
9444 * in the coordinates (with denominator) "pnt".
9446 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9447 __isl_keep isl_vec *pnt)
9449 isl_int n, d;
9450 isl_ctx *ctx;
9451 isl_val *v;
9453 if (!aff || !pnt)
9454 return NULL;
9456 ctx = isl_vec_get_ctx(aff);
9457 isl_int_init(n);
9458 isl_int_init(d);
9459 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9460 isl_int_mul(d, aff->el[0], pnt->el[0]);
9461 v = isl_val_rat_from_isl_int(ctx, n, d);
9462 v = isl_val_normalize(v);
9463 isl_int_clear(n);
9464 isl_int_clear(d);
9466 return v;
9469 /* Check that the domain space of "aff" is equal to "space".
9471 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9472 __isl_keep isl_space *space)
9474 isl_bool ok;
9476 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9477 if (ok < 0)
9478 return isl_stat_error;
9479 if (!ok)
9480 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9481 "incompatible spaces", return isl_stat_error);
9482 return isl_stat_ok;
9485 /* Evaluate the affine function "aff" in "pnt".
9487 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9488 __isl_take isl_point *pnt)
9490 isl_bool is_void;
9491 isl_val *v;
9492 isl_local_space *ls;
9494 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9495 goto error;
9496 is_void = isl_point_is_void(pnt);
9497 if (is_void < 0)
9498 goto error;
9499 if (is_void)
9500 return eval_void(aff, pnt);
9502 ls = isl_aff_get_domain_local_space(aff);
9503 pnt = isl_local_space_lift_point(ls, pnt);
9505 v = eval(aff->v, isl_point_peek_vec(pnt));
9507 isl_aff_free(aff);
9508 isl_point_free(pnt);
9510 return v;
9511 error:
9512 isl_aff_free(aff);
9513 isl_point_free(pnt);
9514 return NULL;