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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
|
typedef __m256i cube_fast_t;
#define _co2_avx2 _mm256_set_epi64x(0, 0, 0, 0x6060606060606060)
#define _cocw_avx2 _mm256_set_epi64x(0, 0, 0, 0x2020202020202020)
#define _cp_avx2 _mm256_set_epi64x(0, 0, 0, 0x0707070707070707)
#define _ep_avx2 _mm256_set_epi64x(0x0F0F0F0F, 0x0F0F0F0F0F0F0F0F, 0, 0)
#define _eo_avx2 _mm256_set_epi64x(0x10101010, 0x1010101010101010, 0, 0)
_static_inline cube_fast_t fastcube(
uint8_t, uint8_t, uint8_t, uint8_t, uint8_t,
uint8_t, uint8_t, uint8_t, uint8_t, uint8_t,
uint8_t, uint8_t, uint8_t, uint8_t, uint8_t,
uint8_t, uint8_t, uint8_t, uint8_t, uint8_t
);
_static cube_fast_t cubetofast(cube_t);
_static cube_t fasttocube(cube_fast_t);
_static_inline bool equal_fast(cube_fast_t, cube_fast_t);
_static_inline bool issolved_fast(cube_fast_t);
_static_inline cube_fast_t invertco_fast(cube_fast_t);
_static_inline cube_fast_t compose_epcpeo(cube_fast_t, cube_fast_t);
_static_inline cube_fast_t compose_fast_edges(cube_fast_t, cube_fast_t);
_static_inline cube_fast_t compose_fast_corners(cube_fast_t, cube_fast_t);
_static_inline cube_fast_t compose_fast(cube_fast_t, cube_fast_t);
_static_inline int64_t coord_fast_co(cube_fast_t);
_static_inline int64_t coord_fast_csep(cube_fast_t);
_static_inline int64_t coord_fast_cocsep(cube_fast_t);
_static_inline int64_t coord_fast_eo(cube_fast_t);
_static_inline int64_t coord_fast_esep(cube_fast_t);
_static_inline void copy_corners_fast(cube_fast_t *, cube_fast_t);
_static_inline void copy_edges_fast(cube_fast_t *, cube_fast_t);
_static_inline void set_eo_fast(cube_fast_t *, int64_t);
_static_inline cube_fast_t invcoord_fast_esep(int64_t);
_static_inline cube_fast_t
fastcube(
uint8_t c_ufr,
uint8_t c_ubl,
uint8_t c_dfl,
uint8_t c_dbr,
uint8_t c_ufl,
uint8_t c_ubr,
uint8_t c_dfr,
uint8_t c_dbl,
uint8_t e_uf,
uint8_t e_ub,
uint8_t e_db,
uint8_t e_df,
uint8_t e_ur,
uint8_t e_ul,
uint8_t e_dl,
uint8_t e_dr,
uint8_t e_fr,
uint8_t e_fl,
uint8_t e_bl,
uint8_t e_br
)
{
return _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
);
}
_static cube_fast_t
cubetofast(cube_t a)
{
uint8_t aux[32];
memset(aux, 0, 32);
memcpy(aux, &a.corner, 8);
memcpy(aux + 16, &a.edge, 12);
return _mm256_loadu_si256((__m256i_u *)&aux);
}
_static cube_t
fasttocube(cube_fast_t c)
{
cube_t a;
uint8_t aux[32];
_mm256_storeu_si256((__m256i_u *)aux, c);
memcpy(&a.corner, aux, 8);
memcpy(&a.edge, aux + 16, 12);
return a;
}
_static_inline bool
equal_fast(cube_fast_t c1, cube_fast_t c2)
{
int32_t mask;
__m256i cmp;
cmp = _mm256_cmpeq_epi8(c1, c2);
mask = _mm256_movemask_epi8(cmp);
return mask == ~0;
}
_static_inline bool
issolved_fast(cube_fast_t cube)
{
return equal_fast(cube, solved_fast);
}
_static_inline cube_fast_t
invertco_fast(cube_fast_t c)
{
cube_fast_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_fast_t
compose_epcpeo(cube_fast_t c1, cube_fast_t c2)
{
cube_fast_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_fast_t
compose_fast_edges(cube_fast_t c1, cube_fast_t c2)
{
return compose_epcpeo(c1, c2);
}
_static_inline cube_fast_t
compose_fast_corners(cube_fast_t c1, cube_fast_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_fast(c1, c2);
}
_static_inline cube_fast_t
compose_fast(cube_fast_t c1, cube_fast_t c2)
{
cube_fast_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 int64_t
coord_fast_co(cube_fast_t c)
{
cube_fast_t co;
int64_t mem[4], ret, i, p;
co = _mm256_and_si256(c, _co2_avx2);
_mm256_storeu_si256((__m256i *)mem, co);
mem[0] >>= 5L;
for (i = 0, ret = 0, p = 1; i < 7; i++, mem[0] >>= 8L, p *= 3)
ret += (mem[0] & 3L) * p;
return ret;
}
_static_inline int64_t
coord_fast_csep(cube_fast_t c)
{
cube_fast_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_fast_cocsep(cube_fast_t c)
{
return (coord_fast_co(c) << 7) + coord_fast_csep(c);
}
_static_inline int64_t
coord_fast_eo(cube_fast_t c)
{
cube_fast_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_fast_esep(cube_fast_t c)
{
cube_fast_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] <<= 8L;
ret1 = ret2 = 0;
k = l = 4;
for (i = 0, j = 0; i < 12; i++, mem[i/8 + 2] >>= 8L) {
e = mem[i/8 + 2];
bit1 = (e & _esepbit1) >> 2L;
bit2 = (e & _esepbit2) >> 3L;
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_fast(cube_fast_t *dest, cube_fast_t src)
{
*dest = _mm256_blend_epi32(*dest, src, 0x0F);
}
_static_inline void
copy_edges_fast(cube_fast_t *dest, cube_fast_t src)
{
*dest = _mm256_blend_epi32(*dest, src, 0xF0);
}
_static_inline void
set_eo_fast(cube_fast_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_fast_t
invcoord_fast_esep(int64_t esep)
{
cube_fast_t eee, ret;
int64_t i, j, jj, k, l, s, v, w, is1, set1, set2;
uint8_t bit2, bit1, 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]++) | (s << 2);
}
ret = cubetofast(solved);
eee = _mm256_loadu_si256((__m256i_u *)&mem);
copy_edges_fast(&ret, eee);
return ret;
}
|