cubing / twsearch

🔍 Twizzle Search — a program to find algs and scrambles for twisty puzzles
GNU General Public License v3.0
24 stars 8 forks source link

Pack `orientation_mod` more densely. #25

Closed lgarron closed 10 months ago

lgarron commented 10 months ago

The valid values for orientation_mod and orientation for a given piece are:

We currently pack these into the higher and lower nibbles of a single bit: https://github.com/cubing/twsearch/blob/a98834f47fb458dc5a2598578b75cb5bdef17c1f/examples/cpp_port/packed/packed_kstate.rs#L113

This limits us to num_orientations ≤ 16.

But if we use a more efficient encoding, we can fit num_orientations up to 107 into a single byte (as well as some composite values up to 221 and primes up to 251):

Pairs[n_] := 
 Flatten[
  Table[{orientation, 
    If[orientationMod == n, 0, orientationMod]}, {orientationMod, 
    Divisors[n]}, {orientation, 0, orientationMod - 1}], 1]; n = 1;
While[(Pairs[n] // Length) < 257, n++];
n

(* Output *)
108

If we use a lookup table (https://github.com/cubing/twsearch/issues/24) there won't even be a performance hit for most calculations.

For the curious, all possible num_orientation values that can fit in one byte:

Select[Range[1, 256], Length[Pairs[#]] < 257 &]

(* Output *)
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103,
104, 105, 106, 107, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118,
119, 121, 122, 123, 124, 125, 127, 128, 129, 130, 131, 133, 134, 135,
137, 139, 141, 142, 143, 145, 146, 147, 149, 151, 153, 155, 157, 158,
159, 161, 163, 166, 167, 169, 173, 175, 177, 179, 181, 183, 185, 187,
191, 193, 197, 199, 203, 205, 209, 211, 217, 221, 223, 227, 229, 233,
239, 241, 251}
lgarron commented 10 months ago

This was done in 9f77f299a9f671c53b62828ac16a64b258dfeb3a behind the orientation_packer feature flag. It does not seem to have a significant impact on performance, but it does allow num_orientations significantly over 16.