isl_tab_pip.c: sol_for_add: correctly set denominator
[isl.git] / isl_reordering.c
blob392152f92352319915b6af27e5f0664bfb9efd24
1 /*
2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8 * 91893 Orsay, France
9 */
11 #include <isl_ctx_private.h>
12 #include <isl_dim_private.h>
13 #include <isl_reordering.h>
15 __isl_give isl_reordering *isl_reordering_alloc(isl_ctx *ctx, int len)
17 isl_reordering *exp;
19 exp = isl_alloc(ctx, struct isl_reordering,
20 sizeof(struct isl_reordering) + (len - 1) * sizeof(int));
21 if (!exp)
22 return NULL;
24 exp->ref = 1;
25 exp->len = len;
26 exp->dim = NULL;
28 return exp;
31 __isl_give isl_reordering *isl_reordering_copy(__isl_keep isl_reordering *exp)
33 if (!exp)
34 return NULL;
36 exp->ref++;
37 return exp;
40 __isl_give isl_reordering *isl_reordering_dup(__isl_keep isl_reordering *r)
42 int i;
43 isl_reordering *dup;
45 if (!r)
46 return NULL;
48 dup = isl_reordering_alloc(r->dim->ctx, r->len);
49 if (!dup)
50 return NULL;
52 dup->dim = isl_dim_copy(r->dim);
53 if (!dup->dim)
54 return isl_reordering_free(dup);
55 for (i = 0; i < dup->len; ++i)
56 dup->pos[i] = r->pos[i];
58 return dup;
61 __isl_give isl_reordering *isl_reordering_cow(__isl_take isl_reordering *r)
63 if (!r)
64 return NULL;
66 if (r->ref == 1)
67 return r;
68 r->ref--;
69 return isl_reordering_dup(r);
72 void *isl_reordering_free(__isl_take isl_reordering *exp)
74 if (!exp)
75 return NULL;
77 if (--exp->ref > 0)
78 return NULL;
80 isl_dim_free(exp->dim);
81 free(exp);
82 return NULL;
85 /* Construct a reordering that maps the parameters of "alignee"
86 * to the corresponding parameters in a new dimension specification
87 * that has the parameters of "aligner" first, followed by
88 * any remaining parameters of "alignee" that do not occur in "aligner".
90 __isl_give isl_reordering *isl_parameter_alignment_reordering(
91 __isl_keep isl_dim *alignee, __isl_keep isl_dim *aligner)
93 int i, j;
94 isl_reordering *exp;
96 if (!alignee || !aligner)
97 return NULL;
99 exp = isl_reordering_alloc(alignee->ctx, alignee->nparam);
100 if (!exp)
101 return NULL;
103 exp->dim = isl_dim_copy(aligner);
105 for (i = 0; i < alignee->nparam; ++i) {
106 const char *name_i;
107 name_i = isl_dim_get_name(alignee, isl_dim_param, i);
108 if (!name_i)
109 isl_die(alignee->ctx, isl_error_invalid,
110 "cannot align unnamed parameters", goto error);
111 for (j = 0; j < aligner->nparam; ++j) {
112 const char *name_j;
113 name_j = isl_dim_get_name(aligner, isl_dim_param, j);
114 if (name_i == name_j)
115 break;
117 if (j < aligner->nparam)
118 exp->pos[i] = j;
119 else {
120 int pos;
121 pos = isl_dim_size(exp->dim, isl_dim_param);
122 exp->dim = isl_dim_add(exp->dim, isl_dim_param, 1);
123 exp->dim = isl_dim_set_name(exp->dim,
124 isl_dim_param, pos, name_i);
125 exp->pos[i] = pos;
129 return exp;
130 error:
131 isl_reordering_free(exp);
132 return NULL;
135 __isl_give isl_reordering *isl_reordering_extend(__isl_take isl_reordering *exp,
136 unsigned extra)
138 int i;
139 isl_reordering *res;
140 int offset;
142 if (!exp)
143 return NULL;
144 if (extra == 0)
145 return exp;
147 offset = isl_dim_total(exp->dim) - exp->len;
148 res = isl_reordering_alloc(exp->dim->ctx, exp->len + extra);
149 if (!res)
150 goto error;
151 res->dim = isl_dim_copy(exp->dim);
152 for (i = 0; i < exp->len; ++i)
153 res->pos[i] = exp->pos[i];
154 for (i = exp->len; i < res->len; ++i)
155 res->pos[i] = offset + i;
157 isl_reordering_free(exp);
159 return res;
160 error:
161 isl_reordering_free(exp);
162 return NULL;
165 __isl_give isl_reordering *isl_reordering_extend_dim(
166 __isl_take isl_reordering *exp, __isl_take isl_dim *dim)
168 isl_reordering *res;
170 if (!exp || !dim)
171 goto error;
173 res = isl_reordering_extend(isl_reordering_copy(exp),
174 isl_dim_total(dim) - exp->len);
175 res = isl_reordering_cow(res);
176 if (!res)
177 goto error;
178 isl_dim_free(res->dim);
179 res->dim = isl_dim_replace(dim, isl_dim_param, exp->dim);
181 isl_reordering_free(exp);
183 return res;
184 error:
185 isl_reordering_free(exp);
186 isl_dim_free(dim);
187 return NULL;
190 void isl_reordering_dump(__isl_keep isl_reordering *exp)
192 int i;
194 for (i = 0; i < exp->len; ++i)
195 fprintf(stderr, "%d -> %d; ", i, exp->pos[i]);
196 fprintf(stderr, "\n");