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 (3184B)


      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_edges(cube_t [static 1], cube_t);
     39 STATIC_INLINE void set_eo(cube_t [static 1], uint64_t);
     40 
     41 STATIC_INLINE void invcoord_esep_array(uint64_t, uint64_t, uint8_t[static 12]);
     42 STATIC_INLINE cube_t invcoord_eoesep(uint64_t);
     43 STATIC_INLINE uint64_t coord_epudsep_array(const uint8_t [8]);
     44 STATIC_INLINE void invcoord_epudsep_array(uint64_t, uint8_t [8]);
     45 
     46 STATIC_INLINE uint64_t coord_cp(cube_t);
     47 STATIC_INLINE cube_t invcoord_cp(uint64_t);
     48 STATIC_INLINE uint64_t coord_epud(cube_t);
     49 STATIC_INLINE cube_t invcoord_epud(uint64_t);
     50 STATIC_INLINE uint64_t coord_epe(cube_t);
     51 STATIC_INLINE cube_t invcoord_epe(uint64_t);
     52 
     53 STATIC_INLINE void
     54 invcoord_esep_array(uint64_t set1, uint64_t set2, uint8_t mem[static 12])
     55 {
     56 	uint64_t bit1, bit2, i, j, jj, k, l, s, v, w, is1;
     57 	uint8_t slice[3] = {0};
     58 
     59 	for (i = 0, j = 0, k = 4, l = 4; i < 12; i++)
     60 	{
     61 		v = binomial[11 - i][k];
     62 		jj = j < 8;
     63 		w = jj * binomial[7 - (j * jj)][l];
     64 		bit2 = set2 >= v;
     65 		bit1 = set1 >= w;
     66 		is1 = (1 - bit2) * bit1;
     67 
     68 		set2 -= bit2 * v;
     69 		k -= bit2;
     70 		set1 -= is1 * w;
     71 		l -= is1;
     72 		j += (1 - bit2);
     73 		s = 2 * bit2 + (1 - bit2) * bit1;
     74 
     75 		mem[i] = (slice[s]++) | (uint8_t)(s << 2);
     76 	}
     77 }
     78 
     79 STATIC_INLINE cube_t
     80 invcoord_eoesep(uint64_t i)
     81 {
     82 	cube_t c;
     83 	uint64_t esep, eo;
     84 
     85 	esep = i >> INT64_C(11);
     86 	eo = i % POW_2_11;
     87 	c = invcoord_esep(esep);
     88 	set_eo(&c, eo);
     89 
     90 	return c;
     91 }
     92 
     93 STATIC_INLINE uint64_t
     94 coord_epudsep_array(const uint8_t e[8])
     95 {
     96 	uint8_t i, k, is;
     97 	uint64_t ret;
     98 
     99 	ret = 0;
    100 	k = 4;
    101 	for (i = 0; i < 8; i++) {
    102 		is = (e[i] & UINT8_C(4)) >> UINT8_C(2);
    103 		ret += is * binomial[7-i][k];
    104 		k -= is;
    105 	}
    106 
    107 	return ret;
    108 }
    109 
    110 STATIC_INLINE void
    111 invcoord_epudsep_array(uint64_t c, uint8_t ret[8])
    112 {
    113 	uint8_t i, k, is, x, y;
    114 
    115 	k = 4;
    116 	x = 0;
    117 	y = 4;
    118 	for (i = 0; i < 8; i++) {
    119 		is = c >= binomial[7-i][k];
    120 		ret[i] = is*y + (1-is)*x;
    121 		y += is;
    122 		x += 1-is;
    123 		c -= is * binomial[7-i][k];
    124 		k -= is;
    125 	}
    126 }