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
340
341
342
343
344
345
346
347
348
349
350
351
352
|
STATIC size_t gendata_coord(const coord_t [static 1], unsigned char *);
STATIC size_t gendata_multicoord(
const multicoord_t [static 1], unsigned char *);
STATIC long long gendata_coord_dispatch(const char *, unsigned long long,
unsigned char *);
STATIC tableinfo_t genptable_coord(
const coord_t [static 1], const unsigned char *, unsigned char *);
STATIC uint64_t genptable_coord_init_solved(
const coord_t [static 1], const unsigned char *, unsigned char *);
STATIC bool switch_to_fromnew(uint64_t, uint64_t, uint64_t);
STATIC uint64_t genptable_coord_fillneighbors(const coord_t [static 1],
const unsigned char *, uint64_t, uint8_t, unsigned char *);
STATIC uint64_t genptable_coord_fillfromnew(const coord_t [static 1],
const unsigned char *, uint64_t, uint8_t, unsigned char *);
STATIC uint8_t get_coord_pval(
const coord_t [static 1], const unsigned char *, uint64_t);
STATIC void set_coord_pval(
const coord_t [static 1], unsigned char *, uint64_t, uint8_t);
STATIC long long
gendata_coord_dispatch(
const char *coordstr,
unsigned long long bufsize,
unsigned char *buf
)
{
coord_t *coord;
multicoord_t *mcoord;
parse_coord_and_trans(coordstr, &coord, &mcoord, NULL);
if (coord != NULL)
return gendata_coord(coord, buf);
if (mcoord != NULL)
return gendata_multicoord(mcoord, buf);
LOG("Error: could not parse coordinate '%s'\n", coordstr);
return NISSY_ERROR_INVALID_SOLVER;
}
STATIC size_t
gendata_coord(const coord_t coord[static 1], unsigned char *buf)
{
uint64_t coord_dsize, tablesize, ninfo;
unsigned char *pruningbuf, *coord_data;
unsigned char *table;
tableinfo_t coord_data_info, pruning_info;
coord_data = buf == NULL ? NULL : buf + INFOSIZE;
coord_dsize = coord->gendata(coord_data);
if (coord_dsize == SIZE_MAX)
goto gendata_coord_error;
ninfo = coord_dsize == 0 ? 1 : 2;
tablesize = DIV_ROUND_UP(coord->max, 2);
if (buf == NULL)
goto gendata_coord_return_size;
if (ninfo == 2) {
coord_data_info = (tableinfo_t) {
.solver = "coord helper table for ",
.type = TABLETYPE_SPECIAL,
.infosize = INFOSIZE,
.fullsize = INFOSIZE + coord_dsize,
.hash = 0,
.next = INFOSIZE + coord_dsize,
/* Unknown / non-applicable values */
.entries = 0,
.classes = 0,
.bits = 0,
.base = 0,
.maxvalue = 0,
};
append_name(&coord_data_info, coord->name);
writetableinfo(&coord_data_info, INFOSIZE + coord_dsize, buf);
pruningbuf = buf + INFOSIZE + coord_dsize;
} else {
pruningbuf = buf;
}
table = pruningbuf + INFOSIZE;
pruning_info = genptable_coord(coord, coord_data, table);
writetableinfo(&pruning_info, INFOSIZE + tablesize, pruningbuf);
gendata_coord_return_size:
return ninfo * INFOSIZE + coord_dsize + tablesize;
gendata_coord_error:
LOG("Unexpected error generating coordinate data\n");
return 0;
}
STATIC size_t
gendata_multicoord(const multicoord_t mcoord[static 1], unsigned char *buf)
{
unsigned char *b;
size_t i, s, ret;
tableinfo_t info;
info = (tableinfo_t) {
.solver = "multicoordinate table for ",
.type = TABLETYPE_MULTI,
.infosize = INFOSIZE,
.fullsize = INFOSIZE,
.hash = 0,
.next = INFOSIZE,
/* Unknown / non-applicable values */
.entries = 0,
.classes = 0,
.bits = 0,
.base = 0,
.maxvalue = 0,
};
append_name(&info, mcoord->name);
if (buf != NULL)
writetableinfo(&info, INFOSIZE, buf);
ret = INFOSIZE;
for (i = 0; mcoord->coordinates[i] != NULL; i++) {
b = buf == NULL ? NULL : buf + ret;
s = gendata_coord(mcoord->coordinates[i], b);
if (s == 0)
return 0;
ret += s;
/* Pad so that each coordinate's table is 8-byte aligned */
while (ret % 8 != 0)
buf[ret++] = 0;
}
return ret;
}
STATIC tableinfo_t
genptable_coord(
const coord_t coord[static 1],
const unsigned char *data,
unsigned char *table
)
{
uint64_t tablesize, i, d, tot, t, nm;
tableinfo_t info;
tablesize = DIV_ROUND_UP(coord->max, 2);
info = (tableinfo_t) {
.solver = "coordinate solver for ",
.type = TABLETYPE_PRUNING,
.infosize = INFOSIZE,
.fullsize = INFOSIZE + tablesize,
.hash = 0,
.entries = coord->max,
.classes = 0,
.bits = 4,
.base = 0,
.maxvalue = 0,
.next = 0
};
memset(table, 0xFF, tablesize);
memset(info.distribution, 0, INFO_DISTRIBUTION_LEN * sizeof(uint64_t));
append_name(&info, coord->name);
tot = info.distribution[0] =
genptable_coord_init_solved(coord, data, table);
nm = popcount_u32(coord->moves_mask_gendata);
for (d = 1; tot < coord->max && d < 15; d++) {
t = 0;
if (switch_to_fromnew(tot, coord->max, nm)) {
for (i = 0; i < coord->max; i++)
if (get_coord_pval(coord, table, i) > d)
t += genptable_coord_fillfromnew(
coord, data, i, d, table);
} else {
for (i = 0; i < coord->max; i++)
if (get_coord_pval(coord, table, i) == d-1)
t += genptable_coord_fillneighbors(
coord, data, i, d, table);
}
tot += t;
info.distribution[d] = t;
LOG("[%s gendata] Depth %" PRIu64 ": found %" PRIu64 " (%"
PRIu64 " of %" PRIu64 ")\n",
coord->name, d, t, tot, coord->max);
}
if (tot == coord->max) {
info.maxvalue = d-1;
} else {
LOG("[%s gendata] Depth >= 15: %" PRIu64 " remaining\n",
coord->name, coord->max - tot);
info.distribution[d] = coord->max - tot;
info.maxvalue = 15;
}
return info;
}
STATIC uint64_t
genptable_coord_init_solved(
const coord_t coord[static 1],
const unsigned char *coord_data,
unsigned char *table
)
{
uint64_t i, max_solved, ret;
max_solved = coord->is_solved == NULL ? 1 : coord->max;
for (i = 0, ret = 0; i < max_solved; i++) {
if (coord_is_solved(coord, i, coord_data)) {
set_coord_pval(coord, table, i, 0);
ret++;
}
}
return ret;
}
STATIC bool
switch_to_fromnew(uint64_t done, uint64_t max, uint64_t nm)
{
/*
Heuristic to determine if it is more conveniente to loop over
done coordinates and fill the new discovered, or to loop from new
coordinates and fill them if they have a done neighbor.
*/
double r = (double)done / (double)max;
return 1.0 - intpow(1.0-r, nm) > nm * (1.0-r);
}
STATIC uint64_t
genptable_coord_fillneighbors(
const coord_t coord[static 1],
const unsigned char *data,
uint64_t i,
uint8_t d,
unsigned char *table
)
{
bool isnasty;
uint8_t m;
uint64_t ii, j, t, tot;
cube_t c, moved;
c = coord->cube(i, data);
tot = 0;
for (m = 0; m < NMOVES; m++) {
if (!((UINT32_C(1) << (uint32_t)m) &
coord->moves_mask_gendata))
continue;
moved = move(c, m);
ii = coord->coord(moved, data);
isnasty = coord->isnasty(ii, data);
for (t = 0; t < NTRANS && (t == 0 || isnasty); t++) {
if (!((UINT64_C(1) << t) & coord->trans_mask))
continue;
j = coord->coord(transform(moved, t), data);
if (get_coord_pval(coord, table, j) > d) {
set_coord_pval(coord, table, j, d);
tot++;
}
}
}
return tot;
}
STATIC uint64_t
genptable_coord_fillfromnew(
const coord_t coord[static 1],
const unsigned char *data,
uint64_t i,
uint8_t d,
unsigned char *table
)
{
bool found;
uint8_t m;
uint64_t tot, t, ii, j, nsim, sim[NTRANS];
cube_t c;
tot = 0;
c = coord->cube(i, data);
for (t = 0, nsim = 0; t < NTRANS; t++) {
if (!((UINT64_C(1) << t) & coord->trans_mask))
continue;
ii = coord->coord(transform(c, t), data);
for (j = 0, found = false; j < nsim && !found; j++)
found = sim[j] == ii;
if (!found)
sim[nsim++] = ii;
}
for (j = 0, found = false; j < nsim && !found; j++) {
c = coord->cube(sim[j], data);
for (m = 0; m < NMOVES; m++) {
if (!((UINT32_C(1) << (uint32_t)m) &
coord->moves_mask_gendata))
continue;
ii = coord->coord(move(c, m), data);
if (get_coord_pval(coord, table, ii) < d) {
found = true;
break;
}
}
}
tot = 0;
if (found) {
for (j = 0; j < nsim; j++) {
if (get_coord_pval(coord, table, sim[j]) > d) {
set_coord_pval(coord, table, sim[j], d);
tot++;
}
}
}
return tot;
}
STATIC uint8_t
get_coord_pval(
const coord_t coord[static 1],
const unsigned char *table,
uint64_t i
)
{
return (table[COORD_INDEX(i)] & COORD_MASK(i)) >> COORD_SHIFT(i);
}
STATIC void
set_coord_pval(
const coord_t coord[static 1],
unsigned char *table,
uint64_t i,
uint8_t val
)
{
table[COORD_INDEX(i)] = (table[COORD_INDEX(i)] & (~COORD_MASK(i)))
| (val << COORD_SHIFT(i));
}
|