diff options
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | TODO.txt | 5 | ||||
| -rw-r--r-- | benchmark/00_gendata_h48/gendata_h48_benchmark.c | 42 | ||||
| -rw-r--r-- | benchmark/benchmark.h | 39 | ||||
| -rwxr-xr-x | benchmark/benchmark.sh | 27 | ||||
| -rw-r--r-- | old/benchmark/bench.c | 83 | ||||
| -rwxr-xr-x | old/benchmark/bench.sh | 21 | ||||
| -rw-r--r-- | old/benchmark/cube-bench.c | 147 | ||||
| -rw-r--r-- | old/manualsimd/cube.c | 888 | ||||
| -rw-r--r-- | old/manualsimd/cube.h | 27 | ||||
| -rw-r--r-- | src/solve_generic.h | 14 | ||||
| -rw-r--r-- | utils/cube_h_with_format_documentation (renamed from old/cube_h_with_format_documentation) | 0 |
13 files changed, 124 insertions, 1173 deletions
| @@ -1,7 +1,5 @@ | |||
| 1 | generated | ||
| 2 | config.mk | 1 | config.mk |
| 3 | benchmark/results | 2 | benchmark/results |
| 4 | old/benchmark/results | ||
| 5 | benchmark/run | 3 | benchmark/run |
| 6 | gen | 4 | gen |
| 7 | debuggen | 5 | debuggen |
| @@ -18,7 +18,7 @@ test: debugcube.o | |||
| 18 | CUBETYPE=${CUBETYPE} TEST=${TEST} ./test/test.sh | 18 | CUBETYPE=${CUBETYPE} TEST=${TEST} ./test/test.sh |
| 19 | 19 | ||
| 20 | benchmark: cube.o | 20 | benchmark: cube.o |
| 21 | CUBETYPE=${CUBETYPE} ./benchmark/bench.sh | 21 | CUBETYPE=${CUBETYPE} ./benchmark/benchmark.sh |
| 22 | 22 | ||
| 23 | gen: cube.o | 23 | gen: cube.o |
| 24 | ${CC} ${CFLAGS} -o gen cube.o tables/tables.c | 24 | ${CC} ${CFLAGS} -o gen cube.o tables/tables.c |
| @@ -1,4 +1,9 @@ | |||
| 1 | Fixes | ||
| 2 | - tables/tables.c: fix, use new nissy_gendata and nissy_datasize | ||
| 3 | |||
| 1 | Solver | 4 | Solver |
| 5 | - benchmark for solve | ||
| 6 | table generation, where to keep tables? in benchmark folder or in tables/? | ||
| 2 | - write a solver (how many tricks? some, but not all are needed) | 7 | - write a solver (how many tricks? some, but not all are needed) |
| 3 | 8 | ||
| 4 | More utilities for tables (in cube.h) | 9 | More utilities for tables (in cube.h) |
diff --git a/benchmark/00_gendata_h48/gendata_h48_benchmark.c b/benchmark/00_gendata_h48/gendata_h48_benchmark.c new file mode 100644 index 0000000..1dc7c57 --- /dev/null +++ b/benchmark/00_gendata_h48/gendata_h48_benchmark.c | |||
| @@ -0,0 +1,42 @@ | |||
| 1 | #include "../benchmark.h" | ||
| 2 | #include "../../src/cube.h" | ||
| 3 | |||
| 4 | #define MAXDEPTH 10 | ||
| 5 | #define HVALUE 2 | ||
| 6 | #define OPTIONS "2;10" | ||
| 7 | #define LONGOPTIONS "h = 2, max depth = 10" | ||
| 8 | |||
| 9 | char *buf; | ||
| 10 | |||
| 11 | void run(void) { | ||
| 12 | uint32_t *buf32; | ||
| 13 | int i; | ||
| 14 | int64_t s; | ||
| 15 | |||
| 16 | s = nissy_gendata("H48", OPTIONS, buf); | ||
| 17 | |||
| 18 | if (s == -1) { | ||
| 19 | printf("Error generating table\n"); | ||
| 20 | } else { | ||
| 21 | printf("Succesfully generated %" PRId64 " bytes. Table:\n", s); | ||
| 22 | buf32 = (uint32_t *)&buf[s-sizeof(uint32_t)*(MAXDEPTH+1)]; | ||
| 23 | for (i = 0; i <= MAXDEPTH; i++) | ||
| 24 | printf("%d:\t%" PRId32 "\n", i, buf32[i]); | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | int main() { | ||
| 29 | int64_t size; | ||
| 30 | |||
| 31 | size = nissy_datasize("H48", OPTIONS); | ||
| 32 | if (size == -1) { | ||
| 33 | printf("gendata_h48 benchmark: error in datasize\n"); | ||
| 34 | return 1; | ||
| 35 | } | ||
| 36 | |||
| 37 | buf = malloc(size); | ||
| 38 | |||
| 39 | time_benchmark(run, "gendata_h48 " LONGOPTIONS); | ||
| 40 | |||
| 41 | return 0; | ||
| 42 | } | ||
diff --git a/benchmark/benchmark.h b/benchmark/benchmark.h new file mode 100644 index 0000000..e9361a8 --- /dev/null +++ b/benchmark/benchmark.h | |||
| @@ -0,0 +1,39 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <inttypes.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | #include <time.h> | ||
| 6 | |||
| 7 | double | ||
| 8 | time_benchmark(void (*run)(void), char *name) | ||
| 9 | { | ||
| 10 | struct timespec start, end; | ||
| 11 | double tdiff, tdsec, tdnano; | ||
| 12 | |||
| 13 | printf("\n"); | ||
| 14 | fflush(stdout); | ||
| 15 | |||
| 16 | if (run == NULL) { | ||
| 17 | printf("> %s: nothing to run!\n", name); | ||
| 18 | fflush(stdout); | ||
| 19 | return -1.0; | ||
| 20 | } | ||
| 21 | |||
| 22 | printf("Benchmark: %s\n\n", name); | ||
| 23 | printf("==========\n"); | ||
| 24 | fflush(stdout); | ||
| 25 | |||
| 26 | clock_gettime(CLOCK_MONOTONIC, &start); | ||
| 27 | run(); | ||
| 28 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
| 29 | |||
| 30 | tdsec = end.tv_sec - start.tv_sec; | ||
| 31 | tdnano = end.tv_nsec - start.tv_nsec; | ||
| 32 | tdiff = tdsec + 1e-9 * tdnano; | ||
| 33 | |||
| 34 | printf("==========\n"); | ||
| 35 | printf("\nTotal time: %.4fs\n", tdiff); | ||
| 36 | fflush(stdout); | ||
| 37 | |||
| 38 | return tdiff; | ||
| 39 | } | ||
diff --git a/benchmark/benchmark.sh b/benchmark/benchmark.sh new file mode 100755 index 0000000..6de280c --- /dev/null +++ b/benchmark/benchmark.sh | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | re="${RUN:-$@}" | ||
| 4 | |||
| 5 | CC="cc -std=c99 -pedantic -Wall -Wextra \ | ||
| 6 | -Wno-unused-parameter -Wno-unused-function -O3 -D$CUBETYPE \ | ||
| 7 | -D_POSIX_C_SOURCE=199309L" | ||
| 8 | |||
| 9 | [ "$CUBETYPE" = "CUBE_AVX2" ] && CC="$CC -mavx2" | ||
| 10 | |||
| 11 | BIN="benchmark/run" | ||
| 12 | RES="benchmark/results" | ||
| 13 | CUBEOBJ="cube.o" | ||
| 14 | d="$(date +'%Y-%m-%d-%H-%M-%S')" | ||
| 15 | |||
| 16 | mkdir -p "$RES" | ||
| 17 | |||
| 18 | for t in benchmark/*; do | ||
| 19 | if [ -n "$re" ] && [ -z "$(echo "$t" | grep "$re")" ]; then | ||
| 20 | continue | ||
| 21 | fi | ||
| 22 | if [ ! -d "$t" ] || [ "$t" = "benchmark/results" ]; then continue; fi | ||
| 23 | $CC -o $BIN $t/*.c $CUBEOBJ || exit 1; | ||
| 24 | $BIN | tee "$RES/results-$d.txt" "$RES/results-last.txt" | ||
| 25 | done | ||
| 26 | |||
| 27 | rm -rf $BIN $CUBEOBJ | ||
diff --git a/old/benchmark/bench.c b/old/benchmark/bench.c deleted file mode 100644 index 2c3358f..0000000 --- a/old/benchmark/bench.c +++ /dev/null | |||
| @@ -1,83 +0,0 @@ | |||
| 1 | #include <stdbool.h> | ||
| 2 | #include <stdint.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdlib.h> | ||
| 5 | #include <time.h> | ||
| 6 | |||
| 7 | #include "../cube.h" | ||
| 8 | |||
| 9 | #define MOVES 100000000 | ||
| 10 | #define TRANS 100000000 | ||
| 11 | #define COMPOSE 100000000 | ||
| 12 | #define INVERSE 100000000 | ||
| 13 | |||
| 14 | double | ||
| 15 | bench(cube_t (*run)(int64_t), int64_t n, char *name) | ||
| 16 | { | ||
| 17 | char str[1000]; | ||
| 18 | cube_t cube; | ||
| 19 | struct timespec start, end; | ||
| 20 | double tdiff, tdsec, tdnano; | ||
| 21 | |||
| 22 | printf("\n"); | ||
| 23 | fflush(stdout); | ||
| 24 | |||
| 25 | if (run == NULL) { | ||
| 26 | printf("> %s: nothing to run!\n", name); | ||
| 27 | fflush(stdout); | ||
| 28 | return -1.0; | ||
| 29 | } | ||
| 30 | |||
| 31 | printf("> %s: running benchmark...\n", name); | ||
| 32 | fflush(stdout); | ||
| 33 | clock_gettime(CLOCK_MONOTONIC, &start); | ||
| 34 | |||
| 35 | cube = run(n); | ||
| 36 | writecube("H48", cube, str); | ||
| 37 | str[3] = 0; | ||
| 38 | printf("> %s: resulting cube, first piece: %s\n", name, str); | ||
| 39 | fflush(stdout); | ||
| 40 | |||
| 41 | clock_gettime(CLOCK_MONOTONIC, &end); | ||
| 42 | tdsec = end.tv_sec - start.tv_sec; | ||
| 43 | tdnano = end.tv_nsec - start.tv_nsec; | ||
| 44 | tdiff = tdsec + 1e-9 * tdnano; | ||
| 45 | printf("> %s: %.4fs\n", name, tdiff); | ||
| 46 | fflush(stdout); | ||
| 47 | |||
| 48 | return tdiff; | ||
| 49 | } | ||
| 50 | |||
| 51 | int main() { | ||
| 52 | double tmoves, ttrans, tcompose, tinverse; | ||
| 53 | |||
| 54 | printf( | ||
| 55 | "Benchmarks settings:\n" | ||
| 56 | "MOVES:\t%d\nTRANS:\t%d\nCOMPOSE:\t%d\nINVERSE:\t%d\n", | ||
| 57 | MOVES, TRANS, COMPOSE, INVERSE | ||
| 58 | ); | ||
| 59 | fflush(stdout); | ||
| 60 | |||
| 61 | srand(time(NULL)); | ||
| 62 | |||
| 63 | tmoves = bench(run_moves, MOVES, "moves"); | ||
| 64 | ttrans = bench(run_trans, TRANS, "trans"); | ||
| 65 | tcompose = bench(run_compose, COMPOSE, "compose"); | ||
| 66 | tinverse = bench(run_inverse, INVERSE, "inverse"); | ||
| 67 | |||
| 68 | printf( | ||
| 69 | "\nBenchmark summary:\n" | ||
| 70 | "moves: %d moves in %.4fs (%.4f MTPS)\n" | ||
| 71 | "trans: %d transformations in %.4fs (%.4f MTPS)\n" | ||
| 72 | "compose: %d compositions in %.4fs (%.4f MCPS)\n" | ||
| 73 | "inverse: %d inverses in %.4fs (%.4f MIPS)\n" | ||
| 74 | "Total time: %.4f\n", | ||
| 75 | MOVES, tmoves, MOVES / (1e6 * tmoves), | ||
| 76 | TRANS, ttrans, TRANS / (1e6 * ttrans), | ||
| 77 | COMPOSE, tcompose, COMPOSE / (1e6 * tcompose), | ||
| 78 | INVERSE, tinverse, INVERSE / (1e6 * tinverse), | ||
| 79 | tmoves + ttrans + tcompose + tinverse | ||
| 80 | ); | ||
| 81 | |||
| 82 | return 0; | ||
| 83 | } | ||
diff --git a/old/benchmark/bench.sh b/old/benchmark/bench.sh deleted file mode 100755 index cc31c1e..0000000 --- a/old/benchmark/bench.sh +++ /dev/null | |||
| @@ -1,21 +0,0 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | |||
| 3 | CC="cc -std=c99 -O3 -D$CUBETYPE" | ||
| 4 | if [ "$CUBETYPE" = "CUBE_AVX2" ]; then | ||
| 5 | CC="$CC -mavx2" | ||
| 6 | fi | ||
| 7 | |||
| 8 | BENCHBIN="benchmark/run" | ||
| 9 | BENCHDIR="benchmark/results" | ||
| 10 | CUBEOBJ="cube.o" | ||
| 11 | |||
| 12 | $CC -D_POSIX_C_SOURCE=199309L -o $BENCHBIN benchmark/bench.c $CUBEOBJ || exit 1 | ||
| 13 | |||
| 14 | d="$(date +'%Y-%m-%d-%H-%M-%S')" | ||
| 15 | mkdir -p "$BENCHDIR" | ||
| 16 | $BENCHBIN | tee "$BENCHDIR/results-$d.txt" "$BENCHDIR/results.txt" | ||
| 17 | |||
| 18 | echo "" | ||
| 19 | echo "Results saved to $BENCHDIR/results.txt" | ||
| 20 | |||
| 21 | rm -rf $BENCHBIN $CUBEOBJ | ||
diff --git a/old/benchmark/cube-bench.c b/old/benchmark/cube-bench.c deleted file mode 100644 index 26d12a4..0000000 --- a/old/benchmark/cube-bench.c +++ /dev/null | |||
| @@ -1,147 +0,0 @@ | |||
| 1 | /****************************************************************************** | ||
| 2 | Section: benchmarks | ||
| 3 | |||
| 4 | Here you can find some simple functions that can be used to benchmark the | ||
| 5 | rest of the code. | ||
| 6 | ******************************************************************************/ | ||
| 7 | |||
| 8 | #define RANDOMCUBES 157 | ||
| 9 | |||
| 10 | static void | ||
| 11 | setup_randomcubes(cube_fast_t *cubes) | ||
| 12 | { | ||
| 13 | int i; | ||
| 14 | |||
| 15 | for (i = 0; i < RANDOMCUBES; i++) | ||
| 16 | cubes[i] = run_moves(i*4); | ||
| 17 | } | ||
| 18 | |||
| 19 | cube_t | ||
| 20 | run_moves(int64_t n) | ||
| 21 | { | ||
| 22 | cube_fast_t fast; | ||
| 23 | int64_t m, i; | ||
| 24 | |||
| 25 | fast = solvedcube(); | ||
| 26 | m = n / 18; | ||
| 27 | |||
| 28 | for (i = 0; i < m; i++) { | ||
| 29 | fast = _move_U(fast); | ||
| 30 | fast = _move_U2(fast); | ||
| 31 | fast = _move_U3(fast); | ||
| 32 | fast = _move_D(fast); | ||
| 33 | fast = _move_D2(fast); | ||
| 34 | fast = _move_D3(fast); | ||
| 35 | fast = _move_R(fast); | ||
| 36 | fast = _move_R2(fast); | ||
| 37 | fast = _move_R3(fast); | ||
| 38 | fast = _move_L(fast); | ||
| 39 | fast = _move_L2(fast); | ||
| 40 | fast = _move_L3(fast); | ||
| 41 | fast = _move_F(fast); | ||
| 42 | fast = _move_F2(fast); | ||
| 43 | fast = _move_F3(fast); | ||
| 44 | fast = _move_B(fast); | ||
| 45 | fast = _move_B2(fast); | ||
| 46 | fast = _move_B3(fast); | ||
| 47 | } | ||
| 48 | |||
| 49 | for (i = m * 18; i < n; i++) | ||
| 50 | fast = _move_F(fast); | ||
| 51 | |||
| 52 | return fast; | ||
| 53 | } | ||
| 54 | |||
| 55 | cube_t | ||
| 56 | run_trans(int64_t n) | ||
| 57 | { | ||
| 58 | cube_fast_t fast; | ||
| 59 | int64_t m, i; | ||
| 60 | |||
| 61 | fast = run_moves(33); | ||
| 62 | m = n / 18; | ||
| 63 | |||
| 64 | for (i = 0; i < m; i++) { | ||
| 65 | fast = _trans_UFr(fast); | ||
| 66 | fast = _trans_ULr(fast); | ||
| 67 | fast = _trans_UBr(fast); | ||
| 68 | fast = _trans_URr(fast); | ||
| 69 | fast = _trans_DFr(fast); | ||
| 70 | fast = _trans_DLr(fast); | ||
| 71 | fast = _trans_DBr(fast); | ||
| 72 | fast = _trans_DRr(fast); | ||
| 73 | fast = _trans_RUr(fast); | ||
| 74 | fast = _trans_RFr(fast); | ||
| 75 | fast = _trans_RDr(fast); | ||
| 76 | fast = _trans_RBr(fast); | ||
| 77 | fast = _trans_LUr(fast); | ||
| 78 | fast = _trans_LFr(fast); | ||
| 79 | fast = _trans_LDr(fast); | ||
| 80 | fast = _trans_LBr(fast); | ||
| 81 | fast = _trans_FUr(fast); | ||
| 82 | fast = _trans_FRr(fast); | ||
| 83 | fast = _trans_FDr(fast); | ||
| 84 | fast = _trans_FLr(fast); | ||
| 85 | fast = _trans_BUr(fast); | ||
| 86 | fast = _trans_BRr(fast); | ||
| 87 | fast = _trans_BDr(fast); | ||
| 88 | fast = _trans_BLr(fast); | ||
| 89 | fast = _trans_UFm(fast); | ||
| 90 | fast = _trans_ULm(fast); | ||
| 91 | fast = _trans_UBm(fast); | ||
| 92 | fast = _trans_URm(fast); | ||
| 93 | fast = _trans_DFm(fast); | ||
| 94 | fast = _trans_DLm(fast); | ||
| 95 | fast = _trans_DBm(fast); | ||
| 96 | fast = _trans_DRm(fast); | ||
| 97 | fast = _trans_RUm(fast); | ||
| 98 | fast = _trans_RFm(fast); | ||
| 99 | fast = _trans_RDm(fast); | ||
| 100 | fast = _trans_RBm(fast); | ||
| 101 | fast = _trans_LUm(fast); | ||
| 102 | fast = _trans_LFm(fast); | ||
| 103 | fast = _trans_LDm(fast); | ||
| 104 | fast = _trans_LBm(fast); | ||
| 105 | fast = _trans_FUm(fast); | ||
| 106 | fast = _trans_FRm(fast); | ||
| 107 | fast = _trans_FDm(fast); | ||
| 108 | fast = _trans_FLm(fast); | ||
| 109 | fast = _trans_BUm(fast); | ||
| 110 | fast = _trans_BRm(fast); | ||
| 111 | fast = _trans_BDm(fast); | ||
| 112 | fast = _trans_BLm(fast); | ||
| 113 | } | ||
| 114 | |||
| 115 | for (i = m * 18; i < n; i++) | ||
| 116 | fast = _trans_FRm(fast); | ||
| 117 | |||
| 118 | return fast; | ||
| 119 | } | ||
| 120 | |||
| 121 | cube_t | ||
| 122 | run_compose(int64_t n) | ||
| 123 | { | ||
| 124 | cube_fast_t fast, cubes[RANDOMCUBES]; | ||
| 125 | int64_t i; | ||
| 126 | |||
| 127 | setup_randomcubes(cubes); | ||
| 128 | |||
| 129 | for (i = 0; i < n; i++) | ||
| 130 | fast = compose_fast(fast, cubes[i % RANDOMCUBES]); | ||
| 131 | |||
| 132 | return fast; | ||
| 133 | } | ||
| 134 | |||
| 135 | cube_t | ||
| 136 | run_inverse(int64_t n) | ||
| 137 | { | ||
| 138 | cube_fast_t fast, cubes[RANDOMCUBES]; | ||
| 139 | int64_t i; | ||
| 140 | |||
| 141 | setup_randomcubes(cubes); | ||
| 142 | |||
| 143 | for (i = 0; i < n; i++) | ||
| 144 | fast = inverse_fast(cubes[i % RANDOMCUBES]); | ||
| 145 | |||
| 146 | return fast; | ||
| 147 | } | ||
diff --git a/old/manualsimd/cube.c b/old/manualsimd/cube.c deleted file mode 100644 index 4372b5e..0000000 --- a/old/manualsimd/cube.c +++ /dev/null | |||
| @@ -1,888 +0,0 @@ | |||
| 1 | /* | ||
| 2 | # Cube representation, moves, transformations and (tentatively) indexing | ||
| 3 | |||
| 4 | ## Textual description | ||
| 5 | |||
| 6 | The functions readcube() and writecube() use the following format. | ||
| 7 | Each edge is represented by two letters denoting the sides it belongs to | ||
| 8 | and one number denoting its orientation (0 oriented, 1 mis-oriented). | ||
| 9 | Similarly, each corner is represented by three letters and a number | ||
| 10 | (0 oriented, 1 twisted clockwise, 2 twisted counter-clockwise). | ||
| 11 | Edge orientation is relative to the F / B axis, corner orientation is | ||
| 12 | relative to the U / D axis. | ||
| 13 | |||
| 14 | The correct order of the pieces is the same as that defined in the | ||
| 15 | section "Internal cube representation", except that pieces are read | ||
| 16 | left-to-right. Pieces are divided by slices, so the ordering is not the | ||
| 17 | most intuitive, but it is more convenient for the internal representation. | ||
| 18 | |||
| 19 | Whitespaces between pieces are ignored when reading the cube, and a | ||
| 20 | single whitespace character is added between pieces when writing. | ||
| 21 | |||
| 22 | For example, the solved cube looks like this: | ||
| 23 | |||
| 24 | UF0 UB0 DB0 DF0 UR0 UL0 DL0 DR0 FR0 FL0 BL0 BR0 \ (no newline) | ||
| 25 | UFR0 UBL0 DFL0 DBR0 UFL0 UBR0 DFR0 DBL0 | ||
| 26 | |||
| 27 | The cube after the moves R'U'F looks like this: | ||
| 28 | |||
| 29 | FL1 BR0 DB0 UR1 UF0 UB0 DL0 FR0 UL1 DF1 BL0 DR0 \ (no newline) | ||
| 30 | UBL1 DBR1 UFR2 DFR2 DFL2 UBL2 UFL2 DBL0 | ||
| 31 | |||
| 32 | More formats might be supported in the future. | ||
| 33 | |||
| 34 | ## Internal cube representation | ||
| 35 | |||
| 36 | The cube_t data structure implemented in this file is designed to | ||
| 37 | efficiently perform common operations on a 3x3x3 Rubik's cube when | ||
| 38 | solving it with an iterative-deepening DFS search. It is not the most | ||
| 39 | general, complete, easy to read or compact one. Since the cube can | ||
| 40 | be trivially reoriented before the search, we only encode permutations | ||
| 41 | of the cube that keep the center pieces in a fixed position (that is, | ||
| 42 | we do not encode the position of the centers). | ||
| 43 | |||
| 44 | The cube state is encoded in two 64-bit integers, one for edges and one | ||
| 45 | for centers. We explain how edges are encoded first, and the highlight | ||
| 46 | the few differences with corners afterwards. | ||
| 47 | |||
| 48 | For encoding edges, only the 60 least-significant bits are used. Each | ||
| 49 | edge described by 5 bits. The position of a 5-bit block in the 64-bit | ||
| 50 | integer determine the position of the edge piece in the cube, according | ||
| 51 | to the following table (least-significant bits on the right): | ||
| 52 | |||
| 53 | 55-59 50-54 45-49 40-44 35-39 30-34 25-29 20-24 15-19 10-14 5-9 0-4 | ||
| 54 | BR BL FL FR DR DL UL UR DF DB UB UF | ||
| 55 | ossee ossee ossee ossee ossee ossee ossee ossee ossee ossee ossee ossee | ||
| 56 | |||
| 57 | For each edge, the 4 least-significant bits ('ssee' in the table) | ||
| 58 | determine the piece. The two bits marked with 'ss' determine the internal | ||
| 59 | slice the piece belongs to, i.e. they are either '00' for M, '01' for | ||
| 60 | S or '10' for E. The other two bits (marked with 'ee') determine the | ||
| 61 | actual edge piece among the 4 in the same slice, and they are assigned | ||
| 62 | somewhat arbitarily. Using this representation and the ordering defined | ||
| 63 | in the table above, the edges are correctly permuted when these 4 bits | ||
| 64 | for each represent the numbers 0 to 11 in the correct order. | ||
| 65 | |||
| 66 | The last bit determines the orientation. The orientation of an edge | ||
| 67 | depends on its position, and it is defined being 0 if the edge can be | ||
| 68 | moved to its place in the solved orientation by permutations in the | ||
| 69 | subgroup <U, D, R, L, F2, B2>. | ||
| 70 | |||
| 71 | Corners are encoded in the 48 least-significant bits, and are described | ||
| 72 | by 6 bits each, their position being defined by the following table: | ||
| 73 | |||
| 74 | 35-39 30-34 25-29 20-24 15-19 10-14 5-9 0-4 | ||
| 75 | DBL DFL UBR UFL DBR DFL UBL UFR | ||
| 76 | oooxcc oooxcc oooxcc oooxcc oooxcc oooxcc oooxcc oooxcc | ||
| 77 | |||
| 78 | The bit marked with an 'x' describes the axis the corner belongs to. | ||
| 79 | The 0 axis consists of the corners UFR, UBL, DFL and DBR, and the other | ||
| 80 | four corners form the axis marked with 1. Then two bits are needed to | ||
| 81 | identify the corner among the four of the same axis. The last three bits | ||
| 82 | determine the orientation, where one corner is defined to be oriented | ||
| 83 | (marked with '000') if its top or bottom sticker faces the top or bottom | ||
| 84 | side. A corner a clockwise turn away from being oriented, thus requiring | ||
| 85 | a counter-clockwise turn to be oriented correctly, is marked with '001', | ||
| 86 | and a corner a counter-clockwise turn away is marked with '010'. The most | ||
| 87 | significant bit is not used to determine the corner orientation, but it | ||
| 88 | must always be set to '0' to simplify the moving operations (see below). | ||
| 89 | |||
| 90 | ## Basic moves | ||
| 91 | |||
| 92 | The 18 basic moves of the cube could be performed by applying a suitable | ||
| 93 | general permutation (see below), but they have instead been manually | ||
| 94 | implemented with a few simple operations each, to improve performance. | ||
| 95 | |||
| 96 | For each move we first permute the pieces. This amounts to shifting | ||
| 97 | around 4 blocks of bits for edges and 4 for corners. Since in some cases | ||
| 98 | adjacent pieces on the cube are also adjacent in the bit representation we | ||
| 99 | use, we can save some operations by shifting multiple blocks together. | ||
| 100 | |||
| 101 | There are some moves that change the orientation of the pieces. Namely, | ||
| 102 | the moves F, F', B and B' change the orientation of the edges and those | ||
| 103 | moves as well as R, R', L and L' change the orientation of the corners. | ||
| 104 | Edge orientation is easy to address: we simply xor the edge representation | ||
| 105 | by a bit mask with zeroes everywhere except for the 4 edges that need | ||
| 106 | to be flipped (i.e. the ones on the twisted face). | ||
| 107 | |||
| 108 | Corner orientation is harder to reproduce efficiently working only with | ||
| 109 | bitwise operations, as it involves performing operations modulo 3. | ||
| 110 | However, with the help of the extra bit we reserved, we are able to | ||
| 111 | do this using only two additions and 3 bitwise operations, without | ||
| 112 | any multiplication, division or modulo operation. The trick is | ||
| 113 | to use the following formula to sum two numbers x, y in {0,1,2}: | ||
| 114 | |||
| 115 | ((x+y) + (x+y+1)/4) % 4 | ||
| 116 | |||
| 117 | The thrid bit is needed because x+y and x+y+1 can exceed 3. We can | ||
| 118 | apply this operation to multiple pairs of bits representing ternary | ||
| 119 | digits at once using binary operations. See the function coapply() | ||
| 120 | below for the details. | ||
| 121 | |||
| 122 | ## Inverting the cube | ||
| 123 | |||
| 124 | TODO | ||
| 125 | |||
| 126 | ## Transformations (conjugations by full-cube rotations) | ||
| 127 | |||
| 128 | TODO | ||
| 129 | |||
| 130 | ## Indexing (tentative) | ||
| 131 | |||
| 132 | TODO - subgroup description etc | ||
| 133 | |||
| 134 | */ | ||
| 135 | |||
| 136 | #include <stdbool.h> | ||
| 137 | #include <stdint.h> | ||
| 138 | #include <string.h> | ||
| 139 | |||
| 140 | #ifdef DEBUG | ||
| 141 | #include <stdio.h> | ||
| 142 | #endif | ||
| 143 | |||
| 144 | #include "cube.h" | ||
| 145 | |||
| 146 | #define _error 0xFFFFFFFF | ||
| 147 | |||
| 148 | #define _eoblock 0x10ULL /* 10000 */ | ||
| 149 | #define _epblock 0x0FULL /* 01111 */ | ||
| 150 | #define _eblock 0x1FULL /* 11111 */ | ||
| 151 | #define _eblock2 0x3FFULL /* 1111111111 */ | ||
| 152 | |||
| 153 | #define _coblock 0x18ULL /* 011000 */ | ||
| 154 | #define _cpblock 0x07ULL /* 000111 */ | ||
| 155 | #define _cblock 0x3FULL /* 111111 */ | ||
| 156 | #define _cblock2 0xFFFULL /* 111111111111 */ | ||
| 157 | |||
| 158 | #define _eomask 0x842108421084210ULL /* 10000 repeated 12 times */ | ||
| 159 | #define _comask 0x618618618618ULL /* 011000 repeated 8 times */ | ||
| 160 | #define _coonemask 0x208208208208ULL /* 001000 repeated 8 times */ | ||
| 161 | #define _coextramask 0x820820820820ULL /* 100000 repeated 8 times */ | ||
| 162 | |||
| 163 | #define _emask_u (_eblock2 | _eblock2 << 20ULL) | ||
| 164 | #define _emask_d (_eblock2 << 10ULL | _eblock2 << 30ULL) | ||
| 165 | #define _emask_r (_eblock << 20ULL | _eblock2 << 35ULL | _eblock << 55ULL) | ||
| 166 | #define _emask_l (_eblock2 << 45ULL | _eblock2 << 25ULL) | ||
| 167 | #define _emask_f (_eblock | _eblock << 15ULL | _eblock2 << 40ULL) | ||
| 168 | #define _emask_b (_eblock2 << 5ULL | _eblock2 << 50ULL) | ||
| 169 | |||
| 170 | #define _cmask_u (_cblock2 | _cblock2 << 24ULL) | ||
| 171 | #define _cmask_d (_cblock2 << 12ULL | _cblock2 << 36ULL) | ||
| 172 | #define _cmask_r (_cblock | _cblock << 18ULL | _cblock2 << 30ULL) | ||
| 173 | #define _cmask_l (_cblock2 << 6ULL | _cblock << 24ULL | _cblock << 42ULL) | ||
| 174 | #define _cmask_f (_cblock | _cblock << 12ULL | _cblock << 24ULL | _cblock << 36ULL) | ||
| 175 | #define _cmask_b (_cblock << 6ULL | _cblock << 18ULL | _cblock << 30ULL | _cblock << 42ULL) | ||
| 176 | |||
| 177 | #define _eomask_f (1ULL << 4ULL | 1ULL << 19ULL | 1ULL << 44ULL | 1ULL << 49ULL) | ||
| 178 | #define _eomask_b (1ULL << 9ULL | 1ULL << 14ULL | 1ULL << 54ULL | 1ULL << 59ULL) | ||
| 179 | |||
| 180 | #define _comask_r (2ULL << 3ULL | 1ULL << 33ULL | 2ULL << 21ULL | 1ULL << 39ULL) | ||
| 181 | #define _comask_l (1ULL << 27ULL | 2ULL << 9ULL | 2ULL << 15ULL | 1ULL << 45ULL) | ||
| 182 | #define _comask_f (1ULL << 3ULL | 1ULL << 15ULL | 2ULL << 27ULL | 2ULL << 39ULL) | ||
| 183 | #define _comask_b (1ULL << 9ULL | 1ULL << 21ULL | 2ULL << 33ULL | 2ULL << 45ULL) | ||
| 184 | |||
| 185 | static inline uint64_t coapply(uint64_t, uint64_t); | ||
| 186 | static uint64_t permsign(uint64_t *, int); | ||
| 187 | static uint64_t readep(char *); | ||
| 188 | static uint64_t readeo(char *); | ||
| 189 | static uint64_t readcp(char *); | ||
| 190 | static uint64_t readco(char *); | ||
| 191 | static uint64_t readmove(char); | ||
| 192 | static uint64_t readmodifier(char); | ||
| 193 | |||
| 194 | static char *edgestr[] = { | ||
| 195 | "UF", "UB", "DB", "DF", | ||
| 196 | "UR", "UL", "DL", "DR", | ||
| 197 | "FR", "FL", "BL", "BR" | ||
| 198 | }; | ||
| 199 | static char *cornerstr[] = { | ||
| 200 | "UFR", "UBL", "DFL", "DBR", | ||
| 201 | "UFL", "UBR", "DFR", "DBL" | ||
| 202 | }; | ||
| 203 | static char *cornerstralt[] = { | ||
| 204 | "URF", "ULB", "DLF", "DRB", | ||
| 205 | "ULF", "URB", "DRF", "DLB" | ||
| 206 | }; | ||
| 207 | static char *movestr[] = { | ||
| 208 | [U] = "U", [U2] = "U2", [U3] = "U'", | ||
| 209 | [D] = "D", [D2] = "D2", [D3] = "D'", | ||
| 210 | [R] = "R", [R2] = "R2", [R3] = "R'", | ||
| 211 | [L] = "L", [L2] = "L2", [L3] = "L'", | ||
| 212 | [F] = "F", [F2] = "F2", [F3] = "F'", | ||
| 213 | [B] = "B", [B2] = "B2", [B3] = "B'", | ||
| 214 | }; | ||
| 215 | |||
| 216 | cube_t solvedcube = { .e = 0x5A928398A418820ULL, .c = 0x1C61440C2040ULL }; | ||
| 217 | cube_t errorcube = { .e = _error, .c = _error }; | ||
| 218 | |||
| 219 | |||
| 220 | static uint64_t | ||
| 221 | permsign(uint64_t *a, int n) | ||
| 222 | { | ||
| 223 | int i, j; | ||
| 224 | uint64_t ret; | ||
| 225 | |||
| 226 | ret = 0; | ||
| 227 | |||
| 228 | for (i = 0; i < n; i++) | ||
| 229 | for (j = i+1; j < n; j++) | ||
| 230 | ret += a[i] > a[j] ? 1 : 0; | ||
| 231 | |||
| 232 | return ret % 2; | ||
| 233 | } | ||
| 234 | |||
| 235 | bool | ||
| 236 | isconsistent(cube_t cube) | ||
| 237 | { | ||
| 238 | uint64_t x, p[12], sum, co; | ||
| 239 | bool found[12]; | ||
| 240 | int i; | ||
| 241 | |||
| 242 | sum = 0; | ||
| 243 | |||
| 244 | /* Check for EP consistency */ | ||
| 245 | for (i = 0; i < 12; i++) | ||
| 246 | found[i] = false; | ||
| 247 | for (i = 0, x = cube.e; i < 12; i++, x >>= 5ULL) { | ||
| 248 | p[i] = x & _epblock; | ||
| 249 | if (p[i] >= 12) | ||
| 250 | goto inconsistent_ep; | ||
| 251 | found[p[i]] = true; | ||
| 252 | } | ||
| 253 | for (i = 0; i < 12; i++) | ||
| 254 | if (!found[i]) | ||
| 255 | goto inconsistent_ep; | ||
| 256 | sum = permsign(p, 12); | ||
| 257 | |||
| 258 | /* Check for CP consistency */ | ||
| 259 | for (i = 0; i < 8; i++) | ||
| 260 | found[i] = false; | ||
| 261 | for (i = 0, x = cube.c; i < 8; i++, x >>= 6ULL) { | ||
| 262 | p[i] = x & _cpblock; | ||
| 263 | if (p[i] >= 8) | ||
| 264 | goto inconsistent_cp; | ||
| 265 | found[p[i]] = true; | ||
| 266 | } | ||
| 267 | for (i = 0; i < 8; i++) | ||
| 268 | if (!found[i]) | ||
| 269 | goto inconsistent_cp; | ||
| 270 | sum += permsign(p, 8); | ||
| 271 | |||
| 272 | /* Check permutation parity */ | ||
| 273 | if (sum % 2 != 0) | ||
| 274 | goto inconsistent_parity; | ||
| 275 | |||
| 276 | /* Check for EO parity */ | ||
| 277 | for (i = 0, sum = 0, x = cube.e; i < 12; i++, x >>= 5ULL) | ||
| 278 | sum += (x & _eoblock) >> 4ULL; | ||
| 279 | if (sum % 2 != 0) | ||
| 280 | goto inconsistent_eo; | ||
| 281 | |||
| 282 | /* Check for CO parity */ | ||
| 283 | for (i = 0, sum = 0, x = cube.c; i < 8; i++, x >>= 6ULL) { | ||
| 284 | co = (x & _coblock) >> 3ULL; | ||
| 285 | if (co > 2) | ||
| 286 | goto inconsistent_co3; | ||
| 287 | sum += co; | ||
| 288 | } | ||
| 289 | if (sum % 3 != 0) | ||
| 290 | goto inconsistent_co; | ||
| 291 | |||
| 292 | /* Check that CO extra bit is zero */ | ||
| 293 | if (cube.c & _coextramask) | ||
| 294 | goto inconsistent_coextra; | ||
| 295 | |||
| 296 | return true; | ||
| 297 | |||
| 298 | inconsistent_ep: | ||
| 299 | #ifdef DEBUG | ||
| 300 | fprintf(stderr, "Inconsistent EP\n"); | ||
| 301 | #endif | ||
| 302 | goto inconsistent_return; | ||
| 303 | inconsistent_cp: | ||
| 304 | #ifdef DEBUG | ||
| 305 | fprintf(stderr, "Inconsistent CP\n"); | ||
| 306 | #endif | ||
| 307 | goto inconsistent_return; | ||
| 308 | inconsistent_parity: | ||
| 309 | #ifdef DEBUG | ||
| 310 | fprintf(stderr, "Inconsistent parity\n"); | ||
| 311 | #endif | ||
| 312 | goto inconsistent_return; | ||
| 313 | inconsistent_eo: | ||
| 314 | #ifdef DEBUG | ||
| 315 | fprintf(stderr, "Inconsistent EO\n"); | ||
| 316 | #endif | ||
| 317 | goto inconsistent_return; | ||
| 318 | inconsistent_co3: | ||
| 319 | #ifdef DEBUG | ||
| 320 | fprintf(stderr, "Inconsistent CO=3\n"); | ||
| 321 | #endif | ||
| 322 | goto inconsistent_return; | ||
| 323 | inconsistent_co: | ||
| 324 | #ifdef DEBUG | ||
| 325 | fprintf(stderr, "Inconsistent CO\n"); | ||
| 326 | #endif | ||
| 327 | goto inconsistent_return; | ||
| 328 | inconsistent_coextra: | ||
| 329 | #ifdef DEBUG | ||
| 330 | fprintf(stderr, "Inconsistent extra bit for CO\n"); | ||
| 331 | #endif | ||
| 332 | goto inconsistent_return; | ||
| 333 | inconsistent_return: | ||
| 334 | return false; | ||
| 335 | } | ||
| 336 | |||
| 337 | bool | ||
| 338 | issolved(cube_t cube) | ||
| 339 | { | ||
| 340 | return cube.c == solvedcube.c && cube.e == solvedcube.e; | ||
| 341 | } | ||
| 342 | |||
| 343 | |||
| 344 | static uint64_t | ||
| 345 | readep(char *str) | ||
| 346 | { | ||
| 347 | uint64_t e; | ||
| 348 | |||
| 349 | for (e = 0; e < 12; e++) | ||
| 350 | if (!strncmp(str, edgestr[e], 2)) | ||
| 351 | return e; | ||
| 352 | |||
| 353 | #ifdef DEBUG | ||
| 354 | fprintf(stderr, "Error reading EP\n"); | ||
| 355 | #endif | ||
| 356 | return _error; | ||
| 357 | } | ||
| 358 | |||
| 359 | static uint64_t | ||
| 360 | readeo(char *str) | ||
| 361 | { | ||
| 362 | if (*str == '0') | ||
| 363 | return 0ULL; | ||
| 364 | if (*str == '1') | ||
| 365 | return 1ULL; | ||
| 366 | |||
| 367 | #ifdef DEBUG | ||
| 368 | fprintf(stderr, "Error reading EO\n"); | ||
| 369 | #endif | ||
| 370 | return _error; | ||
| 371 | } | ||
| 372 | |||
| 373 | static uint64_t | ||
| 374 | readcp(char *str) | ||
| 375 | { | ||
| 376 | uint64_t c; | ||
| 377 | |||
| 378 | for (c = 0; c < 8; c++) | ||
| 379 | if (!strncmp(str, cornerstr[c], 3) || | ||
| 380 | !strncmp(str, cornerstralt[c], 3)) | ||
| 381 | return c; | ||
| 382 | |||
| 383 | #ifdef DEBUG | ||
| 384 | fprintf(stderr, "Error reading CP\n"); | ||
| 385 | #endif | ||
| 386 | return _error; | ||
| 387 | } | ||
| 388 | |||
| 389 | static uint64_t | ||
| 390 | readco(char *str) | ||
| 391 | { | ||
| 392 | if (*str == '0') | ||
| 393 | return 0ULL; | ||
| 394 | if (*str == '1') | ||
| 395 | return 1ULL; | ||
| 396 | if (*str == '2') | ||
| 397 | return 2ULL; | ||
| 398 | |||
| 399 | #ifdef DEBUG | ||
| 400 | fprintf(stderr, "Error reading CO\n"); | ||
| 401 | #endif | ||
| 402 | return _error; | ||
| 403 | } | ||
| 404 | |||
| 405 | cube_t | ||
| 406 | readcube(char *buf) | ||
| 407 | { | ||
| 408 | int i; | ||
| 409 | uint64_t piece, orient; | ||
| 410 | cube_t ret = {0}; | ||
| 411 | char *b = buf; | ||
| 412 | |||
| 413 | for (i = 0; i < 12; i++) { | ||
| 414 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 415 | b++; | ||
| 416 | if ((piece = readep(b)) == _error) | ||
| 417 | goto readcube_error; | ||
| 418 | b += 2; | ||
| 419 | if ((orient = readeo(b)) == _error) | ||
| 420 | goto readcube_error; | ||
| 421 | b++; | ||
| 422 | ret.e |= (piece << (i * 5ULL)) | (orient << (i * 5ULL + 4ULL)); | ||
| 423 | } | ||
| 424 | for (i = 0; i < 8; i++) { | ||
| 425 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 426 | b++; | ||
| 427 | if ((piece = readcp(b)) == _error) | ||
| 428 | goto readcube_error; | ||
| 429 | b += 3; | ||
| 430 | if ((orient = readco(b)) == _error) | ||
| 431 | goto readcube_error; | ||
| 432 | b++; | ||
| 433 | ret.c |= (piece << (i * 6ULL)) | (orient << (i * 6ULL + 3ULL)); | ||
| 434 | } | ||
| 435 | |||
| 436 | return ret; | ||
| 437 | |||
| 438 | readcube_error: | ||
| 439 | #ifdef DEBUG | ||
| 440 | fprintf(stderr, "readcube error\n"); | ||
| 441 | #endif | ||
| 442 | return errorcube; | ||
| 443 | } | ||
| 444 | |||
| 445 | void | ||
| 446 | writecube(cube_t cube, char *buf) | ||
| 447 | { | ||
| 448 | char *errormsg; | ||
| 449 | uint64_t piece, orien, x; | ||
| 450 | size_t len; | ||
| 451 | int i; | ||
| 452 | |||
| 453 | if (!isconsistent(cube)) { | ||
| 454 | errormsg = "ERROR: cannot write inconsistent cube"; | ||
| 455 | goto writecube_error; | ||
| 456 | } | ||
| 457 | |||
| 458 | for (i = 0, x = cube.e; i < 12; i++, x >>= 5ULL) { | ||
| 459 | piece = x & _epblock; | ||
| 460 | orien = (x & _eoblock) >> 4ULL; | ||
| 461 | buf[4*i ] = edgestr[piece][0]; | ||
| 462 | buf[4*i + 1] = edgestr[piece][1]; | ||
| 463 | buf[4*i + 2] = orien + '0'; | ||
| 464 | buf[4*i + 3] = ' '; | ||
| 465 | } | ||
| 466 | for (i = 0, x = cube.c; i < 8; i++, x >>= 6ULL) { | ||
| 467 | piece = x & _cpblock; | ||
| 468 | orien = (x & _coblock) >> 3ULL; | ||
| 469 | buf[48 + 5*i ] = cornerstr[piece][0]; | ||
| 470 | buf[48 + 5*i + 1] = cornerstr[piece][1]; | ||
| 471 | buf[48 + 5*i + 2] = cornerstr[piece][2]; | ||
| 472 | buf[48 + 5*i + 3] = orien + '0'; | ||
| 473 | buf[48 + 5*i + 4] = ' '; | ||
| 474 | } | ||
| 475 | |||
| 476 | buf[48+39] = '\0'; | ||
| 477 | |||
| 478 | return; | ||
| 479 | |||
| 480 | writecube_error: | ||
| 481 | #ifdef DEBUG | ||
| 482 | fprintf(stderr, "writecube error, see stdout for details\n"); | ||
| 483 | #endif | ||
| 484 | len = strlen(errormsg); | ||
| 485 | memcpy(buf, errormsg, len); | ||
| 486 | buf[len] = '\n'; | ||
| 487 | buf[len+1] = '\0'; | ||
| 488 | } | ||
| 489 | |||
| 490 | |||
| 491 | static uint64_t | ||
| 492 | readmove(char c) | ||
| 493 | { | ||
| 494 | switch (c) { | ||
| 495 | case 'U': | ||
| 496 | return U; | ||
| 497 | case 'D': | ||
| 498 | return D; | ||
| 499 | case 'R': | ||
| 500 | return R; | ||
| 501 | case 'L': | ||
| 502 | return L; | ||
| 503 | case 'F': | ||
| 504 | return F; | ||
| 505 | case 'B': | ||
| 506 | return B; | ||
| 507 | default: | ||
| 508 | return _error; | ||
| 509 | } | ||
| 510 | } | ||
| 511 | |||
| 512 | static uint64_t | ||
| 513 | readmodifier(char c) | ||
| 514 | { | ||
| 515 | switch (c) { | ||
| 516 | case '1': /* Fallthrough */ | ||
| 517 | case '2': /* Fallthrough */ | ||
| 518 | case '3': | ||
| 519 | return c - '0' - 1; | ||
| 520 | case '\'': | ||
| 521 | return 2; | ||
| 522 | default: | ||
| 523 | return 0; | ||
| 524 | } | ||
| 525 | } | ||
| 526 | |||
| 527 | int | ||
| 528 | readmoves(char *buf, move_t *m) | ||
| 529 | { | ||
| 530 | int n; | ||
| 531 | uint64_t r; | ||
| 532 | char *b; | ||
| 533 | |||
| 534 | for (b = buf, n = 0; *b != '\0'; b++) { | ||
| 535 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 536 | b++; | ||
| 537 | if (*b == '\0') | ||
| 538 | return n; | ||
| 539 | if ((r = readmove(*b)) == _error) | ||
| 540 | goto readmoves_error; | ||
| 541 | m[n] = (move_t)r; | ||
| 542 | if ((r = readmodifier(*(b+1))) != 0) { | ||
| 543 | b++; | ||
| 544 | m[n] += r; | ||
| 545 | } | ||
| 546 | n++; | ||
| 547 | } | ||
| 548 | |||
| 549 | return n; | ||
| 550 | |||
| 551 | readmoves_error: | ||
| 552 | #ifdef DEBUG | ||
| 553 | fprintf(stderr, "readmoves error\n"); | ||
| 554 | #endif | ||
| 555 | return -1; | ||
| 556 | } | ||
| 557 | |||
| 558 | void | ||
| 559 | writemoves(move_t *m, int n, char *buf) | ||
| 560 | { | ||
| 561 | int i; | ||
| 562 | size_t len; | ||
| 563 | char *b, *s; | ||
| 564 | |||
| 565 | for (i = 0, b = buf; i < n; i++, b++) { | ||
| 566 | s = movestr[m[i]]; | ||
| 567 | len = strlen(s); | ||
| 568 | memcpy(b, s, len); | ||
| 569 | b += len; | ||
| 570 | *b = ' '; | ||
| 571 | } | ||
| 572 | *b = '\0'; | ||
| 573 | } | ||
| 574 | |||
| 575 | |||
| 576 | static inline uint64_t | ||
| 577 | coapply(uint64_t c, uint64_t m) | ||
| 578 | { | ||
| 579 | uint64_t z, b; | ||
| 580 | |||
| 581 | z = c + m; | ||
| 582 | b = ((z + _coonemask) & _coextramask) >> 2ULL; | ||
| 583 | |||
| 584 | return (z + b) & ~_coextramask; | ||
| 585 | } | ||
| 586 | |||
| 587 | cube_t | ||
| 588 | move(cube_t c, move_t m) | ||
| 589 | { | ||
| 590 | cube_t ret = {0}; | ||
| 591 | |||
| 592 | #ifdef DEBUG | ||
| 593 | if (!isconsistent(c)) { | ||
| 594 | fprintf(stderr, "move error, inconsistent cube\n"); | ||
| 595 | goto move_error; | ||
| 596 | } | ||
| 597 | #endif | ||
| 598 | |||
| 599 | switch (m) { | ||
| 600 | case U: | ||
| 601 | ret.e = c.e & ~_emask_u; | ||
| 602 | ret.e |= | ||
| 603 | (c.e & _eblock) << 25ULL | | ||
| 604 | (c.e & _eblock << 5ULL) << 15ULL | | ||
| 605 | (c.e & _eblock2 << 20ULL) >> 20ULL; | ||
| 606 | |||
| 607 | ret.c = c.c & ~_cmask_u; | ||
| 608 | ret.c |= | ||
| 609 | (c.c & _cblock2) << 24ULL | | ||
| 610 | (c.c & _cblock << 24ULL) >> 18ULL | | ||
| 611 | (c.c & _cblock << 30ULL) >> 30ULL; | ||
| 612 | |||
| 613 | return ret; | ||
| 614 | case U2: | ||
| 615 | ret.e = c.e & ~_emask_u; | ||
| 616 | ret.e |= | ||
| 617 | (c.e & (_eblock | _eblock << 20ULL)) << 5ULL | | ||
| 618 | (c.e & (_eblock << 5ULL | _eblock << 25ULL)) >> 5ULL; | ||
| 619 | |||
| 620 | ret.c = c.c & ~_cmask_u; | ||
| 621 | ret.c |= | ||
| 622 | (c.c & (_cblock | _cblock << 24ULL)) << 6ULL | | ||
| 623 | (c.c & (_cblock << 6ULL | _cblock << 30ULL)) >> 6ULL; | ||
| 624 | |||
| 625 | return ret; | ||
| 626 | case U3: | ||
| 627 | ret.e = c.e & ~_emask_u; | ||
| 628 | ret.e |= | ||
| 629 | (c.e & _eblock2) << 20ULL | | ||
| 630 | (c.e & _eblock << 25ULL) >> 25ULL | | ||
| 631 | (c.e & _eblock << 20ULL) >> 15ULL; | ||
| 632 | |||
| 633 | ret.c = c.c & ~_cmask_u; | ||
| 634 | ret.c |= | ||
| 635 | (c.c & _cblock) << 30ULL | | ||
| 636 | (c.c & _cblock << 6ULL) << 18ULL | | ||
| 637 | (c.c & _cblock2 << 24ULL) >> 24ULL; | ||
| 638 | |||
| 639 | return ret; | ||
| 640 | case D: | ||
| 641 | ret.e = c.e & ~_emask_d; | ||
| 642 | ret.e |= | ||
| 643 | (c.e & _eblock2 << 10ULL) << 20ULL | | ||
| 644 | (c.e & _eblock << 30ULL) >> 15ULL | | ||
| 645 | (c.e & _eblock << 35ULL) >> 25ULL; | ||
| 646 | |||
| 647 | ret.c = c.c & ~_cmask_d; | ||
| 648 | ret.c |= | ||
| 649 | (c.c & _cblock2 << 12ULL) << 24ULL | | ||
| 650 | (c.c & _cblock << 36ULL) >> 18ULL | | ||
| 651 | (c.c & _cblock << 42ULL) >> 30ULL; | ||
| 652 | |||
| 653 | return ret; | ||
| 654 | case D2: | ||
| 655 | ret.e = c.e & ~_emask_d; | ||
| 656 | ret.e |= | ||
| 657 | (c.e & (_eblock << 10ULL | _eblock << 30ULL)) << 5ULL | | ||
| 658 | (c.e & (_eblock << 15ULL | _eblock << 35ULL)) >> 5ULL; | ||
| 659 | |||
| 660 | ret.c = c.c & ~_cmask_d; | ||
| 661 | ret.c |= | ||
| 662 | (c.c & (_cblock << 12ULL | _cblock << 36ULL)) << 6ULL | | ||
| 663 | (c.c & (_cblock << 18ULL | _cblock << 42ULL)) >> 6ULL; | ||
| 664 | |||
| 665 | return ret; | ||
| 666 | case D3: | ||
| 667 | ret.e = c.e & ~_emask_d; | ||
| 668 | ret.e |= | ||
| 669 | (c.e & _eblock << 10ULL) << 25ULL | | ||
| 670 | (c.e & _eblock << 15ULL) << 15ULL | | ||
| 671 | (c.e & _eblock2 << 30ULL) >> 20ULL; | ||
| 672 | |||
| 673 | ret.c = c.c & ~_cmask_d; | ||
| 674 | ret.c |= | ||
| 675 | (c.c & _cblock << 12ULL) << 30ULL | | ||
| 676 | (c.c & _cblock << 18ULL) << 18ULL | | ||
| 677 | (c.c & _cblock2 << 36ULL) >> 24ULL; | ||
| 678 | |||
| 679 | return ret; | ||
| 680 | case R: | ||
| 681 | ret.e = c.e & ~_emask_r; | ||
| 682 | ret.e |= | ||
| 683 | (c.e & _eblock << 20ULL) << 35ULL | | ||
| 684 | (c.e & _eblock << 55ULL) >> 20ULL | | ||
| 685 | (c.e & _eblock << 35ULL) << 5ULL | | ||
| 686 | (c.e & _eblock << 40ULL) >> 20ULL; | ||
| 687 | |||
| 688 | ret.c = c.c & ~_cmask_r; | ||
| 689 | ret.c |= | ||
| 690 | (c.c & _cblock) << 30ULL | | ||
| 691 | (c.c & _cblock << 30ULL) >> 12ULL | | ||
| 692 | (c.c & _cblock << 18ULL) << 18ULL | | ||
| 693 | (c.c & _cblock << 36ULL) >> 36ULL; | ||
| 694 | |||
| 695 | ret.c = coapply(ret.c, _comask_r); | ||
| 696 | |||
| 697 | return ret; | ||
| 698 | case R2: | ||
| 699 | ret.e = c.e & ~_emask_r; | ||
| 700 | ret.e |= | ||
| 701 | (c.e & (_eblock << 20ULL | _eblock << 40ULL)) << 15ULL | | ||
| 702 | (c.e & (_eblock << 35ULL | _eblock << 55ULL)) >> 15ULL; | ||
| 703 | |||
| 704 | ret.c = c.c & ~_cmask_r; | ||
| 705 | ret.c |= | ||
| 706 | (c.c & _cblock) << 18ULL | | ||
| 707 | (c.c & _cblock << 18ULL) >> 18ULL | | ||
| 708 | (c.c & _cblock << 30ULL) << 6ULL | | ||
| 709 | (c.c & _cblock << 36ULL) >> 6ULL; | ||
| 710 | |||
| 711 | return ret; | ||
| 712 | case R3: | ||
| 713 | ret.e = c.e & ~_emask_r; | ||
| 714 | ret.e |= | ||
| 715 | (c.e & (_eblock << 20ULL | _eblock << 35ULL)) << 20ULL | | ||
| 716 | (c.e & _eblock << 55ULL) >> 35ULL | | ||
| 717 | (c.e & _eblock << 40ULL) >> 5ULL; | ||
| 718 | |||
| 719 | ret.c = c.c & ~_cmask_r; | ||
| 720 | ret.c |= | ||
| 721 | (c.c & _cblock) << 36ULL | | ||
| 722 | (c.c & _cblock << 30ULL) >> 30ULL | | ||
| 723 | (c.c & _cblock << 18ULL) << 12ULL | | ||
| 724 | (c.c & _cblock << 36ULL) >> 18ULL; | ||
| 725 | |||
| 726 | ret.c = coapply(ret.c, _comask_r); | ||
| 727 | |||
| 728 | return ret; | ||
| 729 | case L: | ||
| 730 | ret.e = c.e & ~_emask_l; | ||
| 731 | ret.e |= | ||
| 732 | (c.e & _eblock2 << 25ULL) << 20ULL | | ||
| 733 | (c.e & _eblock << 45ULL) >> 15ULL | | ||
| 734 | (c.e & _eblock << 50ULL) >> 25ULL; | ||
| 735 | |||
| 736 | ret.c = c.c & ~_cmask_l; | ||
| 737 | ret.c |= | ||
| 738 | (c.c & _cblock << 6ULL) << 18ULL | | ||
| 739 | (c.c & _cblock << 12ULL) << 30ULL | | ||
| 740 | (c.c & _cblock << 24ULL) >> 12ULL | | ||
| 741 | (c.c & _cblock << 42ULL) >> 36ULL; | ||
| 742 | |||
| 743 | ret.c = coapply(ret.c, _comask_l); | ||
| 744 | |||
| 745 | return ret; | ||
| 746 | case L2: | ||
| 747 | ret.e = c.e & ~_emask_l; | ||
| 748 | ret.e |= | ||
| 749 | (c.e & (_eblock << 25ULL | _eblock << 45ULL)) << 5ULL | | ||
| 750 | (c.e & (_eblock << 30ULL | _eblock << 50ULL)) >> 5ULL; | ||
| 751 | |||
| 752 | ret.c = c.c & ~_cmask_l; | ||
| 753 | ret.c |= | ||
| 754 | (c.c & _cblock << 6ULL) << 6ULL | | ||
| 755 | (c.c & _cblock << 12ULL) >> 6ULL | | ||
| 756 | (c.c & _cblock << 24ULL) << 18ULL | | ||
| 757 | (c.c & _cblock << 42ULL) >> 18ULL; | ||
| 758 | |||
| 759 | return ret; | ||
| 760 | case L3: | ||
| 761 | ret.e = c.e & ~_emask_l; | ||
| 762 | ret.e |= | ||
| 763 | (c.e & _eblock << 25ULL) << 25ULL | | ||
| 764 | (c.e & _eblock << 30ULL) << 15ULL | | ||
| 765 | (c.e & _eblock2 << 45ULL) >> 20ULL; | ||
| 766 | |||
| 767 | ret.c = c.c & ~_cmask_l; | ||
| 768 | ret.c |= | ||
| 769 | (c.c & _cblock << 6ULL) << 36ULL | | ||
| 770 | (c.c & _cblock << 12ULL) << 12ULL | | ||
| 771 | (c.c & _cblock << 24ULL) >> 18ULL | | ||
| 772 | (c.c & _cblock << 42ULL) >> 30ULL; | ||
| 773 | |||
| 774 | ret.c = coapply(ret.c, _comask_l); | ||
| 775 | |||
| 776 | return ret; | ||
| 777 | case F: | ||
| 778 | ret.e = c.e & ~_emask_f; | ||
| 779 | ret.e |= | ||
| 780 | (c.e & _eblock) << 40ULL | | ||
| 781 | (c.e & _eblock << 15ULL) << 30ULL | | ||
| 782 | (c.e & _eblock << 40ULL) >> 25ULL | | ||
| 783 | (c.e & _eblock << 45ULL) >> 45ULL; | ||
| 784 | |||
| 785 | ret.e ^= _eomask_f; | ||
| 786 | |||
| 787 | ret.c = c.c & ~_cmask_f; | ||
| 788 | ret.c |= | ||
| 789 | (c.c & _cblock) << 36ULL | | ||
| 790 | (c.c & (_cblock << 24ULL | _cblock << 36ULL)) >> 24ULL | | ||
| 791 | (c.c & _cblock << 12ULL) << 12ULL; | ||
| 792 | |||
| 793 | ret.c = coapply(ret.c, _comask_f); | ||
| 794 | |||
| 795 | return ret; | ||
| 796 | case F2: | ||
| 797 | ret.e = c.e & ~_emask_f; | ||
| 798 | ret.e |= | ||
| 799 | (c.e & _eblock) << 15ULL | | ||
| 800 | (c.e & _eblock << 15ULL) >> 15ULL | | ||
| 801 | (c.e & _eblock << 40ULL) << 5ULL | | ||
| 802 | (c.e & _eblock << 45ULL) >> 5ULL; | ||
| 803 | |||
| 804 | ret.c = c.c & ~_cmask_f; | ||
| 805 | ret.c |= | ||
| 806 | (c.c & (_cblock | _cblock << 24ULL)) << 12ULL | | ||
| 807 | (c.c & (_cblock << 12ULL | _cblock << 36ULL)) >> 12ULL; | ||
| 808 | |||
| 809 | return ret; | ||
| 810 | case F3: | ||
| 811 | ret.e = c.e & ~_emask_f; | ||
| 812 | ret.e |= | ||
| 813 | (c.e & _eblock) << 45ULL | | ||
| 814 | (c.e & _eblock << 15ULL) << 25ULL | | ||
| 815 | (c.e & _eblock << 40ULL) >> 40ULL | | ||
| 816 | (c.e & _eblock << 45ULL) >> 30ULL; | ||
| 817 | |||
| 818 | ret.e ^= _eomask_f; | ||
| 819 | |||
| 820 | ret.c = c.c & ~_cmask_f; | ||
| 821 | ret.c |= | ||
| 822 | (c.c & (_cblock | _cblock << 12ULL)) << 24ULL | | ||
| 823 | (c.c & _cblock << 24ULL) >> 12ULL | | ||
| 824 | (c.c & _cblock << 36ULL) >> 36ULL; | ||
| 825 | |||
| 826 | ret.c = coapply(ret.c, _comask_f); | ||
| 827 | |||
| 828 | return ret; | ||
| 829 | case B: | ||
| 830 | ret.e = c.e & ~_emask_b; | ||
| 831 | ret.e |= | ||
| 832 | (c.e & _eblock2 << 5ULL) << 45ULL | | ||
| 833 | (c.e & _eblock << 50ULL) >> 40ULL | | ||
| 834 | (c.e & _eblock << 55ULL) >> 50ULL; | ||
| 835 | |||
| 836 | ret.e ^= _eomask_b; | ||
| 837 | |||
| 838 | ret.c = c.c & ~_cmask_b; | ||
| 839 | ret.c |= | ||
| 840 | (c.c & _cblock << 6ULL) << 36ULL | | ||
| 841 | (c.c & _cblock << 18ULL) << 12ULL | | ||
| 842 | (c.c & (_cblock << 30ULL | _cblock << 42ULL)) >> 24ULL; | ||
| 843 | |||
| 844 | ret.c = coapply(ret.c, _comask_b); | ||
| 845 | |||
| 846 | return ret; | ||
| 847 | case B2: | ||
| 848 | ret.e = c.e & ~_emask_b; | ||
| 849 | ret.e |= | ||
| 850 | (c.e & (_eblock << 5ULL | _eblock << 50ULL)) << 5ULL | | ||
| 851 | (c.e & (_eblock << 10ULL | _eblock << 55ULL)) >> 5ULL; | ||
| 852 | |||
| 853 | ret.c = c.c & ~_cmask_b; | ||
| 854 | ret.c |= | ||
| 855 | (c.c & (_cblock << 6ULL | _cblock << 30ULL)) << 12ULL | | ||
| 856 | (c.c & (_cblock << 18ULL | _cblock << 42ULL)) >> 12ULL; | ||
| 857 | |||
| 858 | return ret; | ||
| 859 | case B3: | ||
| 860 | ret.e = c.e & ~_emask_b; | ||
| 861 | ret.e |= | ||
| 862 | (c.e & _eblock << 5ULL) << 50ULL | | ||
| 863 | (c.e & _eblock << 10ULL) << 40ULL | | ||
| 864 | (c.e & _eblock2 << 50ULL) >> 45ULL; | ||
| 865 | |||
| 866 | ret.e ^= _eomask_b; | ||
| 867 | |||
| 868 | ret.c = c.c & ~_cmask_b; | ||
| 869 | ret.c |= | ||
| 870 | (c.c & (_cblock << 6ULL | _cblock << 18ULL)) << 24ULL | | ||
| 871 | (c.c & _cblock << 30ULL) >> 12ULL | | ||
| 872 | (c.c & _cblock << 42ULL) >> 36ULL; | ||
| 873 | |||
| 874 | ret.c = coapply(ret.c, _comask_b); | ||
| 875 | |||
| 876 | return ret; | ||
| 877 | default: | ||
| 878 | goto move_unknown; | ||
| 879 | } | ||
| 880 | |||
| 881 | move_unknown: | ||
| 882 | #ifdef DEBUG | ||
| 883 | fprintf(stderr, "move error, unknow move\n"); | ||
| 884 | #endif | ||
| 885 | goto move_error; | ||
| 886 | move_error: | ||
| 887 | return errorcube; | ||
| 888 | } | ||
diff --git a/old/manualsimd/cube.h b/old/manualsimd/cube.h deleted file mode 100644 index b916f96..0000000 --- a/old/manualsimd/cube.h +++ /dev/null | |||
| @@ -1,27 +0,0 @@ | |||
| 1 | typedef enum { | ||
| 2 | U, U2, U3, D, D2, D3, | ||
| 3 | R, R2, R3, L, L2, L3, | ||
| 4 | F, F2, F3, B, B2, B3 | ||
| 5 | } move_t; | ||
| 6 | typedef struct { | ||
| 7 | uint64_t e; | ||
| 8 | uint64_t c; | ||
| 9 | } cube_t; | ||
| 10 | |||
| 11 | extern cube_t solvedcube; | ||
| 12 | extern cube_t errorcube; | ||
| 13 | |||
| 14 | bool isconsistent(cube_t); | ||
| 15 | bool issolved(cube_t); | ||
| 16 | |||
| 17 | cube_t readcube(char *); | ||
| 18 | void writecube(cube_t, char *); | ||
| 19 | |||
| 20 | int readmoves(char *, move_t *); | ||
| 21 | void writemoves(move_t *, int, char *); | ||
| 22 | |||
| 23 | cube_t move(cube_t, move_t); | ||
| 24 | |||
| 25 | /* | ||
| 26 | cube_t inverse(cube_t); | ||
| 27 | */ | ||
diff --git a/src/solve_generic.h b/src/solve_generic.h index c80f3ef..4e7833f 100644 --- a/src/solve_generic.h +++ b/src/solve_generic.h | |||
| @@ -235,12 +235,18 @@ int64_t | |||
| 235 | gendata(const char *solver, const char *options, void *data) | 235 | gendata(const char *solver, const char *options, void *data) |
| 236 | { | 236 | { |
| 237 | int64_t ret; | 237 | int64_t ret; |
| 238 | uint8_t maxdepth; | 238 | uint8_t maxdepth, h, i, j; |
| 239 | 239 | ||
| 240 | if (!strcmp(solver, "H48")) { | 240 | if (!strcmp(solver, "H48")) { |
| 241 | /* TODO: write a generic parser for options */ | 241 | /* |
| 242 | maxdepth = atoi(options); | 242 | TODO: write a generic parser for options |
| 243 | ret = gendata_h48(data, 0, maxdepth); | 243 | for now it accepts "h;maxdepth" |
| 244 | */ | ||
| 245 | for (i = 0; options[i] != ';'; i++) ; | ||
| 246 | for (j = i; options[j]; j++) ; | ||
| 247 | h = atoi(options); | ||
| 248 | maxdepth = atoi(&options[i+1]); | ||
| 249 | ret = gendata_h48(data, h, maxdepth); | ||
| 244 | } else { | 250 | } else { |
| 245 | DBG_LOG("gendata: implemented only for H48 solver\n"); | 251 | DBG_LOG("gendata: implemented only for H48 solver\n"); |
| 246 | ret = -1; | 252 | ret = -1; |
diff --git a/old/cube_h_with_format_documentation b/utils/cube_h_with_format_documentation index 3cbc3b5..3cbc3b5 100644 --- a/old/cube_h_with_format_documentation +++ b/utils/cube_h_with_format_documentation | |||
