From 18c9a8b8905304cf5f8fc15825769046a3144866 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Sun, 18 Aug 2024 14:26:45 +0200 Subject: Reorganized folder structure --- LICENSE | 4 +- Makefile | 6 +- shell.c | 2 +- src/arch/arch.h | 41 ++ src/arch/avx2.h | 319 ++++++++++ src/arch/common.h | 19 + src/arch/neon.h | 348 +++++++++++ src/arch/portable.h | 272 +++++++++ src/constant_cubes.h | 229 ------- src/constants.h | 295 --------- src/core/constant_cubes.h | 229 +++++++ src/core/core.h | 13 + src/core/cube.h | 291 +++++++++ src/core/io_cube.h | 348 +++++++++++ src/core/io_moves.h | 62 ++ src/core/io_trans.h | 25 + src/core/moves.h | 47 ++ src/core/transform.h | 176 ++++++ src/core/transform_with_switch.h | 336 +++++++++++ src/cube.c | 55 -- src/cube.h | 104 ---- src/cube_avx2.h | 341 ----------- src/cube_generic.h | 291 --------- src/cube_neon.h | 377 ------------ src/cube_portable.h | 298 --------- src/cube_public.h | 255 -------- src/cube_transform.h | 176 ------ src/cube_transform_with_switch.h | 336 ----------- src/io_cube.h | 348 ----------- src/io_move_trans.h | 87 --- src/moves.h | 47 -- src/nissy.c | 265 ++++++++ src/nissy.h | 104 ++++ src/solve_generic.h | 157 ----- src/solve_h48.h | 839 -------------------------- src/solvers/generic/generic.h | 157 +++++ src/solvers/h48/coordinate.h | 68 +++ src/solvers/h48/gendata.h | 425 +++++++++++++ src/solvers/h48/h48.h | 4 + src/solvers/h48/map.h | 104 ++++ src/solvers/h48/solve.h | 242 ++++++++ src/solvers/solvers.h | 2 + src/utils.h | 185 ------ src/utils/constants.h | 295 +++++++++ src/utils/dbg_log.h | 16 + src/utils/math.h | 185 ++++++ src/utils/utils.h | 3 + test/test.h | 21 +- tools/001_gendata_h48/gendata_h48.c | 2 +- tools/002_stats_tables_h48/stats_tables_h48.c | 2 +- utils/genmovecode.sh | 2 +- utils/gentranscode.sh | 4 +- utils/h48_to_lst.c | 2 +- utils/invert.c | 2 +- 54 files changed, 4414 insertions(+), 4449 deletions(-) create mode 100644 src/arch/arch.h create mode 100644 src/arch/avx2.h create mode 100644 src/arch/common.h create mode 100644 src/arch/neon.h create mode 100644 src/arch/portable.h delete mode 100644 src/constant_cubes.h delete mode 100644 src/constants.h create mode 100644 src/core/constant_cubes.h create mode 100644 src/core/core.h create mode 100644 src/core/cube.h create mode 100644 src/core/io_cube.h create mode 100644 src/core/io_moves.h create mode 100644 src/core/io_trans.h create mode 100644 src/core/moves.h create mode 100644 src/core/transform.h create mode 100644 src/core/transform_with_switch.h delete mode 100644 src/cube.c delete mode 100644 src/cube.h delete mode 100644 src/cube_avx2.h delete mode 100644 src/cube_generic.h delete mode 100644 src/cube_neon.h delete mode 100644 src/cube_portable.h delete mode 100644 src/cube_public.h delete mode 100644 src/cube_transform.h delete mode 100644 src/cube_transform_with_switch.h delete mode 100644 src/io_cube.h delete mode 100644 src/io_move_trans.h delete mode 100644 src/moves.h create mode 100644 src/nissy.c create mode 100644 src/nissy.h delete mode 100644 src/solve_generic.h delete mode 100644 src/solve_h48.h create mode 100644 src/solvers/generic/generic.h create mode 100644 src/solvers/h48/coordinate.h create mode 100644 src/solvers/h48/gendata.h create mode 100644 src/solvers/h48/h48.h create mode 100644 src/solvers/h48/map.h create mode 100644 src/solvers/h48/solve.h create mode 100644 src/solvers/solvers.h delete mode 100644 src/utils.h create mode 100644 src/utils/constants.h create mode 100644 src/utils/dbg_log.h create mode 100644 src/utils/math.h create mode 100644 src/utils/utils.h diff --git a/LICENSE b/LICENSE index 8af6571..7c787f3 100644 --- a/LICENSE +++ b/LICENSE @@ -2,7 +2,9 @@ The following license applies to every C source code and header file distributed with this LICENSE file. h48 - a prototype for an optimal rubik's cube solver - Copyright (C) 2023 Sebastiano Tronto + Copyright (C) 2023-2024 + Sebastiano Tronto + Enrico Tenuti <> This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by diff --git a/Makefile b/Makefile index 5130940..0b7204e 100644 --- a/Makefile +++ b/Makefile @@ -3,13 +3,13 @@ include config.mk all: cube.o debugcube.o cube.s: clean - ${CC} -D${CUBETYPE} ${CFLAGS} -c -S -o cube.s src/cube.c + ${CC} -D${CUBETYPE} ${CFLAGS} -c -S -o cube.s src/nissy.c cube.o: clean - ${CC} -D${CUBETYPE} ${CFLAGS} -c -o cube.o src/cube.c + ${CC} -D${CUBETYPE} ${CFLAGS} -c -o cube.o src/nissy.c debugcube.o: clean - ${CC} -D${CUBETYPE} ${DBGFLAGS} -c -o debugcube.o src/cube.c + ${CC} -D${CUBETYPE} ${DBGFLAGS} -c -o debugcube.o src/nissy.c clean: rm -rf *.o run diff --git a/shell.c b/shell.c index 9c5ac08..c1efcbd 100644 --- a/shell.c +++ b/shell.c @@ -7,7 +7,7 @@ #include #include -#include "src/cube.h" +#include "src/nissy.h" #define PRINTCUBE_BUFFER_SIZE 1024 /* Should be enough */ #define SOLUTIONS_BUFFER_SIZE 500000 /* Should be enough */ diff --git a/src/arch/arch.h b/src/arch/arch.h new file mode 100644 index 0000000..d5e0218 --- /dev/null +++ b/src/arch/arch.h @@ -0,0 +1,41 @@ +#if defined(CUBE_AVX2) + +#include + +typedef __m256i cube_t; + +#if !defined(TEST_H) +#include "common.h" +#include "avx2.h" +#endif + +#elif defined(CUBE_NEON) + +#include +#include + +typedef struct { + uint8x16_t corner; + uint8x16_t edge; +} cube_t; + +#if !defined(TEST_H) +#include "common.h" +#include "neon.h" +#endif + +#else + +#include + +typedef struct { + uint8_t corner[8]; + uint8_t edge[12]; +} cube_t; + +#if !defined(TEST_H) +#include "common.h" +#include "portable.h" +#endif + +#endif diff --git a/src/arch/avx2.h b/src/arch/avx2.h new file mode 100644 index 0000000..59a0d6c --- /dev/null +++ b/src/arch/avx2.h @@ -0,0 +1,319 @@ +#define _co2_avx2 _mm256_set_epi64x(0, 0, 0, INT64_C(0x6060606060606060)) +#define _cocw_avx2 _mm256_set_epi64x(0, 0, 0, INT64_C(0x2020202020202020)) +#define _cp_avx2 _mm256_set_epi64x(0, 0, 0, INT64_C(0x0707070707070707)) +#define _ep_avx2 \ + _mm256_set_epi64x(INT64_C(0x0F0F0F0F), INT64_C(0x0F0F0F0F0F0F0F0F), 0, 0) +#define _eo_avx2 \ + _mm256_set_epi64x(INT64_C(0x10101010), INT64_C(0x1010101010101010), 0, 0) + +#define static_cube(c_ufr, c_ubl, c_dfl, c_dbr, c_ufl, c_ubr, c_dfr, c_dbl, \ + e_uf, e_ub, e_db, e_df, e_ur, e_ul, e_dl, e_dr, e_fr, e_fl, e_bl, e_br) \ + _mm256_set_epi8(0, 0, 0, 0, e_br, e_bl, e_fl, e_fr, \ + e_dr, e_dl, e_ul, e_ur, e_df, e_db, e_ub, e_uf, \ + 0, 0, 0, 0, 0, 0, 0, 0, \ + c_dbl, c_dfr, c_ubr, c_ufl, c_dbr, c_dfl, c_ubl, c_ufr) +#define zero _mm256_set_epi64x(0, 0, 0, 0) +#define solved static_cube( \ + 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) + +_static void +pieces(cube_t *cube, uint8_t c[static 8], uint8_t e[static 12]) +{ + uint8_t aux[32]; + + _mm256_storeu_si256((__m256i_u *)aux, *cube); + memcpy(c, aux, 8); + memcpy(e, aux+16, 12); +} + +_static_inline bool +equal(cube_t c1, cube_t c2) +{ + int32_t mask; + __m256i cmp; + + cmp = _mm256_cmpeq_epi8(c1, c2); + mask = _mm256_movemask_epi8(cmp); + + return mask == ~0; +} + +_static_inline cube_t +invertco(cube_t c) +{ + cube_t co, shleft, shright, summed, newco, cleanco, ret; + + co = _mm256_and_si256(c, _co2_avx2); + shleft = _mm256_slli_epi32(co, 1); + shright = _mm256_srli_epi32(co, 1); + summed = _mm256_or_si256(shleft, shright); + newco = _mm256_and_si256(summed, _co2_avx2); + cleanco = _mm256_xor_si256(c, co); + ret = _mm256_or_si256(cleanco, newco); + + return ret; +} + +_static_inline cube_t +compose_epcpeo(cube_t c1, cube_t c2) +{ + cube_t b, s, eo2; + + /* Permute and clean unused bits */ + s = _mm256_shuffle_epi8(c1, c2); + b = _mm256_set_epi8( + ~0, ~0, ~0, ~0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ~0, ~0, ~0, ~0, ~0, ~0, ~0, ~0, 0, 0, 0, 0, 0, 0, 0, 0 + ); + s = _mm256_andnot_si256(b, s); + + /* Change EO */ + eo2 = _mm256_and_si256(c2, _eo_avx2); + s = _mm256_xor_si256(s, eo2); + + return s; +} + +_static_inline cube_t +compose_edges(cube_t c1, cube_t c2) +{ + return compose_epcpeo(c1, c2); +} + +_static_inline cube_t +compose_corners(cube_t c1, cube_t c2) +{ + /* + * We do a full compose. Minor optimizations are possible, like + * saving one instruction by not doing EO, but it should not + * be significant. + */ + return compose(c1, c2); +} + +_static_inline cube_t +compose(cube_t c1, cube_t c2) +{ + cube_t s, co1, co2, aux, auy1, auy2, auz1, auz2; + + s = compose_epcpeo(c1, c2); + + /* Change CO */ + co1 = _mm256_and_si256(s, _co2_avx2); + co2 = _mm256_and_si256(c2, _co2_avx2); + aux = _mm256_add_epi8(co1, co2); + auy1 = _mm256_add_epi8(aux, _cocw_avx2); + auy2 = _mm256_srli_epi32(auy1, 2); + auz1 = _mm256_add_epi8(aux, auy2); + auz2 = _mm256_and_si256(auz1, _co2_avx2); + + /* Put together */ + s = _mm256_andnot_si256(_co2_avx2, s); + s = _mm256_or_si256(s, auz2); + + return s; +} + +_static_inline cube_t +cleanaftershuffle(cube_t c) +{ + __m256i b; + + b = _mm256_set_epi8( + ~0, ~0, ~0, ~0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ~0, ~0, ~0, ~0, ~0, ~0, ~0, ~0, 0, 0, 0, 0, 0, 0, 0, 0 + ); + + return _mm256_andnot_si256(b, c); +} + +_static_inline cube_t +inverse(cube_t c) +{ + /* Method taken from Andrew Skalski's vcube[1]. The addition sequence + * was generated using [2]. + * [1] https://github.com/Voltara/vcube + * [2] http://wwwhomes.uni-bielefeld.de/achim/addition_chain.html + */ + cube_t v3, vi, vo, vp, ret; + + v3 = _mm256_shuffle_epi8(c, c); + v3 = _mm256_shuffle_epi8(v3, c); + vi = _mm256_shuffle_epi8(v3, v3); + vi = _mm256_shuffle_epi8(vi, vi); + vi = _mm256_shuffle_epi8(vi, vi); + vi = _mm256_shuffle_epi8(vi, v3); + vi = _mm256_shuffle_epi8(vi, vi); + vi = _mm256_shuffle_epi8(vi, vi); + vi = _mm256_shuffle_epi8(vi, vi); + vi = _mm256_shuffle_epi8(vi, vi); + vi = _mm256_shuffle_epi8(vi, c); + vi = _mm256_shuffle_epi8(vi, vi); + vi = _mm256_shuffle_epi8(vi, vi); + vi = _mm256_shuffle_epi8(vi, vi); + vi = _mm256_shuffle_epi8(vi, vi); + vi = _mm256_shuffle_epi8(vi, vi); + vi = _mm256_shuffle_epi8(vi, v3); + vi = _mm256_shuffle_epi8(vi, vi); + vi = _mm256_shuffle_epi8(vi, c); + + vo = _mm256_and_si256(c, _mm256_or_si256(_eo_avx2, _co2_avx2)); + vo = _mm256_shuffle_epi8(vo, vi); + vp = _mm256_andnot_si256(_mm256_or_si256(_eo_avx2, _co2_avx2), vi); + ret = _mm256_or_si256(vp, vo); + ret = cleanaftershuffle(ret); + + return invertco(ret); +} + +_static_inline int64_t +coord_co(cube_t c) +{ + cube_t co; + int64_t mem[4], ret, i, p; + + co = _mm256_and_si256(c, _co2_avx2); + _mm256_storeu_si256((__m256i *)mem, co); + + mem[0] >>= 5; + for (i = 0, ret = 0, p = 1; i < 7; i++, mem[0] >>= 8, p *= 3) + ret += (mem[0] & 3) * p; + + return ret; +} + +_static_inline int64_t +coord_csep(cube_t c) +{ + cube_t cp, shifted; + int64_t mask; + + cp = _mm256_and_si256(c, _cp_avx2); + shifted = _mm256_slli_epi32(cp, 5); + mask = _mm256_movemask_epi8(shifted); + + return mask & 0x7F; +} + +_static_inline int64_t +coord_cocsep(cube_t c) +{ + return (coord_co(c) << 7) + coord_csep(c); +} + +_static_inline int64_t +coord_eo(cube_t c) +{ + cube_t eo, shifted; + int64_t mask; + + eo = _mm256_and_si256(c, _eo_avx2); + shifted = _mm256_slli_epi32(eo, 3); + mask = _mm256_movemask_epi8(shifted); + + return mask >> 17; +} + +_static_inline int64_t +coord_esep(cube_t c) +{ + cube_t ep; + int64_t e, mem[4], i, j, jj, k, l, ret1, ret2, bit1, bit2, is1; + + ep = _mm256_and_si256(c, _ep_avx2); + _mm256_storeu_si256((__m256i *)mem, ep); + + mem[3] <<= 8; + ret1 = ret2 = 0; + k = l = 4; + for (i = 0, j = 0; i < 12; i++, mem[i/8 + 2] >>= 8) { + e = mem[i/8 + 2]; + + bit1 = (e & _esepbit1) >> 2; + bit2 = (e & _esepbit2) >> 3; + is1 = (1 - bit2) * bit1; + + ret1 += bit2 * binomial[11-i][k]; + k -= bit2; + + jj = j < 8; + ret2 += jj * is1 * binomial[7-(j*jj)][l]; + l -= is1; + j += (1-bit2); + } + + return ret1 * 70 + ret2; +} + +_static_inline void +copy_corners(cube_t *dest, cube_t src) +{ + *dest = _mm256_blend_epi32(*dest, src, 0x0F); +} + +_static_inline void +copy_edges(cube_t *dest, cube_t src) +{ + *dest = _mm256_blend_epi32(*dest, src, 0xF0); +} + +_static_inline void +set_eo(cube_t *cube, int64_t eo) +{ + int64_t eo12, eotop, eobot; + __m256i veo; + + eo12 = (eo << 1) + (_mm_popcnt_u64(eo) % 2); + eotop = (eo12 & (1 << 11)) << 17 | + (eo12 & (1 << 10)) << 10 | + (eo12 & (1 << 9)) << 3 | + (eo12 & (1 << 8)) >> 4; + eobot = (eo12 & (1 << 7)) << 53 | + (eo12 & (1 << 6)) << 46 | + (eo12 & (1 << 5)) << 39 | + (eo12 & (1 << 4)) << 32 | + (eo12 & (1 << 3)) << 25 | + (eo12 & (1 << 2)) << 18 | + (eo12 & (1 << 1)) << 11 | + (eo12 & 1) << 4; + veo = _mm256_set_epi64x(eotop, eobot, 0, 0); + + *cube = _mm256_andnot_si256(_eo_avx2, *cube); + *cube = _mm256_or_si256(*cube, veo); +} + +_static_inline cube_t +invcoord_esep(int64_t esep) +{ + cube_t eee, ret; + int64_t bit1, bit2, i, j, jj, k, l, s, v, w, is1, set1, set2; + uint8_t mem[32]; + uint8_t slice[3] = {0}; + + set1 = esep % 70; + set2 = esep / 70; + + for (i = 0, j = 0, k = 4, l = 4; i < 12; i++) { + v = binomial[11-i][k]; + jj = j < 8; + w = jj * binomial[7-(j*jj)][l]; + bit2 = set2 >= v; + bit1 = set1 >= w; + is1 = (1 - bit2) * bit1; + + set2 -= bit2 * v; + k -= bit2; + set1 -= is1 * w; + l -= is1; + j += (1-bit2); + s = 2*bit2 + (1-bit2)*bit1; + + mem[i+16] = (slice[s]++) | (uint8_t)(s << 2); + } + + ret = solved; + eee = _mm256_loadu_si256((__m256i_u *)&mem); + copy_edges(&ret, eee); + + return ret; +} diff --git a/src/arch/common.h b/src/arch/common.h new file mode 100644 index 0000000..cd2d36e --- /dev/null +++ b/src/arch/common.h @@ -0,0 +1,19 @@ +_static void pieces(cube_t *, uint8_t [static 8], uint8_t [static 12]); +_static_inline bool equal(cube_t, cube_t); +_static_inline cube_t invertco(cube_t); +_static_inline cube_t compose_epcpeo(cube_t, cube_t); +_static_inline cube_t compose_edges(cube_t, cube_t); +_static_inline cube_t compose_corners(cube_t, cube_t); +_static_inline cube_t compose(cube_t, cube_t); +_static_inline cube_t inverse(cube_t); + +_static_inline int64_t coord_co(cube_t); +_static_inline int64_t coord_csep(cube_t); +_static_inline int64_t coord_cocsep(cube_t); +_static_inline int64_t coord_eo(cube_t); +_static_inline int64_t coord_esep(cube_t); + +_static_inline void copy_corners(cube_t *, cube_t); +_static_inline void copy_edges(cube_t *, cube_t); +_static_inline void set_eo(cube_t *, int64_t); +_static_inline cube_t invcoord_esep(int64_t); diff --git a/src/arch/neon.h b/src/arch/neon.h new file mode 100644 index 0000000..a75f86d --- /dev/null +++ b/src/arch/neon.h @@ -0,0 +1,348 @@ +#define _co2_neon vdupq_n_u8(0x60) +#define _cocw_neon vdupq_n_u8(0x20) +#define _cp_neon vdupq_n_u8(0x07) +#define _ep_neon vcombine_u8(vdupq_n_u8(0x0F), vdupq_n_u8(0x0F)) +#define _eo_neon vcombine_u8(vdupq_n_u8(0x10), vdupq_n_u8(0x10)) + +// static cube +#define static_cube(c_ufr, c_ubl, c_dfl, c_dbr, c_ufl, c_ubr, c_dfr, c_dbl, \ + e_uf, e_ub, e_db, e_df, e_ur, e_ul, e_dl, e_dr, e_fr, e_fl, e_bl, e_br) \ + ((cube_t){ \ + .corner = {c_ufr, c_ubl, c_dfl, c_dbr, c_ufl, c_ubr, c_dfr, c_dbl, 0, 0, 0, 0, 0, 0, 0, 0}, \ + .edge = {e_uf, e_ub, e_db, e_df, e_ur, e_ul, e_dl, e_dr, e_fr, e_fl, e_bl, e_br, 0, 0, 0, 0}}) + +// zero cube +#define zero \ + (cube_t) \ + { \ + .corner = vdupq_n_u8(0), \ + .edge = vdupq_n_u8(0) \ + } + +// solved cube +#define solved static_cube( \ + 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) + +_static void +pieces(cube_t *cube, uint8_t c[static 8], uint8_t e[static 12]) +{ + // First 8 bytes of the corner vector are copied from the c array + vst1_u8(c, vget_low_u8(cube->corner)); + + // 12 bytes of the edge vector are copied from the e array + // First 8 bytes + vst1_u8(e, vget_low_u8(cube->edge)); + // Next 4 bytes + vst1_lane_u32((uint32_t *)(e + 8), vreinterpret_u32_u8(vget_high_u8(cube->edge)), 0); +} + +_static_inline bool +equal(cube_t c1, cube_t c2) +{ + uint8x16_t cmp_corner, cmp_edge; + uint64x2_t cmp_corner_u64, cmp_edge_u64; + uint64x2_t cmp_result; + + // compare the corner vectors + cmp_corner = vceqq_u8(c1.corner, c2.corner); + // compare the edge vectors + cmp_edge = vceqq_u8(c1.edge, c2.edge); + + // convert the comparison vectors to 64-bit vectors + cmp_corner_u64 = vreinterpretq_u64_u8(cmp_corner); + cmp_edge_u64 = vreinterpretq_u64_u8(cmp_edge); + + // combine the comparison vectors + cmp_result = vandq_u64(cmp_corner_u64, cmp_edge_u64); + + // check if all the bits are set + return vgetq_lane_u64(cmp_result, 0) == ~0ULL && vgetq_lane_u64(cmp_result, 1) == ~0ULL; +} + +_static_inline cube_t +invertco(cube_t c) +{ + cube_t ret; + uint8x16_t co, shleft, shright, summed, newco, cleanco; + + co = vandq_u8(c.corner, _co2_neon); + shleft = vshlq_n_u8(co, 1); + shright = vshrq_n_u8(co, 1); + summed = vorrq_u8(shleft, shright); + newco = vandq_u8(summed, _co2_neon); + cleanco = veorq_u8(c.corner, co); + ret.corner = vorrq_u8(cleanco, newco); + ret.edge = c.edge; + + return ret; +} + +_static_inline cube_t +compose_edges(cube_t c1, cube_t c2) +{ + cube_t ret = {0}; + ret.edge = compose_edges_slim(c1.edge, c2.edge); + return ret; +} + +_static_inline cube_t +compose_corners(cube_t c1, cube_t c2) +{ + cube_t ret = {0}; + ret.corner = compose_corners_slim(c1.corner, c2.corner); + return ret; +} + +_static_inline uint8x16_t +compose_edges_slim(uint8x16_t edge1, uint8x16_t edge2) +{ + // Masks + uint8x16_t p_bits = vdupq_n_u8(_pbits); + uint8x16_t eo_bit = vdupq_n_u8(_eobit); + + // Find the index and permutation + uint8x16_t p = vandq_u8(edge2, p_bits); + uint8x16_t piece1 = vqtbl1q_u8(edge1, p); + + // Calculate the orientation through XOR + uint8x16_t orien = vandq_u8(veorq_u8(edge2, piece1), eo_bit); + + // Combine the results + uint8x16_t ret = vorrq_u8(vandq_u8(piece1, p_bits), orien); + + // Mask to clear the last 32 bits of the result + uint8x16_t mask_last_32 = vsetq_lane_u32(0, vreinterpretq_u32_u8(ret), 3); + ret = vreinterpretq_u8_u32(mask_last_32); + + return ret; +} + +_static_inline uint8x16_t +compose_corners_slim(uint8x16_t corner1, uint8x16_t corner2) +{ + // Masks + uint8x16_t p_bits = vdupq_n_u8(_pbits); + uint8x16_t cobits = vdupq_n_u8(_cobits); + uint8x16_t cobits2 = vdupq_n_u8(_cobits2); + uint8x16_t twist_cw = vdupq_n_u8(_ctwist_cw); + + // Find the index and permutation + uint8x16_t p = vandq_u8(corner2, p_bits); + uint8x16_t piece1 = vqtbl1q_u8(corner1, p); + + // Calculate the orientation + uint8x16_t aux = vaddq_u8(vandq_u8(corner2, cobits), vandq_u8(piece1, cobits)); + uint8x16_t auy = vshrq_n_u8(vaddq_u8(aux, twist_cw), 2); + uint8x16_t orien = vandq_u8(vaddq_u8(aux, auy), cobits2); + + // Combine the results + uint8x16_t ret = vorrq_u8(vandq_u8(piece1, p_bits), orien); + + // Mask to clear the last 64 bits of the result + uint8x16_t mask_last_64 = vsetq_lane_u64(0, vreinterpretq_u64_u8(ret), 1); + ret = vreinterpretq_u8_u64(mask_last_64); + + return ret; +} + +_static_inline cube_t +compose(cube_t c1, cube_t c2) +{ + cube_t ret = {0}; + + ret.edge = compose_edges_slim(c1.edge, c2.edge); + ret.corner = compose_corners_slim(c1.corner, c2.corner); + + return ret; +} + +_static_inline cube_t +inverse(cube_t cube) +{ + uint8_t i, piece, orien; + cube_t ret; + + // Temp arrays to store the NEON vectors + uint8_t edges[16]; + uint8_t corners[16]; + + // Copy the NEON vectors to the arrays + vst1q_u8(edges, cube.edge); + vst1q_u8(corners, cube.corner); + + uint8_t edge_result[16] = {0}; + uint8_t corner_result[16] = {0}; + + // Process the edges + for (i = 0; i < 12; i++) + { + piece = edges[i]; + orien = piece & _eobit; + edge_result[piece & _pbits] = i | orien; + } + + // Process the corners + for (i = 0; i < 8; i++) + { + piece = corners[i]; + orien = ((piece << 1) | (piece >> 1)) & _cobits2; + corner_result[piece & _pbits] = i | orien; + } + + // Copy the results back to the NEON vectors + ret.edge = vld1q_u8(edge_result); + ret.corner = vld1q_u8(corner_result); + + return ret; +} + +_static_inline int64_t +coord_co(cube_t c) +{ + // Temp array to store the NEON vector + uint8_t mem[16]; + vst1q_u8(mem, c.corner); + + int i, p; + int64_t ret; + + for (ret = 0, i = 0, p = 1; i < 7; i++, p *= 3) + ret += p * (mem[i] >> _coshift); + + return ret; +} + +_static_inline int64_t +coord_csep(cube_t c) +{ + // Temp array to store the NEON vector + uint8_t mem[16]; + vst1q_u8(mem, c.corner); + + int64_t ret = 0; + int i, p; + for (ret = 0, i = 0, p = 1; i < 7; i++, p *= 2) + ret += p * ((mem[i] & _csepbit) >> 2); + + return ret; + return 0; +} + +_static_inline int64_t +coord_cocsep(cube_t c) +{ + return (coord_co(c) << 7) + coord_csep(c); +} + +_static_inline int64_t +coord_eo(cube_t c) +{ + int64_t ret = 0; + int64_t p = 1; + + // Temp array to store the NEON vector + uint8_t mem[16]; + vst1q_u8(mem, c.edge); + + for (int i = 1; i < 12; i++, p *= 2) + { + ret += p * (mem[i] >> _eoshift); + } + + return ret; +} + +_static_inline int64_t +coord_esep(cube_t c) +{ + int64_t i, j, jj, k, l, ret1, ret2, bit1, bit2, is1; + + // Temp array to store the NEON vector + uint8_t mem[16]; + vst1q_u8(mem, c.edge); + + for (i = 0, j = 0, k = 4, l = 4, ret1 = 0, ret2 = 0; i < 12; i++) + { + bit1 = (mem[i] & _esepbit1) >> 2; + bit2 = (mem[i] & _esepbit2) >> 3; + is1 = (1 - bit2) * bit1; + + ret1 += bit2 * binomial[11 - i][k]; + k -= bit2; + + jj = j < 8; + ret2 += jj * is1 * binomial[7 - (j * jj)][l]; + l -= is1; + j += (1 - bit2); + } + + return ret1 * 70 + ret2; +} + +_static_inline void +copy_corners(cube_t *dst, cube_t src) +{ + dst->corner = src.corner; +} + +_static_inline void +copy_edges(cube_t *dst, cube_t src) +{ + dst->edge = src.edge; +} + +_static_inline void +set_eo(cube_t *cube, int64_t eo) +{ + // Temp array to store the NEON vector + uint8_t mem[16]; + vst1q_u8(mem, cube->edge); + uint8_t i, sum, flip; + + for (sum = 0, i = 1; i < 12; i++, eo >>= 1) + { + flip = eo % 2; + sum += flip; + mem[i] = (mem[i] & ~_eobit) | (_eobit * flip); + } + mem[0] = (mem[0] & ~_eobit) | (_eobit * (sum % 2)); + + // Copy the results back to the NEON vector + cube->edge = vld1q_u8(mem); + return; +} + +_static_inline cube_t +invcoord_esep(int64_t esep) +{ + cube_t ret; + int64_t bit1, bit2, i, j, jj, k, l, s, v, w, is1, set1, set2; + uint8_t slice[3] = {0}; + + ret = solved; + uint8_t mem[16]; + set1 = esep % 70; + set2 = esep / 70; + + for (i = 0, j = 0, k = 4, l = 4; i < 12; i++) + { + v = binomial[11 - i][k]; + jj = j < 8; + w = jj * binomial[7 - (j * jj)][l]; + bit2 = set2 >= v; + bit1 = set1 >= w; + is1 = (1 - bit2) * bit1; + + set2 -= bit2 * v; + k -= bit2; + set1 -= is1 * w; + l -= is1; + j += (1 - bit2); + s = 2 * bit2 + (1 - bit2) * bit1; + + mem[i] = (slice[s]++) | (uint8_t)(s << 2); + } + + ret.edge = vld1q_u8(mem); + return ret; +} diff --git a/src/arch/portable.h b/src/arch/portable.h new file mode 100644 index 0000000..b8330af --- /dev/null +++ b/src/arch/portable.h @@ -0,0 +1,272 @@ +#define static_cube(c_ufr, c_ubl, c_dfl, c_dbr, c_ufl, c_ubr, c_dfr, c_dbl, \ + e_uf, e_ub, e_db, e_df, e_ur, e_ul, e_dl, e_dr, e_fr, e_fl, e_bl, e_br) \ + ((cube_t) { \ + .corner = { c_ufr, c_ubl, c_dfl, c_dbr, c_ufl, c_ubr, c_dfr, c_dbl }, \ + .edge = { e_uf, e_ub, e_db, e_df, e_ur, e_ul, \ + e_dl, e_dr, e_fr, e_fl, e_bl, e_br } }) +#define zero static_cube( \ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) +#define solved static_cube( \ + 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) + +_static void +pieces(cube_t *cube, uint8_t c[static 8], uint8_t e[static 12]) +{ + memcpy(c, cube->corner, 8); + memcpy(e, cube->edge, 12); +} + +_static_inline bool +equal(cube_t c1, cube_t c2) +{ + uint8_t i; + bool ret; + + ret = true; + for (i = 0; i < 8; i++) + ret = ret && c1.corner[i] == c2.corner[i]; + for (i = 0; i < 12; i++) + ret = ret && c1.edge[i] == c2.edge[i]; + + return ret; +} + +_static_inline cube_t +invertco(cube_t c) +{ + uint8_t i, piece, orien; + cube_t ret; + + ret = c; + for (i = 0; i < 8; i++) { + piece = c.corner[i]; + orien = ((piece << 1) | (piece >> 1)) & _cobits2; + ret.corner[i] = (piece & _pbits) | orien; + } + + return ret; +} + +_static_inline void +compose_edges_inplace(cube_t c1, cube_t c2, cube_t *ret) +{ + uint8_t i, piece1, piece2, p, orien; + + for (i = 0; i < 12; i++) { + piece2 = c2.edge[i]; + p = piece2 & _pbits; + piece1 = c1.edge[p]; + orien = (piece2 ^ piece1) & _eobit; + ret->edge[i] = (piece1 & _pbits) | orien; + } +} + +_static_inline void +compose_corners_inplace(cube_t c1, cube_t c2, cube_t *ret) +{ + uint8_t i, piece1, piece2, p, orien, aux, auy; + + for (i = 0; i < 8; i++) { + piece2 = c2.corner[i]; + p = piece2 & _pbits; + piece1 = c1.corner[p]; + aux = (piece2 & _cobits) + (piece1 & _cobits); + auy = (aux + _ctwist_cw) >> 2; + orien = (aux + auy) & _cobits2; + ret->corner[i] = (piece1 & _pbits) | orien; + } +} + +_static_inline cube_t +compose_edges(cube_t c1, cube_t c2) +{ + cube_t ret = zero; + + compose_edges_inplace(c1, c2, &ret); + + return ret; +} + +_static_inline cube_t +compose_corners(cube_t c1, cube_t c2) +{ + cube_t ret = zero; + + compose_corners_inplace(c1, c2, &ret); + + return ret; +} + +_static_inline cube_t +compose(cube_t c1, cube_t c2) +{ + cube_t ret = zero; + + compose_edges_inplace(c1, c2, &ret); + compose_corners_inplace(c1, c2, &ret); + + return ret; +} + +cube_t +inverse(cube_t cube) +{ + uint8_t i, piece, orien; + cube_t ret; + + for (i = 0; i < 12; i++) { + piece = cube.edge[i]; + orien = piece & _eobit; + ret.edge[piece & _pbits] = i | orien; + } + + for (i = 0; i < 8; i++) { + piece = cube.corner[i]; + orien = ((piece << 1) | (piece >> 1)) & _cobits2; + ret.corner[piece & _pbits] = i | orien; + } + + return ret; +} + +_static_inline int64_t +coord_co(cube_t c) +{ + int i, p; + int64_t ret; + + for (ret = 0, i = 0, p = 1; i < 7; i++, p *= 3) + ret += p * (c.corner[i] >> _coshift); + + return ret; +} + +/* +For corner separation, we consider the axis (a.k.a. tetrad) each +corner belongs to as 0 or 1 and we translate this sequence into binary. +Ignoring the last bit, we have a value up to 2^7, but not all values are +possible. Encoding this as a number from 0 to C(8,4) would save about 40% +of space, but we are not going to use this coordinate in large tables. +*/ +_static_inline int64_t +coord_csep(cube_t c) +{ + int i, p; + int64_t ret; + + for (ret = 0, i = 0, p = 1; i < 7; i++, p *= 2) + ret += p * ((c.corner[i] & _csepbit) >> 2); + + return ret; +} + +_static_inline int64_t +coord_cocsep(cube_t c) +{ + return (coord_co(c) << 7) + coord_csep(c); +} + +_static_inline int64_t +coord_eo(cube_t c) +{ + int i, p; + int64_t ret; + + for (ret = 0, i = 1, p = 1; i < 12; i++, p *= 2) + ret += p * (c.edge[i] >> _eoshift); + + return ret; +} + +/* +We encode the edge separation as a number from 0 to C(12,4)*C(8,4). +It can be seen as the composition of two "subset index" coordinates. +*/ +_static_inline int64_t +coord_esep(cube_t c) +{ + int64_t i, j, jj, k, l, ret1, ret2, bit1, bit2, is1; + + for (i = 0, j = 0, k = 4, l = 4, ret1 = 0, ret2 = 0; i < 12; i++) { + /* Simple version: + if (c.edge[i] & _esepbit2) { + ret1 += binomial[11-i][k--]; + } else { + if (c.edge[i] & _esepbit1) + ret2 += binomial[7-j][l--]; + j++; + } + */ + + bit1 = (c.edge[i] & _esepbit1) >> 2; + bit2 = (c.edge[i] & _esepbit2) >> 3; + is1 = (1 - bit2) * bit1; + + ret1 += bit2 * binomial[11-i][k]; + k -= bit2; + + jj = j < 8; + ret2 += jj * is1 * binomial[7-(j*jj)][l]; + l -= is1; + j += (1-bit2); + } + + return ret1 * 70 + ret2; +} + +_static_inline void +copy_corners(cube_t *dest, cube_t src) +{ + memcpy(&dest->corner, src.corner, sizeof(src.corner)); +} + +_static_inline void +copy_edges(cube_t *dest, cube_t src) +{ + memcpy(&dest->edge, src.edge, sizeof(src.edge)); +} + +_static_inline void +set_eo(cube_t *cube, int64_t eo) +{ + uint8_t i, sum, flip; + + for (sum = 0, i = 1; i < 12; i++, eo >>= 1) { + flip = eo % 2; + sum += flip; + cube->edge[i] = (cube->edge[i] & ~_eobit) | (_eobit * flip); + } + cube->edge[0] = (cube->edge[0] & ~_eobit) | (_eobit * (sum % 2)); +} + +_static_inline cube_t +invcoord_esep(int64_t esep) +{ + cube_t ret; + int64_t bit1, bit2, i, j, jj, k, l, s, v, w, is1, set1, set2; + uint8_t slice[3] = {0}; + + ret = solved; + set1 = esep % 70; + set2 = esep / 70; + + for (i = 0, j = 0, k = 4, l = 4; i < 12; i++) { + v = binomial[11-i][k]; + jj = j < 8; + w = jj * binomial[7-(j*jj)][l]; + bit2 = set2 >= v; + bit1 = set1 >= w; + is1 = (1 - bit2) * bit1; + + set2 -= bit2 * v; + k -= bit2; + set1 -= is1 * w; + l -= is1; + j += (1-bit2); + s = 2*bit2 + (1-bit2)*bit1; + + ret.edge[i] = (slice[s]++) | (uint8_t)(s << 2); + } + + return ret; +} diff --git a/src/constant_cubes.h b/src/constant_cubes.h deleted file mode 100644 index b7748d8..0000000 --- a/src/constant_cubes.h +++ /dev/null @@ -1,229 +0,0 @@ -#define _move_cube_U static_cube( \ - 5, 4, 2, 3, 0, 1, 6, 7, 4, 5, 2, 3, 1, 0, 6, 7, 8, 9, 10, 11) -#define _move_cube_U2 static_cube( \ - 1, 0, 2, 3, 5, 4, 6, 7, 1, 0, 2, 3, 5, 4, 6, 7, 8, 9, 10, 11) -#define _move_cube_U3 static_cube( \ - 4, 5, 2, 3, 1, 0, 6, 7, 5, 4, 2, 3, 0, 1, 6, 7, 8, 9, 10, 11) -#define _move_cube_D static_cube( \ - 0, 1, 7, 6, 4, 5, 2, 3, 0, 1, 7, 6, 4, 5, 2, 3, 8, 9, 10, 11) -#define _move_cube_D2 static_cube( \ - 0, 1, 3, 2, 4, 5, 7, 6, 0, 1, 3, 2, 4, 5, 7, 6, 8, 9, 10, 11) -#define _move_cube_D3 static_cube( \ - 0, 1, 6, 7, 4, 5, 3, 2, 0, 1, 6, 7, 4, 5, 3, 2, 8, 9, 10, 11) -#define _move_cube_R static_cube( \ - 70, 1, 2, 69, 4, 32, 35, 7, 0, 1, 2, 3, 8, 5, 6, 11, 7, 9, 10, 4) -#define _move_cube_R2 static_cube( \ - 3, 1, 2, 0, 4, 6, 5, 7, 0, 1, 2, 3, 7, 5, 6, 4, 11, 9, 10, 8) -#define _move_cube_R3 static_cube( \ - 69, 1, 2, 70, 4, 35, 32, 7, 0, 1, 2, 3, 11, 5, 6, 8, 4, 9, 10, 7) -#define _move_cube_L static_cube( \ - 0, 71, 68, 3, 33, 5, 6, 34, 0, 1, 2, 3, 4, 10, 9, 7, 8, 5, 6, 11) -#define _move_cube_L2 static_cube( \ - 0, 2, 1, 3, 7, 5, 6, 4, 0, 1, 2, 3, 4, 6, 5, 7, 8, 10, 9, 11) -#define _move_cube_L3 static_cube( \ - 0, 68, 71, 3, 34, 5, 6, 33, 0, 1, 2, 3, 4, 9, 10, 7, 8, 6, 5, 11) -#define _move_cube_F static_cube( \ - 36, 1, 38, 3, 66, 5, 64, 7, 25, 1, 2, 24, 4, 5, 6, 7, 16, 19, 10, 11) -#define _move_cube_F2 static_cube( \ - 2, 1, 0, 3, 6, 5, 4, 7, 3, 1, 2, 0, 4, 5, 6, 7, 9, 8, 10, 11) -#define _move_cube_F3 static_cube( \ - 38, 1, 36, 3, 64, 5, 66, 7, 24, 1, 2, 25, 4, 5, 6, 7, 19, 16, 10, 11) -#define _move_cube_B static_cube( \ - 0, 37, 2, 39, 4, 67, 6, 65, 0, 27, 26, 3, 4, 5, 6, 7, 8, 9, 17, 18) -#define _move_cube_B2 static_cube( \ - 0, 3, 2, 1, 4, 7, 6, 5, 0, 2, 1, 3, 4, 5, 6, 7, 8, 9, 11, 10) -#define _move_cube_B3 static_cube( \ - 0, 39, 2, 37, 4, 65, 6, 67, 0, 26, 27, 3, 4, 5, 6, 7, 8, 9, 18, 17) - -#define _trans_cube_UFr static_cube( \ - 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) -#define _trans_cube_UFr_inverse static_cube( \ - 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) -#define _trans_cube_ULr static_cube( \ - 4, 5, 7, 6, 1, 0, 2, 3, 5, 4, 7, 6, 0, 1, 2, 3, 25, 26, 27, 24) -#define _trans_cube_ULr_inverse static_cube( \ - 5, 4, 6, 7, 0, 1, 3, 2, 4, 5, 6, 7, 1, 0, 3, 2, 27, 24, 25, 26) -#define _trans_cube_UBr static_cube( \ - 1, 0, 3, 2, 5, 4, 7, 6, 1, 0, 3, 2, 5, 4, 7, 6, 10, 11, 8, 9) -#define _trans_cube_UBr_inverse static_cube( \ - 1, 0, 3, 2, 5, 4, 7, 6, 1, 0, 3, 2, 5, 4, 7, 6, 10, 11, 8, 9) -#define _trans_cube_URr static_cube( \ - 5, 4, 6, 7, 0, 1, 3, 2, 4, 5, 6, 7, 1, 0, 3, 2, 27, 24, 25, 26) -#define _trans_cube_URr_inverse static_cube( \ - 4, 5, 7, 6, 1, 0, 2, 3, 5, 4, 7, 6, 0, 1, 2, 3, 25, 26, 27, 24) -#define _trans_cube_DFr static_cube( \ - 2, 3, 0, 1, 6, 7, 4, 5, 3, 2, 1, 0, 6, 7, 4, 5, 9, 8, 11, 10) -#define _trans_cube_DFr_inverse static_cube( \ - 2, 3, 0, 1, 6, 7, 4, 5, 3, 2, 1, 0, 6, 7, 4, 5, 9, 8, 11, 10) -#define _trans_cube_DLr static_cube( \ - 7, 6, 4, 5, 2, 3, 1, 0, 6, 7, 4, 5, 2, 3, 0, 1, 26, 25, 24, 27) -#define _trans_cube_DLr_inverse static_cube( \ - 7, 6, 4, 5, 2, 3, 1, 0, 6, 7, 4, 5, 2, 3, 0, 1, 26, 25, 24, 27) -#define _trans_cube_DBr static_cube( \ - 3, 2, 1, 0, 7, 6, 5, 4, 2, 3, 0, 1, 7, 6, 5, 4, 11, 10, 9, 8) -#define _trans_cube_DBr_inverse static_cube( \ - 3, 2, 1, 0, 7, 6, 5, 4, 2, 3, 0, 1, 7, 6, 5, 4, 11, 10, 9, 8) -#define _trans_cube_DRr static_cube( \ - 6, 7, 5, 4, 3, 2, 0, 1, 7, 6, 5, 4, 3, 2, 1, 0, 24, 27, 26, 25) -#define _trans_cube_DRr_inverse static_cube( \ - 6, 7, 5, 4, 3, 2, 0, 1, 7, 6, 5, 4, 3, 2, 1, 0, 24, 27, 26, 25) -#define _trans_cube_RUr static_cube( \ - 64, 67, 65, 66, 37, 38, 36, 39, 20, 23, 22, 21, 24, 27, 26, 25, 0, 1, 2, 3) -#define _trans_cube_RUr_inverse static_cube( \ - 32, 34, 35, 33, 70, 68, 69, 71, 8, 9, 10, 11, 16, 19, 18, 17, 20, 23, 22, 21) -#define _trans_cube_RFr static_cube( \ - 38, 37, 36, 39, 64, 67, 66, 65, 24, 27, 26, 25, 23, 20, 21, 22, 19, 16, 17, 18) -#define _trans_cube_RFr_inverse static_cube( \ - 36, 39, 38, 37, 66, 65, 64, 67, 25, 26, 27, 24, 21, 22, 23, 20, 16, 19, 18, 17) -#define _trans_cube_RDr static_cube( \ - 67, 64, 66, 65, 38, 37, 39, 36, 23, 20, 21, 22, 27, 24, 25, 26, 2, 3, 0, 1) -#define _trans_cube_RDr_inverse static_cube( \ - 33, 35, 34, 32, 71, 69, 68, 70, 10, 11, 8, 9, 17, 18, 19, 16, 21, 22, 23, 20) -#define _trans_cube_RBr static_cube( \ - 37, 38, 39, 36, 67, 64, 65, 66, 27, 24, 25, 26, 20, 23, 22, 21, 17, 18, 19, 16) -#define _trans_cube_RBr_inverse static_cube( \ - 37, 38, 39, 36, 67, 64, 65, 66, 27, 24, 25, 26, 20, 23, 22, 21, 17, 18, 19, 16) -#define _trans_cube_LUr static_cube( \ - 65, 66, 64, 67, 36, 39, 37, 38, 21, 22, 23, 20, 26, 25, 24, 27, 1, 0, 3, 2) -#define _trans_cube_LUr_inverse static_cube( \ - 34, 32, 33, 35, 68, 70, 71, 69, 9, 8, 11, 10, 19, 16, 17, 18, 22, 21, 20, 23) -#define _trans_cube_LFr static_cube( \ - 36, 39, 38, 37, 66, 65, 64, 67, 25, 26, 27, 24, 21, 22, 23, 20, 16, 19, 18, 17) -#define _trans_cube_LFr_inverse static_cube( \ - 38, 37, 36, 39, 64, 67, 66, 65, 24, 27, 26, 25, 23, 20, 21, 22, 19, 16, 17, 18) -#define _trans_cube_LDr static_cube( \ - 66, 65, 67, 64, 39, 36, 38, 37, 22, 21, 20, 23, 25, 26, 27, 24, 3, 2, 1, 0) -#define _trans_cube_LDr_inverse static_cube( \ - 35, 33, 32, 34, 69, 71, 70, 68, 11, 10, 9, 8, 18, 17, 16, 19, 23, 20, 21, 22) -#define _trans_cube_LBr static_cube( \ - 39, 36, 37, 38, 65, 66, 67, 64, 26, 25, 24, 27, 22, 21, 20, 23, 18, 17, 16, 19) -#define _trans_cube_LBr_inverse static_cube( \ - 39, 36, 37, 38, 65, 66, 67, 64, 26, 25, 24, 27, 22, 21, 20, 23, 18, 17, 16, 19) -#define _trans_cube_FUr static_cube( \ - 68, 70, 69, 71, 32, 34, 33, 35, 16, 19, 18, 17, 9, 8, 11, 10, 5, 4, 7, 6) -#define _trans_cube_FUr_inverse static_cube( \ - 68, 70, 69, 71, 32, 34, 33, 35, 16, 19, 18, 17, 9, 8, 11, 10, 5, 4, 7, 6) -#define _trans_cube_FRr static_cube( \ - 32, 34, 35, 33, 70, 68, 69, 71, 8, 9, 10, 11, 16, 19, 18, 17, 20, 23, 22, 21) -#define _trans_cube_FRr_inverse static_cube( \ - 64, 67, 65, 66, 37, 38, 36, 39, 20, 23, 22, 21, 24, 27, 26, 25, 0, 1, 2, 3) -#define _trans_cube_FDr static_cube( \ - 70, 68, 71, 69, 34, 32, 35, 33, 19, 16, 17, 18, 8, 9, 10, 11, 7, 6, 5, 4) -#define _trans_cube_FDr_inverse static_cube( \ - 69, 71, 68, 70, 33, 35, 32, 34, 17, 18, 19, 16, 11, 10, 9, 8, 4, 5, 6, 7) -#define _trans_cube_FLr static_cube( \ - 34, 32, 33, 35, 68, 70, 71, 69, 9, 8, 11, 10, 19, 16, 17, 18, 22, 21, 20, 23) -#define _trans_cube_FLr_inverse static_cube( \ - 65, 66, 64, 67, 36, 39, 37, 38, 21, 22, 23, 20, 26, 25, 24, 27, 1, 0, 3, 2) -#define _trans_cube_BUr static_cube( \ - 69, 71, 68, 70, 33, 35, 32, 34, 17, 18, 19, 16, 11, 10, 9, 8, 4, 5, 6, 7) -#define _trans_cube_BUr_inverse static_cube( \ - 70, 68, 71, 69, 34, 32, 35, 33, 19, 16, 17, 18, 8, 9, 10, 11, 7, 6, 5, 4) -#define _trans_cube_BRr static_cube( \ - 35, 33, 32, 34, 69, 71, 70, 68, 11, 10, 9, 8, 18, 17, 16, 19, 23, 20, 21, 22) -#define _trans_cube_BRr_inverse static_cube( \ - 66, 65, 67, 64, 39, 36, 38, 37, 22, 21, 20, 23, 25, 26, 27, 24, 3, 2, 1, 0) -#define _trans_cube_BDr static_cube( \ - 71, 69, 70, 68, 35, 33, 34, 32, 18, 17, 16, 19, 10, 11, 8, 9, 6, 7, 4, 5) -#define _trans_cube_BDr_inverse static_cube( \ - 71, 69, 70, 68, 35, 33, 34, 32, 18, 17, 16, 19, 10, 11, 8, 9, 6, 7, 4, 5) -#define _trans_cube_BLr static_cube( \ - 33, 35, 34, 32, 71, 69, 68, 70, 10, 11, 8, 9, 17, 18, 19, 16, 21, 22, 23, 20) -#define _trans_cube_BLr_inverse static_cube( \ - 67, 64, 66, 65, 38, 37, 39, 36, 23, 20, 21, 22, 27, 24, 25, 26, 2, 3, 0, 1) -#define _trans_cube_UFm static_cube( \ - 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 2, 3, 5, 4, 7, 6, 9, 8, 11, 10) -#define _trans_cube_UFm_inverse static_cube( \ - 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 2, 3, 5, 4, 7, 6, 9, 8, 11, 10) -#define _trans_cube_ULm static_cube( \ - 0, 1, 3, 2, 5, 4, 6, 7, 4, 5, 6, 7, 0, 1, 2, 3, 24, 27, 26, 25) -#define _trans_cube_ULm_inverse static_cube( \ - 0, 1, 3, 2, 5, 4, 6, 7, 4, 5, 6, 7, 0, 1, 2, 3, 24, 27, 26, 25) -#define _trans_cube_UBm static_cube( \ - 5, 4, 7, 6, 1, 0, 3, 2, 1, 0, 3, 2, 4, 5, 6, 7, 11, 10, 9, 8) -#define _trans_cube_UBm_inverse static_cube( \ - 5, 4, 7, 6, 1, 0, 3, 2, 1, 0, 3, 2, 4, 5, 6, 7, 11, 10, 9, 8) -#define _trans_cube_URm static_cube( \ - 1, 0, 2, 3, 4, 5, 7, 6, 5, 4, 7, 6, 1, 0, 3, 2, 26, 25, 24, 27) -#define _trans_cube_URm_inverse static_cube( \ - 1, 0, 2, 3, 4, 5, 7, 6, 5, 4, 7, 6, 1, 0, 3, 2, 26, 25, 24, 27) -#define _trans_cube_DFm static_cube( \ - 6, 7, 4, 5, 2, 3, 0, 1, 3, 2, 1, 0, 7, 6, 5, 4, 8, 9, 10, 11) -#define _trans_cube_DFm_inverse static_cube( \ - 6, 7, 4, 5, 2, 3, 0, 1, 3, 2, 1, 0, 7, 6, 5, 4, 8, 9, 10, 11) -#define _trans_cube_DLm static_cube( \ - 3, 2, 0, 1, 6, 7, 5, 4, 7, 6, 5, 4, 2, 3, 0, 1, 27, 24, 25, 26) -#define _trans_cube_DLm_inverse static_cube( \ - 2, 3, 1, 0, 7, 6, 4, 5, 6, 7, 4, 5, 3, 2, 1, 0, 25, 26, 27, 24) -#define _trans_cube_DBm static_cube( \ - 7, 6, 5, 4, 3, 2, 1, 0, 2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9) -#define _trans_cube_DBm_inverse static_cube( \ - 7, 6, 5, 4, 3, 2, 1, 0, 2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9) -#define _trans_cube_DRm static_cube( \ - 2, 3, 1, 0, 7, 6, 4, 5, 6, 7, 4, 5, 3, 2, 1, 0, 25, 26, 27, 24) -#define _trans_cube_DRm_inverse static_cube( \ - 3, 2, 0, 1, 6, 7, 5, 4, 7, 6, 5, 4, 2, 3, 0, 1, 27, 24, 25, 26) -#define _trans_cube_RUm static_cube( \ - 68, 71, 69, 70, 33, 34, 32, 35, 21, 22, 23, 20, 25, 26, 27, 24, 0, 1, 2, 3) -#define _trans_cube_RUm_inverse static_cube( \ - 70, 68, 69, 71, 32, 34, 35, 33, 8, 9, 10, 11, 19, 16, 17, 18, 23, 20, 21, 22) -#define _trans_cube_RFm static_cube( \ - 34, 33, 32, 35, 68, 71, 70, 69, 25, 26, 27, 24, 22, 21, 20, 23, 19, 16, 17, 18) -#define _trans_cube_RFm_inverse static_cube( \ - 66, 65, 64, 67, 36, 39, 38, 37, 25, 26, 27, 24, 22, 21, 20, 23, 19, 16, 17, 18) -#define _trans_cube_RDm static_cube( \ - 71, 68, 70, 69, 34, 33, 35, 32, 22, 21, 20, 23, 26, 25, 24, 27, 2, 3, 0, 1) -#define _trans_cube_RDm_inverse static_cube( \ - 71, 69, 68, 70, 33, 35, 34, 32, 10, 11, 8, 9, 18, 17, 16, 19, 22, 21, 20, 23) -#define _trans_cube_RBm static_cube( \ - 33, 34, 35, 32, 71, 68, 69, 70, 26, 25, 24, 27, 21, 22, 23, 20, 17, 18, 19, 16) -#define _trans_cube_RBm_inverse static_cube( \ - 67, 64, 65, 66, 37, 38, 39, 36, 27, 24, 25, 26, 23, 20, 21, 22, 18, 17, 16, 19) -#define _trans_cube_LUm static_cube( \ - 69, 70, 68, 71, 32, 35, 33, 34, 20, 23, 22, 21, 27, 24, 25, 26, 1, 0, 3, 2) -#define _trans_cube_LUm_inverse static_cube( \ - 68, 70, 71, 69, 34, 32, 33, 35, 9, 8, 11, 10, 16, 19, 18, 17, 21, 22, 23, 20) -#define _trans_cube_LFm static_cube( \ - 32, 35, 34, 33, 70, 69, 68, 71, 24, 27, 26, 25, 20, 23, 22, 21, 16, 19, 18, 17) -#define _trans_cube_LFm_inverse static_cube( \ - 64, 67, 66, 65, 38, 37, 36, 39, 24, 27, 26, 25, 20, 23, 22, 21, 16, 19, 18, 17) -#define _trans_cube_LDm static_cube( \ - 70, 69, 71, 68, 35, 32, 34, 33, 23, 20, 21, 22, 24, 27, 26, 25, 3, 2, 1, 0) -#define _trans_cube_LDm_inverse static_cube( \ - 69, 71, 70, 68, 35, 33, 32, 34, 11, 10, 9, 8, 17, 18, 19, 16, 20, 23, 22, 21) -#define _trans_cube_LBm static_cube( \ - 35, 32, 33, 34, 69, 70, 71, 68, 27, 24, 25, 26, 23, 20, 21, 22, 18, 17, 16, 19) -#define _trans_cube_LBm_inverse static_cube( \ - 65, 66, 67, 64, 39, 36, 37, 38, 26, 25, 24, 27, 21, 22, 23, 20, 17, 18, 19, 16) -#define _trans_cube_FUm static_cube( \ - 64, 66, 65, 67, 36, 38, 37, 39, 16, 19, 18, 17, 8, 9, 10, 11, 4, 5, 6, 7) -#define _trans_cube_FUm_inverse static_cube( \ - 32, 34, 33, 35, 68, 70, 69, 71, 16, 19, 18, 17, 8, 9, 10, 11, 4, 5, 6, 7) -#define _trans_cube_FRm static_cube( \ - 36, 38, 39, 37, 66, 64, 65, 67, 9, 8, 11, 10, 16, 19, 18, 17, 21, 22, 23, 20) -#define _trans_cube_FRm_inverse static_cube( \ - 37, 38, 36, 39, 64, 67, 65, 66, 20, 23, 22, 21, 27, 24, 25, 26, 1, 0, 3, 2) -#define _trans_cube_FDm static_cube( \ - 66, 64, 67, 65, 38, 36, 39, 37, 19, 16, 17, 18, 9, 8, 11, 10, 6, 7, 4, 5) -#define _trans_cube_FDm_inverse static_cube( \ - 33, 35, 32, 34, 69, 71, 68, 70, 17, 18, 19, 16, 10, 11, 8, 9, 5, 4, 7, 6) -#define _trans_cube_FLm static_cube( \ - 38, 36, 37, 39, 64, 66, 67, 65, 8, 9, 10, 11, 19, 16, 17, 18, 23, 20, 21, 22) -#define _trans_cube_FLm_inverse static_cube( \ - 36, 39, 37, 38, 65, 66, 64, 67, 21, 22, 23, 20, 25, 26, 27, 24, 0, 1, 2, 3) -#define _trans_cube_BUm static_cube( \ - 65, 67, 64, 66, 37, 39, 36, 38, 17, 18, 19, 16, 10, 11, 8, 9, 5, 4, 7, 6) -#define _trans_cube_BUm_inverse static_cube( \ - 34, 32, 35, 33, 70, 68, 71, 69, 19, 16, 17, 18, 9, 8, 11, 10, 6, 7, 4, 5) -#define _trans_cube_BRm static_cube( \ - 39, 37, 36, 38, 65, 67, 66, 64, 10, 11, 8, 9, 18, 17, 16, 19, 22, 21, 20, 23) -#define _trans_cube_BRm_inverse static_cube( \ - 39, 36, 38, 37, 66, 65, 67, 64, 22, 21, 20, 23, 26, 25, 24, 27, 2, 3, 0, 1) -#define _trans_cube_BDm static_cube( \ - 67, 65, 66, 64, 39, 37, 38, 36, 18, 17, 16, 19, 11, 10, 9, 8, 7, 6, 5, 4) -#define _trans_cube_BDm_inverse static_cube( \ - 35, 33, 34, 32, 71, 69, 70, 68, 18, 17, 16, 19, 11, 10, 9, 8, 7, 6, 5, 4) -#define _trans_cube_BLm static_cube( \ - 37, 39, 38, 36, 67, 65, 64, 66, 11, 10, 9, 8, 17, 18, 19, 16, 20, 23, 22, 21) -#define _trans_cube_BLm_inverse static_cube( \ - 38, 37, 39, 36, 67, 64, 66, 65, 23, 20, 21, 22, 24, 27, 26, 25, 3, 2, 1, 0) diff --git a/src/constants.h b/src/constants.h deleted file mode 100644 index 52e2810..0000000 --- a/src/constants.h +++ /dev/null @@ -1,295 +0,0 @@ -#define _bit_u8(i) (UINT8_C(1) << (uint8_t)(i)) -#define _bit_u32(i) (UINT32_C(1) << (uint32_t)(i)) -#define _bit_u64(i) (UINT64_C(1) << (uint64_t)(i)) - -#define _max_factorial INT64_C(12) - -#define _2p11 INT64_C(2048) -#define _2p12 INT64_C(4096) -#define _3p7 INT64_C(2187) -#define _3p8 INT64_C(6561) -#define _12f INT64_C(479001600) -#define _8f INT64_C(40320) -#define _12c4 INT64_C(495) -#define _8c4 INT64_C(70) - -_static int64_t binomial[12][12] = { - {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - {1, 3, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0}, - {1, 4, 6, 4, 1, 0, 0, 0, 0, 0, 0, 0}, - {1, 5, 10, 10, 5, 1, 0, 0, 0, 0, 0, 0}, - {1, 6, 15, 20, 15, 6, 1, 0, 0, 0, 0, 0}, - {1, 7, 21, 35, 35, 21, 7, 1, 0, 0, 0, 0}, - {1, 8, 28, 56, 70, 56, 28, 8, 1, 0, 0, 0}, - {1, 9, 36, 84, 126, 126, 84, 36, 9, 1, 0, 0}, - {1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1, 0}, - {1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1}, -}; - -#define _move_U UINT8_C(0) -#define _move_U2 UINT8_C(1) -#define _move_U3 UINT8_C(2) -#define _move_D UINT8_C(3) -#define _move_D2 UINT8_C(4) -#define _move_D3 UINT8_C(5) -#define _move_R UINT8_C(6) -#define _move_R2 UINT8_C(7) -#define _move_R3 UINT8_C(8) -#define _move_L UINT8_C(9) -#define _move_L2 UINT8_C(10) -#define _move_L3 UINT8_C(11) -#define _move_F UINT8_C(12) -#define _move_F2 UINT8_C(13) -#define _move_F3 UINT8_C(14) -#define _move_B UINT8_C(15) -#define _move_B2 UINT8_C(16) -#define _move_B3 UINT8_C(17) - -#define _trans_UFr UINT8_C(0) -#define _trans_ULr UINT8_C(1) -#define _trans_UBr UINT8_C(2) -#define _trans_URr UINT8_C(3) -#define _trans_DFr UINT8_C(4) -#define _trans_DLr UINT8_C(5) -#define _trans_DBr UINT8_C(6) -#define _trans_DRr UINT8_C(7) -#define _trans_RUr UINT8_C(8) -#define _trans_RFr UINT8_C(9) -#define _trans_RDr UINT8_C(10) -#define _trans_RBr UINT8_C(11) -#define _trans_LUr UINT8_C(12) -#define _trans_LFr UINT8_C(13) -#define _trans_LDr UINT8_C(14) -#define _trans_LBr UINT8_C(15) -#define _trans_FUr UINT8_C(16) -#define _trans_FRr UINT8_C(17) -#define _trans_FDr UINT8_C(18) -#define _trans_FLr UINT8_C(19) -#define _trans_BUr UINT8_C(20) -#define _trans_BRr UINT8_C(21) -#define _trans_BDr UINT8_C(22) -#define _trans_BLr UINT8_C(23) - -#define _trans_UFm UINT8_C(24) -#define _trans_ULm UINT8_C(25) -#define _trans_UBm UINT8_C(26) -#define _trans_URm UINT8_C(27) -#define _trans_DFm UINT8_C(28) -#define _trans_DLm UINT8_C(29) -#define _trans_DBm UINT8_C(30) -#define _trans_DRm UINT8_C(31) -#define _trans_RUm UINT8_C(32) -#define _trans_RFm UINT8_C(33) -#define _trans_RDm UINT8_C(34) -#define _trans_RBm UINT8_C(35) -#define _trans_LUm UINT8_C(36) -#define _trans_LFm UINT8_C(37) -#define _trans_LDm UINT8_C(38) -#define _trans_LBm UINT8_C(39) -#define _trans_FUm UINT8_C(40) -#define _trans_FRm UINT8_C(41) -#define _trans_FDm UINT8_C(42) -#define _trans_FLm UINT8_C(43) -#define _trans_BUm UINT8_C(44) -#define _trans_BRm UINT8_C(45) -#define _trans_BDm UINT8_C(46) -#define _trans_BLm UINT8_C(47) - -#define _c_ufr UINT8_C(0) -#define _c_ubl UINT8_C(1) -#define _c_dfl UINT8_C(2) -#define _c_dbr UINT8_C(3) -#define _c_ufl UINT8_C(4) -#define _c_ubr UINT8_C(5) -#define _c_dfr UINT8_C(6) -#define _c_dbl UINT8_C(7) - -#define _e_uf UINT8_C(0) -#define _e_ub UINT8_C(1) -#define _e_db UINT8_C(2) -#define _e_df UINT8_C(3) -#define _e_ur UINT8_C(4) -#define _e_ul UINT8_C(5) -#define _e_dl UINT8_C(6) -#define _e_dr UINT8_C(7) -#define _e_fr UINT8_C(8) -#define _e_fl UINT8_C(9) -#define _e_bl UINT8_C(10) -#define _e_br UINT8_C(11) - -#define _eoshift UINT8_C(4) -#define _coshift UINT8_C(5) - -#define _pbits UINT8_C(0xF) -#define _esepbit1 UINT8_C(0x4) -#define _esepbit2 UINT8_C(0x8) -#define _csepbit UINT8_C(0x4) -#define _eobit UINT8_C(0x10) -#define _cobits UINT8_C(0xF0) -#define _cobits2 UINT8_C(0x60) -#define _ctwist_cw UINT8_C(0x20) -#define _ctwist_ccw UINT8_C(0x40) -#define _eflip UINT8_C(0x10) -#define _error UINT8_C(0xFF) - -_static const char *cornerstr[] = { - [_c_ufr] = "UFR", - [_c_ubl] = "UBL", - [_c_dfl] = "DFL", - [_c_dbr] = "DBR", - [_c_ufl] = "UFL", - [_c_ubr] = "UBR", - [_c_dfr] = "DFR", - [_c_dbl] = "DBL" -}; - -_static const char *cornerstralt[] = { - [_c_ufr] = "URF", - [_c_ubl] = "ULB", - [_c_dfl] = "DLF", - [_c_dbr] = "DRB", - [_c_ufl] = "ULF", - [_c_ubr] = "URB", - [_c_dfr] = "DRF", - [_c_dbl] = "DLB" -}; - -_static const char *edgestr[] = { - [_e_uf] = "UF", - [_e_ub] = "UB", - [_e_db] = "DB", - [_e_df] = "DF", - [_e_ur] = "UR", - [_e_ul] = "UL", - [_e_dl] = "DL", - [_e_dr] = "DR", - [_e_fr] = "FR", - [_e_fl] = "FL", - [_e_bl] = "BL", - [_e_br] = "BR" -}; - -_static const char *movestr[] = { - [_move_U] = "U", - [_move_U2] = "U2", - [_move_U3] = "U'", - [_move_D] = "D", - [_move_D2] = "D2", - [_move_D3] = "D'", - [_move_R] = "R", - [_move_R2] = "R2", - [_move_R3] = "R'", - [_move_L] = "L", - [_move_L2] = "L2", - [_move_L3] = "L'", - [_move_F] = "F", - [_move_F2] = "F2", - [_move_F3] = "F'", - [_move_B] = "B", - [_move_B2] = "B2", - [_move_B3] = "B'", -}; - -_static const char *transstr[] = { - [_trans_UFr] = "rotation UF", - [_trans_UFm] = "mirrored UF", - [_trans_ULr] = "rotation UL", - [_trans_ULm] = "mirrored UL", - [_trans_UBr] = "rotation UB", - [_trans_UBm] = "mirrored UB", - [_trans_URr] = "rotation UR", - [_trans_URm] = "mirrored UR", - [_trans_DFr] = "rotation DF", - [_trans_DFm] = "mirrored DF", - [_trans_DLr] = "rotation DL", - [_trans_DLm] = "mirrored DL", - [_trans_DBr] = "rotation DB", - [_trans_DBm] = "mirrored DB", - [_trans_DRr] = "rotation DR", - [_trans_DRm] = "mirrored DR", - [_trans_RUr] = "rotation RU", - [_trans_RUm] = "mirrored RU", - [_trans_RFr] = "rotation RF", - [_trans_RFm] = "mirrored RF", - [_trans_RDr] = "rotation RD", - [_trans_RDm] = "mirrored RD", - [_trans_RBr] = "rotation RB", - [_trans_RBm] = "mirrored RB", - [_trans_LUr] = "rotation LU", - [_trans_LUm] = "mirrored LU", - [_trans_LFr] = "rotation LF", - [_trans_LFm] = "mirrored LF", - [_trans_LDr] = "rotation LD", - [_trans_LDm] = "mirrored LD", - [_trans_LBr] = "rotation LB", - [_trans_LBm] = "mirrored LB", - [_trans_FUr] = "rotation FU", - [_trans_FUm] = "mirrored FU", - [_trans_FRr] = "rotation FR", - [_trans_FRm] = "mirrored FR", - [_trans_FDr] = "rotation FD", - [_trans_FDm] = "mirrored FD", - [_trans_FLr] = "rotation FL", - [_trans_FLm] = "mirrored FL", - [_trans_BUr] = "rotation BU", - [_trans_BUm] = "mirrored BU", - [_trans_BRr] = "rotation BR", - [_trans_BRm] = "mirrored BR", - [_trans_BDr] = "rotation BD", - [_trans_BDm] = "mirrored BD", - [_trans_BLr] = "rotation BL", - [_trans_BLm] = "mirrored BL", -}; - -static uint8_t inverse_trans_table[48] = { - [_trans_UFr] = _trans_UFr, - [_trans_UFm] = _trans_UFm, - [_trans_ULr] = _trans_URr, - [_trans_ULm] = _trans_ULm, - [_trans_UBr] = _trans_UBr, - [_trans_UBm] = _trans_UBm, - [_trans_URr] = _trans_ULr, - [_trans_URm] = _trans_URm, - [_trans_DFr] = _trans_DFr, - [_trans_DFm] = _trans_DFm, - [_trans_DLr] = _trans_DLr, - [_trans_DLm] = _trans_DRm, - [_trans_DBr] = _trans_DBr, - [_trans_DBm] = _trans_DBm, - [_trans_DRr] = _trans_DRr, - [_trans_DRm] = _trans_DLm, - [_trans_RUr] = _trans_FRr, - [_trans_RUm] = _trans_FLm, - [_trans_RFr] = _trans_LFr, - [_trans_RFm] = _trans_RFm, - [_trans_RDr] = _trans_BLr, - [_trans_RDm] = _trans_BRm, - [_trans_RBr] = _trans_RBr, - [_trans_RBm] = _trans_LBm, - [_trans_LUr] = _trans_FLr, - [_trans_LUm] = _trans_FRm, - [_trans_LFr] = _trans_RFr, - [_trans_LFm] = _trans_LFm, - [_trans_LDr] = _trans_BRr, - [_trans_LDm] = _trans_BLm, - [_trans_LBr] = _trans_LBr, - [_trans_LBm] = _trans_RBm, - [_trans_FUr] = _trans_FUr, - [_trans_FUm] = _trans_FUm, - [_trans_FRr] = _trans_RUr, - [_trans_FRm] = _trans_LUm, - [_trans_FDr] = _trans_BUr, - [_trans_FDm] = _trans_BUm, - [_trans_FLr] = _trans_LUr, - [_trans_FLm] = _trans_RUm, - [_trans_BUr] = _trans_FDr, - [_trans_BUm] = _trans_FDm, - [_trans_BRr] = _trans_LDr, - [_trans_BRm] = _trans_RDm, - [_trans_BDr] = _trans_BDr, - [_trans_BDm] = _trans_BDm, - [_trans_BLr] = _trans_RDr, - [_trans_BLm] = _trans_LDm, -}; diff --git a/src/core/constant_cubes.h b/src/core/constant_cubes.h new file mode 100644 index 0000000..b7748d8 --- /dev/null +++ b/src/core/constant_cubes.h @@ -0,0 +1,229 @@ +#define _move_cube_U static_cube( \ + 5, 4, 2, 3, 0, 1, 6, 7, 4, 5, 2, 3, 1, 0, 6, 7, 8, 9, 10, 11) +#define _move_cube_U2 static_cube( \ + 1, 0, 2, 3, 5, 4, 6, 7, 1, 0, 2, 3, 5, 4, 6, 7, 8, 9, 10, 11) +#define _move_cube_U3 static_cube( \ + 4, 5, 2, 3, 1, 0, 6, 7, 5, 4, 2, 3, 0, 1, 6, 7, 8, 9, 10, 11) +#define _move_cube_D static_cube( \ + 0, 1, 7, 6, 4, 5, 2, 3, 0, 1, 7, 6, 4, 5, 2, 3, 8, 9, 10, 11) +#define _move_cube_D2 static_cube( \ + 0, 1, 3, 2, 4, 5, 7, 6, 0, 1, 3, 2, 4, 5, 7, 6, 8, 9, 10, 11) +#define _move_cube_D3 static_cube( \ + 0, 1, 6, 7, 4, 5, 3, 2, 0, 1, 6, 7, 4, 5, 3, 2, 8, 9, 10, 11) +#define _move_cube_R static_cube( \ + 70, 1, 2, 69, 4, 32, 35, 7, 0, 1, 2, 3, 8, 5, 6, 11, 7, 9, 10, 4) +#define _move_cube_R2 static_cube( \ + 3, 1, 2, 0, 4, 6, 5, 7, 0, 1, 2, 3, 7, 5, 6, 4, 11, 9, 10, 8) +#define _move_cube_R3 static_cube( \ + 69, 1, 2, 70, 4, 35, 32, 7, 0, 1, 2, 3, 11, 5, 6, 8, 4, 9, 10, 7) +#define _move_cube_L static_cube( \ + 0, 71, 68, 3, 33, 5, 6, 34, 0, 1, 2, 3, 4, 10, 9, 7, 8, 5, 6, 11) +#define _move_cube_L2 static_cube( \ + 0, 2, 1, 3, 7, 5, 6, 4, 0, 1, 2, 3, 4, 6, 5, 7, 8, 10, 9, 11) +#define _move_cube_L3 static_cube( \ + 0, 68, 71, 3, 34, 5, 6, 33, 0, 1, 2, 3, 4, 9, 10, 7, 8, 6, 5, 11) +#define _move_cube_F static_cube( \ + 36, 1, 38, 3, 66, 5, 64, 7, 25, 1, 2, 24, 4, 5, 6, 7, 16, 19, 10, 11) +#define _move_cube_F2 static_cube( \ + 2, 1, 0, 3, 6, 5, 4, 7, 3, 1, 2, 0, 4, 5, 6, 7, 9, 8, 10, 11) +#define _move_cube_F3 static_cube( \ + 38, 1, 36, 3, 64, 5, 66, 7, 24, 1, 2, 25, 4, 5, 6, 7, 19, 16, 10, 11) +#define _move_cube_B static_cube( \ + 0, 37, 2, 39, 4, 67, 6, 65, 0, 27, 26, 3, 4, 5, 6, 7, 8, 9, 17, 18) +#define _move_cube_B2 static_cube( \ + 0, 3, 2, 1, 4, 7, 6, 5, 0, 2, 1, 3, 4, 5, 6, 7, 8, 9, 11, 10) +#define _move_cube_B3 static_cube( \ + 0, 39, 2, 37, 4, 65, 6, 67, 0, 26, 27, 3, 4, 5, 6, 7, 8, 9, 18, 17) + +#define _trans_cube_UFr static_cube( \ + 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) +#define _trans_cube_UFr_inverse static_cube( \ + 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) +#define _trans_cube_ULr static_cube( \ + 4, 5, 7, 6, 1, 0, 2, 3, 5, 4, 7, 6, 0, 1, 2, 3, 25, 26, 27, 24) +#define _trans_cube_ULr_inverse static_cube( \ + 5, 4, 6, 7, 0, 1, 3, 2, 4, 5, 6, 7, 1, 0, 3, 2, 27, 24, 25, 26) +#define _trans_cube_UBr static_cube( \ + 1, 0, 3, 2, 5, 4, 7, 6, 1, 0, 3, 2, 5, 4, 7, 6, 10, 11, 8, 9) +#define _trans_cube_UBr_inverse static_cube( \ + 1, 0, 3, 2, 5, 4, 7, 6, 1, 0, 3, 2, 5, 4, 7, 6, 10, 11, 8, 9) +#define _trans_cube_URr static_cube( \ + 5, 4, 6, 7, 0, 1, 3, 2, 4, 5, 6, 7, 1, 0, 3, 2, 27, 24, 25, 26) +#define _trans_cube_URr_inverse static_cube( \ + 4, 5, 7, 6, 1, 0, 2, 3, 5, 4, 7, 6, 0, 1, 2, 3, 25, 26, 27, 24) +#define _trans_cube_DFr static_cube( \ + 2, 3, 0, 1, 6, 7, 4, 5, 3, 2, 1, 0, 6, 7, 4, 5, 9, 8, 11, 10) +#define _trans_cube_DFr_inverse static_cube( \ + 2, 3, 0, 1, 6, 7, 4, 5, 3, 2, 1, 0, 6, 7, 4, 5, 9, 8, 11, 10) +#define _trans_cube_DLr static_cube( \ + 7, 6, 4, 5, 2, 3, 1, 0, 6, 7, 4, 5, 2, 3, 0, 1, 26, 25, 24, 27) +#define _trans_cube_DLr_inverse static_cube( \ + 7, 6, 4, 5, 2, 3, 1, 0, 6, 7, 4, 5, 2, 3, 0, 1, 26, 25, 24, 27) +#define _trans_cube_DBr static_cube( \ + 3, 2, 1, 0, 7, 6, 5, 4, 2, 3, 0, 1, 7, 6, 5, 4, 11, 10, 9, 8) +#define _trans_cube_DBr_inverse static_cube( \ + 3, 2, 1, 0, 7, 6, 5, 4, 2, 3, 0, 1, 7, 6, 5, 4, 11, 10, 9, 8) +#define _trans_cube_DRr static_cube( \ + 6, 7, 5, 4, 3, 2, 0, 1, 7, 6, 5, 4, 3, 2, 1, 0, 24, 27, 26, 25) +#define _trans_cube_DRr_inverse static_cube( \ + 6, 7, 5, 4, 3, 2, 0, 1, 7, 6, 5, 4, 3, 2, 1, 0, 24, 27, 26, 25) +#define _trans_cube_RUr static_cube( \ + 64, 67, 65, 66, 37, 38, 36, 39, 20, 23, 22, 21, 24, 27, 26, 25, 0, 1, 2, 3) +#define _trans_cube_RUr_inverse static_cube( \ + 32, 34, 35, 33, 70, 68, 69, 71, 8, 9, 10, 11, 16, 19, 18, 17, 20, 23, 22, 21) +#define _trans_cube_RFr static_cube( \ + 38, 37, 36, 39, 64, 67, 66, 65, 24, 27, 26, 25, 23, 20, 21, 22, 19, 16, 17, 18) +#define _trans_cube_RFr_inverse static_cube( \ + 36, 39, 38, 37, 66, 65, 64, 67, 25, 26, 27, 24, 21, 22, 23, 20, 16, 19, 18, 17) +#define _trans_cube_RDr static_cube( \ + 67, 64, 66, 65, 38, 37, 39, 36, 23, 20, 21, 22, 27, 24, 25, 26, 2, 3, 0, 1) +#define _trans_cube_RDr_inverse static_cube( \ + 33, 35, 34, 32, 71, 69, 68, 70, 10, 11, 8, 9, 17, 18, 19, 16, 21, 22, 23, 20) +#define _trans_cube_RBr static_cube( \ + 37, 38, 39, 36, 67, 64, 65, 66, 27, 24, 25, 26, 20, 23, 22, 21, 17, 18, 19, 16) +#define _trans_cube_RBr_inverse static_cube( \ + 37, 38, 39, 36, 67, 64, 65, 66, 27, 24, 25, 26, 20, 23, 22, 21, 17, 18, 19, 16) +#define _trans_cube_LUr static_cube( \ + 65, 66, 64, 67, 36, 39, 37, 38, 21, 22, 23, 20, 26, 25, 24, 27, 1, 0, 3, 2) +#define _trans_cube_LUr_inverse static_cube( \ + 34, 32, 33, 35, 68, 70, 71, 69, 9, 8, 11, 10, 19, 16, 17, 18, 22, 21, 20, 23) +#define _trans_cube_LFr static_cube( \ + 36, 39, 38, 37, 66, 65, 64, 67, 25, 26, 27, 24, 21, 22, 23, 20, 16, 19, 18, 17) +#define _trans_cube_LFr_inverse static_cube( \ + 38, 37, 36, 39, 64, 67, 66, 65, 24, 27, 26, 25, 23, 20, 21, 22, 19, 16, 17, 18) +#define _trans_cube_LDr static_cube( \ + 66, 65, 67, 64, 39, 36, 38, 37, 22, 21, 20, 23, 25, 26, 27, 24, 3, 2, 1, 0) +#define _trans_cube_LDr_inverse static_cube( \ + 35, 33, 32, 34, 69, 71, 70, 68, 11, 10, 9, 8, 18, 17, 16, 19, 23, 20, 21, 22) +#define _trans_cube_LBr static_cube( \ + 39, 36, 37, 38, 65, 66, 67, 64, 26, 25, 24, 27, 22, 21, 20, 23, 18, 17, 16, 19) +#define _trans_cube_LBr_inverse static_cube( \ + 39, 36, 37, 38, 65, 66, 67, 64, 26, 25, 24, 27, 22, 21, 20, 23, 18, 17, 16, 19) +#define _trans_cube_FUr static_cube( \ + 68, 70, 69, 71, 32, 34, 33, 35, 16, 19, 18, 17, 9, 8, 11, 10, 5, 4, 7, 6) +#define _trans_cube_FUr_inverse static_cube( \ + 68, 70, 69, 71, 32, 34, 33, 35, 16, 19, 18, 17, 9, 8, 11, 10, 5, 4, 7, 6) +#define _trans_cube_FRr static_cube( \ + 32, 34, 35, 33, 70, 68, 69, 71, 8, 9, 10, 11, 16, 19, 18, 17, 20, 23, 22, 21) +#define _trans_cube_FRr_inverse static_cube( \ + 64, 67, 65, 66, 37, 38, 36, 39, 20, 23, 22, 21, 24, 27, 26, 25, 0, 1, 2, 3) +#define _trans_cube_FDr static_cube( \ + 70, 68, 71, 69, 34, 32, 35, 33, 19, 16, 17, 18, 8, 9, 10, 11, 7, 6, 5, 4) +#define _trans_cube_FDr_inverse static_cube( \ + 69, 71, 68, 70, 33, 35, 32, 34, 17, 18, 19, 16, 11, 10, 9, 8, 4, 5, 6, 7) +#define _trans_cube_FLr static_cube( \ + 34, 32, 33, 35, 68, 70, 71, 69, 9, 8, 11, 10, 19, 16, 17, 18, 22, 21, 20, 23) +#define _trans_cube_FLr_inverse static_cube( \ + 65, 66, 64, 67, 36, 39, 37, 38, 21, 22, 23, 20, 26, 25, 24, 27, 1, 0, 3, 2) +#define _trans_cube_BUr static_cube( \ + 69, 71, 68, 70, 33, 35, 32, 34, 17, 18, 19, 16, 11, 10, 9, 8, 4, 5, 6, 7) +#define _trans_cube_BUr_inverse static_cube( \ + 70, 68, 71, 69, 34, 32, 35, 33, 19, 16, 17, 18, 8, 9, 10, 11, 7, 6, 5, 4) +#define _trans_cube_BRr static_cube( \ + 35, 33, 32, 34, 69, 71, 70, 68, 11, 10, 9, 8, 18, 17, 16, 19, 23, 20, 21, 22) +#define _trans_cube_BRr_inverse static_cube( \ + 66, 65, 67, 64, 39, 36, 38, 37, 22, 21, 20, 23, 25, 26, 27, 24, 3, 2, 1, 0) +#define _trans_cube_BDr static_cube( \ + 71, 69, 70, 68, 35, 33, 34, 32, 18, 17, 16, 19, 10, 11, 8, 9, 6, 7, 4, 5) +#define _trans_cube_BDr_inverse static_cube( \ + 71, 69, 70, 68, 35, 33, 34, 32, 18, 17, 16, 19, 10, 11, 8, 9, 6, 7, 4, 5) +#define _trans_cube_BLr static_cube( \ + 33, 35, 34, 32, 71, 69, 68, 70, 10, 11, 8, 9, 17, 18, 19, 16, 21, 22, 23, 20) +#define _trans_cube_BLr_inverse static_cube( \ + 67, 64, 66, 65, 38, 37, 39, 36, 23, 20, 21, 22, 27, 24, 25, 26, 2, 3, 0, 1) +#define _trans_cube_UFm static_cube( \ + 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 2, 3, 5, 4, 7, 6, 9, 8, 11, 10) +#define _trans_cube_UFm_inverse static_cube( \ + 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 2, 3, 5, 4, 7, 6, 9, 8, 11, 10) +#define _trans_cube_ULm static_cube( \ + 0, 1, 3, 2, 5, 4, 6, 7, 4, 5, 6, 7, 0, 1, 2, 3, 24, 27, 26, 25) +#define _trans_cube_ULm_inverse static_cube( \ + 0, 1, 3, 2, 5, 4, 6, 7, 4, 5, 6, 7, 0, 1, 2, 3, 24, 27, 26, 25) +#define _trans_cube_UBm static_cube( \ + 5, 4, 7, 6, 1, 0, 3, 2, 1, 0, 3, 2, 4, 5, 6, 7, 11, 10, 9, 8) +#define _trans_cube_UBm_inverse static_cube( \ + 5, 4, 7, 6, 1, 0, 3, 2, 1, 0, 3, 2, 4, 5, 6, 7, 11, 10, 9, 8) +#define _trans_cube_URm static_cube( \ + 1, 0, 2, 3, 4, 5, 7, 6, 5, 4, 7, 6, 1, 0, 3, 2, 26, 25, 24, 27) +#define _trans_cube_URm_inverse static_cube( \ + 1, 0, 2, 3, 4, 5, 7, 6, 5, 4, 7, 6, 1, 0, 3, 2, 26, 25, 24, 27) +#define _trans_cube_DFm static_cube( \ + 6, 7, 4, 5, 2, 3, 0, 1, 3, 2, 1, 0, 7, 6, 5, 4, 8, 9, 10, 11) +#define _trans_cube_DFm_inverse static_cube( \ + 6, 7, 4, 5, 2, 3, 0, 1, 3, 2, 1, 0, 7, 6, 5, 4, 8, 9, 10, 11) +#define _trans_cube_DLm static_cube( \ + 3, 2, 0, 1, 6, 7, 5, 4, 7, 6, 5, 4, 2, 3, 0, 1, 27, 24, 25, 26) +#define _trans_cube_DLm_inverse static_cube( \ + 2, 3, 1, 0, 7, 6, 4, 5, 6, 7, 4, 5, 3, 2, 1, 0, 25, 26, 27, 24) +#define _trans_cube_DBm static_cube( \ + 7, 6, 5, 4, 3, 2, 1, 0, 2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9) +#define _trans_cube_DBm_inverse static_cube( \ + 7, 6, 5, 4, 3, 2, 1, 0, 2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9) +#define _trans_cube_DRm static_cube( \ + 2, 3, 1, 0, 7, 6, 4, 5, 6, 7, 4, 5, 3, 2, 1, 0, 25, 26, 27, 24) +#define _trans_cube_DRm_inverse static_cube( \ + 3, 2, 0, 1, 6, 7, 5, 4, 7, 6, 5, 4, 2, 3, 0, 1, 27, 24, 25, 26) +#define _trans_cube_RUm static_cube( \ + 68, 71, 69, 70, 33, 34, 32, 35, 21, 22, 23, 20, 25, 26, 27, 24, 0, 1, 2, 3) +#define _trans_cube_RUm_inverse static_cube( \ + 70, 68, 69, 71, 32, 34, 35, 33, 8, 9, 10, 11, 19, 16, 17, 18, 23, 20, 21, 22) +#define _trans_cube_RFm static_cube( \ + 34, 33, 32, 35, 68, 71, 70, 69, 25, 26, 27, 24, 22, 21, 20, 23, 19, 16, 17, 18) +#define _trans_cube_RFm_inverse static_cube( \ + 66, 65, 64, 67, 36, 39, 38, 37, 25, 26, 27, 24, 22, 21, 20, 23, 19, 16, 17, 18) +#define _trans_cube_RDm static_cube( \ + 71, 68, 70, 69, 34, 33, 35, 32, 22, 21, 20, 23, 26, 25, 24, 27, 2, 3, 0, 1) +#define _trans_cube_RDm_inverse static_cube( \ + 71, 69, 68, 70, 33, 35, 34, 32, 10, 11, 8, 9, 18, 17, 16, 19, 22, 21, 20, 23) +#define _trans_cube_RBm static_cube( \ + 33, 34, 35, 32, 71, 68, 69, 70, 26, 25, 24, 27, 21, 22, 23, 20, 17, 18, 19, 16) +#define _trans_cube_RBm_inverse static_cube( \ + 67, 64, 65, 66, 37, 38, 39, 36, 27, 24, 25, 26, 23, 20, 21, 22, 18, 17, 16, 19) +#define _trans_cube_LUm static_cube( \ + 69, 70, 68, 71, 32, 35, 33, 34, 20, 23, 22, 21, 27, 24, 25, 26, 1, 0, 3, 2) +#define _trans_cube_LUm_inverse static_cube( \ + 68, 70, 71, 69, 34, 32, 33, 35, 9, 8, 11, 10, 16, 19, 18, 17, 21, 22, 23, 20) +#define _trans_cube_LFm static_cube( \ + 32, 35, 34, 33, 70, 69, 68, 71, 24, 27, 26, 25, 20, 23, 22, 21, 16, 19, 18, 17) +#define _trans_cube_LFm_inverse static_cube( \ + 64, 67, 66, 65, 38, 37, 36, 39, 24, 27, 26, 25, 20, 23, 22, 21, 16, 19, 18, 17) +#define _trans_cube_LDm static_cube( \ + 70, 69, 71, 68, 35, 32, 34, 33, 23, 20, 21, 22, 24, 27, 26, 25, 3, 2, 1, 0) +#define _trans_cube_LDm_inverse static_cube( \ + 69, 71, 70, 68, 35, 33, 32, 34, 11, 10, 9, 8, 17, 18, 19, 16, 20, 23, 22, 21) +#define _trans_cube_LBm static_cube( \ + 35, 32, 33, 34, 69, 70, 71, 68, 27, 24, 25, 26, 23, 20, 21, 22, 18, 17, 16, 19) +#define _trans_cube_LBm_inverse static_cube( \ + 65, 66, 67, 64, 39, 36, 37, 38, 26, 25, 24, 27, 21, 22, 23, 20, 17, 18, 19, 16) +#define _trans_cube_FUm static_cube( \ + 64, 66, 65, 67, 36, 38, 37, 39, 16, 19, 18, 17, 8, 9, 10, 11, 4, 5, 6, 7) +#define _trans_cube_FUm_inverse static_cube( \ + 32, 34, 33, 35, 68, 70, 69, 71, 16, 19, 18, 17, 8, 9, 10, 11, 4, 5, 6, 7) +#define _trans_cube_FRm static_cube( \ + 36, 38, 39, 37, 66, 64, 65, 67, 9, 8, 11, 10, 16, 19, 18, 17, 21, 22, 23, 20) +#define _trans_cube_FRm_inverse static_cube( \ + 37, 38, 36, 39, 64, 67, 65, 66, 20, 23, 22, 21, 27, 24, 25, 26, 1, 0, 3, 2) +#define _trans_cube_FDm static_cube( \ + 66, 64, 67, 65, 38, 36, 39, 37, 19, 16, 17, 18, 9, 8, 11, 10, 6, 7, 4, 5) +#define _trans_cube_FDm_inverse static_cube( \ + 33, 35, 32, 34, 69, 71, 68, 70, 17, 18, 19, 16, 10, 11, 8, 9, 5, 4, 7, 6) +#define _trans_cube_FLm static_cube( \ + 38, 36, 37, 39, 64, 66, 67, 65, 8, 9, 10, 11, 19, 16, 17, 18, 23, 20, 21, 22) +#define _trans_cube_FLm_inverse static_cube( \ + 36, 39, 37, 38, 65, 66, 64, 67, 21, 22, 23, 20, 25, 26, 27, 24, 0, 1, 2, 3) +#define _trans_cube_BUm static_cube( \ + 65, 67, 64, 66, 37, 39, 36, 38, 17, 18, 19, 16, 10, 11, 8, 9, 5, 4, 7, 6) +#define _trans_cube_BUm_inverse static_cube( \ + 34, 32, 35, 33, 70, 68, 71, 69, 19, 16, 17, 18, 9, 8, 11, 10, 6, 7, 4, 5) +#define _trans_cube_BRm static_cube( \ + 39, 37, 36, 38, 65, 67, 66, 64, 10, 11, 8, 9, 18, 17, 16, 19, 22, 21, 20, 23) +#define _trans_cube_BRm_inverse static_cube( \ + 39, 36, 38, 37, 66, 65, 67, 64, 22, 21, 20, 23, 26, 25, 24, 27, 2, 3, 0, 1) +#define _trans_cube_BDm static_cube( \ + 67, 65, 66, 64, 39, 37, 38, 36, 18, 17, 16, 19, 11, 10, 9, 8, 7, 6, 5, 4) +#define _trans_cube_BDm_inverse static_cube( \ + 35, 33, 34, 32, 71, 69, 70, 68, 18, 17, 16, 19, 11, 10, 9, 8, 7, 6, 5, 4) +#define _trans_cube_BLm static_cube( \ + 37, 39, 38, 36, 67, 65, 64, 66, 11, 10, 9, 8, 17, 18, 19, 16, 20, 23, 22, 21) +#define _trans_cube_BLm_inverse static_cube( \ + 38, 37, 39, 36, 67, 64, 66, 65, 23, 20, 21, 22, 24, 27, 26, 25, 3, 2, 1, 0) diff --git a/src/core/core.h b/src/core/core.h new file mode 100644 index 0000000..6ffd76f --- /dev/null +++ b/src/core/core.h @@ -0,0 +1,13 @@ +#include "constant_cubes.h" +#include "io_moves.h" +#include "io_trans.h" +#include "cube.h" +#include "io_cube.h" +#include "moves.h" + +/* TODO: work in progress */ +#if 0 +#include "transform.h" +#else +#include "transform_with_switch.h" +#endif diff --git a/src/core/cube.h b/src/core/cube.h new file mode 100644 index 0000000..fe36f90 --- /dev/null +++ b/src/core/cube.h @@ -0,0 +1,291 @@ +#define _move(M, c) compose(c, _move_cube_ ## M) +#define _premove(M, c) compose(_move_cube_ ## M, c) + +_static cube_t cubefromarray(uint8_t [static 8], uint8_t [static 12]); +_static cube_t solvedcube(void); +_static bool isconsistent(cube_t); +_static bool issolvable(cube_t); +_static bool issolved(cube_t); +_static bool iserror(cube_t); +_static cube_t applymoves(cube_t, const char *); +_static cube_t applytrans(cube_t, const char *); +_static cube_t frommoves(const char *); +_static void getcube_fix(int64_t *, int64_t *, int64_t *, int64_t *); +_static cube_t getcube(int64_t, int64_t, int64_t, int64_t); + +_static cube_t move(cube_t, uint8_t); +_static cube_t transform_edges(cube_t, uint8_t); +_static cube_t transform_corners(cube_t, uint8_t); +_static cube_t transform(cube_t, uint8_t); + +_static cube_t +cubefromarray(uint8_t c[static 8], uint8_t e[static 12]) +{ + return static_cube( + c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], + e[0], e[1], e[2], e[3], e[4], e[5], e[6], e[7], + e[8], e[9], e[10], e[11]); +} + +_static cube_t +solvedcube(void) +{ + return solved; +} + +_static bool +isconsistent(cube_t cube) +{ + uint8_t i, p, e, piece, corner[8], edge[12]; + bool found[12]; + + pieces(&cube, corner, edge); + + for (i = 0; i < 12; i++) + found[i] = false; + for (i = 0; i < 12; i++) { + piece = edge[i]; + p = piece & _pbits; + e = piece & _eobit; + if (p >= 12) + goto inconsistent_ep; + if (e != 0 && e != _eobit) + goto inconsistent_eo; + found[p] = true; + } + for (i = 0; i < 12; i++) + if (!found[i]) + goto inconsistent_ep; + + for (i = 0; i < 8; i++) + found[i] = false; + for (i = 0; i < 8; i++) { + piece = corner[i]; + p = piece & _pbits; + e = piece & _cobits; + if (p >= 8) + goto inconsistent_cp; + if (e != 0 && e != _ctwist_cw && e != _ctwist_ccw) + goto inconsistent_co; + found[p] = true; + } + for (i = 0; i < 8; i++) + if (!found[i]) + goto inconsistent_co; + + return true; + +inconsistent_ep: + LOG("Inconsistent EP\n"); + return false; +inconsistent_cp: + LOG("Inconsistent CP\n"); + return false; +inconsistent_eo: + LOG("Inconsistent EO\n"); + return false; +inconsistent_co: + LOG("Inconsistent CO\n"); + return false; +} + +_static bool +issolvable(cube_t cube) +{ + uint8_t i, eo, co, piece, edge[12], corner[8], ep[12], cp[8]; + + DBG_ASSERT(isconsistent(cube), false, + "issolvable: cube is inconsistent\n"); + + pieces(&cube, corner, edge); + for (i = 0; i < 12; i++) + ep[i] = edge[i] & _pbits; + for (i = 0; i < 8; i++) + cp[i] = corner[i] & _pbits; + + if (permsign(ep, 12) != permsign(cp, 8)) + goto issolvable_parity; + + eo = 0; + for (i = 0; i < 12; i++) { + piece = edge[i]; + eo += (piece & _eobit) >> _eoshift; + } + if (eo % 2 != 0) + goto issolvable_eo; + + co = 0; + for (i = 0; i < 8; i++) { + piece = corner[i]; + co += (piece & _cobits) >> _coshift; + } + if (co % 3 != 0) + goto issolvable_co; + + return true; + +issolvable_parity: + LOG("EP and CP parities are different\n"); + return false; +issolvable_eo: + LOG("Odd number of flipped edges\n"); + return false; +issolvable_co: + LOG("Sum of corner orientation is not multiple of 3\n"); + return false; +} + +bool +issolved(cube_t cube) +{ + return equal(cube, solved); +} + +bool +iserror(cube_t cube) +{ + return equal(cube, zero); +} + +_static cube_t +applymoves(cube_t cube, const char *buf) +{ + uint8_t r, m; + const char *b; + + DBG_ASSERT(isconsistent(cube), zero, + "move error: inconsistent cube\n"); + + for (b = buf; *b != '\0'; b++) { + while (*b == ' ' || *b == '\t' || *b == '\n') + b++; + if (*b == '\0') + goto applymoves_finish; + if ((r = readmove(*b)) == _error) + goto applymoves_error; + if ((m = readmodifier(*(b+1))) != 0) + b++; + cube = move(cube, r + m); + } + +applymoves_finish: + return cube; + +applymoves_error: + LOG("applymoves error\n"); + return zero; +} + +_static cube_t +frommoves(const char *buf) +{ + return applymoves(solved, buf); +} + +_static void +getcube_fix(int64_t *ep, int64_t *eo, int64_t *cp, int64_t *co) +{ + uint8_t e[12], c[8], coarr[8]; + + *ep = (*ep % _12f + _12f) % _12f; + *eo = (*eo % _2p11 + _2p11) % _2p11; + *cp = (*cp % _8f + _8f) % _8f; + *co = (*cp % _3p7 + _3p7) % _3p7; + + indextoperm(*ep, 12, e); + indextoperm(*cp, 8, c); + if (permsign(e, 12) != permsign(c, 8)) { + _swap(c[0], c[1]); + *cp = permtoindex(c, 8); + + sumzerotodigits(*co, 8, 3, coarr); + _swap(coarr[0], coarr[1]); + *co = digitstosumzero(coarr, 8, 3); + } +} + +_static cube_t +getcube(int64_t ep, int64_t eo, int64_t cp, int64_t co) +{ + uint8_t i, earr[12], carr[8], eoarr[12], coarr[8]; + + sumzerotodigits(eo, 12, 2, eoarr); + DBG_ASSERT(eoarr[0] != _error, zero, "Error making EO"); + indextoperm(ep, 12, earr); + DBG_ASSERT(earr[0] != _error, zero, "Error making EP"); + for (i = 0; i < 12; i++) + earr[i] |= eoarr[i] << _eoshift; + + sumzerotodigits(co, 8, 3, coarr); + DBG_ASSERT(coarr[0] != _error, zero, "Error making CO"); + indextoperm(cp, 8, carr); + DBG_ASSERT(carr[0] != _error, zero, "Error making CP"); + for (i = 0; i < 8; i++) + carr[i] |= coarr[i] << _coshift; + + return cubefromarray(carr, earr); +} + +_static cube_t +applytrans(cube_t cube, const char *buf) +{ + uint8_t t; + + DBG_ASSERT(isconsistent(cube), zero, + "transformation error: inconsistent cube\n"); + + t = readtrans(buf); + + return transform(cube, t); +} + +_static cube_t +move(cube_t c, uint8_t m) +{ + switch (m) { + case _move_U: + return _move(U, c); + case _move_U2: + return _move(U2, c); + case _move_U3: + return _move(U3, c); + case _move_D: + return _move(D, c); + case _move_D2: + return _move(D2, c); + case _move_D3: + return _move(D3, c); + case _move_R: + return _move(R, c); + case _move_R2: + return _move(R2, c); + case _move_R3: + return _move(R3, c); + case _move_L: + return _move(L, c); + case _move_L2: + return _move(L2, c); + case _move_L3: + return _move(L3, c); + case _move_F: + return _move(F, c); + case _move_F2: + return _move(F2, c); + case _move_F3: + return _move(F3, c); + case _move_B: + return _move(B, c); + case _move_B2: + return _move(B2, c); + case _move_B3: + return _move(B3, c); + default: + LOG("move error, unknown move\n"); + return zero; + } +} + +/* +TODO transform is now relegated to a separated file because it is too long. +It would be nice to make it shorter without loosing performance. +*/ diff --git a/src/core/io_cube.h b/src/core/io_cube.h new file mode 100644 index 0000000..956494d --- /dev/null +++ b/src/core/io_cube.h @@ -0,0 +1,348 @@ +_static uint8_t readco(const char *); +_static uint8_t readcp(const char *); +_static uint8_t readeo(const char *); +_static uint8_t readep(const char *); +_static cube_t readcube_B32(const char *); +_static cube_t readcube_H48(const char *); +_static uint8_t readpiece_LST(const char **); +_static cube_t readcube_LST(const char *); + +_static int writepiece_LST(uint8_t, char *); +_static void writecube_B32(cube_t, char *); +_static void writecube_H48(cube_t, char *); +_static void writecube_LST(cube_t, char *); + +_static uint8_t b32toedge(char); +_static uint8_t b32tocorner(char); +_static char edgetob32(uint8_t); +_static char cornertob32(uint8_t); + +_static struct { + const char *name; + cube_t (*read)(const char *); + void (*write)(cube_t, char *); +} ioformat[] = +{ + { .name = "B32", .read = readcube_B32, .write = writecube_B32 }, + { .name = "LST", .read = readcube_LST, .write = writecube_LST }, + { .name = "H48", .read = readcube_H48, .write = writecube_H48 }, + { .name = "NONE", .read = NULL, .write = NULL }, +}; + +cube_t +readcube(const char *format, const char *buf) +{ + int i; + + for (i = 0; ioformat[i].read != NULL; i++) + if (!strcmp(format, ioformat[i].name)) + return ioformat[i].read(buf); + + LOG("Cannot read cube in the given format\n"); + return zero; +} + +void +writecube(const char *format, cube_t cube, char *buf) +{ + char *errormsg; + size_t len; + + if (!isconsistent(cube)) { + errormsg = "ERROR: inconsistent"; + goto writecube_error; + } + + int i; + + for (i = 0; ioformat[i].write != NULL; i++) { + if (!strcmp(format, ioformat[i].name)) { + ioformat[i].write(cube, buf); + return; + } + } + + errormsg = "ERROR: format"; + +writecube_error: + LOG("writecube error, see stdout for details\n"); + len = strlen(errormsg); + memcpy(buf, errormsg, len); + buf[len] = '\n'; + buf[len+1] = '\0'; +} + +_static uint8_t +readco(const char *str) +{ + if (*str == '0') + return 0; + if (*str == '1') + return _ctwist_cw; + if (*str == '2') + return _ctwist_ccw; + + LOG("Error reading CO\n"); + return _error; +} + +_static uint8_t +readcp(const char *str) +{ + uint8_t c; + + for (c = 0; c < 8; c++) + if (!strncmp(str, cornerstr[c], 3) || + !strncmp(str, cornerstralt[c], 3)) + return c; + + LOG("Error reading CP\n"); + return _error; +} + +_static uint8_t +readeo(const char *str) +{ + if (*str == '0') + return 0; + if (*str == '1') + return _eflip; + + LOG("Error reading EO\n"); + return _error; +} + +_static uint8_t +readep(const char *str) +{ + uint8_t e; + + for (e = 0; e < 12; e++) + if (!strncmp(str, edgestr[e], 2)) + return e; + + LOG("Error reading EP\n"); + return _error; +} + +_static cube_t +readcube_B32(const char *buf) +{ + int i; + uint8_t c[8], e[12]; + + for (i = 0; i < 8; i++) { + c[i] = b32tocorner(buf[i]); + DBG_ASSERT(c[i] < 255, zero, + "Error reading B32 corner %d (char %d)\n", i, i); + } + + for (i = 0; i < 12; i++) { + e[i] = b32toedge(buf[i+9]); + DBG_ASSERT(e[i] < 255, zero, + "Error reading B32 edge %d (char %d)\n", i, i+9); + } + + return cubefromarray(c, e); +} + +_static cube_t +readcube_H48(const char *buf) +{ + int i; + uint8_t piece, orient, c[8], e[12]; + const char *b; + + b = buf; + + for (i = 0; i < 12; i++) { + while (*b == ' ' || *b == '\t' || *b == '\n') + b++; + if ((piece = readep(b)) == _error) + return zero; + b += 2; + if ((orient = readeo(b)) == _error) + return zero; + b++; + e[i] = piece | orient; + } + for (i = 0; i < 8; i++) { + while (*b == ' ' || *b == '\t' || *b == '\n') + b++; + if ((piece = readcp(b)) == _error) + return zero; + b += 3; + if ((orient = readco(b)) == _error) + return zero; + b++; + c[i] = piece | orient; + } + + return cubefromarray(c, e); +} + +_static uint8_t +readpiece_LST(const char **b) +{ + uint8_t ret; + bool read; + + while (**b == ',' || **b == ' ' || **b == '\t' || **b == '\n') + (*b)++; + + for (ret = 0, read = false; **b >= '0' && **b <= '9'; (*b)++) { + read = true; + ret = ret * 10 + (**b) - '0'; + } + + return read ? ret : _error; +} + +_static cube_t +readcube_LST(const char *buf) +{ + int i; + uint8_t c[8], e[12]; + + for (i = 0; i < 8; i++) + c[i] = readpiece_LST(&buf); + + for (i = 0; i < 12; i++) + e[i] = readpiece_LST(&buf); + + return cubefromarray(c, e); +} + +_static int +writepiece_LST(uint8_t piece, char *buf) +{ + char digits[3]; + int i, len; + + len = 0; + while (piece != 0) { + digits[len++] = (piece % 10) + '0'; + piece /= 10; + } + + if (len == 0) + digits[len++] = '0'; + + for (i = 0; i < len; i++) + buf[i] = digits[len-i-1]; + + buf[len] = ','; + buf[len+1] = ' '; + + return len+2; +} + +_static void +writecube_B32(cube_t cube, char *buf) +{ + int i; + uint8_t corner[8], edge[12]; + + pieces(&cube, corner, edge); + + for (i = 0; i < 8; i++) + buf[i] = cornertob32(corner[i]); + + buf[8] = '='; + + for (i = 0; i < 12; i++) + buf[i+9] = edgetob32(edge[i]); + + buf[21] = '\0'; +} + +_static void +writecube_H48(cube_t cube, char *buf) +{ + uint8_t piece, perm, orient, corner[8], edge[12]; + int i; + + pieces(&cube, corner, edge); + + for (i = 0; i < 12; i++) { + piece = edge[i]; + perm = piece & _pbits; + orient = (piece & _eobit) >> _eoshift; + buf[4*i ] = edgestr[perm][0]; + buf[4*i + 1] = edgestr[perm][1]; + buf[4*i + 2] = orient + '0'; + buf[4*i + 3] = ' '; + } + for (i = 0; i < 8; i++) { + piece = corner[i]; + perm = piece & _pbits; + orient = (piece & _cobits) >> _coshift; + buf[48 + 5*i ] = cornerstr[perm][0]; + buf[48 + 5*i + 1] = cornerstr[perm][1]; + buf[48 + 5*i + 2] = cornerstr[perm][2]; + buf[48 + 5*i + 3] = orient + '0'; + buf[48 + 5*i + 4] = ' '; + } + + buf[48+39] = '\0'; +} + +_static void +writecube_LST(cube_t cube, char *buf) +{ + int i; + size_t ptr; + uint8_t piece, corner[8], edge[12]; + + ptr = 0; + pieces(&cube, corner, edge); + + for (i = 0; i < 8; i++) { + piece = corner[i]; + ptr += writepiece_LST(piece, buf + ptr); + } + + for (i = 0; i < 12; i++) { + piece = edge[i]; + ptr += writepiece_LST(piece, buf + ptr); + } + + *(buf+ptr-2) = 0; +} + +_static uint8_t +b32toedge(char c) +{ + if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'f'))) + return 255; + + return c <= 'Z' ? (uint8_t)(c - 'A') : (uint8_t)(c - 'a') + 26; +} + +_static uint8_t +b32tocorner(char c) { + uint8_t val; + + if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'f'))) + return 255; + + val = c <= 'Z' ? (uint8_t)(c - 'A') : (uint8_t)(c - 'a') + 26; + + return (val & 7) | ((val & 24) << 2); +} + +_static char +edgetob32(uint8_t edge) +{ + return edge < 26 ? 'A' + (char)edge : 'a' + (char)(edge - 26); +} + +_static char +cornertob32(uint8_t corner) +{ + uint8_t val; + + val = (corner & 7) | ((corner & 96) >> 2); + + return val < 26 ? 'A' + (char)val : 'a' + (char)(val - 26); +} diff --git a/src/core/io_moves.h b/src/core/io_moves.h new file mode 100644 index 0000000..eb6290e --- /dev/null +++ b/src/core/io_moves.h @@ -0,0 +1,62 @@ +_static uint8_t readmove(char); +_static uint8_t readmodifier(char); +_static int writemoves(uint8_t *, int, char *); + +_static uint8_t +readmove(char c) +{ + switch (c) { + case 'U': + return _move_U; + case 'D': + return _move_D; + case 'R': + return _move_R; + case 'L': + return _move_L; + case 'F': + return _move_F; + case 'B': + return _move_B; + default: + return _error; + } +} + +_static uint8_t +readmodifier(char c) +{ + switch (c) { + case '1': /* Fallthrough */ + case '2': /* Fallthrough */ + case '3': + return c - '0' - 1; + case '\'': + return 2; + default: + return 0; + } +} + +_static int +writemoves(uint8_t *m, int n, char *buf) +{ + int i; + size_t len; + const char *s; + char *b; + + for (i = 0, b = buf; i < n; i++, b++) { + s = movestr[m[i]]; + len = strlen(s); + memcpy(b, s, len); + b += len; + *b = ' '; + } + + if (b != buf) + b--; /* Remove last space */ + *b = '\0'; + + return b - buf; +} diff --git a/src/core/io_trans.h b/src/core/io_trans.h new file mode 100644 index 0000000..7187b7a --- /dev/null +++ b/src/core/io_trans.h @@ -0,0 +1,25 @@ +_static uint8_t readtrans(const char *); +_static void writetrans(uint8_t, char *); + +_static uint8_t +readtrans(const char *buf) +{ + uint8_t t; + + for (t = 0; t < 48; t++) + if (!strncmp(buf, transstr[t], 11)) + return t; + + LOG("readtrans error\n"); + return _error; +} + +_static void +writetrans(uint8_t t, char *buf) +{ + if (t >= 48) + memcpy(buf, "error trans", 11); + else + memcpy(buf, transstr[t], 11); + buf[11] = '\0'; +} diff --git a/src/core/moves.h b/src/core/moves.h new file mode 100644 index 0000000..7da6405 --- /dev/null +++ b/src/core/moves.h @@ -0,0 +1,47 @@ +_static_inline bool allowednextmove(uint8_t *, uint8_t); +_static_inline uint8_t inverse_trans(uint8_t); +_static_inline uint8_t movebase(uint8_t); +_static_inline uint8_t moveaxis(uint8_t); + +_static bool +allowednextmove(uint8_t *moves, uint8_t n) +{ + uint8_t base[3], axis[3]; + + if (n < 2) + return true; + + base[0] = movebase(moves[n-1]); + axis[0] = moveaxis(moves[n-1]); + base[1] = movebase(moves[n-2]); + axis[1] = moveaxis(moves[n-2]); + + if (base[0] == base[1] || (axis[0] == axis[1] && base[0] < base[1])) + return false; + + if (n == 2) + return true; + + base[2] = movebase(moves[n-3]); + axis[2] = moveaxis(moves[n-3]); + + return axis[1] != axis[2] || base[0] != base[2]; +} + +_static_inline uint8_t +inverse_trans(uint8_t t) +{ + return inverse_trans_table[t]; +} + +_static_inline uint8_t +movebase(uint8_t move) +{ + return move / 3; +} + +_static_inline uint8_t +moveaxis(uint8_t move) +{ + return move / 6; +} diff --git a/src/core/transform.h b/src/core/transform.h new file mode 100644 index 0000000..358b051 --- /dev/null +++ b/src/core/transform.h @@ -0,0 +1,176 @@ +/* TODO: remove these macros, if the below works */ + +#define _trans_edges_rotation(T, c) \ + compose_edges(compose_edges(_trans_cube_ ## T, c), \ + _trans_cube_ ## T ## _inverse) +#define _trans_edges_mirrored(T, c) _trans_edges_rotation(T, c) + +#define _trans_corners_rotation(T, c) \ + compose_corners(compose_corners(_trans_cube_ ## T, c), \ + _trans_cube_ ## T ## _inverse) +#define _trans_corners_mirrored(T, c) \ + invertco(compose_corners( \ + compose_corners(_trans_cube_ ## T, c), _trans_cube_ ## T ## _inverse)) + +#define _trans_rotation(T, c) \ + compose(compose(_trans_cube_ ## T, c), \ + _trans_cube_ ## T ## _inverse) +#define _trans_mirrored(T, c) \ + invertco(compose(compose(_trans_cube_ ## T, c), \ + _trans_cube_ ## T ## _inverse)) + +static cube_t cube_trans_table[48] = { + [_trans_UFr] = _trans_cube_UFr, + [_trans_UFm] = _trans_cube_UFm, + [_trans_ULr] = _trans_cube_URr, + [_trans_ULm] = _trans_cube_ULm, + [_trans_UBr] = _trans_cube_UBr, + [_trans_UBm] = _trans_cube_UBm, + [_trans_URr] = _trans_cube_ULr, + [_trans_URm] = _trans_cube_URm, + [_trans_DFr] = _trans_cube_DFr, + [_trans_DFm] = _trans_cube_DFm, + [_trans_DLr] = _trans_cube_DLr, + [_trans_DLm] = _trans_cube_DRm, + [_trans_DBr] = _trans_cube_DBr, + [_trans_DBm] = _trans_cube_DBm, + [_trans_DRr] = _trans_cube_DRr, + [_trans_DRm] = _trans_cube_DLm, + [_trans_RUr] = _trans_cube_FRr, + [_trans_RUm] = _trans_cube_FLm, + [_trans_RFr] = _trans_cube_LFr, + [_trans_RFm] = _trans_cube_RFm, + [_trans_RDr] = _trans_cube_BLr, + [_trans_RDm] = _trans_cube_BRm, + [_trans_RBr] = _trans_cube_RBr, + [_trans_RBm] = _trans_cube_LBm, + [_trans_LUr] = _trans_cube_FLr, + [_trans_LUm] = _trans_cube_FRm, + [_trans_LFr] = _trans_cube_RFr, + [_trans_LFm] = _trans_cube_LFm, + [_trans_LDr] = _trans_cube_BRr, + [_trans_LDm] = _trans_cube_BLm, + [_trans_LBr] = _trans_cube_LBr, + [_trans_LBm] = _trans_cube_RBm, + [_trans_FUr] = _trans_cube_FUr, + [_trans_FUm] = _trans_cube_FUm, + [_trans_FRr] = _trans_cube_RUr, + [_trans_FRm] = _trans_cube_LUm, + [_trans_FDr] = _trans_cube_BUr, + [_trans_FDm] = _trans_cube_BUm, + [_trans_FLr] = _trans_cube_LUr, + [_trans_FLm] = _trans_cube_RUm, + [_trans_BUr] = _trans_cube_FDr, + [_trans_BUm] = _trans_cube_FDm, + [_trans_BRr] = _trans_cube_LDr, + [_trans_BRm] = _trans_cube_RDm, + [_trans_BDr] = _trans_cube_BDr, + [_trans_BDm] = _trans_cube_BDm, + [_trans_BLr] = _trans_cube_RDr, + [_trans_BLm] = _trans_cube_LDm, +}; + +static cube_t cube_trans_table_inverse[48] = { + [_trans_UFr] = _trans_cube_UFr_inverse, + [_trans_UFm] = _trans_cube_UFm_inverse, + [_trans_ULr] = _trans_cube_URr_inverse, + [_trans_ULm] = _trans_cube_ULm_inverse, + [_trans_UBr] = _trans_cube_UBr_inverse, + [_trans_UBm] = _trans_cube_UBm_inverse, + [_trans_URr] = _trans_cube_ULr_inverse, + [_trans_URm] = _trans_cube_URm_inverse, + [_trans_DFr] = _trans_cube_DFr_inverse, + [_trans_DFm] = _trans_cube_DFm_inverse, + [_trans_DLr] = _trans_cube_DLr_inverse, + [_trans_DLm] = _trans_cube_DRm_inverse, + [_trans_DBr] = _trans_cube_DBr_inverse, + [_trans_DBm] = _trans_cube_DBm_inverse, + [_trans_DRr] = _trans_cube_DRr_inverse, + [_trans_DRm] = _trans_cube_DLm_inverse, + [_trans_RUr] = _trans_cube_FRr_inverse, + [_trans_RUm] = _trans_cube_FLm_inverse, + [_trans_RFr] = _trans_cube_LFr_inverse, + [_trans_RFm] = _trans_cube_RFm_inverse, + [_trans_RDr] = _trans_cube_BLr_inverse, + [_trans_RDm] = _trans_cube_BRm_inverse, + [_trans_RBr] = _trans_cube_RBr_inverse, + [_trans_RBm] = _trans_cube_LBm_inverse, + [_trans_LUr] = _trans_cube_FLr_inverse, + [_trans_LUm] = _trans_cube_FRm_inverse, + [_trans_LFr] = _trans_cube_RFr_inverse, + [_trans_LFm] = _trans_cube_LFm_inverse, + [_trans_LDr] = _trans_cube_BRr_inverse, + [_trans_LDm] = _trans_cube_BLm_inverse, + [_trans_LBr] = _trans_cube_LBr_inverse, + [_trans_LBm] = _trans_cube_RBm_inverse, + [_trans_FUr] = _trans_cube_FUr_inverse, + [_trans_FUm] = _trans_cube_FUm_inverse, + [_trans_FRr] = _trans_cube_RUr_inverse, + [_trans_FRm] = _trans_cube_LUm_inverse, + [_trans_FDr] = _trans_cube_BUr_inverse, + [_trans_FDm] = _trans_cube_BUm_inverse, + [_trans_FLr] = _trans_cube_LUr_inverse, + [_trans_FLm] = _trans_cube_RUm_inverse, + [_trans_BUr] = _trans_cube_FDr_inverse, + [_trans_BUm] = _trans_cube_FDm_inverse, + [_trans_BRr] = _trans_cube_LDr_inverse, + [_trans_BRm] = _trans_cube_RDm_inverse, + [_trans_BDr] = _trans_cube_BDr_inverse, + [_trans_BDm] = _trans_cube_BDm_inverse, + [_trans_BLr] = _trans_cube_RDr_inverse, + [_trans_BLm] = _trans_cube_LDm_inverse, +}; + +_static cube_t +transform_edges(cube_t c, uint8_t t) +{ + cube_t ret, trans_cube, trans_inv; + + DBG_ASSERT(t < 48, zero, + "transform: invalid transformation %" PRIu8 + ", must be between 0 and 47\n", t); + + trans_cube = cube_trans_table[t]; + trans_inv = cube_trans_table_inverse[t]; + + ret = compose_edges(trans_cube, c); + ret = compose_edges(ret, trans_inv); + + return ret; +} + +_static cube_t +transform_corners(cube_t c, uint8_t t) +{ + cube_t ret, trans_cube, trans_inv; + + DBG_ASSERT(t < 48, zero, + "transform: invalid transformation %" PRIu8 + ", must be between 0 and 47\n", t); + + trans_cube = cube_trans_table[t]; + trans_inv = cube_trans_table_inverse[t]; + + ret = compose_corners(trans_cube, c); + ret = compose_corners(ret, trans_inv); + + return t < 24 ? ret : invertco(ret); +} + +_static cube_t +transform(cube_t c, uint8_t t) +{ + cube_t ret, trans_cube, trans_inv; + + DBG_ASSERT(t < 48, zero, + "transform: invalid transformation %" PRIu8 + ", must be between 0 and 47\n", t); + + trans_cube = cube_trans_table[t]; + trans_inv = cube_trans_table_inverse[t]; + + ret = compose(trans_cube, c); + ret = compose(ret, trans_inv); + + return t < 24 ? ret : invertco(ret); +} diff --git a/src/core/transform_with_switch.h b/src/core/transform_with_switch.h new file mode 100644 index 0000000..d3a108c --- /dev/null +++ b/src/core/transform_with_switch.h @@ -0,0 +1,336 @@ +#define _trans_edges_rotation(T, c) \ + compose_edges(compose_edges(_trans_cube_ ## T, c), \ + _trans_cube_ ## T ## _inverse) +#define _trans_edges_mirrored(T, c) _trans_edges_rotation(T, c) + +#define _trans_corners_rotation(T, c) \ + compose_corners(compose_corners(_trans_cube_ ## T, c), \ + _trans_cube_ ## T ## _inverse) +#define _trans_corners_mirrored(T, c) \ + invertco(compose_corners( \ + compose_corners(_trans_cube_ ## T, c), _trans_cube_ ## T ## _inverse)) + +#define _trans_rotation(T, c) \ + compose(compose(_trans_cube_ ## T, c), \ + _trans_cube_ ## T ## _inverse) +#define _trans_mirrored(T, c) \ + invertco(compose(compose(_trans_cube_ ## T, c), \ + _trans_cube_ ## T ## _inverse)) + +_static cube_t +transform_edges(cube_t c, uint8_t t) +{ + switch (t) { + case _trans_UFr: + return c; + case _trans_ULr: + return _trans_edges_rotation(ULr, c); + case _trans_UBr: + return _trans_edges_rotation(UBr, c); + case _trans_URr: + return _trans_edges_rotation(URr, c); + case _trans_DFr: + return _trans_edges_rotation(DFr, c); + case _trans_DLr: + return _trans_edges_rotation(DLr, c); + case _trans_DBr: + return _trans_edges_rotation(DBr, c); + case _trans_DRr: + return _trans_edges_rotation(DRr, c); + case _trans_RUr: + return _trans_edges_rotation(RUr, c); + case _trans_RFr: + return _trans_edges_rotation(RFr, c); + case _trans_RDr: + return _trans_edges_rotation(RDr, c); + case _trans_RBr: + return _trans_edges_rotation(RBr, c); + case _trans_LUr: + return _trans_edges_rotation(LUr, c); + case _trans_LFr: + return _trans_edges_rotation(LFr, c); + case _trans_LDr: + return _trans_edges_rotation(LDr, c); + case _trans_LBr: + return _trans_edges_rotation(LBr, c); + case _trans_FUr: + return _trans_edges_rotation(FUr, c); + case _trans_FRr: + return _trans_edges_rotation(FRr, c); + case _trans_FDr: + return _trans_edges_rotation(FDr, c); + case _trans_FLr: + return _trans_edges_rotation(FLr, c); + case _trans_BUr: + return _trans_edges_rotation(BUr, c); + case _trans_BRr: + return _trans_edges_rotation(BRr, c); + case _trans_BDr: + return _trans_edges_rotation(BDr, c); + case _trans_BLr: + return _trans_edges_rotation(BLr, c); + case _trans_UFm: + return _trans_edges_mirrored(UFm, c); + case _trans_ULm: + return _trans_edges_mirrored(ULm, c); + case _trans_UBm: + return _trans_edges_mirrored(UBm, c); + case _trans_URm: + return _trans_edges_mirrored(URm, c); + case _trans_DFm: + return _trans_edges_mirrored(DFm, c); + case _trans_DLm: + return _trans_edges_mirrored(DLm, c); + case _trans_DBm: + return _trans_edges_mirrored(DBm, c); + case _trans_DRm: + return _trans_edges_mirrored(DRm, c); + case _trans_RUm: + return _trans_edges_mirrored(RUm, c); + case _trans_RFm: + return _trans_edges_mirrored(RFm, c); + case _trans_RDm: + return _trans_edges_mirrored(RDm, c); + case _trans_RBm: + return _trans_edges_mirrored(RBm, c); + case _trans_LUm: + return _trans_edges_mirrored(LUm, c); + case _trans_LFm: + return _trans_edges_mirrored(LFm, c); + case _trans_LDm: + return _trans_edges_mirrored(LDm, c); + case _trans_LBm: + return _trans_edges_mirrored(LBm, c); + case _trans_FUm: + return _trans_edges_mirrored(FUm, c); + case _trans_FRm: + return _trans_edges_mirrored(FRm, c); + case _trans_FDm: + return _trans_edges_mirrored(FDm, c); + case _trans_FLm: + return _trans_edges_mirrored(FLm, c); + case _trans_BUm: + return _trans_edges_mirrored(BUm, c); + case _trans_BRm: + return _trans_edges_mirrored(BRm, c); + case _trans_BDm: + return _trans_edges_mirrored(BDm, c); + case _trans_BLm: + return _trans_edges_mirrored(BLm, c); + default: + LOG("transform error, unknown transformation %" PRIu8 "\n", t); + return zero; + } +} + +_static cube_t +transform_corners(cube_t c, uint8_t t) +{ + switch (t) { + case _trans_UFr: + return c; + case _trans_ULr: + return _trans_corners_rotation(ULr, c); + case _trans_UBr: + return _trans_corners_rotation(UBr, c); + case _trans_URr: + return _trans_corners_rotation(URr, c); + case _trans_DFr: + return _trans_corners_rotation(DFr, c); + case _trans_DLr: + return _trans_corners_rotation(DLr, c); + case _trans_DBr: + return _trans_corners_rotation(DBr, c); + case _trans_DRr: + return _trans_corners_rotation(DRr, c); + case _trans_RUr: + return _trans_corners_rotation(RUr, c); + case _trans_RFr: + return _trans_corners_rotation(RFr, c); + case _trans_RDr: + return _trans_corners_rotation(RDr, c); + case _trans_RBr: + return _trans_corners_rotation(RBr, c); + case _trans_LUr: + return _trans_corners_rotation(LUr, c); + case _trans_LFr: + return _trans_corners_rotation(LFr, c); + case _trans_LDr: + return _trans_corners_rotation(LDr, c); + case _trans_LBr: + return _trans_corners_rotation(LBr, c); + case _trans_FUr: + return _trans_corners_rotation(FUr, c); + case _trans_FRr: + return _trans_corners_rotation(FRr, c); + case _trans_FDr: + return _trans_corners_rotation(FDr, c); + case _trans_FLr: + return _trans_corners_rotation(FLr, c); + case _trans_BUr: + return _trans_corners_rotation(BUr, c); + case _trans_BRr: + return _trans_corners_rotation(BRr, c); + case _trans_BDr: + return _trans_corners_rotation(BDr, c); + case _trans_BLr: + return _trans_corners_rotation(BLr, c); + case _trans_UFm: + return _trans_corners_mirrored(UFm, c); + case _trans_ULm: + return _trans_corners_mirrored(ULm, c); + case _trans_UBm: + return _trans_corners_mirrored(UBm, c); + case _trans_URm: + return _trans_corners_mirrored(URm, c); + case _trans_DFm: + return _trans_corners_mirrored(DFm, c); + case _trans_DLm: + return _trans_corners_mirrored(DLm, c); + case _trans_DBm: + return _trans_corners_mirrored(DBm, c); + case _trans_DRm: + return _trans_corners_mirrored(DRm, c); + case _trans_RUm: + return _trans_corners_mirrored(RUm, c); + case _trans_RFm: + return _trans_corners_mirrored(RFm, c); + case _trans_RDm: + return _trans_corners_mirrored(RDm, c); + case _trans_RBm: + return _trans_corners_mirrored(RBm, c); + case _trans_LUm: + return _trans_corners_mirrored(LUm, c); + case _trans_LFm: + return _trans_corners_mirrored(LFm, c); + case _trans_LDm: + return _trans_corners_mirrored(LDm, c); + case _trans_LBm: + return _trans_corners_mirrored(LBm, c); + case _trans_FUm: + return _trans_corners_mirrored(FUm, c); + case _trans_FRm: + return _trans_corners_mirrored(FRm, c); + case _trans_FDm: + return _trans_corners_mirrored(FDm, c); + case _trans_FLm: + return _trans_corners_mirrored(FLm, c); + case _trans_BUm: + return _trans_corners_mirrored(BUm, c); + case _trans_BRm: + return _trans_corners_mirrored(BRm, c); + case _trans_BDm: + return _trans_corners_mirrored(BDm, c); + case _trans_BLm: + return _trans_corners_mirrored(BLm, c); + default: + LOG("transform error, unknown transformation %" PRIu8 "\n", t); + return zero; + } +} + +_static cube_t +transform(cube_t c, uint8_t t) +{ + switch (t) { + case _trans_UFr: + return c; + case _trans_ULr: + return _trans_rotation(ULr, c); + case _trans_UBr: + return _trans_rotation(UBr, c); + case _trans_URr: + return _trans_rotation(URr, c); + case _trans_DFr: + return _trans_rotation(DFr, c); + case _trans_DLr: + return _trans_rotation(DLr, c); + case _trans_DBr: + return _trans_rotation(DBr, c); + case _trans_DRr: + return _trans_rotation(DRr, c); + case _trans_RUr: + return _trans_rotation(RUr, c); + case _trans_RFr: + return _trans_rotation(RFr, c); + case _trans_RDr: + return _trans_rotation(RDr, c); + case _trans_RBr: + return _trans_rotation(RBr, c); + case _trans_LUr: + return _trans_rotation(LUr, c); + case _trans_LFr: + return _trans_rotation(LFr, c); + case _trans_LDr: + return _trans_rotation(LDr, c); + case _trans_LBr: + return _trans_rotation(LBr, c); + case _trans_FUr: + return _trans_rotation(FUr, c); + case _trans_FRr: + return _trans_rotation(FRr, c); + case _trans_FDr: + return _trans_rotation(FDr, c); + case _trans_FLr: + return _trans_rotation(FLr, c); + case _trans_BUr: + return _trans_rotation(BUr, c); + case _trans_BRr: + return _trans_rotation(BRr, c); + case _trans_BDr: + return _trans_rotation(BDr, c); + case _trans_BLr: + return _trans_rotation(BLr, c); + case _trans_UFm: + return _trans_mirrored(UFm, c); + case _trans_ULm: + return _trans_mirrored(ULm, c); + case _trans_UBm: + return _trans_mirrored(UBm, c); + case _trans_URm: + return _trans_mirrored(URm, c); + case _trans_DFm: + return _trans_mirrored(DFm, c); + case _trans_DLm: + return _trans_mirrored(DLm, c); + case _trans_DBm: + return _trans_mirrored(DBm, c); + case _trans_DRm: + return _trans_mirrored(DRm, c); + case _trans_RUm: + return _trans_mirrored(RUm, c); + case _trans_RFm: + return _trans_mirrored(RFm, c); + case _trans_RDm: + return _trans_mirrored(RDm, c); + case _trans_RBm: + return _trans_mirrored(RBm, c); + case _trans_LUm: + return _trans_mirrored(LUm, c); + case _trans_LFm: + return _trans_mirrored(LFm, c); + case _trans_LDm: + return _trans_mirrored(LDm, c); + case _trans_LBm: + return _trans_mirrored(LBm, c); + case _trans_FUm: + return _trans_mirrored(FUm, c); + case _trans_FRm: + return _trans_mirrored(FRm, c); + case _trans_FDm: + return _trans_mirrored(FDm, c); + case _trans_FLm: + return _trans_mirrored(FLm, c); + case _trans_BUm: + return _trans_mirrored(BUm, c); + case _trans_BRm: + return _trans_mirrored(BRm, c); + case _trans_BDm: + return _trans_mirrored(BDm, c); + case _trans_BLm: + return _trans_mirrored(BLm, c); + default: + LOG("transform error, unknown transformation %" PRIu8 "\n", t); + return zero; + } +} diff --git a/src/cube.c b/src/cube.c deleted file mode 100644 index e8dedec..0000000 --- a/src/cube.c +++ /dev/null @@ -1,55 +0,0 @@ -#include -#include -#include -#include - -void (*nissy_log)(const char *, ...); - -#define LOG(...) if (nissy_log != NULL) nissy_log(__VA_ARGS__); - -#ifdef DEBUG -#define _static -#define _static_inline -#define DBG_WARN(condition, ...) if (!(condition)) LOG(__VA_ARGS__); -#define DBG_ASSERT(condition, retval, ...) \ - if (!(condition)) { LOG(__VA_ARGS__); return retval; } -#else -#define _static static -#define _static_inline static inline -#define DBG_WARN(condition, ...) -#define DBG_ASSERT(condition, retval, ...) -#endif - -#include "constants.h" -#include "utils.h" - -#if defined(CUBE_AVX2) -#include -#include "cube_avx2.h" -#elif defined(CUBE_NEON) -#include -#include -#include "cube_neon.h" -#else -#include /* TODO: check if can be removed */ -#include "cube_portable.h" -#endif - -#include "io_move_trans.h" -#include "constant_cubes.h" -#include "cube_generic.h" -#include "io_cube.h" - -/* TODO: work in progress */ -#if 0 -#include "constant_cubes_transform.h" -#include "cube_transform.h" -#else -#include "cube_transform_with_switch.h" -#endif - -#include "moves.h" -#include "solve_h48.h" -#include "solve_generic.h" - -#include "cube_public.h" diff --git a/src/cube.h b/src/cube.h deleted file mode 100644 index 346b99a..0000000 --- a/src/cube.h +++ /dev/null @@ -1,104 +0,0 @@ -/* -If you include this file, you should also include the following: - -inttypes, stdarg, stdbool, string - -All the functions below return 0 in case of success and a positive -number in case of error, unless otherwise specified. - -Arguments of type char [static 22] denote a cube in B32 format. -Other available formats are H48 and SRC. See README.md for more info on -these formats. - -Accepted moves are U, D, R, L, F and B, optionally followed by a 2, -a ' or a 3. - -A transformation must be given in the format - (rotation|mirrored) (2 letters) -for example 'rotation UF' or 'mirrored BL'. -*/ - -/* Apply the secod argument as a permutation on the first argument */ -int64_t nissy_compose( - const char cube[static 22], - const char permutation[static 22], - char result[static 22] -); - -/* Compute the inverse of the given cube */ -int64_t nissy_inverse( - const char cube[static 22], - char result[static 22] -); - -/* Apply the given sequence of moves on the given cube */ -int64_t nissy_applymoves( - const char cube[static 22], - const char *moves, - char result[static 22] -); - -/* Apply the single given transformation to the given cube */ -int64_t nissy_applytrans( - const char cube[static 22], - const char *transformation, - char result[static 22] -); - -/* Return the cube obtained by applying the given moves to the solved cube */ -int64_t nissy_frommoves( - const char *moves, - char result[static 22] -); - -/* Convert the given cube between the two given formats */ -int64_t nissy_convert( - const char *format_in, - const char *format_out, - const char *cube_string, - char *result -); - -/* Get the cube with the given ep, eo, cp and co values. */ -int64_t nissy_getcube( - int64_t ep, - int64_t eo, - int64_t cp, - int64_t co, - const char *options, - char result[static 22] -); - -/* -Returns the size of the data generated by nissy_gendata, when called with -the same parameters, or -1 in case of error. The returned value can be -slightly larger than the actual table size. -*/ -int64_t nissy_datasize( - const char *solver, - const char *options /* TODO: remove options, use only solver name */ -); - -/* Returns the number of bytes written, or -1 in case of error */ -int64_t nissy_gendata( - const char *solver, - const char *options, /* TODO: remove options, use only solver name */ - void *generated_data -); - -/* Returns the number of solutions found, or -1 in case of error */ -int64_t nissy_solve( - const char cube[static 22], - const char *solver, - const char *options, /* TODO: remove options, use only solver name */ - const char *nisstype, /* TODO: remove, use flags */ - int8_t minmoves, - int8_t maxmoves, - int64_t maxsolutions, - int8_t optimal, - const void *data, - char *solutions -); - -/* Set a global logger function used by this library. */ -void nissy_setlogger(void (*logger_function)(const char *, ...)); diff --git a/src/cube_avx2.h b/src/cube_avx2.h deleted file mode 100644 index b189023..0000000 --- a/src/cube_avx2.h +++ /dev/null @@ -1,341 +0,0 @@ -typedef __m256i cube_t; - -#define _co2_avx2 _mm256_set_epi64x(0, 0, 0, INT64_C(0x6060606060606060)) -#define _cocw_avx2 _mm256_set_epi64x(0, 0, 0, INT64_C(0x2020202020202020)) -#define _cp_avx2 _mm256_set_epi64x(0, 0, 0, INT64_C(0x0707070707070707)) -#define _ep_avx2 \ - _mm256_set_epi64x(INT64_C(0x0F0F0F0F), INT64_C(0x0F0F0F0F0F0F0F0F), 0, 0) -#define _eo_avx2 \ - _mm256_set_epi64x(INT64_C(0x10101010), INT64_C(0x1010101010101010), 0, 0) - -#define static_cube(c_ufr, c_ubl, c_dfl, c_dbr, c_ufl, c_ubr, c_dfr, c_dbl, \ - e_uf, e_ub, e_db, e_df, e_ur, e_ul, e_dl, e_dr, e_fr, e_fl, e_bl, e_br) \ - _mm256_set_epi8(0, 0, 0, 0, e_br, e_bl, e_fl, e_fr, \ - e_dr, e_dl, e_ul, e_ur, e_df, e_db, e_ub, e_uf, \ - 0, 0, 0, 0, 0, 0, 0, 0, \ - c_dbl, c_dfr, c_ubr, c_ufl, c_dbr, c_dfl, c_ubl, c_ufr) -#define zero _mm256_set_epi64x(0, 0, 0, 0) -#define solved static_cube( \ - 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) - -_static void pieces(cube_t *, uint8_t [static 8], uint8_t [static 12]); -_static_inline bool equal(cube_t, cube_t); -_static_inline cube_t invertco(cube_t); -_static_inline cube_t compose_epcpeo(cube_t, cube_t); -_static_inline cube_t compose_edges(cube_t, cube_t); -_static_inline cube_t compose_corners(cube_t, cube_t); -_static_inline cube_t compose(cube_t, cube_t); -_static_inline cube_t inverse(cube_t); - -_static_inline int64_t coord_co(cube_t); -_static_inline int64_t coord_csep(cube_t); -_static_inline int64_t coord_cocsep(cube_t); -_static_inline int64_t coord_eo(cube_t); -_static_inline int64_t coord_esep(cube_t); - -_static_inline void copy_corners(cube_t *, cube_t); -_static_inline void copy_edges(cube_t *, cube_t); -_static_inline void set_eo(cube_t *, int64_t); -_static_inline cube_t invcoord_esep(int64_t); - -_static void -pieces(cube_t *cube, uint8_t c[static 8], uint8_t e[static 12]) -{ - uint8_t aux[32]; - - _mm256_storeu_si256((__m256i_u *)aux, *cube); - memcpy(c, aux, 8); - memcpy(e, aux+16, 12); -} - -_static_inline bool -equal(cube_t c1, cube_t c2) -{ - int32_t mask; - __m256i cmp; - - cmp = _mm256_cmpeq_epi8(c1, c2); - mask = _mm256_movemask_epi8(cmp); - - return mask == ~0; -} - -_static_inline cube_t -invertco(cube_t c) -{ - cube_t co, shleft, shright, summed, newco, cleanco, ret; - - co = _mm256_and_si256(c, _co2_avx2); - shleft = _mm256_slli_epi32(co, 1); - shright = _mm256_srli_epi32(co, 1); - summed = _mm256_or_si256(shleft, shright); - newco = _mm256_and_si256(summed, _co2_avx2); - cleanco = _mm256_xor_si256(c, co); - ret = _mm256_or_si256(cleanco, newco); - - return ret; -} - -_static_inline cube_t -compose_epcpeo(cube_t c1, cube_t c2) -{ - cube_t b, s, eo2; - - /* Permute and clean unused bits */ - s = _mm256_shuffle_epi8(c1, c2); - b = _mm256_set_epi8( - ~0, ~0, ~0, ~0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ~0, ~0, ~0, ~0, ~0, ~0, ~0, ~0, 0, 0, 0, 0, 0, 0, 0, 0 - ); - s = _mm256_andnot_si256(b, s); - - /* Change EO */ - eo2 = _mm256_and_si256(c2, _eo_avx2); - s = _mm256_xor_si256(s, eo2); - - return s; -} - -_static_inline cube_t -compose_edges(cube_t c1, cube_t c2) -{ - return compose_epcpeo(c1, c2); -} - -_static_inline cube_t -compose_corners(cube_t c1, cube_t c2) -{ - /* - * We do a full compose. Minor optimizations are possible, like - * saving one instruction by not doing EO, but it should not - * be significant. - */ - return compose(c1, c2); -} - -_static_inline cube_t -compose(cube_t c1, cube_t c2) -{ - cube_t s, co1, co2, aux, auy1, auy2, auz1, auz2; - - s = compose_epcpeo(c1, c2); - - /* Change CO */ - co1 = _mm256_and_si256(s, _co2_avx2); - co2 = _mm256_and_si256(c2, _co2_avx2); - aux = _mm256_add_epi8(co1, co2); - auy1 = _mm256_add_epi8(aux, _cocw_avx2); - auy2 = _mm256_srli_epi32(auy1, 2); - auz1 = _mm256_add_epi8(aux, auy2); - auz2 = _mm256_and_si256(auz1, _co2_avx2); - - /* Put together */ - s = _mm256_andnot_si256(_co2_avx2, s); - s = _mm256_or_si256(s, auz2); - - return s; -} - -_static_inline cube_t -cleanaftershuffle(cube_t c) -{ - __m256i b; - - b = _mm256_set_epi8( - ~0, ~0, ~0, ~0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - ~0, ~0, ~0, ~0, ~0, ~0, ~0, ~0, 0, 0, 0, 0, 0, 0, 0, 0 - ); - - return _mm256_andnot_si256(b, c); -} - -_static_inline cube_t -inverse(cube_t c) -{ - /* Method taken from Andrew Skalski's vcube[1]. The addition sequence - * was generated using [2]. - * [1] https://github.com/Voltara/vcube - * [2] http://wwwhomes.uni-bielefeld.de/achim/addition_chain.html - */ - cube_t v3, vi, vo, vp, ret; - - v3 = _mm256_shuffle_epi8(c, c); - v3 = _mm256_shuffle_epi8(v3, c); - vi = _mm256_shuffle_epi8(v3, v3); - vi = _mm256_shuffle_epi8(vi, vi); - vi = _mm256_shuffle_epi8(vi, vi); - vi = _mm256_shuffle_epi8(vi, v3); - vi = _mm256_shuffle_epi8(vi, vi); - vi = _mm256_shuffle_epi8(vi, vi); - vi = _mm256_shuffle_epi8(vi, vi); - vi = _mm256_shuffle_epi8(vi, vi); - vi = _mm256_shuffle_epi8(vi, c); - vi = _mm256_shuffle_epi8(vi, vi); - vi = _mm256_shuffle_epi8(vi, vi); - vi = _mm256_shuffle_epi8(vi, vi); - vi = _mm256_shuffle_epi8(vi, vi); - vi = _mm256_shuffle_epi8(vi, vi); - vi = _mm256_shuffle_epi8(vi, v3); - vi = _mm256_shuffle_epi8(vi, vi); - vi = _mm256_shuffle_epi8(vi, c); - - vo = _mm256_and_si256(c, _mm256_or_si256(_eo_avx2, _co2_avx2)); - vo = _mm256_shuffle_epi8(vo, vi); - vp = _mm256_andnot_si256(_mm256_or_si256(_eo_avx2, _co2_avx2), vi); - ret = _mm256_or_si256(vp, vo); - ret = cleanaftershuffle(ret); - - return invertco(ret); -} - -_static_inline int64_t -coord_co(cube_t c) -{ - cube_t co; - int64_t mem[4], ret, i, p; - - co = _mm256_and_si256(c, _co2_avx2); - _mm256_storeu_si256((__m256i *)mem, co); - - mem[0] >>= 5; - for (i = 0, ret = 0, p = 1; i < 7; i++, mem[0] >>= 8, p *= 3) - ret += (mem[0] & 3) * p; - - return ret; -} - -_static_inline int64_t -coord_csep(cube_t c) -{ - cube_t cp, shifted; - int64_t mask; - - cp = _mm256_and_si256(c, _cp_avx2); - shifted = _mm256_slli_epi32(cp, 5); - mask = _mm256_movemask_epi8(shifted); - - return mask & 0x7F; -} - -_static_inline int64_t -coord_cocsep(cube_t c) -{ - return (coord_co(c) << 7) + coord_csep(c); -} - -_static_inline int64_t -coord_eo(cube_t c) -{ - cube_t eo, shifted; - int64_t mask; - - eo = _mm256_and_si256(c, _eo_avx2); - shifted = _mm256_slli_epi32(eo, 3); - mask = _mm256_movemask_epi8(shifted); - - return mask >> 17; -} - -_static_inline int64_t -coord_esep(cube_t c) -{ - cube_t ep; - int64_t e, mem[4], i, j, jj, k, l, ret1, ret2, bit1, bit2, is1; - - ep = _mm256_and_si256(c, _ep_avx2); - _mm256_storeu_si256((__m256i *)mem, ep); - - mem[3] <<= 8; - ret1 = ret2 = 0; - k = l = 4; - for (i = 0, j = 0; i < 12; i++, mem[i/8 + 2] >>= 8) { - e = mem[i/8 + 2]; - - bit1 = (e & _esepbit1) >> 2; - bit2 = (e & _esepbit2) >> 3; - is1 = (1 - bit2) * bit1; - - ret1 += bit2 * binomial[11-i][k]; - k -= bit2; - - jj = j < 8; - ret2 += jj * is1 * binomial[7-(j*jj)][l]; - l -= is1; - j += (1-bit2); - } - - return ret1 * 70 + ret2; -} - -_static_inline void -copy_corners(cube_t *dest, cube_t src) -{ - *dest = _mm256_blend_epi32(*dest, src, 0x0F); -} - -_static_inline void -copy_edges(cube_t *dest, cube_t src) -{ - *dest = _mm256_blend_epi32(*dest, src, 0xF0); -} - -_static_inline void -set_eo(cube_t *cube, int64_t eo) -{ - int64_t eo12, eotop, eobot; - __m256i veo; - - eo12 = (eo << 1) + (_mm_popcnt_u64(eo) % 2); - eotop = (eo12 & (1 << 11)) << 17 | - (eo12 & (1 << 10)) << 10 | - (eo12 & (1 << 9)) << 3 | - (eo12 & (1 << 8)) >> 4; - eobot = (eo12 & (1 << 7)) << 53 | - (eo12 & (1 << 6)) << 46 | - (eo12 & (1 << 5)) << 39 | - (eo12 & (1 << 4)) << 32 | - (eo12 & (1 << 3)) << 25 | - (eo12 & (1 << 2)) << 18 | - (eo12 & (1 << 1)) << 11 | - (eo12 & 1) << 4; - veo = _mm256_set_epi64x(eotop, eobot, 0, 0); - - *cube = _mm256_andnot_si256(_eo_avx2, *cube); - *cube = _mm256_or_si256(*cube, veo); -} - -_static_inline cube_t -invcoord_esep(int64_t esep) -{ - cube_t eee, ret; - int64_t bit1, bit2, i, j, jj, k, l, s, v, w, is1, set1, set2; - uint8_t mem[32]; - uint8_t slice[3] = {0}; - - set1 = esep % 70; - set2 = esep / 70; - - for (i = 0, j = 0, k = 4, l = 4; i < 12; i++) { - v = binomial[11-i][k]; - jj = j < 8; - w = jj * binomial[7-(j*jj)][l]; - bit2 = set2 >= v; - bit1 = set1 >= w; - is1 = (1 - bit2) * bit1; - - set2 -= bit2 * v; - k -= bit2; - set1 -= is1 * w; - l -= is1; - j += (1-bit2); - s = 2*bit2 + (1-bit2)*bit1; - - mem[i+16] = (slice[s]++) | (uint8_t)(s << 2); - } - - ret = solved; - eee = _mm256_loadu_si256((__m256i_u *)&mem); - copy_edges(&ret, eee); - - return ret; -} diff --git a/src/cube_generic.h b/src/cube_generic.h deleted file mode 100644 index fe36f90..0000000 --- a/src/cube_generic.h +++ /dev/null @@ -1,291 +0,0 @@ -#define _move(M, c) compose(c, _move_cube_ ## M) -#define _premove(M, c) compose(_move_cube_ ## M, c) - -_static cube_t cubefromarray(uint8_t [static 8], uint8_t [static 12]); -_static cube_t solvedcube(void); -_static bool isconsistent(cube_t); -_static bool issolvable(cube_t); -_static bool issolved(cube_t); -_static bool iserror(cube_t); -_static cube_t applymoves(cube_t, const char *); -_static cube_t applytrans(cube_t, const char *); -_static cube_t frommoves(const char *); -_static void getcube_fix(int64_t *, int64_t *, int64_t *, int64_t *); -_static cube_t getcube(int64_t, int64_t, int64_t, int64_t); - -_static cube_t move(cube_t, uint8_t); -_static cube_t transform_edges(cube_t, uint8_t); -_static cube_t transform_corners(cube_t, uint8_t); -_static cube_t transform(cube_t, uint8_t); - -_static cube_t -cubefromarray(uint8_t c[static 8], uint8_t e[static 12]) -{ - return static_cube( - c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], - e[0], e[1], e[2], e[3], e[4], e[5], e[6], e[7], - e[8], e[9], e[10], e[11]); -} - -_static cube_t -solvedcube(void) -{ - return solved; -} - -_static bool -isconsistent(cube_t cube) -{ - uint8_t i, p, e, piece, corner[8], edge[12]; - bool found[12]; - - pieces(&cube, corner, edge); - - for (i = 0; i < 12; i++) - found[i] = false; - for (i = 0; i < 12; i++) { - piece = edge[i]; - p = piece & _pbits; - e = piece & _eobit; - if (p >= 12) - goto inconsistent_ep; - if (e != 0 && e != _eobit) - goto inconsistent_eo; - found[p] = true; - } - for (i = 0; i < 12; i++) - if (!found[i]) - goto inconsistent_ep; - - for (i = 0; i < 8; i++) - found[i] = false; - for (i = 0; i < 8; i++) { - piece = corner[i]; - p = piece & _pbits; - e = piece & _cobits; - if (p >= 8) - goto inconsistent_cp; - if (e != 0 && e != _ctwist_cw && e != _ctwist_ccw) - goto inconsistent_co; - found[p] = true; - } - for (i = 0; i < 8; i++) - if (!found[i]) - goto inconsistent_co; - - return true; - -inconsistent_ep: - LOG("Inconsistent EP\n"); - return false; -inconsistent_cp: - LOG("Inconsistent CP\n"); - return false; -inconsistent_eo: - LOG("Inconsistent EO\n"); - return false; -inconsistent_co: - LOG("Inconsistent CO\n"); - return false; -} - -_static bool -issolvable(cube_t cube) -{ - uint8_t i, eo, co, piece, edge[12], corner[8], ep[12], cp[8]; - - DBG_ASSERT(isconsistent(cube), false, - "issolvable: cube is inconsistent\n"); - - pieces(&cube, corner, edge); - for (i = 0; i < 12; i++) - ep[i] = edge[i] & _pbits; - for (i = 0; i < 8; i++) - cp[i] = corner[i] & _pbits; - - if (permsign(ep, 12) != permsign(cp, 8)) - goto issolvable_parity; - - eo = 0; - for (i = 0; i < 12; i++) { - piece = edge[i]; - eo += (piece & _eobit) >> _eoshift; - } - if (eo % 2 != 0) - goto issolvable_eo; - - co = 0; - for (i = 0; i < 8; i++) { - piece = corner[i]; - co += (piece & _cobits) >> _coshift; - } - if (co % 3 != 0) - goto issolvable_co; - - return true; - -issolvable_parity: - LOG("EP and CP parities are different\n"); - return false; -issolvable_eo: - LOG("Odd number of flipped edges\n"); - return false; -issolvable_co: - LOG("Sum of corner orientation is not multiple of 3\n"); - return false; -} - -bool -issolved(cube_t cube) -{ - return equal(cube, solved); -} - -bool -iserror(cube_t cube) -{ - return equal(cube, zero); -} - -_static cube_t -applymoves(cube_t cube, const char *buf) -{ - uint8_t r, m; - const char *b; - - DBG_ASSERT(isconsistent(cube), zero, - "move error: inconsistent cube\n"); - - for (b = buf; *b != '\0'; b++) { - while (*b == ' ' || *b == '\t' || *b == '\n') - b++; - if (*b == '\0') - goto applymoves_finish; - if ((r = readmove(*b)) == _error) - goto applymoves_error; - if ((m = readmodifier(*(b+1))) != 0) - b++; - cube = move(cube, r + m); - } - -applymoves_finish: - return cube; - -applymoves_error: - LOG("applymoves error\n"); - return zero; -} - -_static cube_t -frommoves(const char *buf) -{ - return applymoves(solved, buf); -} - -_static void -getcube_fix(int64_t *ep, int64_t *eo, int64_t *cp, int64_t *co) -{ - uint8_t e[12], c[8], coarr[8]; - - *ep = (*ep % _12f + _12f) % _12f; - *eo = (*eo % _2p11 + _2p11) % _2p11; - *cp = (*cp % _8f + _8f) % _8f; - *co = (*cp % _3p7 + _3p7) % _3p7; - - indextoperm(*ep, 12, e); - indextoperm(*cp, 8, c); - if (permsign(e, 12) != permsign(c, 8)) { - _swap(c[0], c[1]); - *cp = permtoindex(c, 8); - - sumzerotodigits(*co, 8, 3, coarr); - _swap(coarr[0], coarr[1]); - *co = digitstosumzero(coarr, 8, 3); - } -} - -_static cube_t -getcube(int64_t ep, int64_t eo, int64_t cp, int64_t co) -{ - uint8_t i, earr[12], carr[8], eoarr[12], coarr[8]; - - sumzerotodigits(eo, 12, 2, eoarr); - DBG_ASSERT(eoarr[0] != _error, zero, "Error making EO"); - indextoperm(ep, 12, earr); - DBG_ASSERT(earr[0] != _error, zero, "Error making EP"); - for (i = 0; i < 12; i++) - earr[i] |= eoarr[i] << _eoshift; - - sumzerotodigits(co, 8, 3, coarr); - DBG_ASSERT(coarr[0] != _error, zero, "Error making CO"); - indextoperm(cp, 8, carr); - DBG_ASSERT(carr[0] != _error, zero, "Error making CP"); - for (i = 0; i < 8; i++) - carr[i] |= coarr[i] << _coshift; - - return cubefromarray(carr, earr); -} - -_static cube_t -applytrans(cube_t cube, const char *buf) -{ - uint8_t t; - - DBG_ASSERT(isconsistent(cube), zero, - "transformation error: inconsistent cube\n"); - - t = readtrans(buf); - - return transform(cube, t); -} - -_static cube_t -move(cube_t c, uint8_t m) -{ - switch (m) { - case _move_U: - return _move(U, c); - case _move_U2: - return _move(U2, c); - case _move_U3: - return _move(U3, c); - case _move_D: - return _move(D, c); - case _move_D2: - return _move(D2, c); - case _move_D3: - return _move(D3, c); - case _move_R: - return _move(R, c); - case _move_R2: - return _move(R2, c); - case _move_R3: - return _move(R3, c); - case _move_L: - return _move(L, c); - case _move_L2: - return _move(L2, c); - case _move_L3: - return _move(L3, c); - case _move_F: - return _move(F, c); - case _move_F2: - return _move(F2, c); - case _move_F3: - return _move(F3, c); - case _move_B: - return _move(B, c); - case _move_B2: - return _move(B2, c); - case _move_B3: - return _move(B3, c); - default: - LOG("move error, unknown move\n"); - return zero; - } -} - -/* -TODO transform is now relegated to a separated file because it is too long. -It would be nice to make it shorter without loosing performance. -*/ diff --git a/src/cube_neon.h b/src/cube_neon.h deleted file mode 100644 index 23e5565..0000000 --- a/src/cube_neon.h +++ /dev/null @@ -1,377 +0,0 @@ -// cube_t -typedef struct -{ - uint8x16_t corner; - uint8x16_t edge; -} cube_t; - -#define _co2_neon vdupq_n_u8(0x60) -#define _cocw_neon vdupq_n_u8(0x20) -#define _cp_neon vdupq_n_u8(0x07) -#define _ep_neon vcombine_u8(vdupq_n_u8(0x0F), vdupq_n_u8(0x0F)) -#define _eo_neon vcombine_u8(vdupq_n_u8(0x10), vdupq_n_u8(0x10)) - -// static cube -#define static_cube(c_ufr, c_ubl, c_dfl, c_dbr, c_ufl, c_ubr, c_dfr, c_dbl, \ - e_uf, e_ub, e_db, e_df, e_ur, e_ul, e_dl, e_dr, e_fr, e_fl, e_bl, e_br) \ - ((cube_t){ \ - .corner = {c_ufr, c_ubl, c_dfl, c_dbr, c_ufl, c_ubr, c_dfr, c_dbl, 0, 0, 0, 0, 0, 0, 0, 0}, \ - .edge = {e_uf, e_ub, e_db, e_df, e_ur, e_ul, e_dl, e_dr, e_fr, e_fl, e_bl, e_br, 0, 0, 0, 0}}) - -// zero cube -#define zero \ - (cube_t) \ - { \ - .corner = vdupq_n_u8(0), \ - .edge = vdupq_n_u8(0) \ - } - -// solved cube -#define solved static_cube( \ - 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) - -// Functions -_static void pieces(cube_t *, uint8_t[static 8], uint8_t[static 12]); -_static_inline bool equal(cube_t, cube_t); -_static_inline cube_t invertco(cube_t); -_static_inline cube_t compose_edges(cube_t, cube_t); -_static_inline cube_t compose_corners(cube_t, cube_t); -_static_inline uint8x16_t compose_edges_slim(uint8x16_t, uint8x16_t); -_static_inline uint8x16_t compose_corners_slim(uint8x16_t, uint8x16_t); -_static_inline cube_t compose(cube_t, cube_t); -_static_inline cube_t inverse(cube_t); - -_static_inline int64_t coord_co(cube_t); -_static_inline int64_t coord_csep(cube_t); -_static_inline int64_t coord_cocsep(cube_t); -_static_inline int64_t coord_eo(cube_t); -_static_inline int64_t coord_esep(cube_t); - -_static_inline void copy_corners(cube_t *, cube_t); -_static_inline void copy_edges(cube_t *, cube_t); -_static_inline void set_eo(cube_t *, int64_t); -_static_inline cube_t invcoord_esep(int64_t); - -_static void -pieces(cube_t *cube, uint8_t c[static 8], uint8_t e[static 12]) -{ - // First 8 bytes of the corner vector are copied from the c array - vst1_u8(c, vget_low_u8(cube->corner)); - - // 12 bytes of the edge vector are copied from the e array - // First 8 bytes - vst1_u8(e, vget_low_u8(cube->edge)); - // Next 4 bytes - vst1_lane_u32((uint32_t *)(e + 8), vreinterpret_u32_u8(vget_high_u8(cube->edge)), 0); -} - -_static_inline bool -equal(cube_t c1, cube_t c2) -{ - uint8x16_t cmp_corner, cmp_edge; - uint64x2_t cmp_corner_u64, cmp_edge_u64; - uint64x2_t cmp_result; - - // compare the corner vectors - cmp_corner = vceqq_u8(c1.corner, c2.corner); - // compare the edge vectors - cmp_edge = vceqq_u8(c1.edge, c2.edge); - - // convert the comparison vectors to 64-bit vectors - cmp_corner_u64 = vreinterpretq_u64_u8(cmp_corner); - cmp_edge_u64 = vreinterpretq_u64_u8(cmp_edge); - - // combine the comparison vectors - cmp_result = vandq_u64(cmp_corner_u64, cmp_edge_u64); - - // check if all the bits are set - return vgetq_lane_u64(cmp_result, 0) == ~0ULL && vgetq_lane_u64(cmp_result, 1) == ~0ULL; -} - -_static_inline cube_t -invertco(cube_t c) -{ - cube_t ret; - uint8x16_t co, shleft, shright, summed, newco, cleanco; - - co = vandq_u8(c.corner, _co2_neon); - shleft = vshlq_n_u8(co, 1); - shright = vshrq_n_u8(co, 1); - summed = vorrq_u8(shleft, shright); - newco = vandq_u8(summed, _co2_neon); - cleanco = veorq_u8(c.corner, co); - ret.corner = vorrq_u8(cleanco, newco); - ret.edge = c.edge; - - return ret; -} - -_static_inline cube_t -compose_edges(cube_t c1, cube_t c2) -{ - cube_t ret = {0}; - ret.edge = compose_edges_slim(c1.edge, c2.edge); - return ret; -} - -_static_inline cube_t -compose_corners(cube_t c1, cube_t c2) -{ - cube_t ret = {0}; - ret.corner = compose_corners_slim(c1.corner, c2.corner); - return ret; -} - -_static_inline uint8x16_t -compose_edges_slim(uint8x16_t edge1, uint8x16_t edge2) -{ - // Masks - uint8x16_t p_bits = vdupq_n_u8(_pbits); - uint8x16_t eo_bit = vdupq_n_u8(_eobit); - - // Find the index and permutation - uint8x16_t p = vandq_u8(edge2, p_bits); - uint8x16_t piece1 = vqtbl1q_u8(edge1, p); - - // Calculate the orientation through XOR - uint8x16_t orien = vandq_u8(veorq_u8(edge2, piece1), eo_bit); - - // Combine the results - uint8x16_t ret = vorrq_u8(vandq_u8(piece1, p_bits), orien); - - // Mask to clear the last 32 bits of the result - uint8x16_t mask_last_32 = vsetq_lane_u32(0, vreinterpretq_u32_u8(ret), 3); - ret = vreinterpretq_u8_u32(mask_last_32); - - return ret; -} - -_static_inline uint8x16_t -compose_corners_slim(uint8x16_t corner1, uint8x16_t corner2) -{ - // Masks - uint8x16_t p_bits = vdupq_n_u8(_pbits); - uint8x16_t cobits = vdupq_n_u8(_cobits); - uint8x16_t cobits2 = vdupq_n_u8(_cobits2); - uint8x16_t twist_cw = vdupq_n_u8(_ctwist_cw); - - // Find the index and permutation - uint8x16_t p = vandq_u8(corner2, p_bits); - uint8x16_t piece1 = vqtbl1q_u8(corner1, p); - - // Calculate the orientation - uint8x16_t aux = vaddq_u8(vandq_u8(corner2, cobits), vandq_u8(piece1, cobits)); - uint8x16_t auy = vshrq_n_u8(vaddq_u8(aux, twist_cw), 2); - uint8x16_t orien = vandq_u8(vaddq_u8(aux, auy), cobits2); - - // Combine the results - uint8x16_t ret = vorrq_u8(vandq_u8(piece1, p_bits), orien); - - // Mask to clear the last 64 bits of the result - uint8x16_t mask_last_64 = vsetq_lane_u64(0, vreinterpretq_u64_u8(ret), 1); - ret = vreinterpretq_u8_u64(mask_last_64); - - return ret; -} - -_static_inline cube_t -compose(cube_t c1, cube_t c2) -{ - cube_t ret = {0}; - - ret.edge = compose_edges_slim(c1.edge, c2.edge); - ret.corner = compose_corners_slim(c1.corner, c2.corner); - - return ret; -} - -_static_inline cube_t -inverse(cube_t cube) -{ - uint8_t i, piece, orien; - cube_t ret; - - // Temp arrays to store the NEON vectors - uint8_t edges[16]; - uint8_t corners[16]; - - // Copy the NEON vectors to the arrays - vst1q_u8(edges, cube.edge); - vst1q_u8(corners, cube.corner); - - uint8_t edge_result[16] = {0}; - uint8_t corner_result[16] = {0}; - - // Process the edges - for (i = 0; i < 12; i++) - { - piece = edges[i]; - orien = piece & _eobit; - edge_result[piece & _pbits] = i | orien; - } - - // Process the corners - for (i = 0; i < 8; i++) - { - piece = corners[i]; - orien = ((piece << 1) | (piece >> 1)) & _cobits2; - corner_result[piece & _pbits] = i | orien; - } - - // Copy the results back to the NEON vectors - ret.edge = vld1q_u8(edge_result); - ret.corner = vld1q_u8(corner_result); - - return ret; -} - -_static_inline int64_t -coord_co(cube_t c) -{ - // Temp array to store the NEON vector - uint8_t mem[16]; - vst1q_u8(mem, c.corner); - - int i, p; - int64_t ret; - - for (ret = 0, i = 0, p = 1; i < 7; i++, p *= 3) - ret += p * (mem[i] >> _coshift); - - return ret; -} - -_static_inline int64_t -coord_csep(cube_t c) -{ - // Temp array to store the NEON vector - uint8_t mem[16]; - vst1q_u8(mem, c.corner); - - int64_t ret = 0; - int i, p; - for (ret = 0, i = 0, p = 1; i < 7; i++, p *= 2) - ret += p * ((mem[i] & _csepbit) >> 2); - - return ret; - return 0; -} - -_static_inline int64_t -coord_cocsep(cube_t c) -{ - return (coord_co(c) << 7) + coord_csep(c); -} - -_static_inline int64_t -coord_eo(cube_t c) -{ - int64_t ret = 0; - int64_t p = 1; - - // Temp array to store the NEON vector - uint8_t mem[16]; - vst1q_u8(mem, c.edge); - - for (int i = 1; i < 12; i++, p *= 2) - { - ret += p * (mem[i] >> _eoshift); - } - - return ret; -} - -_static_inline int64_t -coord_esep(cube_t c) -{ - int64_t i, j, jj, k, l, ret1, ret2, bit1, bit2, is1; - - // Temp array to store the NEON vector - uint8_t mem[16]; - vst1q_u8(mem, c.edge); - - for (i = 0, j = 0, k = 4, l = 4, ret1 = 0, ret2 = 0; i < 12; i++) - { - bit1 = (mem[i] & _esepbit1) >> 2; - bit2 = (mem[i] & _esepbit2) >> 3; - is1 = (1 - bit2) * bit1; - - ret1 += bit2 * binomial[11 - i][k]; - k -= bit2; - - jj = j < 8; - ret2 += jj * is1 * binomial[7 - (j * jj)][l]; - l -= is1; - j += (1 - bit2); - } - - return ret1 * 70 + ret2; -} - -_static_inline void -copy_corners(cube_t *dst, cube_t src) -{ - dst->corner = src.corner; -} - -_static_inline void -copy_edges(cube_t *dst, cube_t src) -{ - dst->edge = src.edge; -} - -_static_inline void -set_eo(cube_t *cube, int64_t eo) -{ - // Temp array to store the NEON vector - uint8_t mem[16]; - vst1q_u8(mem, cube->edge); - uint8_t i, sum, flip; - - for (sum = 0, i = 1; i < 12; i++, eo >>= 1) - { - flip = eo % 2; - sum += flip; - mem[i] = (mem[i] & ~_eobit) | (_eobit * flip); - } - mem[0] = (mem[0] & ~_eobit) | (_eobit * (sum % 2)); - - // Copy the results back to the NEON vector - cube->edge = vld1q_u8(mem); - return; -} - -_static_inline cube_t -invcoord_esep(int64_t esep) -{ - cube_t ret; - int64_t bit1, bit2, i, j, jj, k, l, s, v, w, is1, set1, set2; - uint8_t slice[3] = {0}; - - ret = solved; - uint8_t mem[16]; - set1 = esep % 70; - set2 = esep / 70; - - for (i = 0, j = 0, k = 4, l = 4; i < 12; i++) - { - v = binomial[11 - i][k]; - jj = j < 8; - w = jj * binomial[7 - (j * jj)][l]; - bit2 = set2 >= v; - bit1 = set1 >= w; - is1 = (1 - bit2) * bit1; - - set2 -= bit2 * v; - k -= bit2; - set1 -= is1 * w; - l -= is1; - j += (1 - bit2); - s = 2 * bit2 + (1 - bit2) * bit1; - - mem[i] = (slice[s]++) | (uint8_t)(s << 2); - } - - ret.edge = vld1q_u8(mem); - return ret; -} diff --git a/src/cube_portable.h b/src/cube_portable.h deleted file mode 100644 index 77e803b..0000000 --- a/src/cube_portable.h +++ /dev/null @@ -1,298 +0,0 @@ -typedef struct { - uint8_t corner[8]; - uint8_t edge[12]; -} cube_t; - -#define static_cube(c_ufr, c_ubl, c_dfl, c_dbr, c_ufl, c_ubr, c_dfr, c_dbl, \ - e_uf, e_ub, e_db, e_df, e_ur, e_ul, e_dl, e_dr, e_fr, e_fl, e_bl, e_br) \ - ((cube_t) { \ - .corner = { c_ufr, c_ubl, c_dfl, c_dbr, c_ufl, c_ubr, c_dfr, c_dbl }, \ - .edge = { e_uf, e_ub, e_db, e_df, e_ur, e_ul, \ - e_dl, e_dr, e_fr, e_fl, e_bl, e_br } }) -#define zero static_cube( \ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) -#define solved static_cube( \ - 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) - -_static void pieces(cube_t *, uint8_t [static 8], uint8_t [static 12]); -_static_inline bool equal(cube_t, cube_t); -_static_inline cube_t invertco(cube_t); -_static_inline void compose_edges_inplace(cube_t, cube_t, cube_t *); -_static_inline void compose_corners_inplace(cube_t, cube_t, cube_t *); -_static_inline cube_t compose_edges(cube_t, cube_t); -_static_inline cube_t compose_corners(cube_t, cube_t); -_static_inline cube_t compose(cube_t, cube_t); -_static_inline cube_t inverse(cube_t); - -_static_inline int64_t coord_co(cube_t); -_static_inline int64_t coord_csep(cube_t); -_static_inline int64_t coord_cocsep(cube_t); -_static_inline int64_t coord_eo(cube_t); -_static_inline int64_t coord_esep(cube_t); - -_static_inline void copy_corners(cube_t *, cube_t); -_static_inline void copy_edges(cube_t *, cube_t); -_static_inline void set_eo(cube_t *, int64_t); -_static_inline cube_t invcoord_esep(int64_t); - -_static void -pieces(cube_t *cube, uint8_t c[static 8], uint8_t e[static 12]) -{ - memcpy(c, cube->corner, 8); - memcpy(e, cube->edge, 12); -} - -_static_inline bool -equal(cube_t c1, cube_t c2) -{ - uint8_t i; - bool ret; - - ret = true; - for (i = 0; i < 8; i++) - ret = ret && c1.corner[i] == c2.corner[i]; - for (i = 0; i < 12; i++) - ret = ret && c1.edge[i] == c2.edge[i]; - - return ret; -} - -_static_inline cube_t -invertco(cube_t c) -{ - uint8_t i, piece, orien; - cube_t ret; - - ret = c; - for (i = 0; i < 8; i++) { - piece = c.corner[i]; - orien = ((piece << 1) | (piece >> 1)) & _cobits2; - ret.corner[i] = (piece & _pbits) | orien; - } - - return ret; -} - -_static_inline void -compose_edges_inplace(cube_t c1, cube_t c2, cube_t *ret) -{ - uint8_t i, piece1, piece2, p, orien; - - for (i = 0; i < 12; i++) { - piece2 = c2.edge[i]; - p = piece2 & _pbits; - piece1 = c1.edge[p]; - orien = (piece2 ^ piece1) & _eobit; - ret->edge[i] = (piece1 & _pbits) | orien; - } -} - -_static_inline void -compose_corners_inplace(cube_t c1, cube_t c2, cube_t *ret) -{ - uint8_t i, piece1, piece2, p, orien, aux, auy; - - for (i = 0; i < 8; i++) { - piece2 = c2.corner[i]; - p = piece2 & _pbits; - piece1 = c1.corner[p]; - aux = (piece2 & _cobits) + (piece1 & _cobits); - auy = (aux + _ctwist_cw) >> 2; - orien = (aux + auy) & _cobits2; - ret->corner[i] = (piece1 & _pbits) | orien; - } -} - -_static_inline cube_t -compose_edges(cube_t c1, cube_t c2) -{ - cube_t ret = zero; - - compose_edges_inplace(c1, c2, &ret); - - return ret; -} - -_static_inline cube_t -compose_corners(cube_t c1, cube_t c2) -{ - cube_t ret = zero; - - compose_corners_inplace(c1, c2, &ret); - - return ret; -} - -_static_inline cube_t -compose(cube_t c1, cube_t c2) -{ - cube_t ret = zero; - - compose_edges_inplace(c1, c2, &ret); - compose_corners_inplace(c1, c2, &ret); - - return ret; -} - -cube_t -inverse(cube_t cube) -{ - uint8_t i, piece, orien; - cube_t ret; - - for (i = 0; i < 12; i++) { - piece = cube.edge[i]; - orien = piece & _eobit; - ret.edge[piece & _pbits] = i | orien; - } - - for (i = 0; i < 8; i++) { - piece = cube.corner[i]; - orien = ((piece << 1) | (piece >> 1)) & _cobits2; - ret.corner[piece & _pbits] = i | orien; - } - - return ret; -} - -_static_inline int64_t -coord_co(cube_t c) -{ - int i, p; - int64_t ret; - - for (ret = 0, i = 0, p = 1; i < 7; i++, p *= 3) - ret += p * (c.corner[i] >> _coshift); - - return ret; -} - -/* -For corner separation, we consider the axis (a.k.a. tetrad) each -corner belongs to as 0 or 1 and we translate this sequence into binary. -Ignoring the last bit, we have a value up to 2^7, but not all values are -possible. Encoding this as a number from 0 to C(8,4) would save about 40% -of space, but we are not going to use this coordinate in large tables. -*/ -_static_inline int64_t -coord_csep(cube_t c) -{ - int i, p; - int64_t ret; - - for (ret = 0, i = 0, p = 1; i < 7; i++, p *= 2) - ret += p * ((c.corner[i] & _csepbit) >> 2); - - return ret; -} - -_static_inline int64_t -coord_cocsep(cube_t c) -{ - return (coord_co(c) << 7) + coord_csep(c); -} - -_static_inline int64_t -coord_eo(cube_t c) -{ - int i, p; - int64_t ret; - - for (ret = 0, i = 1, p = 1; i < 12; i++, p *= 2) - ret += p * (c.edge[i] >> _eoshift); - - return ret; -} - -/* -We encode the edge separation as a number from 0 to C(12,4)*C(8,4). -It can be seen as the composition of two "subset index" coordinates. -*/ -_static_inline int64_t -coord_esep(cube_t c) -{ - int64_t i, j, jj, k, l, ret1, ret2, bit1, bit2, is1; - - for (i = 0, j = 0, k = 4, l = 4, ret1 = 0, ret2 = 0; i < 12; i++) { - /* Simple version: - if (c.edge[i] & _esepbit2) { - ret1 += binomial[11-i][k--]; - } else { - if (c.edge[i] & _esepbit1) - ret2 += binomial[7-j][l--]; - j++; - } - */ - - bit1 = (c.edge[i] & _esepbit1) >> 2; - bit2 = (c.edge[i] & _esepbit2) >> 3; - is1 = (1 - bit2) * bit1; - - ret1 += bit2 * binomial[11-i][k]; - k -= bit2; - - jj = j < 8; - ret2 += jj * is1 * binomial[7-(j*jj)][l]; - l -= is1; - j += (1-bit2); - } - - return ret1 * 70 + ret2; -} - -_static_inline void -copy_corners(cube_t *dest, cube_t src) -{ - memcpy(&dest->corner, src.corner, sizeof(src.corner)); -} - -_static_inline void -copy_edges(cube_t *dest, cube_t src) -{ - memcpy(&dest->edge, src.edge, sizeof(src.edge)); -} - -_static_inline void -set_eo(cube_t *cube, int64_t eo) -{ - uint8_t i, sum, flip; - - for (sum = 0, i = 1; i < 12; i++, eo >>= 1) { - flip = eo % 2; - sum += flip; - cube->edge[i] = (cube->edge[i] & ~_eobit) | (_eobit * flip); - } - cube->edge[0] = (cube->edge[0] & ~_eobit) | (_eobit * (sum % 2)); -} - -_static_inline cube_t -invcoord_esep(int64_t esep) -{ - cube_t ret; - int64_t bit1, bit2, i, j, jj, k, l, s, v, w, is1, set1, set2; - uint8_t slice[3] = {0}; - - ret = solved; - set1 = esep % 70; - set2 = esep / 70; - - for (i = 0, j = 0, k = 4, l = 4; i < 12; i++) { - v = binomial[11-i][k]; - jj = j < 8; - w = jj * binomial[7-(j*jj)][l]; - bit2 = set2 >= v; - bit1 = set1 >= w; - is1 = (1 - bit2) * bit1; - - set2 -= bit2 * v; - k -= bit2; - set1 -= is1 * w; - l -= is1; - j += (1-bit2); - s = 2*bit2 + (1-bit2)*bit1; - - ret.edge[i] = (slice[s]++) | (uint8_t)(s << 2); - } - - return ret; -} diff --git a/src/cube_public.h b/src/cube_public.h deleted file mode 100644 index e080f40..0000000 --- a/src/cube_public.h +++ /dev/null @@ -1,255 +0,0 @@ -#include "cube.h" - -_static int64_t write_result(cube_t, char [static 22]); - -/* TODO: add option to get DR, maybe C-only, E-only, eo... */ -#define GETCUBE_OPTIONS(S, F) { .option = S, .fix = F } -struct { - char *option; - void (*fix)(int64_t *, int64_t *, int64_t *, int64_t *); -} getcube_options[] = { - GETCUBE_OPTIONS("fix", getcube_fix), - GETCUBE_OPTIONS(NULL, NULL) -}; - -_static int64_t -write_result(cube_t cube, char result[static 22]) -{ - if (!isconsistent(cube)) { - writecube("B32", zero, result); - return 2; - } - - writecube("B32", cube, result); - - return issolvable(cube) ? 0 : 1; -} - -int64_t -nissy_compose( - const char cube[static 22], - const char permutation[static 22], - char result[static 22] -) -{ - cube_t c, p, res; - - c = readcube("B32", cube); - p = readcube("B32", permutation); - res = compose(c, p); - - return write_result(res, result); -} - -int64_t -nissy_inverse( - const char cube[static 22], - char result[static 22] -) -{ - cube_t c, res; - - c = readcube("B32", cube); - res = inverse(c); - - return write_result(res, result); -} - -int64_t -nissy_applymoves( - const char cube[static 22], - const char *moves, - char result[static 22] -) -{ - cube_t c, res; - - c = readcube("B32", cube); - res = applymoves(c, moves); - - return write_result(res, result); -} - -int64_t -nissy_applytrans( - const char cube[static 22], - const char *transformation, - char result[static 22] -) -{ - cube_t c, res; - - c = readcube("B32", cube); - res = applytrans(c, transformation); - - return write_result(res, result); -} - -int64_t -nissy_frommoves( - const char *moves, - char result[static 22] -) -{ - cube_t res; - - res = applymoves(solved, moves); - - return write_result(res, result); -} - -int64_t -nissy_convert( - const char *format_in, - const char *format_out, - const char *cube_string, - char *result -) -{ - cube_t c; - - c = readcube(format_in, cube_string); - writecube(format_out, c, result); - - return isconsistent(c) ? 0 : 2; -} - -int64_t -nissy_getcube( - int64_t ep, - int64_t eo, - int64_t cp, - int64_t co, - const char *options, - char result[static 22] -) -{ - int i; - cube_t c; - - for (i = 0; getcube_options[i].option != NULL; i++) - if (!strcmp(options, getcube_options[i].option)) - getcube_options[i].fix(&ep, &eo, &cp, &co); - - c = getcube(ep, eo, cp, co); - - return write_result(c, result); -} - -int64_t -nissy_datasize( - const char *solver, - const char *options -) -{ - /* gendata() handles a NULL *data as a "dryrun" request */ - return nissy_gendata(solver, options, NULL); -} - -int64_t -nissy_gendata( - const char *solver, - const char *options, - void *data -) -{ - int64_t ret; - uint8_t maxdepth, h, i, j; - - if (!strcmp(solver, "h48")) { - /* options are in the form "h;maxdepth" */ - for (i = 0; options[i] != ';'; i++) ; - for (j = i; options[j]; j++) ; - h = atoi(options); - if (h != 0) { - LOG("Temporarily only h=0 is supported\n"); - ret = -1; - } else { - maxdepth = atoi(&options[i+1]); - ret = gendata_h48h0k4(data, maxdepth); - } - } else if (!strcmp(solver, "h48stats")) { - ret = gendata_h48h0k4(data, 20); - } else { - LOG("gendata: implemented only for h48 solver\n"); - ret = -1; - } - - return ret; -} - -int64_t -nissy_solve( - const char cube[static 22], - const char *solver, - const char *options, - const char *nisstype, - int8_t minmoves, - int8_t maxmoves, - int64_t maxsolutions, - int8_t optimal, - const void *data, - char *solutions -) -{ - cube_t c; - int64_t ret; - int h; - - c = readcube_B32(cube); - - if (!issolvable(c)) { - LOG("solve: cube is not solvable\n"); - return -1; - } - - if (minmoves < 0) { - LOG("solve: 'minmoves' is negative, setting it to 0\n"); - minmoves = 0; - } - - if (maxmoves < 0) { - LOG("solve: 'maxmoves' is negative, setting it to 20\n"); - maxmoves = 20; - } - - if (maxsolutions < 0) { - LOG("solve: 'maxsols' is negative, stopping\n"); - return -1; - } - - if (maxsolutions == 0) { - LOG("solve: 'maxsols' is 0, returning no solution\n"); - return 0; - } - - if (solutions == NULL) { - LOG("solve: return parameter 'solutions' is NULL, stopping\n"); - return -1; - } - - /* TODO define and use solve_options_t */ - if (!strcmp(solver, "h48")) { - h = atoi(options); /* TODO: better parsing */ - ret = solve_h48( - c, minmoves, maxmoves, maxsolutions, - (uint8_t)h, data, solutions); - ret = -1; - } else if (!strcmp(solver, "h48stats")) { - ret = solve_h48stats(c, maxmoves, data, solutions); - } else if (!strcmp(solver, "simple")) { - ret = solve_simple( - c, minmoves, maxmoves, maxsolutions, optimal, solutions); - } else { - LOG("solve: unknown solver '%s'\n", solver); - ret = -1; - } - - return ret; -} - -void -nissy_setlogger(void (*log)(const char *, ...)) -{ - nissy_log = log; -} diff --git a/src/cube_transform.h b/src/cube_transform.h deleted file mode 100644 index 358b051..0000000 --- a/src/cube_transform.h +++ /dev/null @@ -1,176 +0,0 @@ -/* TODO: remove these macros, if the below works */ - -#define _trans_edges_rotation(T, c) \ - compose_edges(compose_edges(_trans_cube_ ## T, c), \ - _trans_cube_ ## T ## _inverse) -#define _trans_edges_mirrored(T, c) _trans_edges_rotation(T, c) - -#define _trans_corners_rotation(T, c) \ - compose_corners(compose_corners(_trans_cube_ ## T, c), \ - _trans_cube_ ## T ## _inverse) -#define _trans_corners_mirrored(T, c) \ - invertco(compose_corners( \ - compose_corners(_trans_cube_ ## T, c), _trans_cube_ ## T ## _inverse)) - -#define _trans_rotation(T, c) \ - compose(compose(_trans_cube_ ## T, c), \ - _trans_cube_ ## T ## _inverse) -#define _trans_mirrored(T, c) \ - invertco(compose(compose(_trans_cube_ ## T, c), \ - _trans_cube_ ## T ## _inverse)) - -static cube_t cube_trans_table[48] = { - [_trans_UFr] = _trans_cube_UFr, - [_trans_UFm] = _trans_cube_UFm, - [_trans_ULr] = _trans_cube_URr, - [_trans_ULm] = _trans_cube_ULm, - [_trans_UBr] = _trans_cube_UBr, - [_trans_UBm] = _trans_cube_UBm, - [_trans_URr] = _trans_cube_ULr, - [_trans_URm] = _trans_cube_URm, - [_trans_DFr] = _trans_cube_DFr, - [_trans_DFm] = _trans_cube_DFm, - [_trans_DLr] = _trans_cube_DLr, - [_trans_DLm] = _trans_cube_DRm, - [_trans_DBr] = _trans_cube_DBr, - [_trans_DBm] = _trans_cube_DBm, - [_trans_DRr] = _trans_cube_DRr, - [_trans_DRm] = _trans_cube_DLm, - [_trans_RUr] = _trans_cube_FRr, - [_trans_RUm] = _trans_cube_FLm, - [_trans_RFr] = _trans_cube_LFr, - [_trans_RFm] = _trans_cube_RFm, - [_trans_RDr] = _trans_cube_BLr, - [_trans_RDm] = _trans_cube_BRm, - [_trans_RBr] = _trans_cube_RBr, - [_trans_RBm] = _trans_cube_LBm, - [_trans_LUr] = _trans_cube_FLr, - [_trans_LUm] = _trans_cube_FRm, - [_trans_LFr] = _trans_cube_RFr, - [_trans_LFm] = _trans_cube_LFm, - [_trans_LDr] = _trans_cube_BRr, - [_trans_LDm] = _trans_cube_BLm, - [_trans_LBr] = _trans_cube_LBr, - [_trans_LBm] = _trans_cube_RBm, - [_trans_FUr] = _trans_cube_FUr, - [_trans_FUm] = _trans_cube_FUm, - [_trans_FRr] = _trans_cube_RUr, - [_trans_FRm] = _trans_cube_LUm, - [_trans_FDr] = _trans_cube_BUr, - [_trans_FDm] = _trans_cube_BUm, - [_trans_FLr] = _trans_cube_LUr, - [_trans_FLm] = _trans_cube_RUm, - [_trans_BUr] = _trans_cube_FDr, - [_trans_BUm] = _trans_cube_FDm, - [_trans_BRr] = _trans_cube_LDr, - [_trans_BRm] = _trans_cube_RDm, - [_trans_BDr] = _trans_cube_BDr, - [_trans_BDm] = _trans_cube_BDm, - [_trans_BLr] = _trans_cube_RDr, - [_trans_BLm] = _trans_cube_LDm, -}; - -static cube_t cube_trans_table_inverse[48] = { - [_trans_UFr] = _trans_cube_UFr_inverse, - [_trans_UFm] = _trans_cube_UFm_inverse, - [_trans_ULr] = _trans_cube_URr_inverse, - [_trans_ULm] = _trans_cube_ULm_inverse, - [_trans_UBr] = _trans_cube_UBr_inverse, - [_trans_UBm] = _trans_cube_UBm_inverse, - [_trans_URr] = _trans_cube_ULr_inverse, - [_trans_URm] = _trans_cube_URm_inverse, - [_trans_DFr] = _trans_cube_DFr_inverse, - [_trans_DFm] = _trans_cube_DFm_inverse, - [_trans_DLr] = _trans_cube_DLr_inverse, - [_trans_DLm] = _trans_cube_DRm_inverse, - [_trans_DBr] = _trans_cube_DBr_inverse, - [_trans_DBm] = _trans_cube_DBm_inverse, - [_trans_DRr] = _trans_cube_DRr_inverse, - [_trans_DRm] = _trans_cube_DLm_inverse, - [_trans_RUr] = _trans_cube_FRr_inverse, - [_trans_RUm] = _trans_cube_FLm_inverse, - [_trans_RFr] = _trans_cube_LFr_inverse, - [_trans_RFm] = _trans_cube_RFm_inverse, - [_trans_RDr] = _trans_cube_BLr_inverse, - [_trans_RDm] = _trans_cube_BRm_inverse, - [_trans_RBr] = _trans_cube_RBr_inverse, - [_trans_RBm] = _trans_cube_LBm_inverse, - [_trans_LUr] = _trans_cube_FLr_inverse, - [_trans_LUm] = _trans_cube_FRm_inverse, - [_trans_LFr] = _trans_cube_RFr_inverse, - [_trans_LFm] = _trans_cube_LFm_inverse, - [_trans_LDr] = _trans_cube_BRr_inverse, - [_trans_LDm] = _trans_cube_BLm_inverse, - [_trans_LBr] = _trans_cube_LBr_inverse, - [_trans_LBm] = _trans_cube_RBm_inverse, - [_trans_FUr] = _trans_cube_FUr_inverse, - [_trans_FUm] = _trans_cube_FUm_inverse, - [_trans_FRr] = _trans_cube_RUr_inverse, - [_trans_FRm] = _trans_cube_LUm_inverse, - [_trans_FDr] = _trans_cube_BUr_inverse, - [_trans_FDm] = _trans_cube_BUm_inverse, - [_trans_FLr] = _trans_cube_LUr_inverse, - [_trans_FLm] = _trans_cube_RUm_inverse, - [_trans_BUr] = _trans_cube_FDr_inverse, - [_trans_BUm] = _trans_cube_FDm_inverse, - [_trans_BRr] = _trans_cube_LDr_inverse, - [_trans_BRm] = _trans_cube_RDm_inverse, - [_trans_BDr] = _trans_cube_BDr_inverse, - [_trans_BDm] = _trans_cube_BDm_inverse, - [_trans_BLr] = _trans_cube_RDr_inverse, - [_trans_BLm] = _trans_cube_LDm_inverse, -}; - -_static cube_t -transform_edges(cube_t c, uint8_t t) -{ - cube_t ret, trans_cube, trans_inv; - - DBG_ASSERT(t < 48, zero, - "transform: invalid transformation %" PRIu8 - ", must be between 0 and 47\n", t); - - trans_cube = cube_trans_table[t]; - trans_inv = cube_trans_table_inverse[t]; - - ret = compose_edges(trans_cube, c); - ret = compose_edges(ret, trans_inv); - - return ret; -} - -_static cube_t -transform_corners(cube_t c, uint8_t t) -{ - cube_t ret, trans_cube, trans_inv; - - DBG_ASSERT(t < 48, zero, - "transform: invalid transformation %" PRIu8 - ", must be between 0 and 47\n", t); - - trans_cube = cube_trans_table[t]; - trans_inv = cube_trans_table_inverse[t]; - - ret = compose_corners(trans_cube, c); - ret = compose_corners(ret, trans_inv); - - return t < 24 ? ret : invertco(ret); -} - -_static cube_t -transform(cube_t c, uint8_t t) -{ - cube_t ret, trans_cube, trans_inv; - - DBG_ASSERT(t < 48, zero, - "transform: invalid transformation %" PRIu8 - ", must be between 0 and 47\n", t); - - trans_cube = cube_trans_table[t]; - trans_inv = cube_trans_table_inverse[t]; - - ret = compose(trans_cube, c); - ret = compose(ret, trans_inv); - - return t < 24 ? ret : invertco(ret); -} diff --git a/src/cube_transform_with_switch.h b/src/cube_transform_with_switch.h deleted file mode 100644 index d3a108c..0000000 --- a/src/cube_transform_with_switch.h +++ /dev/null @@ -1,336 +0,0 @@ -#define _trans_edges_rotation(T, c) \ - compose_edges(compose_edges(_trans_cube_ ## T, c), \ - _trans_cube_ ## T ## _inverse) -#define _trans_edges_mirrored(T, c) _trans_edges_rotation(T, c) - -#define _trans_corners_rotation(T, c) \ - compose_corners(compose_corners(_trans_cube_ ## T, c), \ - _trans_cube_ ## T ## _inverse) -#define _trans_corners_mirrored(T, c) \ - invertco(compose_corners( \ - compose_corners(_trans_cube_ ## T, c), _trans_cube_ ## T ## _inverse)) - -#define _trans_rotation(T, c) \ - compose(compose(_trans_cube_ ## T, c), \ - _trans_cube_ ## T ## _inverse) -#define _trans_mirrored(T, c) \ - invertco(compose(compose(_trans_cube_ ## T, c), \ - _trans_cube_ ## T ## _inverse)) - -_static cube_t -transform_edges(cube_t c, uint8_t t) -{ - switch (t) { - case _trans_UFr: - return c; - case _trans_ULr: - return _trans_edges_rotation(ULr, c); - case _trans_UBr: - return _trans_edges_rotation(UBr, c); - case _trans_URr: - return _trans_edges_rotation(URr, c); - case _trans_DFr: - return _trans_edges_rotation(DFr, c); - case _trans_DLr: - return _trans_edges_rotation(DLr, c); - case _trans_DBr: - return _trans_edges_rotation(DBr, c); - case _trans_DRr: - return _trans_edges_rotation(DRr, c); - case _trans_RUr: - return _trans_edges_rotation(RUr, c); - case _trans_RFr: - return _trans_edges_rotation(RFr, c); - case _trans_RDr: - return _trans_edges_rotation(RDr, c); - case _trans_RBr: - return _trans_edges_rotation(RBr, c); - case _trans_LUr: - return _trans_edges_rotation(LUr, c); - case _trans_LFr: - return _trans_edges_rotation(LFr, c); - case _trans_LDr: - return _trans_edges_rotation(LDr, c); - case _trans_LBr: - return _trans_edges_rotation(LBr, c); - case _trans_FUr: - return _trans_edges_rotation(FUr, c); - case _trans_FRr: - return _trans_edges_rotation(FRr, c); - case _trans_FDr: - return _trans_edges_rotation(FDr, c); - case _trans_FLr: - return _trans_edges_rotation(FLr, c); - case _trans_BUr: - return _trans_edges_rotation(BUr, c); - case _trans_BRr: - return _trans_edges_rotation(BRr, c); - case _trans_BDr: - return _trans_edges_rotation(BDr, c); - case _trans_BLr: - return _trans_edges_rotation(BLr, c); - case _trans_UFm: - return _trans_edges_mirrored(UFm, c); - case _trans_ULm: - return _trans_edges_mirrored(ULm, c); - case _trans_UBm: - return _trans_edges_mirrored(UBm, c); - case _trans_URm: - return _trans_edges_mirrored(URm, c); - case _trans_DFm: - return _trans_edges_mirrored(DFm, c); - case _trans_DLm: - return _trans_edges_mirrored(DLm, c); - case _trans_DBm: - return _trans_edges_mirrored(DBm, c); - case _trans_DRm: - return _trans_edges_mirrored(DRm, c); - case _trans_RUm: - return _trans_edges_mirrored(RUm, c); - case _trans_RFm: - return _trans_edges_mirrored(RFm, c); - case _trans_RDm: - return _trans_edges_mirrored(RDm, c); - case _trans_RBm: - return _trans_edges_mirrored(RBm, c); - case _trans_LUm: - return _trans_edges_mirrored(LUm, c); - case _trans_LFm: - return _trans_edges_mirrored(LFm, c); - case _trans_LDm: - return _trans_edges_mirrored(LDm, c); - case _trans_LBm: - return _trans_edges_mirrored(LBm, c); - case _trans_FUm: - return _trans_edges_mirrored(FUm, c); - case _trans_FRm: - return _trans_edges_mirrored(FRm, c); - case _trans_FDm: - return _trans_edges_mirrored(FDm, c); - case _trans_FLm: - return _trans_edges_mirrored(FLm, c); - case _trans_BUm: - return _trans_edges_mirrored(BUm, c); - case _trans_BRm: - return _trans_edges_mirrored(BRm, c); - case _trans_BDm: - return _trans_edges_mirrored(BDm, c); - case _trans_BLm: - return _trans_edges_mirrored(BLm, c); - default: - LOG("transform error, unknown transformation %" PRIu8 "\n", t); - return zero; - } -} - -_static cube_t -transform_corners(cube_t c, uint8_t t) -{ - switch (t) { - case _trans_UFr: - return c; - case _trans_ULr: - return _trans_corners_rotation(ULr, c); - case _trans_UBr: - return _trans_corners_rotation(UBr, c); - case _trans_URr: - return _trans_corners_rotation(URr, c); - case _trans_DFr: - return _trans_corners_rotation(DFr, c); - case _trans_DLr: - return _trans_corners_rotation(DLr, c); - case _trans_DBr: - return _trans_corners_rotation(DBr, c); - case _trans_DRr: - return _trans_corners_rotation(DRr, c); - case _trans_RUr: - return _trans_corners_rotation(RUr, c); - case _trans_RFr: - return _trans_corners_rotation(RFr, c); - case _trans_RDr: - return _trans_corners_rotation(RDr, c); - case _trans_RBr: - return _trans_corners_rotation(RBr, c); - case _trans_LUr: - return _trans_corners_rotation(LUr, c); - case _trans_LFr: - return _trans_corners_rotation(LFr, c); - case _trans_LDr: - return _trans_corners_rotation(LDr, c); - case _trans_LBr: - return _trans_corners_rotation(LBr, c); - case _trans_FUr: - return _trans_corners_rotation(FUr, c); - case _trans_FRr: - return _trans_corners_rotation(FRr, c); - case _trans_FDr: - return _trans_corners_rotation(FDr, c); - case _trans_FLr: - return _trans_corners_rotation(FLr, c); - case _trans_BUr: - return _trans_corners_rotation(BUr, c); - case _trans_BRr: - return _trans_corners_rotation(BRr, c); - case _trans_BDr: - return _trans_corners_rotation(BDr, c); - case _trans_BLr: - return _trans_corners_rotation(BLr, c); - case _trans_UFm: - return _trans_corners_mirrored(UFm, c); - case _trans_ULm: - return _trans_corners_mirrored(ULm, c); - case _trans_UBm: - return _trans_corners_mirrored(UBm, c); - case _trans_URm: - return _trans_corners_mirrored(URm, c); - case _trans_DFm: - return _trans_corners_mirrored(DFm, c); - case _trans_DLm: - return _trans_corners_mirrored(DLm, c); - case _trans_DBm: - return _trans_corners_mirrored(DBm, c); - case _trans_DRm: - return _trans_corners_mirrored(DRm, c); - case _trans_RUm: - return _trans_corners_mirrored(RUm, c); - case _trans_RFm: - return _trans_corners_mirrored(RFm, c); - case _trans_RDm: - return _trans_corners_mirrored(RDm, c); - case _trans_RBm: - return _trans_corners_mirrored(RBm, c); - case _trans_LUm: - return _trans_corners_mirrored(LUm, c); - case _trans_LFm: - return _trans_corners_mirrored(LFm, c); - case _trans_LDm: - return _trans_corners_mirrored(LDm, c); - case _trans_LBm: - return _trans_corners_mirrored(LBm, c); - case _trans_FUm: - return _trans_corners_mirrored(FUm, c); - case _trans_FRm: - return _trans_corners_mirrored(FRm, c); - case _trans_FDm: - return _trans_corners_mirrored(FDm, c); - case _trans_FLm: - return _trans_corners_mirrored(FLm, c); - case _trans_BUm: - return _trans_corners_mirrored(BUm, c); - case _trans_BRm: - return _trans_corners_mirrored(BRm, c); - case _trans_BDm: - return _trans_corners_mirrored(BDm, c); - case _trans_BLm: - return _trans_corners_mirrored(BLm, c); - default: - LOG("transform error, unknown transformation %" PRIu8 "\n", t); - return zero; - } -} - -_static cube_t -transform(cube_t c, uint8_t t) -{ - switch (t) { - case _trans_UFr: - return c; - case _trans_ULr: - return _trans_rotation(ULr, c); - case _trans_UBr: - return _trans_rotation(UBr, c); - case _trans_URr: - return _trans_rotation(URr, c); - case _trans_DFr: - return _trans_rotation(DFr, c); - case _trans_DLr: - return _trans_rotation(DLr, c); - case _trans_DBr: - return _trans_rotation(DBr, c); - case _trans_DRr: - return _trans_rotation(DRr, c); - case _trans_RUr: - return _trans_rotation(RUr, c); - case _trans_RFr: - return _trans_rotation(RFr, c); - case _trans_RDr: - return _trans_rotation(RDr, c); - case _trans_RBr: - return _trans_rotation(RBr, c); - case _trans_LUr: - return _trans_rotation(LUr, c); - case _trans_LFr: - return _trans_rotation(LFr, c); - case _trans_LDr: - return _trans_rotation(LDr, c); - case _trans_LBr: - return _trans_rotation(LBr, c); - case _trans_FUr: - return _trans_rotation(FUr, c); - case _trans_FRr: - return _trans_rotation(FRr, c); - case _trans_FDr: - return _trans_rotation(FDr, c); - case _trans_FLr: - return _trans_rotation(FLr, c); - case _trans_BUr: - return _trans_rotation(BUr, c); - case _trans_BRr: - return _trans_rotation(BRr, c); - case _trans_BDr: - return _trans_rotation(BDr, c); - case _trans_BLr: - return _trans_rotation(BLr, c); - case _trans_UFm: - return _trans_mirrored(UFm, c); - case _trans_ULm: - return _trans_mirrored(ULm, c); - case _trans_UBm: - return _trans_mirrored(UBm, c); - case _trans_URm: - return _trans_mirrored(URm, c); - case _trans_DFm: - return _trans_mirrored(DFm, c); - case _trans_DLm: - return _trans_mirrored(DLm, c); - case _trans_DBm: - return _trans_mirrored(DBm, c); - case _trans_DRm: - return _trans_mirrored(DRm, c); - case _trans_RUm: - return _trans_mirrored(RUm, c); - case _trans_RFm: - return _trans_mirrored(RFm, c); - case _trans_RDm: - return _trans_mirrored(RDm, c); - case _trans_RBm: - return _trans_mirrored(RBm, c); - case _trans_LUm: - return _trans_mirrored(LUm, c); - case _trans_LFm: - return _trans_mirrored(LFm, c); - case _trans_LDm: - return _trans_mirrored(LDm, c); - case _trans_LBm: - return _trans_mirrored(LBm, c); - case _trans_FUm: - return _trans_mirrored(FUm, c); - case _trans_FRm: - return _trans_mirrored(FRm, c); - case _trans_FDm: - return _trans_mirrored(FDm, c); - case _trans_FLm: - return _trans_mirrored(FLm, c); - case _trans_BUm: - return _trans_mirrored(BUm, c); - case _trans_BRm: - return _trans_mirrored(BRm, c); - case _trans_BDm: - return _trans_mirrored(BDm, c); - case _trans_BLm: - return _trans_mirrored(BLm, c); - default: - LOG("transform error, unknown transformation %" PRIu8 "\n", t); - return zero; - } -} diff --git a/src/io_cube.h b/src/io_cube.h deleted file mode 100644 index 956494d..0000000 --- a/src/io_cube.h +++ /dev/null @@ -1,348 +0,0 @@ -_static uint8_t readco(const char *); -_static uint8_t readcp(const char *); -_static uint8_t readeo(const char *); -_static uint8_t readep(const char *); -_static cube_t readcube_B32(const char *); -_static cube_t readcube_H48(const char *); -_static uint8_t readpiece_LST(const char **); -_static cube_t readcube_LST(const char *); - -_static int writepiece_LST(uint8_t, char *); -_static void writecube_B32(cube_t, char *); -_static void writecube_H48(cube_t, char *); -_static void writecube_LST(cube_t, char *); - -_static uint8_t b32toedge(char); -_static uint8_t b32tocorner(char); -_static char edgetob32(uint8_t); -_static char cornertob32(uint8_t); - -_static struct { - const char *name; - cube_t (*read)(const char *); - void (*write)(cube_t, char *); -} ioformat[] = -{ - { .name = "B32", .read = readcube_B32, .write = writecube_B32 }, - { .name = "LST", .read = readcube_LST, .write = writecube_LST }, - { .name = "H48", .read = readcube_H48, .write = writecube_H48 }, - { .name = "NONE", .read = NULL, .write = NULL }, -}; - -cube_t -readcube(const char *format, const char *buf) -{ - int i; - - for (i = 0; ioformat[i].read != NULL; i++) - if (!strcmp(format, ioformat[i].name)) - return ioformat[i].read(buf); - - LOG("Cannot read cube in the given format\n"); - return zero; -} - -void -writecube(const char *format, cube_t cube, char *buf) -{ - char *errormsg; - size_t len; - - if (!isconsistent(cube)) { - errormsg = "ERROR: inconsistent"; - goto writecube_error; - } - - int i; - - for (i = 0; ioformat[i].write != NULL; i++) { - if (!strcmp(format, ioformat[i].name)) { - ioformat[i].write(cube, buf); - return; - } - } - - errormsg = "ERROR: format"; - -writecube_error: - LOG("writecube error, see stdout for details\n"); - len = strlen(errormsg); - memcpy(buf, errormsg, len); - buf[len] = '\n'; - buf[len+1] = '\0'; -} - -_static uint8_t -readco(const char *str) -{ - if (*str == '0') - return 0; - if (*str == '1') - return _ctwist_cw; - if (*str == '2') - return _ctwist_ccw; - - LOG("Error reading CO\n"); - return _error; -} - -_static uint8_t -readcp(const char *str) -{ - uint8_t c; - - for (c = 0; c < 8; c++) - if (!strncmp(str, cornerstr[c], 3) || - !strncmp(str, cornerstralt[c], 3)) - return c; - - LOG("Error reading CP\n"); - return _error; -} - -_static uint8_t -readeo(const char *str) -{ - if (*str == '0') - return 0; - if (*str == '1') - return _eflip; - - LOG("Error reading EO\n"); - return _error; -} - -_static uint8_t -readep(const char *str) -{ - uint8_t e; - - for (e = 0; e < 12; e++) - if (!strncmp(str, edgestr[e], 2)) - return e; - - LOG("Error reading EP\n"); - return _error; -} - -_static cube_t -readcube_B32(const char *buf) -{ - int i; - uint8_t c[8], e[12]; - - for (i = 0; i < 8; i++) { - c[i] = b32tocorner(buf[i]); - DBG_ASSERT(c[i] < 255, zero, - "Error reading B32 corner %d (char %d)\n", i, i); - } - - for (i = 0; i < 12; i++) { - e[i] = b32toedge(buf[i+9]); - DBG_ASSERT(e[i] < 255, zero, - "Error reading B32 edge %d (char %d)\n", i, i+9); - } - - return cubefromarray(c, e); -} - -_static cube_t -readcube_H48(const char *buf) -{ - int i; - uint8_t piece, orient, c[8], e[12]; - const char *b; - - b = buf; - - for (i = 0; i < 12; i++) { - while (*b == ' ' || *b == '\t' || *b == '\n') - b++; - if ((piece = readep(b)) == _error) - return zero; - b += 2; - if ((orient = readeo(b)) == _error) - return zero; - b++; - e[i] = piece | orient; - } - for (i = 0; i < 8; i++) { - while (*b == ' ' || *b == '\t' || *b == '\n') - b++; - if ((piece = readcp(b)) == _error) - return zero; - b += 3; - if ((orient = readco(b)) == _error) - return zero; - b++; - c[i] = piece | orient; - } - - return cubefromarray(c, e); -} - -_static uint8_t -readpiece_LST(const char **b) -{ - uint8_t ret; - bool read; - - while (**b == ',' || **b == ' ' || **b == '\t' || **b == '\n') - (*b)++; - - for (ret = 0, read = false; **b >= '0' && **b <= '9'; (*b)++) { - read = true; - ret = ret * 10 + (**b) - '0'; - } - - return read ? ret : _error; -} - -_static cube_t -readcube_LST(const char *buf) -{ - int i; - uint8_t c[8], e[12]; - - for (i = 0; i < 8; i++) - c[i] = readpiece_LST(&buf); - - for (i = 0; i < 12; i++) - e[i] = readpiece_LST(&buf); - - return cubefromarray(c, e); -} - -_static int -writepiece_LST(uint8_t piece, char *buf) -{ - char digits[3]; - int i, len; - - len = 0; - while (piece != 0) { - digits[len++] = (piece % 10) + '0'; - piece /= 10; - } - - if (len == 0) - digits[len++] = '0'; - - for (i = 0; i < len; i++) - buf[i] = digits[len-i-1]; - - buf[len] = ','; - buf[len+1] = ' '; - - return len+2; -} - -_static void -writecube_B32(cube_t cube, char *buf) -{ - int i; - uint8_t corner[8], edge[12]; - - pieces(&cube, corner, edge); - - for (i = 0; i < 8; i++) - buf[i] = cornertob32(corner[i]); - - buf[8] = '='; - - for (i = 0; i < 12; i++) - buf[i+9] = edgetob32(edge[i]); - - buf[21] = '\0'; -} - -_static void -writecube_H48(cube_t cube, char *buf) -{ - uint8_t piece, perm, orient, corner[8], edge[12]; - int i; - - pieces(&cube, corner, edge); - - for (i = 0; i < 12; i++) { - piece = edge[i]; - perm = piece & _pbits; - orient = (piece & _eobit) >> _eoshift; - buf[4*i ] = edgestr[perm][0]; - buf[4*i + 1] = edgestr[perm][1]; - buf[4*i + 2] = orient + '0'; - buf[4*i + 3] = ' '; - } - for (i = 0; i < 8; i++) { - piece = corner[i]; - perm = piece & _pbits; - orient = (piece & _cobits) >> _coshift; - buf[48 + 5*i ] = cornerstr[perm][0]; - buf[48 + 5*i + 1] = cornerstr[perm][1]; - buf[48 + 5*i + 2] = cornerstr[perm][2]; - buf[48 + 5*i + 3] = orient + '0'; - buf[48 + 5*i + 4] = ' '; - } - - buf[48+39] = '\0'; -} - -_static void -writecube_LST(cube_t cube, char *buf) -{ - int i; - size_t ptr; - uint8_t piece, corner[8], edge[12]; - - ptr = 0; - pieces(&cube, corner, edge); - - for (i = 0; i < 8; i++) { - piece = corner[i]; - ptr += writepiece_LST(piece, buf + ptr); - } - - for (i = 0; i < 12; i++) { - piece = edge[i]; - ptr += writepiece_LST(piece, buf + ptr); - } - - *(buf+ptr-2) = 0; -} - -_static uint8_t -b32toedge(char c) -{ - if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'f'))) - return 255; - - return c <= 'Z' ? (uint8_t)(c - 'A') : (uint8_t)(c - 'a') + 26; -} - -_static uint8_t -b32tocorner(char c) { - uint8_t val; - - if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'f'))) - return 255; - - val = c <= 'Z' ? (uint8_t)(c - 'A') : (uint8_t)(c - 'a') + 26; - - return (val & 7) | ((val & 24) << 2); -} - -_static char -edgetob32(uint8_t edge) -{ - return edge < 26 ? 'A' + (char)edge : 'a' + (char)(edge - 26); -} - -_static char -cornertob32(uint8_t corner) -{ - uint8_t val; - - val = (corner & 7) | ((corner & 96) >> 2); - - return val < 26 ? 'A' + (char)val : 'a' + (char)(val - 26); -} diff --git a/src/io_move_trans.h b/src/io_move_trans.h deleted file mode 100644 index cfeb93c..0000000 --- a/src/io_move_trans.h +++ /dev/null @@ -1,87 +0,0 @@ -_static uint8_t readmove(char); -_static uint8_t readmodifier(char); -_static uint8_t readtrans(const char *); -_static int writemoves(uint8_t *, int, char *); -_static void writetrans(uint8_t, char *); - -_static uint8_t -readmove(char c) -{ - switch (c) { - case 'U': - return _move_U; - case 'D': - return _move_D; - case 'R': - return _move_R; - case 'L': - return _move_L; - case 'F': - return _move_F; - case 'B': - return _move_B; - default: - return _error; - } -} - -_static uint8_t -readmodifier(char c) -{ - switch (c) { - case '1': /* Fallthrough */ - case '2': /* Fallthrough */ - case '3': - return c - '0' - 1; - case '\'': - return 2; - default: - return 0; - } -} - -_static uint8_t -readtrans(const char *buf) -{ - uint8_t t; - - for (t = 0; t < 48; t++) - if (!strncmp(buf, transstr[t], 11)) - return t; - - LOG("readtrans error\n"); - return _error; -} - -_static int -writemoves(uint8_t *m, int n, char *buf) -{ - int i; - size_t len; - const char *s; - char *b; - - for (i = 0, b = buf; i < n; i++, b++) { - s = movestr[m[i]]; - len = strlen(s); - memcpy(b, s, len); - b += len; - *b = ' '; - } - - if (b != buf) - b--; /* Remove last space */ - *b = '\0'; - - return b - buf; -} - -_static void -writetrans(uint8_t t, char *buf) -{ - if (t >= 48) - memcpy(buf, "error trans", 11); - else - memcpy(buf, transstr[t], 11); - buf[11] = '\0'; -} diff --git a/src/moves.h b/src/moves.h deleted file mode 100644 index 7da6405..0000000 --- a/src/moves.h +++ /dev/null @@ -1,47 +0,0 @@ -_static_inline bool allowednextmove(uint8_t *, uint8_t); -_static_inline uint8_t inverse_trans(uint8_t); -_static_inline uint8_t movebase(uint8_t); -_static_inline uint8_t moveaxis(uint8_t); - -_static bool -allowednextmove(uint8_t *moves, uint8_t n) -{ - uint8_t base[3], axis[3]; - - if (n < 2) - return true; - - base[0] = movebase(moves[n-1]); - axis[0] = moveaxis(moves[n-1]); - base[1] = movebase(moves[n-2]); - axis[1] = moveaxis(moves[n-2]); - - if (base[0] == base[1] || (axis[0] == axis[1] && base[0] < base[1])) - return false; - - if (n == 2) - return true; - - base[2] = movebase(moves[n-3]); - axis[2] = moveaxis(moves[n-3]); - - return axis[1] != axis[2] || base[0] != base[2]; -} - -_static_inline uint8_t -inverse_trans(uint8_t t) -{ - return inverse_trans_table[t]; -} - -_static_inline uint8_t -movebase(uint8_t move) -{ - return move / 3; -} - -_static_inline uint8_t -moveaxis(uint8_t move) -{ - return move / 6; -} diff --git a/src/nissy.c b/src/nissy.c new file mode 100644 index 0000000..9df775c --- /dev/null +++ b/src/nissy.c @@ -0,0 +1,265 @@ +#include +#include +#include +#include + +#include "utils/utils.h" +#include "arch/arch.h" +#include "core/core.h" +#include "solvers/solvers.h" + +#include "nissy.h" + +_static int64_t write_result(cube_t, char [static 22]); + +/* TODO: add option to get DR, maybe C-only, E-only, eo... */ +#define GETCUBE_OPTIONS(S, F) { .option = S, .fix = F } +struct { + char *option; + void (*fix)(int64_t *, int64_t *, int64_t *, int64_t *); +} getcube_options[] = { + GETCUBE_OPTIONS("fix", getcube_fix), + GETCUBE_OPTIONS(NULL, NULL) +}; + +_static int64_t +write_result(cube_t cube, char result[static 22]) +{ + if (!isconsistent(cube)) { + writecube("B32", zero, result); + return 2; + } + + writecube("B32", cube, result); + + return issolvable(cube) ? 0 : 1; +} + +int64_t +nissy_compose( + const char cube[static 22], + const char permutation[static 22], + char result[static 22] +) +{ + cube_t c, p, res; + + c = readcube("B32", cube); + p = readcube("B32", permutation); + res = compose(c, p); + + return write_result(res, result); +} + +int64_t +nissy_inverse( + const char cube[static 22], + char result[static 22] +) +{ + cube_t c, res; + + c = readcube("B32", cube); + res = inverse(c); + + return write_result(res, result); +} + +int64_t +nissy_applymoves( + const char cube[static 22], + const char *moves, + char result[static 22] +) +{ + cube_t c, res; + + c = readcube("B32", cube); + res = applymoves(c, moves); + + return write_result(res, result); +} + +int64_t +nissy_applytrans( + const char cube[static 22], + const char *transformation, + char result[static 22] +) +{ + cube_t c, res; + + c = readcube("B32", cube); + res = applytrans(c, transformation); + + return write_result(res, result); +} + +int64_t +nissy_frommoves( + const char *moves, + char result[static 22] +) +{ + cube_t res; + + res = applymoves(solved, moves); + + return write_result(res, result); +} + +int64_t +nissy_convert( + const char *format_in, + const char *format_out, + const char *cube_string, + char *result +) +{ + cube_t c; + + c = readcube(format_in, cube_string); + writecube(format_out, c, result); + + return isconsistent(c) ? 0 : 2; +} + +int64_t +nissy_getcube( + int64_t ep, + int64_t eo, + int64_t cp, + int64_t co, + const char *options, + char result[static 22] +) +{ + int i; + cube_t c; + + for (i = 0; getcube_options[i].option != NULL; i++) + if (!strcmp(options, getcube_options[i].option)) + getcube_options[i].fix(&ep, &eo, &cp, &co); + + c = getcube(ep, eo, cp, co); + + return write_result(c, result); +} + +int64_t +nissy_datasize( + const char *solver, + const char *options +) +{ + /* gendata() handles a NULL *data as a "dryrun" request */ + return nissy_gendata(solver, options, NULL); +} + +int64_t +nissy_gendata( + const char *solver, + const char *options, + void *data +) +{ + int64_t ret; + uint8_t maxdepth, h, i, j; + + if (!strcmp(solver, "h48")) { + /* options are in the form "h;maxdepth" */ + for (i = 0; options[i] != ';'; i++) ; + for (j = i; options[j]; j++) ; + h = atoi(options); + if (h != 0) { + LOG("Temporarily only h=0 is supported\n"); + ret = -1; + } else { + maxdepth = atoi(&options[i+1]); + ret = gendata_h48h0k4(data, maxdepth); + } + } else if (!strcmp(solver, "h48stats")) { + ret = gendata_h48h0k4(data, 20); + } else { + LOG("gendata: implemented only for h48 solver\n"); + ret = -1; + } + + return ret; +} + +int64_t +nissy_solve( + const char cube[static 22], + const char *solver, + const char *options, + const char *nisstype, + int8_t minmoves, + int8_t maxmoves, + int64_t maxsolutions, + int8_t optimal, + const void *data, + char *solutions +) +{ + cube_t c; + int64_t ret; + int h; + + c = readcube_B32(cube); + + if (!issolvable(c)) { + LOG("solve: cube is not solvable\n"); + return -1; + } + + if (minmoves < 0) { + LOG("solve: 'minmoves' is negative, setting it to 0\n"); + minmoves = 0; + } + + if (maxmoves < 0) { + LOG("solve: 'maxmoves' is negative, setting it to 20\n"); + maxmoves = 20; + } + + if (maxsolutions < 0) { + LOG("solve: 'maxsols' is negative, stopping\n"); + return -1; + } + + if (maxsolutions == 0) { + LOG("solve: 'maxsols' is 0, returning no solution\n"); + return 0; + } + + if (solutions == NULL) { + LOG("solve: return parameter 'solutions' is NULL, stopping\n"); + return -1; + } + + /* TODO define and use solve_options_t */ + if (!strcmp(solver, "h48")) { + h = atoi(options); /* TODO: better parsing */ + ret = solve_h48( + c, minmoves, maxmoves, maxsolutions, + (uint8_t)h, data, solutions); + ret = -1; + } else if (!strcmp(solver, "h48stats")) { + ret = solve_h48stats(c, maxmoves, data, solutions); + } else if (!strcmp(solver, "simple")) { + ret = solve_simple( + c, minmoves, maxmoves, maxsolutions, optimal, solutions); + } else { + LOG("solve: unknown solver '%s'\n", solver); + ret = -1; + } + + return ret; +} + +void +nissy_setlogger(void (*log)(const char *, ...)) +{ + nissy_log = log; +} diff --git a/src/nissy.h b/src/nissy.h new file mode 100644 index 0000000..346b99a --- /dev/null +++ b/src/nissy.h @@ -0,0 +1,104 @@ +/* +If you include this file, you should also include the following: + +inttypes, stdarg, stdbool, string + +All the functions below return 0 in case of success and a positive +number in case of error, unless otherwise specified. + +Arguments of type char [static 22] denote a cube in B32 format. +Other available formats are H48 and SRC. See README.md for more info on +these formats. + +Accepted moves are U, D, R, L, F and B, optionally followed by a 2, +a ' or a 3. + +A transformation must be given in the format + (rotation|mirrored) (2 letters) +for example 'rotation UF' or 'mirrored BL'. +*/ + +/* Apply the secod argument as a permutation on the first argument */ +int64_t nissy_compose( + const char cube[static 22], + const char permutation[static 22], + char result[static 22] +); + +/* Compute the inverse of the given cube */ +int64_t nissy_inverse( + const char cube[static 22], + char result[static 22] +); + +/* Apply the given sequence of moves on the given cube */ +int64_t nissy_applymoves( + const char cube[static 22], + const char *moves, + char result[static 22] +); + +/* Apply the single given transformation to the given cube */ +int64_t nissy_applytrans( + const char cube[static 22], + const char *transformation, + char result[static 22] +); + +/* Return the cube obtained by applying the given moves to the solved cube */ +int64_t nissy_frommoves( + const char *moves, + char result[static 22] +); + +/* Convert the given cube between the two given formats */ +int64_t nissy_convert( + const char *format_in, + const char *format_out, + const char *cube_string, + char *result +); + +/* Get the cube with the given ep, eo, cp and co values. */ +int64_t nissy_getcube( + int64_t ep, + int64_t eo, + int64_t cp, + int64_t co, + const char *options, + char result[static 22] +); + +/* +Returns the size of the data generated by nissy_gendata, when called with +the same parameters, or -1 in case of error. The returned value can be +slightly larger than the actual table size. +*/ +int64_t nissy_datasize( + const char *solver, + const char *options /* TODO: remove options, use only solver name */ +); + +/* Returns the number of bytes written, or -1 in case of error */ +int64_t nissy_gendata( + const char *solver, + const char *options, /* TODO: remove options, use only solver name */ + void *generated_data +); + +/* Returns the number of solutions found, or -1 in case of error */ +int64_t nissy_solve( + const char cube[static 22], + const char *solver, + const char *options, /* TODO: remove options, use only solver name */ + const char *nisstype, /* TODO: remove, use flags */ + int8_t minmoves, + int8_t maxmoves, + int64_t maxsolutions, + int8_t optimal, + const void *data, + char *solutions +); + +/* Set a global logger function used by this library. */ +void nissy_setlogger(void (*logger_function)(const char *, ...)); diff --git a/src/solve_generic.h b/src/solve_generic.h deleted file mode 100644 index 41d995a..0000000 --- a/src/solve_generic.h +++ /dev/null @@ -1,157 +0,0 @@ -typedef struct { - cube_t cube; - uint8_t depth; - int64_t maxsols; - char **nextsol; - int64_t *nsols; - uint8_t nmoves; - uint8_t moves[20]; - uint8_t (*estimate)(cube_t); -} dfsarg_generic_t; - -_static void solve_generic_appendsolution(dfsarg_generic_t *); -_static int solve_generic_dfs(dfsarg_generic_t *); -_static int64_t solve_generic(cube_t, const char *, int8_t, int8_t, int64_t, - int8_t, char *, uint8_t (*)(cube_t)); -_static uint8_t estimate_simple(cube_t); -_static int64_t solve_simple(cube_t, int8_t, int8_t, int64_t, int8_t, char *); - -_static void -solve_generic_appendsolution(dfsarg_generic_t *arg) -{ - int strl; - - strl = writemoves(arg->moves, arg->nmoves, *arg->nextsol); - LOG("Solution found: %s\n", *arg->nextsol); - *arg->nextsol += strl; - **arg->nextsol = '\n'; - (*arg->nextsol)++; - (*arg->nsols)++; -} - -_static int -solve_generic_dfs(dfsarg_generic_t *arg) -{ - dfsarg_generic_t nextarg; - uint8_t m, bound; - int64_t ret; - - if (!allowednextmove(arg->moves, arg->nmoves)) - return 0; - - if (arg->nmoves > 0) - arg->cube = move(arg->cube, arg->moves[arg->nmoves-1]); - - bound = arg->estimate(arg->cube); - if (*arg->nsols == arg->maxsols || bound + arg->nmoves > arg->depth) - return 0; - - if (bound == 0) { - if (arg->nmoves != arg->depth) - return 0; - solve_generic_appendsolution(arg); - return 1; - } - - nextarg = *arg; - nextarg.nmoves = arg->nmoves + 1; - for (m = 0, ret = 0; m < 18; m++) { - nextarg.cube = arg->cube; - nextarg.moves[arg->nmoves] = m; - ret += solve_generic_dfs(&nextarg); - } - - return ret; -} - -_static int64_t -solve_generic( - cube_t cube, - const char *nisstype, - /* TODO: handle NISS */ - int8_t minmoves, - int8_t maxmoves, - int64_t maxsols, - int8_t optimal, - char *sols, - uint8_t (*estimate)(cube_t) - /* TODO: add validator */ - /* TODO: maybe add data for estimate */ - /* TODO: add moveset (and allowednext?) */ -) -{ - dfsarg_generic_t arg; - int64_t ret, tmp, first; - - if (issolved(cube)) { - LOG("solve: cube is already solved\n"); - sols[0] = '\n'; - sols[1] = 0; - return 1; - } - - if (estimate == NULL) { - LOG("solve: 'estimate' is NULL\n"); - return -1; - } - - arg = (dfsarg_generic_t) { - .cube = cube, - .maxsols = maxsols, - .nextsol = &sols, - .nsols = &ret, - .nmoves = 0, - .moves = {0}, - .estimate = estimate, - }; - - ret = 0; - first = -1; - for (arg.depth = minmoves; arg.depth <= maxmoves; arg.depth++) { - tmp = solve_generic_dfs(&arg); - if (tmp != 0) - first = arg.depth; - - LOG("Found %" PRId64 " solution%s at depth %" PRIu8 "\n", - tmp, tmp == 1 ? "" : "s", arg.depth); - - if (ret >= maxsols) - break; - - if (optimal >= 0 && first >= 0 && arg.depth - first == optimal) - break; - } - - DBG_ASSERT(ret <= maxsols, ret, - "solve: found more than 'maxsols' solutions\n"); - - return ret; -} - -_static uint8_t -estimate_simple(cube_t cube) -{ - return issolved(cube) ? 0 : 1; -} - -_static int64_t -solve_simple( - cube_t cube, - int8_t minmoves, - int8_t maxmoves, - int64_t maxsols, - int8_t optimal, - char *solutions -) -{ - return solve_generic( - cube, - "", - minmoves, - maxmoves, - maxsols, - optimal, - solutions, - &estimate_simple - ); -} diff --git a/src/solve_h48.h b/src/solve_h48.h deleted file mode 100644 index 1701268..0000000 --- a/src/solve_h48.h +++ /dev/null @@ -1,839 +0,0 @@ -#define MAP_UNSET UINT64_C(0xFFFFFFFFFFFFFFFF) -#define MAP_KEYMASK UINT64_C(0xFFFFFFFFFF) -#define MAP_KEYSHIFT UINT64_C(40) - -#define COCSEP_CLASSES ((size_t)3393) -#define COCSEP_TABLESIZE ((size_t)_3p7 << (size_t)7) -#define COCSEP_VISITEDSIZE ((COCSEP_TABLESIZE + (size_t)7) / (size_t)8) -#define COCSEP_FULLSIZE ((size_t)4 * (COCSEP_TABLESIZE + (size_t)12)) - -#define ESEP_NOEO (COCSEP_CLASSES * (size_t)_12c4 * (size_t)_8c4) -#define ESEP_MAX(h) (ESEP_NOEO << (size_t)(h)) -#define ESEP_TABLESIZE(h, k) (ESEP_MAX((h)) / ((size_t)8 / (size_t)(k))) - -#define COCLASS_MASK (UINT32_C(0xFFFF) << UINT32_C(16)) -#define COCLASS(x) (((x) & COCLASS_MASK) >> UINT32_C(16)) -#define TTREP_MASK (UINT32_C(0xFF) << UINT32_C(8)) -#define TTREP(x) (((x) & TTREP_MASK) >> UINT32_C(8)) -#define CBOUND_MASK UINT32_C(0xFF) -#define CBOUND(x) ((x) & CBOUND_MASK) -#define H48_ESIZE(h) ((_12c4 * _8c4) << (int64_t)(h)) - -#define ESEP_IND(i) ((uint32_t)(i) / UINT32_C(8)) -#define ESEP_SHIFT(i) (UINT32_C(4) * ((uint32_t)(i) % UINT32_C(8))) -#define ESEP_MASK(i) ((_bit_u32(4) - (uint32_t)(1)) << ESEP_SHIFT(i)) -#define VISITED_IND(i) ((uint32_t)(i) / UINT32_C(8)) -#define VISITED_MASK(i) (UINT32_C(1) << ((uint32_t)(i) % UINT32_C(8))) - -#define MAX_SOLUTION_LENGTH 20 - -/* -TODO: This loop other similar h48 coordinates can be improved by only -transforming edges, but we need to compose transformations (i.e. conjugate -_t by _ttrep). -*/ -#define _foreach_h48sim(_cube, _cocsepdata, _selfsim, _h, _action) \ - int64_t _cocsep = coord_cocsep(_cube); \ - uint8_t _ttrep = TTREP(_cocsepdata[_cocsep]); \ - uint8_t _inverse_ttrep = inverse_trans(_ttrep); \ - int64_t _coclass = COCLASS(_cocsepdata[_cocsep]); \ - cube_t _rep = transform(_cube, _ttrep); \ - uint64_t _sim = _selfsim[_coclass]; \ - for (uint8_t _t = 0; _t < 48 && _sim; _t++, _sim >>= 1) { \ - if (!(_sim & 1)) continue; \ - _cube = transform(_rep, _t); \ - _cube = transform(_cube, _inverse_ttrep); \ - _action \ - } - -typedef struct { - uint64_t n; - uint64_t capacity; - uint64_t randomizer; - uint64_t *table; -} h48map_t; - -typedef struct { - uint64_t key; - uint64_t val; -} kvpair_t; - -typedef struct { - cube_t cube; - uint8_t depth; - uint8_t maxdepth; - uint16_t *n; - uint32_t *buf32; - uint8_t *visited; - uint64_t *selfsim; - cube_t *rep; -} dfsarg_cocsep_t; - -/* TODO keep or not? */ -typedef struct { - cube_t cube; - int8_t nmoves; - int8_t depth; - uint8_t moves[MAX_SOLUTION_LENGTH]; - uint32_t *cocsepdata; - h48map_t *visited; -} dfsarg_genh48set_t; - -typedef struct { - uint8_t depth; - uint32_t *cocsepdata; - uint32_t *buf32; - uint64_t *selfsim; - int64_t done; - cube_t *crep; -} bfsarg_esep_t; - -typedef struct { - cube_t cube; - cube_t inverse; - int8_t nmoves; - int8_t depth; - uint8_t moves[MAX_SOLUTION_LENGTH]; - int64_t *nsols; - int64_t maxsolutions; - uint8_t h; - uint32_t *cocsepdata; - uint32_t *h48data; - char **nextsol; -} dfsarg_solveh48_t; - -typedef struct { - cube_t cube; - int8_t nmoves; - int8_t depth; - uint8_t moves[MAX_SOLUTION_LENGTH]; - uint32_t *cocsepdata; - uint32_t *h48data; - char *s; -} dfsarg_solveh48stats_t; - -_static void h48map_create(h48map_t *, uint64_t, uint64_t); -_static void h48map_clear(h48map_t *); -_static void h48map_destroy(h48map_t *); -_static uint64_t h48map_lookup(h48map_t *, uint64_t); -_static void h48map_insertmin(h48map_t *, uint64_t, uint64_t); -_static uint64_t h48map_value(h48map_t *, uint64_t); -_static kvpair_t h48map_nextkvpair(h48map_t *, uint64_t *); - -_static_inline int64_t coord_h48(cube_t, const uint32_t *, uint8_t); -_static_inline int64_t coord_h48_edges(cube_t, int64_t, uint8_t, uint8_t); -_static_inline cube_t invcoord_h48(int64_t, const cube_t *, uint8_t); - -_static_inline bool get_visited(const uint8_t *, int64_t); -_static_inline void set_visited(uint8_t *, int64_t); -_static_inline uint8_t get_esep_pval(const uint32_t *, int64_t); -_static_inline void set_esep_pval(uint32_t *, int64_t, uint8_t); - -_static size_t gendata_cocsep(void *, uint64_t *, cube_t *); -_static uint32_t gendata_cocsep_dfs(dfsarg_cocsep_t *); -_static uint64_t gen_h48short( - uint8_t, const uint32_t *, const cube_t *, const uint64_t *, h48map_t *); -_static size_t gendata_h48h0k4(void *, uint8_t); -_static int64_t gendata_h48h0k4_bfs(bfsarg_esep_t *); -_static int64_t gendata_h48h0k4_bfs_fromdone(bfsarg_esep_t *); -_static int64_t gendata_h48h0k4_bfs_fromnew(bfsarg_esep_t *); - -_static void solve_h48_appendsolution(dfsarg_solveh48_t *); -_static_inline int8_t get_h48_cdata(cube_t, uint32_t *, uint32_t *); -_static_inline int8_t get_h48_bound(cube_t, uint32_t, uint8_t, uint32_t *); -_static_inline bool solve_h48_stop(dfsarg_solveh48_t *); -_static int64_t solve_h48_dfs(dfsarg_solveh48_t *); -_static int64_t solve_h48( - cube_t, int8_t, int8_t, int8_t, uint8_t, const void *, char *); - -_static int64_t solve_h48stats_dfs(dfsarg_solveh48stats_t *); -_static int64_t solve_h48stats(cube_t, int8_t, const void *, char [static 12]); - -_static void -h48map_create(h48map_t *map, uint64_t capacity, uint64_t randomizer) -{ - map->capacity = capacity; - map->randomizer = randomizer; - - map->table = malloc(map->capacity * sizeof(int64_t)); - h48map_clear(map); -} - -_static void -h48map_clear(h48map_t *map) -{ - memset(map->table, 0xFF, map->capacity * sizeof(uint64_t)); - map->n = 0; -} - -_static void -h48map_destroy(h48map_t *map) -{ - free(map->table); -} - -_static_inline uint64_t -h48map_lookup(h48map_t *map, uint64_t x) -{ - uint64_t hash, i; - - hash = ((x % map->capacity) * map->randomizer) % map->capacity; - for (i = hash; - map->table[i] != MAP_UNSET && (map->table[i] & MAP_KEYMASK) != x; - i = (i+1) % map->capacity - ) ; - - return i; -} - -_static_inline void -h48map_insertmin(h48map_t *map, uint64_t key, uint64_t val) -{ - uint64_t i, oldval, min; - - i = h48map_lookup(map, key); - oldval = map->table[i] >> MAP_KEYSHIFT; - min = _min(val, oldval); - - map->n += map->table[i] == MAP_UNSET; - map->table[i] = (key & MAP_KEYMASK) | (min << MAP_KEYSHIFT); -} - -_static_inline uint64_t -h48map_value(h48map_t *map, uint64_t key) -{ - return map->table[h48map_lookup(map, key)] >> MAP_KEYSHIFT; -} - -_static kvpair_t -h48map_nextkvpair(h48map_t *map, uint64_t *p) -{ - kvpair_t kv; - uint64_t pair; - - kv.key = MAP_UNSET; - kv.val = MAP_UNSET; - - DBG_ASSERT(*p < map->capacity, kv, - "Error looping over map: given index %" PRIu64 " is out of " - "range [0,%" PRIu64 "]", *p, map->capacity); - - for ( ; *p < map->capacity; (*p)++) { - if (map->table[*p] != MAP_UNSET) { - pair = map->table[(*p)++]; - kv.key = pair & MAP_KEYMASK; - kv.val = pair >> MAP_KEYSHIFT; - return kv; - } - } - - return kv; -} - -_static_inline int64_t -coord_h48(cube_t c, const uint32_t *cocsepdata, uint8_t h) -{ - int64_t cocsep, coclass; - uint32_t data; - uint8_t ttrep; - - DBG_ASSERT(h <= 11, -1, "coord_h48: h must be between 0 and 11\n"); - - cocsep = coord_cocsep(c); - data = cocsepdata[cocsep]; - coclass = (int64_t)COCLASS(data); - ttrep = (int64_t)TTREP(data); - - return coord_h48_edges(c, coclass, ttrep, h); -} - -_static_inline int64_t -coord_h48_edges(cube_t c, int64_t coclass, uint8_t ttrep, uint8_t h) -{ - cube_t d; - int64_t esep, eo, edges; - - d = transform_edges(c, ttrep); - esep = coord_esep(d); - eo = coord_eo(d); - edges = (esep << 11) + eo; - - return (coclass * H48_ESIZE(11) + edges) >> (11 - (int64_t)h); -} - -/* -This function does not necessarily return a cube whose coordinate is -the given value, because it works up to symmetry. This means that the -returned cube is a transformed cube of one that gives the correct value. -*/ -_static_inline cube_t -invcoord_h48(int64_t i, const cube_t *crep, uint8_t h) -{ - cube_t ret; - int64_t hh, coclass, ee, esep, eo; - - DBG_ASSERT(h <= 11, zero, - "invcoord_h48: h must be between 0 and 11\n"); - - hh = (int64_t)h; - coclass = i / H48_ESIZE(h); - ee = i % H48_ESIZE(h); - esep = ee >> hh; - eo = (ee & ((1 << hh) - 1)) << (11 - hh); - - ret = invcoord_esep(esep); - copy_corners(&ret, crep[coclass]); - set_eo(&ret, eo); - - return ret; -} - -/* -Each element of the cocsep table is a uint32_t used as follows: - - Lowest 8-bit block: pruning value - - Second-lowest 8-bit block: "ttrep" (transformation to representative) - - Top 16-bit block: symcoord value -After the data as described above, more auxiliary information is appended: - - A uint32_t representing the number of symmetry classes - - A uint32_t representing the highest value of the pruning table - - One uint32_t for each "line" of the pruning table, representing the number - of positions having that pruning value. -*/ -_static size_t -gendata_cocsep(void *buf, uint64_t *selfsim, cube_t *rep) -{ - uint32_t *buf32, *info, cc; - uint16_t n; - uint8_t i, j, visited[COCSEP_VISITEDSIZE]; - dfsarg_cocsep_t arg; - - if (buf == NULL) - goto gendata_cocsep_return_size; - - buf32 = (uint32_t *)buf; - info = buf32 + COCSEP_TABLESIZE; - memset(buf32, 0xFF, sizeof(uint32_t) * COCSEP_TABLESIZE); - if (selfsim != NULL) - memset(selfsim, 0, sizeof(uint64_t) * COCSEP_CLASSES); - - arg = (dfsarg_cocsep_t) { - .cube = solved, - .n = &n, - .buf32 = buf32, - .visited = visited, - .selfsim = selfsim, - .rep = rep - }; - for (i = 0, n = 0, cc = 0; i < 10; i++) { - LOG("cocsep: generating depth %" PRIu8 "\n", i); - memset(visited, 0, COCSEP_VISITEDSIZE); - arg.depth = 0; - arg.maxdepth = i; - cc = gendata_cocsep_dfs(&arg); - info[i+2] = cc; - LOG("found %" PRIu32 "\n", cc); - } - - info[0] = (uint32_t)n; - info[1] = 9; /* Known max pruning value */ - DBG_ASSERT(n == COCSEP_CLASSES, 0, - "cocsep: computed %" PRIu16 " symmetry classes, " - "expected %zu\n", n, COCSEP_CLASSES); - - LOG("cocsep data computed\n"); - LOG("Symmetry classes: %" PRIu32 "\n", info[0]); - LOG("Maximum pruning value: %" PRIu32 "\n", info[1]); - LOG("Pruning value distribution:\n"); - for (j = 0; j < 10; j++) - LOG("%" PRIu8 ":\t%" PRIu32 "\n", j, info[j+2]); - -gendata_cocsep_return_size: - return COCSEP_FULLSIZE; -} - -_static uint32_t -gendata_cocsep_dfs(dfsarg_cocsep_t *arg) -{ - uint8_t m; - uint32_t cc, class, ttrep, depth, olddepth, tinv; - uint64_t t; - int64_t i, j; - cube_t d; - dfsarg_cocsep_t nextarg; - - i = coord_cocsep(arg->cube); - olddepth = (uint8_t)(arg->buf32[i] & 0xFF); - if (olddepth < arg->depth || get_visited(arg->visited, i)) - return 0; - set_visited(arg->visited, i); - - if (arg->depth == arg->maxdepth) { - if ((arg->buf32[i] & 0xFF) != 0xFF) - return 0; - - if (arg->rep != NULL) - arg->rep[*arg->n] = arg->cube; - for (t = 0, cc = 0; t < 48; t++) { - d = transform_corners(arg->cube, t); - j = coord_cocsep(d); - if (i == j && arg->selfsim != NULL) - arg->selfsim[*arg->n] |= UINT64_C(1) << t; - if (COCLASS(arg->buf32[j]) != UINT32_C(0xFFFF)) - continue; - set_visited(arg->visited, j); - tinv = inverse_trans(t); - olddepth = arg->buf32[j] & 0xFF; - cc += olddepth == 0xFF; - - class = (uint32_t)(*arg->n) << UINT32_C(16); - ttrep = (uint32_t)tinv << UINT32_C(8); - depth = (uint32_t)arg->depth; - arg->buf32[j] = class | ttrep | depth; - } - (*arg->n)++; - - return cc; - } - - memcpy(&nextarg, arg, sizeof(dfsarg_cocsep_t)); - nextarg.depth++; - for (m = 0, cc = 0; m < 18; m++) { - nextarg.cube = move(arg->cube, m); - cc += gendata_cocsep_dfs(&nextarg); - } - - return cc; -} - -_static uint64_t -gen_h48short( - uint8_t n, - const uint32_t *cocsepdata, - const cube_t *crep, - const uint64_t *selfsim, - h48map_t *map -) { - uint8_t i, m; - int64_t coord; - uint64_t j, oldn; - kvpair_t kv; - cube_t cube, d; - - cube = solvedcube(); - coord = coord_h48(cube, cocsepdata, 11); - h48map_insertmin(map, coord, 0); - oldn = 0; - LOG("Short h48: generating depth 0\nfound %" PRIu8 "\n", map->n-oldn); - for (i = 0; i < n; i++) { - LOG("Short h48: generating depth %" PRIu8 "\n", i+1); - j = 0; - oldn = map->n; - for (kv = h48map_nextkvpair(map, &j); - j != map->capacity; - kv = h48map_nextkvpair(map, &j) - ) { - if (kv.val != i) - continue; - cube = invcoord_h48(kv.key, crep, 11); - for (m = 0; m < 18; m++) { - d = move(cube, m); - _foreach_h48sim(d, cocsepdata, selfsim, 11, - coord = coord_h48(d, cocsepdata, 11); - h48map_insertmin(map, coord, i+1); - ) - } - } - LOG("found %" PRIu8 "\n", map->n-oldn); - } - - return map->n; -} - -/* -TODO description -generating fixed table with h=0, k=4 -*/ -_static size_t -gendata_h48h0k4(void *buf, uint8_t maxdepth) -{ - uint32_t j, *buf32, *info, *cocsepdata; - bfsarg_esep_t arg; - int64_t sc, cc, esep_max; - uint64_t selfsim[COCSEP_CLASSES]; - cube_t crep[COCSEP_CLASSES]; - size_t cocsepsize, infosize; - - /* TODO: move info at start of tables (all tables!) */ - infosize = 4 * maxdepth; - cocsepsize = gendata_cocsep(buf, selfsim, crep); - infosize = 88; - - if (buf == NULL) - goto gendata_h48h0k4_return_size; - - esep_max = (int64_t)ESEP_MAX(0); - cocsepdata = (uint32_t *)buf; - buf32 = cocsepdata + cocsepsize / 4; - info = buf32 + (ESEP_TABLESIZE(0, 4) / sizeof(uint32_t)); - memset(buf32, 0xFF, ESEP_TABLESIZE(0, 4)); - - sc = coord_h48(solved, cocsepdata, 0); - set_esep_pval(buf32, sc, 0); - info[1] = 1; - arg = (bfsarg_esep_t) { - .cocsepdata = cocsepdata, - .buf32 = buf32, - .selfsim = selfsim, - .crep = crep - }; - for ( - arg.done = 1, arg.depth = 1, cc = 0; - arg.done < esep_max && arg.depth <= maxdepth; - arg.depth++ - ) { - LOG("esep: generating depth %" PRIu8 "\n", arg.depth); - cc = gendata_h48h0k4_bfs(&arg); - arg.done += cc; - info[arg.depth+1] = cc; - LOG("found %" PRId64 "\n", cc); - } - - info[0] = arg.depth-1; - - LOG("h48 pruning table computed\n"); - LOG("Maximum pruning value: %" PRIu32 "\n", info[0]); - LOG("Pruning value distribution:\n"); - for (j = 0; j <= info[0]; j++) - LOG("%" PRIu8 ":\t%" PRIu32 "\n", j, info[j+1]); - -gendata_h48h0k4_return_size: - return cocsepsize + ESEP_TABLESIZE(0, 4) + infosize; -} - -_static int64_t -gendata_h48h0k4_bfs(bfsarg_esep_t *arg) -{ - const uint8_t breakpoint = 10; /* Hand-picked optimal */ - - if (arg->depth < breakpoint) - return gendata_h48h0k4_bfs_fromdone(arg); - else - return gendata_h48h0k4_bfs_fromnew(arg); -} - -_static int64_t -gendata_h48h0k4_bfs_fromdone(bfsarg_esep_t *arg) -{ - uint8_t c, m, x; - uint32_t cc; - int64_t i, j, k; - cube_t cube, moved; - - for (i = 0, cc = 0; i < (int64_t)ESEP_MAX(0); i++) { - c = get_esep_pval(arg->buf32, i); - if (c != arg->depth - 1) - continue; - cube = invcoord_h48(i, arg->crep, 0); - for (m = 0; m < 18; m++) { - moved = move(cube, m); - j = coord_h48(moved, arg->cocsepdata, 0); - if (get_esep_pval(arg->buf32, j) <= arg->depth) - continue; - _foreach_h48sim(moved, arg->cocsepdata, arg->selfsim, 0, - k = coord_h48(moved, arg->cocsepdata, 0); - x = get_esep_pval(arg->buf32, k); - set_esep_pval(arg->buf32, k, arg->depth); - cc += x != arg->depth; - ) - } - } - - return cc; -} - -_static int64_t -gendata_h48h0k4_bfs_fromnew(bfsarg_esep_t *arg) -{ - uint8_t c, m, x; - uint32_t cc; - int64_t i, j; - cube_t cube, moved; - - for (i = 0, cc = 0; i < (int64_t)ESEP_MAX(0); i++) { - c = get_esep_pval(arg->buf32, i); - if (c != 0xF) - continue; - cube = invcoord_h48(i, arg->crep, 0); - for (m = 0; m < 18; m++) { - moved = move(cube, m); - j = coord_h48(moved, arg->cocsepdata, 0); - x = get_esep_pval(arg->buf32, j); - if (x >= arg->depth) - continue; - _foreach_h48sim(cube, arg->cocsepdata, arg->selfsim, 0, - j = coord_h48(cube, arg->cocsepdata, 0); - x = get_esep_pval(arg->buf32, j); - set_esep_pval(arg->buf32, j, arg->depth); - cc += x == 0xF; - ) - break; /* Enough to find one, skip the rest */ - } - } - - return cc; -} - -_static_inline bool -get_visited(const uint8_t *a, int64_t i) -{ - return a[VISITED_IND(i)] & VISITED_MASK(i); -} - -_static_inline void -set_visited(uint8_t *a, int64_t i) -{ - a[VISITED_IND(i)] |= VISITED_MASK(i); -} - -_static_inline uint8_t -get_esep_pval(const uint32_t *buf32, int64_t i) -{ - return (buf32[ESEP_IND(i)] & ESEP_MASK(i)) >> ESEP_SHIFT(i); -} - -_static_inline void -set_esep_pval(uint32_t *buf32, int64_t i, uint8_t val) -{ - buf32[ESEP_IND(i)] = - (buf32[ESEP_IND(i)] & (~ESEP_MASK(i))) | (val << ESEP_SHIFT(i)); -} - -_static void -solve_h48_appendsolution(dfsarg_solveh48_t *arg) -{ - int strl; - - strl = writemoves(arg->moves, arg->nmoves, *arg->nextsol); - LOG("Solution found: %s\n", *arg->nextsol); - *arg->nextsol += strl; - **arg->nextsol = '\n'; - (*arg->nextsol)++; - (*arg->nsols)++; -} - -_static_inline int8_t -get_h48_cdata(cube_t cube, uint32_t *cocsepdata, uint32_t *cdata) -{ - int64_t coord; - - coord = coord_cocsep(cube); - *cdata = cocsepdata[coord]; - - return CBOUND(*cdata); -} - -_static_inline int8_t -get_h48_bound(cube_t cube, uint32_t cdata, uint8_t h, uint32_t *h48data) -{ - int64_t coord; - - coord = coord_h48_edges(cube, COCLASS(cdata), TTREP(cdata), h); - return get_esep_pval(h48data, coord); -} - -_static_inline bool -solve_h48_stop(dfsarg_solveh48_t *arg) -{ - uint32_t data, data_inv; - int8_t bound; - - bound = get_h48_cdata(arg->cube, arg->cocsepdata, &data); - if (bound + arg->nmoves > arg->depth) - return true; - - bound = get_h48_cdata(arg->inverse, arg->cocsepdata, &data_inv); - if (bound + arg->nmoves > arg->depth) - return true; - -/* - bound = get_h48_bound(arg->cube, data, arg->h, arg->h48data); -LOG("Using pval %" PRId8 "\n", bound); - if (bound + arg->nmoves > arg->depth) - return true; - - bound = get_h48_bound(arg->inverse, data_inv, arg->h, arg->h48data); - if (bound + arg->nmoves > arg->depth) - return true; -*/ - - return false; -} - -_static int64_t -solve_h48_dfs(dfsarg_solveh48_t *arg) -{ - dfsarg_solveh48_t nextarg; - int64_t ret; - uint8_t m; - - if (*arg->nsols == arg->maxsolutions) - return 0; - - if (solve_h48_stop(arg)) - return 0; - - if (issolved(arg->cube)) { - if (arg->nmoves != arg->depth) - return 0; - solve_h48_appendsolution(arg); - return 1; - } - - /* TODO: avoid copy, change arg and undo changes after recursion */ - nextarg = *arg; - nextarg.nmoves = arg->nmoves + 1; - ret = 0; - for (m = 0; m < 18; m++) { - nextarg.moves[arg->nmoves] = m; - if (!allowednextmove(nextarg.moves, nextarg.nmoves)) { - /* If a move is not allowed, neither are its 180 - * and 270 degree variations */ - m += 2; - continue; - } - nextarg.cube = move(arg->cube, m); - nextarg.inverse = inverse(nextarg.cube); /* TODO: use premove */ - ret += solve_h48_dfs(&nextarg); - } - - return ret; -} - -_static int64_t -solve_h48( - cube_t cube, - int8_t minmoves, - int8_t maxmoves, - int8_t maxsolutions, - uint8_t h, - const void *data, - char *solutions -) -{ - int64_t nsols; - dfsarg_solveh48_t arg; - - arg = (dfsarg_solveh48_t) { - .cube = cube, - .inverse = inverse(cube), - .nsols = &nsols, - .maxsolutions = maxsolutions, - .h = h, - .cocsepdata = (uint32_t *)data, - .h48data = ((uint32_t *)data) + COCSEP_FULLSIZE / 4, - .nextsol = &solutions - }; - - nsols = 0; - for (arg.depth = minmoves; - arg.depth <= maxmoves && nsols < maxsolutions; - arg.depth++) - { - LOG("Found %" PRId64 " solutions, searching at depth %" - PRId8 "\n", nsols, arg.depth); - arg.nmoves = 0; - solve_h48_dfs(&arg); - } - - return nsols; -} - -/* -The h48stats solver computes how many moves it takes to solve to -each of the 12 h48 coordinates, one for each value of h from 0 to 11. -The solutions array is filled with the length of the solutions. The -solution array is therefore not a printable string. -*/ -_static int64_t -solve_h48stats_dfs(dfsarg_solveh48stats_t *arg) -{ - const int64_t limit = 11; - - int8_t bound, u; - uint8_t m; - uint32_t d; - int64_t coord, h; - dfsarg_solveh48stats_t nextarg; - - /* Check cocsep lower bound (corners only) */ - bound = get_h48_cdata(arg->cube, arg->cocsepdata, &d); - if (bound + arg->nmoves > arg->depth) - return 0; - - /* Check h48 lower bound for h=0 (esep, but no eo) */ - coord = coord_h48_edges(arg->cube, COCLASS(d), TTREP(d), 0); - bound = get_esep_pval(arg->h48data, coord); - if (bound + arg->nmoves > arg->depth) - return 0; - - /* Update all other values, if solved */ - coord = coord_h48_edges(arg->cube, COCLASS(d), TTREP(d), 11); - for (h = 0; h <= limit; h++) { - u = coord >> (11-h) == 0 && arg->s[h] == 99; - arg->s[h] = u * arg->nmoves + (1-u) * arg->s[h]; - } - - if (arg->s[limit] != 99) - return 0; - - nextarg = *arg; - nextarg.nmoves = arg->nmoves + 1; - for (m = 0; m < 18; m++) { - nextarg.moves[arg->nmoves] = m; - if (!allowednextmove(nextarg.moves, nextarg.nmoves)) { - /* If a move is not allowed, neither are its 180 - * and 270 degree variations */ - m += 2; - continue; - } - nextarg.cube = move(arg->cube, m); - solve_h48stats_dfs(&nextarg); - } - - return 0; -} - -_static int64_t -solve_h48stats( - cube_t cube, - int8_t maxmoves, - const void *data, - char solutions[static 12] -) -{ - int i; - size_t cocsepsize; - dfsarg_solveh48stats_t arg; - - cocsepsize = gendata_cocsep(NULL, NULL, NULL); - - arg = (dfsarg_solveh48stats_t) { - .cube = cube, - .cocsepdata = (uint32_t *)data, - .h48data = ((uint32_t *)data) + (cocsepsize/4), - .s = solutions - }; - - for (i = 0; i < 12; i++) - solutions[i] = (char)99; - - for (arg.depth = 0; - arg.depth <= maxmoves && solutions[11] == 99; - arg.depth++) - { - arg.nmoves = 0; - solve_h48stats_dfs(&arg); - } - - return 0; -} diff --git a/src/solvers/generic/generic.h b/src/solvers/generic/generic.h new file mode 100644 index 0000000..41d995a --- /dev/null +++ b/src/solvers/generic/generic.h @@ -0,0 +1,157 @@ +typedef struct { + cube_t cube; + uint8_t depth; + int64_t maxsols; + char **nextsol; + int64_t *nsols; + uint8_t nmoves; + uint8_t moves[20]; + uint8_t (*estimate)(cube_t); +} dfsarg_generic_t; + +_static void solve_generic_appendsolution(dfsarg_generic_t *); +_static int solve_generic_dfs(dfsarg_generic_t *); +_static int64_t solve_generic(cube_t, const char *, int8_t, int8_t, int64_t, + int8_t, char *, uint8_t (*)(cube_t)); +_static uint8_t estimate_simple(cube_t); +_static int64_t solve_simple(cube_t, int8_t, int8_t, int64_t, int8_t, char *); + +_static void +solve_generic_appendsolution(dfsarg_generic_t *arg) +{ + int strl; + + strl = writemoves(arg->moves, arg->nmoves, *arg->nextsol); + LOG("Solution found: %s\n", *arg->nextsol); + *arg->nextsol += strl; + **arg->nextsol = '\n'; + (*arg->nextsol)++; + (*arg->nsols)++; +} + +_static int +solve_generic_dfs(dfsarg_generic_t *arg) +{ + dfsarg_generic_t nextarg; + uint8_t m, bound; + int64_t ret; + + if (!allowednextmove(arg->moves, arg->nmoves)) + return 0; + + if (arg->nmoves > 0) + arg->cube = move(arg->cube, arg->moves[arg->nmoves-1]); + + bound = arg->estimate(arg->cube); + if (*arg->nsols == arg->maxsols || bound + arg->nmoves > arg->depth) + return 0; + + if (bound == 0) { + if (arg->nmoves != arg->depth) + return 0; + solve_generic_appendsolution(arg); + return 1; + } + + nextarg = *arg; + nextarg.nmoves = arg->nmoves + 1; + for (m = 0, ret = 0; m < 18; m++) { + nextarg.cube = arg->cube; + nextarg.moves[arg->nmoves] = m; + ret += solve_generic_dfs(&nextarg); + } + + return ret; +} + +_static int64_t +solve_generic( + cube_t cube, + const char *nisstype, + /* TODO: handle NISS */ + int8_t minmoves, + int8_t maxmoves, + int64_t maxsols, + int8_t optimal, + char *sols, + uint8_t (*estimate)(cube_t) + /* TODO: add validator */ + /* TODO: maybe add data for estimate */ + /* TODO: add moveset (and allowednext?) */ +) +{ + dfsarg_generic_t arg; + int64_t ret, tmp, first; + + if (issolved(cube)) { + LOG("solve: cube is already solved\n"); + sols[0] = '\n'; + sols[1] = 0; + return 1; + } + + if (estimate == NULL) { + LOG("solve: 'estimate' is NULL\n"); + return -1; + } + + arg = (dfsarg_generic_t) { + .cube = cube, + .maxsols = maxsols, + .nextsol = &sols, + .nsols = &ret, + .nmoves = 0, + .moves = {0}, + .estimate = estimate, + }; + + ret = 0; + first = -1; + for (arg.depth = minmoves; arg.depth <= maxmoves; arg.depth++) { + tmp = solve_generic_dfs(&arg); + if (tmp != 0) + first = arg.depth; + + LOG("Found %" PRId64 " solution%s at depth %" PRIu8 "\n", + tmp, tmp == 1 ? "" : "s", arg.depth); + + if (ret >= maxsols) + break; + + if (optimal >= 0 && first >= 0 && arg.depth - first == optimal) + break; + } + + DBG_ASSERT(ret <= maxsols, ret, + "solve: found more than 'maxsols' solutions\n"); + + return ret; +} + +_static uint8_t +estimate_simple(cube_t cube) +{ + return issolved(cube) ? 0 : 1; +} + +_static int64_t +solve_simple( + cube_t cube, + int8_t minmoves, + int8_t maxmoves, + int64_t maxsols, + int8_t optimal, + char *solutions +) +{ + return solve_generic( + cube, + "", + minmoves, + maxmoves, + maxsols, + optimal, + solutions, + &estimate_simple + ); +} diff --git a/src/solvers/h48/coordinate.h b/src/solvers/h48/coordinate.h new file mode 100644 index 0000000..47b805d --- /dev/null +++ b/src/solvers/h48/coordinate.h @@ -0,0 +1,68 @@ +#define H48_ESIZE(h) ((_12c4 * _8c4) << (int64_t)(h)) + +#define COCLASS_MASK (UINT32_C(0xFFFF) << UINT32_C(16)) +#define COCLASS(x) (((x) & COCLASS_MASK) >> UINT32_C(16)) +#define TTREP_MASK (UINT32_C(0xFF) << UINT32_C(8)) +#define TTREP(x) (((x) & TTREP_MASK) >> UINT32_C(8)) + +_static_inline int64_t coord_h48(cube_t, const uint32_t *, uint8_t); +_static_inline int64_t coord_h48_edges(cube_t, int64_t, uint8_t, uint8_t); +_static_inline cube_t invcoord_h48(int64_t, const cube_t *, uint8_t); + +_static_inline int64_t +coord_h48(cube_t c, const uint32_t *cocsepdata, uint8_t h) +{ + int64_t cocsep, coclass; + uint32_t data; + uint8_t ttrep; + + DBG_ASSERT(h <= 11, -1, "coord_h48: h must be between 0 and 11\n"); + + cocsep = coord_cocsep(c); + data = cocsepdata[cocsep]; + coclass = (int64_t)COCLASS(data); + ttrep = (int64_t)TTREP(data); + + return coord_h48_edges(c, coclass, ttrep, h); +} + +_static_inline int64_t +coord_h48_edges(cube_t c, int64_t coclass, uint8_t ttrep, uint8_t h) +{ + cube_t d; + int64_t esep, eo, edges; + + d = transform_edges(c, ttrep); + esep = coord_esep(d); + eo = coord_eo(d); + edges = (esep << 11) + eo; + + return (coclass * H48_ESIZE(11) + edges) >> (11 - (int64_t)h); +} + +/* +This function does not necessarily return a cube whose coordinate is +the given value, because it works up to symmetry. This means that the +returned cube is a transformed cube of one that gives the correct value. +*/ +_static_inline cube_t +invcoord_h48(int64_t i, const cube_t *crep, uint8_t h) +{ + cube_t ret; + int64_t hh, coclass, ee, esep, eo; + + DBG_ASSERT(h <= 11, zero, + "invcoord_h48: h must be between 0 and 11\n"); + + hh = (int64_t)h; + coclass = i / H48_ESIZE(h); + ee = i % H48_ESIZE(h); + esep = ee >> hh; + eo = (ee & ((1 << hh) - 1)) << (11 - hh); + + ret = invcoord_esep(esep); + copy_corners(&ret, crep[coclass]); + set_eo(&ret, eo); + + return ret; +} diff --git a/src/solvers/h48/gendata.h b/src/solvers/h48/gendata.h new file mode 100644 index 0000000..d6bf85b --- /dev/null +++ b/src/solvers/h48/gendata.h @@ -0,0 +1,425 @@ +#define COCSEP_CLASSES ((size_t)3393) +#define COCSEP_TABLESIZE ((size_t)_3p7 << (size_t)7) +#define COCSEP_VISITEDSIZE ((COCSEP_TABLESIZE + (size_t)7) / (size_t)8) +#define COCSEP_FULLSIZE ((size_t)4 * (COCSEP_TABLESIZE + (size_t)12)) + +#define ESEP_NOEO (COCSEP_CLASSES * (size_t)_12c4 * (size_t)_8c4) +#define ESEP_MAX(h) (ESEP_NOEO << (size_t)(h)) +#define ESEP_TABLESIZE(h, k) (ESEP_MAX((h)) / ((size_t)8 / (size_t)(k))) + +#define ESEP_IND(i) ((uint32_t)(i) / UINT32_C(8)) +#define ESEP_SHIFT(i) (UINT32_C(4) * ((uint32_t)(i) % UINT32_C(8))) +#define ESEP_MASK(i) ((_bit_u32(4) - (uint32_t)(1)) << ESEP_SHIFT(i)) +#define VISITED_IND(i) ((uint32_t)(i) / UINT32_C(8)) +#define VISITED_MASK(i) (UINT32_C(1) << ((uint32_t)(i) % UINT32_C(8))) + +#define CBOUND_MASK UINT32_C(0xFF) +#define CBOUND(x) ((x) & CBOUND_MASK) + +#define MAXLEN 20 + +/* +TODO: This loop over similar h48 coordinates can be improved by only +transforming edges, but we need to compose transformations (i.e. conjugate +_t by _ttrep). +*/ +#define _foreach_h48sim(_cube, _cocsepdata, _selfsim, _h, _action) \ + int64_t _cocsep = coord_cocsep(_cube); \ + uint8_t _ttrep = TTREP(_cocsepdata[_cocsep]); \ + uint8_t _inverse_ttrep = inverse_trans(_ttrep); \ + int64_t _coclass = COCLASS(_cocsepdata[_cocsep]); \ + cube_t _rep = transform(_cube, _ttrep); \ + uint64_t _sim = _selfsim[_coclass]; \ + for (uint8_t _t = 0; _t < 48 && _sim; _t++, _sim >>= 1) { \ + if (!(_sim & 1)) continue; \ + _cube = transform(_rep, _t); \ + _cube = transform(_cube, _inverse_ttrep); \ + _action \ + } + +typedef struct { + cube_t cube; + uint8_t depth; + uint8_t maxdepth; + uint16_t *n; + uint32_t *buf32; + uint8_t *visited; + uint64_t *selfsim; + cube_t *rep; +} dfsarg_cocsep_t; + +/* TODO keep or not? */ +typedef struct { + cube_t cube; + int8_t nmoves; + int8_t depth; + uint8_t moves[MAXLEN]; + uint32_t *cocsepdata; + h48map_t *visited; +} dfsarg_genh48set_t; + +typedef struct { + uint8_t depth; + uint32_t *cocsepdata; + uint32_t *buf32; + uint64_t *selfsim; + int64_t done; + cube_t *crep; +} bfsarg_esep_t; + +_static_inline bool get_visited(const uint8_t *, int64_t); +_static_inline void set_visited(uint8_t *, int64_t); +_static_inline uint8_t get_esep_pval(const uint32_t *, int64_t); +_static_inline void set_esep_pval(uint32_t *, int64_t, uint8_t); + +_static size_t gendata_cocsep(void *, uint64_t *, cube_t *); +_static uint32_t gendata_cocsep_dfs(dfsarg_cocsep_t *); +_static uint64_t gen_h48short( + uint8_t, const uint32_t *, const cube_t *, const uint64_t *, h48map_t *); +_static size_t gendata_h48h0k4(void *, uint8_t); +_static int64_t gendata_h48h0k4_bfs(bfsarg_esep_t *); +_static int64_t gendata_h48h0k4_bfs_fromdone(bfsarg_esep_t *); +_static int64_t gendata_h48h0k4_bfs_fromnew(bfsarg_esep_t *); + +_static_inline int8_t get_h48_cdata(cube_t, uint32_t *, uint32_t *); +_static_inline int8_t get_h48_bound(cube_t, uint32_t, uint8_t, uint32_t *); + +/* +Each element of the cocsep table is a uint32_t used as follows: + - Lowest 8-bit block: pruning value + - Second-lowest 8-bit block: "ttrep" (transformation to representative) + - Top 16-bit block: symcoord value +After the data as described above, more auxiliary information is appended: + - A uint32_t representing the number of symmetry classes + - A uint32_t representing the highest value of the pruning table + - One uint32_t for each "line" of the pruning table, representing the number + of positions having that pruning value. +*/ +_static size_t +gendata_cocsep(void *buf, uint64_t *selfsim, cube_t *rep) +{ + uint32_t *buf32, *info, cc; + uint16_t n; + uint8_t i, j, visited[COCSEP_VISITEDSIZE]; + dfsarg_cocsep_t arg; + + if (buf == NULL) + goto gendata_cocsep_return_size; + + buf32 = (uint32_t *)buf; + info = buf32 + COCSEP_TABLESIZE; + memset(buf32, 0xFF, sizeof(uint32_t) * COCSEP_TABLESIZE); + if (selfsim != NULL) + memset(selfsim, 0, sizeof(uint64_t) * COCSEP_CLASSES); + + arg = (dfsarg_cocsep_t) { + .cube = solved, + .n = &n, + .buf32 = buf32, + .visited = visited, + .selfsim = selfsim, + .rep = rep + }; + for (i = 0, n = 0, cc = 0; i < 10; i++) { + LOG("cocsep: generating depth %" PRIu8 "\n", i); + memset(visited, 0, COCSEP_VISITEDSIZE); + arg.depth = 0; + arg.maxdepth = i; + cc = gendata_cocsep_dfs(&arg); + info[i+2] = cc; + LOG("found %" PRIu32 "\n", cc); + } + + info[0] = (uint32_t)n; + info[1] = 9; /* Known max pruning value */ + DBG_ASSERT(n == COCSEP_CLASSES, 0, + "cocsep: computed %" PRIu16 " symmetry classes, " + "expected %zu\n", n, COCSEP_CLASSES); + + LOG("cocsep data computed\n"); + LOG("Symmetry classes: %" PRIu32 "\n", info[0]); + LOG("Maximum pruning value: %" PRIu32 "\n", info[1]); + LOG("Pruning value distribution:\n"); + for (j = 0; j < 10; j++) + LOG("%" PRIu8 ":\t%" PRIu32 "\n", j, info[j+2]); + +gendata_cocsep_return_size: + return COCSEP_FULLSIZE; +} + +_static uint32_t +gendata_cocsep_dfs(dfsarg_cocsep_t *arg) +{ + uint8_t m; + uint32_t cc, class, ttrep, depth, olddepth, tinv; + uint64_t t; + int64_t i, j; + cube_t d; + dfsarg_cocsep_t nextarg; + + i = coord_cocsep(arg->cube); + olddepth = (uint8_t)(arg->buf32[i] & 0xFF); + if (olddepth < arg->depth || get_visited(arg->visited, i)) + return 0; + set_visited(arg->visited, i); + + if (arg->depth == arg->maxdepth) { + if ((arg->buf32[i] & 0xFF) != 0xFF) + return 0; + + if (arg->rep != NULL) + arg->rep[*arg->n] = arg->cube; + for (t = 0, cc = 0; t < 48; t++) { + d = transform_corners(arg->cube, t); + j = coord_cocsep(d); + if (i == j && arg->selfsim != NULL) + arg->selfsim[*arg->n] |= UINT64_C(1) << t; + if (COCLASS(arg->buf32[j]) != UINT32_C(0xFFFF)) + continue; + set_visited(arg->visited, j); + tinv = inverse_trans(t); + olddepth = arg->buf32[j] & 0xFF; + cc += olddepth == 0xFF; + + class = (uint32_t)(*arg->n) << UINT32_C(16); + ttrep = (uint32_t)tinv << UINT32_C(8); + depth = (uint32_t)arg->depth; + arg->buf32[j] = class | ttrep | depth; + } + (*arg->n)++; + + return cc; + } + + memcpy(&nextarg, arg, sizeof(dfsarg_cocsep_t)); + nextarg.depth++; + for (m = 0, cc = 0; m < 18; m++) { + nextarg.cube = move(arg->cube, m); + cc += gendata_cocsep_dfs(&nextarg); + } + + return cc; +} + +_static uint64_t +gen_h48short( + uint8_t n, + const uint32_t *cocsepdata, + const cube_t *crep, + const uint64_t *selfsim, + h48map_t *map +) { + uint8_t i, m; + int64_t coord; + uint64_t j, oldn; + kvpair_t kv; + cube_t cube, d; + + cube = solvedcube(); + coord = coord_h48(cube, cocsepdata, 11); + h48map_insertmin(map, coord, 0); + oldn = 0; + LOG("Short h48: generating depth 0\nfound %" PRIu8 "\n", map->n-oldn); + for (i = 0; i < n; i++) { + LOG("Short h48: generating depth %" PRIu8 "\n", i+1); + j = 0; + oldn = map->n; + for (kv = h48map_nextkvpair(map, &j); + j != map->capacity; + kv = h48map_nextkvpair(map, &j) + ) { + if (kv.val != i) + continue; + cube = invcoord_h48(kv.key, crep, 11); + for (m = 0; m < 18; m++) { + d = move(cube, m); + _foreach_h48sim(d, cocsepdata, selfsim, 11, + coord = coord_h48(d, cocsepdata, 11); + h48map_insertmin(map, coord, i+1); + ) + } + } + LOG("found %" PRIu8 "\n", map->n-oldn); + } + + return map->n; +} + +/* +TODO description +generating fixed table with h=0, k=4 +*/ +_static size_t +gendata_h48h0k4(void *buf, uint8_t maxdepth) +{ + uint32_t j, *buf32, *info, *cocsepdata; + bfsarg_esep_t arg; + int64_t sc, cc, esep_max; + uint64_t selfsim[COCSEP_CLASSES]; + cube_t crep[COCSEP_CLASSES]; + size_t cocsepsize, infosize; + + /* TODO: move info at start of tables (all tables!) */ + infosize = 4 * maxdepth; + cocsepsize = gendata_cocsep(buf, selfsim, crep); + infosize = 88; + + if (buf == NULL) + goto gendata_h48h0k4_return_size; + + esep_max = (int64_t)ESEP_MAX(0); + cocsepdata = (uint32_t *)buf; + buf32 = cocsepdata + cocsepsize / 4; + info = buf32 + (ESEP_TABLESIZE(0, 4) / sizeof(uint32_t)); + memset(buf32, 0xFF, ESEP_TABLESIZE(0, 4)); + + sc = coord_h48(solved, cocsepdata, 0); + set_esep_pval(buf32, sc, 0); + info[1] = 1; + arg = (bfsarg_esep_t) { + .cocsepdata = cocsepdata, + .buf32 = buf32, + .selfsim = selfsim, + .crep = crep + }; + for ( + arg.done = 1, arg.depth = 1, cc = 0; + arg.done < esep_max && arg.depth <= maxdepth; + arg.depth++ + ) { + LOG("esep: generating depth %" PRIu8 "\n", arg.depth); + cc = gendata_h48h0k4_bfs(&arg); + arg.done += cc; + info[arg.depth+1] = cc; + LOG("found %" PRId64 "\n", cc); + } + + info[0] = arg.depth-1; + + LOG("h48 pruning table computed\n"); + LOG("Maximum pruning value: %" PRIu32 "\n", info[0]); + LOG("Pruning value distribution:\n"); + for (j = 0; j <= info[0]; j++) + LOG("%" PRIu8 ":\t%" PRIu32 "\n", j, info[j+1]); + +gendata_h48h0k4_return_size: + return cocsepsize + ESEP_TABLESIZE(0, 4) + infosize; +} + +_static int64_t +gendata_h48h0k4_bfs(bfsarg_esep_t *arg) +{ + const uint8_t breakpoint = 10; /* Hand-picked optimal */ + + if (arg->depth < breakpoint) + return gendata_h48h0k4_bfs_fromdone(arg); + else + return gendata_h48h0k4_bfs_fromnew(arg); +} + +_static int64_t +gendata_h48h0k4_bfs_fromdone(bfsarg_esep_t *arg) +{ + uint8_t c, m, x; + uint32_t cc; + int64_t i, j, k; + cube_t cube, moved; + + for (i = 0, cc = 0; i < (int64_t)ESEP_MAX(0); i++) { + c = get_esep_pval(arg->buf32, i); + if (c != arg->depth - 1) + continue; + cube = invcoord_h48(i, arg->crep, 0); + for (m = 0; m < 18; m++) { + moved = move(cube, m); + j = coord_h48(moved, arg->cocsepdata, 0); + if (get_esep_pval(arg->buf32, j) <= arg->depth) + continue; + _foreach_h48sim(moved, arg->cocsepdata, arg->selfsim, 0, + k = coord_h48(moved, arg->cocsepdata, 0); + x = get_esep_pval(arg->buf32, k); + set_esep_pval(arg->buf32, k, arg->depth); + cc += x != arg->depth; + ) + } + } + + return cc; +} + +_static int64_t +gendata_h48h0k4_bfs_fromnew(bfsarg_esep_t *arg) +{ + uint8_t c, m, x; + uint32_t cc; + int64_t i, j; + cube_t cube, moved; + + for (i = 0, cc = 0; i < (int64_t)ESEP_MAX(0); i++) { + c = get_esep_pval(arg->buf32, i); + if (c != 0xF) + continue; + cube = invcoord_h48(i, arg->crep, 0); + for (m = 0; m < 18; m++) { + moved = move(cube, m); + j = coord_h48(moved, arg->cocsepdata, 0); + x = get_esep_pval(arg->buf32, j); + if (x >= arg->depth) + continue; + _foreach_h48sim(cube, arg->cocsepdata, arg->selfsim, 0, + j = coord_h48(cube, arg->cocsepdata, 0); + x = get_esep_pval(arg->buf32, j); + set_esep_pval(arg->buf32, j, arg->depth); + cc += x == 0xF; + ) + break; /* Enough to find one, skip the rest */ + } + } + + return cc; +} + +_static_inline bool +get_visited(const uint8_t *a, int64_t i) +{ + return a[VISITED_IND(i)] & VISITED_MASK(i); +} + +_static_inline void +set_visited(uint8_t *a, int64_t i) +{ + a[VISITED_IND(i)] |= VISITED_MASK(i); +} + +_static_inline uint8_t +get_esep_pval(const uint32_t *buf32, int64_t i) +{ + return (buf32[ESEP_IND(i)] & ESEP_MASK(i)) >> ESEP_SHIFT(i); +} + +_static_inline void +set_esep_pval(uint32_t *buf32, int64_t i, uint8_t val) +{ + buf32[ESEP_IND(i)] = + (buf32[ESEP_IND(i)] & (~ESEP_MASK(i))) | (val << ESEP_SHIFT(i)); +} + +_static_inline int8_t +get_h48_cdata(cube_t cube, uint32_t *cocsepdata, uint32_t *cdata) +{ + int64_t coord; + + coord = coord_cocsep(cube); + *cdata = cocsepdata[coord]; + + return CBOUND(*cdata); +} + +_static_inline int8_t +get_h48_bound(cube_t cube, uint32_t cdata, uint8_t h, uint32_t *h48data) +{ + int64_t coord; + + coord = coord_h48_edges(cube, COCLASS(cdata), TTREP(cdata), h); + return get_esep_pval(h48data, coord); +} diff --git a/src/solvers/h48/h48.h b/src/solvers/h48/h48.h new file mode 100644 index 0000000..d33ad63 --- /dev/null +++ b/src/solvers/h48/h48.h @@ -0,0 +1,4 @@ +#include "coordinate.h" +#include "map.h" +#include "gendata.h" +#include "solve.h" diff --git a/src/solvers/h48/map.h b/src/solvers/h48/map.h new file mode 100644 index 0000000..82e5a2c --- /dev/null +++ b/src/solvers/h48/map.h @@ -0,0 +1,104 @@ +#define MAP_UNSET UINT64_C(0xFFFFFFFFFFFFFFFF) +#define MAP_KEYMASK UINT64_C(0xFFFFFFFFFF) +#define MAP_KEYSHIFT UINT64_C(40) + +typedef struct { + uint64_t n; + uint64_t capacity; + uint64_t randomizer; + uint64_t *table; +} h48map_t; + +typedef struct { + uint64_t key; + uint64_t val; +} kvpair_t; + +_static void h48map_create(h48map_t *, uint64_t, uint64_t); +_static void h48map_clear(h48map_t *); +_static void h48map_destroy(h48map_t *); +_static uint64_t h48map_lookup(h48map_t *, uint64_t); +_static void h48map_insertmin(h48map_t *, uint64_t, uint64_t); +_static uint64_t h48map_value(h48map_t *, uint64_t); +_static kvpair_t h48map_nextkvpair(h48map_t *, uint64_t *); + +_static void +h48map_create(h48map_t *map, uint64_t capacity, uint64_t randomizer) +{ + map->capacity = capacity; + map->randomizer = randomizer; + + map->table = malloc(map->capacity * sizeof(int64_t)); + h48map_clear(map); +} + +_static void +h48map_clear(h48map_t *map) +{ + memset(map->table, 0xFF, map->capacity * sizeof(uint64_t)); + map->n = 0; +} + +_static void +h48map_destroy(h48map_t *map) +{ + free(map->table); +} + +_static_inline uint64_t +h48map_lookup(h48map_t *map, uint64_t x) +{ + uint64_t hash, i; + + hash = ((x % map->capacity) * map->randomizer) % map->capacity; + for (i = hash; + map->table[i] != MAP_UNSET && (map->table[i] & MAP_KEYMASK) != x; + i = (i+1) % map->capacity + ) ; + + return i; +} + +_static_inline void +h48map_insertmin(h48map_t *map, uint64_t key, uint64_t val) +{ + uint64_t i, oldval, min; + + i = h48map_lookup(map, key); + oldval = map->table[i] >> MAP_KEYSHIFT; + min = _min(val, oldval); + + map->n += map->table[i] == MAP_UNSET; + map->table[i] = (key & MAP_KEYMASK) | (min << MAP_KEYSHIFT); +} + +_static_inline uint64_t +h48map_value(h48map_t *map, uint64_t key) +{ + return map->table[h48map_lookup(map, key)] >> MAP_KEYSHIFT; +} + +_static kvpair_t +h48map_nextkvpair(h48map_t *map, uint64_t *p) +{ + kvpair_t kv; + uint64_t pair; + + kv.key = MAP_UNSET; + kv.val = MAP_UNSET; + + DBG_ASSERT(*p < map->capacity, kv, + "Error looping over map: given index %" PRIu64 " is out of " + "range [0,%" PRIu64 "]", *p, map->capacity); + + for ( ; *p < map->capacity; (*p)++) { + if (map->table[*p] != MAP_UNSET) { + pair = map->table[(*p)++]; + kv.key = pair & MAP_KEYMASK; + kv.val = pair >> MAP_KEYSHIFT; + return kv; + } + } + + return kv; +} diff --git a/src/solvers/h48/solve.h b/src/solvers/h48/solve.h new file mode 100644 index 0000000..8531a40 --- /dev/null +++ b/src/solvers/h48/solve.h @@ -0,0 +1,242 @@ +typedef struct { + cube_t cube; + cube_t inverse; + int8_t nmoves; + int8_t depth; + uint8_t moves[MAXLEN]; + int64_t *nsols; + int64_t maxsolutions; + uint8_t h; + uint32_t *cocsepdata; + uint32_t *h48data; + char **nextsol; +} dfsarg_solveh48_t; + +typedef struct { + cube_t cube; + int8_t nmoves; + int8_t depth; + uint8_t moves[MAXLEN]; + uint32_t *cocsepdata; + uint32_t *h48data; + char *s; +} dfsarg_solveh48stats_t; + +_static void solve_h48_appendsolution(dfsarg_solveh48_t *); +_static_inline bool solve_h48_stop(dfsarg_solveh48_t *); +_static int64_t solve_h48_dfs(dfsarg_solveh48_t *); +_static int64_t solve_h48( + cube_t, int8_t, int8_t, int8_t, uint8_t, const void *, char *); + +_static int64_t solve_h48stats_dfs(dfsarg_solveh48stats_t *); +_static int64_t solve_h48stats(cube_t, int8_t, const void *, char [static 12]); + +_static void +solve_h48_appendsolution(dfsarg_solveh48_t *arg) +{ + int strl; + + strl = writemoves(arg->moves, arg->nmoves, *arg->nextsol); + LOG("Solution found: %s\n", *arg->nextsol); + *arg->nextsol += strl; + **arg->nextsol = '\n'; + (*arg->nextsol)++; + (*arg->nsols)++; +} + +_static_inline bool +solve_h48_stop(dfsarg_solveh48_t *arg) +{ + uint32_t data, data_inv; + int8_t bound; + + bound = get_h48_cdata(arg->cube, arg->cocsepdata, &data); + if (bound + arg->nmoves > arg->depth) + return true; + + bound = get_h48_cdata(arg->inverse, arg->cocsepdata, &data_inv); + if (bound + arg->nmoves > arg->depth) + return true; + +/* + bound = get_h48_bound(arg->cube, data, arg->h, arg->h48data); +LOG("Using pval %" PRId8 "\n", bound); + if (bound + arg->nmoves > arg->depth) + return true; + + bound = get_h48_bound(arg->inverse, data_inv, arg->h, arg->h48data); + if (bound + arg->nmoves > arg->depth) + return true; +*/ + + return false; +} + +_static int64_t +solve_h48_dfs(dfsarg_solveh48_t *arg) +{ + dfsarg_solveh48_t nextarg; + int64_t ret; + uint8_t m; + + if (*arg->nsols == arg->maxsolutions) + return 0; + + if (solve_h48_stop(arg)) + return 0; + + if (issolved(arg->cube)) { + if (arg->nmoves != arg->depth) + return 0; + solve_h48_appendsolution(arg); + return 1; + } + + /* TODO: avoid copy, change arg and undo changes after recursion */ + nextarg = *arg; + nextarg.nmoves = arg->nmoves + 1; + ret = 0; + for (m = 0; m < 18; m++) { + nextarg.moves[arg->nmoves] = m; + if (!allowednextmove(nextarg.moves, nextarg.nmoves)) { + /* If a move is not allowed, neither are its 180 + * and 270 degree variations */ + m += 2; + continue; + } + nextarg.cube = move(arg->cube, m); + nextarg.inverse = inverse(nextarg.cube); /* TODO: use premove */ + ret += solve_h48_dfs(&nextarg); + } + + return ret; +} + +_static int64_t +solve_h48( + cube_t cube, + int8_t minmoves, + int8_t maxmoves, + int8_t maxsolutions, + uint8_t h, + const void *data, + char *solutions +) +{ + int64_t nsols; + dfsarg_solveh48_t arg; + + arg = (dfsarg_solveh48_t) { + .cube = cube, + .inverse = inverse(cube), + .nsols = &nsols, + .maxsolutions = maxsolutions, + .h = h, + .cocsepdata = (uint32_t *)data, + .h48data = ((uint32_t *)data) + COCSEP_FULLSIZE / 4, + .nextsol = &solutions + }; + + nsols = 0; + for (arg.depth = minmoves; + arg.depth <= maxmoves && nsols < maxsolutions; + arg.depth++) + { + LOG("Found %" PRId64 " solutions, searching at depth %" + PRId8 "\n", nsols, arg.depth); + arg.nmoves = 0; + solve_h48_dfs(&arg); + } + + return nsols; +} + +/* +The h48stats solver computes how many moves it takes to solve to +each of the 12 h48 coordinates, one for each value of h from 0 to 11. +The solutions array is filled with the length of the solutions. The +solution array is therefore not a printable string. +*/ +_static int64_t +solve_h48stats_dfs(dfsarg_solveh48stats_t *arg) +{ + const int64_t limit = 11; + + int8_t bound, u; + uint8_t m; + uint32_t d; + int64_t coord, h; + dfsarg_solveh48stats_t nextarg; + + /* Check cocsep lower bound (corners only) */ + bound = get_h48_cdata(arg->cube, arg->cocsepdata, &d); + if (bound + arg->nmoves > arg->depth) + return 0; + + /* Check h48 lower bound for h=0 (esep, but no eo) */ + coord = coord_h48_edges(arg->cube, COCLASS(d), TTREP(d), 0); + bound = get_esep_pval(arg->h48data, coord); + if (bound + arg->nmoves > arg->depth) + return 0; + + /* Update all other values, if solved */ + coord = coord_h48_edges(arg->cube, COCLASS(d), TTREP(d), 11); + for (h = 0; h <= limit; h++) { + u = coord >> (11-h) == 0 && arg->s[h] == 99; + arg->s[h] = u * arg->nmoves + (1-u) * arg->s[h]; + } + + if (arg->s[limit] != 99) + return 0; + + nextarg = *arg; + nextarg.nmoves = arg->nmoves + 1; + for (m = 0; m < 18; m++) { + nextarg.moves[arg->nmoves] = m; + if (!allowednextmove(nextarg.moves, nextarg.nmoves)) { + /* If a move is not allowed, neither are its 180 + * and 270 degree variations */ + m += 2; + continue; + } + nextarg.cube = move(arg->cube, m); + solve_h48stats_dfs(&nextarg); + } + + return 0; +} + +_static int64_t +solve_h48stats( + cube_t cube, + int8_t maxmoves, + const void *data, + char solutions[static 12] +) +{ + int i; + size_t cocsepsize; + dfsarg_solveh48stats_t arg; + + cocsepsize = gendata_cocsep(NULL, NULL, NULL); + + arg = (dfsarg_solveh48stats_t) { + .cube = cube, + .cocsepdata = (uint32_t *)data, + .h48data = ((uint32_t *)data) + (cocsepsize/4), + .s = solutions + }; + + for (i = 0; i < 12; i++) + solutions[i] = (char)99; + + for (arg.depth = 0; + arg.depth <= maxmoves && solutions[11] == 99; + arg.depth++) + { + arg.nmoves = 0; + solve_h48stats_dfs(&arg); + } + + return 0; +} diff --git a/src/solvers/solvers.h b/src/solvers/solvers.h new file mode 100644 index 0000000..66c9b27 --- /dev/null +++ b/src/solvers/solvers.h @@ -0,0 +1,2 @@ +#include "generic/generic.h" +#include "h48/h48.h" diff --git a/src/utils.h b/src/utils.h deleted file mode 100644 index 87402e6..0000000 --- a/src/utils.h +++ /dev/null @@ -1,185 +0,0 @@ -#define _swap(x, y) do { x ^= y; y ^= x; x ^= y; } while (0) -#define _min(x, y) ((x) < (y) ? (x) : (y)) -#define _max(x, y) ((x) > (y) ? (x) : (y)) - -_static int64_t factorial(int64_t); -_static bool isperm(uint8_t *, int64_t); -_static int64_t permtoindex(uint8_t *, int64_t); -_static void indextoperm(int64_t, int64_t, uint8_t *); -_static int permsign(uint8_t *, int64_t); -_static int64_t digitstosumzero(uint8_t *, uint8_t, uint8_t); -_static void sumzerotodigits(int64_t, uint8_t, uint8_t, uint8_t *); - -_static int64_t -factorial(int64_t n) -{ - int64_t i, ret; - - if (n > _max_factorial) { - LOG("Error: won't compute factorial for n=%" PRId64 " because" - " it is larger than %" PRId64 "\n", n, _max_factorial); - return -1; - } - - if (n < 0) - return 0; - - for (i = 1, ret = 1; i <= n; i++) - ret *= i; - - return ret; -} - -_static bool -isperm(uint8_t *a, int64_t n) -{ - int64_t i; - bool aux[_max_factorial+1]; - - if (n > _max_factorial) { - LOG("Error: won't compute 'isperm()' for n=%" PRId64 " because" - " it is larger than %" PRId64 "\n", n, _max_factorial); - return false; - } - - memset(aux, false, n); - - for (i = 0; i < n; i++) { - if (a[i] >= n) - return false; - else - aux[a[i]] = true; - } - - for (i = 0; i < n; i++) - if (!aux[i]) - return false; - - return true; -} - -_static int64_t -permtoindex(uint8_t *a, int64_t n) -{ - int64_t i, j, c, ret; - - if (n > _max_factorial) { - LOG("Error: won't compute 'permtoindex()' for n=%" PRId64 - " because it is larger than %" PRId64 "\n", - n, _max_factorial); - return -1; - } - - if (!isperm(a, n)) - return -1; - - for (i = 0, ret = 0; i < n; i++) { - for (j = i+1, c = 0; j < n; j++) - c += (a[i] > a[j]) ? 1 : 0; - ret += factorial(n-i-1) * c; - } - - return ret; -} - -_static void -indextoperm(int64_t p, int64_t n, uint8_t *r) -{ - int64_t i, j, c; - uint8_t a[_max_factorial+1]; - - if (n > _max_factorial) { - LOG("Error: won't compute 'permtoindex()' for n=%" PRId64 - " because it is larger than %" PRId64 "\n", - n, _max_factorial); - goto indextoperm_error; - } - - memset(a, 0, n); - - if (p < 0 || p >= factorial(n)) - goto indextoperm_error; - - for (i = 0; i < n; i++) { - for (j = 0, c = 0; c <= p / factorial(n-i-1); j++) - c += a[j] ? 0 : 1; - r[i] = j-1; - a[j-1] = 1; - p %= factorial(n-i-1); - } - - if (!isperm(r, n)) - goto indextoperm_error; - - return; - -indextoperm_error: - memset(r, _error, n); -} - -_static int -permsign(uint8_t *a, int64_t n) -{ - int i, j; - uint8_t ret; - - for (i = 0, ret = 0; i < n; i++) - for (j = i+1; j < n; j++) - ret += a[i] > a[j] ? 1 : 0; - - return ret % 2; -} - -_static int64_t -digitstosumzero(uint8_t *a, uint8_t n, uint8_t b) -{ - int64_t ret, p; - uint8_t i, sum; - - if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) { - LOG("Won't compute 'sumzero' for n=%" PRIu8 "and b=%" PRIu8 - " (use n=8 b=3 or n=12 b=2)\n", n, b); - return -1; - } - - for (i = 1, ret = 0, p = 1, sum = 0; i < n; i++, p *= (int64_t)b) { - if (a[i] >= b) { - LOG("Error: digit %" PRIu8 " larger than maximum" - " (b=%" PRIu8 "\n", a[i], b); - return -1; - } - sum += a[i]; - ret += p * (int64_t)a[i]; - } - - if ((sum + a[0]) % b != 0) { - LOG("Error: digits do not have sum zero modulo b\n"); - return -1; - } - - return ret; -} - -_static void -sumzerotodigits(int64_t d, uint8_t n, uint8_t b, uint8_t *a) -{ - uint8_t sum; - int64_t i; - - if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) { - LOG("Won't compute 'digits' for n=%" PRIu8 "and b=%" PRIu8 - " (use n=8 b=3 or n=12 b=2)\n"); - goto digitstosumzero_error; - } - - for (i = 1, sum = 0; i < n; i++, d /= (int64_t)b) { - a[i] = (uint8_t)(d % (int64_t)b); - sum += a[i]; - } - a[0] = (b - (sum % b)) % b; - - return; - -digitstosumzero_error: - memset(a, _error, n); -} diff --git a/src/utils/constants.h b/src/utils/constants.h new file mode 100644 index 0000000..52e2810 --- /dev/null +++ b/src/utils/constants.h @@ -0,0 +1,295 @@ +#define _bit_u8(i) (UINT8_C(1) << (uint8_t)(i)) +#define _bit_u32(i) (UINT32_C(1) << (uint32_t)(i)) +#define _bit_u64(i) (UINT64_C(1) << (uint64_t)(i)) + +#define _max_factorial INT64_C(12) + +#define _2p11 INT64_C(2048) +#define _2p12 INT64_C(4096) +#define _3p7 INT64_C(2187) +#define _3p8 INT64_C(6561) +#define _12f INT64_C(479001600) +#define _8f INT64_C(40320) +#define _12c4 INT64_C(495) +#define _8c4 INT64_C(70) + +_static int64_t binomial[12][12] = { + {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {1, 3, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0}, + {1, 4, 6, 4, 1, 0, 0, 0, 0, 0, 0, 0}, + {1, 5, 10, 10, 5, 1, 0, 0, 0, 0, 0, 0}, + {1, 6, 15, 20, 15, 6, 1, 0, 0, 0, 0, 0}, + {1, 7, 21, 35, 35, 21, 7, 1, 0, 0, 0, 0}, + {1, 8, 28, 56, 70, 56, 28, 8, 1, 0, 0, 0}, + {1, 9, 36, 84, 126, 126, 84, 36, 9, 1, 0, 0}, + {1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1, 0}, + {1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1}, +}; + +#define _move_U UINT8_C(0) +#define _move_U2 UINT8_C(1) +#define _move_U3 UINT8_C(2) +#define _move_D UINT8_C(3) +#define _move_D2 UINT8_C(4) +#define _move_D3 UINT8_C(5) +#define _move_R UINT8_C(6) +#define _move_R2 UINT8_C(7) +#define _move_R3 UINT8_C(8) +#define _move_L UINT8_C(9) +#define _move_L2 UINT8_C(10) +#define _move_L3 UINT8_C(11) +#define _move_F UINT8_C(12) +#define _move_F2 UINT8_C(13) +#define _move_F3 UINT8_C(14) +#define _move_B UINT8_C(15) +#define _move_B2 UINT8_C(16) +#define _move_B3 UINT8_C(17) + +#define _trans_UFr UINT8_C(0) +#define _trans_ULr UINT8_C(1) +#define _trans_UBr UINT8_C(2) +#define _trans_URr UINT8_C(3) +#define _trans_DFr UINT8_C(4) +#define _trans_DLr UINT8_C(5) +#define _trans_DBr UINT8_C(6) +#define _trans_DRr UINT8_C(7) +#define _trans_RUr UINT8_C(8) +#define _trans_RFr UINT8_C(9) +#define _trans_RDr UINT8_C(10) +#define _trans_RBr UINT8_C(11) +#define _trans_LUr UINT8_C(12) +#define _trans_LFr UINT8_C(13) +#define _trans_LDr UINT8_C(14) +#define _trans_LBr UINT8_C(15) +#define _trans_FUr UINT8_C(16) +#define _trans_FRr UINT8_C(17) +#define _trans_FDr UINT8_C(18) +#define _trans_FLr UINT8_C(19) +#define _trans_BUr UINT8_C(20) +#define _trans_BRr UINT8_C(21) +#define _trans_BDr UINT8_C(22) +#define _trans_BLr UINT8_C(23) + +#define _trans_UFm UINT8_C(24) +#define _trans_ULm UINT8_C(25) +#define _trans_UBm UINT8_C(26) +#define _trans_URm UINT8_C(27) +#define _trans_DFm UINT8_C(28) +#define _trans_DLm UINT8_C(29) +#define _trans_DBm UINT8_C(30) +#define _trans_DRm UINT8_C(31) +#define _trans_RUm UINT8_C(32) +#define _trans_RFm UINT8_C(33) +#define _trans_RDm UINT8_C(34) +#define _trans_RBm UINT8_C(35) +#define _trans_LUm UINT8_C(36) +#define _trans_LFm UINT8_C(37) +#define _trans_LDm UINT8_C(38) +#define _trans_LBm UINT8_C(39) +#define _trans_FUm UINT8_C(40) +#define _trans_FRm UINT8_C(41) +#define _trans_FDm UINT8_C(42) +#define _trans_FLm UINT8_C(43) +#define _trans_BUm UINT8_C(44) +#define _trans_BRm UINT8_C(45) +#define _trans_BDm UINT8_C(46) +#define _trans_BLm UINT8_C(47) + +#define _c_ufr UINT8_C(0) +#define _c_ubl UINT8_C(1) +#define _c_dfl UINT8_C(2) +#define _c_dbr UINT8_C(3) +#define _c_ufl UINT8_C(4) +#define _c_ubr UINT8_C(5) +#define _c_dfr UINT8_C(6) +#define _c_dbl UINT8_C(7) + +#define _e_uf UINT8_C(0) +#define _e_ub UINT8_C(1) +#define _e_db UINT8_C(2) +#define _e_df UINT8_C(3) +#define _e_ur UINT8_C(4) +#define _e_ul UINT8_C(5) +#define _e_dl UINT8_C(6) +#define _e_dr UINT8_C(7) +#define _e_fr UINT8_C(8) +#define _e_fl UINT8_C(9) +#define _e_bl UINT8_C(10) +#define _e_br UINT8_C(11) + +#define _eoshift UINT8_C(4) +#define _coshift UINT8_C(5) + +#define _pbits UINT8_C(0xF) +#define _esepbit1 UINT8_C(0x4) +#define _esepbit2 UINT8_C(0x8) +#define _csepbit UINT8_C(0x4) +#define _eobit UINT8_C(0x10) +#define _cobits UINT8_C(0xF0) +#define _cobits2 UINT8_C(0x60) +#define _ctwist_cw UINT8_C(0x20) +#define _ctwist_ccw UINT8_C(0x40) +#define _eflip UINT8_C(0x10) +#define _error UINT8_C(0xFF) + +_static const char *cornerstr[] = { + [_c_ufr] = "UFR", + [_c_ubl] = "UBL", + [_c_dfl] = "DFL", + [_c_dbr] = "DBR", + [_c_ufl] = "UFL", + [_c_ubr] = "UBR", + [_c_dfr] = "DFR", + [_c_dbl] = "DBL" +}; + +_static const char *cornerstralt[] = { + [_c_ufr] = "URF", + [_c_ubl] = "ULB", + [_c_dfl] = "DLF", + [_c_dbr] = "DRB", + [_c_ufl] = "ULF", + [_c_ubr] = "URB", + [_c_dfr] = "DRF", + [_c_dbl] = "DLB" +}; + +_static const char *edgestr[] = { + [_e_uf] = "UF", + [_e_ub] = "UB", + [_e_db] = "DB", + [_e_df] = "DF", + [_e_ur] = "UR", + [_e_ul] = "UL", + [_e_dl] = "DL", + [_e_dr] = "DR", + [_e_fr] = "FR", + [_e_fl] = "FL", + [_e_bl] = "BL", + [_e_br] = "BR" +}; + +_static const char *movestr[] = { + [_move_U] = "U", + [_move_U2] = "U2", + [_move_U3] = "U'", + [_move_D] = "D", + [_move_D2] = "D2", + [_move_D3] = "D'", + [_move_R] = "R", + [_move_R2] = "R2", + [_move_R3] = "R'", + [_move_L] = "L", + [_move_L2] = "L2", + [_move_L3] = "L'", + [_move_F] = "F", + [_move_F2] = "F2", + [_move_F3] = "F'", + [_move_B] = "B", + [_move_B2] = "B2", + [_move_B3] = "B'", +}; + +_static const char *transstr[] = { + [_trans_UFr] = "rotation UF", + [_trans_UFm] = "mirrored UF", + [_trans_ULr] = "rotation UL", + [_trans_ULm] = "mirrored UL", + [_trans_UBr] = "rotation UB", + [_trans_UBm] = "mirrored UB", + [_trans_URr] = "rotation UR", + [_trans_URm] = "mirrored UR", + [_trans_DFr] = "rotation DF", + [_trans_DFm] = "mirrored DF", + [_trans_DLr] = "rotation DL", + [_trans_DLm] = "mirrored DL", + [_trans_DBr] = "rotation DB", + [_trans_DBm] = "mirrored DB", + [_trans_DRr] = "rotation DR", + [_trans_DRm] = "mirrored DR", + [_trans_RUr] = "rotation RU", + [_trans_RUm] = "mirrored RU", + [_trans_RFr] = "rotation RF", + [_trans_RFm] = "mirrored RF", + [_trans_RDr] = "rotation RD", + [_trans_RDm] = "mirrored RD", + [_trans_RBr] = "rotation RB", + [_trans_RBm] = "mirrored RB", + [_trans_LUr] = "rotation LU", + [_trans_LUm] = "mirrored LU", + [_trans_LFr] = "rotation LF", + [_trans_LFm] = "mirrored LF", + [_trans_LDr] = "rotation LD", + [_trans_LDm] = "mirrored LD", + [_trans_LBr] = "rotation LB", + [_trans_LBm] = "mirrored LB", + [_trans_FUr] = "rotation FU", + [_trans_FUm] = "mirrored FU", + [_trans_FRr] = "rotation FR", + [_trans_FRm] = "mirrored FR", + [_trans_FDr] = "rotation FD", + [_trans_FDm] = "mirrored FD", + [_trans_FLr] = "rotation FL", + [_trans_FLm] = "mirrored FL", + [_trans_BUr] = "rotation BU", + [_trans_BUm] = "mirrored BU", + [_trans_BRr] = "rotation BR", + [_trans_BRm] = "mirrored BR", + [_trans_BDr] = "rotation BD", + [_trans_BDm] = "mirrored BD", + [_trans_BLr] = "rotation BL", + [_trans_BLm] = "mirrored BL", +}; + +static uint8_t inverse_trans_table[48] = { + [_trans_UFr] = _trans_UFr, + [_trans_UFm] = _trans_UFm, + [_trans_ULr] = _trans_URr, + [_trans_ULm] = _trans_ULm, + [_trans_UBr] = _trans_UBr, + [_trans_UBm] = _trans_UBm, + [_trans_URr] = _trans_ULr, + [_trans_URm] = _trans_URm, + [_trans_DFr] = _trans_DFr, + [_trans_DFm] = _trans_DFm, + [_trans_DLr] = _trans_DLr, + [_trans_DLm] = _trans_DRm, + [_trans_DBr] = _trans_DBr, + [_trans_DBm] = _trans_DBm, + [_trans_DRr] = _trans_DRr, + [_trans_DRm] = _trans_DLm, + [_trans_RUr] = _trans_FRr, + [_trans_RUm] = _trans_FLm, + [_trans_RFr] = _trans_LFr, + [_trans_RFm] = _trans_RFm, + [_trans_RDr] = _trans_BLr, + [_trans_RDm] = _trans_BRm, + [_trans_RBr] = _trans_RBr, + [_trans_RBm] = _trans_LBm, + [_trans_LUr] = _trans_FLr, + [_trans_LUm] = _trans_FRm, + [_trans_LFr] = _trans_RFr, + [_trans_LFm] = _trans_LFm, + [_trans_LDr] = _trans_BRr, + [_trans_LDm] = _trans_BLm, + [_trans_LBr] = _trans_LBr, + [_trans_LBm] = _trans_RBm, + [_trans_FUr] = _trans_FUr, + [_trans_FUm] = _trans_FUm, + [_trans_FRr] = _trans_RUr, + [_trans_FRm] = _trans_LUm, + [_trans_FDr] = _trans_BUr, + [_trans_FDm] = _trans_BUm, + [_trans_FLr] = _trans_LUr, + [_trans_FLm] = _trans_RUm, + [_trans_BUr] = _trans_FDr, + [_trans_BUm] = _trans_FDm, + [_trans_BRr] = _trans_LDr, + [_trans_BRm] = _trans_RDm, + [_trans_BDr] = _trans_BDr, + [_trans_BDm] = _trans_BDm, + [_trans_BLr] = _trans_RDr, + [_trans_BLm] = _trans_LDm, +}; diff --git a/src/utils/dbg_log.h b/src/utils/dbg_log.h new file mode 100644 index 0000000..427eceb --- /dev/null +++ b/src/utils/dbg_log.h @@ -0,0 +1,16 @@ +void (*nissy_log)(const char *, ...); + +#define LOG(...) if (nissy_log != NULL) nissy_log(__VA_ARGS__); + +#ifdef DEBUG +#define _static +#define _static_inline +#define DBG_WARN(condition, ...) if (!(condition)) LOG(__VA_ARGS__); +#define DBG_ASSERT(condition, retval, ...) \ + if (!(condition)) { LOG(__VA_ARGS__); return retval; } +#else +#define _static static +#define _static_inline static inline +#define DBG_WARN(condition, ...) +#define DBG_ASSERT(condition, retval, ...) +#endif diff --git a/src/utils/math.h b/src/utils/math.h new file mode 100644 index 0000000..87402e6 --- /dev/null +++ b/src/utils/math.h @@ -0,0 +1,185 @@ +#define _swap(x, y) do { x ^= y; y ^= x; x ^= y; } while (0) +#define _min(x, y) ((x) < (y) ? (x) : (y)) +#define _max(x, y) ((x) > (y) ? (x) : (y)) + +_static int64_t factorial(int64_t); +_static bool isperm(uint8_t *, int64_t); +_static int64_t permtoindex(uint8_t *, int64_t); +_static void indextoperm(int64_t, int64_t, uint8_t *); +_static int permsign(uint8_t *, int64_t); +_static int64_t digitstosumzero(uint8_t *, uint8_t, uint8_t); +_static void sumzerotodigits(int64_t, uint8_t, uint8_t, uint8_t *); + +_static int64_t +factorial(int64_t n) +{ + int64_t i, ret; + + if (n > _max_factorial) { + LOG("Error: won't compute factorial for n=%" PRId64 " because" + " it is larger than %" PRId64 "\n", n, _max_factorial); + return -1; + } + + if (n < 0) + return 0; + + for (i = 1, ret = 1; i <= n; i++) + ret *= i; + + return ret; +} + +_static bool +isperm(uint8_t *a, int64_t n) +{ + int64_t i; + bool aux[_max_factorial+1]; + + if (n > _max_factorial) { + LOG("Error: won't compute 'isperm()' for n=%" PRId64 " because" + " it is larger than %" PRId64 "\n", n, _max_factorial); + return false; + } + + memset(aux, false, n); + + for (i = 0; i < n; i++) { + if (a[i] >= n) + return false; + else + aux[a[i]] = true; + } + + for (i = 0; i < n; i++) + if (!aux[i]) + return false; + + return true; +} + +_static int64_t +permtoindex(uint8_t *a, int64_t n) +{ + int64_t i, j, c, ret; + + if (n > _max_factorial) { + LOG("Error: won't compute 'permtoindex()' for n=%" PRId64 + " because it is larger than %" PRId64 "\n", + n, _max_factorial); + return -1; + } + + if (!isperm(a, n)) + return -1; + + for (i = 0, ret = 0; i < n; i++) { + for (j = i+1, c = 0; j < n; j++) + c += (a[i] > a[j]) ? 1 : 0; + ret += factorial(n-i-1) * c; + } + + return ret; +} + +_static void +indextoperm(int64_t p, int64_t n, uint8_t *r) +{ + int64_t i, j, c; + uint8_t a[_max_factorial+1]; + + if (n > _max_factorial) { + LOG("Error: won't compute 'permtoindex()' for n=%" PRId64 + " because it is larger than %" PRId64 "\n", + n, _max_factorial); + goto indextoperm_error; + } + + memset(a, 0, n); + + if (p < 0 || p >= factorial(n)) + goto indextoperm_error; + + for (i = 0; i < n; i++) { + for (j = 0, c = 0; c <= p / factorial(n-i-1); j++) + c += a[j] ? 0 : 1; + r[i] = j-1; + a[j-1] = 1; + p %= factorial(n-i-1); + } + + if (!isperm(r, n)) + goto indextoperm_error; + + return; + +indextoperm_error: + memset(r, _error, n); +} + +_static int +permsign(uint8_t *a, int64_t n) +{ + int i, j; + uint8_t ret; + + for (i = 0, ret = 0; i < n; i++) + for (j = i+1; j < n; j++) + ret += a[i] > a[j] ? 1 : 0; + + return ret % 2; +} + +_static int64_t +digitstosumzero(uint8_t *a, uint8_t n, uint8_t b) +{ + int64_t ret, p; + uint8_t i, sum; + + if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) { + LOG("Won't compute 'sumzero' for n=%" PRIu8 "and b=%" PRIu8 + " (use n=8 b=3 or n=12 b=2)\n", n, b); + return -1; + } + + for (i = 1, ret = 0, p = 1, sum = 0; i < n; i++, p *= (int64_t)b) { + if (a[i] >= b) { + LOG("Error: digit %" PRIu8 " larger than maximum" + " (b=%" PRIu8 "\n", a[i], b); + return -1; + } + sum += a[i]; + ret += p * (int64_t)a[i]; + } + + if ((sum + a[0]) % b != 0) { + LOG("Error: digits do not have sum zero modulo b\n"); + return -1; + } + + return ret; +} + +_static void +sumzerotodigits(int64_t d, uint8_t n, uint8_t b, uint8_t *a) +{ + uint8_t sum; + int64_t i; + + if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) { + LOG("Won't compute 'digits' for n=%" PRIu8 "and b=%" PRIu8 + " (use n=8 b=3 or n=12 b=2)\n"); + goto digitstosumzero_error; + } + + for (i = 1, sum = 0; i < n; i++, d /= (int64_t)b) { + a[i] = (uint8_t)(d % (int64_t)b); + sum += a[i]; + } + a[0] = (b - (sum % b)) % b; + + return; + +digitstosumzero_error: + memset(a, _error, n); +} diff --git a/src/utils/utils.h b/src/utils/utils.h new file mode 100644 index 0000000..ce4355e --- /dev/null +++ b/src/utils/utils.h @@ -0,0 +1,3 @@ +#include "dbg_log.h" +#include "constants.h" +#include "math.h" diff --git a/test/test.h b/test/test.h index 1f4b2c3..d7658cb 100644 --- a/test/test.h +++ b/test/test.h @@ -1,3 +1,5 @@ +#define TEST_H + #include #include #include @@ -5,24 +7,9 @@ #include #include -#define STRLENMAX 10000 +#include "../src/arch/arch.h" -#if defined(CUBE_AVX2) -#include -typedef __m256i cube_t; -#elif defined(CUBE_NEON) -#include -#include -typedef struct { - uint8x16_t corner; - uint8x16_t edge; -} cube_t; -#else -typedef struct { - uint8_t corner[8]; - uint8_t edge[12]; -} cube_t; -#endif +#define STRLENMAX 10000 /* Basic functions used in most tests */ cube_t solvedcube(void); diff --git a/tools/001_gendata_h48/gendata_h48.c b/tools/001_gendata_h48/gendata_h48.c index 0456616..da0b4ad 100644 --- a/tools/001_gendata_h48/gendata_h48.c +++ b/tools/001_gendata_h48/gendata_h48.c @@ -1,5 +1,5 @@ #include "../timerun.h" -#include "../../src/cube.h" +#include "../../src/nissy.h" #define MAXDEPTH 20 #define HVALUE 0 diff --git a/tools/002_stats_tables_h48/stats_tables_h48.c b/tools/002_stats_tables_h48/stats_tables_h48.c index 7df396a..2e48497 100644 --- a/tools/002_stats_tables_h48/stats_tables_h48.c +++ b/tools/002_stats_tables_h48/stats_tables_h48.c @@ -1,7 +1,7 @@ #include #include #include "../timerun.h" -#include "../../src/cube.h" +#include "../../src/nissy.h" #define MAXMOVES 20 #define NTHREADS 32 diff --git a/utils/genmovecode.sh b/utils/genmovecode.sh index daf268e..ddde0f7 100755 --- a/utils/genmovecode.sh +++ b/utils/genmovecode.sh @@ -1,6 +1,6 @@ #!/bin/sh -cc -DDEBUG h48_to_lst.c ../src/cube.c -o h48_to_lst +cc -DDEBUG h48_to_lst.c ../src/nissy.c -o h48_to_lst gen() { for f in cubes/move_??_*.txt; do diff --git a/utils/gentranscode.sh b/utils/gentranscode.sh index 9f9ec4d..e3e199e 100755 --- a/utils/gentranscode.sh +++ b/utils/gentranscode.sh @@ -1,7 +1,7 @@ #!/bin/sh -cc -DDEBUG h48_to_lst.c ../src/cube.c -o h48_to_lst -cc -DDEBUG invert.c ../src/cube.c -o invert +cc -DDEBUG h48_to_lst.c ../src/nissy.c -o h48_to_lst +cc -DDEBUG invert.c ../src/nissy.c -o invert lineavx() { printf '#define _trans_cube_%s ' "$1"; } linesrc() { printf '_static cube_fast_t _trans_cube_%s = ' "$1"; } diff --git a/utils/h48_to_lst.c b/utils/h48_to_lst.c index 977c95d..03265b7 100644 --- a/utils/h48_to_lst.c +++ b/utils/h48_to_lst.c @@ -2,7 +2,7 @@ #include #include -#include "../src/cube.h" +#include "../src/nissy.h" #define STRLENMAX 1000 diff --git a/utils/invert.c b/utils/invert.c index 9121a47..41580c0 100644 --- a/utils/invert.c +++ b/utils/invert.c @@ -2,7 +2,7 @@ #include #include -#include "../src/cube.h" +#include "../src/nissy.h" #define STRLENMAX 1000 -- cgit v1.3