aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2023-01-14 19:51:39 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2023-01-14 19:51:39 +0100
commitd97a625e8d96337dfa6a158cd247462b9cac29a5 (patch)
tree279de1da2de2bbda5b491e17d320ed3eef63b8f6
parent5cf18fa4694ab9f082813d04e3ec5b78a061d9df (diff)
downloadnissy-d97a625e8d96337dfa6a158cd247462b9cac29a5.tar.gz
nissy-d97a625e8d96337dfa6a158cd247462b9cac29a5.zip
Save fst tables to file
-rw-r--r--TODO.md1
-rw-r--r--src/fst.c143
2 files changed, 86 insertions, 58 deletions
diff --git a/TODO.md b/TODO.md
index 23cde09..16e547c 100644
--- a/TODO.md
+++ b/TODO.md
@@ -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"
diff --git a/src/fst.c b/src/fst.c
index 7fee732..b51e2d4 100644
--- a/src/fst.c
+++ b/src/fst.c
@@ -6,8 +6,9 @@ static FstCube ep_to_fst_epos(int *ep);
6static void init_fst_corner_invtables(); 6static void init_fst_corner_invtables();
7static void init_fst_eo_invtables(); 7static void init_fst_eo_invtables();
8static void init_fst_eo_update(uint64_t, uint64_t, int, Cube *); 8static void init_fst_eo_update(uint64_t, uint64_t, int, Cube *);
9static void init_fst_transalg();
10static void init_fst_where_is_edge(); 9static void init_fst_where_is_edge();
10static bool read_fst_tables_file();
11static bool write_fst_tables_file();
11 12
12static int edge_slice[12] = {[FR] = 0, [FL] = 0, [BL] = 0, [BR] = 0, 13static 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];
17static uint16_t inv_cp[FACTORIAL8]; 18static uint16_t inv_cp[FACTORIAL8];
18static uint16_t uf_cp_to_fr_cp[FACTORIAL8]; 19static uint16_t uf_cp_to_fr_cp[FACTORIAL8];
19static uint16_t uf_cp_to_rd_cp[FACTORIAL8]; 20static uint16_t uf_cp_to_rd_cp[FACTORIAL8];
20 21static uint16_t eo_invtable[3][POW2TO11][BINOM12ON4*FACTORIAL4];
21static int16_t eo_invtable[3][POW2TO11][BINOM12ON4*FACTORIAL4];
22
23static int trans_ep_alg[NROTATIONS][12];
24static int trans_ep_inv[NROTATIONS][12];
25
26static uint16_t fst_where_is_edge_arr[3][12][BINOM12ON4*FACTORIAL4]; 22static uint16_t fst_where_is_edge_arr[3][12][BINOM12ON4*FACTORIAL4];
27 23
28FstCube 24FstCube
@@ -53,32 +49,6 @@ cube_to_fst(Cube *cube)
53static FstCube 49static FstCube
54ep_to_fst_epos(int *ep) 50ep_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
253static void 228static void
@@ -320,26 +295,6 @@ init_fst_eo_update(uint64_t eo, uint64_t ep, int s, Cube *d)
320} 295}
321 296
322static void 297static void
323init_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
342static void
343init_fst_where_is_edge() 298init_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
329static bool
330read_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
366static bool
367write_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}

Generated with cgit - Back to sebastiano.tronto.net