isl_dim_map: allow signed mapping
[platform/upstream/isl.git] / isl_div.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, K.U.Leuven, Departement
7  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8  */
9
10 #include <isl_map_private.h>
11 #include <isl/div.h>
12 #include <isl/map.h>
13 #include <isl_dim_private.h>
14 #include <isl/seq.h>
15
16 static unsigned n(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 dim->nparam;
21         case isl_dim_in:        return dim->n_in;
22         case isl_dim_out:       return dim->n_out;
23         case isl_dim_div:       return d->bmap->n_div;
24         default:                return 0;
25         }
26 }
27
28 unsigned isl_div_dim(__isl_keep isl_div *div, enum isl_dim_type type)
29 {
30         return n(div, type);
31 }
32
33 static unsigned offset(struct isl_div *d, enum isl_dim_type type)
34 {
35         struct isl_dim *dim = d->bmap->dim;
36         switch (type) {
37         case isl_dim_param: return 1 + 1;
38         case isl_dim_in:    return 1 + 1 + dim->nparam;
39         case isl_dim_out:   return 1 + 1 + dim->nparam + dim->n_in;
40         case isl_dim_div:   return 1 + 1 + dim->nparam + dim->n_in + dim->n_out;
41         default:            return 0;
42         }
43 }
44
45 struct isl_div *isl_basic_map_div(struct isl_basic_map *bmap, int pos)
46 {
47         struct isl_div *div;
48
49         if (!bmap)
50                 goto error;
51
52         isl_assert(bmap->ctx, pos < bmap->n_div, goto error);
53         
54         div = isl_alloc_type(bmap->ctx, struct isl_div);
55         if (!div)
56                 goto error;
57
58         div->ctx = bmap->ctx;
59         isl_ctx_ref(div->ctx);
60         div->ref = 1;
61         div->bmap = bmap;
62         div->line = &bmap->div[pos];
63
64         return div;
65 error:
66         isl_basic_map_free(bmap);
67         return NULL;
68 }
69
70 struct isl_div *isl_basic_set_div(struct isl_basic_set *bset, int pos)
71 {
72         return isl_basic_map_div((struct isl_basic_map *)bset, pos);
73 }
74
75 __isl_give isl_div *isl_div_div(__isl_take isl_div *div, int pos)
76 {
77         isl_basic_map *bmap;
78         if (!div)
79                 return NULL;
80         bmap = isl_basic_map_copy(div->bmap);
81         isl_div_free(div);
82         return isl_basic_map_div(bmap, pos);
83 }
84
85 struct isl_div *isl_div_alloc(struct isl_dim *dim)
86 {
87         struct isl_basic_map *bmap;
88
89         if (!dim)
90                 return NULL;
91
92         bmap = isl_basic_map_alloc_dim(dim, 1, 0, 0);
93         if (!bmap)
94                 return NULL;
95
96         isl_basic_map_alloc_div(bmap);
97         isl_seq_clr(bmap->div[0], 1 + 1 + isl_basic_map_total_dim(bmap));
98         return isl_basic_map_div(bmap, 0);
99 }
100
101 __isl_give isl_div *isl_div_copy(__isl_keep isl_div *div)
102 {
103         if (!div)
104                 return NULL;
105
106         div->ref++;
107         return div;
108 }
109
110 void isl_div_free(struct isl_div *c)
111 {
112         if (!c)
113                 return;
114
115         if (--c->ref > 0)
116                 return;
117
118         isl_basic_map_free(c->bmap);
119         isl_ctx_deref(c->ctx);
120         free(c);
121 }
122
123 void isl_div_get_constant(struct isl_div *div, isl_int *v)
124 {
125         if (!div)
126                 return;
127         isl_int_set(*v, div->line[0][1]);
128 }
129
130 void isl_div_get_denominator(struct isl_div *div, isl_int *v)
131 {
132         if (!div)
133                 return;
134         isl_int_set(*v, div->line[0][0]);
135 }
136
137 void isl_div_get_coefficient(struct isl_div *div,
138         enum isl_dim_type type, int pos, isl_int *v)
139 {
140         if (!div)
141                 return;
142
143         isl_assert(div->ctx, pos < n(div, type), return);
144         isl_int_set(*v, div->line[0][offset(div, type) + pos]);
145 }
146
147 void isl_div_set_constant(struct isl_div *div, isl_int v)
148 {
149         if (!div)
150                 return;
151         isl_int_set(div->line[0][1], v);
152 }
153
154 void isl_div_set_denominator(struct isl_div *div, isl_int v)
155 {
156         if (!div)
157                 return;
158         isl_int_set(div->line[0][0], v);
159 }
160
161 void isl_div_set_coefficient(struct isl_div *div,
162         enum isl_dim_type type, int pos, isl_int v)
163 {
164         if (!div)
165                 return;
166
167         isl_assert(div->ctx, pos < n(div, type), return);
168         isl_int_set(div->line[0][offset(div, type) + pos], v);
169 }