diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-04-12 20:22:01 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-04-12 20:22:01 +0200 |
| commit | 72e018b066bd5cc3ce0181b43865a227e8b9b6ec (patch) | |
| tree | 60b2e8dbe7fd2dd7cf58ecd1606e03146329b516 /cube.c | |
| download | cubecore-72e018b066bd5cc3ce0181b43865a227e8b9b6ec.tar.gz cubecore-72e018b066bd5cc3ce0181b43865a227e8b9b6ec.zip | |
Initial fork from H48
Diffstat (limited to 'cube.c')
| -rw-r--r-- | cube.c | 1520 |
1 files changed, 1520 insertions, 0 deletions
| @@ -0,0 +1,1520 @@ | |||
| 1 | #include <inttypes.h> | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | #include "cube.h" | ||
| 6 | |||
| 7 | #ifdef DEBUG | ||
| 8 | |||
| 9 | #include <stdio.h> | ||
| 10 | #define _static | ||
| 11 | #define _static_inline | ||
| 12 | #define DBG_LOG(...) fprintf(stderr, __VA_ARGS__) | ||
| 13 | #define DBG_WARN(condition, ...) if (!(condition)) DBG_LOG(__VA_ARGS__); | ||
| 14 | #define DBG_ASSERT(condition, retval, ...) \ | ||
| 15 | if (!(condition)) { \ | ||
| 16 | DBG_LOG(__VA_ARGS__); \ | ||
| 17 | return retval; \ | ||
| 18 | } | ||
| 19 | |||
| 20 | #else | ||
| 21 | |||
| 22 | #define _static static | ||
| 23 | #define _static_inline static inline | ||
| 24 | #define DBG_LOG(...) | ||
| 25 | #define DBG_WARN(condition, ...) | ||
| 26 | #define DBG_ASSERT(condition, retval, ...) | ||
| 27 | |||
| 28 | #endif | ||
| 29 | |||
| 30 | /****************************************************************************** | ||
| 31 | Section: mathematical constants | ||
| 32 | ******************************************************************************/ | ||
| 33 | |||
| 34 | #define _2p11 2048U | ||
| 35 | #define _2p12 4096U | ||
| 36 | #define _3p7 2187U | ||
| 37 | #define _3p8 6561U | ||
| 38 | #define _12c4 495U | ||
| 39 | #define _8c4 70U | ||
| 40 | |||
| 41 | _static int64_t binomial[12][12] = { | ||
| 42 | {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, | ||
| 43 | {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, | ||
| 44 | {1, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0}, | ||
| 45 | {1, 3, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0}, | ||
| 46 | {1, 4, 6, 4, 1, 0, 0, 0, 0, 0, 0, 0}, | ||
| 47 | {1, 5, 10, 10, 5, 1, 0, 0, 0, 0, 0, 0}, | ||
| 48 | {1, 6, 15, 20, 15, 6, 1, 0, 0, 0, 0, 0}, | ||
| 49 | {1, 7, 21, 35, 35, 21, 7, 1, 0, 0, 0, 0}, | ||
| 50 | {1, 8, 28, 56, 70, 56, 28, 8, 1, 0, 0, 0}, | ||
| 51 | {1, 9, 36, 84, 126, 126, 84, 36, 9, 1, 0, 0}, | ||
| 52 | {1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1, 0}, | ||
| 53 | {1, 11, 55, 165, 330, 462, 462, 330, 165, 55, 11, 1}, | ||
| 54 | }; | ||
| 55 | |||
| 56 | /****************************************************************************** | ||
| 57 | Section: moves definitions and tables | ||
| 58 | ******************************************************************************/ | ||
| 59 | |||
| 60 | #define U 0U | ||
| 61 | #define U2 1U | ||
| 62 | #define U3 2U | ||
| 63 | #define D 3U | ||
| 64 | #define D2 4U | ||
| 65 | #define D3 5U | ||
| 66 | #define R 6U | ||
| 67 | #define R2 7U | ||
| 68 | #define R3 8U | ||
| 69 | #define L 9U | ||
| 70 | #define L2 10U | ||
| 71 | #define L3 11U | ||
| 72 | #define F 12U | ||
| 73 | #define F2 13U | ||
| 74 | #define F3 14U | ||
| 75 | #define B 15U | ||
| 76 | #define B2 16U | ||
| 77 | #define B3 17U | ||
| 78 | |||
| 79 | #define UFr 0 | ||
| 80 | #define ULr 1 | ||
| 81 | #define UBr 2 | ||
| 82 | #define URr 3 | ||
| 83 | #define DFr 4 | ||
| 84 | #define DLr 5 | ||
| 85 | #define DBr 6 | ||
| 86 | #define DRr 7 | ||
| 87 | #define RUr 8 | ||
| 88 | #define RFr 9 | ||
| 89 | #define RDr 10 | ||
| 90 | #define RBr 11 | ||
| 91 | #define LUr 12 | ||
| 92 | #define LFr 13 | ||
| 93 | #define LDr 14 | ||
| 94 | #define LBr 15 | ||
| 95 | #define FUr 16 | ||
| 96 | #define FRr 17 | ||
| 97 | #define FDr 18 | ||
| 98 | #define FLr 19 | ||
| 99 | #define BUr 20 | ||
| 100 | #define BRr 21 | ||
| 101 | #define BDr 22 | ||
| 102 | #define BLr 23 | ||
| 103 | |||
| 104 | #define UFm 24 | ||
| 105 | #define ULm 25 | ||
| 106 | #define UBm 26 | ||
| 107 | #define URm 27 | ||
| 108 | #define DFm 28 | ||
| 109 | #define DLm 29 | ||
| 110 | #define DBm 30 | ||
| 111 | #define DRm 31 | ||
| 112 | #define RUm 32 | ||
| 113 | #define RFm 33 | ||
| 114 | #define RDm 34 | ||
| 115 | #define RBm 35 | ||
| 116 | #define LUm 36 | ||
| 117 | #define LFm 37 | ||
| 118 | #define LDm 38 | ||
| 119 | #define LBm 39 | ||
| 120 | #define FUm 40 | ||
| 121 | #define FRm 41 | ||
| 122 | #define FDm 42 | ||
| 123 | #define FLm 43 | ||
| 124 | #define BUm 44 | ||
| 125 | #define BRm 45 | ||
| 126 | #define BDm 46 | ||
| 127 | #define BLm 47 | ||
| 128 | |||
| 129 | #define _c_ufr 0U | ||
| 130 | #define _c_ubl 1U | ||
| 131 | #define _c_dfl 2U | ||
| 132 | #define _c_dbr 3U | ||
| 133 | #define _c_ufl 4U | ||
| 134 | #define _c_ubr 5U | ||
| 135 | #define _c_dfr 6U | ||
| 136 | #define _c_dbl 7U | ||
| 137 | |||
| 138 | #define _e_uf 0U | ||
| 139 | #define _e_ub 1U | ||
| 140 | #define _e_db 2U | ||
| 141 | #define _e_df 3U | ||
| 142 | #define _e_ur 4U | ||
| 143 | #define _e_ul 5U | ||
| 144 | #define _e_dl 6U | ||
| 145 | #define _e_dr 7U | ||
| 146 | #define _e_fr 8U | ||
| 147 | #define _e_fl 9U | ||
| 148 | #define _e_bl 10U | ||
| 149 | #define _e_br 11U | ||
| 150 | |||
| 151 | #define _eoshift 4U | ||
| 152 | #define _coshift 5U | ||
| 153 | |||
| 154 | #define _pbits 0xFU | ||
| 155 | #define _esepbit1 0x4U | ||
| 156 | #define _esepbit2 0x8U | ||
| 157 | #define _csepbit 0x4U | ||
| 158 | #define _eobit 0x10U | ||
| 159 | #define _cobits 0xF0U | ||
| 160 | #define _cobits2 0x60U | ||
| 161 | #define _ctwist_cw 0x20U | ||
| 162 | #define _ctwist_ccw 0x40U | ||
| 163 | #define _eflip 0x10U | ||
| 164 | #define _error 0xFFU | ||
| 165 | |||
| 166 | _static cube_t zero = { .corner = {0}, .edge = {0} }; | ||
| 167 | _static cube_t solved = { | ||
| 168 | .corner = {0, 1, 2, 3, 4, 5, 6, 7}, | ||
| 169 | .edge = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} | ||
| 170 | }; | ||
| 171 | |||
| 172 | #define zero_fast fastcube( \ | ||
| 173 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) | ||
| 174 | #define solved_fast fastcube( \ | ||
| 175 | 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) | ||
| 176 | |||
| 177 | #define _move_cube_U fastcube( \ | ||
| 178 | 5, 4, 2, 3, 0, 1, 6, 7, 4, 5, 2, 3, 1, 0, 6, 7, 8, 9, 10, 11) | ||
| 179 | #define _move_cube_U2 fastcube( \ | ||
| 180 | 1, 0, 2, 3, 5, 4, 6, 7, 1, 0, 2, 3, 5, 4, 6, 7, 8, 9, 10, 11) | ||
| 181 | #define _move_cube_U3 fastcube( \ | ||
| 182 | 4, 5, 2, 3, 1, 0, 6, 7, 5, 4, 2, 3, 0, 1, 6, 7, 8, 9, 10, 11) | ||
| 183 | #define _move_cube_D fastcube( \ | ||
| 184 | 0, 1, 7, 6, 4, 5, 2, 3, 0, 1, 7, 6, 4, 5, 2, 3, 8, 9, 10, 11) | ||
| 185 | #define _move_cube_D2 fastcube( \ | ||
| 186 | 0, 1, 3, 2, 4, 5, 7, 6, 0, 1, 3, 2, 4, 5, 7, 6, 8, 9, 10, 11) | ||
| 187 | #define _move_cube_D3 fastcube( \ | ||
| 188 | 0, 1, 6, 7, 4, 5, 3, 2, 0, 1, 6, 7, 4, 5, 3, 2, 8, 9, 10, 11) | ||
| 189 | #define _move_cube_R fastcube( \ | ||
| 190 | 70, 1, 2, 69, 4, 32, 35, 7, 0, 1, 2, 3, 8, 5, 6, 11, 7, 9, 10, 4) | ||
| 191 | #define _move_cube_R2 fastcube( \ | ||
| 192 | 3, 1, 2, 0, 4, 6, 5, 7, 0, 1, 2, 3, 7, 5, 6, 4, 11, 9, 10, 8) | ||
| 193 | #define _move_cube_R3 fastcube( \ | ||
| 194 | 69, 1, 2, 70, 4, 35, 32, 7, 0, 1, 2, 3, 11, 5, 6, 8, 4, 9, 10, 7) | ||
| 195 | #define _move_cube_L fastcube( \ | ||
| 196 | 0, 71, 68, 3, 33, 5, 6, 34, 0, 1, 2, 3, 4, 10, 9, 7, 8, 5, 6, 11) | ||
| 197 | #define _move_cube_L2 fastcube( \ | ||
| 198 | 0, 2, 1, 3, 7, 5, 6, 4, 0, 1, 2, 3, 4, 6, 5, 7, 8, 10, 9, 11) | ||
| 199 | #define _move_cube_L3 fastcube( \ | ||
| 200 | 0, 68, 71, 3, 34, 5, 6, 33, 0, 1, 2, 3, 4, 9, 10, 7, 8, 6, 5, 11) | ||
| 201 | #define _move_cube_F fastcube( \ | ||
| 202 | 36, 1, 38, 3, 66, 5, 64, 7, 25, 1, 2, 24, 4, 5, 6, 7, 16, 19, 10, 11) | ||
| 203 | #define _move_cube_F2 fastcube( \ | ||
| 204 | 2, 1, 0, 3, 6, 5, 4, 7, 3, 1, 2, 0, 4, 5, 6, 7, 9, 8, 10, 11) | ||
| 205 | #define _move_cube_F3 fastcube( \ | ||
| 206 | 38, 1, 36, 3, 64, 5, 66, 7, 24, 1, 2, 25, 4, 5, 6, 7, 19, 16, 10, 11) | ||
| 207 | #define _move_cube_B fastcube( \ | ||
| 208 | 0, 37, 2, 39, 4, 67, 6, 65, 0, 27, 26, 3, 4, 5, 6, 7, 8, 9, 17, 18) | ||
| 209 | #define _move_cube_B2 fastcube( \ | ||
| 210 | 0, 3, 2, 1, 4, 7, 6, 5, 0, 2, 1, 3, 4, 5, 6, 7, 8, 9, 11, 10) | ||
| 211 | #define _move_cube_B3 fastcube( \ | ||
| 212 | 0, 39, 2, 37, 4, 65, 6, 67, 0, 26, 27, 3, 4, 5, 6, 7, 8, 9, 18, 17) | ||
| 213 | |||
| 214 | #define _trans_cube_UFr fastcube( \ | ||
| 215 | 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) | ||
| 216 | #define _trans_cube_UFr_inverse fastcube( \ | ||
| 217 | 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11) | ||
| 218 | #define _trans_cube_ULr fastcube( \ | ||
| 219 | 4, 5, 7, 6, 1, 0, 2, 3, 5, 4, 7, 6, 0, 1, 2, 3, 25, 26, 27, 24) | ||
| 220 | #define _trans_cube_ULr_inverse fastcube( \ | ||
| 221 | 5, 4, 6, 7, 0, 1, 3, 2, 4, 5, 6, 7, 1, 0, 3, 2, 27, 24, 25, 26) | ||
| 222 | #define _trans_cube_UBr fastcube( \ | ||
| 223 | 1, 0, 3, 2, 5, 4, 7, 6, 1, 0, 3, 2, 5, 4, 7, 6, 10, 11, 8, 9) | ||
| 224 | #define _trans_cube_UBr_inverse fastcube( \ | ||
| 225 | 1, 0, 3, 2, 5, 4, 7, 6, 1, 0, 3, 2, 5, 4, 7, 6, 10, 11, 8, 9) | ||
| 226 | #define _trans_cube_URr fastcube( \ | ||
| 227 | 5, 4, 6, 7, 0, 1, 3, 2, 4, 5, 6, 7, 1, 0, 3, 2, 27, 24, 25, 26) | ||
| 228 | #define _trans_cube_URr_inverse fastcube( \ | ||
| 229 | 4, 5, 7, 6, 1, 0, 2, 3, 5, 4, 7, 6, 0, 1, 2, 3, 25, 26, 27, 24) | ||
| 230 | #define _trans_cube_DFr fastcube( \ | ||
| 231 | 2, 3, 0, 1, 6, 7, 4, 5, 3, 2, 1, 0, 6, 7, 4, 5, 9, 8, 11, 10) | ||
| 232 | #define _trans_cube_DFr_inverse fastcube( \ | ||
| 233 | 2, 3, 0, 1, 6, 7, 4, 5, 3, 2, 1, 0, 6, 7, 4, 5, 9, 8, 11, 10) | ||
| 234 | #define _trans_cube_DLr fastcube( \ | ||
| 235 | 7, 6, 4, 5, 2, 3, 1, 0, 6, 7, 4, 5, 2, 3, 0, 1, 26, 25, 24, 27) | ||
| 236 | #define _trans_cube_DLr_inverse fastcube( \ | ||
| 237 | 7, 6, 4, 5, 2, 3, 1, 0, 6, 7, 4, 5, 2, 3, 0, 1, 26, 25, 24, 27) | ||
| 238 | #define _trans_cube_DBr fastcube( \ | ||
| 239 | 3, 2, 1, 0, 7, 6, 5, 4, 2, 3, 0, 1, 7, 6, 5, 4, 11, 10, 9, 8) | ||
| 240 | #define _trans_cube_DBr_inverse fastcube( \ | ||
| 241 | 3, 2, 1, 0, 7, 6, 5, 4, 2, 3, 0, 1, 7, 6, 5, 4, 11, 10, 9, 8) | ||
| 242 | #define _trans_cube_DRr fastcube( \ | ||
| 243 | 6, 7, 5, 4, 3, 2, 0, 1, 7, 6, 5, 4, 3, 2, 1, 0, 24, 27, 26, 25) | ||
| 244 | #define _trans_cube_DRr_inverse fastcube( \ | ||
| 245 | 6, 7, 5, 4, 3, 2, 0, 1, 7, 6, 5, 4, 3, 2, 1, 0, 24, 27, 26, 25) | ||
| 246 | #define _trans_cube_RUr fastcube( \ | ||
| 247 | 64, 67, 65, 66, 37, 38, 36, 39, 20, 23, 22, 21, 24, 27, 26, 25, 0, 1, 2, 3) | ||
| 248 | #define _trans_cube_RUr_inverse fastcube( \ | ||
| 249 | 32, 34, 35, 33, 70, 68, 69, 71, 8, 9, 10, 11, 16, 19, 18, 17, 20, 23, 22, 21) | ||
| 250 | #define _trans_cube_RFr fastcube( \ | ||
| 251 | 38, 37, 36, 39, 64, 67, 66, 65, 24, 27, 26, 25, 23, 20, 21, 22, 19, 16, 17, 18) | ||
| 252 | #define _trans_cube_RFr_inverse fastcube( \ | ||
| 253 | 36, 39, 38, 37, 66, 65, 64, 67, 25, 26, 27, 24, 21, 22, 23, 20, 16, 19, 18, 17) | ||
| 254 | #define _trans_cube_RDr fastcube( \ | ||
| 255 | 67, 64, 66, 65, 38, 37, 39, 36, 23, 20, 21, 22, 27, 24, 25, 26, 2, 3, 0, 1) | ||
| 256 | #define _trans_cube_RDr_inverse fastcube( \ | ||
| 257 | 33, 35, 34, 32, 71, 69, 68, 70, 10, 11, 8, 9, 17, 18, 19, 16, 21, 22, 23, 20) | ||
| 258 | #define _trans_cube_RBr fastcube( \ | ||
| 259 | 37, 38, 39, 36, 67, 64, 65, 66, 27, 24, 25, 26, 20, 23, 22, 21, 17, 18, 19, 16) | ||
| 260 | #define _trans_cube_RBr_inverse fastcube( \ | ||
| 261 | 37, 38, 39, 36, 67, 64, 65, 66, 27, 24, 25, 26, 20, 23, 22, 21, 17, 18, 19, 16) | ||
| 262 | #define _trans_cube_LUr fastcube( \ | ||
| 263 | 65, 66, 64, 67, 36, 39, 37, 38, 21, 22, 23, 20, 26, 25, 24, 27, 1, 0, 3, 2) | ||
| 264 | #define _trans_cube_LUr_inverse fastcube( \ | ||
| 265 | 34, 32, 33, 35, 68, 70, 71, 69, 9, 8, 11, 10, 19, 16, 17, 18, 22, 21, 20, 23) | ||
| 266 | #define _trans_cube_LFr fastcube( \ | ||
| 267 | 36, 39, 38, 37, 66, 65, 64, 67, 25, 26, 27, 24, 21, 22, 23, 20, 16, 19, 18, 17) | ||
| 268 | #define _trans_cube_LFr_inverse fastcube( \ | ||
| 269 | 38, 37, 36, 39, 64, 67, 66, 65, 24, 27, 26, 25, 23, 20, 21, 22, 19, 16, 17, 18) | ||
| 270 | #define _trans_cube_LDr fastcube( \ | ||
| 271 | 66, 65, 67, 64, 39, 36, 38, 37, 22, 21, 20, 23, 25, 26, 27, 24, 3, 2, 1, 0) | ||
| 272 | #define _trans_cube_LDr_inverse fastcube( \ | ||
| 273 | 35, 33, 32, 34, 69, 71, 70, 68, 11, 10, 9, 8, 18, 17, 16, 19, 23, 20, 21, 22) | ||
| 274 | #define _trans_cube_LBr fastcube( \ | ||
| 275 | 39, 36, 37, 38, 65, 66, 67, 64, 26, 25, 24, 27, 22, 21, 20, 23, 18, 17, 16, 19) | ||
| 276 | #define _trans_cube_LBr_inverse fastcube( \ | ||
| 277 | 39, 36, 37, 38, 65, 66, 67, 64, 26, 25, 24, 27, 22, 21, 20, 23, 18, 17, 16, 19) | ||
| 278 | #define _trans_cube_FUr fastcube( \ | ||
| 279 | 68, 70, 69, 71, 32, 34, 33, 35, 16, 19, 18, 17, 9, 8, 11, 10, 5, 4, 7, 6) | ||
| 280 | #define _trans_cube_FUr_inverse fastcube( \ | ||
| 281 | 68, 70, 69, 71, 32, 34, 33, 35, 16, 19, 18, 17, 9, 8, 11, 10, 5, 4, 7, 6) | ||
| 282 | #define _trans_cube_FRr fastcube( \ | ||
| 283 | 32, 34, 35, 33, 70, 68, 69, 71, 8, 9, 10, 11, 16, 19, 18, 17, 20, 23, 22, 21) | ||
| 284 | #define _trans_cube_FRr_inverse fastcube( \ | ||
| 285 | 64, 67, 65, 66, 37, 38, 36, 39, 20, 23, 22, 21, 24, 27, 26, 25, 0, 1, 2, 3) | ||
| 286 | #define _trans_cube_FDr fastcube( \ | ||
| 287 | 70, 68, 71, 69, 34, 32, 35, 33, 19, 16, 17, 18, 8, 9, 10, 11, 7, 6, 5, 4) | ||
| 288 | #define _trans_cube_FDr_inverse fastcube( \ | ||
| 289 | 69, 71, 68, 70, 33, 35, 32, 34, 17, 18, 19, 16, 11, 10, 9, 8, 4, 5, 6, 7) | ||
| 290 | #define _trans_cube_FLr fastcube( \ | ||
| 291 | 34, 32, 33, 35, 68, 70, 71, 69, 9, 8, 11, 10, 19, 16, 17, 18, 22, 21, 20, 23) | ||
| 292 | #define _trans_cube_FLr_inverse fastcube( \ | ||
| 293 | 65, 66, 64, 67, 36, 39, 37, 38, 21, 22, 23, 20, 26, 25, 24, 27, 1, 0, 3, 2) | ||
| 294 | #define _trans_cube_BUr fastcube( \ | ||
| 295 | 69, 71, 68, 70, 33, 35, 32, 34, 17, 18, 19, 16, 11, 10, 9, 8, 4, 5, 6, 7) | ||
| 296 | #define _trans_cube_BUr_inverse fastcube( \ | ||
| 297 | 70, 68, 71, 69, 34, 32, 35, 33, 19, 16, 17, 18, 8, 9, 10, 11, 7, 6, 5, 4) | ||
| 298 | #define _trans_cube_BRr fastcube( \ | ||
| 299 | 35, 33, 32, 34, 69, 71, 70, 68, 11, 10, 9, 8, 18, 17, 16, 19, 23, 20, 21, 22) | ||
| 300 | #define _trans_cube_BRr_inverse fastcube( \ | ||
| 301 | 66, 65, 67, 64, 39, 36, 38, 37, 22, 21, 20, 23, 25, 26, 27, 24, 3, 2, 1, 0) | ||
| 302 | #define _trans_cube_BDr fastcube( \ | ||
| 303 | 71, 69, 70, 68, 35, 33, 34, 32, 18, 17, 16, 19, 10, 11, 8, 9, 6, 7, 4, 5) | ||
| 304 | #define _trans_cube_BDr_inverse fastcube( \ | ||
| 305 | 71, 69, 70, 68, 35, 33, 34, 32, 18, 17, 16, 19, 10, 11, 8, 9, 6, 7, 4, 5) | ||
| 306 | #define _trans_cube_BLr fastcube( \ | ||
| 307 | 33, 35, 34, 32, 71, 69, 68, 70, 10, 11, 8, 9, 17, 18, 19, 16, 21, 22, 23, 20) | ||
| 308 | #define _trans_cube_BLr_inverse fastcube( \ | ||
| 309 | 67, 64, 66, 65, 38, 37, 39, 36, 23, 20, 21, 22, 27, 24, 25, 26, 2, 3, 0, 1) | ||
| 310 | #define _trans_cube_UFm fastcube( \ | ||
| 311 | 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 2, 3, 5, 4, 7, 6, 9, 8, 11, 10) | ||
| 312 | #define _trans_cube_UFm_inverse fastcube( \ | ||
| 313 | 4, 5, 6, 7, 0, 1, 2, 3, 0, 1, 2, 3, 5, 4, 7, 6, 9, 8, 11, 10) | ||
| 314 | #define _trans_cube_ULm fastcube( \ | ||
| 315 | 0, 1, 3, 2, 5, 4, 6, 7, 4, 5, 6, 7, 0, 1, 2, 3, 24, 27, 26, 25) | ||
| 316 | #define _trans_cube_ULm_inverse fastcube( \ | ||
| 317 | 0, 1, 3, 2, 5, 4, 6, 7, 4, 5, 6, 7, 0, 1, 2, 3, 24, 27, 26, 25) | ||
| 318 | #define _trans_cube_UBm fastcube( \ | ||
| 319 | 5, 4, 7, 6, 1, 0, 3, 2, 1, 0, 3, 2, 4, 5, 6, 7, 11, 10, 9, 8) | ||
| 320 | #define _trans_cube_UBm_inverse fastcube( \ | ||
| 321 | 5, 4, 7, 6, 1, 0, 3, 2, 1, 0, 3, 2, 4, 5, 6, 7, 11, 10, 9, 8) | ||
| 322 | #define _trans_cube_URm fastcube( \ | ||
| 323 | 1, 0, 2, 3, 4, 5, 7, 6, 5, 4, 7, 6, 1, 0, 3, 2, 26, 25, 24, 27) | ||
| 324 | #define _trans_cube_URm_inverse fastcube( \ | ||
| 325 | 1, 0, 2, 3, 4, 5, 7, 6, 5, 4, 7, 6, 1, 0, 3, 2, 26, 25, 24, 27) | ||
| 326 | #define _trans_cube_DFm fastcube( \ | ||
| 327 | 6, 7, 4, 5, 2, 3, 0, 1, 3, 2, 1, 0, 7, 6, 5, 4, 8, 9, 10, 11) | ||
| 328 | #define _trans_cube_DFm_inverse fastcube( \ | ||
| 329 | 6, 7, 4, 5, 2, 3, 0, 1, 3, 2, 1, 0, 7, 6, 5, 4, 8, 9, 10, 11) | ||
| 330 | #define _trans_cube_DLm fastcube( \ | ||
| 331 | 3, 2, 0, 1, 6, 7, 5, 4, 7, 6, 5, 4, 2, 3, 0, 1, 27, 24, 25, 26) | ||
| 332 | #define _trans_cube_DLm_inverse fastcube( \ | ||
| 333 | 2, 3, 1, 0, 7, 6, 4, 5, 6, 7, 4, 5, 3, 2, 1, 0, 25, 26, 27, 24) | ||
| 334 | #define _trans_cube_DBm fastcube( \ | ||
| 335 | 7, 6, 5, 4, 3, 2, 1, 0, 2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9) | ||
| 336 | #define _trans_cube_DBm_inverse fastcube( \ | ||
| 337 | 7, 6, 5, 4, 3, 2, 1, 0, 2, 3, 0, 1, 6, 7, 4, 5, 10, 11, 8, 9) | ||
| 338 | #define _trans_cube_DRm fastcube( \ | ||
| 339 | 2, 3, 1, 0, 7, 6, 4, 5, 6, 7, 4, 5, 3, 2, 1, 0, 25, 26, 27, 24) | ||
| 340 | #define _trans_cube_DRm_inverse fastcube( \ | ||
| 341 | 3, 2, 0, 1, 6, 7, 5, 4, 7, 6, 5, 4, 2, 3, 0, 1, 27, 24, 25, 26) | ||
| 342 | #define _trans_cube_RUm fastcube( \ | ||
| 343 | 68, 71, 69, 70, 33, 34, 32, 35, 21, 22, 23, 20, 25, 26, 27, 24, 0, 1, 2, 3) | ||
| 344 | #define _trans_cube_RUm_inverse fastcube( \ | ||
| 345 | 70, 68, 69, 71, 32, 34, 35, 33, 8, 9, 10, 11, 19, 16, 17, 18, 23, 20, 21, 22) | ||
| 346 | #define _trans_cube_RFm fastcube( \ | ||
| 347 | 34, 33, 32, 35, 68, 71, 70, 69, 25, 26, 27, 24, 22, 21, 20, 23, 19, 16, 17, 18) | ||
| 348 | #define _trans_cube_RFm_inverse fastcube( \ | ||
| 349 | 66, 65, 64, 67, 36, 39, 38, 37, 25, 26, 27, 24, 22, 21, 20, 23, 19, 16, 17, 18) | ||
| 350 | #define _trans_cube_RDm fastcube( \ | ||
| 351 | 71, 68, 70, 69, 34, 33, 35, 32, 22, 21, 20, 23, 26, 25, 24, 27, 2, 3, 0, 1) | ||
| 352 | #define _trans_cube_RDm_inverse fastcube( \ | ||
| 353 | 71, 69, 68, 70, 33, 35, 34, 32, 10, 11, 8, 9, 18, 17, 16, 19, 22, 21, 20, 23) | ||
| 354 | #define _trans_cube_RBm fastcube( \ | ||
| 355 | 33, 34, 35, 32, 71, 68, 69, 70, 26, 25, 24, 27, 21, 22, 23, 20, 17, 18, 19, 16) | ||
| 356 | #define _trans_cube_RBm_inverse fastcube( \ | ||
| 357 | 67, 64, 65, 66, 37, 38, 39, 36, 27, 24, 25, 26, 23, 20, 21, 22, 18, 17, 16, 19) | ||
| 358 | #define _trans_cube_LUm fastcube( \ | ||
| 359 | 69, 70, 68, 71, 32, 35, 33, 34, 20, 23, 22, 21, 27, 24, 25, 26, 1, 0, 3, 2) | ||
| 360 | #define _trans_cube_LUm_inverse fastcube( \ | ||
| 361 | 68, 70, 71, 69, 34, 32, 33, 35, 9, 8, 11, 10, 16, 19, 18, 17, 21, 22, 23, 20) | ||
| 362 | #define _trans_cube_LFm fastcube( \ | ||
| 363 | 32, 35, 34, 33, 70, 69, 68, 71, 24, 27, 26, 25, 20, 23, 22, 21, 16, 19, 18, 17) | ||
| 364 | #define _trans_cube_LFm_inverse fastcube( \ | ||
| 365 | 64, 67, 66, 65, 38, 37, 36, 39, 24, 27, 26, 25, 20, 23, 22, 21, 16, 19, 18, 17) | ||
| 366 | #define _trans_cube_LDm fastcube( \ | ||
| 367 | 70, 69, 71, 68, 35, 32, 34, 33, 23, 20, 21, 22, 24, 27, 26, 25, 3, 2, 1, 0) | ||
| 368 | #define _trans_cube_LDm_inverse fastcube( \ | ||
| 369 | 69, 71, 70, 68, 35, 33, 32, 34, 11, 10, 9, 8, 17, 18, 19, 16, 20, 23, 22, 21) | ||
| 370 | #define _trans_cube_LBm fastcube( \ | ||
| 371 | 35, 32, 33, 34, 69, 70, 71, 68, 27, 24, 25, 26, 23, 20, 21, 22, 18, 17, 16, 19) | ||
| 372 | #define _trans_cube_LBm_inverse fastcube( \ | ||
| 373 | 65, 66, 67, 64, 39, 36, 37, 38, 26, 25, 24, 27, 21, 22, 23, 20, 17, 18, 19, 16) | ||
| 374 | #define _trans_cube_FUm fastcube( \ | ||
| 375 | 64, 66, 65, 67, 36, 38, 37, 39, 16, 19, 18, 17, 8, 9, 10, 11, 4, 5, 6, 7) | ||
| 376 | #define _trans_cube_FUm_inverse fastcube( \ | ||
| 377 | 32, 34, 33, 35, 68, 70, 69, 71, 16, 19, 18, 17, 8, 9, 10, 11, 4, 5, 6, 7) | ||
| 378 | #define _trans_cube_FRm fastcube( \ | ||
| 379 | 36, 38, 39, 37, 66, 64, 65, 67, 9, 8, 11, 10, 16, 19, 18, 17, 21, 22, 23, 20) | ||
| 380 | #define _trans_cube_FRm_inverse fastcube( \ | ||
| 381 | 37, 38, 36, 39, 64, 67, 65, 66, 20, 23, 22, 21, 27, 24, 25, 26, 1, 0, 3, 2) | ||
| 382 | #define _trans_cube_FDm fastcube( \ | ||
| 383 | 66, 64, 67, 65, 38, 36, 39, 37, 19, 16, 17, 18, 9, 8, 11, 10, 6, 7, 4, 5) | ||
| 384 | #define _trans_cube_FDm_inverse fastcube( \ | ||
| 385 | 33, 35, 32, 34, 69, 71, 68, 70, 17, 18, 19, 16, 10, 11, 8, 9, 5, 4, 7, 6) | ||
| 386 | #define _trans_cube_FLm fastcube( \ | ||
| 387 | 38, 36, 37, 39, 64, 66, 67, 65, 8, 9, 10, 11, 19, 16, 17, 18, 23, 20, 21, 22) | ||
| 388 | #define _trans_cube_FLm_inverse fastcube( \ | ||
| 389 | 36, 39, 37, 38, 65, 66, 64, 67, 21, 22, 23, 20, 25, 26, 27, 24, 0, 1, 2, 3) | ||
| 390 | #define _trans_cube_BUm fastcube( \ | ||
| 391 | 65, 67, 64, 66, 37, 39, 36, 38, 17, 18, 19, 16, 10, 11, 8, 9, 5, 4, 7, 6) | ||
| 392 | #define _trans_cube_BUm_inverse fastcube( \ | ||
| 393 | 34, 32, 35, 33, 70, 68, 71, 69, 19, 16, 17, 18, 9, 8, 11, 10, 6, 7, 4, 5) | ||
| 394 | #define _trans_cube_BRm fastcube( \ | ||
| 395 | 39, 37, 36, 38, 65, 67, 66, 64, 10, 11, 8, 9, 18, 17, 16, 19, 22, 21, 20, 23) | ||
| 396 | #define _trans_cube_BRm_inverse fastcube( \ | ||
| 397 | 39, 36, 38, 37, 66, 65, 67, 64, 22, 21, 20, 23, 26, 25, 24, 27, 2, 3, 0, 1) | ||
| 398 | #define _trans_cube_BDm fastcube( \ | ||
| 399 | 67, 65, 66, 64, 39, 37, 38, 36, 18, 17, 16, 19, 11, 10, 9, 8, 7, 6, 5, 4) | ||
| 400 | #define _trans_cube_BDm_inverse fastcube( \ | ||
| 401 | 35, 33, 34, 32, 71, 69, 70, 68, 18, 17, 16, 19, 11, 10, 9, 8, 7, 6, 5, 4) | ||
| 402 | #define _trans_cube_BLm fastcube( \ | ||
| 403 | 37, 39, 38, 36, 67, 65, 64, 66, 11, 10, 9, 8, 17, 18, 19, 16, 20, 23, 22, 21) | ||
| 404 | #define _trans_cube_BLm_inverse fastcube( \ | ||
| 405 | 38, 37, 39, 36, 67, 64, 66, 65, 23, 20, 21, 22, 24, 27, 26, 25, 3, 2, 1, 0) | ||
| 406 | |||
| 407 | _static char *cornerstr[] = { | ||
| 408 | [_c_ufr] = "UFR", | ||
| 409 | [_c_ubl] = "UBL", | ||
| 410 | [_c_dfl] = "DFL", | ||
| 411 | [_c_dbr] = "DBR", | ||
| 412 | [_c_ufl] = "UFL", | ||
| 413 | [_c_ubr] = "UBR", | ||
| 414 | [_c_dfr] = "DFR", | ||
| 415 | [_c_dbl] = "DBL" | ||
| 416 | }; | ||
| 417 | |||
| 418 | _static char *cornerstralt[] = { | ||
| 419 | [_c_ufr] = "URF", | ||
| 420 | [_c_ubl] = "ULB", | ||
| 421 | [_c_dfl] = "DLF", | ||
| 422 | [_c_dbr] = "DRB", | ||
| 423 | [_c_ufl] = "ULF", | ||
| 424 | [_c_ubr] = "URB", | ||
| 425 | [_c_dfr] = "DRF", | ||
| 426 | [_c_dbl] = "DLB" | ||
| 427 | }; | ||
| 428 | |||
| 429 | _static char *edgestr[] = { | ||
| 430 | [_e_uf] = "UF", | ||
| 431 | [_e_ub] = "UB", | ||
| 432 | [_e_db] = "DB", | ||
| 433 | [_e_df] = "DF", | ||
| 434 | [_e_ur] = "UR", | ||
| 435 | [_e_ul] = "UL", | ||
| 436 | [_e_dl] = "DL", | ||
| 437 | [_e_dr] = "DR", | ||
| 438 | [_e_fr] = "FR", | ||
| 439 | [_e_fl] = "FL", | ||
| 440 | [_e_bl] = "BL", | ||
| 441 | [_e_br] = "BR" | ||
| 442 | }; | ||
| 443 | |||
| 444 | _static char *movestr[] = { | ||
| 445 | [U] = "U", | ||
| 446 | [U2] = "U2", | ||
| 447 | [U3] = "U'", | ||
| 448 | [D] = "D", | ||
| 449 | [D2] = "D2", | ||
| 450 | [D3] = "D'", | ||
| 451 | [R] = "R", | ||
| 452 | [R2] = "R2", | ||
| 453 | [R3] = "R'", | ||
| 454 | [L] = "L", | ||
| 455 | [L2] = "L2", | ||
| 456 | [L3] = "L'", | ||
| 457 | [F] = "F", | ||
| 458 | [F2] = "F2", | ||
| 459 | [F3] = "F'", | ||
| 460 | [B] = "B", | ||
| 461 | [B2] = "B2", | ||
| 462 | [B3] = "B'", | ||
| 463 | }; | ||
| 464 | |||
| 465 | _static char *transstr[] = { | ||
| 466 | [UFr] = "rotation UF", | ||
| 467 | [UFm] = "mirrored UF", | ||
| 468 | [ULr] = "rotation UL", | ||
| 469 | [ULm] = "mirrored UL", | ||
| 470 | [UBr] = "rotation UB", | ||
| 471 | [UBm] = "mirrored UB", | ||
| 472 | [URr] = "rotation UR", | ||
| 473 | [URm] = "mirrored UR", | ||
| 474 | [DFr] = "rotation DF", | ||
| 475 | [DFm] = "mirrored DF", | ||
| 476 | [DLr] = "rotation DL", | ||
| 477 | [DLm] = "mirrored DL", | ||
| 478 | [DBr] = "rotation DB", | ||
| 479 | [DBm] = "mirrored DB", | ||
| 480 | [DRr] = "rotation DR", | ||
| 481 | [DRm] = "mirrored DR", | ||
| 482 | [RUr] = "rotation RU", | ||
| 483 | [RUm] = "mirrored RU", | ||
| 484 | [RFr] = "rotation RF", | ||
| 485 | [RFm] = "mirrored RF", | ||
| 486 | [RDr] = "rotation RD", | ||
| 487 | [RDm] = "mirrored RD", | ||
| 488 | [RBr] = "rotation RB", | ||
| 489 | [RBm] = "mirrored RB", | ||
| 490 | [LUr] = "rotation LU", | ||
| 491 | [LUm] = "mirrored LU", | ||
| 492 | [LFr] = "rotation LF", | ||
| 493 | [LFm] = "mirrored LF", | ||
| 494 | [LDr] = "rotation LD", | ||
| 495 | [LDm] = "mirrored LD", | ||
| 496 | [LBr] = "rotation LB", | ||
| 497 | [LBm] = "mirrored LB", | ||
| 498 | [FUr] = "rotation FU", | ||
| 499 | [FUm] = "mirrored FU", | ||
| 500 | [FRr] = "rotation FR", | ||
| 501 | [FRm] = "mirrored FR", | ||
| 502 | [FDr] = "rotation FD", | ||
| 503 | [FDm] = "mirrored FD", | ||
| 504 | [FLr] = "rotation FL", | ||
| 505 | [FLm] = "mirrored FL", | ||
| 506 | [BUr] = "rotation BU", | ||
| 507 | [BUm] = "mirrored BU", | ||
| 508 | [BRr] = "rotation BR", | ||
| 509 | [BRm] = "mirrored BR", | ||
| 510 | [BDr] = "rotation BD", | ||
| 511 | [BDm] = "mirrored BD", | ||
| 512 | [BLr] = "rotation BL", | ||
| 513 | [BLm] = "mirrored BL", | ||
| 514 | }; | ||
| 515 | |||
| 516 | static uint8_t inverse_trans_table[48] = { | ||
| 517 | [UFr] = UFr, | ||
| 518 | [UFm] = UFm, | ||
| 519 | [ULr] = URr, | ||
| 520 | [ULm] = ULm, | ||
| 521 | [UBr] = UBr, | ||
| 522 | [UBm] = UBm, | ||
| 523 | [URr] = ULr, | ||
| 524 | [URm] = URm, | ||
| 525 | [DFr] = DFr, | ||
| 526 | [DFm] = DFm, | ||
| 527 | [DLr] = DLr, | ||
| 528 | [DLm] = DRm, | ||
| 529 | [DBr] = DBr, | ||
| 530 | [DBm] = DBm, | ||
| 531 | [DRr] = DRr, | ||
| 532 | [DRm] = DLm, | ||
| 533 | [RUr] = FRr, | ||
| 534 | [RUm] = FLm, | ||
| 535 | [RFr] = LFr, | ||
| 536 | [RFm] = RFm, | ||
| 537 | [RDr] = BLr, | ||
| 538 | [RDm] = BRm, | ||
| 539 | [RBr] = RBr, | ||
| 540 | [RBm] = LBm, | ||
| 541 | [LUr] = FLr, | ||
| 542 | [LUm] = FRm, | ||
| 543 | [LFr] = RFr, | ||
| 544 | [LFm] = LFm, | ||
| 545 | [LDr] = BRr, | ||
| 546 | [LDm] = BLm, | ||
| 547 | [LBr] = LBr, | ||
| 548 | [LBm] = RBm, | ||
| 549 | [FUr] = FUr, | ||
| 550 | [FUm] = FUm, | ||
| 551 | [FRr] = RUr, | ||
| 552 | [FRm] = LUm, | ||
| 553 | [FDr] = BUr, | ||
| 554 | [FDm] = BUm, | ||
| 555 | [FLr] = LUr, | ||
| 556 | [FLm] = RUm, | ||
| 557 | [BUr] = FDr, | ||
| 558 | [BUm] = FDm, | ||
| 559 | [BRr] = LDr, | ||
| 560 | [BRm] = RDm, | ||
| 561 | [BDr] = BDr, | ||
| 562 | [BDm] = BDm, | ||
| 563 | [BLr] = RDr, | ||
| 564 | [BLm] = LDm, | ||
| 565 | }; | ||
| 566 | |||
| 567 | /****************************************************************************** | ||
| 568 | Section: portable fast methods | ||
| 569 | |||
| 570 | This section contains performance-critical methods that do not use | ||
| 571 | advanced CPU instructions. They are used as an alternative to the ones | ||
| 572 | in the previous section(s) for unsupported architectures. | ||
| 573 | ******************************************************************************/ | ||
| 574 | |||
| 575 | typedef cube_t cube_fast_t; | ||
| 576 | |||
| 577 | _static_inline cube_fast_t fastcube( | ||
| 578 | uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, | ||
| 579 | uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, | ||
| 580 | uint8_t, uint8_t, uint8_t, uint8_t, uint8_t, | ||
| 581 | uint8_t, uint8_t, uint8_t, uint8_t, uint8_t | ||
| 582 | ); | ||
| 583 | _static cube_fast_t cubetofast(cube_t); | ||
| 584 | _static cube_t fasttocube(cube_fast_t); | ||
| 585 | _static_inline bool equal_fast(cube_fast_t, cube_fast_t); | ||
| 586 | _static_inline bool issolved_fast(cube_fast_t); | ||
| 587 | _static_inline cube_fast_t invertco_fast(cube_fast_t); | ||
| 588 | _static_inline cube_fast_t compose_fast(cube_fast_t, cube_fast_t); | ||
| 589 | |||
| 590 | _static_inline int64_t coord_fast_co(cube_fast_t); | ||
| 591 | _static_inline int64_t coord_fast_eo(cube_fast_t); | ||
| 592 | |||
| 593 | _static_inline cube_fast_t | ||
| 594 | fastcube( | ||
| 595 | uint8_t c_ufr, | ||
| 596 | uint8_t c_ubl, | ||
| 597 | uint8_t c_dfl, | ||
| 598 | uint8_t c_dbr, | ||
| 599 | uint8_t c_ufl, | ||
| 600 | uint8_t c_ubr, | ||
| 601 | uint8_t c_dfr, | ||
| 602 | uint8_t c_dbl, | ||
| 603 | |||
| 604 | uint8_t e_uf, | ||
| 605 | uint8_t e_ub, | ||
| 606 | uint8_t e_db, | ||
| 607 | uint8_t e_df, | ||
| 608 | uint8_t e_ur, | ||
| 609 | uint8_t e_ul, | ||
| 610 | uint8_t e_dl, | ||
| 611 | uint8_t e_dr, | ||
| 612 | uint8_t e_fr, | ||
| 613 | uint8_t e_fl, | ||
| 614 | uint8_t e_bl, | ||
| 615 | uint8_t e_br | ||
| 616 | ) | ||
| 617 | { | ||
| 618 | cube_fast_t cube = { | ||
| 619 | .corner = { | ||
| 620 | c_ufr, c_ubl, c_dfl, c_dbr, c_ufl, c_ubr, c_dfr, c_dbl | ||
| 621 | }, | ||
| 622 | .edge = { | ||
| 623 | e_uf, e_ub, e_db, e_df, e_ur, e_ul, | ||
| 624 | e_dl, e_dr, e_fr, e_fl, e_bl, e_br | ||
| 625 | } | ||
| 626 | }; | ||
| 627 | |||
| 628 | return cube; | ||
| 629 | } | ||
| 630 | |||
| 631 | _static cube_fast_t | ||
| 632 | cubetofast(cube_t cube) | ||
| 633 | { | ||
| 634 | cube_fast_t fast; | ||
| 635 | memcpy(&fast, &cube, sizeof(cube_fast_t)); | ||
| 636 | return fast; | ||
| 637 | } | ||
| 638 | |||
| 639 | _static cube_t | ||
| 640 | fasttocube(cube_fast_t fast) | ||
| 641 | { | ||
| 642 | cube_t cube; | ||
| 643 | memcpy(&cube, &fast, sizeof(cube_fast_t)); | ||
| 644 | return cube; | ||
| 645 | } | ||
| 646 | |||
| 647 | _static_inline bool | ||
| 648 | equal_fast(cube_fast_t c1, cube_fast_t c2) | ||
| 649 | { | ||
| 650 | uint8_t i; | ||
| 651 | bool ret; | ||
| 652 | |||
| 653 | ret = true; | ||
| 654 | for (i = 0; i < 8; i++) | ||
| 655 | ret = ret && c1.corner[i] == c2.corner[i]; | ||
| 656 | for (i = 0; i < 12; i++) | ||
| 657 | ret = ret && c1.edge[i] == c2.edge[i]; | ||
| 658 | |||
| 659 | return ret; | ||
| 660 | } | ||
| 661 | |||
| 662 | _static_inline bool | ||
| 663 | issolved_fast(cube_fast_t cube) | ||
| 664 | { | ||
| 665 | return equal_fast(cube, solved_fast); | ||
| 666 | } | ||
| 667 | |||
| 668 | _static_inline cube_fast_t | ||
| 669 | invertco_fast(cube_fast_t c) | ||
| 670 | { | ||
| 671 | uint8_t i, piece, orien; | ||
| 672 | cube_fast_t ret; | ||
| 673 | |||
| 674 | ret = c; | ||
| 675 | for (i = 0; i < 8; i++) { | ||
| 676 | piece = c.corner[i]; | ||
| 677 | orien = ((piece << 1) | (piece >> 1)) & _cobits2; | ||
| 678 | ret.corner[i] = (piece & _pbits) | orien; | ||
| 679 | } | ||
| 680 | |||
| 681 | return ret; | ||
| 682 | } | ||
| 683 | |||
| 684 | _static_inline cube_fast_t | ||
| 685 | compose_fast(cube_fast_t c1, cube_fast_t c2) | ||
| 686 | { | ||
| 687 | cube_fast_t ret; | ||
| 688 | uint8_t i, piece1, piece2, p, orien, aux, auy; | ||
| 689 | |||
| 690 | ret = zero_fast; | ||
| 691 | |||
| 692 | for (i = 0; i < 12; i++) { | ||
| 693 | piece2 = c2.edge[i]; | ||
| 694 | p = piece2 & _pbits; | ||
| 695 | piece1 = c1.edge[p]; | ||
| 696 | orien = (piece2 ^ piece1) & _eobit; | ||
| 697 | ret.edge[i] = (piece1 & _pbits) | orien; | ||
| 698 | } | ||
| 699 | |||
| 700 | for (i = 0; i < 8; i++) { | ||
| 701 | piece2 = c2.corner[i]; | ||
| 702 | p = piece2 & _pbits; | ||
| 703 | piece1 = c1.corner[p]; | ||
| 704 | aux = (piece2 & _cobits) + (piece1 & _cobits); | ||
| 705 | auy = (aux + _ctwist_cw) >> 2U; | ||
| 706 | orien = (aux + auy) & _cobits2; | ||
| 707 | ret.corner[i] = (piece1 & _pbits) | orien; | ||
| 708 | } | ||
| 709 | |||
| 710 | return ret; | ||
| 711 | } | ||
| 712 | |||
| 713 | _static_inline int64_t | ||
| 714 | coord_fast_co(cube_fast_t c) | ||
| 715 | { | ||
| 716 | int i, p; | ||
| 717 | int64_t ret; | ||
| 718 | |||
| 719 | for (ret = 0, i = 0, p = 1; i < 7; i++, p *= 3) | ||
| 720 | ret += p * (c.corner[i] >> _coshift); | ||
| 721 | |||
| 722 | return ret; | ||
| 723 | } | ||
| 724 | |||
| 725 | _static_inline int64_t | ||
| 726 | coord_fast_eo(cube_fast_t c) | ||
| 727 | { | ||
| 728 | int i, p; | ||
| 729 | int64_t ret; | ||
| 730 | |||
| 731 | for (ret = 0, i = 1, p = 1; i < 12; i++, p *= 2) | ||
| 732 | ret += p * (c.edge[i] >> _eoshift); | ||
| 733 | |||
| 734 | return ret; | ||
| 735 | } | ||
| 736 | |||
| 737 | /****************************************************************************** | ||
| 738 | Section: generic methods | ||
| 739 | |||
| 740 | This section contains generic functionality, including the public functions. | ||
| 741 | Some of these routines depend on the efficient functions implemented in the | ||
| 742 | previous sections, while some other operate directly on the cube. | ||
| 743 | ******************************************************************************/ | ||
| 744 | |||
| 745 | #define _move(M, c) compose_fast(c, _move_cube_ ## M) | ||
| 746 | #define _premove(M, c) compose_fast(_move_cube_ ## M, c) | ||
| 747 | #define _trans_rotation(T, c) \ | ||
| 748 | compose_fast(compose_fast(_trans_cube_ ## T, c), \ | ||
| 749 | _trans_cube_ ## T ## _inverse) | ||
| 750 | #define _trans_mirrored(T, c) \ | ||
| 751 | invertco_fast(compose_fast(compose_fast(_trans_cube_ ## T, c), \ | ||
| 752 | _trans_cube_ ## T ## _inverse)) | ||
| 753 | |||
| 754 | #define _foreach_move(_m, _c, _d, instruction) \ | ||
| 755 | for (_m = 0; _m < 18; _m++) { _d = move(_c, _m); instruction } | ||
| 756 | #define _foreach_trans(_t, _c, _d, instruction) \ | ||
| 757 | for (_t = 0; _t < 48; _t++) { _d = transform(_c, _t); instruction } | ||
| 758 | |||
| 759 | cube_t solvedcube(void); | ||
| 760 | bool isconsistent(cube_t); | ||
| 761 | bool issolvable(cube_t); | ||
| 762 | bool issolved(cube_t cube); | ||
| 763 | bool equal(cube_t, cube_t); | ||
| 764 | bool iserror(cube_t); | ||
| 765 | cube_t compose(cube_t, cube_t); | ||
| 766 | cube_t inverse(cube_t); | ||
| 767 | cube_t applymoves(cube_t, char *); | ||
| 768 | cube_t applytrans(cube_t, char *); | ||
| 769 | cube_t readcube(char *, char *); | ||
| 770 | void writecube(char *, cube_t, char *); | ||
| 771 | |||
| 772 | _static int permsign(uint8_t *, int); | ||
| 773 | _static uint8_t readco(char *); | ||
| 774 | _static uint8_t readcp(char *); | ||
| 775 | _static uint8_t readeo(char *); | ||
| 776 | _static uint8_t readep(char *); | ||
| 777 | _static cube_t readcube_H48(char *); | ||
| 778 | _static uint8_t readpiece_LST(char **); | ||
| 779 | _static cube_t readcube_LST(char *); | ||
| 780 | _static int writepiece_LST(uint8_t, char *); | ||
| 781 | _static void writecube_H48(cube_t, char *); | ||
| 782 | _static void writecube_LST(cube_t, char *); | ||
| 783 | _static uint8_t readmove(char); | ||
| 784 | _static uint8_t readmodifier(char); | ||
| 785 | _static uint8_t readtrans(char *); | ||
| 786 | _static int writemoves(uint8_t *, int, char *); | ||
| 787 | _static void writetrans(uint8_t, char *); | ||
| 788 | _static cube_fast_t move(cube_fast_t, uint8_t); | ||
| 789 | _static cube_fast_t transform(cube_fast_t, uint8_t); | ||
| 790 | |||
| 791 | cube_t | ||
| 792 | solvedcube(void) | ||
| 793 | { | ||
| 794 | return solved; | ||
| 795 | } | ||
| 796 | |||
| 797 | bool | ||
| 798 | isconsistent(cube_t cube) | ||
| 799 | { | ||
| 800 | uint8_t i, p, e, piece; | ||
| 801 | bool found[12]; | ||
| 802 | |||
| 803 | for (i = 0; i < 12; i++) | ||
| 804 | found[i] = false; | ||
| 805 | for (i = 0; i < 12; i++) { | ||
| 806 | piece = cube.edge[i]; | ||
| 807 | p = piece & _pbits; | ||
| 808 | e = piece & _eobit; | ||
| 809 | if (p >= 12) | ||
| 810 | goto inconsistent_ep; | ||
| 811 | if (e != 0 && e != _eobit) | ||
| 812 | goto inconsistent_eo; | ||
| 813 | found[p] = true; | ||
| 814 | } | ||
| 815 | for (i = 0; i < 12; i++) | ||
| 816 | if (!found[i]) | ||
| 817 | goto inconsistent_ep; | ||
| 818 | |||
| 819 | for (i = 0; i < 8; i++) | ||
| 820 | found[i] = false; | ||
| 821 | for (i = 0; i < 8; i++) { | ||
| 822 | piece = cube.corner[i]; | ||
| 823 | p = piece & _pbits; | ||
| 824 | e = piece & _cobits; | ||
| 825 | if (p >= 8) | ||
| 826 | goto inconsistent_cp; | ||
| 827 | if (e != 0 && e != _ctwist_cw && e != _ctwist_ccw) | ||
| 828 | goto inconsistent_co; | ||
| 829 | found[p] = true; | ||
| 830 | } | ||
| 831 | for (i = 0; i < 8; i++) | ||
| 832 | if (!found[i]) | ||
| 833 | goto inconsistent_co; | ||
| 834 | |||
| 835 | return true; | ||
| 836 | |||
| 837 | inconsistent_ep: | ||
| 838 | DBG_LOG("Inconsistent EP\n"); | ||
| 839 | return false; | ||
| 840 | inconsistent_cp: | ||
| 841 | DBG_LOG("Inconsistent CP\n"); | ||
| 842 | return false; | ||
| 843 | inconsistent_eo: | ||
| 844 | DBG_LOG("Inconsistent EO\n"); | ||
| 845 | return false; | ||
| 846 | inconsistent_co: | ||
| 847 | DBG_LOG("Inconsistent CO\n"); | ||
| 848 | return false; | ||
| 849 | } | ||
| 850 | |||
| 851 | bool | ||
| 852 | issolvable(cube_t cube) | ||
| 853 | { | ||
| 854 | uint8_t i, eo, co, piece, edges[12], corners[8]; | ||
| 855 | |||
| 856 | DBG_ASSERT(isconsistent(cube), false, | ||
| 857 | "issolvable: cube is inconsistent\n"); | ||
| 858 | |||
| 859 | for (i = 0; i < 12; i++) | ||
| 860 | edges[i] = cube.edge[i] & _pbits; | ||
| 861 | for (i = 0; i < 8; i++) | ||
| 862 | corners[i] = cube.corner[i] & _pbits; | ||
| 863 | |||
| 864 | if (permsign(edges, 12) != permsign(corners, 8)) | ||
| 865 | goto issolvable_parity; | ||
| 866 | |||
| 867 | eo = 0; | ||
| 868 | for (i = 0; i < 12; i++) { | ||
| 869 | piece = cube.edge[i]; | ||
| 870 | eo += (piece & _eobit) >> _eoshift; | ||
| 871 | } | ||
| 872 | if (eo % 2 != 0) | ||
| 873 | goto issolvable_eo; | ||
| 874 | |||
| 875 | co = 0; | ||
| 876 | for (i = 0; i < 8; i++) { | ||
| 877 | piece = cube.corner[i]; | ||
| 878 | co += (piece & _cobits) >> _coshift; | ||
| 879 | } | ||
| 880 | if (co % 3 != 0) | ||
| 881 | goto issolvable_co; | ||
| 882 | |||
| 883 | return true; | ||
| 884 | |||
| 885 | issolvable_parity: | ||
| 886 | DBG_LOG("EP and CP parities are different\n"); | ||
| 887 | return false; | ||
| 888 | issolvable_eo: | ||
| 889 | DBG_LOG("Odd number of flipped edges\n"); | ||
| 890 | return false; | ||
| 891 | issolvable_co: | ||
| 892 | DBG_LOG("Sum of corner orientation is not multiple of 3\n"); | ||
| 893 | return false; | ||
| 894 | } | ||
| 895 | |||
| 896 | bool | ||
| 897 | issolved(cube_t cube) | ||
| 898 | { | ||
| 899 | return equal(cube, solved); | ||
| 900 | } | ||
| 901 | |||
| 902 | bool | ||
| 903 | equal(cube_t c1, cube_t c2) | ||
| 904 | { | ||
| 905 | int i; | ||
| 906 | bool ret; | ||
| 907 | |||
| 908 | ret = true; | ||
| 909 | for (i = 0; i < 8; i++) | ||
| 910 | ret = ret && c1.corner[i] == c2.corner[i]; | ||
| 911 | for (i = 0; i < 12; i++) | ||
| 912 | ret = ret && c1.edge[i] == c2.edge[i]; | ||
| 913 | |||
| 914 | return ret; | ||
| 915 | } | ||
| 916 | |||
| 917 | bool | ||
| 918 | iserror(cube_t cube) | ||
| 919 | { | ||
| 920 | return equal(cube, zero); | ||
| 921 | } | ||
| 922 | |||
| 923 | cube_t | ||
| 924 | compose(cube_t c1, cube_t c2) | ||
| 925 | { | ||
| 926 | DBG_ASSERT(isconsistent(c1) && isconsistent(c2), | ||
| 927 | zero, "compose error: inconsistent cube\n") | ||
| 928 | |||
| 929 | return fasttocube(compose_fast(cubetofast(c1), cubetofast(c2))); | ||
| 930 | } | ||
| 931 | |||
| 932 | cube_t | ||
| 933 | inverse(cube_t cube) | ||
| 934 | { | ||
| 935 | cube_t ret; | ||
| 936 | uint8_t i, piece, orien; | ||
| 937 | |||
| 938 | DBG_ASSERT(isconsistent(cube), zero, | ||
| 939 | "inverse error: inconsistent cube\n"); | ||
| 940 | |||
| 941 | ret = zero; | ||
| 942 | |||
| 943 | for (i = 0; i < 12; i++) { | ||
| 944 | piece = cube.edge[i]; | ||
| 945 | orien = piece & _eobit; | ||
| 946 | ret.edge[piece & _pbits] = i | orien; | ||
| 947 | } | ||
| 948 | |||
| 949 | for (i = 0; i < 8; i++) { | ||
| 950 | piece = cube.corner[i]; | ||
| 951 | orien = ((piece << 1) | (piece >> 1)) & _cobits2; | ||
| 952 | ret.corner[piece & _pbits] = i | orien; | ||
| 953 | } | ||
| 954 | |||
| 955 | return ret; | ||
| 956 | } | ||
| 957 | |||
| 958 | cube_t | ||
| 959 | applymoves(cube_t cube, char *buf) | ||
| 960 | { | ||
| 961 | cube_fast_t fast; | ||
| 962 | uint8_t r, m; | ||
| 963 | char *b; | ||
| 964 | |||
| 965 | DBG_ASSERT(isconsistent(cube), zero, | ||
| 966 | "move error: inconsistent cube\n"); | ||
| 967 | |||
| 968 | fast = cubetofast(cube); | ||
| 969 | |||
| 970 | for (b = buf; *b != '\0'; b++) { | ||
| 971 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 972 | b++; | ||
| 973 | if (*b == '\0') | ||
| 974 | goto applymoves_finish; | ||
| 975 | if ((r = readmove(*b)) == _error) | ||
| 976 | goto applymoves_error; | ||
| 977 | if ((m = readmodifier(*(b+1))) != 0) | ||
| 978 | b++; | ||
| 979 | fast = move(fast, r + m); | ||
| 980 | } | ||
| 981 | |||
| 982 | applymoves_finish: | ||
| 983 | return fasttocube(fast); | ||
| 984 | |||
| 985 | applymoves_error: | ||
| 986 | DBG_LOG("applymoves error\n"); | ||
| 987 | return zero; | ||
| 988 | } | ||
| 989 | |||
| 990 | cube_t | ||
| 991 | applytrans(cube_t cube, char *buf) | ||
| 992 | { | ||
| 993 | cube_fast_t fast; | ||
| 994 | uint8_t t; | ||
| 995 | |||
| 996 | DBG_ASSERT(isconsistent(cube), zero, | ||
| 997 | "transformation error: inconsistent cube\n"); | ||
| 998 | |||
| 999 | t = readtrans(buf); | ||
| 1000 | fast = cubetofast(cube); | ||
| 1001 | fast = transform(fast, t); | ||
| 1002 | |||
| 1003 | return fasttocube(fast); | ||
| 1004 | } | ||
| 1005 | |||
| 1006 | cube_t | ||
| 1007 | readcube(char *format, char *buf) | ||
| 1008 | { | ||
| 1009 | cube_t cube; | ||
| 1010 | |||
| 1011 | if (!strcmp(format, "H48")) { | ||
| 1012 | cube = readcube_H48(buf); | ||
| 1013 | } else if (!strcmp(format, "LST")) { | ||
| 1014 | cube = readcube_LST(buf); | ||
| 1015 | } else { | ||
| 1016 | DBG_LOG("Cannot read cube in the given format\n"); | ||
| 1017 | cube = zero; | ||
| 1018 | } | ||
| 1019 | |||
| 1020 | return cube; | ||
| 1021 | } | ||
| 1022 | |||
| 1023 | void | ||
| 1024 | writecube(char *format, cube_t cube, char *buf) | ||
| 1025 | { | ||
| 1026 | char *errormsg; | ||
| 1027 | size_t len; | ||
| 1028 | |||
| 1029 | if (!isconsistent(cube)) { | ||
| 1030 | errormsg = "ERROR: cannot write inconsistent cube"; | ||
| 1031 | goto writecube_error; | ||
| 1032 | } | ||
| 1033 | |||
| 1034 | if (!strcmp(format, "H48")) { | ||
| 1035 | writecube_H48(cube, buf); | ||
| 1036 | } else if (!strcmp(format, "LST")) { | ||
| 1037 | writecube_LST(cube, buf); | ||
| 1038 | } else { | ||
| 1039 | errormsg = "ERROR: cannot write cube in the given format"; | ||
| 1040 | goto writecube_error; | ||
| 1041 | } | ||
| 1042 | |||
| 1043 | return; | ||
| 1044 | |||
| 1045 | writecube_error: | ||
| 1046 | DBG_LOG("writecube error, see stdout for details\n"); | ||
| 1047 | len = strlen(errormsg); | ||
| 1048 | memcpy(buf, errormsg, len); | ||
| 1049 | buf[len] = '\n'; | ||
| 1050 | buf[len+1] = '\0'; | ||
| 1051 | } | ||
| 1052 | |||
| 1053 | _static int | ||
| 1054 | permsign(uint8_t *a, int n) | ||
| 1055 | { | ||
| 1056 | int i, j; | ||
| 1057 | uint8_t ret = 0; | ||
| 1058 | |||
| 1059 | for (i = 0; i < n; i++) | ||
| 1060 | for (j = i+1; j < n; j++) | ||
| 1061 | ret += a[i] > a[j] ? 1 : 0; | ||
| 1062 | |||
| 1063 | return ret % 2; | ||
| 1064 | } | ||
| 1065 | |||
| 1066 | _static uint8_t | ||
| 1067 | readco(char *str) | ||
| 1068 | { | ||
| 1069 | if (*str == '0') | ||
| 1070 | return 0; | ||
| 1071 | if (*str == '1') | ||
| 1072 | return _ctwist_cw; | ||
| 1073 | if (*str == '2') | ||
| 1074 | return _ctwist_ccw; | ||
| 1075 | |||
| 1076 | DBG_LOG("Error reading CO\n"); | ||
| 1077 | return _error; | ||
| 1078 | } | ||
| 1079 | |||
| 1080 | _static uint8_t | ||
| 1081 | readcp(char *str) | ||
| 1082 | { | ||
| 1083 | uint8_t c; | ||
| 1084 | |||
| 1085 | for (c = 0; c < 8; c++) | ||
| 1086 | if (!strncmp(str, cornerstr[c], 3) || | ||
| 1087 | !strncmp(str, cornerstralt[c], 3)) | ||
| 1088 | return c; | ||
| 1089 | |||
| 1090 | DBG_LOG("Error reading CP\n"); | ||
| 1091 | return _error; | ||
| 1092 | } | ||
| 1093 | |||
| 1094 | _static uint8_t | ||
| 1095 | readeo(char *str) | ||
| 1096 | { | ||
| 1097 | if (*str == '0') | ||
| 1098 | return 0; | ||
| 1099 | if (*str == '1') | ||
| 1100 | return _eflip; | ||
| 1101 | |||
| 1102 | DBG_LOG("Error reading EO\n"); | ||
| 1103 | return _error; | ||
| 1104 | } | ||
| 1105 | |||
| 1106 | _static uint8_t | ||
| 1107 | readep(char *str) | ||
| 1108 | { | ||
| 1109 | uint8_t e; | ||
| 1110 | |||
| 1111 | for (e = 0; e < 12; e++) | ||
| 1112 | if (!strncmp(str, edgestr[e], 2)) | ||
| 1113 | return e; | ||
| 1114 | |||
| 1115 | DBG_LOG("Error reading EP\n"); | ||
| 1116 | return _error; | ||
| 1117 | } | ||
| 1118 | |||
| 1119 | _static cube_t | ||
| 1120 | readcube_H48(char *buf) | ||
| 1121 | { | ||
| 1122 | int i; | ||
| 1123 | uint8_t piece, orient; | ||
| 1124 | cube_t ret = {0}; | ||
| 1125 | char *b; | ||
| 1126 | |||
| 1127 | b = buf; | ||
| 1128 | |||
| 1129 | for (i = 0; i < 12; i++) { | ||
| 1130 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 1131 | b++; | ||
| 1132 | if ((piece = readep(b)) == _error) | ||
| 1133 | return zero; | ||
| 1134 | b += 2; | ||
| 1135 | if ((orient = readeo(b)) == _error) | ||
| 1136 | return zero; | ||
| 1137 | b++; | ||
| 1138 | ret.edge[i] = piece | orient; | ||
| 1139 | } | ||
| 1140 | for (i = 0; i < 8; i++) { | ||
| 1141 | while (*b == ' ' || *b == '\t' || *b == '\n') | ||
| 1142 | b++; | ||
| 1143 | if ((piece = readcp(b)) == _error) | ||
| 1144 | return zero; | ||
| 1145 | b += 3; | ||
| 1146 | if ((orient = readco(b)) == _error) | ||
| 1147 | return zero; | ||
| 1148 | b++; | ||
| 1149 | ret.corner[i] = piece | orient; | ||
| 1150 | } | ||
| 1151 | |||
| 1152 | return ret; | ||
| 1153 | } | ||
| 1154 | |||
| 1155 | _static uint8_t | ||
| 1156 | readpiece_LST(char **b) | ||
| 1157 | { | ||
| 1158 | uint8_t ret; | ||
| 1159 | bool read; | ||
| 1160 | |||
| 1161 | while (**b == ',' || **b == ' ' || **b == '\t' || **b == '\n') | ||
| 1162 | (*b)++; | ||
| 1163 | |||
| 1164 | for (ret = 0, read = false; **b >= '0' && **b <= '9'; (*b)++) { | ||
| 1165 | read = true; | ||
| 1166 | ret = ret * 10 + (**b) - '0'; | ||
| 1167 | } | ||
| 1168 | |||
| 1169 | return read ? ret : _error; | ||
| 1170 | } | ||
| 1171 | |||
| 1172 | _static cube_t | ||
| 1173 | readcube_LST(char *buf) | ||
| 1174 | { | ||
| 1175 | int i; | ||
| 1176 | cube_t ret = {0}; | ||
| 1177 | |||
| 1178 | for (i = 0; i < 8; i++) | ||
| 1179 | ret.corner[i] = readpiece_LST(&buf); | ||
| 1180 | |||
| 1181 | for (i = 0; i < 12; i++) | ||
| 1182 | ret.edge[i] = readpiece_LST(&buf); | ||
| 1183 | |||
| 1184 | return ret; | ||
| 1185 | } | ||
| 1186 | |||
| 1187 | _static int | ||
| 1188 | writepiece_LST(uint8_t piece, char *buf) | ||
| 1189 | { | ||
| 1190 | char digits[3]; | ||
| 1191 | int i, len = 0; | ||
| 1192 | |||
| 1193 | while (piece != 0) { | ||
| 1194 | digits[len++] = (piece % 10) + '0'; | ||
| 1195 | piece /= 10; | ||
| 1196 | } | ||
| 1197 | |||
| 1198 | if (len == 0) | ||
| 1199 | digits[len++] = '0'; | ||
| 1200 | |||
| 1201 | for (i = 0; i < len; i++) | ||
| 1202 | buf[i] = digits[len-i-1]; | ||
| 1203 | |||
| 1204 | buf[len] = ','; | ||
| 1205 | buf[len+1] = ' '; | ||
| 1206 | |||
| 1207 | return len+2; | ||
| 1208 | } | ||
| 1209 | |||
| 1210 | _static void | ||
| 1211 | writecube_H48(cube_t cube, char *buf) | ||
| 1212 | { | ||
| 1213 | uint8_t piece, perm, orient; | ||
| 1214 | int i; | ||
| 1215 | |||
| 1216 | for (i = 0; i < 12; i++) { | ||
| 1217 | piece = cube.edge[i]; | ||
| 1218 | perm = piece & _pbits; | ||
| 1219 | orient = (piece & _eobit) >> _eoshift; | ||
| 1220 | buf[4*i ] = edgestr[perm][0]; | ||
| 1221 | buf[4*i + 1] = edgestr[perm][1]; | ||
| 1222 | buf[4*i + 2] = orient + '0'; | ||
| 1223 | buf[4*i + 3] = ' '; | ||
| 1224 | } | ||
| 1225 | for (i = 0; i < 8; i++) { | ||
| 1226 | piece = cube.corner[i]; | ||
| 1227 | perm = piece & _pbits; | ||
| 1228 | orient = (piece & _cobits) >> _coshift; | ||
| 1229 | buf[48 + 5*i ] = cornerstr[perm][0]; | ||
| 1230 | buf[48 + 5*i + 1] = cornerstr[perm][1]; | ||
| 1231 | buf[48 + 5*i + 2] = cornerstr[perm][2]; | ||
| 1232 | buf[48 + 5*i + 3] = orient + '0'; | ||
| 1233 | buf[48 + 5*i + 4] = ' '; | ||
| 1234 | } | ||
| 1235 | |||
| 1236 | buf[48+39] = '\0'; | ||
| 1237 | } | ||
| 1238 | |||
| 1239 | _static void | ||
| 1240 | writecube_LST(cube_t cube, char *buf) | ||
| 1241 | { | ||
| 1242 | int i, ptr; | ||
| 1243 | uint8_t piece; | ||
| 1244 | |||
| 1245 | ptr = 0; | ||
| 1246 | |||
| 1247 | for (i = 0; i < 8; i++) { | ||
| 1248 | piece = cube.corner[i]; | ||
| 1249 | ptr += writepiece_LST(piece, buf + ptr); | ||
| 1250 | } | ||
| 1251 | |||
| 1252 | for (i = 0; i < 12; i++) { | ||
| 1253 | piece = cube.edge[i]; | ||
| 1254 | ptr += writepiece_LST(piece, buf + ptr); | ||
| 1255 | } | ||
| 1256 | |||
| 1257 | *(buf+ptr-2) = 0; | ||
| 1258 | } | ||
| 1259 | |||
| 1260 | _static uint8_t | ||
| 1261 | readmove(char c) | ||
| 1262 | { | ||
| 1263 | switch (c) { | ||
| 1264 | case 'U': | ||
| 1265 | return U; | ||
| 1266 | case 'D': | ||
| 1267 | return D; | ||
| 1268 | case 'R': | ||
| 1269 | return R; | ||
| 1270 | case 'L': | ||
| 1271 | return L; | ||
| 1272 | case 'F': | ||
| 1273 | return F; | ||
| 1274 | case 'B': | ||
| 1275 | return B; | ||
| 1276 | default: | ||
| 1277 | return _error; | ||
| 1278 | } | ||
| 1279 | } | ||
| 1280 | |||
| 1281 | _static uint8_t | ||
| 1282 | readmodifier(char c) | ||
| 1283 | { | ||
| 1284 | switch (c) { | ||
| 1285 | case '1': /* Fallthrough */ | ||
| 1286 | case '2': /* Fallthrough */ | ||
| 1287 | case '3': | ||
| 1288 | return c - '0' - 1; | ||
| 1289 | case '\'': | ||
| 1290 | return 2; | ||
| 1291 | default: | ||
| 1292 | return 0; | ||
| 1293 | } | ||
| 1294 | } | ||
| 1295 | |||
| 1296 | _static uint8_t | ||
| 1297 | readtrans(char *buf) | ||
| 1298 | { | ||
| 1299 | uint8_t t; | ||
| 1300 | |||
| 1301 | for (t = 0; t < 48; t++) | ||
| 1302 | if (!strncmp(buf, transstr[t], 11)) | ||
| 1303 | return t; | ||
| 1304 | |||
| 1305 | DBG_LOG("readtrans error\n"); | ||
| 1306 | return _error; | ||
| 1307 | } | ||
| 1308 | |||
| 1309 | _static int | ||
| 1310 | writemoves(uint8_t *m, int n, char *buf) | ||
| 1311 | { | ||
| 1312 | int i; | ||
| 1313 | size_t len; | ||
| 1314 | char *b, *s; | ||
| 1315 | |||
| 1316 | for (i = 0, b = buf; i < n; i++, b++) { | ||
| 1317 | s = movestr[m[i]]; | ||
| 1318 | len = strlen(s); | ||
| 1319 | memcpy(b, s, len); | ||
| 1320 | b += len; | ||
| 1321 | *b = ' '; | ||
| 1322 | } | ||
| 1323 | |||
| 1324 | if (b != buf) | ||
| 1325 | b--; /* Remove last space */ | ||
| 1326 | *b = '\0'; | ||
| 1327 | |||
| 1328 | return b - buf; | ||
| 1329 | } | ||
| 1330 | |||
| 1331 | _static void | ||
| 1332 | writetrans(uint8_t t, char *buf) | ||
| 1333 | { | ||
| 1334 | if (t >= 48) | ||
| 1335 | memcpy(buf, "error trans", 11); | ||
| 1336 | else | ||
| 1337 | memcpy(buf, transstr[t], 11); | ||
| 1338 | buf[11] = '\0'; | ||
| 1339 | } | ||
| 1340 | |||
| 1341 | _static cube_fast_t | ||
| 1342 | move(cube_fast_t c, uint8_t m) | ||
| 1343 | { | ||
| 1344 | switch (m) { | ||
| 1345 | case U: | ||
| 1346 | return _move(U, c); | ||
| 1347 | case U2: | ||
| 1348 | return _move(U2, c); | ||
| 1349 | case U3: | ||
| 1350 | return _move(U3, c); | ||
| 1351 | case D: | ||
| 1352 | return _move(D, c); | ||
| 1353 | case D2: | ||
| 1354 | return _move(D2, c); | ||
| 1355 | case D3: | ||
| 1356 | return _move(D3, c); | ||
| 1357 | case R: | ||
| 1358 | return _move(R, c); | ||
| 1359 | case R2: | ||
| 1360 | return _move(R2, c); | ||
| 1361 | case R3: | ||
| 1362 | return _move(R3, c); | ||
| 1363 | case L: | ||
| 1364 | return _move(L, c); | ||
| 1365 | case L2: | ||
| 1366 | return _move(L2, c); | ||
| 1367 | case L3: | ||
| 1368 | return _move(L3, c); | ||
| 1369 | case F: | ||
| 1370 | return _move(F, c); | ||
| 1371 | case F2: | ||
| 1372 | return _move(F2, c); | ||
| 1373 | case F3: | ||
| 1374 | return _move(F3, c); | ||
| 1375 | case B: | ||
| 1376 | return _move(B, c); | ||
| 1377 | case B2: | ||
| 1378 | return _move(B2, c); | ||
| 1379 | case B3: | ||
| 1380 | return _move(B3, c); | ||
| 1381 | default: | ||
| 1382 | DBG_LOG("move error, unknown move\n"); | ||
| 1383 | return zero_fast; | ||
| 1384 | } | ||
| 1385 | } | ||
| 1386 | |||
| 1387 | _static cube_fast_t | ||
| 1388 | transform(cube_fast_t c, uint8_t t) | ||
| 1389 | { | ||
| 1390 | switch (t) { | ||
| 1391 | case UFr: | ||
| 1392 | return _trans_rotation(UFr, c); | ||
| 1393 | case ULr: | ||
| 1394 | return _trans_rotation(ULr, c); | ||
| 1395 | case UBr: | ||
| 1396 | return _trans_rotation(UBr, c); | ||
| 1397 | case URr: | ||
| 1398 | return _trans_rotation(URr, c); | ||
| 1399 | case DFr: | ||
| 1400 | return _trans_rotation(DFr, c); | ||
| 1401 | case DLr: | ||
| 1402 | return _trans_rotation(DLr, c); | ||
| 1403 | case DBr: | ||
| 1404 | return _trans_rotation(DBr, c); | ||
| 1405 | case DRr: | ||
| 1406 | return _trans_rotation(DRr, c); | ||
| 1407 | case RUr: | ||
| 1408 | return _trans_rotation(RUr, c); | ||
| 1409 | case RFr: | ||
| 1410 | return _trans_rotation(RFr, c); | ||
| 1411 | case RDr: | ||
| 1412 | return _trans_rotation(RDr, c); | ||
| 1413 | case RBr: | ||
| 1414 | return _trans_rotation(RBr, c); | ||
| 1415 | case LUr: | ||
| 1416 | return _trans_rotation(LUr, c); | ||
| 1417 | case LFr: | ||
| 1418 | return _trans_rotation(LFr, c); | ||
| 1419 | case LDr: | ||
| 1420 | return _trans_rotation(LDr, c); | ||
| 1421 | case LBr: | ||
| 1422 | return _trans_rotation(LBr, c); | ||
| 1423 | case FUr: | ||
| 1424 | return _trans_rotation(FUr, c); | ||
| 1425 | case FRr: | ||
| 1426 | return _trans_rotation(FRr, c); | ||
| 1427 | case FDr: | ||
| 1428 | return _trans_rotation(FDr, c); | ||
| 1429 | case FLr: | ||
| 1430 | return _trans_rotation(FLr, c); | ||
| 1431 | case BUr: | ||
| 1432 | return _trans_rotation(BUr, c); | ||
| 1433 | case BRr: | ||
| 1434 | return _trans_rotation(BRr, c); | ||
| 1435 | case BDr: | ||
| 1436 | return _trans_rotation(BDr, c); | ||
| 1437 | case BLr: | ||
| 1438 | return _trans_rotation(BLr, c); | ||
| 1439 | case UFm: | ||
| 1440 | return _trans_mirrored(UFm, c); | ||
| 1441 | case ULm: | ||
| 1442 | return _trans_mirrored(ULm, c); | ||
| 1443 | case UBm: | ||
| 1444 | return _trans_mirrored(UBm, c); | ||
| 1445 | case URm: | ||
| 1446 | return _trans_mirrored(URm, c); | ||
| 1447 | case DFm: | ||
| 1448 | return _trans_mirrored(DFm, c); | ||
| 1449 | case DLm: | ||
| 1450 | return _trans_mirrored(DLm, c); | ||
| 1451 | case DBm: | ||
| 1452 | return _trans_mirrored(DBm, c); | ||
| 1453 | case DRm: | ||
| 1454 | return _trans_mirrored(DRm, c); | ||
| 1455 | case RUm: | ||
| 1456 | return _trans_mirrored(RUm, c); | ||
| 1457 | case RFm: | ||
| 1458 | return _trans_mirrored(RFm, c); | ||
| 1459 | case RDm: | ||
| 1460 | return _trans_mirrored(RDm, c); | ||
| 1461 | case RBm: | ||
| 1462 | return _trans_mirrored(RBm, c); | ||
| 1463 | case LUm: | ||
| 1464 | return _trans_mirrored(LUm, c); | ||
| 1465 | case LFm: | ||
| 1466 | return _trans_mirrored(LFm, c); | ||
| 1467 | case LDm: | ||
| 1468 | return _trans_mirrored(LDm, c); | ||
| 1469 | case LBm: | ||
| 1470 | return _trans_mirrored(LBm, c); | ||
| 1471 | case FUm: | ||
| 1472 | return _trans_mirrored(FUm, c); | ||
| 1473 | case FRm: | ||
| 1474 | return _trans_mirrored(FRm, c); | ||
| 1475 | case FDm: | ||
| 1476 | return _trans_mirrored(FDm, c); | ||
| 1477 | case FLm: | ||
| 1478 | return _trans_mirrored(FLm, c); | ||
| 1479 | case BUm: | ||
| 1480 | return _trans_mirrored(BUm, c); | ||
| 1481 | case BRm: | ||
| 1482 | return _trans_mirrored(BRm, c); | ||
| 1483 | case BDm: | ||
| 1484 | return _trans_mirrored(BDm, c); | ||
| 1485 | case BLm: | ||
| 1486 | return _trans_mirrored(BLm, c); | ||
| 1487 | default: | ||
| 1488 | DBG_LOG("transform error, unknown transformation\n"); | ||
| 1489 | return zero_fast; | ||
| 1490 | } | ||
| 1491 | } | ||
| 1492 | |||
| 1493 | /****************************************************************************** | ||
| 1494 | Section: moves, move sequences and transformations | ||
| 1495 | |||
| 1496 | This section contains methods to work with moves and arrays of moves. They | ||
| 1497 | do not rely on the cube structure. | ||
| 1498 | ******************************************************************************/ | ||
| 1499 | |||
| 1500 | _static_inline uint8_t inverse_trans(uint8_t); | ||
| 1501 | _static_inline uint8_t movebase(uint8_t); | ||
| 1502 | _static_inline uint8_t moveaxis(uint8_t); | ||
| 1503 | |||
| 1504 | _static_inline uint8_t | ||
| 1505 | inverse_trans(uint8_t t) | ||
| 1506 | { | ||
| 1507 | return inverse_trans_table[t]; | ||
| 1508 | } | ||
| 1509 | |||
| 1510 | _static_inline uint8_t | ||
| 1511 | movebase(uint8_t move) | ||
| 1512 | { | ||
| 1513 | return move / 3; | ||
| 1514 | } | ||
| 1515 | |||
| 1516 | _static_inline uint8_t | ||
| 1517 | moveaxis(uint8_t move) | ||
| 1518 | { | ||
| 1519 | return move / 6; | ||
| 1520 | } | ||
