8bfa391f9bf98609454340108c58d31164bc77b9
[platform/upstream/isl.git] / isl_vec.c
1 #include "isl_vec.h"
2
3 struct isl_vec *isl_vec_alloc(struct isl_ctx *ctx, unsigned size)
4 {
5         struct isl_vec *vec;
6
7         vec = isl_alloc_type(ctx, struct isl_vec);
8         if (!vec)
9                 return NULL;
10
11         vec->block = isl_blk_alloc(ctx, size);
12         if (isl_blk_is_error(vec->block))
13                 goto error;
14
15         vec->ctx = ctx;
16         isl_ctx_ref(ctx);
17         vec->ref = 1;
18         vec->size = size;
19         vec->el = vec->block.data;
20
21         return vec;
22 error:
23         isl_blk_free(ctx, vec->block);
24         return NULL;
25 }
26
27 struct isl_vec *isl_vec_copy(struct isl_vec *vec)
28 {
29         if (!vec)
30                 return NULL;
31
32         vec->ref++;
33         return vec;
34 }
35
36 struct isl_vec *isl_vec_dup(struct isl_vec *vec)
37 {
38         struct isl_vec *vec2;
39
40         if (!vec)
41                 return NULL;
42         vec2 = isl_vec_alloc(vec->ctx, vec->size);
43         isl_seq_cpy(vec2->el, vec->el, vec->size);
44         return vec2;
45 }
46
47 struct isl_vec *isl_vec_cow(struct isl_vec *vec)
48 {
49         struct isl_vec *vec2;
50         if (!vec)
51                 return NULL;
52
53         if (vec->ref == 1)
54                 return vec;
55
56         vec2 = isl_vec_dup(vec);
57         isl_vec_free( vec);
58         return vec2;
59 }
60
61 void isl_vec_free(struct isl_vec *vec)
62 {
63         if (!vec)
64                 return;
65
66         if (--vec->ref > 0)
67                 return;
68
69         isl_ctx_deref(vec->ctx);
70         isl_blk_free(vec->ctx, vec->block);
71         free(vec);
72 }
73
74 void isl_vec_dump(struct isl_vec *vec, FILE *out, int indent)
75 {
76         int i;
77
78         if (!vec) {
79                 fprintf(out, "%*snull vec\n", indent, "");
80                 return;
81         }
82
83         fprintf(out, "%*s[", indent, "");
84         for (i = 0; i < vec->size; ++i) {
85                 if (i)
86                     fprintf(out, ",");
87                 isl_int_print(out, vec->el[i], 0);
88         }
89         fprintf(out, "]\n");
90 }
91
92 void isl_vec_lcm(struct isl_vec *vec, isl_int *lcm)
93 {
94         isl_seq_lcm(vec->block.data, vec->size, lcm);
95 }
96
97 /* Given a rational vector, with the denominator in the first element
98  * of the vector, round up all coordinates.
99  */
100 struct isl_vec *isl_vec_ceil(struct isl_vec *vec)
101 {
102         int i;
103
104         vec = isl_vec_cow(vec);
105         if (!vec)
106                 return NULL;
107
108         isl_seq_cdiv_q(vec->el + 1, vec->el + 1, vec->el[0], vec->size - 1);
109
110         isl_int_set_si(vec->el[0], 1);
111
112         return vec;
113 }
114
115 struct isl_vec *isl_vec_normalize(struct isl_vec *vec)
116 {
117         if (!vec)
118                 return NULL;
119         isl_seq_normalize(vec->el, vec->size);
120         return vec;
121 }