aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2022-12-26 12:41:36 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2022-12-26 12:41:36 +0100
commitcecdb7c5a0fc4821bc56c6aae5428925ec1baa15 (patch)
tree5f478c2154ffafe55f1866edd82546d1472fb6c3 /src
parent1ac26de8dcb76323a5f96eab200a027236cb55d0 (diff)
downloadnissy-cecdb7c5a0fc4821bc56c6aae5428925ec1baa15.tar.gz
nissy-cecdb7c5a0fc4821bc56c6aae5428925ec1baa15.zip
Fixed fst_inverse
Diffstat (limited to '')
-rw-r--r--src/cube.c126
-rw-r--r--src/cube.h12
-rw-r--r--src/fst.c75
-rw-r--r--src/fst.h1
-rw-r--r--src/moves.c18
-rw-r--r--src/moves.h3
6 files changed, 189 insertions, 46 deletions
diff --git a/src/cube.c b/src/cube.c
index c2a457c..92c6713 100644
--- a/src/cube.c
+++ b/src/cube.c
@@ -5,27 +5,61 @@
5static int where_is_piece(int piece, int *arr, int n); 5static int where_is_piece(int piece, int *arr, int n);
6 6
7void 7void
8compose(Cube *c2, Cube *c1) 8compose_centers(Cube *c2, Cube *c1)
9{ 9{
10 apply_permutation(c2->ep, c1->ep, 12); 10 apply_permutation(c2->xp, c1->xp, 6);
11 apply_permutation(c2->ep, c1->eo, 12); 11}
12 sum_arrays_mod(c2->eo, c1->eo, 12, 2);
13 12
13void
14compose_corners(Cube *c2, Cube *c1)
15{
14 apply_permutation(c2->cp, c1->cp, 8); 16 apply_permutation(c2->cp, c1->cp, 8);
15 apply_permutation(c2->cp, c1->co, 8); 17 apply_permutation(c2->cp, c1->co, 8);
16 sum_arrays_mod(c2->co, c1->co, 8, 3); 18 sum_arrays_mod(c2->co, c1->co, 8, 3);
19}
17 20
18 apply_permutation(c2->xp, c1->xp, 6); 21void
22compose_edges(Cube *c2, Cube *c1)
23{
24 apply_permutation(c2->ep, c1->ep, 12);
25 apply_permutation(c2->ep, c1->eo, 12);
26 sum_arrays_mod(c2->eo, c1->eo, 12, 2);
19} 27}
20 28
21void 29void
22copy_cube(Cube *src, Cube *dst) 30compose(Cube *c2, Cube *c1)
31{
32 compose_centers(c2, c1);
33 compose_corners(c2, c1);
34 compose_edges(c2, c1);
35}
36
37void
38copy_cube_centers(Cube *src, Cube *dst)
39{
40 memcpy(dst->xp, src->xp, 6 * sizeof(int));
41}
42
43void
44copy_cube_corners(Cube *src, Cube *dst)
23{ 45{
24 memcpy(dst->ep, src->ep, 12 * sizeof(int));
25 memcpy(dst->eo, src->eo, 12 * sizeof(int));
26 memcpy(dst->cp, src->cp, 8 * sizeof(int)); 46 memcpy(dst->cp, src->cp, 8 * sizeof(int));
27 memcpy(dst->co, src->co, 8 * sizeof(int)); 47 memcpy(dst->co, src->co, 8 * sizeof(int));
28 memcpy(dst->xp, src->xp, 6 * sizeof(int)); 48}
49
50void
51copy_cube_edges(Cube *src, Cube *dst)
52{
53 memcpy(dst->ep, src->ep, 12 * sizeof(int));
54 memcpy(dst->eo, src->eo, 12 * sizeof(int));
55}
56
57void
58copy_cube(Cube *src, Cube *dst)
59{
60 copy_cube_centers(src, dst);
61 copy_cube_corners(src, dst);
62 copy_cube_edges(src, dst);
29} 63}
30 64
31bool 65bool
@@ -49,25 +83,51 @@ equal(Cube *c1, Cube *c2)
49} 83}
50 84
51void 85void
52invert_cube(Cube *cube) 86invert_cube_centers(Cube *cube)
53{ 87{
54 Cube aux;
55 int i; 88 int i;
89 Cube aux;
56 90
57 copy_cube(cube, &aux); 91 copy_cube_centers(cube, &aux);
58 92
59 for (i = 0; i < 12; i++) { 93 for (i = 0; i < 6; i++)
60 cube->ep[aux.ep[i]] = i; 94 cube->xp[aux.xp[i]] = i;
61 cube->eo[aux.ep[i]] = aux.eo[i]; 95}
62 } 96
97void
98invert_cube_corners(Cube *cube)
99{
100 int i;
101 Cube aux;
102
103 copy_cube_corners(cube, &aux);
63 104
64 for (i = 0; i < 8; i++) { 105 for (i = 0; i < 8; i++) {
65 cube->cp[aux.cp[i]] = i; 106 cube->cp[aux.cp[i]] = i;
66 cube->co[aux.cp[i]] = (3 - aux.co[i]) % 3; 107 cube->co[aux.cp[i]] = (3 - aux.co[i]) % 3;
67 } 108 }
109}
68 110
69 for (i = 0; i < 6; i++) 111void
70 cube->xp[aux.xp[i]] = i; 112invert_cube_edges(Cube *cube)
113{
114 int i;
115 Cube aux;
116
117 copy_cube_edges(cube, &aux);
118
119 for (i = 0; i < 12; i++) {
120 cube->ep[aux.ep[i]] = i;
121 cube->eo[aux.ep[i]] = aux.eo[i];
122 }
123}
124
125void
126invert_cube(Cube *cube)
127{
128 invert_cube_centers(cube);
129 invert_cube_corners(cube);
130 invert_cube_edges(cube);
71} 131}
72 132
73bool 133bool
@@ -105,15 +165,37 @@ is_solved(Cube *cube)
105} 165}
106 166
107void 167void
108make_solved(Cube *cube) 168make_solved_centers(Cube *cube)
169{
170 static int sorted[6] = {0, 1, 2, 3, 4, 5};
171
172 memcpy(cube->xp, sorted, 6 * sizeof(int));
173}
174
175void
176make_solved_corners(Cube *cube)
177{
178 static int sorted[8] = {0, 1, 2, 3, 4, 5, 6, 7};
179
180 memcpy(cube->cp, sorted, 8 * sizeof(int));
181 memset(cube->co, 0, 8 * sizeof(int));
182}
183
184void
185make_solved_edges(Cube *cube)
109{ 186{
110 static int sorted[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; 187 static int sorted[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
111 188
112 memcpy(cube->ep, sorted, 12 * sizeof(int)); 189 memcpy(cube->ep, sorted, 12 * sizeof(int));
113 memset(cube->eo, 0, 12 * sizeof(int)); 190 memset(cube->eo, 0, 12 * sizeof(int));
114 memcpy(cube->cp, sorted, 8 * sizeof(int)); 191}
115 memset(cube->co, 0, 8 * sizeof(int)); 192
116 memcpy(cube->xp, sorted, 6 * sizeof(int)); 193void
194make_solved(Cube *cube)
195{
196 make_solved_centers(cube);
197 make_solved_corners(cube);
198 make_solved_edges(cube);
117} 199}
118 200
119void 201void
diff --git a/src/cube.h b/src/cube.h
index 789bb2e..f182b27 100644
--- a/src/cube.h
+++ b/src/cube.h
@@ -8,12 +8,24 @@
8#include "utils.h" 8#include "utils.h"
9 9
10void compose(Cube *c2, Cube *c1); /* Use c2 as an alg on c1 */ 10void compose(Cube *c2, Cube *c1); /* Use c2 as an alg on c1 */
11void compose_centers(Cube *c2, Cube *c1);
12void compose_corners(Cube *c2, Cube *c1);
13void compose_edges(Cube *c2, Cube *c1);
11void copy_cube(Cube *src, Cube *dst); 14void copy_cube(Cube *src, Cube *dst);
15void copy_cube_centers(Cube *src, Cube *dst);
16void copy_cube_corners(Cube *src, Cube *dst);
17void copy_cube_edges(Cube *src, Cube *dst);
12bool equal(Cube *c1, Cube *c2); 18bool equal(Cube *c1, Cube *c2);
13void invert_cube(Cube *cube); 19void invert_cube(Cube *cube);
20void invert_cube_centers(Cube *cube);
21void invert_cube_corners(Cube *cube);
22void invert_cube_edges(Cube *cube);
14bool is_admissible(Cube *cube); 23bool is_admissible(Cube *cube);
15bool is_solved(Cube *cube); 24bool is_solved(Cube *cube);
16void make_solved(Cube *cube); 25void make_solved(Cube *cube);
26void make_solved_centers(Cube *cube);
27void make_solved_corners(Cube *cube);
28void make_solved_edges(Cube *cube);
17void print_cube(Cube *cube); 29void print_cube(Cube *cube);
18int where_is_center(Center x, Cube *c); 30int where_is_center(Center x, Cube *c);
19int where_is_corner(Corner k, Cube *c); 31int where_is_corner(Corner k, Cube *c);
diff --git a/src/fst.c b/src/fst.c
index aa9e41e..df5b81f 100644
--- a/src/fst.c
+++ b/src/fst.c
@@ -3,7 +3,6 @@
3#include "fst.h" 3#include "fst.h"
4 4
5static FstCube ep_to_fst_epos(int *ep); 5static FstCube ep_to_fst_epos(int *ep);
6static int fst_where_is_edge(int e, FstCube fst);
7static void transform_ep_only(Trans t, int *ep, Cube *dst); 6static void transform_ep_only(Trans t, int *ep, Cube *dst);
8static void init_fst_corner_invtables(); 7static void init_fst_corner_invtables();
9static void init_fst_eo_invtables(); 8static void init_fst_eo_invtables();
@@ -55,7 +54,10 @@ cube_to_fst(Cube *cube)
55static FstCube 54static FstCube
56ep_to_fst_epos(int *ep) 55ep_to_fst_epos(int *ep)
57{ 56{
58 /* TODO: maybe optimize? */ 57 /* TODO: maybe optimize */
58
59/* TODO: this version if faster, but broken
60 probably need to fix transform_ep_only()
59 61
60 FstCube ret; 62 FstCube ret;
61 Cube c; 63 Cube c;
@@ -68,6 +70,24 @@ ep_to_fst_epos(int *ep)
68 70
69 transform_ep_only(rd, ep, &c); 71 transform_ep_only(rd, ep, &c);
70 ret.rd_eposepe = coord_eposepe.i[0]->index(&c); 72 ret.rd_eposepe = coord_eposepe.i[0]->index(&c);
73*/
74
75 FstCube ret;
76 Cube c, d;
77
78 make_solved(&c);
79 memcpy(c.ep, ep, 12 * sizeof(int));
80
81 copy_cube(&c, &d);
82 ret.uf_eposepe = coord_eposepe.i[0]->index(&d);
83
84 copy_cube(&c, &d);
85 apply_trans(fr, &d);
86 ret.fr_eposepe = coord_eposepe.i[0]->index(&d);
87
88 copy_cube(&c, &d);
89 apply_trans(rd, &d);
90 ret.rd_eposepe = coord_eposepe.i[0]->index(&d);
71 91
72 return ret; 92 return ret;
73} 93}
@@ -155,7 +175,7 @@ fst_to_cube(FstCube fst, Cube *cube)
155 cube->xp[i] = i; 175 cube->xp[i] = i;
156} 176}
157 177
158static int 178int
159fst_where_is_edge(int e, FstCube fst) 179fst_where_is_edge(int e, FstCube fst)
160{ 180{
161 switch (edge_slice[e]) { 181 switch (edge_slice[e]) {
@@ -183,6 +203,10 @@ void
183init_fst() 203init_fst()
184{ 204{
185 init_trans(); 205 init_trans();
206 gen_coord(&coord_eofb);
207 gen_coord(&coord_eposepe);
208 gen_coord(&coord_coud);
209 gen_coord(&coord_cp);
186 210
187 init_fst_corner_invtables(); 211 init_fst_corner_invtables();
188 init_fst_eo_invtables(); 212 init_fst_eo_invtables();
@@ -193,32 +217,29 @@ init_fst()
193static void 217static void
194init_fst_corner_invtables() 218init_fst_corner_invtables()
195{ 219{
196/* TODO: this can be optimized by transforming and copying only corners */
197/* A factor of about 4 would be saved in the innermost loop */
198
199 Cube c, d; 220 Cube c, d;
200 uint64_t cp, coud; 221 uint64_t cp, coud;
201 222
202 for (cp = 0; cp < FACTORIAL8; cp++) { 223 for (cp = 0; cp < FACTORIAL8; cp++) {
203 make_solved(&c); 224 make_solved_corners(&c);
204 coord_cp.i[0]->to_cube(cp, &c); 225 coord_cp.i[0]->to_cube(cp, &c);
205 226
206 copy_cube(&c, &d); 227 copy_cube_corners(&c, &d);
207 invert_cube(&d); 228 invert_cube_corners(&d);
208 inv_cp[cp] = coord_coud.i[0]->index(&d); 229 inv_cp[cp] = coord_cp.i[0]->index(&d);
209 230
210 for (coud = 0; coud < POW3TO7; coud++) { 231 for (coud = 0; coud < POW3TO7; coud++) {
211 copy_cube(&c, &d); 232 copy_cube_corners(&c, &d);
212 coord_coud.i[0]->to_cube(coud, &d); 233 coord_coud.i[0]->to_cube(coud, &d);
213 invert_cube(&d); 234 invert_cube_corners(&d);
214 inv_coud[cp][coud] = coord_coud.i[0]->index(&d); 235 inv_coud[cp][coud] = coord_coud.i[0]->index(&d);
215 } 236 }
216 237
217 copy_cube(&c, &d); 238 copy_cube_corners(&c, &d);
218 apply_trans(fr, &d); 239 apply_trans(fr, &d);
219 uf_cp_to_fr_cp[cp] = coord_cp.i[0]->index(&d); 240 uf_cp_to_fr_cp[cp] = coord_cp.i[0]->index(&d);
220 241
221 copy_cube(&c, &d); 242 copy_cube_corners(&c, &d);
222 apply_trans(rd, &d); 243 apply_trans(rd, &d);
223 uf_cp_to_rd_cp[cp] = coord_cp.i[0]->index(&d); 244 uf_cp_to_rd_cp[cp] = coord_cp.i[0]->index(&d);
224 } 245 }
@@ -234,13 +255,17 @@ init_fst_eo_invtables()
234 make_solved(&c); 255 make_solved(&c);
235 coord_eposepe.i[0]->to_cube(ep, &c); 256 coord_eposepe.i[0]->to_cube(ep, &c);
236 for (eo = 0; eo < POW2TO11; eo++) { 257 for (eo = 0; eo < POW2TO11; eo++) {
237 coord_eofb.i[0]->to_cube(eo, &c); 258 copy_cube_edges(&c, &d);
238 copy_cube(&c, &d); 259 coord_eofb.i[0]->to_cube(eo, &d);
239 init_fst_eo_update(eo, ep, 0, &d); 260 init_fst_eo_update(eo, ep, 0, &d);
261
240 apply_trans(inverse_trans(fr), &d); 262 apply_trans(inverse_trans(fr), &d);
263 coord_eofb.i[0]->to_cube(eo, &d);
241 init_fst_eo_update(eo, ep, 1, &d); 264 init_fst_eo_update(eo, ep, 1, &d);
242 copy_cube(&c, &d); 265
266 copy_cube_edges(&c, &d);
243 apply_trans(inverse_trans(rd), &d); 267 apply_trans(inverse_trans(rd), &d);
268 coord_eofb.i[0]->to_cube(eo, &d);
244 init_fst_eo_update(eo, ep, 2, &d); 269 init_fst_eo_update(eo, ep, 2, &d);
245 } 270 }
246 } 271 }
@@ -251,9 +276,11 @@ init_fst_eo_update(uint64_t eo, uint64_t ep, int s, Cube *d)
251{ 276{
252 int i; 277 int i;
253 278
254 for (i = 0; i < 12; i++) 279 for (i = 0; i < 12; i++) {
255 if (d->eo[i]) 280 if (edge_slice[d->ep[i]] == s && d->eo[i] && d->ep[i] != 11)
256 eo_invtable[s][eo][ep] |= ((uint16_t)1) << d->ep[i]; 281 eo_invtable[s][eo][ep] |=
282 ((uint16_t)1) << ((uint16_t)d->ep[i]);
283 }
257} 284}
258 285
259static void 286static void
@@ -270,7 +297,7 @@ init_fst_transalg()
270 apply_alg(alg, &c); 297 apply_alg(alg, &c);
271 for (i = 0; i < 12; i++) 298 for (i = 0; i < 12; i++)
272 trans_ep_alg[t][i] = c.ep[i]; 299 trans_ep_alg[t][i] = c.ep[i];
273 invert_cube(&c); 300 invert_cube_edges(&c);
274 for (i = 0; i < 12; i++) 301 for (i = 0; i < 12; i++)
275 trans_ep_inv[t][i] = c.ep[i]; 302 trans_ep_inv[t][i] = c.ep[i];
276 } 303 }
@@ -286,20 +313,20 @@ init_fst_where_is_edge()
286 for (e = 0; e < BINOM12ON4 * FACTORIAL4; e++) { 313 for (e = 0; e < BINOM12ON4 * FACTORIAL4; e++) {
287 coord_eposepe.i[0]->to_cube(e, &c); 314 coord_eposepe.i[0]->to_cube(e, &c);
288 315
289 copy_cube(&c, &d); 316 copy_cube_edges(&c, &d);
290 fst_where_is_edge_arr[0][FR][e] = where_is_edge(FR, &d); 317 fst_where_is_edge_arr[0][FR][e] = where_is_edge(FR, &d);
291 fst_where_is_edge_arr[0][FL][e] = where_is_edge(FL, &d); 318 fst_where_is_edge_arr[0][FL][e] = where_is_edge(FL, &d);
292 fst_where_is_edge_arr[0][BL][e] = where_is_edge(BL, &d); 319 fst_where_is_edge_arr[0][BL][e] = where_is_edge(BL, &d);
293 fst_where_is_edge_arr[0][BR][e] = where_is_edge(BR, &d); 320 fst_where_is_edge_arr[0][BR][e] = where_is_edge(BR, &d);
294 321
295 copy_cube(&c, &d); 322 copy_cube_edges(&c, &d);
296 apply_trans(inverse_trans(fr), &d); 323 apply_trans(inverse_trans(fr), &d);
297 fst_where_is_edge_arr[1][UL][e] = where_is_edge(UL, &d); 324 fst_where_is_edge_arr[1][UL][e] = where_is_edge(UL, &d);
298 fst_where_is_edge_arr[1][UR][e] = where_is_edge(UR, &d); 325 fst_where_is_edge_arr[1][UR][e] = where_is_edge(UR, &d);
299 fst_where_is_edge_arr[1][DL][e] = where_is_edge(DL, &d); 326 fst_where_is_edge_arr[1][DL][e] = where_is_edge(DL, &d);
300 fst_where_is_edge_arr[1][DR][e] = where_is_edge(DR, &d); 327 fst_where_is_edge_arr[1][DR][e] = where_is_edge(DR, &d);
301 328
302 copy_cube(&c, &d); 329 copy_cube_edges(&c, &d);
303 apply_trans(inverse_trans(rd), &d); 330 apply_trans(inverse_trans(rd), &d);
304 fst_where_is_edge_arr[2][UF][e] = where_is_edge(UF, &d); 331 fst_where_is_edge_arr[2][UF][e] = where_is_edge(UF, &d);
305 fst_where_is_edge_arr[2][UB][e] = where_is_edge(UB, &d); 332 fst_where_is_edge_arr[2][UB][e] = where_is_edge(UB, &d);
diff --git a/src/fst.h b/src/fst.h
index c3b226c..be5e99b 100644
--- a/src/fst.h
+++ b/src/fst.h
@@ -7,6 +7,7 @@ FstCube cube_to_fst(Cube *cube);
7FstCube fst_inverse(FstCube fst); 7FstCube fst_inverse(FstCube fst);
8FstCube fst_move(Move m, FstCube fst); 8FstCube fst_move(Move m, FstCube fst);
9void fst_to_cube(FstCube fst, Cube *cube); 9void fst_to_cube(FstCube fst, Cube *cube);
10int fst_where_is_edge(int e, FstCube fst);
10void init_fst(); 11void init_fst();
11 12
12#endif 13#endif
diff --git a/src/moves.c b/src/moves.c
index 51012db..a9dfdc0 100644
--- a/src/moves.c
+++ b/src/moves.c
@@ -106,6 +106,24 @@ apply_move(Move m, Cube *cube)
106 compose(&move_array[m], cube); 106 compose(&move_array[m], cube);
107} 107}
108 108
109void
110apply_move_centers(Move m, Cube *cube)
111{
112 compose_centers(&move_array[m], cube);
113}
114
115void
116apply_move_corners(Move m, Cube *cube)
117{
118 compose_corners(&move_array[m], cube);
119}
120
121void
122apply_move_edges(Move m, Cube *cube)
123{
124 compose_edges(&move_array[m], cube);
125}
126
109Alg * 127Alg *
110cleanup(Alg *alg) 128cleanup(Alg *alg)
111{ 129{
diff --git a/src/moves.h b/src/moves.h
index 4afe8fb..4e8be7f 100644
--- a/src/moves.h
+++ b/src/moves.h
@@ -7,6 +7,9 @@
7 7
8void apply_alg(Alg *alg, Cube *cube); 8void apply_alg(Alg *alg, Cube *cube);
9void apply_move(Move m, Cube *cube); 9void apply_move(Move m, Cube *cube);
10void apply_move_centers(Move m, Cube *cube);
11void apply_move_corners(Move m, Cube *cube);
12void apply_move_edges(Move m, Cube *cube);
10Alg * cleanup(Alg *alg); 13Alg * cleanup(Alg *alg);
11 14
12void init_moves(); 15void init_moves();

Generated with cgit - Back to sebastiano.tronto.net