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