2 /*+-----------------------------------------------------------------**
4 **-----------------------------------------------------------------**
6 **-----------------------------------------------------------------**
7 ** First version: 30/04/2008 **
8 **-----------------------------------------------------------------**
11 *****************************************************************************
12 * OpenScop: Structures and formats for polyhedral tools to talk together *
13 *****************************************************************************
14 * ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__, *
15 * / / / // // // // / / / // // / / // / /|,_, *
16 * / / / // // // // / / / // // / / // / / / /\ *
17 * |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/ \ *
18 * | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\ \ /\ *
19 * | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\ *
20 * | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \ \ *
21 * | P |n| l | = | s | t |=| = |d| = | = | = | | |=| o | | \# \ \ *
22 * | H | | y | | e | o | | = |l| | | = | | | | G | | \ \ \ *
23 * | I | | | | e | | | | | | | | | | | | | \ \ \ *
24 * | T | | | | | | | | | | | | | | | | | \ \ \ *
25 * | E | | | | | | | | | | | | | | | | | \ \ \ *
26 * | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | / \* \ \ *
27 * | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/ \ \ / *
28 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
30 * Copyright (C) 2008 University Paris-Sud 11 and INRIA *
32 * (3-clause BSD license) *
33 * Redistribution and use in source and binary forms, with or without *
34 * modification, are permitted provided that the following conditions *
37 * 1. Redistributions of source code must retain the above copyright notice, *
38 * this list of conditions and the following disclaimer. *
39 * 2. Redistributions in binary form must reproduce the above copyright *
40 * notice, this list of conditions and the following disclaimer in the *
41 * documentation and/or other materials provided with the distribution. *
42 * 3. The name of the author may not be used to endorse or promote products *
43 * derived from this software without specific prior written permission. *
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR *
46 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES *
47 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. *
48 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, *
49 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT *
50 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
51 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
52 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
53 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF *
54 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
56 * OpenScop Library, a library to manipulate OpenScop formats and data *
57 * structures. Written by: *
58 * Cedric Bastoul <Cedric.Bastoul@u-psud.fr> and *
59 * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> *
61 *****************************************************************************/
67 # include <openscop/vector.h>
70 /*+****************************************************************************
71 * Structure display function *
72 ******************************************************************************/
76 * openscop_vector_print_structure function:
77 * Displays a openscop_vector_t structure (*vector) into a file (file, possibly
78 * stdout) in a way that trends to be understandable without falling in a deep
79 * depression or, for the lucky ones, getting a headache... It includes an
80 * indentation level (level) in order to work with others print_structure
82 * \param file File where informations are printed.
83 * \param vector The vector whose information have to be printed.
84 * \param level Number of spaces before printing, for each line.
86 * - 01/05/2008: first version.
89 openscop_vector_print_structure(FILE * file
, openscop_vector_p vector
, int level
)
95 /* Go to the right level. */
96 for (j
= 0; j
< level
; j
++)
98 fprintf(file
,"+-- openscop_vector_t\n");
100 for (j
= 0; j
<= level
; j
++)
102 fprintf(file
,"%d\n",vector
->Size
);
104 /* Display the vector. */
105 for (j
= 0; j
<= level
; j
++)
110 for (j
= 0; j
< vector
->Size
; j
++)
112 SCOPINT_print(file
,OPENSCOP_FMT
,vector
->p
[j
]);
120 /* Go to the right level. */
121 for (j
= 0; j
< level
; j
++)
123 fprintf(file
,"+-- NULL vector\n");
127 for (j
= 0; j
<= level
; j
++)
134 * openscop_vector_print function:
135 * This function prints the content of a openscop_vector_t structure
136 * (*vector) into a file (file, possibly stdout).
137 * \param file File where informations are printed.
138 * \param vector The vector whose information have to be printed.
140 * - 01/05/2008: first version.
143 openscop_vector_print(FILE * file
, openscop_vector_p vector
)
145 openscop_vector_print_structure(file
,vector
,0);
149 /*+****************************************************************************
150 * Memory allocation/deallocation function *
151 ******************************************************************************/
155 * openscop_vector_malloc function:
156 * This function allocates the memory space for a openscop_vector_t structure and
157 * sets its fields with default values. Then it returns a pointer to the
159 * \param Size The number of entries of the vector to allocate.
161 * - 01/05/2008: first version.
164 openscop_vector_malloc(unsigned Size
)
166 openscop_vector_p vector
;
170 vector
= (openscop_vector_p
)malloc(sizeof(openscop_vector_t
));
173 fprintf(stderr
, "[Scoplib] Memory Overflow.\n");
181 p
= (openscop_int_t
*)malloc(Size
* sizeof(openscop_int_t
));
184 fprintf(stderr
, "[Scoplib] Memory Overflow.\n");
188 for (i
= 0; i
< Size
; i
++)
189 SCOPINT_init_set_si(vector
->p
[i
],0);
196 * openscop_vector_free function:
197 * This function frees the allocated memory for a openscop_vector_t structure.
198 * \param vector The pointer to the vector we want to free.
200 * - 01/05/2008: first version.
203 openscop_vector_free(openscop_vector_p vector
)
211 for (i
= 0; i
< vector
->Size
; i
++)
220 /*+****************************************************************************
221 * Processing functions *
222 ******************************************************************************/
225 * openscop_vector_add_scalar function:
226 * This function adds a scalar to the vector representation of an affine
227 * expression (this means we add the scalar only to the very last entry of the
228 * vector). It returns a new vector resulting from this addition.
229 * \param vector The basis vector.
230 * \param scalar The scalar to add to the vector.
232 * - 01/05/2008: first version.
235 openscop_vector_add_scalar(openscop_vector_p vector
, int scalar
)
238 openscop_vector_p result
;
240 if ((vector
== NULL
) || (vector
->Size
< 2))
242 fprintf(stderr
,"[Scoplib] Error: incompatible vector for addition\n");
246 result
= openscop_vector_malloc(vector
->Size
);
247 for (i
= 0; i
< vector
->Size
; i
++)
248 SCOPINT_assign(result
->p
[i
],vector
->p
[i
]);
249 SCOPINT_add_int(result
->p
[vector
->Size
- 1],
250 vector
->p
[vector
->Size
- 1],scalar
);
257 * openscop_vector_add function:
258 * This function achieves the addition of two vectors and returns the
259 * result as a new vector (the addition means the ith entry of the new vector
260 * is equal to the ith entry of vector v1 plus the ith entry of vector v2).
261 * \param v1 The first vector for the addition.
262 * \param v2 The second vector for the addition (result is v1+v2).
264 * - 01/05/2008: first version.
267 openscop_vector_add(openscop_vector_p v1
, openscop_vector_p v2
)
270 openscop_vector_p v3
;
272 if ((v1
== NULL
) || (v2
== NULL
) || (v1
->Size
!= v2
->Size
))
274 fprintf(stderr
,"[Scoplib] Error: incompatible vectors for addition\n");
278 v3
= openscop_vector_malloc(v1
->Size
);
279 for (i
= 0; i
< v1
->Size
; i
++)
280 SCOPINT_addto(v3
->p
[i
],v1
->p
[i
],v2
->p
[i
]);
287 * openscop_vector_sub function:
288 * This function achieves the subtraction of two vectors and returns the
289 * result as a new vector (the addition means the ith entry of the new vector
290 * is equal to the ith entry of vector v1 minus the ith entry of vector v2).
291 * \param v1 The first vector for the subtraction.
292 * \param v2 The second vector for the subtraction (result is v1-v2).
294 * - 01/05/2008: first version.
297 openscop_vector_sub(openscop_vector_p v1
, openscop_vector_p v2
)
300 openscop_vector_p v3
;
302 if ((v1
== NULL
) || (v2
== NULL
) || (v1
->Size
!= v2
->Size
))
304 fprintf(stderr
,"[Scoplib] Error: incompatible vectors for subtraction\n");
308 v3
= openscop_vector_malloc(v1
->Size
);
309 for (i
= 0; i
< v1
->Size
; i
++)
310 SCOPINT_subtract(v3
->p
[i
],v1
->p
[i
],v2
->p
[i
]);
317 * openscop_vector_tag_inequality function:
318 * This function tags a vector representation of a contraint as being an
319 * inequality >=0. This means in the PolyLib format, to set to 1 the very first
320 * entry of the vector. It modifies directly the vector provided as an argument.
321 * \param vector The vector to be tagged.
323 * - 01/05/2008: first version.
326 openscop_vector_tag_inequality(openscop_vector_p vector
)
328 if ((vector
== NULL
) || (vector
->Size
< 1))
330 fprintf(stderr
,"[Scoplib] Error: vector cannot be tagged\n");
333 SCOPINT_set_si(vector
->p
[0],1);
338 * openscop_vector_tag_equality function:
339 * This function tags a vector representation of a contraint as being an
340 * equality ==0. This means in the PolyLib format, to set to 0 the very first
341 * entry of the vector. It modifies directly the vector provided as an argument.
342 * \param vector The vector to be tagged.
344 * - 01/05/2008: first version.
347 openscop_vector_tag_equality(openscop_vector_p vector
)
349 if ((vector
== NULL
) || (vector
->Size
< 1))
351 fprintf(stderr
,"[Scoplib] Error: vector cannot be tagged\n");
354 SCOPINT_set_si(vector
->p
[0],0);
359 * openscop_vector_equal function:
360 * this function returns true if the two vectors are the same, false
362 * \param v1 The first vector.
363 * \param v2 The second vector.
364 * \return 1 if v1 and v2 are the same (content-wise), 0 otherwise.
367 openscop_vector_equal(openscop_vector_p v1
, openscop_vector_p v2
)
371 if (v1
->Size
!= v2
->Size
)
374 for (i
= 0; i
< v1
->Size
; i
++)
375 if (SCOPINT_ne(v1
->p
[i
], v2
->p
[i
]))