Hybrid precision (32, 64, GMP)
[openscop.git] / source / vector.c
blob32cffdae689502ace0c7a4044ef7a124c7e42bc8
2 /*+-----------------------------------------------------------------**
3 ** OpenScop Library **
4 **-----------------------------------------------------------------**
5 ** vector.c **
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 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' *
29 * *
30 * Copyright (C) 2008 University Paris-Sud 11 and INRIA *
31 * *
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 *
35 * are met: *
36 * *
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. *
44 * *
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. *
55 * *
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> *
60 * *
61 *****************************************************************************/
64 # include <stdlib.h>
65 # include <stdio.h>
66 # include <ctype.h>
67 # include <openscop/vector.h>
70 /*+***************************************************************************
71 * Structure display function *
72 *****************************************************************************/
75 /**
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
81 * functions.
82 * \param[in] file File where informations are printed.
83 * \param[in] vector The vector whose information have to be printed.
84 * \param[in] level Number of spaces before printing, for each line.
86 void openscop_vector_idump(FILE * file, openscop_vector_p vector, int level) {
87 int j;
89 if (vector != NULL) {
90 // Go to the right level.
91 for (j = 0; j < level; j++)
92 fprintf(file,"|\t");
93 fprintf(file,"+-- openscop_vector_t (");
94 openscop_int_dump_precision(file, vector->precision);
95 fprintf(file, ")\n");
97 for (j = 0; j <= level; j++)
98 fprintf(file,"|\t");
99 fprintf(file,"%d\n", vector->size);
101 // Display the vector.
102 for (j = 0; j <= level; j++)
103 fprintf(file, "|\t");
105 fprintf(file, "[ ");
107 for (j = 0; j < vector->size; j++) {
108 openscop_int_print(file, vector->precision, vector->v, j);
109 fprintf(file, " ");
112 fprintf(file, "]\n");
114 else {
115 // Go to the right level.
116 for (j = 0; j < level; j++)
117 fprintf(file, "|\t");
118 fprintf(file, "+-- NULL vector\n");
121 // The last line.
122 for (j = 0; j <= level; j++)
123 fprintf(file, "|\t");
124 fprintf(file, "\n");
129 * openscop_vector_dump function:
130 * This function prints the content of a openscop_vector_t structure
131 * (*vector) into a file (file, possibly stdout).
132 * \param[in] file File where informations are printed.
133 * \param[in] vector The vector whose information have to be printed.
135 void openscop_vector_dump(FILE * file, openscop_vector_p vector) {
136 openscop_vector_idump(file, vector, 0);
140 /*+***************************************************************************
141 * Memory allocation/deallocation function *
142 *****************************************************************************/
146 * openscop_vector_pmalloc function:
147 * (precision malloc) this function allocates the memory space for an
148 * openscop_vector_t structure and sets its fields with default values. Then
149 * it returns a pointer to the allocated space.
150 * \param[in] precision The precision of the vector entries.
151 * \param[in] size The number of entries of the vector to allocate.
152 * \return A pointer to the newly allocated openscop_vector_t structure.
154 openscop_vector_p openscop_vector_pmalloc(int precision, int size) {
155 openscop_vector_p vector;
156 int i;
158 OPENSCOP_malloc(vector, openscop_vector_p, sizeof(openscop_vector_t));
159 vector->size = size;
160 vector->precision = precision;
161 if (size == 0) {
162 vector->v = NULL;
164 else {
165 OPENSCOP_malloc(vector->v, void *, size * openscop_int_sizeof(precision));
166 for (i = 0; i < size; i++)
167 openscop_int_init_set_si(precision, vector->v, i, 0);
169 return vector;
174 * openscop_vector_malloc function:
175 * This function allocates the memory space for a openscop_vector_t structure
176 * and sets its fields with default values. Then it returns a pointer to the
177 * allocated space. The precision of the vector elements corresponds to the
178 * precision environment variable or to the highest available precision if it
179 * is not defined.
180 * \param[in] size The number of entries of the vector to allocate.
181 * \return A pointer to the newly allocated openscop_vector_t structure.
183 openscop_vector_p openscop_vector_malloc(int size) {
184 int precision = openscop_util_get_precision();
185 return openscop_vector_pmalloc(precision, size);
190 * openscop_vector_free function:
191 * This function frees the allocated memory for a openscop_vector_t structure.
192 * \param[in] vector The pointer to the vector we want to free.
194 void openscop_vector_free(openscop_vector_p vector) {
195 int i;
197 if (vector != NULL) {
198 for (i = 0; i < vector->size; i++)
199 openscop_int_clear(vector->precision, vector->v, i);
201 free(vector->v);
202 free(vector);
207 /*+***************************************************************************
208 * Processing functions *
209 *****************************************************************************/
213 * openscop_vector_add_scalar function:
214 * This function adds a scalar to the vector representation of an affine
215 * expression (this means we add the scalar only to the very last entry of the
216 * vector). It returns a new vector resulting from this addition.
217 * \param[in] vector The basis vector.
218 * \param[in] scalar The scalar to add to the vector.
219 * \return A pointer to a new vector, copy of the basis one plus the scalar.
221 openscop_vector_p openscop_vector_add_scalar(openscop_vector_p vector,
222 int scalar) {
223 int i, precision, last;
224 openscop_vector_p result;
226 if ((vector == NULL) || (vector->size < 2))
227 OPENSCOP_error("incompatible vector for addition");
229 precision = vector->precision;
230 last = vector->size - 1;
232 result = openscop_vector_pmalloc(precision, vector->size);
233 for (i = 0; i < vector->size; i++)
234 openscop_int_assign(precision, result->v, i, vector->v, i);
235 openscop_int_add_ui(precision, result->v, last, vector->v, last, scalar);
237 return result;
242 * openscop_vector_add function:
243 * This function achieves the addition of two vectors and returns the
244 * result as a new vector (the addition means the ith entry of the new vector
245 * is equal to the ith entry of vector v1 plus the ith entry of vector v2).
246 * \param v1 The first vector for the addition.
247 * \param v2 The second vector for the addition.
248 * \return A pointer to a new vector, corresponding to v1 + v2.
250 openscop_vector_p openscop_vector_add(openscop_vector_p v1,
251 openscop_vector_p v2) {
252 int i;
253 openscop_vector_p v3;
255 if ((v1 == NULL) || (v2 == NULL) ||
256 (v1->size != v2->size) || (v1->precision != v2->precision))
257 OPENSCOP_error("incompatible vectors for addition");
259 v3 = openscop_vector_pmalloc(v1->precision, v1->size);
260 for (i = 0; i < v1->size; i++)
261 openscop_int_add(v1->precision, v3->v, i, v1->v, i, v2->v, i);
263 return v3;
268 * openscop_vector_sub function:
269 * This function achieves the subtraction of two vectors and returns the
270 * result as a new vector (the addition means the ith entry of the new vector
271 * is equal to the ith entry of vector v1 minus the ith entry of vector v2).
272 * \param v1 The first vector for the subtraction.
273 * \param v2 The second vector for the subtraction (result is v1-v2).
274 * \return A pointer to a new vector, corresponding to v1 - v2.
276 openscop_vector_p openscop_vector_sub(openscop_vector_p v1,
277 openscop_vector_p v2) {
278 int i;
279 openscop_vector_p v3;
281 if ((v1 == NULL) || (v2 == NULL) ||
282 (v1->size != v2->size) || (v1->precision != v2->precision))
283 OPENSCOP_error("incompatible vectors for subtraction");
285 v3 = openscop_vector_pmalloc(v1->precision, v1->size);
286 for (i = 0; i < v1->size; i++)
287 openscop_int_sub(v1->precision, v3->v, i, v1->v, i, v2->v, i);
289 return v3;
294 * openscop_vector_tag_inequality function:
295 * This function tags a vector representation of a contraint as being an
296 * inequality >=0. This means in the PolyLib format, to set to 1 the very
297 * first entry of the vector. It modifies directly the vector provided as
298 * an argument.
299 * \param vector The vector to be tagged.
301 void openscop_vector_tag_inequality(openscop_vector_p vector) {
302 if ((vector == NULL) || (vector->size < 1))
303 OPENSCOP_error("vector cannot be tagged");
304 openscop_int_set_si(vector->precision, vector->v, 0, 1);
309 * openscop_vector_tag_equality function:
310 * This function tags a vector representation of a contraint as being an
311 * equality ==0. This means in the PolyLib format, to set to 0 the very
312 * first entry of the vector. It modifies directly the vector provided as
313 * an argument.
314 * \param vector The vector to be tagged.
316 void openscop_vector_tag_equality(openscop_vector_p vector) {
317 if ((vector == NULL) || (vector->size < 1))
318 OPENSCOP_error("vector cannot be tagged");
319 openscop_int_set_si(vector->precision, vector->v, 0, 0);
324 * openscop_vector_equal function:
325 * this function returns true if the two vectors are the same, false
326 * otherwise.
327 * \param v1 The first vector.
328 * \param v2 The second vector.
329 * \return 1 if v1 and v2 are the same (content-wise), 0 otherwise.
331 int openscop_vector_equal(openscop_vector_p v1, openscop_vector_p v2) {
332 int i;
334 if (v1 == v2)
335 return 1;
337 if ((v1->size != v2->size) || (v1->precision != v2->precision))
338 return 0;
340 for (i = 0; i < v1->size; i++)
341 if (openscop_int_ne(v1->precision, v1->v, i, v2->v, i))
342 return 0;
344 return 1;
349 * openscop_vector_mul_scalar function:
350 * this function returns a new vector corresponding to the one provided
351 * as parameter with each entry multiplied by a scalar.
352 * \param v The vector to multiply.
353 * \param scalar The scalar coefficient.
354 * \return A new vector corresponding to scalar * v.
356 openscop_vector_p openscop_vector_mul_scalar(openscop_vector_p v,
357 int scalar) {
358 int i;
359 openscop_vector_p result = openscop_vector_pmalloc(v->precision, v->size);
361 for(i = 0; i < v->size; i++)
362 openscop_int_mul_si(v->precision, result->v, i, v->v, i, scalar);
364 return result;