060c0055bff80344dc9e1d3bb2dc4c482ae9c860
[platform/upstream/isl.git] / isl_dim.c
1 #include "isl_dim.h"
2
3 struct isl_dim *isl_dim_alloc(struct isl_ctx *ctx,
4                         unsigned nparam, unsigned n_in, unsigned n_out)
5 {
6         struct isl_dim *dim;
7
8         dim = isl_alloc_type(ctx, struct isl_dim);
9         if (!dim)
10                 return NULL;
11
12         dim->ctx = ctx;
13         isl_ctx_ref(ctx);
14         dim->ref = 1;
15         dim->nparam = nparam;
16         dim->n_in = n_in;
17         dim->n_out = n_out;
18
19         return dim;
20 }
21
22 struct isl_dim *isl_dim_dup(struct isl_dim *dim)
23 {
24         return isl_dim_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
25 }
26
27 struct isl_dim *isl_dim_cow(struct isl_dim *dim)
28 {
29         if (!dim)
30                 return NULL;
31
32         if (dim->ref == 1)
33                 return dim;
34         dim->ref--;
35         return isl_dim_dup(dim);
36 }
37
38 struct isl_dim *isl_dim_copy(struct isl_dim *dim)
39 {
40         if (!dim)
41                 return NULL;
42
43         dim->ref++;
44         return dim;
45 }
46
47 void isl_dim_free(struct isl_dim *dim)
48 {
49         if (!dim)
50                 return;
51
52         if (--dim->ref > 0)
53                 return;
54
55         isl_ctx_deref(dim->ctx);
56         
57         free(dim);
58 }
59
60 struct isl_dim *isl_dim_join(struct isl_dim *left, struct isl_dim *right)
61 {
62         struct isl_dim *dim;
63
64         if (!left || !right)
65                 goto error;
66
67         isl_assert(left->ctx, left->nparam == right->nparam, goto error);
68         isl_assert(left->ctx, left->n_out == right->n_in, goto error);
69
70         dim = isl_dim_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
71         if (!dim)
72                 goto error;
73
74         isl_dim_free(left);
75         isl_dim_free(right);
76
77         return dim;
78 error:
79         isl_dim_free(left);
80         isl_dim_free(right);
81         return NULL;
82 }
83
84 struct isl_dim *isl_dim_reverse(struct isl_dim *dim)
85 {
86         unsigned t;
87
88         if (!dim)
89                 return NULL;
90         if (dim->n_in == dim->n_out)
91                 return dim;
92
93         dim = isl_dim_cow(dim);
94         if (!dim)
95                 return NULL;
96
97         t = dim->n_in;
98         dim->n_in = dim->n_out;
99         dim->n_out = t;
100
101         return dim;
102 error:
103         isl_dim_free(dim);
104         return NULL;
105 }
106
107 unsigned isl_dim_total(struct isl_dim *dim)
108 {
109         return dim->nparam + dim->n_in + dim->n_out;
110 }
111
112 int isl_dim_equal(struct isl_dim *dim1, struct isl_dim *dim2)
113 {
114         return dim1->nparam == dim2->nparam &&
115                dim1->n_in == dim2->n_in &&
116                dim1->n_out == dim2->n_out;
117 }
118
119 int isl_dim_compatible(struct isl_dim *dim1, struct isl_dim *dim2)
120 {
121         return dim1->nparam == dim2->nparam &&
122                dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
123 }