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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
#include <inttypes.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include "utils/utils.h"
#include "arch/arch.h"
#include "core/core.h"
#include "solvers/solvers.h"
#include "nissy.h"
int parse_h48_options(const char *, uint8_t *, uint8_t *, uint8_t *);
STATIC int64_t write_result(cube_t, char [static 22]);
STATIC bool distribution_equal(const uint64_t [static INFO_DISTRIBUTION_LEN],
const uint64_t [static INFO_DISTRIBUTION_LEN], uint8_t);
STATIC bool checkdata(const void *, const tableinfo_t *);
/* TODO: add option to get DR, maybe C-only, E-only, eo... */
#define GETCUBE_OPTIONS(S, F) { .option = S, .fix = F }
struct {
char *option;
void (*fix)(int64_t *, int64_t *, int64_t *, int64_t *);
} getcube_options[] = {
GETCUBE_OPTIONS("fix", getcube_fix),
GETCUBE_OPTIONS(NULL, NULL)
};
int
parse_h48_options(const char *buf, uint8_t *h, uint8_t *k, uint8_t *maxdepth)
{
bool h_valid, k_valid, maxdepth_valid;
int i;
/* TODO temporarily, options are in the form "h;k;maxdepth" */
if (h != NULL)
*h = atoi(buf);
h_valid = h == NULL || *h <= 11;
for (i = 0; buf[i] != ';'; i++)
if (buf[i] == 0)
goto parse_h48_options_error;
if (k != NULL)
*k = atoi(&buf[i+1]);
k_valid = k == NULL || (*k == 2 || *k == 4);
for (i = i+1; buf[i] != ';'; i++)
if (buf[i] == 0)
goto parse_h48_options_error;
if (maxdepth != NULL)
*maxdepth = atoi(&buf[i+1]);
maxdepth_valid = maxdepth == NULL || *maxdepth <= 20;
return h_valid && k_valid && maxdepth_valid ? 0 : 1;
parse_h48_options_error:
*h = 0;
*k = 0;
*maxdepth = 0;
LOG("Error parsing options: must be in \"h;k;maxdepth\" format "
" (instead it was \"%s\")\n", buf);
return -1;
}
STATIC bool
checkdata(const void *buf, const tableinfo_t *info)
{
uint64_t distr[INFO_DISTRIBUTION_LEN];
if (!strncmp(info->solver, "cocsep", 6)) {
getdistribution_cocsep(
(uint32_t *)((char *)buf + INFOSIZE), distr);
} else if (!strncmp(info->solver, "h48", 3)) {
getdistribution_h48((uint8_t *)buf + INFOSIZE, distr,
info->h48h, info->bits);
} else {
LOG("checkdata: unknown solver %s\n", info->solver);
return false;
}
return distribution_equal(info->distribution, distr, info->maxvalue);
}
STATIC bool
distribution_equal(
const uint64_t expected[static INFO_DISTRIBUTION_LEN],
const uint64_t actual[static INFO_DISTRIBUTION_LEN],
uint8_t maxvalue
)
{
int wrong;
uint8_t i;
for (i = 0, wrong = 0; i <= MAX(maxvalue, 20); i++) {
if (expected[i] != actual[i]) {
wrong++;
LOG("Value %" PRIu8 ": expected %" PRIu64 ", found %"
PRIu64 "\n", i, expected[i], actual[i]);
}
}
if (wrong > 0) {
LOG("checkdata: %d wrong values\n", wrong);
} else {
LOG("checkdata: table is consistent with info\n");
}
return wrong > 0;
}
STATIC int64_t
write_result(cube_t cube, char result[static 22])
{
if (!isconsistent(cube)) {
writecube("B32", ZERO_CUBE, result);
return 2;
}
writecube("B32", cube, result);
return issolvable(cube) ? 0 : 1;
}
int64_t
nissy_compose(
const char cube[static 22],
const char permutation[static 22],
char result[static 22]
)
{
cube_t c, p, res;
c = readcube("B32", cube);
p = readcube("B32", permutation);
res = compose(c, p);
return write_result(res, result);
}
int64_t
nissy_inverse(
const char cube[static 22],
char result[static 22]
)
{
cube_t c, res;
c = readcube("B32", cube);
res = inverse(c);
return write_result(res, result);
}
int64_t
nissy_applymoves(
const char cube[static 22],
const char *moves,
char result[static 22]
)
{
cube_t c, res;
c = readcube("B32", cube);
res = applymoves(c, moves);
return write_result(res, result);
}
int64_t
nissy_applytrans(
const char cube[static 22],
const char *transformation,
char result[static 22]
)
{
cube_t c, res;
c = readcube("B32", cube);
res = applytrans(c, transformation);
return write_result(res, result);
}
int64_t
nissy_frommoves(
const char *moves,
char result[static 22]
)
{
cube_t res;
res = applymoves(SOLVED_CUBE, moves);
return write_result(res, result);
}
int64_t
nissy_convert(
const char *format_in,
const char *format_out,
const char *cube_string,
char *result
)
{
cube_t c;
c = readcube(format_in, cube_string);
writecube(format_out, c, result);
return isconsistent(c) ? 0 : 2;
}
int64_t
nissy_getcube(
int64_t ep,
int64_t eo,
int64_t cp,
int64_t co,
const char *options,
char result[static 22]
)
{
int i;
cube_t c;
for (i = 0; getcube_options[i].option != NULL; i++)
if (!strcmp(options, getcube_options[i].option))
getcube_options[i].fix(&ep, &eo, &cp, &co);
c = getcube(ep, eo, cp, co);
return write_result(c, result);
}
int64_t
nissy_datasize(
const char *solver,
const char *options
)
{
/* gendata() handles a NULL *data as a "dryrun" request */
return nissy_gendata(solver, options, NULL);
}
int64_t
nissy_datainfo(
const void *table,
void (*write)(const char *, ...)
)
{
uint8_t i;
tableinfo_t info;
readtableinfo(table, &info);
write("\n---------\n\n");
write("Table information for '%s'\n", info.solver);
write("\n");
write("Size: %" PRIu64 " bytes\n", info.fullsize);
write("Entries: %" PRIu64 " (%" PRIu8 " bits per entry)",
info.entries, info.bits);
write("\n");
switch (info.type) {
case TABLETYPE_PRUNING:
write("\n");
write("Table distribution:\nValue\tPositions\n");
for (i = 0; i <= info.maxvalue; i++) {
write("%" PRIu8 "\t%" PRIu64 "\n",
i + info.base, info.distribution[i]);
}
break;
case TABLETYPE_SPECIAL:
write("This is an ad-hoc table\n");
break;
default:
LOG("datainfo: unknown table type\n");
return 1;
}
if (info.next != 0) {
return nissy_datainfo((char *)table + info.next, write);
}
write("\n---------\n");
return 0;
}
int64_t
nissy_gendata(
const char *solver,
const char *options,
void *data
)
{
int p;
int64_t ret;
gendata_h48_arg_t arg;
arg.buf = data;
if (!strcmp(solver, "h48")) {
p = parse_h48_options(options, &arg.h, &arg.k, &arg.maxdepth);
if (p != 0) {
LOG("gendata: could not parse options\n");
ret = -1;
} else {
ret = gendata_h48(&arg);
}
} else if (!strcmp(solver, "h48stats")) {
arg.h = 0;
arg.k = 4;
arg.maxdepth = 20;
ret = gendata_h48(&arg);
} else {
LOG("gendata: implemented only for h48 solver\n");
ret = -1;
}
return ret;
}
int64_t
nissy_checkdata(
const char *solver,
const char *options,
const void *data
)
{
char *buf;
tableinfo_t info;
for (buf = (char *)data; readtableinfo(buf, &info); buf += info.next)
if (!checkdata(buf, &info))
return 1;
return 0;
}
int64_t
nissy_solve(
const char cube[static 22],
const char *solver,
const char *options,
const char *nisstype,
int8_t minmoves,
int8_t maxmoves,
int64_t maxsolutions,
int8_t optimal,
const void *data,
char *solutions
)
{
cube_t c;
int p;
int64_t ret;
uint8_t h, k;
c = readcube_B32(cube);
if (!issolvable(c)) {
LOG("solve: cube is not solvable\n");
return -1;
}
if (minmoves < 0) {
LOG("solve: 'minmoves' is negative, setting it to 0\n");
minmoves = 0;
}
if (maxmoves < 0) {
LOG("solve: 'maxmoves' is negative, setting it to 20\n");
maxmoves = 20;
}
if (maxsolutions < 0) {
LOG("solve: 'maxsols' is negative, stopping\n");
return -1;
}
if (maxsolutions == 0) {
LOG("solve: 'maxsols' is 0, returning no solution\n");
return 0;
}
if (solutions == NULL) {
LOG("solve: return parameter 'solutions' is NULL, stopping\n");
return -1;
}
/* TODO define and use solve_options_t */
if (!strcmp(solver, "h48")) {
p = parse_h48_options(options, &h, &k, NULL);
if (p != 0) {
LOG("gendata: could not parse options\n");
ret = -1;
} else {
ret = solve_h48(c, minmoves, maxmoves, maxsolutions,
data, solutions);
}
} else if (!strcmp(solver, "h48stats")) {
ret = solve_h48stats(c, maxmoves, data, solutions);
} else if (!strcmp(solver, "simple")) {
ret = solve_simple(
c, minmoves, maxmoves, maxsolutions, optimal, solutions);
} else {
LOG("solve: unknown solver '%s'\n", solver);
ret = -1;
}
return ret;
}
void
nissy_setlogger(void (*log)(const char *, ...))
{
nissy_log = log;
}
|