+
+static int mul_isl_int(void **entry, void *user)
+{
+ PW **pw = (PW **)entry;
+ isl_int *v = user;
+
+ *pw = FN(PW,mul_isl_int)(*pw, *v);
+ if (!*pw)
+ return -1;
+
+ return 0;
+}
+
+__isl_give UNION *FN(UNION,mul_isl_int)(__isl_take UNION *u, isl_int v)
+{
+ if (isl_int_is_one(v))
+ return u;
+
+ if (DEFAULT_IS_ZERO && u && isl_int_is_zero(v)) {
+ UNION *zero;
+ isl_space *dim = FN(UNION,get_space)(u);
+#ifdef HAS_TYPE
+ zero = FN(UNION,ZERO)(dim, u->type);
+#else
+ zero = FN(UNION,ZERO)(dim);
+#endif
+ FN(UNION,free)(u);
+ return zero;
+ }
+
+ u = FN(UNION,cow)(u);
+ if (!u)
+ return NULL;
+
+#ifdef HAS_TYPE
+ if (isl_int_is_neg(v))
+ u->type = isl_fold_type_negate(u->type);
+#endif
+ if (isl_hash_table_foreach(u->dim->ctx, &u->table, &mul_isl_int, v) < 0)
+ goto error;
+
+ return u;
+error:
+ FN(UNION,free)(u);
+ return NULL;
+}
+
+/* Multiply *entry by the isl_val "user".
+ *
+ * Return 0 on success and -1 on error.
+ */
+static int scale_val(void **entry, void *user)
+{
+ PW **pw = (PW **)entry;
+ isl_val *v = user;
+
+ *pw = FN(PW,scale_val)(*pw, isl_val_copy(v));
+ if (!*pw)
+ return -1;
+
+ return 0;
+}
+
+/* Multiply "u" by "v" and return the result.
+ */
+__isl_give UNION *FN(UNION,scale_val)(__isl_take UNION *u,
+ __isl_take isl_val *v)
+{
+ if (!u || !v)
+ goto error;
+ if (isl_val_is_one(v)) {
+ isl_val_free(v);
+ return u;
+ }
+
+ if (DEFAULT_IS_ZERO && u && isl_val_is_zero(v)) {
+ UNION *zero;
+ isl_space *space = FN(UNION,get_space)(u);
+#ifdef HAS_TYPE
+ zero = FN(UNION,ZERO)(space, u->type);
+#else
+ zero = FN(UNION,ZERO)(space);
+#endif
+ FN(UNION,free)(u);
+ isl_val_free(v);
+ return zero;
+ }
+
+ if (!isl_val_is_rat(v))
+ isl_die(isl_val_get_ctx(v), isl_error_invalid,
+ "expecting rational factor", goto error);
+
+ u = FN(UNION,cow)(u);
+ if (!u)
+ return NULL;
+
+#ifdef HAS_TYPE
+ if (isl_val_is_neg(v))
+ u->type = isl_fold_type_negate(u->type);
+#endif
+ if (isl_hash_table_foreach(u->dim->ctx, &u->table, &scale_val, v) < 0)
+ goto error;
+
+ isl_val_free(v);
+ return u;
+error:
+ isl_val_free(v);
+ FN(UNION,free)(u);
+ return NULL;
+}
+
+S(UNION,plain_is_equal_data)
+{
+ UNION *u2;
+ int is_equal;
+};
+
+static int plain_is_equal_entry(void **entry, void *user)
+{
+ S(UNION,plain_is_equal_data) *data = user;
+ uint32_t hash;
+ struct isl_hash_table_entry *entry2;
+ PW *pw = *entry;
+
+ hash = isl_space_get_hash(pw->dim);
+ entry2 = isl_hash_table_find(data->u2->dim->ctx, &data->u2->table,
+ hash, &has_dim, pw->dim, 0);
+ if (!entry2) {
+ data->is_equal = 0;
+ return -1;
+ }
+
+ data->is_equal = FN(PW,plain_is_equal)(pw, entry2->data);
+ if (data->is_equal < 0 || !data->is_equal)
+ return -1;
+
+ return 0;
+}
+
+int FN(UNION,plain_is_equal)(__isl_keep UNION *u1, __isl_keep UNION *u2)
+{
+ S(UNION,plain_is_equal_data) data = { NULL, 1 };
+
+ if (!u1 || !u2)
+ return -1;
+ if (u1 == u2)
+ return 1;
+ if (u1->table.n != u2->table.n)
+ return 0;
+
+ u1 = FN(UNION,copy)(u1);
+ u2 = FN(UNION,copy)(u2);
+ u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
+ u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
+ if (!u1 || !u2)
+ goto error;
+
+ data.u2 = u2;
+ if (isl_hash_table_foreach(u1->dim->ctx, &u1->table,
+ &plain_is_equal_entry, &data) < 0 &&
+ data.is_equal)
+ goto error;
+
+ FN(UNION,free)(u1);
+ FN(UNION,free)(u2);
+
+ return data.is_equal;
+error:
+ FN(UNION,free)(u1);
+ FN(UNION,free)(u2);
+ return -1;
+}