/* Add a specific constraint of bmap (or its opposite) to tab. * The position of the constraint is specified by "c", where * the equalities of bmap are counted twice, once for the inequality * that is equal to the equality, and once for its negation. * * Each of these constraints has been added to "tab" before by * tab_add_constraints (and later removed again), so there should * already be a row available for the constraint. */ static int tab_add_constraint(struct isl_tab *tab, __isl_keep isl_basic_map *bmap, int *div_map, int c, int oppose) { unsigned dim; unsigned tab_total; unsigned bmap_total; isl_vec *v; int r; if (!tab || !bmap) return -1; tab_total = isl_basic_map_total_dim(tab->bmap); bmap_total = isl_basic_map_total_dim(bmap); dim = isl_space_dim(tab->bmap->dim, isl_dim_all); v = isl_vec_alloc(bmap->ctx, 1 + tab_total); if (!v) return -1; if (c < 2 * bmap->n_eq) { if ((c % 2) != oppose) isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2], 1 + bmap_total); if (oppose) isl_int_sub_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1); expand_constraint(v, dim, bmap->eq[c/2], div_map, bmap->n_div); r = isl_tab_add_ineq(tab, v->el); if (oppose) isl_int_add_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1); if ((c % 2) != oppose) isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2], 1 + bmap_total); } else { c -= 2 * bmap->n_eq; if (oppose) { isl_seq_neg(bmap->ineq[c], bmap->ineq[c], 1 + bmap_total); isl_int_sub_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1); } expand_constraint(v, dim, bmap->ineq[c], div_map, bmap->n_div); r = isl_tab_add_ineq(tab, v->el); if (oppose) { isl_int_add_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1); isl_seq_neg(bmap->ineq[c], bmap->ineq[c], 1 + bmap_total); } } isl_vec_free(v); return r; }
static int increment_range(struct isl_scan_callback *cb, isl_int min, isl_int max) { struct isl_counter *cnt = (struct isl_counter *)cb; isl_int_add(cnt->count, cnt->count, max); isl_int_sub(cnt->count, cnt->count, min); isl_int_add_ui(cnt->count, cnt->count, 1); if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max)) return 0; isl_int_set(cnt->count, cnt->max); return -1; }
static int increment_counter(struct isl_scan_callback *cb, __isl_take isl_vec *sample) { struct isl_counter *cnt = (struct isl_counter *)cb; isl_int_add_ui(cnt->count, cnt->count, 1); isl_vec_free(sample); if (isl_int_is_zero(cnt->max) || isl_int_lt(cnt->count, cnt->max)) return 0; return -1; }
/* Find an integer point in the set represented by "tab" * that lies outside of the equality "eq" e(x) = 0. * If "up" is true, look for a point satisfying e(x) - 1 >= 0. * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1). * The point, if found, is returned. * If no point can be found, a zero-length vector is returned. * * Before solving an ILP problem, we first check if simply * adding the normal of the constraint to one of the known * integer points in the basic set represented by "tab" * yields another point inside the basic set. * * The caller of this function ensures that the tableau is bounded or * that tab->basis and tab->n_unbounded have been set appropriately. */ static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up) { struct isl_ctx *ctx; struct isl_vec *sample = NULL; struct isl_tab_undo *snap; unsigned dim; if (!tab) return NULL; ctx = tab->mat->ctx; dim = tab->n_var; sample = isl_vec_alloc(ctx, 1 + dim); if (!sample) return NULL; isl_int_set_si(sample->el[0], 1); isl_seq_combine(sample->el + 1, ctx->one, tab->bmap->sample->el + 1, up ? ctx->one : ctx->negone, eq + 1, dim); if (isl_basic_map_contains(tab->bmap, sample)) return sample; isl_vec_free(sample); sample = NULL; snap = isl_tab_snap(tab); if (!up) isl_seq_neg(eq, eq, 1 + dim); isl_int_sub_ui(eq[0], eq[0], 1); if (isl_tab_extend_cons(tab, 1) < 0) goto error; if (isl_tab_add_ineq(tab, eq) < 0) goto error; sample = isl_tab_sample(tab); isl_int_add_ui(eq[0], eq[0], 1); if (!up) isl_seq_neg(eq, eq, 1 + dim); if (sample && isl_tab_rollback(tab, snap) < 0) goto error; return sample; error: isl_vec_free(sample); return NULL; }
__isl_give isl_point *isl_point_add_ui(__isl_take isl_point *pnt, enum isl_dim_type type, int pos, unsigned val) { if (!pnt || isl_point_is_void(pnt)) return pnt; pnt = isl_point_cow(pnt); if (!pnt) return NULL; pnt->vec = isl_vec_cow(pnt->vec); if (!pnt->vec) goto error; if (type == isl_dim_set) pos += isl_dim_size(pnt->dim, isl_dim_param); isl_int_add_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val); return pnt; error: isl_point_free(pnt); return NULL; }
/* Look for all integer points in "bset", which is assumed to be bounded, * and call callback->add on each of them. * * We first compute a reduced basis for the set and then scan * the set in the directions of this basis. * We basically perform a depth first search, where in each level i * we compute the range in the i-th basis vector direction, given * fixed values in the directions of the previous basis vector. * We then add an equality to the tableau fixing the value in the * direction of the current basis vector to each value in the range * in turn and then continue to the next level. * * The search is implemented iteratively. "level" identifies the current * basis vector. "init" is true if we want the first value at the current * level and false if we want the next value. * Solutions are added in the leaves of the search tree, i.e., after * we have fixed a value in each direction of the basis. */ int isl_basic_set_scan(struct isl_basic_set *bset, struct isl_scan_callback *callback) { unsigned dim; struct isl_mat *B = NULL; struct isl_tab *tab = NULL; struct isl_vec *min; struct isl_vec *max; struct isl_tab_undo **snap; int level; int init; enum isl_lp_result res; if (!bset) return -1; dim = isl_basic_set_total_dim(bset); if (dim == 0) return scan_0D(bset, callback); min = isl_vec_alloc(bset->ctx, dim); max = isl_vec_alloc(bset->ctx, dim); snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, dim); if (!min || !max || !snap) goto error; tab = isl_tab_from_basic_set(bset, 0); if (!tab) goto error; if (isl_tab_extend_cons(tab, dim + 1) < 0) goto error; tab->basis = isl_mat_identity(bset->ctx, 1 + dim); if (1) tab = isl_tab_compute_reduced_basis(tab); if (!tab) goto error; B = isl_mat_copy(tab->basis); if (!B) goto error; level = 0; init = 1; while (level >= 0) { int empty = 0; if (init) { res = isl_tab_min(tab, B->row[1 + level], bset->ctx->one, &min->el[level], NULL, 0); if (res == isl_lp_empty) empty = 1; if (res == isl_lp_error || res == isl_lp_unbounded) goto error; isl_seq_neg(B->row[1 + level] + 1, B->row[1 + level] + 1, dim); res = isl_tab_min(tab, B->row[1 + level], bset->ctx->one, &max->el[level], NULL, 0); isl_seq_neg(B->row[1 + level] + 1, B->row[1 + level] + 1, dim); isl_int_neg(max->el[level], max->el[level]); if (res == isl_lp_empty) empty = 1; if (res == isl_lp_error || res == isl_lp_unbounded) goto error; snap[level] = isl_tab_snap(tab); } else isl_int_add_ui(min->el[level], min->el[level], 1); if (empty || isl_int_gt(min->el[level], max->el[level])) { level--; init = 0; if (level >= 0) if (isl_tab_rollback(tab, snap[level]) < 0) goto error; continue; } if (level == dim - 1 && callback->add == increment_counter) { if (increment_range(callback, min->el[level], max->el[level])) goto error; level--; init = 0; if (level >= 0) if (isl_tab_rollback(tab, snap[level]) < 0) goto error; continue; } isl_int_neg(B->row[1 + level][0], min->el[level]); if (isl_tab_add_valid_eq(tab, B->row[1 + level]) < 0) goto error; isl_int_set_si(B->row[1 + level][0], 0); if (level < dim - 1) { ++level; init = 1; continue; } if (add_solution(tab, callback) < 0) goto error; init = 0; if (isl_tab_rollback(tab, snap[level]) < 0) goto error; } isl_tab_free(tab); free(snap); isl_vec_free(min); isl_vec_free(max); isl_basic_set_free(bset); isl_mat_free(B); return 0; error: isl_tab_free(tab); free(snap); isl_vec_free(min); isl_vec_free(max); isl_basic_set_free(bset); isl_mat_free(B); return -1; }