OpenScop  0.9.0
vector.c
Go to the documentation of this file.
1 
2  /*+-----------------------------------------------------------------**
3  ** OpenScop Library **
4  **-----------------------------------------------------------------**
5  ** vector.c **
6  **-----------------------------------------------------------------**
7  ** First version: 30/04/2008 **
8  **-----------------------------------------------------------------**
9 
10 
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  *****************************************************************************/
62 
63 
64 #include <stdlib.h>
65 #include <stdio.h>
66 #include <ctype.h>
67 
68 #include <osl/macros.h>
69 #include <osl/util.h>
70 #include <osl/int.h>
71 #include <osl/vector.h>
72 
73 
74 /*+***************************************************************************
75  * Structure display function *
76  *****************************************************************************/
77 
78 
89 void osl_vector_idump(FILE * file, osl_vector_p vector, int level) {
90  int j;
91 
92  if (vector != NULL) {
93  // Go to the right level.
94  for (j = 0; j < level; j++)
95  fprintf(file,"|\t");
96  fprintf(file,"+-- osl_vector_t (");
97  osl_int_dump_precision(file, vector->precision);
98  fprintf(file, ")\n");
99 
100  for (j = 0; j <= level; j++)
101  fprintf(file,"|\t");
102  fprintf(file,"%d\n", vector->size);
103 
104  // Display the vector.
105  for (j = 0; j <= level; j++)
106  fprintf(file, "|\t");
107 
108  fprintf(file, "[ ");
109 
110  for (j = 0; j < vector->size; j++) {
111  osl_int_print(file, vector->precision, vector->v[j]);
112  fprintf(file, " ");
113  }
114 
115  fprintf(file, "]\n");
116  }
117  else {
118  // Go to the right level.
119  for (j = 0; j < level; j++)
120  fprintf(file, "|\t");
121  fprintf(file, "+-- NULL vector\n");
122  }
123 
124  // The last line.
125  for (j = 0; j <= level; j++)
126  fprintf(file, "|\t");
127  fprintf(file, "\n");
128 }
129 
130 
138 void osl_vector_dump(FILE * file, osl_vector_p vector) {
139  osl_vector_idump(file, vector, 0);
140 }
141 
142 
143 /*+***************************************************************************
144  * Memory allocation/deallocation function *
145  *****************************************************************************/
146 
147 
157 osl_vector_p osl_vector_pmalloc(int precision, int size) {
158  osl_vector_p vector;
159  int i;
160 
161  OSL_malloc(vector, osl_vector_p, sizeof(osl_vector_t));
162  vector->size = size;
163  vector->precision = precision;
164  if (size == 0) {
165  vector->v = NULL;
166  }
167  else {
168  OSL_malloc(vector->v, osl_int_t*, size * sizeof(osl_int_t));
169  for (i = 0; i < size; i++)
170  osl_int_init_set_si(precision, &vector->v[i], 0);
171  }
172  return vector;
173 }
174 
175 
187  int precision = osl_util_get_precision();
188  return osl_vector_pmalloc(precision, size);
189 }
190 
191 
198  int i;
199 
200  if (vector != NULL) {
201  if (vector->v != NULL) {
202  for (i = 0; i < vector->size; i++)
203  osl_int_clear(vector->precision, &vector->v[i]);
204 
205  free(vector->v);
206  }
207  free(vector);
208  }
209 }
210 
211 
212 /*+***************************************************************************
213  * Processing functions *
214  *****************************************************************************/
215 
216 
227  int i, precision, last;
228  osl_vector_p result;
229 
230  if ((vector == NULL) || (vector->size < 2))
231  OSL_error("incompatible vector for addition");
232 
233  precision = vector->precision;
234  last = vector->size - 1;
235 
236  result = osl_vector_pmalloc(precision, vector->size);
237  for (i = 0; i < vector->size; i++)
238  osl_int_assign(precision, &result->v[i], vector->v[i]);
239  osl_int_add_si(precision, &result->v[last], vector->v[last], scalar);
240 
241  return result;
242 }
243 
244 
255  int i;
256  osl_vector_p v3;
257 
258  if ((v1 == NULL) || (v2 == NULL) ||
259  (v1->size != v2->size) || (v1->precision != v2->precision))
260  OSL_error("incompatible vectors for addition");
261 
262  v3 = osl_vector_pmalloc(v1->precision, v1->size);
263  for (i = 0; i < v1->size; i++)
264  osl_int_add(v1->precision, &v3->v[i], v1->v[i], v2->v[i]);
265 
266  return v3;
267 }
268 
269 
280  int i;
281  osl_vector_p v3;
282 
283  if ((v1 == NULL) || (v2 == NULL) ||
284  (v1->size != v2->size) || (v1->precision != v2->precision))
285  OSL_error("incompatible vectors for subtraction");
286 
287  v3 = osl_vector_pmalloc(v1->precision, v1->size);
288  for (i = 0; i < v1->size; i++)
289  osl_int_sub(v1->precision, &v3->v[i], v1->v[i], v2->v[i]);
290 
291  return v3;
292 }
293 
294 
304  if ((vector == NULL) || (vector->size < 1))
305  OSL_error("vector cannot be tagged");
306  osl_int_set_si(vector->precision, &vector->v[0], 1);
307 }
308 
309 
319  if ((vector == NULL) || (vector->size < 1))
320  OSL_error("vector cannot be tagged");
321  osl_int_set_si(vector->precision, &vector->v[0], 0);
322 }
323 
324 
334  int i;
335 
336  if (v1 == v2)
337  return 1;
338 
339  if ((v1->size != v2->size) || (v1->precision != v2->precision))
340  return 0;
341 
342  for (i = 0; i < v1->size; i++)
343  if (osl_int_ne(v1->precision, v1->v[i], v2->v[i]))
344  return 0;
345 
346  return 1;
347 }
348 
349 
359  int i;
361 
362  for (i = 0; i < v->size; i++)
363  osl_int_mul_si(v->precision, &result->v[i], v->v[i], scalar);
364 
365  return result;
366 }
367 
368 
377  int i;
378 
379  if (vector == NULL)
380  return 0;
381 
382  for (i = 0; i < vector->size - 1; i++)
383  if (!osl_int_zero(vector->precision, vector->v[i]))
384  return 0;
385  return 1;
386 }
387 
osl_vector_p osl_vector_malloc(int size)
Definition: vector.c:186
int size
Definition: vector.h:86
int osl_vector_is_scalar(osl_vector_p vector)
Definition: vector.c:376
void osl_vector_free(osl_vector_p vector)
Definition: vector.c:197
int precision
Definition: vector.h:85
int osl_int_ne(int precision, osl_const_int_t val1, osl_const_int_t val2)
val1 != val2
Definition: int.c:1138
void osl_int_sub(int precision, osl_int_const_p variable, osl_const_int_t val1, osl_const_int_t val2)
variable = val1 - val2
Definition: int.c:685
void osl_int_clear(int precision, osl_int_const_p variable)
variable = 0 // including cleaning for GMP
Definition: int.c:382
Definition: int.h:79
void osl_int_init_set_si(int precision, osl_int_const_p variable, int i)
variable = i // including initialization for GMP
Definition: int.c:314
int osl_util_get_precision()
Definition: util.c:518
void osl_int_print(FILE *file, int precision, osl_const_int_t value)
Definition: int.c:423
osl_vector_p osl_vector_add(osl_vector_p v1, osl_vector_p v2)
Definition: vector.c:254
osl_vector_p osl_vector_pmalloc(int precision, int size)
Definition: vector.c:157
int osl_vector_equal(osl_vector_p v1, osl_vector_p v2)
Definition: vector.c:333
osl_vector_p osl_vector_add_scalar(osl_vector_p vector, int scalar)
Definition: vector.c:226
void osl_int_set_si(int precision, osl_int_const_p variable, int i)
variable = i
Definition: int.c:208
osl_int_t * v
Definition: vector.h:87
void osl_int_add(int precision, osl_int_const_p variable, osl_const_int_t val1, osl_const_int_t val2)
variable = val1 + val2
Definition: int.c:581
void osl_vector_tag_inequality(osl_vector_p vector)
Definition: vector.c:303
void osl_vector_idump(FILE *file, osl_vector_p vector, int level)
Definition: vector.c:89
void osl_int_add_si(int precision, osl_int_const_p variable, osl_const_int_t value, int i)
variable = val1 + i
Definition: int.c:631
void osl_vector_dump(FILE *file, osl_vector_p vector)
Definition: vector.c:138
void osl_int_assign(int precision, osl_int_const_p variable, osl_const_int_t value)
variable = value
Definition: int.c:179
osl_vector_p osl_vector_sub(osl_vector_p v1, osl_vector_p v2)
Definition: vector.c:279
void osl_int_mul_si(int precision, osl_int_const_p variable, osl_const_int_t value, int i)
variable = val1 * i
Definition: int.c:752
int osl_int_zero(int precision, osl_const_int_t value)
value == 0
Definition: int.c:1199
void osl_vector_tag_equality(osl_vector_p vector)
Definition: vector.c:318
osl_vector_p osl_vector_mul_scalar(osl_vector_p v, int scalar)
Definition: vector.c:358
void osl_int_dump_precision(FILE *file, int precision)
Definition: int.c:113