add isl_aff_get_dim
[platform/upstream/isl.git] / isl_aff.c
1 /*
2  * Copyright 2011      INRIA Saclay
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
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  */
10
11 #include <isl_aff_private.h>
12 #include <isl_local_space_private.h>
13 #include <isl/seq.h>
14
15 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
16         __isl_take isl_vec *v)
17 {
18         isl_aff *aff;
19
20         if (!ls || !v)
21                 goto error;
22
23         aff = isl_calloc_type(v->ctx, struct isl_aff);
24         if (!aff)
25                 goto error;
26
27         aff->ref = 1;
28         aff->ls = ls;
29         aff->v = v;
30
31         return aff;
32 error:
33         isl_local_space_free(ls);
34         isl_vec_free(v);
35         return NULL;
36 }
37
38 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
39 {
40         isl_ctx *ctx;
41         isl_vec *v;
42         unsigned total;
43
44         if (!ls)
45                 return NULL;
46
47         ctx = isl_local_space_get_ctx(ls);
48         total = isl_local_space_dim(ls, isl_dim_all);
49         v = isl_vec_alloc(ctx, 1 + 1 + total);
50         return isl_aff_alloc_vec(ls, v);
51 }
52
53 __isl_give isl_aff *isl_aff_zero(__isl_take isl_local_space *ls)
54 {
55         isl_aff *aff;
56
57         aff = isl_aff_alloc(ls);
58         if (!aff)
59                 return NULL;
60
61         isl_int_set_si(aff->v->el[0], 1);
62         isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
63
64         return aff;
65 }
66
67 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
68 {
69         if (!aff)
70                 return NULL;
71
72         aff->ref++;
73         return aff;
74 }
75
76 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
77 {
78         if (!aff)
79                 return NULL;
80
81         return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
82                                  isl_vec_copy(aff->v));
83 }
84
85 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
86 {
87         if (!aff)
88                 return NULL;
89
90         if (aff->ref == 1)
91                 return aff;
92         aff->ref--;
93         return isl_aff_dup(aff);
94 }
95
96 void *isl_aff_free(__isl_take isl_aff *aff)
97 {
98         if (!aff)
99                 return NULL;
100
101         if (--aff->ref > 0)
102                 return NULL;
103
104         isl_local_space_free(aff->ls);
105         isl_vec_free(aff->v);
106
107         free(aff);
108
109         return NULL;
110 }
111
112 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
113 {
114         return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
115 }
116
117 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
118 {
119         return aff ? isl_local_space_dim(aff->ls, type) : 0;
120 }
121
122 __isl_give isl_dim *isl_aff_get_dim(__isl_keep isl_aff *aff)
123 {
124         return aff ? isl_local_space_get_dim(aff->ls) : NULL;
125 }
126
127 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
128 {
129         return aff ? isl_local_space_copy(aff->ls) : NULL;
130 }
131
132 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
133         enum isl_dim_type type, unsigned pos)
134 {
135         return aff ? isl_local_space_get_dim_name(aff->ls, type, pos) : 0;
136 }
137
138 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
139 {
140         if (!aff)
141                 return -1;
142         isl_int_set(*v, aff->v->el[0]);
143         return 0;
144 }
145
146 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
147 {
148         if (!aff)
149                 return -1;
150         isl_int_set(*v, aff->v->el[1]);
151         return 0;
152 }
153
154 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
155         enum isl_dim_type type, int pos, isl_int *v)
156 {
157         if (!aff)
158                 return -1;
159
160         if (pos >= isl_local_space_dim(aff->ls, type))
161                 isl_die(aff->v->ctx, isl_error_invalid,
162                         "position out of bounds", return -1);
163
164         pos += isl_local_space_offset(aff->ls, type);
165         isl_int_set(*v, aff->v->el[1 + pos]);
166
167         return 0;
168 }
169
170 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
171 {
172         aff = isl_aff_cow(aff);
173         if (!aff)
174                 return NULL;
175
176         aff->v = isl_vec_cow(aff->v);
177         if (!aff->v)
178                 return isl_aff_free(aff);
179
180         isl_int_set(aff->v->el[0], v);
181
182         return aff;
183 }
184
185 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
186 {
187         aff = isl_aff_cow(aff);
188         if (!aff)
189                 return NULL;
190
191         aff->v = isl_vec_cow(aff->v);
192         if (!aff->v)
193                 return isl_aff_free(aff);
194
195         isl_int_set(aff->v->el[1], v);
196
197         return aff;
198 }
199
200 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
201 {
202         if (isl_int_is_zero(v))
203                 return aff;
204
205         aff = isl_aff_cow(aff);
206         if (!aff)
207                 return NULL;
208
209         aff->v = isl_vec_cow(aff->v);
210         if (!aff->v)
211                 return isl_aff_free(aff);
212
213         isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
214
215         return aff;
216 }
217
218 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
219 {
220         aff = isl_aff_cow(aff);
221         if (!aff)
222                 return NULL;
223
224         aff->v = isl_vec_cow(aff->v);
225         if (!aff->v)
226                 return isl_aff_free(aff);
227
228         isl_int_set_si(aff->v->el[1], v);
229
230         return aff;
231 }
232
233 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
234         enum isl_dim_type type, int pos, isl_int v)
235 {
236         if (!aff)
237                 return NULL;
238
239         if (pos >= isl_local_space_dim(aff->ls, type))
240                 isl_die(aff->v->ctx, isl_error_invalid,
241                         "position out of bounds", return isl_aff_free(aff));
242
243         aff = isl_aff_cow(aff);
244         if (!aff)
245                 return NULL;
246
247         aff->v = isl_vec_cow(aff->v);
248         if (!aff->v)
249                 return isl_aff_free(aff);
250
251         pos += isl_local_space_offset(aff->ls, type);
252         isl_int_set(aff->v->el[1 + pos], v);
253
254         return aff;
255 }
256
257 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
258         enum isl_dim_type type, int pos, int v)
259 {
260         if (!aff)
261                 return NULL;
262
263         if (pos >= isl_local_space_dim(aff->ls, type))
264                 isl_die(aff->v->ctx, isl_error_invalid,
265                         "position out of bounds", return isl_aff_free(aff));
266
267         aff = isl_aff_cow(aff);
268         if (!aff)
269                 return NULL;
270
271         aff->v = isl_vec_cow(aff->v);
272         if (!aff->v)
273                 return isl_aff_free(aff);
274
275         pos += isl_local_space_offset(aff->ls, type);
276         isl_int_set_si(aff->v->el[1 + pos], v);
277
278         return aff;
279 }
280
281 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
282         enum isl_dim_type type, int pos, isl_int v)
283 {
284         if (!aff)
285                 return NULL;
286
287         if (pos >= isl_local_space_dim(aff->ls, type))
288                 isl_die(aff->v->ctx, isl_error_invalid,
289                         "position out of bounds", return isl_aff_free(aff));
290
291         aff = isl_aff_cow(aff);
292         if (!aff)
293                 return NULL;
294
295         aff->v = isl_vec_cow(aff->v);
296         if (!aff->v)
297                 return isl_aff_free(aff);
298
299         pos += isl_local_space_offset(aff->ls, type);
300         isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
301
302         return aff;
303 }
304
305 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
306         enum isl_dim_type type, int pos, int v)
307 {
308         isl_int t;
309
310         isl_int_init(t);
311         isl_int_set_si(t, v);
312         aff = isl_aff_add_coefficient(aff, type, pos, t);
313         isl_int_clear(t);
314
315         return aff;
316 }
317
318 __isl_give isl_div *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
319 {
320         if (!aff)
321                 return NULL;
322
323         return isl_local_space_get_div(aff->ls, pos);
324 }
325
326 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
327 {
328         aff = isl_aff_cow(aff);
329         if (!aff)
330                 return NULL;
331         aff->v = isl_vec_cow(aff->v);
332         if (!aff->v)
333                 return isl_aff_free(aff);
334
335         isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
336
337         return aff;
338 }
339
340 /* Given f, return ceil(f).
341  * If f is an integer expression, then just return f.
342  * Otherwise, create a new div d = [-f] and return the expression -d.
343  */
344 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
345 {
346         int size;
347         isl_ctx *ctx;
348
349         if (!aff)
350                 return NULL;
351
352         if (isl_int_is_one(aff->v->el[0]))
353                 return aff;
354
355         aff = isl_aff_cow(aff);
356         if (!aff)
357                 return NULL;
358
359         isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
360         aff->ls = isl_local_space_add_div(aff->ls, isl_vec_copy(aff->v));
361         if (!aff->ls)
362                 goto error;
363
364         ctx = isl_aff_get_ctx(aff);
365         size = aff->v->size;
366         isl_vec_free(aff->v);
367         aff->v = isl_vec_alloc(ctx, size + 1);
368         aff->v = isl_vec_clr(aff->v);
369         if (!aff->v)
370                 goto error;
371         isl_int_set_si(aff->v->el[0], 1);
372         isl_int_set_si(aff->v->el[size], -1);
373
374         return aff;
375 error:
376         isl_aff_free(aff);
377         return NULL;
378 }
379
380 /* Apply the expansion computed by isl_merge_divs.
381  * The expansion itself is given by "exp" while the resulting
382  * list of divs is given by "div".
383  */
384 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
385         __isl_take isl_mat *div, int *exp)
386 {
387         int i, j;
388         int old_n_div;
389         int new_n_div;
390         int offset;
391
392         aff = isl_aff_cow(aff);
393         if (!aff || !div)
394                 goto error;
395
396         old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
397         new_n_div = isl_mat_rows(div);
398         if (new_n_div < old_n_div)
399                 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
400                         "not an expansion", goto error);
401
402         aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
403         if (!aff->v)
404                 goto error;
405
406         offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
407         j = old_n_div - 1;
408         for (i = new_n_div - 1; i >= 0; --i) {
409                 if (j >= 0 && exp[j] == i) {
410                         if (i != j)
411                                 isl_int_swap(aff->v->el[offset + i],
412                                              aff->v->el[offset + j]);
413                         j--;
414                 } else
415                         isl_int_set_si(aff->v->el[offset + j], 0);
416         }
417
418         aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
419         if (!aff->ls)
420                 goto error;
421         isl_mat_free(div);
422         return aff;
423 error:
424         isl_aff_free(aff);
425         isl_mat_free(div);
426         return NULL;
427 }