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