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.
87 openscop_vector_print_structure(FILE * file
, openscop_vector_p vector
,
94 // Go to the right level.
95 for (j
= 0; j
< level
; j
++)
97 fprintf(file
,"+-- openscop_vector_t\n");
99 for (j
= 0; j
<= level
; j
++)
101 fprintf(file
,"%d\n",vector
->size
);
103 // Display the vector.
104 for (j
= 0; j
<= level
; j
++)
109 for (j
= 0; j
< vector
->size
; j
++)
111 SCOPINT_print(file
,OPENSCOP_FMT
,vector
->v
[j
]);
119 // Go to the right level.
120 for (j
= 0; j
< level
; j
++)
122 fprintf(file
,"+-- NULL vector\n");
126 for (j
= 0; j
<= level
; j
++)
133 * openscop_vector_print function:
134 * This function prints the content of a openscop_vector_t structure
135 * (*vector) into a file (file, possibly stdout).
136 * \param file File where informations are printed.
137 * \param vector The vector whose information have to be printed.
140 openscop_vector_print(FILE * file
, openscop_vector_p vector
)
142 openscop_vector_print_structure(file
,vector
,0);
146 /*+***************************************************************************
147 * Memory allocation/deallocation function *
148 *****************************************************************************/
152 * openscop_vector_malloc function:
153 * This function allocates the memory space for a openscop_vector_t structure
154 * and sets its fields with default values. Then it returns a pointer to the
156 * \param size The number of entries of the vector to allocate.
157 * \return A pointer to the newly allocated openscop_vector_t structure.
160 openscop_vector_malloc(unsigned size
)
162 openscop_vector_p vector
;
166 vector
= (openscop_vector_p
)malloc(sizeof(openscop_vector_t
));
169 fprintf(stderr
, "[OpenScop] Error: memory overflow.\n");
177 p
= (openscop_int_t
*)malloc(size
* sizeof(openscop_int_t
));
180 fprintf(stderr
, "[OpenScop] Error: memory overflow.\n");
184 for (i
= 0; i
< size
; i
++)
185 SCOPINT_init_set_si(vector
->v
[i
],0);
192 * openscop_vector_free function:
193 * This function frees the allocated memory for a openscop_vector_t structure.
194 * \param vector The pointer to the vector we want to free.
197 openscop_vector_free(openscop_vector_p vector
)
205 for (i
= 0; i
< vector
->size
; i
++)
214 /*+***************************************************************************
215 * Processing functions *
216 *****************************************************************************/
220 * openscop_vector_add_scalar function:
221 * This function adds a scalar to the vector representation of an affine
222 * expression (this means we add the scalar only to the very last entry of the
223 * vector). It returns a new vector resulting from this addition.
224 * \param vector The basis vector.
225 * \param scalar The scalar to add to the vector.
226 * \return A pointer to a new vector, copy of the basis one plus the scalar.
229 openscop_vector_add_scalar(openscop_vector_p vector
, int scalar
)
232 openscop_vector_p result
;
234 if ((vector
== NULL
) || (vector
->size
< 2))
236 fprintf(stderr
,"[OpenScop] Error: incompatible vector for addition.\n");
240 result
= openscop_vector_malloc(vector
->size
);
241 for (i
= 0; i
< vector
->size
; i
++)
242 SCOPINT_assign(result
->v
[i
],vector
->v
[i
]);
243 SCOPINT_add_int(result
->v
[vector
->size
- 1],
244 vector
->v
[vector
->size
- 1],scalar
);
251 * openscop_vector_add function:
252 * This function achieves the addition of two vectors and returns the
253 * result as a new vector (the addition means the ith entry of the new vector
254 * is equal to the ith entry of vector v1 plus the ith entry of vector v2).
255 * \param v1 The first vector for the addition.
256 * \param v2 The second vector for the addition.
257 * \return A pointer to a new vector, corresponding to v1 + v2.
260 openscop_vector_add(openscop_vector_p v1
, openscop_vector_p v2
)
263 openscop_vector_p v3
;
265 if ((v1
== NULL
) || (v2
== NULL
) || (v1
->size
!= v2
->size
))
267 fprintf(stderr
,"[OpenScop] Error: incompatible vectors for addition.\n");
271 v3
= openscop_vector_malloc(v1
->size
);
272 for (i
= 0; i
< v1
->size
; i
++)
273 SCOPINT_addto(v3
->v
[i
],v1
->v
[i
],v2
->v
[i
]);
280 * openscop_vector_sub function:
281 * This function achieves the subtraction of two vectors and returns the
282 * result as a new vector (the addition means the ith entry of the new vector
283 * is equal to the ith entry of vector v1 minus the ith entry of vector v2).
284 * \param v1 The first vector for the subtraction.
285 * \param v2 The second vector for the subtraction (result is v1-v2).
286 * \return A pointer to a new vector, corresponding to v1 - v2.
289 openscop_vector_sub(openscop_vector_p v1
, openscop_vector_p v2
)
292 openscop_vector_p v3
;
294 if ((v1
== NULL
) || (v2
== NULL
) || (v1
->size
!= v2
->size
))
296 fprintf(stderr
,"[OpenScop] Error: "
297 "incompatible vectors for subtraction.\n");
301 v3
= openscop_vector_malloc(v1
->size
);
302 for (i
= 0; i
< v1
->size
; i
++)
303 SCOPINT_subtract(v3
->v
[i
],v1
->v
[i
],v2
->v
[i
]);
310 * openscop_vector_tag_inequality function:
311 * This function tags a vector representation of a contraint as being an
312 * inequality >=0. This means in the PolyLib format, to set to 1 the very
313 * first entry of the vector. It modifies directly the vector provided as
315 * \param vector The vector to be tagged.
318 openscop_vector_tag_inequality(openscop_vector_p vector
)
320 if ((vector
== NULL
) || (vector
->size
< 1))
322 fprintf(stderr
,"[OpenScop] Error: vector cannot be tagged.\n");
325 SCOPINT_set_si(vector
->v
[0],1);
330 * openscop_vector_tag_equality function:
331 * This function tags a vector representation of a contraint as being an
332 * equality ==0. This means in the PolyLib format, to set to 0 the very
333 * first entry of the vector. It modifies directly the vector provided as
335 * \param vector The vector to be tagged.
338 openscop_vector_tag_equality(openscop_vector_p vector
)
340 if ((vector
== NULL
) || (vector
->size
< 1))
342 fprintf(stderr
,"[OpenScop] Error: vector cannot be tagged.\n");
345 SCOPINT_set_si(vector
->v
[0],0);
350 * openscop_vector_equal function:
351 * this function returns true if the two vectors are the same, false
353 * \param v1 The first vector.
354 * \param v2 The second vector.
355 * \return 1 if v1 and v2 are the same (content-wise), 0 otherwise.
358 openscop_vector_equal(openscop_vector_p v1
, openscop_vector_p v2
)
362 if (v1
->size
!= v2
->size
)
365 for (i
= 0; i
< v1
->size
; i
++)
366 if (SCOPINT_ne(v1
->v
[i
], v2
->v
[i
]))
374 * openscop_vector_mul_scalar function:
375 * this function returns a new vector corresponding to the one provided
376 * as parameter with each entry multiplied by a scalar.
377 * \param v The vector to multiply.
378 * \param scalar The scalar coefficient.
379 * \return A new vector corresponding to scalar * v.
381 openscop_vector_p
openscop_vector_mul_scalar(openscop_vector_p v
,
385 openscop_vector_p result
= openscop_vector_malloc(v
->size
);
387 for(i
= 0; i
< v
->size
; i
++)
388 SCOPINT_multo(result
->v
[i
], scalar
, v
->v
[i
]);