add isl_point
[platform/upstream/isl.git] / isl_point.c
1 #include <isl_point_private.h>
2 #include <isl_set.h>
3 #include <isl_map_private.h>
4 #include <isl_sample.h>
5 #include <isl_scan.h>
6 #include <isl_seq.h>
7
8 __isl_give isl_point *isl_point_alloc(__isl_take isl_dim *dim,
9         __isl_take isl_vec *vec)
10 {
11         struct isl_point *pnt;
12
13         if (!dim || !vec)
14                 goto error;
15
16         pnt = isl_alloc_type(dim->ctx, struct isl_point);
17         if (!pnt)
18                 goto error;
19
20         if (vec->size > 1 + isl_dim_total(dim)) {
21                 vec = isl_vec_cow(vec);
22                 if (!vec)
23                         goto error;
24                 vec->size = 1 + isl_dim_total(dim);
25         }
26
27         pnt->ref = 1;
28         pnt->dim = dim;
29         pnt->vec = vec;
30
31         return pnt;
32 error:
33         isl_dim_free(dim);
34         isl_vec_free(vec);
35         return NULL;
36 }
37
38 __isl_give isl_point *isl_point_zero(__isl_take isl_dim *dim)
39 {
40         isl_vec *vec;
41
42         if (!dim)
43                 return NULL;
44         vec = isl_vec_alloc(dim->ctx, 1 + isl_dim_total(dim));
45         if (!vec)
46                 goto error;
47         isl_int_set_si(vec->el[0], 1);
48         isl_seq_clr(vec->el + 1, vec->size - 1);
49         return isl_point_alloc(dim, vec);
50 error:
51         isl_dim_free(dim);
52         return NULL;
53 }
54
55 __isl_give isl_point *isl_point_dup(__isl_keep isl_point *pnt)
56 {
57         struct isl_point *pnt2;
58
59         if (!pnt)
60                 return NULL;
61         pnt2 = isl_point_alloc(isl_dim_copy(pnt->dim), isl_vec_copy(pnt->vec));
62         return pnt2;
63 }
64
65 __isl_give isl_point *isl_point_cow(__isl_take isl_point *pnt)
66 {
67         struct isl_point *pnt2;
68         if (!pnt)
69                 return NULL;
70
71         if (pnt->ref == 1)
72                 return pnt;
73
74         pnt2 = isl_point_dup(pnt);
75         isl_point_free(pnt);
76         return pnt2;
77 }
78
79 __isl_give isl_point *isl_point_copy(__isl_keep isl_point *pnt)
80 {
81         if (!pnt)
82                 return NULL;
83
84         pnt->ref++;
85         return pnt;
86 }
87
88 void isl_point_free(__isl_take isl_point *pnt)
89 {
90         if (!pnt)
91                 return;
92
93         if (--pnt->ref > 0)
94                 return;
95
96         isl_dim_free(pnt->dim);
97         isl_vec_free(pnt->vec);
98         free(pnt);
99 }
100
101 __isl_give isl_point *isl_point_void(__isl_take isl_dim *dim)
102 {
103         if (!dim)
104                 return NULL;
105
106         return isl_point_alloc(dim, isl_vec_alloc(dim->ctx, 0));
107 }
108
109 int isl_point_is_void(__isl_keep isl_point *pnt)
110 {
111         if (!pnt)
112                 return -1;
113
114         return pnt->vec->size == 0;
115 }
116
117 void isl_point_get_coordinate(__isl_keep isl_point *pnt,
118         enum isl_dim_type type, int pos, isl_int *v)
119 {
120         if (!pnt || isl_point_is_void(pnt))
121                 return;
122         if (type == isl_dim_set)
123                 pos += isl_dim_size(pnt->dim, isl_dim_param);
124         isl_int_set(*v, pnt->vec->el[1 + pos]);
125 }
126
127 __isl_give isl_point *isl_point_set_coordinate(__isl_take isl_point *pnt,
128         enum isl_dim_type type, int pos, isl_int v)
129 {
130         if (!pnt || isl_point_is_void(pnt))
131                 return pnt;
132
133         pnt = isl_point_cow(pnt);
134         if (!pnt)
135                 return NULL;
136         pnt->vec = isl_vec_cow(pnt->vec);
137         if (!pnt->vec)
138                 goto error;
139
140         if (type == isl_dim_set)
141                 pos += isl_dim_size(pnt->dim, isl_dim_param);
142
143         isl_int_set(pnt->vec->el[1 + pos], v);
144
145         return pnt;
146 error:
147         isl_point_free(pnt);
148         return NULL;
149 }
150
151 __isl_give isl_point *isl_point_add_ui(__isl_take isl_point *pnt,
152         enum isl_dim_type type, int pos, unsigned val)
153 {
154         if (!pnt || isl_point_is_void(pnt))
155                 return pnt;
156
157         pnt = isl_point_cow(pnt);
158         if (!pnt)
159                 return NULL;
160         pnt->vec = isl_vec_cow(pnt->vec);
161         if (!pnt->vec)
162                 goto error;
163
164         if (type == isl_dim_set)
165                 pos += isl_dim_size(pnt->dim, isl_dim_param);
166
167         isl_int_add_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
168
169         return pnt;
170 error:
171         isl_point_free(pnt);
172         return NULL;
173 }
174
175 __isl_give isl_point *isl_point_sub_ui(__isl_take isl_point *pnt,
176         enum isl_dim_type type, int pos, unsigned val)
177 {
178         if (!pnt || isl_point_is_void(pnt))
179                 return pnt;
180
181         pnt = isl_point_cow(pnt);
182         if (!pnt)
183                 return NULL;
184         pnt->vec = isl_vec_cow(pnt->vec);
185         if (!pnt->vec)
186                 goto error;
187
188         if (type == isl_dim_set)
189                 pos += isl_dim_size(pnt->dim, isl_dim_param);
190
191         isl_int_sub_ui(pnt->vec->el[1 + pos], pnt->vec->el[1 + pos], val);
192
193         return pnt;
194 error:
195         isl_point_free(pnt);
196         return NULL;
197 }
198
199 struct isl_foreach_point {
200         struct isl_scan_callback callback;
201         int (*fn)(__isl_take isl_point *pnt, void *user);
202         void *user;
203         isl_dim *dim;
204 };
205
206 static int foreach_point(struct isl_scan_callback *cb, __isl_take isl_vec *sample)
207 {
208         struct isl_foreach_point *fp = (struct isl_foreach_point *)cb;
209         isl_point *pnt;
210
211         pnt = isl_point_alloc(isl_dim_copy(fp->dim), sample);
212
213         return fp->fn(pnt, fp->user);
214 }
215
216 int isl_set_foreach_point(__isl_keep isl_set *set,
217         int (*fn)(__isl_take isl_point *pnt, void *user), void *user)
218 {
219         struct isl_foreach_point fp = { { &foreach_point }, fn, user };
220         int i;
221
222         if (!set)
223                 return -1;
224
225         fp.dim = isl_set_get_dim(set);
226         if (!fp.dim)
227                 return -1;
228
229         set = isl_set_copy(set);
230         set = isl_set_cow(set);
231         set = isl_set_make_disjoint(set);
232         set = isl_set_compute_divs(set);
233         if (!set)
234                 goto error;
235
236         for (i = 0; i < set->n; ++i)
237                 if (isl_basic_set_scan(isl_basic_set_copy(set->p[i]),
238                                         &fp.callback) < 0)
239                         goto error;
240
241         isl_set_free(set);
242         isl_dim_free(fp.dim);
243
244         return 0;
245 error:
246         isl_set_free(set);
247         isl_dim_free(fp.dim);
248         return -1;
249 }
250
251 __isl_give isl_set *isl_set_box_from_points(__isl_take isl_point *pnt1,
252         __isl_take isl_point *pnt2)
253 {
254         isl_basic_set *bset;
255         unsigned total;
256         int i;
257         int k;
258         isl_int t;
259
260         isl_int_init(t);
261
262         if (!pnt1 || !pnt2)
263                 goto error;
264
265         isl_assert(pnt1->dim->ctx,
266                         isl_dim_equal(pnt1->dim, pnt2->dim), goto error);
267
268         if (isl_point_is_void(pnt1) && isl_point_is_void(pnt2)) {
269                 isl_dim *dim = isl_dim_copy(pnt1->dim);
270                 isl_point_free(pnt1);
271                 isl_point_free(pnt2);
272                 isl_int_clear(t);
273                 return isl_set_empty(dim);
274         }
275         if (isl_point_is_void(pnt1)) {
276                 isl_basic_set *model;
277                 model = isl_basic_set_empty(isl_dim_copy(pnt2->dim));
278                 bset = isl_basic_set_from_vec(isl_vec_copy(pnt2->vec));
279                 bset = isl_basic_set_from_underlying_set(bset, model);
280                 isl_point_free(pnt1);
281                 isl_point_free(pnt2);
282                 isl_int_clear(t);
283                 return isl_set_from_basic_set(bset);
284         }
285         if (isl_point_is_void(pnt2)) {
286                 isl_basic_set *model;
287                 model = isl_basic_set_empty(isl_dim_copy(pnt1->dim));
288                 bset = isl_basic_set_from_vec(isl_vec_copy(pnt1->vec));
289                 bset = isl_basic_set_from_underlying_set(bset, model);
290                 isl_point_free(pnt1);
291                 isl_point_free(pnt2);
292                 isl_int_clear(t);
293                 return isl_set_from_basic_set(bset);
294         }
295
296         total = isl_dim_total(pnt1->dim);
297         bset = isl_basic_set_alloc_dim(isl_dim_copy(pnt1->dim), 0, 0, 2 * total);
298
299         for (i = 0; i < total; ++i) {
300                 isl_int_mul(t, pnt1->vec->el[1 + i], pnt2->vec->el[0]);
301                 isl_int_submul(t, pnt2->vec->el[1 + i], pnt1->vec->el[0]);
302
303                 k = isl_basic_set_alloc_inequality(bset);
304                 if (k < 0)
305                         goto error;
306                 isl_seq_clr(bset->ineq[k] + 1, total);
307                 if (isl_int_is_pos(t)) {
308                         isl_int_set_si(bset->ineq[k][1 + i], -1);
309                         isl_int_set(bset->ineq[k][0], pnt1->vec->el[1 + i]);
310                 } else {
311                         isl_int_set_si(bset->ineq[k][1 + i], 1);
312                         isl_int_neg(bset->ineq[k][0], pnt1->vec->el[1 + i]);
313                 }
314                 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt1->vec->el[0]);
315
316                 k = isl_basic_set_alloc_inequality(bset);
317                 if (k < 0)
318                         goto error;
319                 isl_seq_clr(bset->ineq[k] + 1, total);
320                 if (isl_int_is_pos(t)) {
321                         isl_int_set_si(bset->ineq[k][1 + i], 1);
322                         isl_int_neg(bset->ineq[k][0], pnt2->vec->el[1 + i]);
323                 } else {
324                         isl_int_set_si(bset->ineq[k][1 + i], -1);
325                         isl_int_set(bset->ineq[k][0], pnt2->vec->el[1 + i]);
326                 }
327                 isl_int_fdiv_q(bset->ineq[k][0], bset->ineq[k][0], pnt2->vec->el[0]);
328         }
329
330         bset = isl_basic_set_finalize(bset);
331
332         isl_point_free(pnt1);
333         isl_point_free(pnt2);
334
335         isl_int_clear(t);
336
337         return isl_set_from_basic_set(bset);
338 error:
339         isl_point_free(pnt1);
340         isl_point_free(pnt2);
341         isl_int_clear(t);
342         return NULL;
343 }
344
345 void isl_point_print(__isl_keep isl_point *pnt, FILE *out)
346 {
347         int i;
348         unsigned nparam;
349         unsigned dim;
350
351         if (!pnt)
352                 return;
353         if (isl_point_is_void(pnt)) {
354                 fprintf(out, "void\n");
355                 return;
356         }
357
358         nparam = isl_dim_size(pnt->dim, isl_dim_param);
359         dim = isl_dim_size(pnt->dim, isl_dim_set);
360         if (nparam > 0) {
361                 fprintf(out, "[");
362                 for (i = 0; i < nparam; ++i) {
363                         const char *name;
364                         if (i)
365                                 fprintf(out, ", ");
366                         name = isl_dim_get_name(pnt->dim, isl_dim_param, i);
367                         if (name)
368                                 fprintf(out, "%s = ", name);
369                         isl_int_print(out, pnt->vec->el[1 + i], 0);
370                         if (!isl_int_is_one(pnt->vec->el[0])) {
371                                 fprintf(out, "/");
372                                 isl_int_print(out, pnt->vec->el[0], 0);
373                         }
374                 }
375                 fprintf(out, "] -> ");
376         }
377         fprintf(out, "[");
378         for (i = 0; i < dim; ++i) {
379                 if (i)
380                         fprintf(out, ", ");
381                 isl_int_print(out, pnt->vec->el[1 + nparam + i], 0);
382                 if (!isl_int_is_one(pnt->vec->el[0])) {
383                         fprintf(out, "/");
384                         isl_int_print(out, pnt->vec->el[0], 0);
385                 }
386         }
387         fprintf(out, "]\n");
388 }