OpenScop
0.9.0
|
00001 00002 /*+-----------------------------------------------------------------** 00003 ** OpenScop Library ** 00004 **-----------------------------------------------------------------** 00005 ** vector.c ** 00006 **-----------------------------------------------------------------** 00007 ** First version: 30/04/2008 ** 00008 **-----------------------------------------------------------------** 00009 00010 00011 ***************************************************************************** 00012 * OpenScop: Structures and formats for polyhedral tools to talk together * 00013 ***************************************************************************** 00014 * ,___,,_,__,,__,,__,,__,,_,__,,_,__,,__,,___,_,__,,_,__, * 00015 * / / / // // // // / / / // // / / // / /|,_, * 00016 * / / / // // // // / / / // // / / // / / / /\ * 00017 * |~~~|~|~~~|~~~|~~~|~~~|~|~~~|~|~~~|~~~|~~~|~|~~~|~|~~~|/_/ \ * 00018 * | G |C| P | = | L | P |=| = |C| = | = | = |=| = |=| C |\ \ /\ * 00019 * | R |l| o | = | e | l |=| = |a| = | = | = |=| = |=| L | \# \ /\ * 00020 * | A |a| l | = | t | u |=| = |n| = | = | = |=| = |=| o | |\# \ \ * 00021 * | P |n| l | = | s | t |=| = |d| = | = | = | | |=| o | | \# \ \ * 00022 * | H | | y | | e | o | | = |l| | | = | | | | G | | \ \ \ * 00023 * | I | | | | e | | | | | | | | | | | | | \ \ \ * 00024 * | T | | | | | | | | | | | | | | | | | \ \ \ * 00025 * | E | | | | | | | | | | | | | | | | | \ \ \ * 00026 * | * |*| * | * | * | * |*| * |*| * | * | * |*| * |*| * | / \* \ \ * 00027 * | O |p| e | n | S | c |o| p |-| L | i | b |r| a |r| y |/ \ \ / * 00028 * '---'-'---'---'---'---'-'---'-'---'---'---'-'---'-'---' '--' * 00029 * * 00030 * Copyright (C) 2008 University Paris-Sud 11 and INRIA * 00031 * * 00032 * (3-clause BSD license) * 00033 * Redistribution and use in source and binary forms, with or without * 00034 * modification, are permitted provided that the following conditions * 00035 * are met: * 00036 * * 00037 * 1. Redistributions of source code must retain the above copyright notice, * 00038 * this list of conditions and the following disclaimer. * 00039 * 2. Redistributions in binary form must reproduce the above copyright * 00040 * notice, this list of conditions and the following disclaimer in the * 00041 * documentation and/or other materials provided with the distribution. * 00042 * 3. The name of the author may not be used to endorse or promote products * 00043 * derived from this software without specific prior written permission. * 00044 * * 00045 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * 00046 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * 00047 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * 00048 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * 00049 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * 00050 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * 00051 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * 00052 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 00053 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * 00054 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * 00055 * * 00056 * OpenScop Library, a library to manipulate OpenScop formats and data * 00057 * structures. Written by: * 00058 * Cedric Bastoul <Cedric.Bastoul@u-psud.fr> and * 00059 * Louis-Noel Pouchet <Louis-Noel.pouchet@inria.fr> * 00060 * * 00061 *****************************************************************************/ 00062 00063 00064 #include <stdlib.h> 00065 #include <stdio.h> 00066 #include <ctype.h> 00067 00068 #include <osl/macros.h> 00069 #include <osl/util.h> 00070 #include <osl/int.h> 00071 #include <osl/vector.h> 00072 00073 00074 /*+*************************************************************************** 00075 * Structure display function * 00076 *****************************************************************************/ 00077 00078 00089 void osl_vector_idump(FILE * file, osl_vector_p vector, int level) { 00090 int j; 00091 00092 if (vector != NULL) { 00093 // Go to the right level. 00094 for (j = 0; j < level; j++) 00095 fprintf(file,"|\t"); 00096 fprintf(file,"+-- osl_vector_t ("); 00097 osl_int_dump_precision(file, vector->precision); 00098 fprintf(file, ")\n"); 00099 00100 for (j = 0; j <= level; j++) 00101 fprintf(file,"|\t"); 00102 fprintf(file,"%d\n", vector->size); 00103 00104 // Display the vector. 00105 for (j = 0; j <= level; j++) 00106 fprintf(file, "|\t"); 00107 00108 fprintf(file, "[ "); 00109 00110 for (j = 0; j < vector->size; j++) { 00111 osl_int_print(file, vector->precision, vector->v[j]); 00112 fprintf(file, " "); 00113 } 00114 00115 fprintf(file, "]\n"); 00116 } 00117 else { 00118 // Go to the right level. 00119 for (j = 0; j < level; j++) 00120 fprintf(file, "|\t"); 00121 fprintf(file, "+-- NULL vector\n"); 00122 } 00123 00124 // The last line. 00125 for (j = 0; j <= level; j++) 00126 fprintf(file, "|\t"); 00127 fprintf(file, "\n"); 00128 } 00129 00130 00138 void osl_vector_dump(FILE * file, osl_vector_p vector) { 00139 osl_vector_idump(file, vector, 0); 00140 } 00141 00142 00143 /*+*************************************************************************** 00144 * Memory allocation/deallocation function * 00145 *****************************************************************************/ 00146 00147 00157 osl_vector_p osl_vector_pmalloc(int precision, int size) { 00158 osl_vector_p vector; 00159 int i; 00160 00161 OSL_malloc(vector, osl_vector_p, sizeof(osl_vector_t)); 00162 vector->size = size; 00163 vector->precision = precision; 00164 if (size == 0) { 00165 vector->v = NULL; 00166 } 00167 else { 00168 OSL_malloc(vector->v, osl_int_t*, size * sizeof(osl_int_t)); 00169 for (i = 0; i < size; i++) 00170 osl_int_init_set_si(precision, &vector->v[i], 0); 00171 } 00172 return vector; 00173 } 00174 00175 00186 osl_vector_p osl_vector_malloc(int size) { 00187 int precision = osl_util_get_precision(); 00188 return osl_vector_pmalloc(precision, size); 00189 } 00190 00191 00197 void osl_vector_free(osl_vector_p vector) { 00198 int i; 00199 00200 if (vector != NULL) { 00201 if (vector->v != NULL) { 00202 for (i = 0; i < vector->size; i++) 00203 osl_int_clear(vector->precision, &vector->v[i]); 00204 00205 free(vector->v); 00206 } 00207 free(vector); 00208 } 00209 } 00210 00211 00212 /*+*************************************************************************** 00213 * Processing functions * 00214 *****************************************************************************/ 00215 00216 00226 osl_vector_p osl_vector_add_scalar(osl_vector_p vector, int scalar) { 00227 int i, precision, last; 00228 osl_vector_p result; 00229 00230 if ((vector == NULL) || (vector->size < 2)) 00231 OSL_error("incompatible vector for addition"); 00232 00233 precision = vector->precision; 00234 last = vector->size - 1; 00235 00236 result = osl_vector_pmalloc(precision, vector->size); 00237 for (i = 0; i < vector->size; i++) 00238 osl_int_assign(precision, &result->v[i], vector->v[i]); 00239 osl_int_add_si(precision, &result->v[last], vector->v[last], scalar); 00240 00241 return result; 00242 } 00243 00244 00254 osl_vector_p osl_vector_add(osl_vector_p v1, osl_vector_p v2) { 00255 int i; 00256 osl_vector_p v3; 00257 00258 if ((v1 == NULL) || (v2 == NULL) || 00259 (v1->size != v2->size) || (v1->precision != v2->precision)) 00260 OSL_error("incompatible vectors for addition"); 00261 00262 v3 = osl_vector_pmalloc(v1->precision, v1->size); 00263 for (i = 0; i < v1->size; i++) 00264 osl_int_add(v1->precision, &v3->v[i], v1->v[i], v2->v[i]); 00265 00266 return v3; 00267 } 00268 00269 00279 osl_vector_p osl_vector_sub(osl_vector_p v1, osl_vector_p v2) { 00280 int i; 00281 osl_vector_p v3; 00282 00283 if ((v1 == NULL) || (v2 == NULL) || 00284 (v1->size != v2->size) || (v1->precision != v2->precision)) 00285 OSL_error("incompatible vectors for subtraction"); 00286 00287 v3 = osl_vector_pmalloc(v1->precision, v1->size); 00288 for (i = 0; i < v1->size; i++) 00289 osl_int_sub(v1->precision, &v3->v[i], v1->v[i], v2->v[i]); 00290 00291 return v3; 00292 } 00293 00294 00303 void osl_vector_tag_inequality(osl_vector_p vector) { 00304 if ((vector == NULL) || (vector->size < 1)) 00305 OSL_error("vector cannot be tagged"); 00306 osl_int_set_si(vector->precision, &vector->v[0], 1); 00307 } 00308 00309 00318 void osl_vector_tag_equality(osl_vector_p vector) { 00319 if ((vector == NULL) || (vector->size < 1)) 00320 OSL_error("vector cannot be tagged"); 00321 osl_int_set_si(vector->precision, &vector->v[0], 0); 00322 } 00323 00324 00333 int osl_vector_equal(osl_vector_p v1, osl_vector_p v2) { 00334 int i; 00335 00336 if (v1 == v2) 00337 return 1; 00338 00339 if ((v1->size != v2->size) || (v1->precision != v2->precision)) 00340 return 0; 00341 00342 for (i = 0; i < v1->size; i++) 00343 if (osl_int_ne(v1->precision, v1->v[i], v2->v[i])) 00344 return 0; 00345 00346 return 1; 00347 } 00348 00349 00358 osl_vector_p osl_vector_mul_scalar(osl_vector_p v, int scalar) { 00359 int i; 00360 osl_vector_p result = osl_vector_pmalloc(v->precision, v->size); 00361 00362 for (i = 0; i < v->size; i++) 00363 osl_int_mul_si(v->precision, &result->v[i], v->v[i], scalar); 00364 00365 return result; 00366 } 00367 00368 00376 int osl_vector_is_scalar(osl_vector_p vector) { 00377 int i; 00378 00379 if (vector == NULL) 00380 return 0; 00381 00382 for (i = 0; i < vector->size - 1; i++) 00383 if (!osl_int_zero(vector->precision, vector->v[i])) 00384 return 0; 00385 return 1; 00386 } 00387