isl_stream: accept "." token
[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_div.h>
11 #include <isl_map.h>
12 #include "isl_map_private.h"
13 #include "isl_seq.h"
14
15 static unsigned n(struct isl_div *d, enum isl_dim_type type)
16 {
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         }
24 }
25
26 unsigned isl_div_dim(__isl_keep isl_div *div, enum isl_dim_type type)
27 {
28         return n(div, type);
29 }
30
31 static unsigned offset(struct isl_div *d, enum isl_dim_type type)
32 {
33         struct isl_dim *dim = d->bmap->dim;
34         switch (type) {
35         case isl_dim_param: return 1 + 1;
36         case isl_dim_in:    return 1 + 1 + dim->nparam;
37         case isl_dim_out:   return 1 + 1 + dim->nparam + dim->n_in;
38         case isl_dim_div:   return 1 + 1 + dim->nparam + dim->n_in + dim->n_out;
39         }
40 }
41
42 struct isl_div *isl_basic_map_div(struct isl_basic_map *bmap, int pos)
43 {
44         struct isl_div *div;
45
46         if (!bmap)
47                 goto error;
48
49         isl_assert(bmap->ctx, pos < bmap->n_div, goto error);
50         
51         div = isl_alloc_type(bmap->ctx, struct isl_div);
52         if (!div)
53                 goto error;
54
55         div->ctx = bmap->ctx;
56         isl_ctx_ref(div->ctx);
57         div->ref = 1;
58         div->bmap = bmap;
59         div->line = &bmap->div[pos];
60
61         return div;
62 error:
63         isl_basic_map_free(bmap);
64         return NULL;
65 }
66
67 struct isl_div *isl_basic_set_div(struct isl_basic_set *bset, int pos)
68 {
69         return isl_basic_map_div((struct isl_basic_map *)bset, pos);
70 }
71
72 struct isl_div *isl_div_alloc(struct isl_dim *dim)
73 {
74         struct isl_basic_map *bmap;
75
76         if (!dim)
77                 return NULL;
78
79         bmap = isl_basic_map_alloc_dim(dim, 1, 0, 0);
80         if (!bmap)
81                 return NULL;
82
83         isl_basic_map_alloc_div(bmap);
84         isl_seq_clr(bmap->div[0], 1 + 1 + isl_basic_map_total_dim(bmap));
85         return isl_basic_map_div(bmap, 0);
86 }
87
88 void isl_div_free(struct isl_div *c)
89 {
90         if (!c)
91                 return;
92
93         if (--c->ref > 0)
94                 return;
95
96         isl_basic_map_free(c->bmap);
97         isl_ctx_deref(c->ctx);
98         free(c);
99 }
100
101 void isl_div_get_constant(struct isl_div *div, isl_int *v)
102 {
103         if (!div)
104                 return;
105         isl_int_set(*v, div->line[0][1]);
106 }
107
108 void isl_div_get_denominator(struct isl_div *div, isl_int *v)
109 {
110         if (!div)
111                 return;
112         isl_int_set(*v, div->line[0][0]);
113 }
114
115 void isl_div_get_coefficient(struct isl_div *div,
116         enum isl_dim_type type, int pos, isl_int *v)
117 {
118         if (!div)
119                 return;
120
121         isl_assert(div->ctx, pos < n(div, type), return);
122         isl_int_set(*v, div->line[0][offset(div, type) + pos]);
123 }
124
125 void isl_div_set_constant(struct isl_div *div, isl_int v)
126 {
127         if (!div)
128                 return;
129         isl_int_set(div->line[0][1], v);
130 }
131
132 void isl_div_set_denominator(struct isl_div *div, isl_int v)
133 {
134         if (!div)
135                 return;
136         isl_int_set(div->line[0][0], v);
137 }
138
139 void isl_div_set_coefficient(struct isl_div *div,
140         enum isl_dim_type type, int pos, isl_int v)
141 {
142         if (!div)
143                 return;
144
145         isl_assert(div->ctx, pos < n(div, type), return);
146         isl_int_set(div->line[0][offset(div, type) + pos], v);
147 }