Blame isl-0.16.1/isl_union_templ.c

Packit fb9d21
/*
Packit fb9d21
 * Copyright 2010      INRIA Saclay
Packit fb9d21
 * Copyright 2013      Ecole Normale Superieure
Packit fb9d21
 *
Packit fb9d21
 * Use of this software is governed by the MIT license
Packit fb9d21
 *
Packit fb9d21
 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
Packit fb9d21
 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
Packit fb9d21
 * 91893 Orsay, France 
Packit fb9d21
 * and Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
Packit fb9d21
 */
Packit fb9d21
Packit fb9d21
__isl_give UNION *FN(UNION,cow)(__isl_take UNION *u);
Packit fb9d21
Packit fb9d21
isl_ctx *FN(UNION,get_ctx)(__isl_keep UNION *u)
Packit fb9d21
{
Packit fb9d21
	return u ? u->space->ctx : NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_space *FN(UNION,get_space)(__isl_keep UNION *u)
Packit fb9d21
{
Packit fb9d21
	if (!u)
Packit fb9d21
		return NULL;
Packit fb9d21
	return isl_space_copy(u->space);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the number of parameters of "u", where "type"
Packit fb9d21
 * is required to be set to isl_dim_param.
Packit fb9d21
 */
Packit fb9d21
unsigned FN(UNION,dim)(__isl_keep UNION *u, enum isl_dim_type type)
Packit fb9d21
{
Packit fb9d21
	if (!u)
Packit fb9d21
		return 0;
Packit fb9d21
Packit fb9d21
	if (type != isl_dim_param)
Packit fb9d21
		isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
Packit fb9d21
			"can only reference parameters", return 0);
Packit fb9d21
Packit fb9d21
	return isl_space_dim(u->space, type);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return the position of the parameter with the given name
Packit fb9d21
 * in "u".
Packit fb9d21
 * Return -1 if no such dimension can be found.
Packit fb9d21
 */
Packit fb9d21
int FN(UNION,find_dim_by_name)(__isl_keep UNION *u, enum isl_dim_type type,
Packit fb9d21
	const char *name)
Packit fb9d21
{
Packit fb9d21
	if (!u)
Packit fb9d21
		return -1;
Packit fb9d21
	return isl_space_find_dim_by_name(u->space, type, name);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
#ifdef HAS_TYPE
Packit fb9d21
static __isl_give UNION *FN(UNION,alloc)(__isl_take isl_space *dim,
Packit fb9d21
	enum isl_fold type, int size)
Packit fb9d21
#else
Packit fb9d21
static __isl_give UNION *FN(UNION,alloc)(__isl_take isl_space *dim, int size)
Packit fb9d21
#endif
Packit fb9d21
{
Packit fb9d21
	UNION *u;
Packit fb9d21
Packit fb9d21
	dim = isl_space_params(dim);
Packit fb9d21
	if (!dim)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	u = isl_calloc_type(dim->ctx, UNION);
Packit fb9d21
	if (!u)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	u->ref = 1;
Packit fb9d21
#ifdef HAS_TYPE
Packit fb9d21
	u->type = type;
Packit fb9d21
#endif
Packit fb9d21
	u->space = dim;
Packit fb9d21
	if (isl_hash_table_init(dim->ctx, &u->table, size) < 0)
Packit fb9d21
		return FN(UNION,free)(u);
Packit fb9d21
Packit fb9d21
	return u;
Packit fb9d21
error:
Packit fb9d21
	isl_space_free(dim);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
#ifdef HAS_TYPE
Packit fb9d21
__isl_give UNION *FN(UNION,ZERO)(__isl_take isl_space *dim, enum isl_fold type)
Packit fb9d21
{
Packit fb9d21
	return FN(UNION,alloc)(dim, type, 16);
Packit fb9d21
}
Packit fb9d21
#else
Packit fb9d21
__isl_give UNION *FN(UNION,ZERO)(__isl_take isl_space *dim)
Packit fb9d21
{
Packit fb9d21
	return FN(UNION,alloc)(dim, 16);
Packit fb9d21
}
Packit fb9d21
#endif
Packit fb9d21
Packit fb9d21
__isl_give UNION *FN(UNION,copy)(__isl_keep UNION *u)
Packit fb9d21
{
Packit fb9d21
	if (!u)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	u->ref++;
Packit fb9d21
	return u;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Extract the element of "u" living in "space" (ignoring parameters).
Packit fb9d21
 *
Packit fb9d21
 * Return the ZERO element if "u" does not contain any element
Packit fb9d21
 * living in "space".
Packit fb9d21
 */
Packit fb9d21
__isl_give PART *FN(FN(UNION,extract),PARTS)(__isl_keep UNION *u,
Packit fb9d21
	__isl_take isl_space *space)
Packit fb9d21
{
Packit fb9d21
	struct isl_hash_table_entry *entry;
Packit fb9d21
Packit fb9d21
	if (!u || !space)
Packit fb9d21
		goto error;
Packit fb9d21
	if (!isl_space_match(u->space, isl_dim_param, space, isl_dim_param)) {
Packit fb9d21
		space = isl_space_drop_dims(space, isl_dim_param,
Packit fb9d21
					0, isl_space_dim(space, isl_dim_param));
Packit fb9d21
		space = isl_space_align_params(space,
Packit fb9d21
					FN(UNION,get_space)(u));
Packit fb9d21
		if (!space)
Packit fb9d21
			goto error;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	entry = FN(UNION,find_part_entry)(u, space, 0);
Packit fb9d21
	if (!entry)
Packit fb9d21
		goto error;
Packit fb9d21
	if (entry == isl_hash_table_entry_none)
Packit fb9d21
#ifdef HAS_TYPE
Packit fb9d21
		return FN(PART,ZERO)(space, u->type);
Packit fb9d21
#else
Packit fb9d21
		return FN(PART,ZERO)(space);
Packit fb9d21
#endif
Packit fb9d21
	isl_space_free(space);
Packit fb9d21
	return FN(PART,copy)(entry->data);
Packit fb9d21
error:
Packit fb9d21
	isl_space_free(space);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Add "part" to "u".
Packit fb9d21
 * If "disjoint" is set, then "u" is not allowed to already have
Packit fb9d21
 * a part that is defined over a domain that overlaps with the domain
Packit fb9d21
 * of "part".
Packit fb9d21
 * Otherwise, compute the union sum of "part" and the part in "u"
Packit fb9d21
 * defined on the same space.
Packit fb9d21
 */
Packit fb9d21
static __isl_give UNION *FN(UNION,add_part_generic)(__isl_take UNION *u,
Packit fb9d21
	__isl_take PART *part, int disjoint)
Packit fb9d21
{
Packit fb9d21
	int empty;
Packit fb9d21
	struct isl_hash_table_entry *entry;
Packit fb9d21
Packit fb9d21
	if (!part)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	empty = FN(PART,IS_ZERO)(part);
Packit fb9d21
	if (empty < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	if (empty) {
Packit fb9d21
		FN(PART,free)(part);
Packit fb9d21
		return u;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	u = FN(UNION,align_params)(u, FN(PART,get_space)(part));
Packit fb9d21
	part = FN(PART,align_params)(part, FN(UNION,get_space)(u));
Packit fb9d21
Packit fb9d21
	u = FN(UNION,cow)(u);
Packit fb9d21
Packit fb9d21
	if (!u)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	if (FN(UNION,check_disjoint_domain_other)(u, part) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
	entry = FN(UNION,find_part_entry)(u, part->dim, 1);
Packit fb9d21
	if (!entry)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	if (!entry->data)
Packit fb9d21
		entry->data = part;
Packit fb9d21
	else {
Packit fb9d21
		if (disjoint &&
Packit fb9d21
		    FN(UNION,check_disjoint_domain)(entry->data, part) < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		entry->data = FN(PART,union_add_)(entry->data,
Packit fb9d21
						FN(PART,copy)(part));
Packit fb9d21
		if (!entry->data)
Packit fb9d21
			goto error;
Packit fb9d21
		empty = FN(PART,IS_ZERO)(part);
Packit fb9d21
		if (empty < 0)
Packit fb9d21
			goto error;
Packit fb9d21
		if (empty)
Packit fb9d21
			u = FN(UNION,remove_part_entry)(u, entry);
Packit fb9d21
		FN(PART,free)(part);
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	return u;
Packit fb9d21
error:
Packit fb9d21
	FN(PART,free)(part);
Packit fb9d21
	FN(UNION,free)(u);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Add "part" to "u", where "u" is assumed not to already have
Packit fb9d21
 * a part that is defined on the same space as "part".
Packit fb9d21
 */
Packit fb9d21
__isl_give UNION *FN(FN(UNION,add),PARTS)(__isl_take UNION *u,
Packit fb9d21
	__isl_take PART *part)
Packit fb9d21
{
Packit fb9d21
	return FN(UNION,add_part_generic)(u, part, 1);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
#ifdef HAS_TYPE
Packit fb9d21
/* Allocate a UNION with the same type and the same size as "u" and
Packit fb9d21
 * with space "space".
Packit fb9d21
 */
Packit fb9d21
static __isl_give UNION *FN(UNION,alloc_same_size_on_space)(__isl_keep UNION *u,
Packit fb9d21
	__isl_take isl_space *space)
Packit fb9d21
{
Packit fb9d21
	if (!u)
Packit fb9d21
		goto error;
Packit fb9d21
	return FN(UNION,alloc)(space, u->type, u->table.n);
Packit fb9d21
error:
Packit fb9d21
	isl_space_free(space);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
#else
Packit fb9d21
/* Allocate a UNION with the same size as "u" and with space "space".
Packit fb9d21
 */
Packit fb9d21
static __isl_give UNION *FN(UNION,alloc_same_size_on_space)(__isl_keep UNION *u,
Packit fb9d21
	__isl_take isl_space *space)
Packit fb9d21
{
Packit fb9d21
	if (!u)
Packit fb9d21
		goto error;
Packit fb9d21
	return FN(UNION,alloc)(space, u->table.n);
Packit fb9d21
error:
Packit fb9d21
	isl_space_free(space);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
#endif
Packit fb9d21
Packit fb9d21
/* Allocate a UNION with the same space, the same type (if any) and
Packit fb9d21
 * the same size as "u".
Packit fb9d21
 */
Packit fb9d21
static __isl_give UNION *FN(UNION,alloc_same_size)(__isl_keep UNION *u)
Packit fb9d21
{
Packit fb9d21
	return FN(UNION,alloc_same_size_on_space)(u, FN(UNION,get_space)(u));
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Internal data structure for isl_union_*_transform_space.
Packit fb9d21
 * "fn' is applied to each entry in the input.
Packit fb9d21
 * "res" collects the results.
Packit fb9d21
 */
Packit fb9d21
S(UNION,transform_data)
Packit fb9d21
{
Packit fb9d21
	__isl_give PART *(*fn)(__isl_take PART *part, void *user);
Packit fb9d21
	void *user;
Packit fb9d21
Packit fb9d21
	UNION *res;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Apply data->fn to "part" and add the result to data->res.
Packit fb9d21
 */
Packit fb9d21
static isl_stat FN(UNION,transform_entry)(__isl_take PART *part, void *user)
Packit fb9d21
{
Packit fb9d21
	S(UNION,transform_data) *data = (S(UNION,transform_data) *)user;
Packit fb9d21
Packit fb9d21
	part = data->fn(part, data->user);
Packit fb9d21
	data->res = FN(FN(UNION,add),PARTS)(data->res, part);
Packit fb9d21
	if (!data->res)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return a UNION living in "space" that is obtained by applying "fn"
Packit fb9d21
 * to each of the entries in "u".
Packit fb9d21
 */
Packit fb9d21
static __isl_give UNION *FN(UNION,transform_space)(__isl_take UNION *u,
Packit fb9d21
	isl_space *space,
Packit fb9d21
	__isl_give PART *(*fn)(__isl_take PART *part, void *user), void *user)
Packit fb9d21
{
Packit fb9d21
	S(UNION,transform_data) data = { fn, user };
Packit fb9d21
Packit fb9d21
	data.res = FN(UNION,alloc_same_size_on_space)(u, space);
Packit fb9d21
	if (FN(FN(UNION,foreach),PARTS)(u,
Packit fb9d21
					&FN(UNION,transform_entry), &data) < 0)
Packit fb9d21
		data.res = FN(UNION,free)(data.res);
Packit fb9d21
	FN(UNION,free)(u);
Packit fb9d21
	return data.res;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Return a UNION that lives in the same space as "u" and that is obtained
Packit fb9d21
 * by applying "fn" to each of the entries in "u".
Packit fb9d21
 */
Packit fb9d21
static __isl_give UNION *FN(UNION,transform)(__isl_take UNION *u,
Packit fb9d21
	__isl_give PART *(*fn)(__isl_take PART *part, void *user), void *user)
Packit fb9d21
{
Packit fb9d21
	return FN(UNION,transform_space)(u, FN(UNION,get_space)(u), fn, user);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Apply data->fn to *part and store the result back into *part.
Packit fb9d21
 */
Packit fb9d21
static isl_stat FN(UNION,transform_inplace_entry)(void **part, void *user)
Packit fb9d21
{
Packit fb9d21
	S(UNION,transform_data) *data = (S(UNION,transform_data) *) user;
Packit fb9d21
Packit fb9d21
	*part = data->fn(*part, data->user);
Packit fb9d21
	if (!*part)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Update "u" by applying "fn" to each entry.
Packit fb9d21
 * This operation is assumed not to change the number of entries nor
Packit fb9d21
 * the spaces of the entries.
Packit fb9d21
 *
Packit fb9d21
 * If there is only one reference to "u", then change "u" inplace.
Packit fb9d21
 * Otherwise, create a new UNION from "u" and discard the original.
Packit fb9d21
 */
Packit fb9d21
static __isl_give UNION *FN(UNION,transform_inplace)(__isl_take UNION *u,
Packit fb9d21
	__isl_give PART *(*fn)(__isl_take PART *part, void *user), void *user)
Packit fb9d21
{
Packit fb9d21
	isl_bool single_ref;
Packit fb9d21
Packit fb9d21
	single_ref = FN(UNION,has_single_reference)(u);
Packit fb9d21
	if (single_ref < 0)
Packit fb9d21
		return FN(UNION,free)(u);
Packit fb9d21
	if (single_ref) {
Packit fb9d21
		S(UNION,transform_data) data = { fn, user };
Packit fb9d21
		if (FN(UNION,foreach_inplace)(u,
Packit fb9d21
				&FN(UNION,transform_inplace_entry), &data) < 0)
Packit fb9d21
			return FN(UNION,free)(u);
Packit fb9d21
		return u;
Packit fb9d21
	}
Packit fb9d21
	return FN(UNION,transform)(u, fn, user);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* An isl_union_*_transform callback for use in isl_union_*_dup
Packit fb9d21
 * that simply returns "part".
Packit fb9d21
 */
Packit fb9d21
static __isl_give PART *FN(UNION,copy_part)(__isl_take PART *part, void *user)
Packit fb9d21
{
Packit fb9d21
	return part;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give UNION *FN(UNION,dup)(__isl_keep UNION *u)
Packit fb9d21
{
Packit fb9d21
	u = FN(UNION,copy)(u);
Packit fb9d21
	return FN(UNION,transform)(u, &FN(UNION,copy_part), NULL);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give UNION *FN(UNION,cow)(__isl_take UNION *u)
Packit fb9d21
{
Packit fb9d21
	if (!u)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (u->ref == 1)
Packit fb9d21
		return u;
Packit fb9d21
	u->ref--;
Packit fb9d21
	return FN(UNION,dup)(u);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_null UNION *FN(UNION,free)(__isl_take UNION *u)
Packit fb9d21
{
Packit fb9d21
	if (!u)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (--u->ref > 0)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	isl_hash_table_foreach(u->space->ctx, &u->table,
Packit fb9d21
				&FN(UNION,free_u_entry), NULL);
Packit fb9d21
	isl_hash_table_clear(&u->table);
Packit fb9d21
	isl_space_free(u->space);
Packit fb9d21
	free(u);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static __isl_give PART *FN(UNION,align_entry)(__isl_take PART *part, void *user)
Packit fb9d21
{
Packit fb9d21
	isl_reordering *exp = user;
Packit fb9d21
Packit fb9d21
	exp = isl_reordering_extend_space(isl_reordering_copy(exp),
Packit fb9d21
				    FN(PART,get_domain_space)(part));
Packit fb9d21
	return FN(PART,realign_domain)(part, exp);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Reorder the parameters of "u" according to the given reordering.
Packit fb9d21
 */
Packit fb9d21
static __isl_give UNION *FN(UNION,realign_domain)(__isl_take UNION *u,
Packit fb9d21
	__isl_take isl_reordering *r)
Packit fb9d21
{
Packit fb9d21
	isl_space *space;
Packit fb9d21
Packit fb9d21
	if (!u || !r)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	space = isl_space_copy(r->dim);
Packit fb9d21
	u = FN(UNION,transform_space)(u, space, &FN(UNION,align_entry), r);
Packit fb9d21
	isl_reordering_free(r);
Packit fb9d21
	return u;
Packit fb9d21
error:
Packit fb9d21
	FN(UNION,free)(u);
Packit fb9d21
	isl_reordering_free(r);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Align the parameters of "u" to those of "model".
Packit fb9d21
 */
Packit fb9d21
__isl_give UNION *FN(UNION,align_params)(__isl_take UNION *u,
Packit fb9d21
	__isl_take isl_space *model)
Packit fb9d21
{
Packit fb9d21
	isl_reordering *r;
Packit fb9d21
Packit fb9d21
	if (!u || !model)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	if (isl_space_match(u->space, isl_dim_param, model, isl_dim_param)) {
Packit fb9d21
		isl_space_free(model);
Packit fb9d21
		return u;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	model = isl_space_params(model);
Packit fb9d21
	r = isl_parameter_alignment_reordering(u->space, model);
Packit fb9d21
	isl_space_free(model);
Packit fb9d21
Packit fb9d21
	return FN(UNION,realign_domain)(u, r);
Packit fb9d21
error:
Packit fb9d21
	isl_space_free(model);
Packit fb9d21
	FN(UNION,free)(u);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Add "part" to *u, taking the union sum if "u" already has
Packit fb9d21
 * a part defined on the same space as "part".
Packit fb9d21
 */
Packit fb9d21
static isl_stat FN(UNION,union_add_part)(__isl_take PART *part, void *user)
Packit fb9d21
{
Packit fb9d21
	UNION **u = (UNION **)user;
Packit fb9d21
Packit fb9d21
	*u = FN(UNION,add_part_generic)(*u, part, 0);
Packit fb9d21
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the sum of "u1" and "u2" on the union of their domains,
Packit fb9d21
 * with the actual sum on the shared domain and
Packit fb9d21
 * the defined expression on the symmetric difference of the domains.
Packit fb9d21
 *
Packit fb9d21
 * This is an internal function that is exposed under different
Packit fb9d21
 * names depending on whether the base expressions have a zero default
Packit fb9d21
 * value.
Packit fb9d21
 * If they do, then this function is called "add".
Packit fb9d21
 * Otherwise, it is called "union_add".
Packit fb9d21
 */
Packit fb9d21
static __isl_give UNION *FN(UNION,union_add_)(__isl_take UNION *u1,
Packit fb9d21
	__isl_take UNION *u2)
Packit fb9d21
{
Packit fb9d21
	u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
Packit fb9d21
	u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
Packit fb9d21
Packit fb9d21
	u1 = FN(UNION,cow)(u1);
Packit fb9d21
Packit fb9d21
	if (!u1 || !u2)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	if (FN(FN(UNION,foreach),PARTS)(u2, &FN(UNION,union_add_part), &u1) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	FN(UNION,free)(u2);
Packit fb9d21
Packit fb9d21
	return u1;
Packit fb9d21
error:
Packit fb9d21
	FN(UNION,free)(u1);
Packit fb9d21
	FN(UNION,free)(u2);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give UNION *FN(FN(UNION,from),PARTS)(__isl_take PART *part)
Packit fb9d21
{
Packit fb9d21
	isl_space *dim;
Packit fb9d21
	UNION *u;
Packit fb9d21
Packit fb9d21
	if (!part)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	dim = FN(PART,get_space)(part);
Packit fb9d21
	dim = isl_space_drop_dims(dim, isl_dim_in, 0, isl_space_dim(dim, isl_dim_in));
Packit fb9d21
	dim = isl_space_drop_dims(dim, isl_dim_out, 0, isl_space_dim(dim, isl_dim_out));
Packit fb9d21
#ifdef HAS_TYPE
Packit fb9d21
	u = FN(UNION,ZERO)(dim, part->type);
Packit fb9d21
#else
Packit fb9d21
	u = FN(UNION,ZERO)(dim);
Packit fb9d21
#endif
Packit fb9d21
	u = FN(FN(UNION,add),PARTS)(u, part);
Packit fb9d21
Packit fb9d21
	return u;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
S(UNION,match_bin_data) {
Packit fb9d21
	UNION *u2;
Packit fb9d21
	UNION *res;
Packit fb9d21
	__isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *);
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Check if data->u2 has an element living in the same space as "part".
Packit fb9d21
 * If so, call data->fn on the two elements and add the result to
Packit fb9d21
 * data->res.
Packit fb9d21
 */
Packit fb9d21
static isl_stat FN(UNION,match_bin_entry)(__isl_take PART *part, void *user)
Packit fb9d21
{
Packit fb9d21
	S(UNION,match_bin_data) *data = user;
Packit fb9d21
	struct isl_hash_table_entry *entry2;
Packit fb9d21
	isl_space *space;
Packit fb9d21
	PART *part2;
Packit fb9d21
Packit fb9d21
	space = FN(PART,get_space)(part);
Packit fb9d21
	entry2 = FN(UNION,find_part_entry)(data->u2, space, 0);
Packit fb9d21
	isl_space_free(space);
Packit fb9d21
	if (!entry2)
Packit fb9d21
		goto error;
Packit fb9d21
	if (entry2 == isl_hash_table_entry_none) {
Packit fb9d21
		FN(PART,free)(part);
Packit fb9d21
		return isl_stat_ok;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	part2 = entry2->data;
Packit fb9d21
	if (!isl_space_tuple_is_equal(part->dim, isl_dim_out,
Packit fb9d21
					part2->dim, isl_dim_out))
Packit fb9d21
		isl_die(FN(UNION,get_ctx)(data->u2), isl_error_invalid,
Packit fb9d21
			"entries should have the same range space",
Packit fb9d21
			goto error);
Packit fb9d21
Packit fb9d21
	part = data->fn(part, FN(PART, copy)(entry2->data));
Packit fb9d21
Packit fb9d21
	data->res = FN(FN(UNION,add),PARTS)(data->res, part);
Packit fb9d21
	if (!data->res)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
error:
Packit fb9d21
	FN(PART,free)(part);
Packit fb9d21
	return isl_stat_error;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* This function is currently only used from isl_polynomial.c
Packit fb9d21
 * and not from isl_fold.c.
Packit fb9d21
 */
Packit fb9d21
static __isl_give UNION *FN(UNION,match_bin_op)(__isl_take UNION *u1,
Packit fb9d21
	__isl_take UNION *u2,
Packit fb9d21
	__isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *))
Packit fb9d21
	__attribute__ ((unused));
Packit fb9d21
/* For each pair of elements in "u1" and "u2" living in the same space,
Packit fb9d21
 * call "fn" and collect the results.
Packit fb9d21
 */
Packit fb9d21
static __isl_give UNION *FN(UNION,match_bin_op)(__isl_take UNION *u1,
Packit fb9d21
	__isl_take UNION *u2,
Packit fb9d21
	__isl_give PART *(*fn)(__isl_take PART *, __isl_take PART *))
Packit fb9d21
{
Packit fb9d21
	S(UNION,match_bin_data) data = { NULL, NULL, fn };
Packit fb9d21
Packit fb9d21
	u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
Packit fb9d21
	u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
Packit fb9d21
Packit fb9d21
	if (!u1 || !u2)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	data.u2 = u2;
Packit fb9d21
	data.res = FN(UNION,alloc_same_size)(u1);
Packit fb9d21
	if (FN(FN(UNION,foreach),PARTS)(u1,
Packit fb9d21
				    &FN(UNION,match_bin_entry), &data) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	FN(UNION,free)(u1);
Packit fb9d21
	FN(UNION,free)(u2);
Packit fb9d21
	return data.res;
Packit fb9d21
error:
Packit fb9d21
	FN(UNION,free)(u1);
Packit fb9d21
	FN(UNION,free)(u2);
Packit fb9d21
	FN(UNION,free)(data.res);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the sum of "u1" and "u2".
Packit fb9d21
 *
Packit fb9d21
 * If the base expressions have a default zero value, then the sum
Packit fb9d21
 * is computed on the union of the domains of "u1" and "u2".
Packit fb9d21
 * Otherwise, it is computed on their shared domains.
Packit fb9d21
 */
Packit fb9d21
__isl_give UNION *FN(UNION,add)(__isl_take UNION *u1, __isl_take UNION *u2)
Packit fb9d21
{
Packit fb9d21
#if DEFAULT_IS_ZERO
Packit fb9d21
	return FN(UNION,union_add_)(u1, u2);
Packit fb9d21
#else
Packit fb9d21
	return FN(UNION,match_bin_op)(u1, u2, &FN(PART,add));
Packit fb9d21
#endif
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
#ifndef NO_SUB
Packit fb9d21
/* Subtract "u2" from "u1" and return the result.
Packit fb9d21
 */
Packit fb9d21
__isl_give UNION *FN(UNION,sub)(__isl_take UNION *u1, __isl_take UNION *u2)
Packit fb9d21
{
Packit fb9d21
	return FN(UNION,match_bin_op)(u1, u2, &FN(PART,sub));
Packit fb9d21
}
Packit fb9d21
#endif
Packit fb9d21
Packit fb9d21
S(UNION,any_set_data) {
Packit fb9d21
	isl_set *set;
Packit fb9d21
	__isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
static __isl_give PART *FN(UNION,any_set_entry)(__isl_take PART *part,
Packit fb9d21
	void *user)
Packit fb9d21
{
Packit fb9d21
	S(UNION,any_set_data) *data = user;
Packit fb9d21
Packit fb9d21
	return data->fn(part, isl_set_copy(data->set));
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Update each element of "u" by calling "fn" on the element and "set".
Packit fb9d21
 */
Packit fb9d21
static __isl_give UNION *FN(UNION,any_set_op)(__isl_take UNION *u,
Packit fb9d21
	__isl_take isl_set *set,
Packit fb9d21
	__isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*))
Packit fb9d21
{
Packit fb9d21
	S(UNION,any_set_data) data = { NULL, fn };
Packit fb9d21
Packit fb9d21
	u = FN(UNION,align_params)(u, isl_set_get_space(set));
Packit fb9d21
	set = isl_set_align_params(set, FN(UNION,get_space)(u));
Packit fb9d21
Packit fb9d21
	if (!u || !set)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	data.set = set;
Packit fb9d21
	u = FN(UNION,transform)(u, &FN(UNION,any_set_entry), &data);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return u;
Packit fb9d21
error:
Packit fb9d21
	FN(UNION,free)(u);
Packit fb9d21
	isl_set_free(set);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Intersect the domain of "u" with the parameter domain "context".
Packit fb9d21
 */
Packit fb9d21
__isl_give UNION *FN(UNION,intersect_params)(__isl_take UNION *u,
Packit fb9d21
	__isl_take isl_set *set)
Packit fb9d21
{
Packit fb9d21
	return FN(UNION,any_set_op)(u, set, &FN(PW,intersect_params));
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Compute the gist of the domain of "u" with respect to
Packit fb9d21
 * the parameter domain "context".
Packit fb9d21
 */
Packit fb9d21
__isl_give UNION *FN(UNION,gist_params)(__isl_take UNION *u,
Packit fb9d21
	__isl_take isl_set *set)
Packit fb9d21
{
Packit fb9d21
	return FN(UNION,any_set_op)(u, set, &FN(PW,gist_params));
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
S(UNION,match_domain_data) {
Packit fb9d21
	isl_union_set *uset;
Packit fb9d21
	UNION *res;
Packit fb9d21
	__isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*);
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
static int FN(UNION,set_has_dim)(const void *entry, const void *val)
Packit fb9d21
{
Packit fb9d21
	isl_set *set = (isl_set *)entry;
Packit fb9d21
	isl_space *dim = (isl_space *)val;
Packit fb9d21
Packit fb9d21
	return isl_space_is_equal(set->dim, dim);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Find the set in data->uset that lives in the same space as the domain
Packit fb9d21
 * of "part", apply data->fn to *entry and this set (if any), and add
Packit fb9d21
 * the result to data->res.
Packit fb9d21
 */
Packit fb9d21
static isl_stat FN(UNION,match_domain_entry)(__isl_take PART *part, void *user)
Packit fb9d21
{
Packit fb9d21
	S(UNION,match_domain_data) *data = user;
Packit fb9d21
	uint32_t hash;
Packit fb9d21
	struct isl_hash_table_entry *entry2;
Packit fb9d21
	isl_space *space;
Packit fb9d21
Packit fb9d21
	space = FN(PART,get_domain_space)(part);
Packit fb9d21
	hash = isl_space_get_hash(space);
Packit fb9d21
	entry2 = isl_hash_table_find(data->uset->dim->ctx, &data->uset->table,
Packit fb9d21
				     hash, &FN(UNION,set_has_dim), space, 0);
Packit fb9d21
	isl_space_free(space);
Packit fb9d21
	if (!entry2) {
Packit fb9d21
		FN(PART,free)(part);
Packit fb9d21
		return isl_stat_ok;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	part = data->fn(part, isl_set_copy(entry2->data));
Packit fb9d21
Packit fb9d21
	data->res = FN(FN(UNION,add),PARTS)(data->res, part);
Packit fb9d21
	if (!data->res)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Apply fn to each pair of PW in u and set in uset such that
Packit fb9d21
 * the set lives in the same space as the domain of PW
Packit fb9d21
 * and collect the results.
Packit fb9d21
 */
Packit fb9d21
static __isl_give UNION *FN(UNION,match_domain_op)(__isl_take UNION *u,
Packit fb9d21
	__isl_take isl_union_set *uset,
Packit fb9d21
	__isl_give PW *(*fn)(__isl_take PW*, __isl_take isl_set*))
Packit fb9d21
{
Packit fb9d21
	S(UNION,match_domain_data) data = { NULL, NULL, fn };
Packit fb9d21
Packit fb9d21
	u = FN(UNION,align_params)(u, isl_union_set_get_space(uset));
Packit fb9d21
	uset = isl_union_set_align_params(uset, FN(UNION,get_space)(u));
Packit fb9d21
Packit fb9d21
	if (!u || !uset)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	data.uset = uset;
Packit fb9d21
	data.res = FN(UNION,alloc_same_size)(u);
Packit fb9d21
	if (FN(FN(UNION,foreach),PARTS)(u,
Packit fb9d21
				   &FN(UNION,match_domain_entry), &data) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	FN(UNION,free)(u);
Packit fb9d21
	isl_union_set_free(uset);
Packit fb9d21
	return data.res;
Packit fb9d21
error:
Packit fb9d21
	FN(UNION,free)(u);
Packit fb9d21
	isl_union_set_free(uset);
Packit fb9d21
	FN(UNION,free)(data.res);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Intersect the domain of "u" with "uset".
Packit fb9d21
 * If "uset" is a parameters domain, then intersect the parameter
Packit fb9d21
 * domain of "u" with this set.
Packit fb9d21
 */
Packit fb9d21
__isl_give UNION *FN(UNION,intersect_domain)(__isl_take UNION *u,
Packit fb9d21
	__isl_take isl_union_set *uset)
Packit fb9d21
{
Packit fb9d21
	if (isl_union_set_is_params(uset))
Packit fb9d21
		return FN(UNION,intersect_params)(u,
Packit fb9d21
						isl_set_from_union_set(uset));
Packit fb9d21
	return FN(UNION,match_domain_op)(u, uset, &FN(PW,intersect_domain));
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Take the set (which may be empty) in data->uset that lives
Packit fb9d21
 * in the same space as the domain of "pw", subtract it from the domain
Packit fb9d21
 * of "part" and return the result.
Packit fb9d21
 */
Packit fb9d21
static __isl_give PART *FN(UNION,subtract_domain_entry)(__isl_take PART *part,
Packit fb9d21
	void *user)
Packit fb9d21
{
Packit fb9d21
	isl_union_set *uset = user;
Packit fb9d21
	isl_space *space;
Packit fb9d21
	isl_set *set;
Packit fb9d21
Packit fb9d21
	space = FN(PART,get_domain_space)(part);
Packit fb9d21
	set = isl_union_set_extract_set(uset, space);
Packit fb9d21
	return FN(PART,subtract_domain)(part, set);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Subtract "uset' from the domain of "u".
Packit fb9d21
 */
Packit fb9d21
__isl_give UNION *FN(UNION,subtract_domain)(__isl_take UNION *u,
Packit fb9d21
	__isl_take isl_union_set *uset)
Packit fb9d21
{
Packit fb9d21
	u = FN(UNION,transform)(u, &FN(UNION,subtract_domain_entry), uset);
Packit fb9d21
	isl_union_set_free(uset);
Packit fb9d21
	return u;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give UNION *FN(UNION,gist)(__isl_take UNION *u,
Packit fb9d21
	__isl_take isl_union_set *uset)
Packit fb9d21
{
Packit fb9d21
	if (isl_union_set_is_params(uset))
Packit fb9d21
		return FN(UNION,gist_params)(u, isl_set_from_union_set(uset));
Packit fb9d21
	return FN(UNION,match_domain_op)(u, uset, &FN(PW,gist));
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Coalesce an entry in a UNION.  Coalescing is performed in-place.
Packit fb9d21
 * Since the UNION may have several references, the entry is only
Packit fb9d21
 * replaced if the coalescing is successful.
Packit fb9d21
 */
Packit fb9d21
static isl_stat FN(UNION,coalesce_entry)(void **entry, void *user)
Packit fb9d21
{
Packit fb9d21
	PART **part_p = (PART **) entry;
Packit fb9d21
	PART *part;
Packit fb9d21
Packit fb9d21
	part = FN(PART,copy)(*part_p);
Packit fb9d21
	part = FN(PW,coalesce)(part);
Packit fb9d21
	if (!part)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
	FN(PART,free)(*part_p);
Packit fb9d21
	*part_p = part;
Packit fb9d21
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give UNION *FN(UNION,coalesce)(__isl_take UNION *u)
Packit fb9d21
{
Packit fb9d21
	if (FN(UNION,foreach_inplace)(u, &FN(UNION,coalesce_entry), NULL) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	return u;
Packit fb9d21
error:
Packit fb9d21
	FN(UNION,free)(u);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
static isl_stat FN(UNION,domain_entry)(__isl_take PART *part, void *user)
Packit fb9d21
{
Packit fb9d21
	isl_union_set **uset = (isl_union_set **)user;
Packit fb9d21
Packit fb9d21
	*uset = isl_union_set_add_set(*uset, FN(PART,domain)(part));
Packit fb9d21
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give isl_union_set *FN(UNION,domain)(__isl_take UNION *u)
Packit fb9d21
{
Packit fb9d21
	isl_union_set *uset;
Packit fb9d21
Packit fb9d21
	uset = isl_union_set_empty(FN(UNION,get_space)(u));
Packit fb9d21
	if (FN(FN(UNION,foreach),PARTS)(u, &FN(UNION,domain_entry), &uset) < 0)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	FN(UNION,free)(u);
Packit fb9d21
	
Packit fb9d21
	return uset;
Packit fb9d21
error:
Packit fb9d21
	isl_union_set_free(uset);
Packit fb9d21
	FN(UNION,free)(u);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
#ifdef HAS_TYPE
Packit fb9d21
/* Negate the type of "u".
Packit fb9d21
 */
Packit fb9d21
static __isl_give UNION *FN(UNION,negate_type)(__isl_take UNION *u)
Packit fb9d21
{
Packit fb9d21
	u = FN(UNION,cow)(u);
Packit fb9d21
	if (!u)
Packit fb9d21
		return NULL;
Packit fb9d21
	u->type = isl_fold_type_negate(u->type);
Packit fb9d21
	return u;
Packit fb9d21
}
Packit fb9d21
#else
Packit fb9d21
/* Negate the type of "u".
Packit fb9d21
 * Since "u" does not have a type, do nothing.
Packit fb9d21
 */
Packit fb9d21
static __isl_give UNION *FN(UNION,negate_type)(__isl_take UNION *u)
Packit fb9d21
{
Packit fb9d21
	return u;
Packit fb9d21
}
Packit fb9d21
#endif
Packit fb9d21
Packit fb9d21
static __isl_give PART *FN(UNION,mul_isl_int_entry)(__isl_take PART *part,
Packit fb9d21
	void *user)
Packit fb9d21
{
Packit fb9d21
	isl_int *v = user;
Packit fb9d21
Packit fb9d21
	return FN(PW,mul_isl_int)(part, *v);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
__isl_give UNION *FN(UNION,mul_isl_int)(__isl_take UNION *u, isl_int v)
Packit fb9d21
{
Packit fb9d21
	if (isl_int_is_one(v))
Packit fb9d21
		return u;
Packit fb9d21
Packit fb9d21
	if (DEFAULT_IS_ZERO && u && isl_int_is_zero(v)) {
Packit fb9d21
		UNION *zero;
Packit fb9d21
		isl_space *dim = FN(UNION,get_space)(u);
Packit fb9d21
#ifdef HAS_TYPE
Packit fb9d21
		zero = FN(UNION,ZERO)(dim, u->type);
Packit fb9d21
#else
Packit fb9d21
		zero = FN(UNION,ZERO)(dim);
Packit fb9d21
#endif
Packit fb9d21
		FN(UNION,free)(u);
Packit fb9d21
		return zero;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	u = FN(UNION,transform_inplace)(u, &FN(UNION,mul_isl_int_entry), &v);
Packit fb9d21
	if (isl_int_is_neg(v))
Packit fb9d21
		u = FN(UNION,negate_type)(u);
Packit fb9d21
Packit fb9d21
	return u;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Multiply "part" by the isl_val "user" and return the result.
Packit fb9d21
 */
Packit fb9d21
static __isl_give PART *FN(UNION,scale_val_entry)(__isl_take PART *part,
Packit fb9d21
	void *user)
Packit fb9d21
{
Packit fb9d21
	isl_val *v = user;
Packit fb9d21
Packit fb9d21
	return FN(PART,scale_val)(part, isl_val_copy(v));
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Multiply "u" by "v" and return the result.
Packit fb9d21
 */
Packit fb9d21
__isl_give UNION *FN(UNION,scale_val)(__isl_take UNION *u,
Packit fb9d21
	__isl_take isl_val *v)
Packit fb9d21
{
Packit fb9d21
	if (!u || !v)
Packit fb9d21
		goto error;
Packit fb9d21
	if (isl_val_is_one(v)) {
Packit fb9d21
		isl_val_free(v);
Packit fb9d21
		return u;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	if (DEFAULT_IS_ZERO && u && isl_val_is_zero(v)) {
Packit fb9d21
		UNION *zero;
Packit fb9d21
		isl_space *space = FN(UNION,get_space)(u);
Packit fb9d21
#ifdef HAS_TYPE
Packit fb9d21
		zero = FN(UNION,ZERO)(space, u->type);
Packit fb9d21
#else
Packit fb9d21
		zero = FN(UNION,ZERO)(space);
Packit fb9d21
#endif
Packit fb9d21
		FN(UNION,free)(u);
Packit fb9d21
		isl_val_free(v);
Packit fb9d21
		return zero;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	if (!isl_val_is_rat(v))
Packit fb9d21
		isl_die(isl_val_get_ctx(v), isl_error_invalid,
Packit fb9d21
			"expecting rational factor", goto error);
Packit fb9d21
Packit fb9d21
	u = FN(UNION,transform_inplace)(u, &FN(UNION,scale_val_entry), v);
Packit fb9d21
	if (isl_val_is_neg(v))
Packit fb9d21
		u = FN(UNION,negate_type)(u);
Packit fb9d21
Packit fb9d21
	isl_val_free(v);
Packit fb9d21
	return u;
Packit fb9d21
error:
Packit fb9d21
	isl_val_free(v);
Packit fb9d21
	FN(UNION,free)(u);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Divide "part" by the isl_val "user" and return the result.
Packit fb9d21
 */
Packit fb9d21
static __isl_give PART *FN(UNION,scale_down_val_entry)(__isl_take PART *part,
Packit fb9d21
	void *user)
Packit fb9d21
{
Packit fb9d21
	isl_val *v = user;
Packit fb9d21
Packit fb9d21
	return FN(PART,scale_down_val)(part, isl_val_copy(v));
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Divide "u" by "v" and return the result.
Packit fb9d21
 */
Packit fb9d21
__isl_give UNION *FN(UNION,scale_down_val)(__isl_take UNION *u,
Packit fb9d21
	__isl_take isl_val *v)
Packit fb9d21
{
Packit fb9d21
	if (!u || !v)
Packit fb9d21
		goto error;
Packit fb9d21
	if (isl_val_is_one(v)) {
Packit fb9d21
		isl_val_free(v);
Packit fb9d21
		return u;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	if (!isl_val_is_rat(v))
Packit fb9d21
		isl_die(isl_val_get_ctx(v), isl_error_invalid,
Packit fb9d21
			"expecting rational factor", goto error);
Packit fb9d21
	if (isl_val_is_zero(v))
Packit fb9d21
		isl_die(isl_val_get_ctx(v), isl_error_invalid,
Packit fb9d21
			"cannot scale down by zero", goto error);
Packit fb9d21
Packit fb9d21
	u = FN(UNION,transform_inplace)(u, &FN(UNION,scale_down_val_entry), v);
Packit fb9d21
	if (isl_val_is_neg(v))
Packit fb9d21
		u = FN(UNION,negate_type)(u);
Packit fb9d21
Packit fb9d21
	isl_val_free(v);
Packit fb9d21
	return u;
Packit fb9d21
error:
Packit fb9d21
	isl_val_free(v);
Packit fb9d21
	FN(UNION,free)(u);
Packit fb9d21
	return NULL;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
S(UNION,plain_is_equal_data)
Packit fb9d21
{
Packit fb9d21
	UNION *u2;
Packit fb9d21
	isl_bool is_equal;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
static isl_stat FN(UNION,plain_is_equal_entry)(void **entry, void *user)
Packit fb9d21
{
Packit fb9d21
	S(UNION,plain_is_equal_data) *data = user;
Packit fb9d21
	struct isl_hash_table_entry *entry2;
Packit fb9d21
	PW *pw = *entry;
Packit fb9d21
Packit fb9d21
	entry2 = FN(UNION,find_part_entry)(data->u2, pw->dim, 0);
Packit fb9d21
	if (!entry2 || entry2 == isl_hash_table_entry_none) {
Packit fb9d21
		if (!entry2)
Packit fb9d21
			data->is_equal = isl_bool_error;
Packit fb9d21
		else
Packit fb9d21
			data->is_equal = isl_bool_false;
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
	}
Packit fb9d21
Packit fb9d21
	data->is_equal = FN(PW,plain_is_equal)(pw, entry2->data);
Packit fb9d21
	if (data->is_equal < 0 || !data->is_equal)
Packit fb9d21
		return isl_stat_error;
Packit fb9d21
Packit fb9d21
	return isl_stat_ok;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
isl_bool FN(UNION,plain_is_equal)(__isl_keep UNION *u1, __isl_keep UNION *u2)
Packit fb9d21
{
Packit fb9d21
	S(UNION,plain_is_equal_data) data = { NULL, isl_bool_true };
Packit fb9d21
	int n1, n2;
Packit fb9d21
Packit fb9d21
	if (!u1 || !u2)
Packit fb9d21
		return isl_bool_error;
Packit fb9d21
	if (u1 == u2)
Packit fb9d21
		return isl_bool_true;
Packit fb9d21
	if (u1->table.n != u2->table.n)
Packit fb9d21
		return isl_bool_false;
Packit fb9d21
	n1 = FN(FN(UNION,n),PARTS)(u1);
Packit fb9d21
	n2 = FN(FN(UNION,n),PARTS)(u2);
Packit fb9d21
	if (n1 < 0 || n2 < 0)
Packit fb9d21
		return isl_bool_error;
Packit fb9d21
	if (n1 != n2)
Packit fb9d21
		return isl_bool_false;
Packit fb9d21
Packit fb9d21
	u1 = FN(UNION,copy)(u1);
Packit fb9d21
	u2 = FN(UNION,copy)(u2);
Packit fb9d21
	u1 = FN(UNION,align_params)(u1, FN(UNION,get_space)(u2));
Packit fb9d21
	u2 = FN(UNION,align_params)(u2, FN(UNION,get_space)(u1));
Packit fb9d21
	if (!u1 || !u2)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	data.u2 = u2;
Packit fb9d21
	if (FN(UNION,foreach_inplace)(u1,
Packit fb9d21
			       &FN(UNION,plain_is_equal_entry), &data) < 0 &&
Packit fb9d21
	    data.is_equal)
Packit fb9d21
		goto error;
Packit fb9d21
Packit fb9d21
	FN(UNION,free)(u1);
Packit fb9d21
	FN(UNION,free)(u2);
Packit fb9d21
Packit fb9d21
	return data.is_equal;
Packit fb9d21
error:
Packit fb9d21
	FN(UNION,free)(u1);
Packit fb9d21
	FN(UNION,free)(u2);
Packit fb9d21
	return isl_bool_error;
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Internal data structure for isl_union_*_drop_dims.
Packit fb9d21
 * type, first and n are passed to isl_*_drop_dims.
Packit fb9d21
 */
Packit fb9d21
S(UNION,drop_dims_data) {
Packit fb9d21
	enum isl_dim_type type;
Packit fb9d21
	unsigned first;
Packit fb9d21
	unsigned n;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Drop the parameters specified by "data" from "part" and return the result.
Packit fb9d21
 */
Packit fb9d21
static __isl_give PART *FN(UNION,drop_dims_entry)(__isl_take PART *part,
Packit fb9d21
	void *user)
Packit fb9d21
{
Packit fb9d21
	S(UNION,drop_dims_data) *data = user;
Packit fb9d21
Packit fb9d21
	return FN(PART,drop_dims)(part, data->type, data->first, data->n);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Drop the specified parameters from "u".
Packit fb9d21
 * That is, type is required to be isl_dim_param.
Packit fb9d21
 */
Packit fb9d21
__isl_give UNION *FN(UNION,drop_dims)( __isl_take UNION *u,
Packit fb9d21
	enum isl_dim_type type, unsigned first, unsigned n)
Packit fb9d21
{
Packit fb9d21
	isl_space *space;
Packit fb9d21
	S(UNION,drop_dims_data) data = { type, first, n };
Packit fb9d21
Packit fb9d21
	if (!u)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (type != isl_dim_param)
Packit fb9d21
		isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
Packit fb9d21
			"can only project out parameters",
Packit fb9d21
			return FN(UNION,free)(u));
Packit fb9d21
Packit fb9d21
	space = FN(UNION,get_space)(u);
Packit fb9d21
	space = isl_space_drop_dims(space, type, first, n);
Packit fb9d21
	return FN(UNION,transform_space)(u, space, &FN(UNION,drop_dims_entry),
Packit fb9d21
					&data);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Internal data structure for isl_union_*_set_dim_name.
Packit fb9d21
 * pos is the position of the parameter that needs to be renamed.
Packit fb9d21
 * s is the new name.
Packit fb9d21
 */
Packit fb9d21
S(UNION,set_dim_name_data) {
Packit fb9d21
	unsigned pos;
Packit fb9d21
	const char *s;
Packit fb9d21
};
Packit fb9d21
Packit fb9d21
/* Change the name of the parameter at position data->pos of "part" to data->s
Packit fb9d21
 * and return the result.
Packit fb9d21
 */
Packit fb9d21
static __isl_give PART *FN(UNION,set_dim_name_entry)(__isl_take PART *part,
Packit fb9d21
	void *user)
Packit fb9d21
{
Packit fb9d21
	S(UNION,set_dim_name_data) *data = user;
Packit fb9d21
Packit fb9d21
	return FN(PART,set_dim_name)(part, isl_dim_param, data->pos, data->s);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Change the name of the parameter at position "pos" to "s".
Packit fb9d21
 * That is, type is required to be isl_dim_param.
Packit fb9d21
 */
Packit fb9d21
__isl_give UNION *FN(UNION,set_dim_name)(__isl_take UNION *u,
Packit fb9d21
	enum isl_dim_type type, unsigned pos, const char *s)
Packit fb9d21
{
Packit fb9d21
	S(UNION,set_dim_name_data) data = { pos, s };
Packit fb9d21
	isl_space *space;
Packit fb9d21
Packit fb9d21
	if (!u)
Packit fb9d21
		return NULL;
Packit fb9d21
Packit fb9d21
	if (type != isl_dim_param)
Packit fb9d21
		isl_die(FN(UNION,get_ctx)(u), isl_error_invalid,
Packit fb9d21
			"can only set parameter names",
Packit fb9d21
			return FN(UNION,free)(u));
Packit fb9d21
Packit fb9d21
	space = FN(UNION,get_space)(u);
Packit fb9d21
	space = isl_space_set_dim_name(space, type, pos, s);
Packit fb9d21
	return FN(UNION,transform_space)(u, space,
Packit fb9d21
					&FN(UNION,set_dim_name_entry), &data);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Reset the user pointer on all identifiers of parameters and tuples
Packit fb9d21
 * of the space of "part" and return the result.
Packit fb9d21
 */
Packit fb9d21
static __isl_give PART *FN(UNION,reset_user_entry)(__isl_take PART *part,
Packit fb9d21
	void *user)
Packit fb9d21
{
Packit fb9d21
	return FN(PART,reset_user)(part);
Packit fb9d21
}
Packit fb9d21
Packit fb9d21
/* Reset the user pointer on all identifiers of parameters and tuples
Packit fb9d21
 * of the spaces of "u".
Packit fb9d21
 */
Packit fb9d21
__isl_give UNION *FN(UNION,reset_user)(__isl_take UNION *u)
Packit fb9d21
{
Packit fb9d21
	isl_space *space;
Packit fb9d21
Packit fb9d21
	space = FN(UNION,get_space)(u);
Packit fb9d21
	space = isl_space_reset_user(space);
Packit fb9d21
	return FN(UNION,transform_space)(u, space, &FN(UNION,reset_user_entry),
Packit fb9d21
					NULL);
Packit fb9d21
}