export isl_pw_aff_coalesce
[isl.git] / isl_div.c
blobd7ca68f26d2b466ec1fb265aec53e706ae715deb
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <isl_map_private.h>
11 #include <isl_div_private.h>
12 #include <isl/map.h>
13 #include <isl_dim_private.h>
14 #include <isl/seq.h>
16 isl_ctx *isl_div_get_ctx(__isl_keep isl_div *div)
18 return div ? div->ctx : NULL;
21 static unsigned n(struct isl_div *d, enum isl_dim_type type)
23 struct isl_dim *dim = d->bmap->dim;
24 switch (type) {
25 case isl_dim_param: return dim->nparam;
26 case isl_dim_in: return dim->n_in;
27 case isl_dim_out: return dim->n_out;
28 case isl_dim_div: return d->bmap->n_div;
29 default: return 0;
33 unsigned isl_div_dim(__isl_keep isl_div *div, enum isl_dim_type type)
35 return n(div, type);
38 static unsigned offset(struct isl_div *d, enum isl_dim_type type)
40 struct isl_dim *dim = d->bmap->dim;
41 switch (type) {
42 case isl_dim_param: return 1 + 1;
43 case isl_dim_in: return 1 + 1 + dim->nparam;
44 case isl_dim_out: return 1 + 1 + dim->nparam + dim->n_in;
45 case isl_dim_div: return 1 + 1 + dim->nparam + dim->n_in + dim->n_out;
46 default: return 0;
50 struct isl_div *isl_basic_map_div(struct isl_basic_map *bmap, int pos)
52 struct isl_div *div;
54 if (!bmap)
55 goto error;
57 isl_assert(bmap->ctx, pos < bmap->n_div, goto error);
59 div = isl_alloc_type(bmap->ctx, struct isl_div);
60 if (!div)
61 goto error;
63 div->ctx = bmap->ctx;
64 isl_ctx_ref(div->ctx);
65 div->ref = 1;
66 div->bmap = bmap;
67 div->line = &bmap->div[pos];
69 return div;
70 error:
71 isl_basic_map_free(bmap);
72 return NULL;
75 struct isl_div *isl_basic_set_div(struct isl_basic_set *bset, int pos)
77 return isl_basic_map_div((struct isl_basic_map *)bset, pos);
80 __isl_give isl_div *isl_div_div(__isl_take isl_div *div, int pos)
82 isl_basic_map *bmap;
83 if (!div)
84 return NULL;
85 bmap = isl_basic_map_copy(div->bmap);
86 isl_div_free(div);
87 return isl_basic_map_div(bmap, pos);
90 struct isl_div *isl_div_alloc(struct isl_dim *dim)
92 struct isl_basic_map *bmap;
94 if (!dim)
95 return NULL;
97 bmap = isl_basic_map_alloc_dim(dim, 1, 0, 0);
98 if (!bmap)
99 return NULL;
101 isl_basic_map_alloc_div(bmap);
102 isl_seq_clr(bmap->div[0], 1 + 1 + isl_basic_map_total_dim(bmap));
103 return isl_basic_map_div(bmap, 0);
106 __isl_give isl_div *isl_div_copy(__isl_keep isl_div *div)
108 if (!div)
109 return NULL;
111 div->ref++;
112 return div;
115 void isl_div_free(struct isl_div *c)
117 if (!c)
118 return;
120 if (--c->ref > 0)
121 return;
123 isl_basic_map_free(c->bmap);
124 isl_ctx_deref(c->ctx);
125 free(c);
128 void isl_div_get_constant(struct isl_div *div, isl_int *v)
130 if (!div)
131 return;
132 isl_int_set(*v, div->line[0][1]);
135 void isl_div_get_denominator(struct isl_div *div, isl_int *v)
137 if (!div)
138 return;
139 isl_int_set(*v, div->line[0][0]);
142 void isl_div_get_coefficient(struct isl_div *div,
143 enum isl_dim_type type, int pos, isl_int *v)
145 if (!div)
146 return;
148 isl_assert(div->ctx, pos < n(div, type), return);
149 isl_int_set(*v, div->line[0][offset(div, type) + pos]);
152 void isl_div_set_constant(struct isl_div *div, isl_int v)
154 if (!div)
155 return;
156 isl_int_set(div->line[0][1], v);
159 void isl_div_set_denominator(struct isl_div *div, isl_int v)
161 if (!div)
162 return;
163 isl_int_set(div->line[0][0], v);
166 void isl_div_set_coefficient(struct isl_div *div,
167 enum isl_dim_type type, int pos, isl_int v)
169 if (!div)
170 return;
172 isl_assert(div->ctx, pos < n(div, type), return);
173 isl_int_set(div->line[0][offset(div, type) + pos], v);