1/*
2 * Copyright 2012      Ecole Normale Superieure
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
8 */
9
10#include <isl/space.h>
11
12#include <isl_multi_macro.h>
13
14/* Given two MULTI(BASE)s A -> B and C -> D,
15 * construct a MULTI(BASE) [A -> C] -> [B -> D].
16 *
17 * If "multi1" and/or "multi2" has an explicit domain, then
18 * intersect the domain of the result with these explicit domains.
19 */
20__isl_give MULTI(BASE) *FN(MULTI(BASE),product)(
21	__isl_take MULTI(BASE) *multi1, __isl_take MULTI(BASE) *multi2)
22{
23	int i;
24	EL *el;
25	isl_space *space;
26	MULTI(BASE) *res;
27	isl_size in1, in2, out1, out2;
28
29	FN(MULTI(BASE),align_params_bin)(&multi1, &multi2);
30	in1 = FN(MULTI(BASE),dim)(multi1, isl_dim_in);
31	in2 = FN(MULTI(BASE),dim)(multi2, isl_dim_in);
32	out1 = FN(MULTI(BASE),dim)(multi1, isl_dim_out);
33	out2 = FN(MULTI(BASE),dim)(multi2, isl_dim_out);
34	if (in1 < 0 || in2 < 0 || out1 < 0 || out2 < 0)
35		goto error;
36	space = isl_space_product(FN(MULTI(BASE),get_space)(multi1),
37				  FN(MULTI(BASE),get_space)(multi2));
38	res = FN(MULTI(BASE),alloc)(isl_space_copy(space));
39	space = isl_space_domain(space);
40
41	for (i = 0; i < out1; ++i) {
42		el = FN(FN(MULTI(BASE),get),BASE)(multi1, i);
43		el = FN(EL,insert_dims)(el, isl_dim_in, in1, in2);
44		el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
45		res = FN(FN(MULTI(BASE),set),BASE)(res, i, el);
46	}
47
48	for (i = 0; i < out2; ++i) {
49		el = FN(FN(MULTI(BASE),get),BASE)(multi2, i);
50		el = FN(EL,insert_dims)(el, isl_dim_in, 0, in1);
51		el = FN(EL,reset_domain_space)(el, isl_space_copy(space));
52		res = FN(FN(MULTI(BASE),set),BASE)(res, out1 + i, el);
53	}
54
55	if (FN(MULTI(BASE),has_explicit_domain)(multi1) ||
56	    FN(MULTI(BASE),has_explicit_domain)(multi2))
57		res = FN(MULTI(BASE),intersect_explicit_domain_product)(res,
58								multi1, multi2);
59
60	isl_space_free(space);
61	FN(MULTI(BASE),free)(multi1);
62	FN(MULTI(BASE),free)(multi2);
63	return res;
64error:
65	FN(MULTI(BASE),free)(multi1);
66	FN(MULTI(BASE),free)(multi2);
67	return NULL;
68}
69