add isl_qpolynomial_degree
[isl.git] / isl_div.c
blob5b5f4cbec39bd1527bc466952baa7c312fad414c
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_div.h>
11 #include <isl_map.h>
12 #include "isl_map_private.h"
13 #include "isl_seq.h"
15 static unsigned n(struct isl_div *d, enum isl_dim_type type)
17 struct isl_dim *dim = d->bmap->dim;
18 switch (type) {
19 case isl_dim_param: return dim->nparam;
20 case isl_dim_in: return dim->n_in;
21 case isl_dim_out: return dim->n_out;
22 case isl_dim_div: return d->bmap->n_div;
23 default: return 0;
27 unsigned isl_div_dim(__isl_keep isl_div *div, enum isl_dim_type type)
29 return n(div, type);
32 static unsigned offset(struct isl_div *d, enum isl_dim_type type)
34 struct isl_dim *dim = d->bmap->dim;
35 switch (type) {
36 case isl_dim_param: return 1 + 1;
37 case isl_dim_in: return 1 + 1 + dim->nparam;
38 case isl_dim_out: return 1 + 1 + dim->nparam + dim->n_in;
39 case isl_dim_div: return 1 + 1 + dim->nparam + dim->n_in + dim->n_out;
40 default: return 0;
44 struct isl_div *isl_basic_map_div(struct isl_basic_map *bmap, int pos)
46 struct isl_div *div;
48 if (!bmap)
49 goto error;
51 isl_assert(bmap->ctx, pos < bmap->n_div, goto error);
53 div = isl_alloc_type(bmap->ctx, struct isl_div);
54 if (!div)
55 goto error;
57 div->ctx = bmap->ctx;
58 isl_ctx_ref(div->ctx);
59 div->ref = 1;
60 div->bmap = bmap;
61 div->line = &bmap->div[pos];
63 return div;
64 error:
65 isl_basic_map_free(bmap);
66 return NULL;
69 struct isl_div *isl_basic_set_div(struct isl_basic_set *bset, int pos)
71 return isl_basic_map_div((struct isl_basic_map *)bset, pos);
74 struct isl_div *isl_div_alloc(struct isl_dim *dim)
76 struct isl_basic_map *bmap;
78 if (!dim)
79 return NULL;
81 bmap = isl_basic_map_alloc_dim(dim, 1, 0, 0);
82 if (!bmap)
83 return NULL;
85 isl_basic_map_alloc_div(bmap);
86 isl_seq_clr(bmap->div[0], 1 + 1 + isl_basic_map_total_dim(bmap));
87 return isl_basic_map_div(bmap, 0);
90 void isl_div_free(struct isl_div *c)
92 if (!c)
93 return;
95 if (--c->ref > 0)
96 return;
98 isl_basic_map_free(c->bmap);
99 isl_ctx_deref(c->ctx);
100 free(c);
103 void isl_div_get_constant(struct isl_div *div, isl_int *v)
105 if (!div)
106 return;
107 isl_int_set(*v, div->line[0][1]);
110 void isl_div_get_denominator(struct isl_div *div, isl_int *v)
112 if (!div)
113 return;
114 isl_int_set(*v, div->line[0][0]);
117 void isl_div_get_coefficient(struct isl_div *div,
118 enum isl_dim_type type, int pos, isl_int *v)
120 if (!div)
121 return;
123 isl_assert(div->ctx, pos < n(div, type), return);
124 isl_int_set(*v, div->line[0][offset(div, type) + pos]);
127 void isl_div_set_constant(struct isl_div *div, isl_int v)
129 if (!div)
130 return;
131 isl_int_set(div->line[0][1], v);
134 void isl_div_set_denominator(struct isl_div *div, isl_int v)
136 if (!div)
137 return;
138 isl_int_set(div->line[0][0], v);
141 void isl_div_set_coefficient(struct isl_div *div,
142 enum isl_dim_type type, int pos, isl_int v)
144 if (!div)
145 return;
147 isl_assert(div->ctx, pos < n(div, type), return);
148 isl_int_set(div->line[0][offset(div, type) + pos], v);