isl_dim_equal: handle NULL input
[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_private.h>
12 #include <isl/map.h>
13 #include <isl_dim_private.h>
14 #include <isl/seq.h>
15 #include <isl/aff.h>
16
17 isl_ctx *isl_div_get_ctx(__isl_keep isl_div *div)
18 {
19         return div ? div->ctx : NULL;
20 }
21
22 static unsigned n(struct isl_div *d, enum isl_dim_type type)
23 {
24         struct isl_dim *dim = d->bmap->dim;
25         switch (type) {
26         case isl_dim_param:     return dim->nparam;
27         case isl_dim_in:        return dim->n_in;
28         case isl_dim_out:       return dim->n_out;
29         case isl_dim_div:       return d->bmap->n_div;
30         default:                return 0;
31         }
32 }
33
34 unsigned isl_div_dim(__isl_keep isl_div *div, enum isl_dim_type type)
35 {
36         return n(div, type);
37 }
38
39 static unsigned offset(struct isl_div *d, enum isl_dim_type type)
40 {
41         struct isl_dim *dim = d->bmap->dim;
42         switch (type) {
43         case isl_dim_param: return 1 + 1;
44         case isl_dim_in:    return 1 + 1 + dim->nparam;
45         case isl_dim_out:   return 1 + 1 + dim->nparam + dim->n_in;
46         case isl_dim_div:   return 1 + 1 + dim->nparam + dim->n_in + dim->n_out;
47         default:            return 0;
48         }
49 }
50
51 struct isl_div *isl_basic_map_div(struct isl_basic_map *bmap, int pos)
52 {
53         struct isl_div *div;
54
55         if (!bmap)
56                 goto error;
57
58         isl_assert(bmap->ctx, pos < bmap->n_div, goto error);
59         
60         div = isl_alloc_type(bmap->ctx, struct isl_div);
61         if (!div)
62                 goto error;
63
64         div->ctx = bmap->ctx;
65         isl_ctx_ref(div->ctx);
66         div->ref = 1;
67         div->bmap = bmap;
68         div->line = &bmap->div[pos];
69
70         return div;
71 error:
72         isl_basic_map_free(bmap);
73         return NULL;
74 }
75
76 struct isl_div *isl_basic_set_div(struct isl_basic_set *bset, int pos)
77 {
78         return isl_basic_map_div((struct isl_basic_map *)bset, pos);
79 }
80
81 __isl_give isl_div *isl_div_div(__isl_take isl_div *div, int pos)
82 {
83         isl_basic_map *bmap;
84         if (!div)
85                 return NULL;
86         bmap = isl_basic_map_copy(div->bmap);
87         isl_div_free(div);
88         return isl_basic_map_div(bmap, pos);
89 }
90
91 struct isl_div *isl_div_alloc(struct isl_dim *dim)
92 {
93         struct isl_basic_map *bmap;
94
95         if (!dim)
96                 return NULL;
97
98         bmap = isl_basic_map_alloc_dim(dim, 1, 0, 0);
99         if (!bmap)
100                 return NULL;
101
102         isl_basic_map_alloc_div(bmap);
103         isl_seq_clr(bmap->div[0], 1 + 1 + isl_basic_map_total_dim(bmap));
104         return isl_basic_map_div(bmap, 0);
105 }
106
107 __isl_give isl_div *isl_div_copy(__isl_keep isl_div *div)
108 {
109         if (!div)
110                 return NULL;
111
112         div->ref++;
113         return div;
114 }
115
116 void isl_div_free(struct isl_div *c)
117 {
118         if (!c)
119                 return;
120
121         if (--c->ref > 0)
122                 return;
123
124         isl_basic_map_free(c->bmap);
125         isl_ctx_deref(c->ctx);
126         free(c);
127 }
128
129 void isl_div_get_constant(struct isl_div *div, isl_int *v)
130 {
131         if (!div)
132                 return;
133         isl_int_set(*v, div->line[0][1]);
134 }
135
136 void isl_div_get_denominator(struct isl_div *div, isl_int *v)
137 {
138         if (!div)
139                 return;
140         isl_int_set(*v, div->line[0][0]);
141 }
142
143 void isl_div_get_coefficient(struct isl_div *div,
144         enum isl_dim_type type, int pos, isl_int *v)
145 {
146         if (!div)
147                 return;
148
149         isl_assert(div->ctx, pos < n(div, type), return);
150         isl_int_set(*v, div->line[0][offset(div, type) + pos]);
151 }
152
153 void isl_div_set_constant(struct isl_div *div, isl_int v)
154 {
155         if (!div)
156                 return;
157         isl_int_set(div->line[0][1], v);
158 }
159
160 void isl_div_set_denominator(struct isl_div *div, isl_int v)
161 {
162         if (!div)
163                 return;
164         isl_int_set(div->line[0][0], v);
165 }
166
167 void isl_div_set_coefficient(struct isl_div *div,
168         enum isl_dim_type type, int pos, isl_int v)
169 {
170         if (!div)
171                 return;
172
173         isl_assert(div->ctx, pos < n(div, type), return);
174         isl_int_set(div->line[0][offset(div, type) + pos], v);
175 }
176
177 __isl_give isl_aff *isl_aff_from_div(__isl_take isl_div *div)
178 {
179         isl_aff *aff;
180         int pos;
181
182         if (!div)
183                 return NULL;
184
185         pos = div->line - div->bmap->div;
186         aff = isl_aff_zero(isl_basic_map_get_local_space(div->bmap));
187         aff = isl_aff_set_coefficient_si(aff, isl_dim_div, pos, 1);
188
189         isl_div_free(div);
190         return aff;
191 }