forked from antoinemine/apron
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbox_representation.c
327 lines (291 loc) · 7.93 KB
/
box_representation.c
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* ********************************************************************** */
/* box_representation.c: general management */
/* ********************************************************************** */
#include <string.h>
#include <stdio.h>
#include "box_internal.h"
#include "box_representation.h"
/* ********************************************************************** */
/* Internal functions */
/* ********************************************************************** */
box_t* box_alloc(size_t intdim, size_t realdim)
{
box_t* itv = malloc(sizeof(box_t));
itv->p = NULL;
itv->intdim = intdim;
itv->realdim = realdim;
return itv;
}
void box_init(box_t* a)
{
size_t i;
size_t nbdims = a->intdim + a->realdim;
assert(a->p==NULL);
a->p = itv_array_alloc(nbdims+1);
/* Add an unused dimension to differentiate
empty and top values in dimension 0+0 */
}
void box_set_bottom(box_t* a)
{
if (a->p){
itv_array_free(a->p,a->intdim+a->realdim+1);
a->p = NULL;
}
}
void box_set_top(box_t* a)
{
size_t i;
size_t nbdims;
nbdims = a->intdim + a->realdim;
if (a->p==NULL){
box_init(a);
};
for (i=0; i<nbdims; i++){
itv_set_top(a->p[i]);
}
}
void box_set(box_t* a, box_t* b)
{
size_t i;
size_t nbdims;
if (b->p==NULL)
return;
nbdims = b->intdim + b->realdim;
if (a->p==NULL){
box_init(a);
};
for (i=0; i<nbdims; i++){
itv_set(a->p[i],b->p[i]);
}
}
/* ********************************************************************** */
/* 1. Memory */
/* ********************************************************************** */
/* Return a copy of an abstract value, on
which destructive update does not affect the initial value. */
box_t* box_copy(ap_manager_t* man, box_t* a)
{
size_t i;
size_t nbdims = a->intdim+a->realdim;
box_t* b = box_alloc(a->intdim,a->realdim);
if (a->p){
b->p = malloc((nbdims+1)*sizeof(itv_t));
for (i=0; i<nbdims; i++){
itv_init_set(b->p[i],a->p[i]);
}
itv_init(b->p[nbdims]);
/* Add an unused dimension to differentiate
empty and top values in dimension 0+0 */
}
man->result.flag_best = true;
man->result.flag_exact = true;
return b;
}
/* Free all the memory used by the abstract value */
void box_free(ap_manager_t* man, box_t* a)
{
if (a->p){
itv_array_free(a->p,a->intdim+a->realdim+1);
a->p = NULL;
}
free(a);
}
/* Return the abstract size of an abstract value (see ap_manager_t) */
size_t box_size(ap_manager_t* man, box_t* a)
{
return 2*(a->intdim+a->realdim);
}
/* ********************************************************************** */
/* 2. Control of internal representation */
/* ********************************************************************** */
/* Minimize the size of the representation of a.
This may result in a later recomputation of internal information.
*/
void box_minimize(ap_manager_t* man, box_t* a)
{
man->result.flag_best = true;
man->result.flag_exact = true;
return;
}
/* Put the abstract value in canonical form. (not yet clear definition) */
void box_canonicalize(ap_manager_t* man, box_t* a)
{
man->result.flag_best = true;
man->result.flag_exact = true;
return;
}
/* Return an hash code */
int box_hash(ap_manager_t* man, box_t* a)
{
int i,dec,size,res;
size = a->intdim +a->realdim;
res = size * 2999;
if (a->p!=NULL){
for (i=0; i<size; i += (size+4)/5){
res = 3*res + itv_hash(a->p[i]);
}
}
man->result.flag_best = true;
man->result.flag_exact = true;
return res;
}
/* Perform some transformation on the abstract value, guided by the
field algorithm.
The transformation may lose information. The argument "algorithm"
overrides the field algorithm of the structure of type foption_t
associated to box_approximate (commodity feature). */
void box_approximate(ap_manager_t* man, box_t* a, int algorithm)
{
man->result.flag_best = true;
man->result.flag_exact = true;
return;
}
/* ********************************************************************** */
/* 3. Printing */
/* ********************************************************************** */
/* Print the abstract value in a pretty way, using function
name_of_dim to name dimensions */
void box_fprint(FILE* stream,
ap_manager_t* man,
box_t* a,
char** name_of_dim)
{
size_t i;
size_t nbdims = a->intdim + a->realdim;
fprintf(stream,"interval of dim (%ld,%ld):",
(long)a->intdim,(long)a->realdim);
if (a->p){
fprintf(stream,"\n");
for(i=0; i<nbdims; i++){
if (name_of_dim){
fprintf(stream,"%8s in ", name_of_dim[i]);
} else {
fprintf(stream,"x%ld in ", (long)i);
}
itv_fprint(stream,a->p[i]);
fprintf(stream,"\n");
}
}
else {
fprintf(stream,nbdims>0 ? " bottom\n" : "top\n");
}
}
/* Dump the internal representation of an abstract value,
for debugging purposes */
void box_fdump(FILE* stream,
ap_manager_t* man,
box_t* a)
{
size_t i;
size_t nbdims = a->intdim + a->realdim;
fprintf(stream,"interval of dim (%ld,%ld):",
(long)a->intdim,(long)a->realdim);
if (a->p){
fprintf(stream,"\n");
for(i=0; i<nbdims; i++){
fprintf(stream,"dim %3ld in ",(long)i);
itv_fprint(stream,a->p[i]);
fprintf(stream,"\n");
}
}
else {
fprintf(stream, nbdims>0 ? " bottom\n" : "top\n");
}
}
/* Print the difference between a1 (old value) and a2 (new value),
using function name_of_dim to name dimensions.
The meaning of difference is library dependent. */
void box_fprintdiff(FILE* stream,
ap_manager_t* man,
box_t* a, box_t* b,
char** name_of_dim)
{
size_t i;
size_t nbdims;
char* str;
nbdims = a->intdim + a->realdim;
fprintf(stream,"diff of 2 intervals of dim (%ld,%ld)",
(long)a->intdim,(long)b->intdim);
if (box_is_eq(man,a,b)){
fprintf(stream," : none\n");
}
else {
/* we are sure that nbdims>0 */
if (a->p==0){
fprintf(stream,"\nbottom =>\n");
box_fprint(stream,man,b,name_of_dim);
}
else if (b->p==0){
fprintf(stream,"\n");
box_fprint(stream,man,a,name_of_dim);
fprintf(stream,"=> bottom\n");
}
else {
bound_t bound;
bound_init(bound);
for(i=0; i<nbdims; i++){
int sgn1 = bound_cmp(a->p[i]->inf, b->p[i]->inf);
int sgn2 = bound_cmp(a->p[i]->sup, b->p[i]->sup);
if (sgn1!=0 || sgn2!=0){
if (name_of_dim)
fprintf(stream,"%8s in ",name_of_dim[i]);
else
fprintf(stream,"x%ld in ", (long)i);
itv_fprint(stream,a->p[i]);
fprintf(stream," => ");
str =
sgn1>0 ?
"-[" :
( sgn1<0 ?
"+[" :
"[=, " );
fprintf(stream,"%s",str);
if (sgn1!=0){
bound_neg(bound,b->p[i]->inf);
bound_fprint(stream,bound);
fprintf(stream,", ");
}
if (sgn2!=0){
bound_fprint(stream,b->p[i]->sup);
}
str =
sgn2>0 ?
"]+" :
( sgn2<0 ?
"]-" :
"=]" );
fprintf(stream,"%s\n",str);
}
else {
if (name_of_dim)
fprintf(stream,"%8s in [=,=]\n",name_of_dim[i]);
else
fprintf(stream,"x%ld in [=,=]\n",(long)i);
}
}
bound_clear(bound);
}
}
}
/* ********************************************************************** */
/* 4. Serialization */
/* ********************************************************************** */
/* Allocate a memory buffer (with malloc), output the abstract value in raw
binary format to it and return a pointer on the memory buffer and the size
of bytes written. It is the user responsability to free the memory
afterwards (with free). */
ap_membuf_t box_serialize_raw(ap_manager_t* man, box_t* a)
{
ap_membuf_t buf;
ap_manager_raise_exception(man,AP_EXC_NOT_IMPLEMENTED,AP_FUNID_SERIALIZE_RAW,"");
buf.ptr = NULL;
buf.size = 0;
return buf;
}
/* Return the abstract value read in raw binary format from the input stream
and store in size the number of bytes read */
box_t* box_deserialize_raw(ap_manager_t* man, void* ptr)
{
ap_manager_raise_exception(man,AP_EXC_NOT_IMPLEMENTED,AP_FUNID_DESERIALIZE_RAW,"");
return NULL;
}