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