summaryrefslogtreecommitdiff
path: root/isl_div.c
blob: df5098015b94b67cd5627d1adc99214cb36ae20a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <isl_div.h>
#include <isl_map.h>
#include "isl_map_private.h"
#include "isl_seq.h"

static unsigned n(struct isl_div *d, enum isl_dim_type type)
{
	struct isl_dim *dim = d->bmap->dim;
	switch (type) {
	case isl_dim_param:	return dim->nparam;
	case isl_dim_in:	return dim->n_in;
	case isl_dim_out:	return dim->n_out;
	case isl_dim_div:	return d->bmap->n_div;
	}
}

static unsigned offset(struct isl_div *d, enum isl_dim_type type)
{
	struct isl_dim *dim = d->bmap->dim;
	switch (type) {
	case isl_dim_param: return 1 + 1;
	case isl_dim_in:    return 1 + 1 + dim->nparam;
	case isl_dim_out:   return 1 + 1 + dim->nparam + dim->n_in;
	case isl_dim_div:   return 1 + 1 + dim->nparam + dim->n_in + dim->n_out;
	}
}

struct isl_div *isl_basic_map_div(struct isl_basic_map *bmap, int pos)
{
	struct isl_div *div;

	if (!bmap)
		goto error;

	isl_assert(bmap->ctx, pos < bmap->n_div, goto error);
	
	div = isl_alloc_type(bmap->ctx, struct isl_div);
	if (!div)
		goto error;

	div->ctx = bmap->ctx;
	isl_ctx_ref(div->ctx);
	div->ref = 1;
	div->bmap = bmap;
	div->line = &bmap->div[pos];

	return div;
error:
	isl_basic_map_free(bmap);
	return NULL;
}

struct isl_div *isl_basic_set_div(struct isl_basic_set *bset, int pos)
{
	return isl_basic_map_div((struct isl_basic_map *)bset, pos);
}

struct isl_div *isl_div_alloc(struct isl_dim *dim)
{
	struct isl_basic_map *bmap;

	if (!dim)
		return NULL;

	bmap = isl_basic_map_alloc_dim(dim, 1, 0, 0);
	if (!bmap)
		return NULL;

	isl_basic_map_alloc_div(bmap);
	isl_seq_clr(bmap->div[0], 1 + 1 + isl_basic_map_total_dim(bmap));
	return isl_basic_map_div(bmap, 0);
}

void isl_div_free(struct isl_div *c)
{
	if (!c)
		return;

	if (--c->ref > 0)
		return;

	isl_basic_map_free(c->bmap);
	isl_ctx_deref(c->ctx);
	free(c);
}

void isl_div_get_constant(struct isl_div *div, isl_int *v)
{
	if (!div)
		return;
	isl_int_set(*v, div->line[0][1]);
}

void isl_div_get_denominator(struct isl_div *div, isl_int *v)
{
	if (!div)
		return;
	isl_int_set(*v, div->line[0][0]);
}

void isl_div_get_coefficient(struct isl_div *div,
	enum isl_dim_type type, int pos, isl_int *v)
{
	if (!div)
		return;

	isl_assert(div->ctx, pos < n(div, type), return);
	isl_int_set(*v, div->line[0][offset(div, type) + pos]);
}

void isl_div_set_constant(struct isl_div *div, isl_int v)
{
	if (!div)
		return;
	isl_int_set(div->line[0][1], v);
}

void isl_div_set_denominator(struct isl_div *div, isl_int v)
{
	if (!div)
		return;
	isl_int_set(div->line[0][0], v);
}

void isl_div_set_coefficient(struct isl_div *div,
	enum isl_dim_type type, int pos, isl_int v)
{
	if (!div)
		return;

	isl_assert(div->ctx, pos < n(div, type), return);
	isl_int_set(div->line[0][offset(div, type) + pos], v);
}