diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2023-01-14 19:51:39 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2023-01-14 19:51:39 +0100 |
| commit | d97a625e8d96337dfa6a158cd247462b9cac29a5 (patch) | |
| tree | 279de1da2de2bbda5b491e17d320ed3eef63b8f6 | |
| parent | 5cf18fa4694ab9f082813d04e3ec5b78a061d9df (diff) | |
| download | nissy-d97a625e8d96337dfa6a158cd247462b9cac29a5.tar.gz nissy-d97a625e8d96337dfa6a158cd247462b9cac29a5.zip | |
Save fst tables to file
| -rw-r--r-- | TODO.md | 1 | ||||
| -rw-r--r-- | src/fst.c | 143 |
2 files changed, 86 insertions, 58 deletions
| @@ -6,7 +6,6 @@ It's more of a personal reminder than anything else. | |||
| 6 | ## After symcoord | 6 | ## After symcoord |
| 7 | ### Fixing stuff + completing new optimal solver | 7 | ### Fixing stuff + completing new optimal solver |
| 8 | - clean up fst, check if more can be improved | 8 | - clean up fst, check if more can be improved |
| 9 | - fst inverse tables: save to file | ||
| 10 | ### generic: | 9 | ### generic: |
| 11 | * all files should have an init function, calling the ones | 10 | * all files should have an init function, calling the ones |
| 12 | of the files includes + doing more stuff. A static "initiliazed" | 11 | of the files includes + doing more stuff. A static "initiliazed" |
| @@ -6,8 +6,9 @@ static FstCube ep_to_fst_epos(int *ep); | |||
| 6 | static void init_fst_corner_invtables(); | 6 | static void init_fst_corner_invtables(); |
| 7 | static void init_fst_eo_invtables(); | 7 | static void init_fst_eo_invtables(); |
| 8 | static void init_fst_eo_update(uint64_t, uint64_t, int, Cube *); | 8 | static void init_fst_eo_update(uint64_t, uint64_t, int, Cube *); |
| 9 | static void init_fst_transalg(); | ||
| 10 | static void init_fst_where_is_edge(); | 9 | static void init_fst_where_is_edge(); |
| 10 | static bool read_fst_tables_file(); | ||
| 11 | static bool write_fst_tables_file(); | ||
| 11 | 12 | ||
| 12 | static int edge_slice[12] = {[FR] = 0, [FL] = 0, [BL] = 0, [BR] = 0, | 13 | static int edge_slice[12] = {[FR] = 0, [FL] = 0, [BL] = 0, [BR] = 0, |
| 13 | [UL] = 1, [UR] = 1, [DR] = 1, [DL] = 1, | 14 | [UL] = 1, [UR] = 1, [DR] = 1, [DL] = 1, |
| @@ -17,12 +18,7 @@ static uint16_t inv_coud[FACTORIAL8][POW3TO7]; | |||
| 17 | static uint16_t inv_cp[FACTORIAL8]; | 18 | static uint16_t inv_cp[FACTORIAL8]; |
| 18 | static uint16_t uf_cp_to_fr_cp[FACTORIAL8]; | 19 | static uint16_t uf_cp_to_fr_cp[FACTORIAL8]; |
| 19 | static uint16_t uf_cp_to_rd_cp[FACTORIAL8]; | 20 | static uint16_t uf_cp_to_rd_cp[FACTORIAL8]; |
| 20 | 21 | static uint16_t eo_invtable[3][POW2TO11][BINOM12ON4*FACTORIAL4]; | |
| 21 | static int16_t eo_invtable[3][POW2TO11][BINOM12ON4*FACTORIAL4]; | ||
| 22 | |||
| 23 | static int trans_ep_alg[NROTATIONS][12]; | ||
| 24 | static int trans_ep_inv[NROTATIONS][12]; | ||
| 25 | |||
| 26 | static uint16_t fst_where_is_edge_arr[3][12][BINOM12ON4*FACTORIAL4]; | 22 | static uint16_t fst_where_is_edge_arr[3][12][BINOM12ON4*FACTORIAL4]; |
| 27 | 23 | ||
| 28 | FstCube | 24 | FstCube |
| @@ -53,32 +49,6 @@ cube_to_fst(Cube *cube) | |||
| 53 | static FstCube | 49 | static FstCube |
| 54 | ep_to_fst_epos(int *ep) | 50 | ep_to_fst_epos(int *ep) |
| 55 | { | 51 | { |
| 56 | |||
| 57 | /* The block of commented code works, but it is slower. | ||
| 58 | The actual code is mysterious, and there is some overlap with coord.c | ||
| 59 | (coord_eposepe.i), and trans.c, butbut it is faster. */ | ||
| 60 | |||
| 61 | /* | ||
| 62 | FstCube ret; | ||
| 63 | Cube c, d; | ||
| 64 | |||
| 65 | make_solved(&c); | ||
| 66 | memcpy(c.ep, ep, 12 * sizeof(int)); | ||
| 67 | |||
| 68 | copy_cube(&c, &d); | ||
| 69 | ret.uf_eposepe = coord_eposepe.i[0]->index(&d); | ||
| 70 | |||
| 71 | copy_cube(&c, &d); | ||
| 72 | apply_trans(fr, &d); | ||
| 73 | ret.fr_eposepe = coord_eposepe.i[0]->index(&d); | ||
| 74 | |||
| 75 | copy_cube(&c, &d); | ||
| 76 | apply_trans(rd, &d); | ||
| 77 | ret.rd_eposepe = coord_eposepe.i[0]->index(&d); | ||
| 78 | |||
| 79 | return ret; | ||
| 80 | */ | ||
| 81 | |||
| 82 | static int eind[12] = { | 52 | static int eind[12] = { |
| 83 | [FR] = 0, [FL] = 1, [BL] = 2, [BR] = 3, | 53 | [FR] = 0, [FL] = 1, [BL] = 2, [BR] = 3, |
| 84 | [UR] = 0, [DR] = 1, [DL] = 2, [UL] = 3, | 54 | [UR] = 0, [DR] = 1, [DL] = 2, [UL] = 3, |
| @@ -244,10 +214,15 @@ init_fst() | |||
| 244 | gen_coord(&coord_coud); | 214 | gen_coord(&coord_coud); |
| 245 | gen_coord(&coord_cp); | 215 | gen_coord(&coord_cp); |
| 246 | 216 | ||
| 247 | init_fst_corner_invtables(); | 217 | if (!read_fst_tables_file()) { |
| 248 | init_fst_eo_invtables(); | 218 | fprintf(stderr, |
| 249 | init_fst_transalg(); | 219 | "Could not load fst_tables, generating them\n"); |
| 250 | init_fst_where_is_edge(); | 220 | init_fst_corner_invtables(); |
| 221 | init_fst_eo_invtables(); | ||
| 222 | init_fst_where_is_edge(); | ||
| 223 | if (!write_fst_tables_file()) | ||
| 224 | fprintf(stderr, "fst_tables could not be written\b"); | ||
| 225 | } | ||
| 251 | } | 226 | } |
| 252 | 227 | ||
| 253 | static void | 228 | static void |
| @@ -320,26 +295,6 @@ init_fst_eo_update(uint64_t eo, uint64_t ep, int s, Cube *d) | |||
| 320 | } | 295 | } |
| 321 | 296 | ||
| 322 | static void | 297 | static void |
| 323 | init_fst_transalg() | ||
| 324 | { | ||
| 325 | Trans t; | ||
| 326 | Alg *alg; | ||
| 327 | Cube c; | ||
| 328 | int i; | ||
| 329 | |||
| 330 | for (t = uf; t < NROTATIONS; t++) { | ||
| 331 | make_solved(&c); | ||
| 332 | alg = rotation_alg(t); | ||
| 333 | apply_alg(alg, &c); | ||
| 334 | for (i = 0; i < 12; i++) | ||
| 335 | trans_ep_alg[t][i] = c.ep[i]; | ||
| 336 | invert_cube_edges(&c); | ||
| 337 | for (i = 0; i < 12; i++) | ||
| 338 | trans_ep_inv[t][i] = c.ep[i]; | ||
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | static void | ||
| 343 | init_fst_where_is_edge() | 298 | init_fst_where_is_edge() |
| 344 | { | 299 | { |
| 345 | Cube c, d; | 300 | Cube c, d; |
| @@ -370,3 +325,77 @@ init_fst_where_is_edge() | |||
| 370 | fst_where_is_edge_arr[2][DB][e] = where_is_edge(DB, &d); | 325 | fst_where_is_edge_arr[2][DB][e] = where_is_edge(DB, &d); |
| 371 | } | 326 | } |
| 372 | } | 327 | } |
| 328 | |||
| 329 | static bool | ||
| 330 | read_fst_tables_file() | ||
| 331 | { | ||
| 332 | init_env(); | ||
| 333 | |||
| 334 | FILE *f; | ||
| 335 | char fname[strlen(tabledir)+256]; | ||
| 336 | uint64_t i, j, r, total; | ||
| 337 | |||
| 338 | strcpy(fname, tabledir); | ||
| 339 | strcat(fname, "/fst_tables"); | ||
| 340 | |||
| 341 | if ((f = fopen(fname, "rb")) == NULL) | ||
| 342 | return false; | ||
| 343 | |||
| 344 | r = 0; | ||
| 345 | total = FACTORIAL8*(POW3TO7+3) + 3*BINOM12ON4*FACTORIAL4*(12+POW2TO11); | ||
| 346 | |||
| 347 | for (i = 0; i < FACTORIAL8; i++) | ||
| 348 | r += fread(inv_coud[i], sizeof(uint16_t), POW3TO7, f); | ||
| 349 | r += fread(inv_cp, sizeof(uint16_t), FACTORIAL8, f); | ||
| 350 | r += fread(uf_cp_to_fr_cp, sizeof(uint16_t), FACTORIAL8, f); | ||
| 351 | r += fread(uf_cp_to_rd_cp, sizeof(uint16_t), FACTORIAL8, f); | ||
| 352 | for (i = 0; i < 3; i++) | ||
| 353 | for (j = 0; j < POW2TO11; j++) | ||
| 354 | r += fread(eo_invtable[i][j], | ||
| 355 | sizeof(uint16_t), BINOM12ON4*FACTORIAL4, f); | ||
| 356 | for (i = 0; i < 3; i++) | ||
| 357 | for (j = 0; j < 12; j++) | ||
| 358 | r += fread(fst_where_is_edge_arr[i][j], | ||
| 359 | sizeof(uint16_t), BINOM12ON4*FACTORIAL4, f); | ||
| 360 | |||
| 361 | fclose(f); | ||
| 362 | |||
| 363 | return r == total; | ||
| 364 | } | ||
| 365 | |||
| 366 | static bool | ||
| 367 | write_fst_tables_file() | ||
| 368 | { | ||
| 369 | init_env(); | ||
| 370 | |||
| 371 | FILE *f; | ||
| 372 | char fname[strlen(tabledir)+256]; | ||
| 373 | uint64_t i, j, w, total; | ||
| 374 | |||
| 375 | strcpy(fname, tabledir); | ||
| 376 | strcat(fname, "/fst_tables"); | ||
| 377 | |||
| 378 | if ((f = fopen(fname, "wb")) == NULL) | ||
| 379 | return false; | ||
| 380 | |||
| 381 | w = 0; | ||
| 382 | total = FACTORIAL8*(POW3TO7+3) + 3*BINOM12ON4*FACTORIAL4*(12+POW2TO11); | ||
| 383 | |||
| 384 | for (i = 0; i < FACTORIAL8; i++) | ||
| 385 | w += fwrite(inv_coud[i], sizeof(uint16_t), POW3TO7, f); | ||
| 386 | w += fwrite(inv_cp, sizeof(uint16_t), FACTORIAL8, f); | ||
| 387 | w += fwrite(uf_cp_to_fr_cp, sizeof(uint16_t), FACTORIAL8, f); | ||
| 388 | w += fwrite(uf_cp_to_rd_cp, sizeof(uint16_t), FACTORIAL8, f); | ||
| 389 | for (i = 0; i < 3; i++) | ||
| 390 | for (j = 0; j < POW2TO11; j++) | ||
| 391 | w += fwrite(eo_invtable[i][j], | ||
| 392 | sizeof(uint16_t), BINOM12ON4*FACTORIAL4, f); | ||
| 393 | for (i = 0; i < 3; i++) | ||
| 394 | for (j = 0; j < 12; j++) | ||
| 395 | w += fwrite(fst_where_is_edge_arr[i][j], | ||
| 396 | sizeof(uint16_t), BINOM12ON4*FACTORIAL4, f); | ||
| 397 | |||
| 398 | fclose(f); | ||
| 399 | |||
| 400 | return w == total; | ||
| 401 | } | ||
