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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
|
#include <inttypes.h>
#include <pthread.h>
#include <stdatomic.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_solver(const char *, uint8_t [static 1], uint8_t [static 1]);
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 char *, const tableinfo_t *);
#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_solver(const char *buf, uint8_t h[static 1], uint8_t k[static 1])
{
const char *fullbuf = buf;
buf += 3;
if (!strcmp(buf, "stats")) {
*h = 0;
*k = 4;
return 0;
}
if (*buf != 'h')
goto parse_h48_solver_error;
buf++;
*h = atoi(buf);
for ( ; *buf >= 0 + '0' && *buf <= 9 + '0'; buf++)
if (*buf == 0)
goto parse_h48_solver_error;
if (*buf != 'k')
goto parse_h48_solver_error;
buf++;
*k = atoi(buf);
return *h < 12 && (*k == 2 || (*k == 4 && *h == 0)) ? 0 : 1;
parse_h48_solver_error:
*h = 0;
*k = 0;
LOG("Error parsing solver: must be in \"h48h*k*\" format"
" or \"h48stats\", but got %s\n", fullbuf);
return -1;
}
STATIC bool
checkdata(const char *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]);
}
}
return wrong == 0;
}
STATIC int64_t
write_result(cube_t cube, char result[static 22])
{
writecube("B32", cube, result);
if (!issolvable(cube)) {
LOG("Warning: resulting cube is not solvable\n");
return NISSY_WARNING_UNSOLVABLE;
}
return NISSY_OK;
}
int64_t
nissy_compose(
const char cube[static 22],
const char permutation[static 22],
char result[static 22]
)
{
cube_t c, p, res;
int64_t err;
c = readcube("B32", cube);
if (!isconsistent(c)) {
LOG("Error in nissy_compose: given cube is invalid\n");
err = NISSY_ERROR_INVALID_CUBE;
goto nissy_compose_error;
}
p = readcube("B32", permutation);
if (!isconsistent(p)) {
LOG("Error in nissy_compose: given permutation is invalid\n");
err = NISSY_ERROR_INVALID_CUBE;
goto nissy_compose_error;
}
res = compose(c, p);
if (!isconsistent(res)) {
LOG("Unknown error: resulting cube is invalid\n");
err = NISSY_ERROR_UNKNOWN;
goto nissy_compose_error;
}
return write_result(res, result);
nissy_compose_error:
writecube("B32", ZERO_CUBE, result);
return err;
}
int64_t
nissy_inverse(
const char cube[static 22],
char result[static 22]
)
{
cube_t c, res;
int64_t err;
c = readcube("B32", cube);
if (iserror(c)) {
LOG("Error in nissy_inverse: given cube is invalid\n");
err = NISSY_ERROR_INVALID_CUBE;
goto nissy_inverse_error;
}
res = inverse(c);
if (!isconsistent(res)) {
LOG("Unknown error: inverted cube is invalid\n");
err = NISSY_ERROR_UNKNOWN;
goto nissy_inverse_error;
}
return write_result(res, result);
nissy_inverse_error:
writecube("B32", ZERO_CUBE, result);
return err;
}
int64_t
nissy_applymoves(
const char cube[static 22],
const char *moves,
char result[static 22]
)
{
cube_t c, res;
int64_t err;
if (moves == NULL) {
LOG("Error: 'moves' argument is NULL\n");
err = NISSY_ERROR_NULL_POINTER;
goto nissy_applymoves_error;
}
c = readcube("B32", cube);
if (!isconsistent(c)) {
LOG("Error in nissy_applymoves: given cube is invalid\n");
err = NISSY_ERROR_INVALID_CUBE;
goto nissy_applymoves_error;
}
res = applymoves(c, moves);
if (!isconsistent(res)) {
/* Assume we got a reasonable error message from applymoves */
err = NISSY_ERROR_INVALID_MOVES;
goto nissy_applymoves_error;
}
return write_result(res, result);
nissy_applymoves_error:
writecube("B32", ZERO_CUBE, result);
return err;
}
int64_t
nissy_applytrans(
const char cube[static 22],
const char transformation[static 12],
char result[static 22]
)
{
cube_t c, res;
int64_t err;
c = readcube("B32", cube);
if (!isconsistent(c)) {
LOG("Error in nissy_applytrans: given cube is invalid\n");
err = NISSY_ERROR_INVALID_CUBE;
goto nissy_applytrans_error;
}
res = applytrans(c, transformation);
if (!isconsistent(res)) {
/* Assume we got a reasonable error message from applytrans */
err = NISSY_ERROR_INVALID_TRANS;
goto nissy_applytrans_error;
}
return write_result(res, result);
nissy_applytrans_error:
writecube("B32", ZERO_CUBE, result);
return err;
}
int64_t
nissy_frommoves(
const char *moves,
char result[static 22]
)
{
cube_t res;
int64_t err;
if (moves == NULL) {
LOG("Error: 'moves' argument is NULL\n");
err = NISSY_ERROR_NULL_POINTER;
goto nissy_frommoves_error;
}
res = applymoves(SOLVED_CUBE, moves);
if (!isconsistent(res)) {
/* Assume we got a reasonable error message from applymoves */
err = NISSY_ERROR_INVALID_MOVES;
goto nissy_frommoves_error;
}
return write_result(res, result);
nissy_frommoves_error:
writecube("B32", ZERO_CUBE, result);
return err;
}
int64_t
nissy_convert(
const char *format_in,
const char *format_out,
const char *cube_string,
size_t result_size,
char result[result_size]
)
{
cube_t c;
int ret;
int64_t err;
if (format_in == NULL) {
LOG("Error: 'format_in' argument is NULL\n");
err = NISSY_ERROR_NULL_POINTER;
goto nissy_convert_error;
}
if (format_out == NULL) {
LOG("Error: 'format_out' argument is NULL\n");
err = NISSY_ERROR_NULL_POINTER;
goto nissy_convert_error;
}
if (cube_string == NULL) {
LOG("Error: 'cube_string' argument is NULL\n");
err = NISSY_ERROR_NULL_POINTER;
goto nissy_convert_error;
}
c = readcube(format_in, cube_string);
if (iserror(c)) {
err = NISSY_ERROR_INVALID_CUBE;
goto nissy_convert_error;
}
ret = writecube(format_out, c, result);
if (ret != 0) {
/* Assume the format was invalid */
err = NISSY_ERROR_INVALID_FORMAT;
goto nissy_convert_error;
}
if (!isconsistent(c)) {
err = NISSY_ERROR_UNKNOWN;
goto nissy_convert_error;
}
return NISSY_OK;
nissy_convert_error:
/* We don't write anything to result, we don't know the format */
return err;
}
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;
if (options == NULL) {
LOG("Error: 'options' argument is NULL\n");
return NISSY_ERROR_NULL_POINTER;
}
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);
if (!isconsistent(c)) {
LOG("Error: could not get cube with ep=%" PRId64 ", eo=%"
PRId64 ", cp=%" PRId64 ", co=%" PRId64 ".\n",
ep, eo, cp, co);
return NISSY_ERROR_OPTIONS;
}
return write_result(c, result);
}
int64_t
nissy_datasize(
const char *solver
)
{
if (solver == NULL) {
LOG("Error: 'solver' argument is NULL\n");
return NISSY_ERROR_NULL_POINTER;
}
/* gendata() handles a NULL *data as a "dryrun" request */
return nissy_gendata(solver, 0, NULL);
}
int64_t
nissy_datainfo(
size_t data_size,
const char data[data_size],
void (*write)(const char *, ...)
)
{
uint8_t i;
tableinfo_t info;
readtableinfo(data, &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 NISSY_ERROR_DATA;
}
if (info.next != 0)
return nissy_datainfo(
data_size - info.next, (char *)data + info.next, write);
write("\n---------\n");
return NISSY_OK;
}
int64_t
nissy_gendata(
const char *solver,
size_t data_size,
char data[data_size]
)
{
int p;
gendata_h48_arg_t arg;
if (solver == NULL) {
LOG("Error: 'solver' argument is NULL\n");
return NISSY_ERROR_NULL_POINTER;
}
arg.buf = data;
if (!strncmp(solver, "h48", 3)) {
p = parse_h48_solver(solver, &arg.h, &arg.k);
arg.maxdepth = 20;
if (p != 0)
return NISSY_ERROR_UNKNOWN;
return (int64_t)gendata_h48(&arg);
} else {
LOG("gendata: unknown solver %s\n", solver);
return NISSY_ERROR_INVALID_SOLVER;
}
}
int64_t
nissy_checkdata(
uint64_t data_size,
const char data[data_size]
)
{
char *buf;
tableinfo_t info;
for (buf = (char *)data; readtableinfo(buf, &info); buf += info.next) {
if (!checkdata(buf, &info)) {
LOG("Error: data for %s is inconsistent with info!\n",
info.solver);
return NISSY_ERROR_DATA;
}
if (info.next == 0)
break;
}
return NISSY_OK;
}
int64_t
nissy_solve(
const char cube[static 22],
const char *solver,
uint8_t nissflag,
int8_t minmoves,
int8_t maxmoves,
int64_t maxsols,
int8_t optimal,
size_t data_size,
const char data[data_size],
size_t sols_size,
char sols[sols_size]
)
{
cube_t c;
int p;
uint8_t h, k;
if (solver == NULL) {
LOG("Error: 'solver' argument is NULL\n");
return NISSY_ERROR_NULL_POINTER;
}
c = readcube_B32(cube);
if (!isconsistent(c)) {
LOG("solve: cube is invalid\n");
return NISSY_ERROR_INVALID_CUBE;
}
if (!issolvable(c)) {
LOG("solve: cube is not solvable\n");
return NISSY_ERROR_UNSOLVABLE_CUBE;
}
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 (maxsols < 0) {
LOG("solve: 'maxsols' is negative, stopping\n");
return NISSY_ERROR_OPTIONS;
}
if (maxsols == 0) {
LOG("solve: 'maxsols' is 0, returning no solution\n");
return 0;
}
if (!strncmp(solver, "h48", 3)) {
if (!strcmp(solver, "h48stats"))
return solve_h48stats(c, maxmoves, data, sols);
p = parse_h48_solver(solver, &h, &k);
if (p != 0) {
LOG("solve: unknown solver %s\n", solver);
return NISSY_ERROR_INVALID_SOLVER;
} else {
return THREADS > 1 ?
solve_h48_multithread(c, minmoves,
maxmoves, maxsols, data, sols) :
solve_h48(c, minmoves,
maxmoves, maxsols, data, sols);
}
} else if (!strcmp(solver, "simple")) {
return solve_simple(
c, minmoves, maxmoves, maxsols, optimal, sols);
} else {
LOG("solve: unknown solver '%s'\n", solver);
return NISSY_ERROR_INVALID_SOLVER;
}
}
int64_t
nissy_setlogger(
void (*log)(const char *, ...)
)
{
nissy_log = log;
if (log == NULL)
return NISSY_WARNING_NULL_CALLBACK;
return NISSY_OK;
}
int64_t
nissy_explainerror(
int64_t error_code,
void (*write)(const char *, ...)
)
{
if (write == NULL)
return NISSY_WARNING_NULL_CALLBACK;
switch (error_code) {
case NISSY_OK:
write(
"The value %" PRId64 " denotes a success.\n"
"If returned by solve, it means that no solutions has "
"been found.\n", NISSY_OK
);
return NISSY_OK;
case NISSY_WARNING_UNSOLVABLE:
write(
"The value %" PRId64 " is a warning. It means that the "
"operation was completed succesfully, but the resulting "
"cube is in an unsolvable state. This could be intended, "
"for example if the user has provided an unsolvable cube "
"as input.\n", NISSY_WARNING_UNSOLVABLE
);
return NISSY_OK;
case NISSY_WARNING_NULL_CALLBACK:
write(
"The value %" PRId64 " is a warning. It means that the "
"provided pointer to a writer function is NULL.\n"
"If returned by nissy_setlogger, it means that any future "
"log messages will not be printed.\n",
NISSY_WARNING_NULL_CALLBACK
);
return NISSY_OK;
case NISSY_ERROR_INVALID_CUBE:
write(
"The value %" PRId64 " means that the provided cube is "
"invalid. It could be written in an unknown format, or "
"in a format different from what specified, or simply "
"ill-formed.\n", NISSY_ERROR_INVALID_CUBE
);
return NISSY_OK;
case NISSY_ERROR_UNSOLVABLE_CUBE:
write(
"The value %" PRId64 " means that the provided cube is "
"in an unsolvable state.\n", NISSY_ERROR_INVALID_CUBE
);
return NISSY_OK;
case NISSY_ERROR_INVALID_MOVES:
write(
"The value %" PRId64 " means that the given moves are "
"invalid.\n", NISSY_ERROR_INVALID_MOVES
);
return NISSY_OK;
case NISSY_ERROR_INVALID_TRANS:
write(
"The value %" PRId64 " means that the given transformation "
"is invalid.\n", NISSY_ERROR_INVALID_TRANS
);
return NISSY_OK;
case NISSY_ERROR_INVALID_FORMAT:
write(
"The value %" PRId64 " means that the given format is not "
"known.\n", NISSY_ERROR_INVALID_FORMAT
);
return NISSY_OK;
case NISSY_ERROR_INVALID_SOLVER:
write(
"The value %" PRId64 " means that the given solver is not "
"known.\n", NISSY_ERROR_INVALID_SOLVER
);
return NISSY_OK;
case NISSY_ERROR_NULL_POINTER:
write(
"The value %" PRId64 " means that one of the provided "
"pointer arguments is NULL. For example, it may be "
"returned by solve when called with a solver that "
"requires some pre-computed data, but the provided data "
"is NULL.\n", NISSY_ERROR_NULL_POINTER
);
return NISSY_OK;
case NISSY_ERROR_DATA:
write(
"The value %" PRId64 " means that the provided data is "
"invalid. For example, it may be returned by solve when "
"called with incompatible solver and data arguments.\n",
NISSY_ERROR_DATA
);
return NISSY_OK;
case NISSY_ERROR_OPTIONS:
write(
"The value %" PRId64 " means that one or more of the "
"given options are invalid. For example, it may be "
"returned by solve when called with a negative maximum "
"number of solutions.\n", NISSY_ERROR_OPTIONS
);
return NISSY_OK;
case NISSY_ERROR_INVALID_CODE:
write(
"The value %" PRId64 " means that the given error code "
"is not known. It may be returned by explainerror.\n",
NISSY_ERROR_INVALID_CODE
);
return NISSY_OK;
case NISSY_ERROR_UNKNOWN:
write(
"The value %" PRId64 " denotes an unexpected error. It "
"probably means that there some bug in this library.\n"
"If you can, report any error of this kind to "
"sebastiano@tronto.net. Thanks!\n", NISSY_ERROR_UNKNOWN
);
return NISSY_OK;
default:
break;
}
if (error_code > 0) {
write(
"A positive return values does not denote an error\n"
"If returned by gendata or datasize, it denotes the size "
"of the data, in bytes\n"
"If returned by solve, it denotes the number of solutions "
"found.\n"
);
return NISSY_OK;
} else {
write("Unknown error code %" PRId64 "\n", error_code);
return NISSY_ERROR_INVALID_CODE;
}
}
|