nissy-core

The "engine" of nissy, including the H48 optimal solver.
git clone https://git.tronto.net/nissy-core
Download | Log | Files | Refs | README | LICENSE

common.h (3239B)


      1 #define EOSHIFT     UINT8_C(4)
      2 #define COSHIFT     UINT8_C(5)
      3 
      4 #define PBITS       UINT8_C(0xF)
      5 #define ESEPBIT_1   UINT8_C(0x4)
      6 #define ESEPBIT_2   UINT8_C(0x8)
      7 #define CSEPBIT     UINT8_C(0x4)
      8 #define EOBIT       UINT8_C(0x10)
      9 #define COBITS      UINT8_C(0xF0)
     10 #define COBITS_2    UINT8_C(0x60)
     11 #define CTWIST_CW   UINT8_C(0x20)
     12 #define CTWIST_CCW  UINT8_C(0x40)
     13 #define EFLIP       UINT8_C(0x10)
     14 
     15 STATIC_INLINE int popcount_u32(uint32_t);
     16 
     17 STATIC void pieces(cube_t [static 1], uint8_t [static 8], uint8_t [static 12]);
     18 STATIC_INLINE bool equal(cube_t, cube_t);
     19 STATIC_INLINE cube_t invertco(cube_t);
     20 STATIC_INLINE cube_t compose_edges(cube_t, cube_t);
     21 STATIC_INLINE cube_t compose_corners(cube_t, cube_t);
     22 STATIC_INLINE cube_t compose(cube_t, cube_t);
     23 STATIC_INLINE cube_t inverse(cube_t);
     24 
     25 STATIC_INLINE uint64_t coord_co(cube_t);
     26 STATIC_INLINE cube_t invcoord_co(uint64_t);
     27 STATIC_INLINE uint64_t coord_csep(cube_t);
     28 STATIC_INLINE uint64_t coord_cocsep(cube_t);
     29 STATIC_INLINE uint64_t coord_eo(cube_t);
     30 STATIC_INLINE uint64_t coord_esep(cube_t);
     31 STATIC_INLINE cube_t invcoord_esep(uint64_t);
     32 STATIC_INLINE uint64_t coord_epudsep(cube_t);
     33 STATIC_INLINE cube_t invcoord_epudsep(uint64_t);
     34 
     35 STATIC_INLINE bool is_eo_even(cube_t);
     36 
     37 STATIC_INLINE void copy_corners(cube_t [static 1], cube_t);
     38 STATIC_INLINE void copy_co(cube_t [static 1], cube_t);
     39 STATIC_INLINE void copy_edges(cube_t [static 1], cube_t);
     40 STATIC_INLINE void set_eo(cube_t [static 1], uint64_t);
     41 
     42 STATIC_INLINE void invcoord_esep_array(uint64_t, uint64_t, uint8_t[static 12]);
     43 STATIC_INLINE cube_t invcoord_eoesep(uint64_t);
     44 STATIC_INLINE uint64_t coord_epudsep_array(const uint8_t [8]);
     45 STATIC_INLINE void invcoord_epudsep_array(uint64_t, uint8_t [8]);
     46 
     47 STATIC_INLINE uint64_t coord_cp(cube_t);
     48 STATIC_INLINE cube_t invcoord_cp(uint64_t);
     49 STATIC_INLINE uint64_t coord_epud(cube_t);
     50 STATIC_INLINE cube_t invcoord_epud(uint64_t);
     51 STATIC_INLINE uint64_t coord_epe(cube_t);
     52 STATIC_INLINE cube_t invcoord_epe(uint64_t);
     53 
     54 STATIC_INLINE void
     55 invcoord_esep_array(uint64_t set1, uint64_t set2, uint8_t mem[static 12])
     56 {
     57 	uint64_t bit1, bit2, i, j, jj, k, l, s, v, w, is1;
     58 	uint8_t slice[3] = {0};
     59 
     60 	for (i = 0, j = 0, k = 4, l = 4; i < 12; i++)
     61 	{
     62 		v = binomial[11 - i][k];
     63 		jj = j < 8;
     64 		w = jj * binomial[7 - (j * jj)][l];
     65 		bit2 = set2 >= v;
     66 		bit1 = set1 >= w;
     67 		is1 = (1 - bit2) * bit1;
     68 
     69 		set2 -= bit2 * v;
     70 		k -= bit2;
     71 		set1 -= is1 * w;
     72 		l -= is1;
     73 		j += (1 - bit2);
     74 		s = 2 * bit2 + (1 - bit2) * bit1;
     75 
     76 		mem[i] = (slice[s]++) | (uint8_t)(s << 2);
     77 	}
     78 }
     79 
     80 STATIC_INLINE cube_t
     81 invcoord_eoesep(uint64_t i)
     82 {
     83 	cube_t c;
     84 	uint64_t esep, eo;
     85 
     86 	esep = i >> INT64_C(11);
     87 	eo = i % POW_2_11;
     88 	c = invcoord_esep(esep);
     89 	set_eo(&c, eo);
     90 
     91 	return c;
     92 }
     93 
     94 STATIC_INLINE uint64_t
     95 coord_epudsep_array(const uint8_t e[8])
     96 {
     97 	uint8_t i, k, is;
     98 	uint64_t ret;
     99 
    100 	ret = 0;
    101 	k = 4;
    102 	for (i = 0; i < 8; i++) {
    103 		is = (e[i] & UINT8_C(4)) >> UINT8_C(2);
    104 		ret += is * binomial[7-i][k];
    105 		k -= is;
    106 	}
    107 
    108 	return ret;
    109 }
    110 
    111 STATIC_INLINE void
    112 invcoord_epudsep_array(uint64_t c, uint8_t ret[8])
    113 {
    114 	uint8_t i, k, is, x, y;
    115 
    116 	k = 4;
    117 	x = 0;
    118 	y = 4;
    119 	for (i = 0; i < 8; i++) {
    120 		is = c >= binomial[7-i][k];
    121 		ret[i] = is*y + (1-is)*x;
    122 		y += is;
    123 		x += 1-is;
    124 		c -= is * binomial[7-i][k];
    125 		k -= is;
    126 	}
    127 }