aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-04-01 09:33:26 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2025-04-01 09:33:26 +0200
commitb0053277e385bee23336d1fe6b69a12f49f9172f (patch)
tree531e44cd6f8e807c991d0a46692140667939fbe3
parentb6c1ff6cfdfe0f4ce602fe83e54c3112a1c95690 (diff)
downloadnissy-core-b0053277e385bee23336d1fe6b69a12f49f9172f.tar.gz
nissy-core-b0053277e385bee23336d1fe6b69a12f49f9172f.zip
simplified allowedmoves logic
-rw-r--r--src/core/moves.h52
-rw-r--r--src/solvers/coord/solve.h14
-rw-r--r--src/solvers/h48/gendata_h48.h4
-rw-r--r--src/solvers/h48/solve.h21
-rw-r--r--src/solvers/solutions.h2
-rw-r--r--src/utils/constants.h21
-rw-r--r--test/080_allowedmoves/00_empty.in (renamed from test/080_allowednext/00_empty_U.in)1
-rw-r--r--test/080_allowedmoves/00_empty.out (renamed from test/080_allowednext/00_empty_U.out)0
-rw-r--r--test/080_allowedmoves/01_onemove.in2
-rw-r--r--test/080_allowedmoves/01_onemove.out (renamed from test/080_allowednext/02_U_F.out)0
-rw-r--r--test/080_allowedmoves/02_F_F2.in (renamed from test/080_allowednext/01_F_F2.in)0
-rw-r--r--test/080_allowedmoves/02_F_F2.out (renamed from test/080_allowednext/01_F_F2.out)0
-rw-r--r--test/080_allowedmoves/03_U_F.in (renamed from test/080_allowednext/02_U_F.in)0
-rw-r--r--test/080_allowedmoves/03_U_F.out (renamed from test/080_allowednext/04_longer_true.out)0
-rw-r--r--test/080_allowedmoves/04_R_L_R.in (renamed from test/080_allowednext/03_R_L_R.in)0
-rw-r--r--test/080_allowedmoves/04_R_L_R.out (renamed from test/080_allowednext/03_R_L_R.out)0
-rw-r--r--test/080_allowedmoves/05_longer_true.in (renamed from test/080_allowednext/04_longer_true.in)8
-rw-r--r--test/080_allowedmoves/05_longer_true.out1
-rw-r--r--test/080_allowedmoves/06_longer_false.in (renamed from test/080_allowednext/05_longer_false.in)11
-rw-r--r--test/080_allowedmoves/06_longer_false.out (renamed from test/080_allowednext/05_longer_false.out)0
-rw-r--r--test/080_allowedmoves/07_D_U_false_order.in (renamed from test/080_allowednext/06_D_U_false_order.in)0
-rw-r--r--test/080_allowedmoves/07_D_U_false_order.out (renamed from test/080_allowednext/06_D_U_false_order.out)0
-rw-r--r--test/080_allowedmoves/allowedmoves_tests.c (renamed from test/080_allowednext/allowednext_tests.c)8
23 files changed, 74 insertions, 71 deletions
diff --git a/src/core/moves.h b/src/core/moves.h
index 11ac34e..1238c7d 100644
--- a/src/core/moves.h
+++ b/src/core/moves.h
@@ -1,15 +1,13 @@
1#define MOVE(M, c) compose(c, MOVE_CUBE_ ## M) 1#define MOVE(M, c) compose(c, MOVE_CUBE_ ## M)
2#define PREMOVE(M, c) compose(MOVE_CUBE_ ## M, c) 2#define PREMOVE(M, c) compose(MOVE_CUBE_ ## M, c)
3 3
4STATIC_INLINE bool allowednextmove(size_t n, const uint8_t [n]); 4STATIC_INLINE bool allowednextmove(uint8_t, uint8_t);
5STATIC_INLINE uint32_t allowednextmove_mask(size_t n, const uint8_t [n]);
6STATIC bool allowedmoves(size_t n, const uint8_t [n]); 5STATIC bool allowedmoves(size_t n, const uint8_t [n]);
7 6
8STATIC_INLINE uint8_t movebase(uint8_t); 7STATIC_INLINE uint8_t movebase(uint8_t);
9STATIC_INLINE uint8_t moveaxis(uint8_t); 8STATIC_INLINE uint8_t moveaxis(uint8_t);
10STATIC_INLINE bool isbase(uint8_t); 9STATIC_INLINE bool isbase(uint8_t);
11STATIC_INLINE bool parallel(uint8_t, uint8_t); 10STATIC_INLINE bool parallel(uint8_t, uint8_t);
12STATIC_INLINE uint32_t disable_moves(uint32_t, uint8_t);
13 11
14STATIC cube_t move(cube_t, uint8_t); 12STATIC cube_t move(cube_t, uint8_t);
15STATIC cube_t premove(cube_t, uint8_t); 13STATIC cube_t premove(cube_t, uint8_t);
@@ -38,60 +36,24 @@ STATIC cube_t applymoves(cube_t, const char *);
38 ARG_ACTION \ 36 ARG_ACTION \
39 } 37 }
40 38
41STATIC bool 39STATIC_INLINE bool
42allowednextmove(size_t n, const uint8_t moves[n]) 40allowednextmove(uint8_t m1, uint8_t m2)
43{ 41{
44 return n == 0 || allowednextmove_mask(n-1, moves) & (1 << moves[n-1]); 42 return allowedmask[movebase(m1)] & (UINT32_C(1) << m2);
45}
46
47STATIC uint32_t
48allowednextmove_mask(size_t n, const uint8_t moves[n])
49{
50 uint32_t result;
51 uint8_t base1, base2, axis1, axis2;
52
53 result = MM_ALLMOVES;
54
55 if (n == 0)
56 return result;
57
58 base1 = movebase(moves[n-1]);
59 axis1 = moveaxis(moves[n-1]);
60 result = disable_moves(result, base1 * 3);
61
62 if (base1 % 2)
63 result = disable_moves(result, (base1 - 1) * 3);
64
65 if (n == 1)
66 return result;
67
68 base2 = movebase(moves[n-2]);
69 axis2 = moveaxis(moves[n-2]);
70
71 if(axis1 == axis2)
72 result = disable_moves(result, base2 * 3);
73
74 return result;
75} 43}
76 44
77STATIC bool 45STATIC bool
78allowedmoves(size_t n, const uint8_t moves[n]) 46allowedmoves(size_t n, const uint8_t m[n])
79{ 47{
80 uint8_t j; 48 uint8_t j;
81 49
82 for (j = 2; j < n; j++) 50 for (j = 1; j < n; j++)
83 if (!allowednextmove(j, moves)) 51 if (!allowednextmove(m[j-1], m[j]))
84 return false; 52 return false;
85 53
86 return true; 54 return true;
87} 55}
88 56
89STATIC_INLINE uint32_t
90disable_moves(uint32_t current_result, uint8_t base_index)
91{
92 return current_result & ~MM_SIDE(base_index);
93}
94
95STATIC_INLINE uint8_t 57STATIC_INLINE uint8_t
96movebase(uint8_t move) 58movebase(uint8_t move)
97{ 59{
diff --git a/src/solvers/coord/solve.h b/src/solvers/coord/solve.h
index c3fbd02..fa4134a 100644
--- a/src/solvers/coord/solve.h
+++ b/src/solvers/coord/solve.h
@@ -135,7 +135,12 @@ solve_coord_dfs(dfsarg_solve_coord_t arg[static 1])
135 ret = 0; 135 ret = 0;
136 if (coord_continue_onnormal(arg)) { 136 if (coord_continue_onnormal(arg)) {
137 l = arg->solution_moves->nmoves; 137 l = arg->solution_moves->nmoves;
138 mm = allowednextmove_mask(l, arg->solution_moves->moves); 138 if (l == 0) {
139 mm = MM_ALLMOVES;
140 } else {
141 m = arg->solution_moves->moves[l-1];
142 mm = allowedmask[movebase(m)];
143 }
139 arg->solution_moves->nmoves++; 144 arg->solution_moves->nmoves++;
140 arg->lastisnormal = true; 145 arg->lastisnormal = true;
141 146
@@ -160,7 +165,12 @@ solve_coord_dfs(dfsarg_solve_coord_t arg[static 1])
160 165
161 if (coord_continue_oninverse(arg)) { 166 if (coord_continue_oninverse(arg)) {
162 l = arg->solution_moves->npremoves; 167 l = arg->solution_moves->npremoves;
163 mm = allowednextmove_mask(l, arg->solution_moves->premoves); 168 if (l == 0) {
169 mm = MM_ALLMOVES;
170 } else {
171 m = arg->solution_moves->premoves[l-1];
172 mm = allowedmask[movebase(m)];
173 }
164 arg->solution_moves->npremoves++; 174 arg->solution_moves->npremoves++;
165 arg->lastisnormal = false; 175 arg->lastisnormal = false;
166 176
diff --git a/src/solvers/h48/gendata_h48.h b/src/solvers/h48/gendata_h48.h
index 959264d..b6647ed 100644
--- a/src/solvers/h48/gendata_h48.h
+++ b/src/solvers/h48/gendata_h48.h
@@ -526,7 +526,7 @@ gendata_h48k2_dfs(h48k2_dfs_arg_t arg[static 1])
526 /* Depth d+3 */ 526 /* Depth d+3 */
527 for (m[2] = 0; m[2] < 18; m[2]++) { 527 for (m[2] = 0; m[2] < 18; m[2]++) {
528 markarg.depth = d+3; 528 markarg.depth = d+3;
529 if (!allowednextmove(3, m)) { 529 if (!allowednextmove(m[1], m[2])) {
530 m[2] += 2; 530 m[2] += 2;
531 continue; 531 continue;
532 } 532 }
@@ -541,7 +541,7 @@ gendata_h48k2_dfs(h48k2_dfs_arg_t arg[static 1])
541 /* Depth d+4 */ 541 /* Depth d+4 */
542 for (m[3] = 0; m[3] < 18; m[3]++) { 542 for (m[3] = 0; m[3] < 18; m[3]++) {
543 markarg.depth = d+4; 543 markarg.depth = d+4;
544 if (!allowednextmove(4, m)) { 544 if (!allowednextmove(m[2], m[3])) {
545 m[3] += 2; 545 m[3] += 2;
546 continue; 546 continue;
547 } 547 }
diff --git a/src/solvers/h48/solve.h b/src/solvers/h48/solve.h
index dcec5a4..fc7631f 100644
--- a/src/solvers/h48/solve.h
+++ b/src/solvers/h48/solve.h
@@ -176,10 +176,16 @@ solve_h48_dfs(dfsarg_solve_h48_t arg[static 1])
176 ulbi = arg->use_lb_inverse; 176 ulbi = arg->use_lb_inverse;
177 177
178 ret = 0; 178 ret = 0;
179 mm_normal = allowednextmove_mask(arg->solution_moves->nmoves, 179 mm_normal = arg->movemask_normal;
180 arg->solution_moves->moves) & arg->movemask_normal; 180 if (arg->solution_moves->nmoves > 0) {
181 mm_inverse = allowednextmove_mask(arg->solution_moves->npremoves, 181 m = arg->solution_moves->moves[arg->solution_moves->nmoves-1];
182 arg->solution_moves->premoves) & arg->movemask_inverse; 182 mm_normal &= allowedmask[movebase(m)];
183 }
184 mm_inverse = arg->movemask_inverse;
185 if (arg->solution_moves->npremoves > 0) {
186 m = arg->solution_moves->premoves[arg->solution_moves->npremoves-1];
187 mm_inverse &= allowedmask[movebase(m)];
188 }
183 if (popcount_u32(mm_normal) <= popcount_u32(mm_inverse)) { 189 if (popcount_u32(mm_normal) <= popcount_u32(mm_inverse)) {
184 arg->solution_moves->nmoves++; 190 arg->solution_moves->nmoves++;
185 for (m = 0; m < 18; m++) { 191 for (m = 0; m < 18; m++) {
@@ -299,7 +305,12 @@ solve_h48_maketasks(
299 return NISSY_OK; 305 return NISSY_OK;
300 } 306 }
301 307
302 mm = allowednextmove_mask(maketasks_arg->nmoves, maketasks_arg->moves); 308 if (maketasks_arg->nmoves == 0) {
309 mm = MM_ALLMOVES;
310 } else {
311 m = maketasks_arg->moves[maketasks_arg->nmoves-1];
312 mm = allowedmask[movebase(m)];
313 }
303 314
304 maketasks_arg->nmoves++; 315 maketasks_arg->nmoves++;
305 backup_cube = maketasks_arg->cube; 316 backup_cube = maketasks_arg->cube;
diff --git a/src/solvers/solutions.h b/src/solvers/solutions.h
index bbff9cf..1b5517c 100644
--- a/src/solvers/solutions.h
+++ b/src/solvers/solutions.h
@@ -128,7 +128,7 @@ appendsolution(
128 128
129 /* 129 /*
130 This is a bit ugly: we have to sort now and then again 130 This is a bit ugly: we have to sort now and then again
131 later, because the allowednext check would fail with 131 later, because the allowedmoves check would fail with
132 improperly sorted parallel moves, but then transforming 132 improperly sorted parallel moves, but then transforming
133 could swap the pairs the wrong way around. 133 could swap the pairs the wrong way around.
134 TODO: maybe fix this 134 TODO: maybe fix this
diff --git a/src/utils/constants.h b/src/utils/constants.h
index a6c6b7a..ba7f4d4 100644
--- a/src/utils/constants.h
+++ b/src/utils/constants.h
@@ -102,7 +102,17 @@ STATIC int64_t binomial[12][12] = {
102 102
103#define MM_ALLMOVES UINT32_C(0x3FFFF) 103#define MM_ALLMOVES UINT32_C(0x3FFFF)
104#define MM_NOHALFTURNS UINT32_C(0x2DB6D) 104#define MM_NOHALFTURNS UINT32_C(0x2DB6D)
105#define MM_SIDE(m) (UINT32_C(7) << (uint32_t)(m)) 105#define MM_SINGLE(m) (UINT32_C(1) << (uint32_t)(m))
106#define MM_FACE(m) (UINT32_C(7) << (uint32_t)(m))
107#define MM_EO (\
108 MM_FACE(MOVE_U) | MM_FACE(MOVE_D) |\
109 MM_FACE(MOVE_R) | MM_FACE(MOVE_L) |\
110 MM_SINGLE(MOVE_F2) | MM_SINGLE(MOVE_B2))
111#define MM_DR (\
112 MM_FACE(MOVE_U) | MM_FACE(MOVE_D) |\
113 MM_SINGLE(MOVE_R2) | MM_SINGLE(MOVE_L2) |\
114 MM_SINGLE(MOVE_F2) | MM_SINGLE(MOVE_B2))
115#define MM_HTR (MM_ALLMOVES & ~MM_NOHALFTURNS)
106 116
107#define TM_ALLTRANS UINT64_C(0xFFFFFFFFFFFF) 117#define TM_ALLTRANS UINT64_C(0xFFFFFFFFFFFF)
108#define TM_SINGLE(t) (UINT64_C(1) << (uint64_t)(t)) 118#define TM_SINGLE(t) (UINT64_C(1) << (uint64_t)(t))
@@ -151,6 +161,15 @@ STATIC int64_t binomial[12][12] = {
151#define EFLIP UINT8_C(0x10) 161#define EFLIP UINT8_C(0x10)
152#define UINT8_ERROR UINT8_MAX 162#define UINT8_ERROR UINT8_MAX
153 163
164STATIC const uint32_t allowedmask[] = {
165 UINT32_C(0x3FFF8),
166 UINT32_C(0x3FFC0),
167 UINT32_C(0x3FE3F),
168 UINT32_C(0x3F03F),
169 UINT32_C(0x38FFF),
170 UINT32_C(0x00FFF)
171};
172
154STATIC const char *cornerstr[] = { 173STATIC const char *cornerstr[] = {
155 [CORNER_UFR] = "UFR", 174 [CORNER_UFR] = "UFR",
156 [CORNER_UBL] = "UBL", 175 [CORNER_UBL] = "UBL",
diff --git a/test/080_allowednext/00_empty_U.in b/test/080_allowedmoves/00_empty.in
index feb4bc3..573541a 100644
--- a/test/080_allowednext/00_empty_U.in
+++ b/test/080_allowedmoves/00_empty.in
@@ -1,2 +1 @@
10 0
2U
diff --git a/test/080_allowednext/00_empty_U.out b/test/080_allowedmoves/00_empty.out
index 27ba77d..27ba77d 100644
--- a/test/080_allowednext/00_empty_U.out
+++ b/test/080_allowedmoves/00_empty.out
diff --git a/test/080_allowedmoves/01_onemove.in b/test/080_allowedmoves/01_onemove.in
new file mode 100644
index 0000000..aac8dac
--- /dev/null
+++ b/test/080_allowedmoves/01_onemove.in
@@ -0,0 +1,2 @@
11
2B'
diff --git a/test/080_allowednext/02_U_F.out b/test/080_allowedmoves/01_onemove.out
index 27ba77d..27ba77d 100644
--- a/test/080_allowednext/02_U_F.out
+++ b/test/080_allowedmoves/01_onemove.out
diff --git a/test/080_allowednext/01_F_F2.in b/test/080_allowedmoves/02_F_F2.in
index 93f0a9c..93f0a9c 100644
--- a/test/080_allowednext/01_F_F2.in
+++ b/test/080_allowedmoves/02_F_F2.in
diff --git a/test/080_allowednext/01_F_F2.out b/test/080_allowedmoves/02_F_F2.out
index c508d53..c508d53 100644
--- a/test/080_allowednext/01_F_F2.out
+++ b/test/080_allowedmoves/02_F_F2.out
diff --git a/test/080_allowednext/02_U_F.in b/test/080_allowedmoves/03_U_F.in
index ad3607e..ad3607e 100644
--- a/test/080_allowednext/02_U_F.in
+++ b/test/080_allowedmoves/03_U_F.in
diff --git a/test/080_allowednext/04_longer_true.out b/test/080_allowedmoves/03_U_F.out
index 27ba77d..27ba77d 100644
--- a/test/080_allowednext/04_longer_true.out
+++ b/test/080_allowedmoves/03_U_F.out
diff --git a/test/080_allowednext/03_R_L_R.in b/test/080_allowedmoves/04_R_L_R.in
index ffd9542..ffd9542 100644
--- a/test/080_allowednext/03_R_L_R.in
+++ b/test/080_allowedmoves/04_R_L_R.in
diff --git a/test/080_allowednext/03_R_L_R.out b/test/080_allowedmoves/04_R_L_R.out
index c508d53..c508d53 100644
--- a/test/080_allowednext/03_R_L_R.out
+++ b/test/080_allowedmoves/04_R_L_R.out
diff --git a/test/080_allowednext/04_longer_true.in b/test/080_allowedmoves/05_longer_true.in
index 0360619..1d9999c 100644
--- a/test/080_allowednext/04_longer_true.in
+++ b/test/080_allowedmoves/05_longer_true.in
@@ -1,11 +1,11 @@
111 111
2F2 2F2
3B 3B
4D2
5D'
6L
7R2
8U 4U
5D'
6R
7L2
8F
9U' 9U'
10D 10D
11F 11F
diff --git a/test/080_allowedmoves/05_longer_true.out b/test/080_allowedmoves/05_longer_true.out
new file mode 100644
index 0000000..27ba77d
--- /dev/null
+++ b/test/080_allowedmoves/05_longer_true.out
@@ -0,0 +1 @@
true
diff --git a/test/080_allowednext/05_longer_false.in b/test/080_allowedmoves/06_longer_false.in
index 6cf8ecd..00fe94f 100644
--- a/test/080_allowednext/05_longer_false.in
+++ b/test/080_allowedmoves/06_longer_false.in
@@ -1,9 +1,12 @@
18 18
2D 2D
3B2
4F' 3F'
4B2
5L' 5L'
6F' 6F'
7L' 7R'
8R2 8L2
9L 9R
10B
11U2
12D
diff --git a/test/080_allowednext/05_longer_false.out b/test/080_allowedmoves/06_longer_false.out
index c508d53..c508d53 100644
--- a/test/080_allowednext/05_longer_false.out
+++ b/test/080_allowedmoves/06_longer_false.out
diff --git a/test/080_allowednext/06_D_U_false_order.in b/test/080_allowedmoves/07_D_U_false_order.in
index 680d1a1..680d1a1 100644
--- a/test/080_allowednext/06_D_U_false_order.in
+++ b/test/080_allowedmoves/07_D_U_false_order.in
diff --git a/test/080_allowednext/06_D_U_false_order.out b/test/080_allowedmoves/07_D_U_false_order.out
index c508d53..c508d53 100644
--- a/test/080_allowednext/06_D_U_false_order.out
+++ b/test/080_allowedmoves/07_D_U_false_order.out
diff --git a/test/080_allowednext/allowednext_tests.c b/test/080_allowedmoves/allowedmoves_tests.c
index 08fe491..aa6bda7 100644
--- a/test/080_allowednext/allowednext_tests.c
+++ b/test/080_allowedmoves/allowedmoves_tests.c
@@ -1,6 +1,6 @@
1#include "../test.h" 1#include "../test.h"
2 2
3bool allowednextmove(size_t n, const uint8_t [n]); 3bool allowedmoves(size_t n, const uint8_t [n]);
4 4
5static char *moves[] = { 5static char *moves[] = {
6 "U", "U2", "U'", 6 "U", "U2", "U'",
@@ -27,9 +27,5 @@ void run(void) {
27 m[i] = j; 27 m[i] = j;
28 } 28 }
29 29
30 fprintf(stderr, "Last two: %s, %s\nNext: %s\n", 30 printf("%s\n", allowedmoves(n, m) ? "true" : "false");
31 n > 2 ? moves[m[n-3]] : "-",
32 n > 1 ? moves[m[n-2]] : "-",
33 n > 0 ? moves[m[n-1]] : "-");
34 printf("%s\n", allowednextmove(n, m) ? "true" : "false");
35} 31}

Generated with cgit - Back to sebastiano.tronto.net