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_idump 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 void openscop_vector_idump(FILE * file
, openscop_vector_p vector
, int level
) {
90 // Go to the right level.
91 for (j
= 0; j
< level
; j
++)
93 fprintf(file
,"+-- openscop_vector_t\n");
95 for (j
= 0; j
<= level
; j
++)
97 fprintf(file
,"%d\n",vector
->size
);
99 // Display the vector.
100 for (j
= 0; j
<= level
; j
++)
105 for (j
= 0; j
< vector
->size
; j
++) {
106 OPENSCOP_INT_dump(file
,OPENSCOP_FMT
,vector
->v
[j
]);
113 // Go to the right level.
114 for (j
= 0; j
< level
; j
++)
116 fprintf(file
,"+-- NULL vector\n");
120 for (j
= 0; j
<= level
; j
++)
127 * openscop_vector_dump function:
128 * This function prints the content of a openscop_vector_t structure
129 * (*vector) into a file (file, possibly stdout).
130 * \param file File where informations are printed.
131 * \param vector The vector whose information have to be printed.
133 void openscop_vector_dump(FILE * file
, openscop_vector_p vector
) {
134 openscop_vector_idump(file
,vector
,0);
138 /*+***************************************************************************
139 * Memory allocation/deallocation function *
140 *****************************************************************************/
144 * openscop_vector_malloc function:
145 * This function allocates the memory space for a openscop_vector_t structure
146 * and sets its fields with default values. Then it returns a pointer to the
148 * \param size The number of entries of the vector to allocate.
149 * \return A pointer to the newly allocated openscop_vector_t structure.
151 openscop_vector_p
openscop_vector_malloc(unsigned size
) {
152 openscop_vector_p vector
;
156 OPENSCOP_malloc(vector
, openscop_vector_p
, sizeof(openscop_vector_t
));
162 OPENSCOP_malloc(p
, openscop_int_t
*, size
* sizeof(openscop_int_t
));
164 for (i
= 0; i
< size
; i
++)
165 OPENSCOP_INT_init_set_si(vector
->v
[i
],0);
172 * openscop_vector_free function:
173 * This function frees the allocated memory for a openscop_vector_t structure.
174 * \param vector The pointer to the vector we want to free.
176 void openscop_vector_free(openscop_vector_p vector
) {
180 if (vector
!= NULL
) {
182 for (i
= 0; i
< vector
->size
; i
++)
183 OPENSCOP_INT_clear(*p
++);
191 /*+***************************************************************************
192 * Processing functions *
193 *****************************************************************************/
197 * openscop_vector_add_scalar function:
198 * This function adds a scalar to the vector representation of an affine
199 * expression (this means we add the scalar only to the very last entry of the
200 * vector). It returns a new vector resulting from this addition.
201 * \param vector The basis vector.
202 * \param scalar The scalar to add to the vector.
203 * \return A pointer to a new vector, copy of the basis one plus the scalar.
205 openscop_vector_p
openscop_vector_add_scalar(openscop_vector_p vector
,
208 openscop_vector_p result
;
210 if ((vector
== NULL
) || (vector
->size
< 2))
211 OPENSCOP_error("incompatible vector for addition");
213 result
= openscop_vector_malloc(vector
->size
);
214 for (i
= 0; i
< vector
->size
; i
++)
215 OPENSCOP_INT_assign(result
->v
[i
],vector
->v
[i
]);
216 OPENSCOP_INT_add_int(result
->v
[vector
->size
- 1],
217 vector
->v
[vector
->size
- 1],scalar
);
224 * openscop_vector_add function:
225 * This function achieves the addition of two vectors and returns the
226 * result as a new vector (the addition means the ith entry of the new vector
227 * is equal to the ith entry of vector v1 plus the ith entry of vector v2).
228 * \param v1 The first vector for the addition.
229 * \param v2 The second vector for the addition.
230 * \return A pointer to a new vector, corresponding to v1 + v2.
232 openscop_vector_p
openscop_vector_add(openscop_vector_p v1
,
233 openscop_vector_p v2
) {
235 openscop_vector_p v3
;
237 if ((v1
== NULL
) || (v2
== NULL
) || (v1
->size
!= v2
->size
))
238 OPENSCOP_error("incompatible vectors for addition");
240 v3
= openscop_vector_malloc(v1
->size
);
241 for (i
= 0; i
< v1
->size
; i
++)
242 OPENSCOP_INT_addto(v3
->v
[i
],v1
->v
[i
],v2
->v
[i
]);
249 * openscop_vector_sub function:
250 * This function achieves the subtraction of two vectors and returns the
251 * result as a new vector (the addition means the ith entry of the new vector
252 * is equal to the ith entry of vector v1 minus the ith entry of vector v2).
253 * \param v1 The first vector for the subtraction.
254 * \param v2 The second vector for the subtraction (result is v1-v2).
255 * \return A pointer to a new vector, corresponding to v1 - v2.
257 openscop_vector_p
openscop_vector_sub(openscop_vector_p v1
,
258 openscop_vector_p v2
) {
260 openscop_vector_p v3
;
262 if ((v1
== NULL
) || (v2
== NULL
) || (v1
->size
!= v2
->size
))
263 OPENSCOP_error("incompatible vectors for subtraction");
265 v3
= openscop_vector_malloc(v1
->size
);
266 for (i
= 0; i
< v1
->size
; i
++)
267 OPENSCOP_INT_subtract(v3
->v
[i
],v1
->v
[i
],v2
->v
[i
]);
274 * openscop_vector_tag_inequality function:
275 * This function tags a vector representation of a contraint as being an
276 * inequality >=0. This means in the PolyLib format, to set to 1 the very
277 * first entry of the vector. It modifies directly the vector provided as
279 * \param vector The vector to be tagged.
281 void openscop_vector_tag_inequality(openscop_vector_p vector
) {
282 if ((vector
== NULL
) || (vector
->size
< 1))
283 OPENSCOP_error("vector cannot be tagged");
284 OPENSCOP_INT_set_si(vector
->v
[0],1);
289 * openscop_vector_tag_equality function:
290 * This function tags a vector representation of a contraint as being an
291 * equality ==0. This means in the PolyLib format, to set to 0 the very
292 * first entry of the vector. It modifies directly the vector provided as
294 * \param vector The vector to be tagged.
296 void openscop_vector_tag_equality(openscop_vector_p vector
) {
297 if ((vector
== NULL
) || (vector
->size
< 1))
298 OPENSCOP_error("vector cannot be tagged");
299 OPENSCOP_INT_set_si(vector
->v
[0],0);
304 * openscop_vector_equal function:
305 * this function returns true if the two vectors are the same, false
307 * \param v1 The first vector.
308 * \param v2 The second vector.
309 * \return 1 if v1 and v2 are the same (content-wise), 0 otherwise.
311 int openscop_vector_equal(openscop_vector_p v1
, openscop_vector_p v2
) {
317 if (v1
->size
!= v2
->size
)
320 for (i
= 0; i
< v1
->size
; i
++)
321 if (OPENSCOP_INT_ne(v1
->v
[i
], v2
->v
[i
]))
329 * openscop_vector_mul_scalar function:
330 * this function returns a new vector corresponding to the one provided
331 * as parameter with each entry multiplied by a scalar.
332 * \param v The vector to multiply.
333 * \param scalar The scalar coefficient.
334 * \return A new vector corresponding to scalar * v.
336 openscop_vector_p
openscop_vector_mul_scalar(openscop_vector_p v
,
339 openscop_vector_p result
= openscop_vector_malloc(v
->size
);
341 for(i
= 0; i
< v
->size
; i
++)
342 OPENSCOP_INT_multo(result
->v
[i
], scalar
, v
->v
[i
]);