+ }
+
+ return pw;
+error:
+ FN(PW,free)(pw);
+ return NULL;
+}
+#endif
+
+__isl_give PW *FN(PW,mul_isl_int)(__isl_take PW *pw, isl_int v)
+{
+ int i;
+
+ if (isl_int_is_one(v))
+ return pw;
+ if (pw && DEFAULT_IS_ZERO && isl_int_is_zero(v)) {
+ PW *zero;
+ isl_space *dim = FN(PW,get_space)(pw);
+#ifdef HAS_TYPE
+ zero = FN(PW,ZERO)(dim, pw->type);
+#else
+ zero = FN(PW,ZERO)(dim);
+#endif
+ FN(PW,free)(pw);
+ return zero;
+ }
+ pw = FN(PW,cow)(pw);
+ if (!pw)
+ return NULL;
+ if (pw->n == 0)
+ return pw;
+
+#ifdef HAS_TYPE
+ if (isl_int_is_neg(v))
+ pw->type = isl_fold_type_negate(pw->type);
+#endif
+ for (i = 0; i < pw->n; ++i) {
+ pw->p[i].FIELD = FN(EL,scale)(pw->p[i].FIELD, v);
+ if (!pw->p[i].FIELD)
+ goto error;
+ }
+
+ return pw;
+error:
+ FN(PW,free)(pw);
+ return NULL;
+}
+
+__isl_give PW *FN(PW,scale)(__isl_take PW *pw, isl_int v)
+{
+ return FN(PW,mul_isl_int)(pw, v);
+}
+
+static int FN(PW,qsort_set_cmp)(const void *p1, const void *p2)
+{
+ isl_set *set1 = *(isl_set * const *)p1;
+ isl_set *set2 = *(isl_set * const *)p2;
+
+ return isl_set_plain_cmp(set1, set2);
+}
+
+/* We normalize in place, but if anything goes wrong we need
+ * to return NULL, so we need to make sure we don't change the
+ * meaning of any possible other copies of map.
+ */
+__isl_give PW *FN(PW,normalize)(__isl_take PW *pw)
+{
+ int i, j;
+ isl_set *set;
+
+ if (!pw)
+ return NULL;
+ for (i = 0; i < pw->n; ++i) {
+ set = isl_set_normalize(isl_set_copy(pw->p[i].set));
+ if (!set)
+ return FN(PW,free)(pw);
+ isl_set_free(pw->p[i].set);
+ pw->p[i].set = set;
+ }
+ qsort(pw->p, pw->n, sizeof(pw->p[0]), &FN(PW,qsort_set_cmp));
+ for (i = pw->n - 1; i >= 1; --i) {
+ if (!isl_set_plain_is_equal(pw->p[i - 1].set, pw->p[i].set))
+ continue;
+ if (!FN(EL,plain_is_equal)(pw->p[i - 1].FIELD, pw->p[i].FIELD))
+ continue;
+ set = isl_set_union(isl_set_copy(pw->p[i - 1].set),
+ isl_set_copy(pw->p[i].set));
+ if (!set)
+ return FN(PW,free)(pw);
+ isl_set_free(pw->p[i].set);
+ FN(EL,free)(pw->p[i].FIELD);
+ isl_set_free(pw->p[i - 1].set);
+ pw->p[i - 1].set = set;
+ for (j = i + 1; j < pw->n; ++j)
+ pw->p[j - 1] = pw->p[j];
+ pw->n--;
+ }
+
+ return pw;
+}
+
+/* Is pw1 obviously equal to pw2?
+ * That is, do they have obviously identical cells and obviously identical
+ * elements on each cell?
+ */
+int FN(PW,plain_is_equal)(__isl_keep PW *pw1, __isl_keep PW *pw2)
+{
+ int i;
+ int equal;
+
+ if (!pw1 || !pw2)
+ return -1;
+
+ if (pw1 == pw2)
+ return 1;
+ if (!isl_space_is_equal(pw1->dim, pw2->dim))
+ return 0;
+
+ pw1 = FN(PW,copy)(pw1);
+ pw2 = FN(PW,copy)(pw2);
+ pw1 = FN(PW,normalize)(pw1);
+ pw2 = FN(PW,normalize)(pw2);
+ if (!pw1 || !pw2)
+ goto error;
+
+ equal = pw1->n == pw2->n;
+ for (i = 0; equal && i < pw1->n; ++i) {
+ equal = isl_set_plain_is_equal(pw1->p[i].set, pw2->p[i].set);
+ if (equal < 0)
+ goto error;
+ if (!equal)
+ break;
+ equal = FN(EL,plain_is_equal)(pw1->p[i].FIELD, pw2->p[i].FIELD);
+ if (equal < 0)
+ goto error;
+ }
+
+ FN(PW,free)(pw1);
+ FN(PW,free)(pw2);
+ return equal;
+error:
+ FN(PW,free)(pw1);
+ FN(PW,free)(pw2);
+ return -1;
+}
+
+#ifndef NO_PULLBACK
+static __isl_give PW *FN(PW,align_params_pw_multi_aff_and)(__isl_take PW *pw,
+ __isl_take isl_multi_aff *ma,
+ __isl_give PW *(*fn)(__isl_take PW *pw, __isl_take isl_multi_aff *ma))
+{
+ isl_ctx *ctx;
+ isl_space *ma_space;
+
+ ma_space = isl_multi_aff_get_space(ma);
+ if (!pw || !ma || !ma_space)
+ goto error;
+ if (isl_space_match(pw->dim, isl_dim_param, ma_space, isl_dim_param)) {
+ isl_space_free(ma_space);
+ return fn(pw, ma);
+ }
+ ctx = FN(PW,get_ctx)(pw);
+ if (!isl_space_has_named_params(pw->dim) ||
+ !isl_space_has_named_params(ma_space))
+ isl_die(ctx, isl_error_invalid,
+ "unaligned unnamed parameters", goto error);
+ pw = FN(PW,align_params)(pw, ma_space);
+ ma = isl_multi_aff_align_params(ma, FN(PW,get_space)(pw));
+ return fn(pw, ma);
+error:
+ isl_space_free(ma_space);
+ FN(PW,free)(pw);
+ isl_multi_aff_free(ma);
+ return NULL;
+}
+
+static __isl_give PW *FN(PW,align_params_pw_pw_multi_aff_and)(__isl_take PW *pw,
+ __isl_take isl_pw_multi_aff *pma,
+ __isl_give PW *(*fn)(__isl_take PW *pw,
+ __isl_take isl_pw_multi_aff *ma))
+{
+ isl_ctx *ctx;
+ isl_space *pma_space;
+
+ pma_space = isl_pw_multi_aff_get_space(pma);
+ if (!pw || !pma || !pma_space)
+ goto error;
+ if (isl_space_match(pw->dim, isl_dim_param, pma_space, isl_dim_param)) {
+ isl_space_free(pma_space);
+ return fn(pw, pma);
+ }
+ ctx = FN(PW,get_ctx)(pw);
+ if (!isl_space_has_named_params(pw->dim) ||
+ !isl_space_has_named_params(pma_space))
+ isl_die(ctx, isl_error_invalid,
+ "unaligned unnamed parameters", goto error);
+ pw = FN(PW,align_params)(pw, pma_space);
+ pma = isl_pw_multi_aff_align_params(pma, FN(PW,get_space)(pw));
+ return fn(pw, pma);
+error:
+ isl_space_free(pma_space);
+ FN(PW,free)(pw);
+ isl_pw_multi_aff_free(pma);
+ return NULL;
+}
+
+/* Compute the pullback of "pw" by the function represented by "ma".
+ * In other words, plug in "ma" in "pw".
+ */
+static __isl_give PW *FN(PW,pullback_multi_aff_aligned)(__isl_take PW *pw,
+ __isl_take isl_multi_aff *ma)
+{
+ int i;
+ isl_space *space = NULL;
+
+ ma = isl_multi_aff_align_divs(ma);
+ pw = FN(PW,cow)(pw);
+ if (!pw || !ma)
+ goto error;
+
+ space = isl_space_join(isl_multi_aff_get_space(ma),
+ FN(PW,get_space)(pw));
+
+ for (i = 0; i < pw->n; ++i) {
+ pw->p[i].set = isl_set_preimage_multi_aff(pw->p[i].set,
+ isl_multi_aff_copy(ma));
+ if (!pw->p[i].set)
+ goto error;
+ pw->p[i].FIELD = FN(EL,pullback_multi_aff)(pw->p[i].FIELD,
+ isl_multi_aff_copy(ma));