add isl_basic_map_remove
[platform/upstream/isl.git] / isl_div.c
1 #include <isl_div.h>
2 #include <isl_map.h>
3 #include "isl_map_private.h"
4
5 static unsigned n(struct isl_div *d, enum isl_dim_type type)
6 {
7         struct isl_dim *dim = d->bmap->dim;
8         switch (type) {
9         case isl_dim_param:     return dim->nparam;
10         case isl_dim_in:        return dim->n_in;
11         case isl_dim_out:       return dim->n_out;
12         case isl_dim_div:       return d->bmap->n_div;
13         }
14 }
15
16 static unsigned offset(struct isl_div *d, enum isl_dim_type type)
17 {
18         struct isl_dim *dim = d->bmap->dim;
19         switch (type) {
20         case isl_dim_param: return 1 + 1;
21         case isl_dim_in:    return 1 + 1 + dim->nparam;
22         case isl_dim_out:   return 1 + 1 + dim->nparam + dim->n_in;
23         case isl_dim_div:   return 1 + 1 + dim->nparam + dim->n_in + dim->n_out;
24         }
25 }
26
27 struct isl_div *isl_basic_map_div(struct isl_basic_map *bmap, isl_int **line)
28 {
29         struct isl_div *div;
30
31         if (!bmap || !line)
32                 goto error;
33         
34         div = isl_alloc_type(bmap->ctx, struct isl_div);
35         if (!div)
36                 goto error;
37
38         div->ctx = bmap->ctx;
39         isl_ctx_ref(div->ctx);
40         div->ref = 1;
41         div->bmap = bmap;
42         div->line = line;
43
44         return div;
45 error:
46         isl_basic_map_free(bmap);
47         return NULL;
48 }
49
50 struct isl_div *isl_div_alloc(struct isl_dim *dim)
51 {
52         struct isl_basic_map *bmap;
53
54         if (!dim)
55                 return NULL;
56
57         bmap = isl_basic_map_alloc_dim(dim, 1, 0, 0);
58         if (!bmap)
59                 return NULL;
60
61         isl_basic_map_alloc_div(bmap);
62         isl_seq_clr(bmap->div[0], 1 + 1 + isl_basic_map_total_dim(bmap));
63         return isl_basic_map_div(bmap, &bmap->div[0]);
64 }
65
66 struct isl_div *isl_div_free(struct isl_div *c)
67 {
68         if (!c)
69                 return;
70
71         if (--c->ref > 0)
72                 return;
73
74         isl_basic_map_free(c->bmap);
75         isl_ctx_deref(c->ctx);
76         free(c);
77 }
78
79 void isl_div_get_constant(struct isl_div *div, isl_int *v)
80 {
81         if (!div)
82                 return;
83         isl_int_set(*v, div->line[0][1]);
84 }
85
86 void isl_div_get_denominator(struct isl_div *div, isl_int *v)
87 {
88         if (!div)
89                 return;
90         isl_int_set(*v, div->line[0][0]);
91 }
92
93 void isl_div_get_coefficient(struct isl_div *div,
94         enum isl_dim_type type, int pos, isl_int *v)
95 {
96         if (!div)
97                 return;
98
99         isl_assert(div->ctx, pos < n(div, type), return);
100         isl_int_set(*v, div->line[0][offset(div, type) + pos]);
101 }
102
103 void isl_div_set_constant(struct isl_div *div, isl_int v)
104 {
105         if (!div)
106                 return;
107         isl_int_set(div->line[0][1], v);
108 }
109
110 void isl_div_set_denominator(struct isl_div *div, isl_int v)
111 {
112         if (!div)
113                 return;
114         isl_int_set(div->line[0][0], v);
115 }
116
117 void isl_div_set_coefficient(struct isl_div *div,
118         enum isl_dim_type type, int pos, isl_int v)
119 {
120         if (!div)
121                 return;
122
123         isl_assert(div->ctx, pos < n(div, type), return);
124         isl_int_set(div->line[0][offset(div, type) + pos], v);
125 }