[772] | 1 | /* -------------------- */ |
---|
| 2 | /* --- ecc_common.c --- */ |
---|
| 3 | /* -------------------- */ |
---|
| 4 | |
---|
| 5 | #include <stdio.h> |
---|
| 6 | #include <stdlib.h> |
---|
[777] | 7 | #include <string.h> |
---|
[772] | 8 | #include <math.h> |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | #ifdef CLI |
---|
| 12 | #include "nrc_os_config.h" |
---|
| 13 | #include "nrc.h" |
---|
| 14 | #endif |
---|
| 15 | |
---|
| 16 | #include "ecc_common.h" |
---|
| 17 | #include "mt19937.h" |
---|
| 18 | |
---|
| 19 | #ifdef ENABLE_GLOBAL_DEBUG |
---|
| 20 | // variables globales externes pour log de configurations particulieres |
---|
| 21 | int g_density; |
---|
| 22 | int g_granularity; |
---|
| 23 | int g_iter; |
---|
| 24 | int g_i; |
---|
| 25 | int g_j; |
---|
| 26 | |
---|
| 27 | int g_p; |
---|
| 28 | uint32 g_ne; |
---|
| 29 | uint32 g_ne_start; |
---|
| 30 | uint32 g_ne_end; |
---|
| 31 | #endif |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | // -------------------------------- |
---|
| 35 | uint32 ui32Max2(uint32 a, uint32 b) |
---|
| 36 | // -------------------------------- |
---|
| 37 | { |
---|
| 38 | return (a > b ? a : b); |
---|
| 39 | } |
---|
| 40 | // ------------------------------------------ |
---|
| 41 | uint32 ui32Max3(uint32 a, uint32 b, uint32 c) |
---|
| 42 | // ------------------------------------------ |
---|
| 43 | { |
---|
| 44 | return ui32Max2(ui32Max2(a, b), c); |
---|
| 45 | } |
---|
| 46 | // ---------------------------------------------------- |
---|
| 47 | uint32 ui32Max4(uint32 a, uint32 b, uint32 c, uint32 d) |
---|
| 48 | // ---------------------------------------------------- |
---|
| 49 | { |
---|
| 50 | return ui32Max2(ui32Max2(a, b), ui32Max2(c,d)); |
---|
| 51 | } |
---|
| 52 | // -------------------------------- |
---|
| 53 | uint32 ui32Min2(uint32 a, uint32 b) |
---|
| 54 | // -------------------------------- |
---|
| 55 | { |
---|
| 56 | return (a < b ? a : b); |
---|
| 57 | } |
---|
| 58 | // ------------------------------------------ |
---|
| 59 | uint32 ui32Min3(uint32 a, uint32 b, uint32 c) |
---|
| 60 | // ------------------------------------------ |
---|
| 61 | { |
---|
| 62 | return ui32Min2(ui32Min2(a, b), c); |
---|
| 63 | } |
---|
| 64 | // ---------------------------------------------------- |
---|
| 65 | uint32 ui32Min4(uint32 a, uint32 b, uint32 c, uint32 d) |
---|
| 66 | // ---------------------------------------------------- |
---|
| 67 | { |
---|
| 68 | return ui32Min2(ui32Min2(a, b), ui32Min2(c,d)); |
---|
| 69 | } |
---|
| 70 | /* ----------------------------------- */ |
---|
| 71 | uint32 ui32MinNonNul2(uint32 a, uint32 b) |
---|
| 72 | /* ----------------------------------- */ |
---|
| 73 | { |
---|
| 74 | if (a && b) |
---|
| 75 | return (a < b ? a : b); |
---|
| 76 | if (a == 0) |
---|
| 77 | return b; |
---|
| 78 | return a; |
---|
| 79 | } |
---|
| 80 | /* --------------------------------------------- */ |
---|
| 81 | uint32 ui32MinNonNul3(uint32 a, uint32 b, uint32 c) |
---|
| 82 | /* --------------------------------------------- */ |
---|
| 83 | { |
---|
| 84 | uint32 m = 0xffffffff; |
---|
| 85 | |
---|
| 86 | if (a) m = a; |
---|
| 87 | if (b && (b<m)) m = b; |
---|
| 88 | if (c && (c<m)) m = c; |
---|
| 89 | |
---|
| 90 | return m; |
---|
| 91 | } |
---|
| 92 | /* ---------------------------------------------------- */ |
---|
| 93 | uint32 ui32MinNonNul3Cond(uint32 a0, uint32 a1, uint32 a2) |
---|
| 94 | /* ---------------------------------------------------- */ |
---|
| 95 | { |
---|
| 96 | uint32 m = 0; |
---|
| 97 | |
---|
| 98 | if (a1) m = ui32MinNonNul3(a0, a1, a2); |
---|
| 99 | return m; |
---|
| 100 | } |
---|
| 101 | /* ----------------------------------------------------------- */ |
---|
| 102 | uint32 ui32MinNonNul4(uint32 a0, uint32 a1, uint32 a2, uint32 a3) |
---|
| 103 | /* ----------------------------------------------------------- */ |
---|
| 104 | { |
---|
| 105 | uint32 m = 0xffffffff; |
---|
| 106 | |
---|
| 107 | if (a0) m = a0; |
---|
| 108 | if (a1 && (a1<m)) m = a1; |
---|
| 109 | if (a2 && (a2<m)) m = a2; |
---|
| 110 | if (a3 && (a3<m)) m = a3; |
---|
| 111 | |
---|
| 112 | return m; |
---|
| 113 | } |
---|
| 114 | /* ---------------------------------------------------------------------- */ |
---|
| 115 | uint32 ui32MinNonNul5(uint32 a0, uint32 a1, uint32 a2, uint32 a3, uint32 a4) |
---|
| 116 | /* ---------------------------------------------------------------------- */ |
---|
| 117 | { |
---|
| 118 | uint32 m = 0xffffffff; |
---|
| 119 | |
---|
| 120 | if (a0) m = a0; |
---|
| 121 | if (a1 && (a1<m)) m = a1; |
---|
| 122 | if (a2 && (a2<m)) m = a2; |
---|
| 123 | if (a3 && (a3<m)) m = a3; |
---|
| 124 | if (a4 && (a4<m)) m = a4; |
---|
| 125 | |
---|
| 126 | return m; |
---|
| 127 | } |
---|
| 128 | /* -------------------------------------------------------------------------- */ |
---|
| 129 | uint32 ui32MinNonNul5Cond(uint32 a0, uint32 a1, uint32 a2, uint32 a3, uint32 a4) |
---|
| 130 | /* -------------------------------------------------------------------------- */ |
---|
| 131 | { |
---|
| 132 | uint32 m = 0; |
---|
| 133 | |
---|
| 134 | if (a2) m = ui32MinNonNul5(a0, a1, a2, a3, a4); |
---|
| 135 | return m; |
---|
| 136 | } |
---|
| 137 | /* ------------------------------------------------------------------------------------------------------- */ |
---|
| 138 | uint32 ui32MinNonNul8(uint32 a0, uint32 a1, uint32 a2, uint32 a3, uint32 a4, uint32 a5, uint32 a6, uint32 a7) |
---|
| 139 | /* ------------------------------------------------------------------------------------------------------- */ |
---|
| 140 | { |
---|
| 141 | uint32 m = 0xffffffff; |
---|
| 142 | |
---|
| 143 | if (a0) m = a0; |
---|
| 144 | if (a1 && (a1<m)) m = a1; |
---|
| 145 | if (a2 && (a2<m)) m = a2; |
---|
| 146 | if (a3 && (a3<m)) m = a3; |
---|
| 147 | if (a4 && (a4<m)) m = a4; |
---|
| 148 | if (a5 && (a5<m)) m = a5; |
---|
| 149 | if (a6 && (a6<m)) m = a6; |
---|
| 150 | if (a7 && (a7<m)) m = a7; |
---|
| 151 | |
---|
| 152 | return m; |
---|
| 153 | } |
---|
| 154 | /* ------------------------------------------------------------------------------------------------------------------ */ |
---|
| 155 | uint32 ui32MinNonNul9(uint32 a0, uint32 a1, uint32 a2, uint32 a3, uint32 a4, uint32 a5, uint32 a6, uint32 a7, uint32 a8) |
---|
| 156 | /* ------------------------------------------------------------------------------------------------------------------ */ |
---|
| 157 | { |
---|
| 158 | uint32 m = 0xffffffff; |
---|
| 159 | |
---|
| 160 | if (a0) m = a0; |
---|
| 161 | if (a1 && (a1<m)) m = a1; |
---|
| 162 | if (a2 && (a2<m)) m = a2; |
---|
| 163 | if (a3 && (a3<m)) m = a3; |
---|
| 164 | if (a4 && (a4<m)) m = a4; |
---|
| 165 | if (a5 && (a5<m)) m = a5; |
---|
| 166 | if (a6 && (a6<m)) m = a6; |
---|
| 167 | if (a7 && (a7<m)) m = a7; |
---|
| 168 | if (a8 && (a8<m)) m = a8; |
---|
| 169 | |
---|
| 170 | return m; |
---|
| 171 | } |
---|
| 172 | /* ---------------------------------------------------------------------------------------------------------------------- */ |
---|
| 173 | uint32 ui32MinNonNul9Cond(uint32 a0, uint32 a1, uint32 a2, uint32 a3, uint32 a4, uint32 a5, uint32 a6, uint32 a7, uint32 a8) |
---|
| 174 | /* ---------------------------------------------------------------------------------------------------------------------- */ |
---|
| 175 | { |
---|
| 176 | // a0 b0 c0 |
---|
| 177 | // a1 b1 c1 |
---|
| 178 | // a2 b2 c2 |
---|
| 179 | |
---|
| 180 | uint32 m = 0; |
---|
| 181 | |
---|
| 182 | if (a4) m = ui32MinNonNul9(a0, a1, a2, a3, a4, a5, a6, a7, a8); |
---|
| 183 | |
---|
| 184 | return m; |
---|
| 185 | } |
---|
| 186 | // -------------------------------- |
---|
| 187 | void initT(uint32 *T, uint32 nemax) |
---|
| 188 | // -------------------------------- |
---|
| 189 | { |
---|
| 190 | int i; |
---|
| 191 | for (i = 0; i <= (int) nemax; i++) { |
---|
| 192 | T[i] = i; |
---|
| 193 | } |
---|
| 194 | } |
---|
| 195 | /* -------------------------- */ |
---|
| 196 | //void initA(uint32 *A, uint32 nemax) |
---|
| 197 | /* ------------------------------- */ |
---|
| 198 | /*{ |
---|
| 199 | int i; |
---|
| 200 | for(i=0; i<=nemax; i++) { |
---|
| 201 | A[i] = 0; |
---|
| 202 | } |
---|
| 203 | }*/ |
---|
| 204 | // -------------------------------- |
---|
| 205 | void initZ(uint32 *T, uint32 nemax) |
---|
| 206 | // -------------------------------- |
---|
| 207 | { |
---|
| 208 | int i; |
---|
| 209 | for (i = 0; i <= (int) nemax; i++) { |
---|
| 210 | T[i] = 0; |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | // -------------------------------------- |
---|
| 214 | void check_initT(uint32 *T, uint32 nemax) |
---|
| 215 | // -------------------------------------- |
---|
| 216 | { |
---|
| 217 | check_initT_range(T, 0, nemax, nemax); |
---|
| 218 | } |
---|
| 219 | // -------------------------------------- |
---|
| 220 | void check_initZ(uint32 *T, uint32 nemax) |
---|
| 221 | // -------------------------------------- |
---|
| 222 | { |
---|
| 223 | check_initZ_range(T, 0, nemax, nemax); |
---|
| 224 | } |
---|
| 225 | // --------------------------------------------------------------- |
---|
| 226 | void check_initT_range(uint32 *T, uint32 i0, uint32 ii, uint32 i1) |
---|
| 227 | // --------------------------------------------------------------- |
---|
| 228 | { |
---|
| 229 | uint32 i; |
---|
| 230 | |
---|
| 231 | printf("check_initT_range[%d %d %d]\n", i0, ii, i1); |
---|
| 232 | |
---|
| 233 | for (i = i0; i <= i1; i++) { |
---|
| 234 | if (T[i] != i) { |
---|
| 235 | printf("[check_initT]: T[%d] = %d\n", i, T[i]); |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | // --------------------------------------------------------------- |
---|
| 240 | void check_initR_range(uint32 *R, uint32 i0, uint32 ii, uint32 i1) |
---|
| 241 | // --------------------------------------------------------------- |
---|
| 242 | { |
---|
| 243 | uint32 i; |
---|
| 244 | |
---|
| 245 | printf("check_initR_range[%d %d %d]\n", i0, ii, i1); |
---|
| 246 | |
---|
| 247 | for (i = i0; i <= i1; i++) { |
---|
| 248 | if (R[i] != i) { |
---|
| 249 | printf("[check_initR]: R[%d] = %d\n", i, R[i]); |
---|
| 250 | } |
---|
| 251 | } |
---|
| 252 | } |
---|
| 253 | // --------------------------------------------------------------- |
---|
| 254 | void check_initZ_range(uint32 *T, uint32 i0, uint32 ii, uint32 i1) |
---|
| 255 | // --------------------------------------------------------------- |
---|
| 256 | { |
---|
| 257 | uint32 i; |
---|
| 258 | |
---|
| 259 | printf("check_initZ_range[%d %d %d]\n", i0, ii, i1); |
---|
| 260 | |
---|
| 261 | for (i = i0; i <= i1; i++) { |
---|
| 262 | if (T[i] != 0) { |
---|
| 263 | printf("[check_initZ]: T[%d] = %d\n", i, T[i]); |
---|
| 264 | } |
---|
| 265 | } |
---|
| 266 | } |
---|
| 267 | /* --------------------------------------------------------------------------------------------------- */ |
---|
| 268 | void binarisation_ui8matrix(uint8 **X, int i0, int i1, int j0, int j1, uint8 seuil, uint8 val, uint8 **Y) |
---|
| 269 | /* --------------------------------------------------------------------------------------------------- */ |
---|
| 270 | { |
---|
| 271 | int i, j; |
---|
| 272 | uint8 x, y; |
---|
| 273 | |
---|
| 274 | for (i = i0; i <= i1; i++) { |
---|
| 275 | for (j = j0; j <= j1; j++) { |
---|
| 276 | x = X[i][j]; |
---|
| 277 | if (x >= seuil) |
---|
| 278 | y = val; |
---|
| 279 | else |
---|
| 280 | y = 0; |
---|
| 281 | Y[i][j] = y; |
---|
| 282 | } |
---|
| 283 | } |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | /* ------------------------------------------------------------------- */ |
---|
| 287 | void applyTable(uint32 **X, int height, int width, uint32 *T, uint32 **Y) |
---|
| 288 | /* ------------------------------------------------------------------- */ |
---|
| 289 | { |
---|
| 290 | int i, j; |
---|
| 291 | |
---|
| 292 | uint32 e, a; |
---|
| 293 | |
---|
| 294 | |
---|
| 295 | for (i = 0; i < height; i++) { |
---|
| 296 | for (j = 0; j < width; j++) { |
---|
| 297 | |
---|
| 298 | e = X[i][j]; |
---|
| 299 | a = T[e]; |
---|
| 300 | |
---|
| 301 | Y[i][j] = a; |
---|
| 302 | } |
---|
| 303 | } |
---|
| 304 | } |
---|
| 305 | /* ------------------------------------------------------------------------------- */ |
---|
| 306 | void graphviz_write_ui8vector(uint8 *v, int i0, int i1, char *format, char *filename) |
---|
| 307 | /* ------------------------------------------------------------------------------- */ |
---|
| 308 | { |
---|
| 309 | int i; |
---|
| 310 | int fd; |
---|
| 311 | char complete_filename[64]; |
---|
| 312 | |
---|
[777] | 313 | snprintf(complete_filename, 64, "%s.dot", filename); |
---|
[772] | 314 | |
---|
| 315 | fd = open(complete_filename, O_CREAT | O_TRUNC); |
---|
| 316 | //if(f == NULL) { nrerror("Can't open file in grawrite_bvector"); } |
---|
| 317 | |
---|
| 318 | fprintf(fd, "digraph %s {\n", filename); |
---|
| 319 | for (i = i0; i <= i1; i++) { |
---|
| 320 | fprintf(fd, "%3d -> %3d;\n", i, v[i]); |
---|
| 321 | } |
---|
| 322 | fprintf(fd, "}\n"); |
---|
| 323 | close(fd); |
---|
| 324 | } |
---|
| 325 | /* --------------------------------------------------------------------------------- */ |
---|
| 326 | void graphviz_write_ui16vector(uint16 *v, int i0, int i1, char *format, char *filename) |
---|
| 327 | /* --------------------------------------------------------------------------------- */ |
---|
| 328 | { |
---|
| 329 | int i; |
---|
| 330 | int fd; |
---|
| 331 | char complete_filename[64]; |
---|
| 332 | |
---|
[777] | 333 | snprintf(complete_filename, 64, "%s.dot", filename); |
---|
[772] | 334 | |
---|
| 335 | |
---|
| 336 | fd = open(complete_filename, O_CREAT | O_TRUNC); |
---|
| 337 | //if(f == NULL) { nrerror("Can't open file in grawrite_bvector"); } |
---|
| 338 | |
---|
| 339 | fprintf(fd, "digraph %s {\n", filename); |
---|
| 340 | for (i = i0; i <= i1; i++) { |
---|
| 341 | fprintf(fd, "%3d -> %3d;\n", i, v[i]); |
---|
| 342 | } |
---|
| 343 | fprintf(fd, "}\n"); |
---|
| 344 | close(fd); |
---|
| 345 | } |
---|
| 346 | /* --------------------------------------------------------------------------------- */ |
---|
| 347 | void graphviz_write_ui32vector(uint32 *v, int i0, int i1, char *format, char *filename) |
---|
| 348 | /* --------------------------------------------------------------------------------- */ |
---|
| 349 | { |
---|
| 350 | int i; |
---|
| 351 | int fd; |
---|
| 352 | char complete_filename[64]; |
---|
| 353 | |
---|
[777] | 354 | snprintf(complete_filename, 64, "%s.dot", filename); |
---|
[772] | 355 | |
---|
| 356 | |
---|
| 357 | fd = open(complete_filename, O_CREAT | O_TRUNC); |
---|
| 358 | //if(f == NULL) { nrerror("Can't open file in grawrite_bvector"); } |
---|
| 359 | |
---|
| 360 | fprintf(fd, "digraph %s {\n", filename); |
---|
| 361 | for (i = i0; i <= i1; i++) { |
---|
| 362 | fprintf(fd, "%3d -> %3d;\n", i, v[i]); |
---|
| 363 | } |
---|
| 364 | fprintf(fd, "}\n"); |
---|
| 365 | close(fd); |
---|
| 366 | } |
---|
| 367 | /* ------------------------------------------------------------------------------ */ |
---|
| 368 | void mod_ui32matrix_ui8matrix(uint32 **X, int i0, int i1, int j0, int j1, uint8 **Y) |
---|
| 369 | /* ------------------------------------------------------------------------------ */ |
---|
| 370 | { |
---|
| 371 | int i, j; |
---|
| 372 | for (i = i0; i <= i1; i++) { |
---|
| 373 | for (j = j0; j <= j1; j++) { |
---|
| 374 | Y[i][j] = X[i][j] & 255; |
---|
| 375 | } |
---|
| 376 | } |
---|
| 377 | } |
---|
| 378 | // ------------------------------------------------------------------------------------------ |
---|
| 379 | void positive_mod_ui32matrix_ui8matrix(uint32 **X, int i0, int i1, int j0, int j1, uint8 **Y) |
---|
| 380 | // ------------------------------------------------------------------------------------------ |
---|
| 381 | { |
---|
| 382 | int i, j; |
---|
| 383 | for (i = i0; i <= i1; i++) { |
---|
| 384 | for (j = j0; j <= j1; j++) { |
---|
| 385 | if (X[i][j]) { |
---|
| 386 | if (X[i][j] < 255) { |
---|
| 387 | Y[i][j] = X[i][j]; // pour que e=1 reste rouge |
---|
| 388 | } else { |
---|
| 389 | Y[i][j] = (X[i][j] % 254) + 1; |
---|
| 390 | } |
---|
| 391 | } else { |
---|
| 392 | Y[i][j] = 0; |
---|
| 393 | } |
---|
| 394 | |
---|
| 395 | } |
---|
| 396 | } |
---|
| 397 | } |
---|
| 398 | /* --------------------------------------------------------------------------------- */ |
---|
| 399 | void graphviz_write_ui32vector_par(uint32 *v, int i0, int i1, char *format, char *filename) |
---|
| 400 | /* --------------------------------------------------------------------------------- */ |
---|
| 401 | { |
---|
| 402 | int i; |
---|
| 403 | int fd; |
---|
| 404 | char complete_filename[64]; |
---|
| 405 | |
---|
[777] | 406 | snprintf(complete_filename, 64, "%s.dot", filename); |
---|
[772] | 407 | |
---|
| 408 | |
---|
| 409 | fd = open(complete_filename, O_CREAT | O_TRUNC); |
---|
| 410 | //if(f == NULL) { nrerror("Can't open file in grawrite_bvector"); } |
---|
| 411 | |
---|
| 412 | fprintf(fd, "digraph %s {\n", filename); |
---|
| 413 | for (i = i0; i <= i1; i++) { |
---|
| 414 | fprintf(fd, "%3d -> %3d;\n", i, v[i]); |
---|
| 415 | } |
---|
| 416 | fprintf(fd, "}\n"); |
---|
| 417 | close(fd); |
---|
| 418 | } |
---|
| 419 | // -------------------------------------- |
---|
| 420 | uint32 mt19937_uint32(uint32 a, uint32 b) |
---|
| 421 | // -------------------------------------- |
---|
| 422 | // nombre aleatoire 32 bit sur [a..b] |
---|
| 423 | { |
---|
| 424 | uint32 d; |
---|
| 425 | uint32 x32; |
---|
| 426 | |
---|
| 427 | if (b < a) return mt19937_uint32(b, a); |
---|
| 428 | |
---|
| 429 | //printf("a = %u b = %u\n", a, b); |
---|
| 430 | |
---|
| 431 | d = b - a + 1; |
---|
| 432 | x32 = genrand_int32(); |
---|
| 433 | //printf("x32 = %u, d = %u\n", x32, d); |
---|
| 434 | x32 = (x32 % d) + a; |
---|
| 435 | return x32; |
---|
| 436 | } |
---|
| 437 | // ----------------------- |
---|
| 438 | BOOL strto_Bool(char *str) |
---|
| 439 | // ----------------------- |
---|
| 440 | { |
---|
| 441 | BOOL b = TRUE; |
---|
| 442 | if (strcmp(str, "TRUE" ) == 0) b = TRUE; |
---|
| 443 | if (strcmp(str, "true" ) == 0) b = TRUE; |
---|
| 444 | |
---|
| 445 | if (strcmp(str, "FALSE") == 0) b = FALSE; |
---|
| 446 | if (strcmp(str, "false") == 0) b = FALSE; |
---|
| 447 | |
---|
| 448 | return b; |
---|
| 449 | } |
---|
| 450 | // ------------------------------------------------------------ |
---|
| 451 | void check_no_write(uint32 **T, int i0, int i1, int j0, int j1) |
---|
| 452 | // ------------------------------------------------------------ |
---|
| 453 | { |
---|
| 454 | int i, j; |
---|
| 455 | for (i = i0; i <= i1; i++) { |
---|
| 456 | for (j = j0; j <= j1; j++) { |
---|
| 457 | if (T[i][j] == 0xDEADBEFF) { |
---|
| 458 | printf("[check_no_write]: T[%d][%d] = %d\n", i, j, T[i][j]); |
---|
| 459 | } |
---|
| 460 | } |
---|
| 461 | } |
---|
| 462 | } |
---|