summaryrefslogtreecommitdiff
path: root/isl_div.c
blob: 4694778c4274e4571136abda739bd1be612a3cf2 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright 2008-2009 Katholieke Universiteit Leuven
 *
 * Use of this software is governed by the GNU LGPLv2.1 license
 *
 * Written by Sven Verdoolaege, K.U.Leuven, Departement
 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
 */

#include <isl_map_private.h>
#include <isl/div.h>
#include <isl/map.h>
#include <isl_dim_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;
	default:		return 0;
	}
}

unsigned isl_div_dim(__isl_keep isl_div *div, enum isl_dim_type type)
{
	return n(div, type);
}

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;
	default:	    return 0;
	}
}

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);
}

__isl_give isl_div *isl_div_div(__isl_take isl_div *div, int pos)
{
	isl_basic_map *bmap;
	if (!div)
		return NULL;
	bmap = isl_basic_map_copy(div->bmap);
	isl_div_free(div);
	return isl_basic_map_div(bmap, 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);
}

__isl_give isl_div *isl_div_copy(__isl_keep isl_div *div)
{
	if (!div)
		return NULL;

	div->ref++;
	return div;
}

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);
}