OSDN Git Service

Updated MSWS RNG to latest version + some more fixes.
[mhash384/mhash384.git] / tools / GenTables / src / gen_table_mix.c
1 /* ----------------------------------------------------------------------------------------------- */
2 /* MHash-384 - Generate tables utility program                                                     */
3 /* Copyright(c) 2016-2017 LoRd_MuldeR <mulder2@gmx.de>                                             */
4 /*                                                                                                 */
5 /* Permission is hereby granted, free of charge, to any person obtaining a copy of this software   */
6 /* and associated documentation files(the "Software"), to deal in the Software without             */
7 /* restriction, including without limitation the rights to use, copy, modify, merge, publish,      */
8 /* distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the */
9 /* Software is furnished to do so, subject to the following conditions:                            */
10 /*                                                                                                 */
11 /* The above copyright notice and this permission notice shall be included in all copies or        */
12 /* substantial portions of the Software.                                                           */
13 /*                                                                                                 */
14 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING   */
15 /* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND      */
16 /* NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,     */
17 /* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  */
18 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.         */
19 /* ----------------------------------------------------------------------------------------------- */
20
21 #include "common.h"
22 #include "thread_utils.h"
23 #include "msws.h"
24 #include "boxmuller.h"
25
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <string.h>
29 #include <time.h>
30 #include <stdbool.h>
31 #include <io.h>
32
33 //-----------------------------------------------------------------------------
34 // Const
35 //-----------------------------------------------------------------------------
36
37 #define HASH_LEN 384U
38
39 #define ROW_NUM 251U                    /*total number of rows*/
40 #define ROW_LEN (HASH_LEN / CHAR_BIT)   /*number of indices per row*/
41 #define DISTANCE_MIN (ROW_LEN - 1U)     /*min. hamming distance*/
42
43 #undef ENABLE_TRACE
44
45 #define __DISTANCE_STR(X) #X
46 #define _DISTANCE_STR(X) __DISTANCE_STR(X)
47 #define DISTANCE_STR _DISTANCE_STR(DISTANCE_MIN)
48
49 #define MAGIC_NUMBER 0x76A3D06509A73016ui64
50
51 //-----------------------------------------------------------------------------
52 // Globals
53 //-----------------------------------------------------------------------------
54
55 static uint8_t g_table[ROW_NUM][HASH_LEN];
56
57 static size_t g_spinpos = 0;
58 static char SPINNER[4] = { '/', '-', '\\', '|' };
59
60 static const uint8_t INDICES[UINT8_MAX + 1U] =
61 {
62         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
63         0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
64         0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
65         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
66         0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
67         0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
68         0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
69         0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
70         0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
71         0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
72         0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
73         0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
74         0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
75         0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
76         0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
77         0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
78 };
79
80 //-----------------------------------------------------------------------------
81 // Utility Functions
82 //-----------------------------------------------------------------------------
83
84 #ifdef ENABLE_TRACE
85 #define TRACE(X, ...) printf(X "\n", __VA_ARGS__)
86 #else
87 #define TRACE(X, ...) __noop()
88 #endif
89
90 static inline void swap(uint8_t *const row_buffer, const uint_fast16_t a, const uint_fast16_t b)
91 {
92         const uint8_t temp = row_buffer[a];
93         row_buffer[a] = row_buffer[b];
94         row_buffer[b] = temp;
95 }
96
97 static inline void random_permutation(msws_t rand, uint8_t *const row_buffer)
98 {
99         for (uint_fast16_t i = 0; i < ROW_LEN; ++i)
100         {
101                 const uint_fast16_t j = msws_uint32_max(rand, i + 1U);
102                 row_buffer[i] = row_buffer[j];
103                 row_buffer[j] = INDICES[i];
104         }
105 }
106
107 static inline void swap_multiple_random(msws_t rand, uint8_t *const row_buffer, const uint_fast16_t count)
108 {
109         for (uint_fast16_t i = 0U; i < count; ++i)
110         {
111                 uint_fast16_t a, b;
112                 do
113                 {
114                         a = msws_uint32_max(rand, ROW_LEN);
115                         b = msws_uint32_max(rand, ROW_LEN);
116                 } 
117                 while(a == b);
118                 swap(row_buffer, a, b);
119         }
120 }
121
122 static inline void rotate_row(uint8_t *const row_buffer)
123 {
124         const uint8_t temp = row_buffer[0];
125         for (uint_fast16_t k = 0U; k < ROW_LEN - 1U; ++k)
126         {
127                 row_buffer[k] = row_buffer[k + 1U];
128         }
129         row_buffer[ROW_LEN - 1U] = temp;
130 }
131
132 static inline void reverse_row(uint8_t *const row_buffer)
133 {
134         uint_fast16_t j = ROW_LEN - 1U;
135         for (uint_fast16_t i = 0U; i < ROW_LEN / 2U; ++i)
136         {
137                 swap(row_buffer, i, j--);
138         }
139 }
140
141 static inline uint_fast16_t row_distance(const uint8_t *const row_a, const uint8_t *const row_b)
142 {
143         uint_fast16_t distance = 0U;
144         for (uint_fast16_t j = 0; j < ROW_LEN; ++j)
145         {
146                 if (row_a[j] != row_b[j])
147                 {
148                         distance++;
149                 }
150         }
151         return distance;
152 }
153
154 #define ERROR_ACC(X,Y) ((X >= Y) ? ((X << 8U) | Y) : ((Y << 8U) | X))
155 static inline uint_fast16_t check_permutation(const uint_fast16_t index, const uint8_t *const row_buffer, const uint_fast16_t limit)
156 {
157         uint_fast16_t error = 0U, failed = 0U;
158         uint_fast16_t distance = row_distance(&INDICES[0], &row_buffer[0]);
159         if (distance < DISTANCE_MIN)
160         {
161                 error = DISTANCE_MIN - distance;
162                 failed = 1U;
163                 if (ERROR_ACC(failed, error) >= limit)
164                 {
165                         return limit; /*early termination*/
166                 }
167         }
168         for (uint_fast16_t i = 0; i < index; ++i)
169         {
170                 distance = row_distance(&g_table[i][0], &row_buffer[0]);
171                 if (distance < DISTANCE_MIN)
172                 {
173                         error = max_ui16(error, DISTANCE_MIN - distance);
174                         failed++;
175                         if (ERROR_ACC(failed, error) >= limit)
176                         {
177                                 return limit; /*early termination*/
178                         }
179                 }
180         }
181         return ERROR_ACC(failed, error);
182 }
183
184 static void print_row(const uint8_t *const row_buffer)
185 {
186         for (uint_fast8_t i = 0; i < ROW_LEN; ++i)
187         {
188                 printf(i ? ",%02X" : "%02X", row_buffer[i]);
189         }
190         puts("");
191 }
192
193 static void dump_table(FILE *out)
194 {
195         fprintf(out, "uint8_t MHASH_384_TABLE_MIX[%u][MHASH_384_LEN] =\n{\n", ROW_NUM);
196         for (uint_fast16_t i = 0; i < ROW_NUM; i++)
197         {
198                 fputs("\t{ ", out);
199                 for (uint_fast8_t j = 0; j < ROW_LEN; j++)
200                 {
201                         if (j > 0)
202                         {
203                                 fputs(", ", out);
204                         }
205                         fprintf(out, "0x%02X", g_table[i][j]);
206                 }
207                 fprintf(out, " }%s /*%03u*/\n", (i != (ROW_NUM - 1)) ? "," : " ", (uint32_t)i);
208         }
209         fputs("};\n", out);
210 }
211
212 //-----------------------------------------------------------------------------
213 // Save / Load
214 //-----------------------------------------------------------------------------
215
216 static bool save_table_data(const wchar_t *const filename, const uint_fast16_t rows_completed_in)
217 {
218         FILE *const file = _wfopen(filename, L"wb");
219         if (file)
220         {
221                 bool success = true;
222                 const uint64_t magic_number = MAGIC_NUMBER;
223                 const uint32_t hash_len = HASH_LEN, distance_min = DISTANCE_MIN, rows_completed = (uint32_t)rows_completed_in;
224                 fwrite(&magic_number, sizeof(uint64_t), 1, file);
225                 fwrite(&hash_len, sizeof(uint32_t), 1, file);
226                 fwrite(&distance_min, sizeof(uint32_t), 1, file);
227                 fwrite(&rows_completed, sizeof(uint32_t), 1, file);
228                 for (uint32_t i = 0; i < rows_completed; ++i)
229                 {
230                         const uint32_t checksum = adler32(&g_table[i][0], ROW_LEN);
231                         fwrite(&checksum, sizeof(uint32_t), 1, file);
232                         fwrite(&g_table[i][0], sizeof(uint8_t), ROW_LEN, file);
233                 }
234                 if (ferror(file))
235                 {
236                         printf("ERROR: Failed to write table data!\n");
237                         success = false;
238                 }
239                 fclose(file);
240                 return success;
241         }
242         else
243         {
244                 printf("ERROR: Failed to open table file for writing!\n");
245                 return false;
246         }
247 }
248
249 static bool load_table_data(const wchar_t *const filename, uint_fast16_t *const rows_completed_out)
250 {
251         FILE *const file = _wfopen(filename, L"rb");
252         if (file)
253         {
254                 bool success = true;
255                 uint64_t magic_number;
256                 uint32_t hash_len, distance_min, rows_completed;
257                 fread(&magic_number, sizeof(uint64_t), 1, file);
258                 fread(&hash_len, sizeof(uint32_t), 1, file);
259                 fread(&distance_min, sizeof(uint32_t), 1, file);
260                 fread(&rows_completed, sizeof(uint32_t), 1, file);
261                 if (ferror(file) || feof(file))
262                 {
263                         printf("ERROR: Failed to read the table header!\n");
264                         success = false;
265                         goto failed;
266                 }
267                 if (magic_number != MAGIC_NUMBER)
268                 {
269                         printf("ERROR: Table file format could not be recognized!\n");
270                         success = false;
271                         goto failed;
272                 }
273                 if ((hash_len != HASH_LEN) || (distance_min != DISTANCE_MIN) || (rows_completed > ROW_NUM))
274                 {
275                         printf("ERROR: Table properties are incompatibe with this instance!\n");
276                         success = false;
277                         goto failed;
278                 }
279                 for (uint_fast16_t i = 0; i < rows_completed; ++i)
280                 {
281                         uint32_t checksum_expected;
282                         if ((fread(&checksum_expected, sizeof(uint32_t), 1, file) != 1) || (fread(&g_table[i][0], sizeof(uint8_t), ROW_LEN, file) != ROW_LEN))
283                         {
284                                 printf("ERROR: Failed to read table data from file!\n");
285                                 success = false;
286                                 goto failed;
287                         }
288                         if (adler32(&g_table[i][0], ROW_LEN) != checksum_expected)
289                         {
290                                 printf("ERROR: Table checksum does *not* match table contents!\n");
291                                 success = false;
292                                 goto failed;
293                         }
294                         if (check_permutation(i, &g_table[i][0], UINT16_MAX) != 0U)
295                         {
296                                 printf("ERROR: Table distance verification has failed!\n");
297                                 success = false;
298                                 goto failed;
299                         }
300                 }
301         failed:
302                 fclose(file);
303                 if (success && rows_completed_out)
304                 {
305                         *rows_completed_out = (uint_fast16_t)rows_completed;
306                 }
307                 return success;
308         }
309         else
310         {
311                 printf("ERROR: Failed to open table file for reading!\n");
312                 return false;
313         }
314 }
315 //-----------------------------------------------------------------------------
316 // MAIN
317 //-----------------------------------------------------------------------------
318
319 int wmain(int argc, wchar_t *argv[])
320 {
321         FILE *file_out = NULL;
322         uint_fast16_t initial_row_index = 0;
323         char time_string[64];
324
325         printf("MHash GenTableMIX [%s]\n\n", __DATE__);
326         printf("HashLen: %d, Distance Min: %d\n\n", HASH_LEN, DISTANCE_MIN);
327
328         if ((HASH_LEN % (8 * sizeof(uint32_t))) != 0)
329         {
330                 crit_exit("FATAL: Hash length must be a multiple of 32 bits!");
331         }
332
333         for (uint_fast8_t i = 0; i < ROW_LEN; ++i)
334         {
335                 if (INDICES[i] != ((uint8_t)i))
336                 {
337                         crit_exit("FATAL: Invalid indices array detected!");
338                 }
339         }
340
341         if (argc < 2)
342         {
343                 printf("Table file not specified!\n\n");
344                 printf("Usage:\n");
345                 printf("   GenTables_MIX.exe <table_file>\n\n");
346                 return 1;
347         }
348
349         msws_t rand;
350         msws_init(rand, make_seed());
351
352         bxmller_t bxmller;
353         gaussian_noise_init(&bxmller);
354         
355         if (_waccess(argv[1], 4) == 0)
356         {
357                 printf("Loading existing table data and proceeding...\n");
358                 if (!load_table_data(argv[1], &initial_row_index))
359                 {
360                         return 1;
361                 }
362         }
363
364         for (uint_fast16_t i = initial_row_index; i < ROW_NUM; ++i)
365         {
366                 printf("Row %03u of %03u [%c]", (uint32_t)(i + 1U), ROW_NUM, SPINNER[g_spinpos]);
367                 uint8_t temp[ROW_LEN];
368                 uint_fast16_t counter = 0U;
369                 for (;;)
370                 {
371                         random_permutation(rand, &g_table[i][0]);
372                         uint_fast16_t error = check_permutation(i, &g_table[i][0], UINT16_MAX);
373                         if (error > 0U)
374                         {
375                                 static const int_fast16_t MAX_RETRY = 1999U;
376                                 for (int_fast16_t retry = 0; retry < MAX_RETRY; ++retry)
377                                 {
378                                         if (!((++counter) & 0x3))
379                                         {
380                                                 TRACE("Randomize round %u of %u", retry, MAX_RETRY);
381                                                 printf("\b\b\b[%c]", SPINNER[g_spinpos]);
382                                                 g_spinpos = (g_spinpos + 1) % 4;
383                                         }
384                                         for (uint_fast16_t rand_init = 0U; rand_init < 9973U; ++rand_init)
385                                         {
386                                                 random_permutation(rand, &temp[0]);
387                                                 const uint_fast16_t error_next = check_permutation(i, &temp[0], error);
388                                                 if (error_next < error)
389                                                 {
390                                                         TRACE("Improved by rand init! (%04X -> %04X)", error, error_next);
391                                                         retry = -1;
392                                                         memcpy(&g_table[i][0], &temp[0], sizeof(uint8_t) * ROW_LEN); /*keep*/
393                                                         if (!((error = error_next) > 0U))
394                                                         {
395                                                                 goto success;
396                                                         }
397                                                 }
398                                                 for (uint_fast8_t rotate = 0U; rotate < ROW_LEN; ++rotate)
399                                                 {
400                                                         rotate_row(&temp[0]);
401                                                         const uint_fast16_t error_next = check_permutation(i, &temp[0], error);
402                                                         if (error_next < error)
403                                                         {
404                                                                 TRACE("Improved by rand init + rotate! (%04X -> %04X)", error, error_next);
405                                                                 retry = -1;
406                                                                 memcpy(&g_table[i][0], &temp[0], sizeof(uint8_t) * ROW_LEN); /*keep*/
407                                                                 if (!((error = error_next) > 0U))
408                                                                 {
409                                                                         goto success;
410                                                                 }
411                                                         }
412                                                         reverse_row(&temp[0]);
413                                                         const uint_fast16_t error_next_reverse = check_permutation(i, &temp[0], error);
414                                                         if (error_next_reverse < error)
415                                                         {
416                                                                 TRACE("Improved by rand init + rotate + reverse! (%04X -> %04X)", error, error_next_reverse);
417                                                                 retry = -1;
418                                                                 memcpy(&g_table[i][0], &temp[0], sizeof(uint8_t) * ROW_LEN); /*keep*/
419                                                                 if (!((error = error_next_reverse) > 0U))
420                                                                 {
421                                                                         goto success;
422                                                                 }
423                                                         }
424                                                         else
425                                                         {
426                                                                 reverse_row(&temp[0]);
427                                                         }
428                                                 }
429                                         }
430                                 }
431                                 for (int_fast16_t retry = 0; retry < MAX_RETRY; ++retry)
432                                 {
433                                         TRACE("Optimizer round %u of %u", retry, MAX_RETRY);
434                                         if (!retry)
435                                         {
436                                                 msws_init(rand, make_seed());
437                                                 TRACE("First round!");
438                                                 for (uint_fast8_t swap_x1 = 0; swap_x1 < ROW_LEN; ++swap_x1)
439                                                 {
440                                                         printf("\b\b\b[%c]", SPINNER[g_spinpos]);
441                                                         g_spinpos = (g_spinpos + 1) % 4;
442                                                         for (uint_fast8_t swap_y1 = swap_x1 + 1U; swap_y1 < ROW_LEN; ++swap_y1)
443                                                         {
444                                                                 bool revert_1 = true;
445                                                                 swap(&g_table[i][0], swap_x1, swap_y1);
446                                                                 const uint_fast16_t error_next = check_permutation(i, &g_table[i][0], error);
447                                                                 if (error_next < error)
448                                                                 {
449                                                                         TRACE("Improved by swap-1! (%04X -> %04X)", error, error_next);
450                                                                         revert_1 = false;
451                                                                         retry = -1;
452                                                                         if (!((error = error_next) > 0U))
453                                                                         {
454                                                                                 goto success;
455                                                                         }
456                                                                 }
457                                                                 for (uint_fast8_t swap_x2 = 0; swap_x2 < ROW_LEN; ++swap_x2)
458                                                                 {
459                                                                         for (uint_fast8_t swap_y2 = swap_x2 + 1U; swap_y2 < ROW_LEN; ++swap_y2)
460                                                                         {
461                                                                                 bool revert_2 = true;
462                                                                                 swap(&g_table[i][0], swap_x2, swap_y2);
463                                                                                 const uint_fast16_t error_next = check_permutation(i, &g_table[i][0], error);
464                                                                                 if (error_next < error)
465                                                                                 {
466                                                                                         TRACE("Improved by swap-2! (%04X -> %04X)", error, error_next);
467                                                                                         revert_1 = revert_2 = false;
468                                                                                         retry = -1;
469                                                                                         if (!((error = error_next) > 0U))
470                                                                                         {
471                                                                                                 goto success;
472                                                                                         }
473                                                                                 }
474                                                                                 for (uint_fast8_t swap_x3 = 0; swap_x3 < ROW_LEN; ++swap_x3)
475                                                                                 {
476                                                                                         for (uint_fast8_t swap_y3 = swap_x3 + 1U; swap_y3 < ROW_LEN; ++swap_y3)
477                                                                                         {
478                                                                                                 swap(&g_table[i][0], swap_x3, swap_y3);
479                                                                                                 const uint_fast16_t error_next = check_permutation(i, &g_table[i][0], error);
480                                                                                                 if (error_next >= error)
481                                                                                                 {
482                                                                                                         swap(&g_table[i][0], swap_x3, swap_y3); /*revert*/
483                                                                                                 }
484                                                                                                 else
485                                                                                                 {
486                                                                                                         TRACE("Improved by swap-3! (%04X -> %04X)", error, error_next);
487                                                                                                         revert_1 = revert_2 = false;
488                                                                                                         retry = -1;
489                                                                                                         if (!((error = error_next) > 0U))
490                                                                                                         {
491                                                                                                                 goto success;
492                                                                                                         }
493                                                                                                 }
494                                                                                         }
495                                                                                 }
496                                                                                 if (revert_2)
497                                                                                 {
498                                                                                         swap(&g_table[i][0], swap_x2, swap_y2); /*revert*/
499                                                                                 }
500                                                                         }
501                                                                 }
502                                                                 if (revert_1)
503                                                                 {
504                                                                         swap(&g_table[i][0], swap_x1, swap_y1); /*revert*/
505                                                                 }
506                                                         }
507                                                 }
508                                         }
509                                         const double sigma = 3.14159 * (1.0 + (retry / (double)MAX_RETRY));
510                                         for (int_fast16_t loop = 0; loop < 9973U; ++loop)
511                                         {
512                                                 const uint_fast8_t swap_count = (uint_fast8_t)gaussian_noise_next(rand, &bxmller, sigma, 4U, (ROW_LEN / 2U));
513                                                 if (!((++counter) & 0x3FFF))
514                                                 {
515                                                         printf("\b\b\b[%c]", SPINNER[g_spinpos]);
516                                                         g_spinpos = (g_spinpos + 1) % 4;
517                                                 }
518                                                 for (int_fast16_t round = 0; round < 997U; ++round)
519                                                 {
520                                                         memcpy(&temp[0], &g_table[i][0], sizeof(uint8_t) * ROW_LEN);
521                                                         swap_multiple_random(rand, &temp[0], swap_count);
522                                                         const uint_fast16_t error_next = check_permutation(i, &temp[0], error);
523                                                         if (error_next < error)
524                                                         {
525                                                                 TRACE("Improved by swap-N! (N=%u, %04X -> %04X)", swap_count, error, error_next);
526                                                                 memcpy(&g_table[i][0], &temp[0], sizeof(uint8_t) * ROW_LEN); /*keep*/
527                                                                 retry = -1;
528                                                                 if (!((error = error_next) > 0U))
529                                                                 {
530                                                                         goto success;
531                                                                 }
532                                                         }
533                                                 }
534                                         }
535                                 }
536                                 TRACE("Restart!");
537                                 msws_init(rand, make_seed());
538                         }
539                         else
540                         {
541                                 break; /*success*/
542                         }
543                 }
544
545         success:
546                 if (check_permutation(i, &g_table[i][0], UINT16_MAX))
547                 {
548                         fprintf(stderr, "ERROR MISCOMPARE!\n");
549                         abort();
550                 }
551
552                 get_time_str(time_string, 64);
553                 printf("\b\b\b[#] - %s\n", time_string);
554
555                 if (!save_table_data(argv[1], i + 1U))
556                 {
557                         return 1; /*failed to save current table data*/
558                 }
559         }
560
561         printf("\n-----\n\n");
562
563         dump_table(stdout);
564         if (fopen_s(&file_out, "table_MIX." DISTANCE_STR ".txt", "w") == 0)
565         {
566                 dump_table(file_out);
567                 fclose(file_out);
568         }
569
570         printf("\n-----\n\n");
571
572         printf("COMPLETED.\n\n");
573         return getchar();
574 }