aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-03-22 06:43:11 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2025-03-22 18:45:47 +0100
commitce3f1cc0ef9f46d70ab5387b1458e9098b40711d (patch)
tree4949745670b829f2211381bf14b8440dcc6ca7b1
parent0550a16c1cce868bbc3f3b5ad59f80e35cf2a6cd (diff)
downloadnissy-core-ce3f1cc0ef9f46d70ab5387b1458e9098b40711d.tar.gz
nissy-core-ce3f1cc0ef9f46d70ab5387b1458e9098b40711d.zip
Some safety with move arrays, small refactor appendchar
Diffstat (limited to '')
-rw-r--r--src/core/cube.h8
-rw-r--r--src/core/io_cube.h24
-rw-r--r--src/core/io_moves.h17
-rw-r--r--src/core/moves.h29
-rw-r--r--src/solvers/coord/common.h25
-rw-r--r--src/solvers/coord/gendata.h4
-rw-r--r--src/solvers/coord/solve.h61
-rw-r--r--src/solvers/coord/types_macros.h2
-rw-r--r--src/solvers/h48/gendata_h48.h4
-rw-r--r--src/solvers/h48/gendata_types_macros.h1
-rw-r--r--src/solvers/h48/solve.h48
-rw-r--r--src/solvers/solutions.h14
-rw-r--r--src/solvers/solvers.h1
-rw-r--r--src/utils/constants.h2
-rw-r--r--src/utils/math.h65
-rw-r--r--test/010_math_permtoindex/permtoindex_tests.c4
-rw-r--r--test/011_math_indextoperm/indextoperm_tests.c2
-rw-r--r--test/012_math_permsign/permsign_tests.c4
-rw-r--r--test/013_math_digitstosumzero/digitstosumzero_tests.c7
-rw-r--r--test/014_math_sumzerotodigits/sumzerotodigits.c2
-rw-r--r--test/032_invertmoves/invertmoves_tests.c8
-rw-r--r--test/080_allowednext/allowednext_tests.c4
-rw-r--r--test/test.h2
23 files changed, 168 insertions, 170 deletions
diff --git a/src/core/cube.h b/src/core/cube.h
index 49d868c..ab5f181 100644
--- a/src/core/cube.h
+++ b/src/core/cube.h
@@ -87,7 +87,7 @@ issolvable(cube_t cube)
87 for (i = 0; i < 8; i++) 87 for (i = 0; i < 8; i++)
88 cp[i] = corner[i] & PBITS; 88 cp[i] = corner[i] & PBITS;
89 89
90 if (permsign(ep, 12) != permsign(cp, 8)) 90 if (permsign(12, ep) != permsign(8, cp))
91 goto issolvable_parity; 91 goto issolvable_parity;
92 92
93 eo = 0; 93 eo = 0;
@@ -139,13 +139,13 @@ getcube_fix(long long *ep, long long *eo, long long *cp, long long *co)
139 139
140 indextoperm(*ep, 12, e); 140 indextoperm(*ep, 12, e);
141 indextoperm(*cp, 8, c); 141 indextoperm(*cp, 8, c);
142 if (permsign(e, 12) != permsign(c, 8)) { 142 if (permsign(12, e) != permsign(8, c)) {
143 SWAP(c[0], c[1]); 143 SWAP(c[0], c[1]);
144 *cp = permtoindex(c, 8); 144 *cp = permtoindex(8, c);
145 145
146 sumzerotodigits(*co, 8, 3, coarr); 146 sumzerotodigits(*co, 8, 3, coarr);
147 SWAP(coarr[0], coarr[1]); 147 SWAP(coarr[0], coarr[1]);
148 *co = digitstosumzero(coarr, 8, 3); 148 *co = digitstosumzero(8, coarr, 3);
149 } 149 }
150} 150}
151 151
diff --git a/src/core/io_cube.h b/src/core/io_cube.h
index 0c18f34..39f3545 100644
--- a/src/core/io_cube.h
+++ b/src/core/io_cube.h
@@ -1,5 +1,5 @@
1STATIC cube_t readcube(const char *, const char *); 1STATIC cube_t readcube(const char *, const char *);
2STATIC int64_t writecube(const char *, cube_t, uint64_t, char *); 2STATIC int64_t writecube(const char *, cube_t, size_t n, char [n]);
3STATIC void log_available_formats(void); 3STATIC void log_available_formats(void);
4STATIC uint8_t readco(const char *); 4STATIC uint8_t readco(const char *);
5STATIC uint8_t readcp(const char *); 5STATIC uint8_t readcp(const char *);
@@ -10,10 +10,10 @@ STATIC cube_t readcube_H48(const char *);
10STATIC uint8_t readpiece_LST(const char **); 10STATIC uint8_t readpiece_LST(const char **);
11STATIC cube_t readcube_LST(const char *); 11STATIC cube_t readcube_LST(const char *);
12 12
13STATIC int64_t writepiece_LST(uint8_t, uint64_t, char *); 13STATIC int64_t writepiece_LST(uint8_t, size_t n, char [n]);
14STATIC int64_t writecube_B32(cube_t, uint64_t, char *); 14STATIC int64_t writecube_B32(cube_t, size_t n, char [n]);
15STATIC int64_t writecube_H48(cube_t, uint64_t, char *); 15STATIC int64_t writecube_H48(cube_t, size_t n, char [n]);
16STATIC int64_t writecube_LST(cube_t, uint64_t, char *); 16STATIC int64_t writecube_LST(cube_t, size_t n, char [n]);
17 17
18STATIC uint8_t b32toedge(char); 18STATIC uint8_t b32toedge(char);
19STATIC uint8_t b32tocorner(char); 19STATIC uint8_t b32tocorner(char);
@@ -23,7 +23,7 @@ STATIC char cornertob32(uint8_t);
23STATIC struct { 23STATIC struct {
24 const char *name; 24 const char *name;
25 cube_t (*read)(const char *); 25 cube_t (*read)(const char *);
26 int64_t (*write)(cube_t, uint64_t, char *); 26 int64_t (*write)(cube_t, size_t n, char [n]);
27} ioformat[] = 27} ioformat[] =
28{ 28{
29 { .name = "B32", .read = readcube_B32, .write = writecube_B32 }, 29 { .name = "B32", .read = readcube_B32, .write = writecube_B32 },
@@ -47,7 +47,7 @@ readcube(const char *format, const char *buf)
47} 47}
48 48
49STATIC int64_t 49STATIC int64_t
50writecube(const char *format, cube_t cube, uint64_t buf_size, char *buf) 50writecube(const char *format, cube_t cube, size_t buf_size, char buf[buf_size])
51{ 51{
52 int i; 52 int i;
53 53
@@ -233,10 +233,10 @@ readcube_LST(const char *buf)
233} 233}
234 234
235STATIC int64_t 235STATIC int64_t
236writepiece_LST(uint8_t piece, uint64_t buf_size, char *buf) 236writepiece_LST(uint8_t piece, size_t buf_size, char buf[buf_size])
237{ 237{
238 char digits[3]; 238 char digits[3];
239 uint64_t i, len; 239 size_t i, len;
240 240
241 if (piece > 99 || buf_size < 3) 241 if (piece > 99 || buf_size < 3)
242 return 0; 242 return 0;
@@ -263,7 +263,7 @@ writepiece_LST(uint8_t piece, uint64_t buf_size, char *buf)
263} 263}
264 264
265STATIC int64_t 265STATIC int64_t
266writecube_B32(cube_t cube, uint64_t buf_size, char *buf) 266writecube_B32(cube_t cube, size_t buf_size, char buf[buf_size])
267{ 267{
268 int i; 268 int i;
269 uint8_t corner[8], edge[12]; 269 uint8_t corner[8], edge[12];
@@ -291,7 +291,7 @@ writecube_B32(cube_t cube, uint64_t buf_size, char *buf)
291} 291}
292 292
293STATIC int64_t 293STATIC int64_t
294writecube_H48(cube_t cube, uint64_t buf_size, char *buf) 294writecube_H48(cube_t cube, size_t buf_size, char buf[buf_size])
295{ 295{
296 uint8_t piece, perm, orient, corner[8], edge[12]; 296 uint8_t piece, perm, orient, corner[8], edge[12];
297 int i; 297 int i;
@@ -331,7 +331,7 @@ writecube_H48(cube_t cube, uint64_t buf_size, char *buf)
331} 331}
332 332
333STATIC int64_t 333STATIC int64_t
334writecube_LST(cube_t cube, uint64_t buf_size, char *buf) 334writecube_LST(cube_t cube, size_t buf_size, char buf[buf_size])
335{ 335{
336 int i; 336 int i;
337 uint64_t ptr; 337 uint64_t ptr;
diff --git a/src/core/io_moves.h b/src/core/io_moves.h
index 806887b..fa91e8b 100644
--- a/src/core/io_moves.h
+++ b/src/core/io_moves.h
@@ -1,6 +1,6 @@
1STATIC uint8_t readmove(char); 1STATIC uint8_t readmove(char);
2STATIC uint8_t readmodifier(char); 2STATIC uint8_t readmodifier(char);
3STATIC int64_t writemoves(uint8_t *, int, uint64_t, char *); 3STATIC int64_t writemoves(size_t n, uint8_t [n], size_t m, char [m]);
4 4
5STATIC uint8_t 5STATIC uint8_t
6readmove(char c) 6readmove(char c)
@@ -39,11 +39,14 @@ readmodifier(char c)
39} 39}
40 40
41STATIC int64_t 41STATIC int64_t
42writemoves(uint8_t *m, int n, uint64_t buf_size, char *buf) 42writemoves(
43 size_t nmoves,
44 uint8_t m[nmoves],
45 size_t buf_size,
46 char buf[buf_size]
47)
43{ 48{
44 int i; 49 size_t i, len, written;
45 uint64_t len;
46 int64_t written;
47 const char *s; 50 const char *s;
48 char *b; 51 char *b;
49 52
@@ -52,7 +55,7 @@ writemoves(uint8_t *m, int n, uint64_t buf_size, char *buf)
52 return NISSY_ERROR_BUFFER_SIZE; 55 return NISSY_ERROR_BUFFER_SIZE;
53 } 56 }
54 57
55 for (i = 0, b = buf, written = 0; i < n; i++, b++, written++) { 58 for (i = 0, b = buf, written = 0; i < nmoves; i++, b++, written++) {
56 s = movestr[m[i]]; 59 s = movestr[m[i]];
57 len = strlen(s); 60 len = strlen(s);
58 if (len + written >= buf_size) { 61 if (len + written >= buf_size) {
@@ -70,7 +73,7 @@ writemoves(uint8_t *m, int n, uint64_t buf_size, char *buf)
70 b--; /* Remove last space */ 73 b--; /* Remove last space */
71 *b = '\0'; 74 *b = '\0';
72 75
73 return written; 76 return (int64_t)written;
74 77
75writemoves_error: 78writemoves_error:
76 *buf = '\0'; 79 *buf = '\0';
diff --git a/src/core/moves.h b/src/core/moves.h
index a45bc05..820406b 100644
--- a/src/core/moves.h
+++ b/src/core/moves.h
@@ -1,8 +1,8 @@
1#define MOVE(M, c) compose(c, MOVE_CUBE_ ## M) 1#define MOVE(M, c) compose(c, MOVE_CUBE_ ## M)
2#define PREMOVE(M, c) compose(MOVE_CUBE_ ## M, c) 2#define PREMOVE(M, c) compose(MOVE_CUBE_ ## M, c)
3 3
4STATIC_INLINE bool allowednextmove(uint8_t *, uint8_t); 4STATIC_INLINE bool allowednextmove(size_t n, const uint8_t [n]);
5STATIC_INLINE uint32_t allowednextmove_mask(uint8_t *, uint8_t); 5STATIC_INLINE uint32_t allowednextmove_mask(size_t n, const uint8_t [n]);
6 6
7STATIC_INLINE uint8_t movebase(uint8_t); 7STATIC_INLINE uint8_t movebase(uint8_t);
8STATIC_INLINE uint8_t moveaxis(uint8_t); 8STATIC_INLINE uint8_t moveaxis(uint8_t);
@@ -13,9 +13,9 @@ STATIC_INLINE uint32_t disable_moves(uint32_t, uint8_t);
13STATIC cube_t move(cube_t, uint8_t); 13STATIC cube_t move(cube_t, uint8_t);
14STATIC cube_t premove(cube_t, uint8_t); 14STATIC cube_t premove(cube_t, uint8_t);
15STATIC uint8_t inverse_move(uint8_t); 15STATIC uint8_t inverse_move(uint8_t);
16STATIC void invertmoves(uint8_t *, uint8_t, uint8_t *); 16STATIC void invertmoves(size_t n, const uint8_t [n], uint8_t [n]);
17STATIC void sortparallel(uint8_t *, uint8_t); 17STATIC void sortparallel(size_t n, uint8_t [n]);
18STATIC bool are_lastmoves_singlecw(int n, uint8_t [n]); 18STATIC bool are_lastmoves_singlecw(size_t n, uint8_t [n]);
19 19
20STATIC int readmoves(const char *, int, uint8_t *); 20STATIC int readmoves(const char *, int, uint8_t *);
21STATIC cube_t applymoves(cube_t, const char *); 21STATIC cube_t applymoves(cube_t, const char *);
@@ -40,14 +40,13 @@ STATIC cube_t applymoves(cube_t, const char *);
40 } 40 }
41 41
42STATIC bool 42STATIC bool
43allowednextmove(uint8_t *moves, uint8_t n) 43allowednextmove(size_t n, const uint8_t moves[n])
44{ 44{
45 return n == 0 ? true : 45 return n == 0 || allowednextmove_mask(n-1, moves) & (1 << moves[n-1]);
46 allowednextmove_mask(moves, n-1) & (1 << moves[n-1]);
47} 46}
48 47
49STATIC uint32_t 48STATIC uint32_t
50allowednextmove_mask(uint8_t *moves, uint8_t n) 49allowednextmove_mask(size_t n, const uint8_t moves[n])
51{ 50{
52 uint32_t result; 51 uint32_t result;
53 uint8_t base1, base2, axis1, axis2; 52 uint8_t base1, base2, axis1, axis2;
@@ -79,7 +78,7 @@ allowednextmove_mask(uint8_t *moves, uint8_t n)
79STATIC_INLINE uint32_t 78STATIC_INLINE uint32_t
80disable_moves(uint32_t current_result, uint8_t base_index) 79disable_moves(uint32_t current_result, uint8_t base_index)
81{ 80{
82 return current_result & ~(7 << base_index); 81 return current_result & ~MM_SIDE(base_index);
83} 82}
84 83
85STATIC_INLINE uint8_t 84STATIC_INLINE uint8_t
@@ -236,17 +235,17 @@ TODO check if the issue is resolved
236#pragma GCC push_options 235#pragma GCC push_options
237#pragma GCC optimize ("O2") 236#pragma GCC optimize ("O2")
238STATIC void 237STATIC void
239invertmoves(uint8_t *moves, uint8_t nmoves, uint8_t *ret) 238invertmoves(size_t n, const uint8_t moves[n], uint8_t ret[n])
240{ 239{
241 uint8_t i; 240 uint8_t i;
242 241
243 for (i = 0; i < nmoves; i++) 242 for (i = 0; i < n; i++)
244 ret[i] = inverse_move(moves[nmoves - i - 1]); 243 ret[i] = inverse_move(moves[n - i - 1]);
245} 244}
246#pragma GCC pop_options 245#pragma GCC pop_options
247 246
248STATIC void 247STATIC void
249sortparallel(uint8_t *moves, uint8_t n) 248sortparallel(size_t n, uint8_t moves[n])
250{ 249{
251 uint8_t i; 250 uint8_t i;
252 251
@@ -257,7 +256,7 @@ sortparallel(uint8_t *moves, uint8_t n)
257} 256}
258 257
259STATIC bool 258STATIC bool
260are_lastmoves_singlecw(int n, uint8_t moves[n]) 259are_lastmoves_singlecw(size_t n, uint8_t moves[n])
261{ 260{
262 bool two; 261 bool two;
263 262
diff --git a/src/solvers/coord/common.h b/src/solvers/coord/common.h
index d570b6d..8553658 100644
--- a/src/solvers/coord/common.h
+++ b/src/solvers/coord/common.h
@@ -4,9 +4,9 @@ coord_t *all_coordinates[] = {
4}; 4};
5 5
6STATIC void append_coord_name(const coord_t *, char *); 6STATIC void append_coord_name(const coord_t *, char *);
7STATIC coord_t *parse_coord(const char *, int); 7STATIC coord_t *parse_coord(size_t n, const char [n]);
8STATIC uint8_t parse_axis(const char *, int); 8STATIC uint8_t parse_axis(size_t n, const char [n]);
9STATIC void parse_coord_and_axis(const char *, int, coord_t **, uint8_t *); 9STATIC void parse_coord_and_axis(size_t n, const char [n], coord_t **, uint8_t *);
10STATIC int64_t dataid_coord(const char *, char [static NISSY_DATAID_SIZE]); 10STATIC int64_t dataid_coord(const char *, char [static NISSY_DATAID_SIZE]);
11 11
12STATIC void 12STATIC void
@@ -22,7 +22,7 @@ append_coord_name(const coord_t *coord, char *str)
22} 22}
23 23
24STATIC coord_t * 24STATIC coord_t *
25parse_coord(const char *coord, int n) 25parse_coord(size_t n, const char coord[n])
26{ 26{
27 int i; 27 int i;
28 28
@@ -34,7 +34,7 @@ parse_coord(const char *coord, int n)
34} 34}
35 35
36STATIC uint8_t 36STATIC uint8_t
37parse_axis(const char *axis, int n) 37parse_axis(size_t n, const char axis[n])
38{ 38{
39 if (!strncmp(axis, "UD", n) || !strncmp(axis, "DU", n)) { 39 if (!strncmp(axis, "UD", n) || !strncmp(axis, "DU", n)) {
40 return AXIS_UD; 40 return AXIS_UD;
@@ -48,19 +48,24 @@ parse_axis(const char *axis, int n)
48} 48}
49 49
50STATIC void 50STATIC void
51parse_coord_and_axis(const char *str, int n, coord_t **coord, uint8_t *axis) 51parse_coord_and_axis(
52 size_t n,
53 const char str[n],
54 coord_t **coord,
55 uint8_t *axis
56)
52{ 57{
53 int i; 58 size_t i;
54 59
55 for (i = 0; i < n; i++) 60 for (i = 0; i < n; i++)
56 if (str[i] == '_') 61 if (str[i] == '_')
57 break; 62 break;
58 63
59 if (coord != NULL) 64 if (coord != NULL)
60 *coord = parse_coord(str, i); 65 *coord = parse_coord(i, str);
61 66
62 if (axis != NULL) 67 if (axis != NULL)
63 *axis = i == n ? UINT8_ERROR : parse_axis(str+i+1, n-i-1); 68 *axis = i == n ? UINT8_ERROR : parse_axis(n-i-1, str+i+1);
64} 69}
65 70
66STATIC int64_t 71STATIC int64_t
@@ -68,7 +73,7 @@ dataid_coord(const char *ca, char dataid[static NISSY_DATAID_SIZE])
68{ 73{
69 coord_t *c; 74 coord_t *c;
70 75
71 parse_coord_and_axis(ca, strlen(ca), &c, NULL); 76 parse_coord_and_axis(strlen(ca), ca, &c, NULL);
72 77
73 if (c == NULL) { 78 if (c == NULL) {
74 LOG("dataid_coord: cannot parse coordinate from '%s'\n", ca); 79 LOG("dataid_coord: cannot parse coordinate from '%s'\n", ca);
diff --git a/src/solvers/coord/gendata.h b/src/solvers/coord/gendata.h
index 93ffc65..5819608 100644
--- a/src/solvers/coord/gendata.h
+++ b/src/solvers/coord/gendata.h
@@ -11,7 +11,7 @@ gendata_coord_dispatch(const char *coordstr, void *buf)
11{ 11{
12 coord_t *coord; 12 coord_t *coord;
13 13
14 parse_coord_and_axis(coordstr, strlen(coordstr), &coord, NULL); 14 parse_coord_and_axis(strlen(coordstr), coordstr, &coord, NULL);
15 15
16 if (coord == NULL) { 16 if (coord == NULL) {
17 LOG("Could not parse coordinate '%s'\n", coord); 17 LOG("Could not parse coordinate '%s'\n", coord);
@@ -137,7 +137,7 @@ getdistribution_coord(
137 137
138 memset(distr, 0, INFO_DISTRIBUTION_LEN * sizeof(uint64_t)); 138 memset(distr, 0, INFO_DISTRIBUTION_LEN * sizeof(uint64_t));
139 139
140 if((c = parse_coord(coord, strlen(coord))) == NULL) 140 if((c = parse_coord(strlen(coord), coord)) == NULL)
141 return; 141 return;
142 142
143 for (i = 0; i < c->max; i++) { 143 for (i = 0; i < c->max; i++) {
diff --git a/src/solvers/coord/solve.h b/src/solvers/coord/solve.h
index 6903d1e..fa160f8 100644
--- a/src/solvers/coord/solve.h
+++ b/src/solvers/coord/solve.h
@@ -1,10 +1,8 @@
1#define MAXLEN_COORDSOL 20
2
3typedef struct { 1typedef struct {
4 cube_t cube; 2 cube_t cube;
5 uint8_t depth; 3 uint8_t depth;
6 uint8_t nmoves; 4 uint8_t nmoves;
7 uint8_t moves[MAXLEN_COORDSOL]; 5 uint8_t moves[MAXLEN];
8 coord_t *coord; 6 coord_t *coord;
9 const void *coord_data; 7 const void *coord_data;
10 const uint8_t *ptable; 8 const uint8_t *ptable;
@@ -13,23 +11,22 @@ typedef struct {
13 int64_t maxsolutions; 11 int64_t maxsolutions;
14 int optimal; 12 int optimal;
15 uint8_t *shortest_sol; 13 uint8_t *shortest_sol;
16 uint64_t solutions_size; 14 size_t solutions_size;
17 uint64_t *solutions_used; 15 size_t *solutions_used;
18 char **solutions; 16 char **solutions;
19} dfsarg_solve_coord_t; 17} dfsarg_solve_coord_t;
20 18
21STATIC int64_t solve_coord(cube_t, coord_t *, uint8_t, uint8_t, uint8_t, 19STATIC int64_t solve_coord(cube_t, coord_t *, uint8_t, uint8_t, uint8_t,
22 uint8_t, uint64_t, int, int, uint64_t, const void *, uint64_t, char *); 20 uint8_t, uint64_t, int, int, uint64_t, const void *, size_t, char *);
23STATIC int64_t solve_coord_dispatch(cube_t, const char *, uint8_t, uint8_t, 21STATIC int64_t solve_coord_dispatch(cube_t, const char *, uint8_t, uint8_t,
24 uint8_t, uint64_t, int, int, uint64_t, const void *, uint64_t, char *); 22 uint8_t, uint64_t, int, int, uint64_t, const void *, size_t, char *);
25STATIC bool solve_coord_appendchar(char *, uint64_t, uint64_t *, char);
26STATIC int64_t solve_coord_appendsolution(dfsarg_solve_coord_t *); 23STATIC int64_t solve_coord_appendsolution(dfsarg_solve_coord_t *);
27STATIC int64_t solve_coord_dfs(dfsarg_solve_coord_t *); 24STATIC int64_t solve_coord_dfs(dfsarg_solve_coord_t *);
28 25
29STATIC int64_t 26STATIC int64_t
30solve_coord_appendsolution(dfsarg_solve_coord_t *arg) 27solve_coord_appendsolution(dfsarg_solve_coord_t *arg)
31{ 28{
32 uint8_t i, t, tmoves[MAXLEN_COORDSOL]; 29 uint8_t i, t, tmoves[MAXLEN];
33 int64_t strl; 30 int64_t strl;
34 uint64_t l; 31 uint64_t l;
35 char *m; 32 char *m;
@@ -44,18 +41,18 @@ solve_coord_appendsolution(dfsarg_solve_coord_t *arg)
44 for (i = 0; i < arg->nmoves; i++) 41 for (i = 0; i < arg->nmoves; i++)
45 tmoves[i] = transform_move(arg->moves[i], t); 42 tmoves[i] = transform_move(arg->moves[i], t);
46 43
47 sortparallel(tmoves, arg->nmoves); 44 sortparallel(arg->nmoves, tmoves);
48 45
49 l = arg->solutions_size - *arg->solutions_used; 46 l = arg->solutions_size - *arg->solutions_used;
50 m = *arg->solutions + *arg->solutions_used; 47 m = *arg->solutions + *arg->solutions_used;
51 strl = writemoves(tmoves, arg->nmoves, l, m); 48 strl = writemoves(arg->nmoves, tmoves, l, m);
52 if (strl < 0) 49 if (strl < 0)
53 goto solve_coord_appendsolution_error; 50 goto solve_coord_appendsolution_error;
54 51
55 *arg->solutions_used += MAX(0, strl-1); 52 *arg->solutions_used += MAX(0, strl-1);
56 53
57 if (!solve_coord_appendchar( 54 if (!appendchar(
58 *arg->solutions, arg->solutions_size, arg->solutions_used, '\n')) 55 arg->solutions_size, *arg->solutions, arg->solutions_used, '\n'))
59 goto solve_coord_appendsolution_error; 56 goto solve_coord_appendsolution_error;
60 57
61 (*arg->nsols)++; 58 (*arg->nsols)++;
@@ -68,18 +65,6 @@ solve_coord_appendsolution_error:
68 return NISSY_ERROR_BUFFER_SIZE; 65 return NISSY_ERROR_BUFFER_SIZE;
69} 66}
70 67
71STATIC bool
72solve_coord_appendchar(char *s, uint64_t s_size, uint64_t *s_used, char c)
73{
74 if (s_size == *s_used)
75 return false;
76
77 s[*s_used] = c;
78 (*s_used)++;
79
80 return true;
81}
82
83STATIC int64_t 68STATIC int64_t
84solve_coord_dfs(dfsarg_solve_coord_t *arg) 69solve_coord_dfs(dfsarg_solve_coord_t *arg)
85{ 70{
@@ -104,7 +89,7 @@ solve_coord_dfs(dfsarg_solve_coord_t *arg)
104 backup_cube = arg->cube; 89 backup_cube = arg->cube;
105 90
106 ret = 0; 91 ret = 0;
107 mm = allowednextmove_mask(arg->moves, arg->nmoves); 92 mm = allowednextmove_mask(arg->nmoves, arg->moves);
108 arg->nmoves++; 93 arg->nmoves++;
109 for (m = 0; m < 18; m++) { 94 for (m = 0; m < 18; m++) {
110 if (!(mm & (1 << m))) 95 if (!(mm & (1 << m)))
@@ -135,7 +120,7 @@ solve_coord_dispatch(
135 int threads, 120 int threads,
136 uint64_t data_size, 121 uint64_t data_size,
137 const void *data, 122 const void *data,
138 uint64_t sols_size, 123 size_t solutions_size,
139 char *sols 124 char *sols
140) 125)
141{ 126{
@@ -143,7 +128,7 @@ solve_coord_dispatch(
143 uint8_t axis; 128 uint8_t axis;
144 129
145 parse_coord_and_axis( 130 parse_coord_and_axis(
146 coord_and_axis, strlen(coord_and_axis), &coord, &axis); 131 strlen(coord_and_axis), coord_and_axis, &coord, &axis);
147 132
148 if (coord == NULL) { 133 if (coord == NULL) {
149 LOG("Could not parse coordinate from '%s'\n", coord_and_axis); 134 LOG("Could not parse coordinate from '%s'\n", coord_and_axis);
@@ -156,7 +141,8 @@ solve_coord_dispatch(
156 } 141 }
157 142
158 return solve_coord(cube, coord, axis, nissflag, minmoves, maxmoves, 143 return solve_coord(cube, coord, axis, nissflag, minmoves, maxmoves,
159 maxsolutions, optimal, threads, data_size, data, sols_size, sols); 144 maxsolutions, optimal, threads, data_size, data,
145 solutions_size, sols);
160} 146}
161 147
162STATIC int64_t 148STATIC int64_t
@@ -172,14 +158,14 @@ solve_coord(
172 int threads, 158 int threads,
173 uint64_t data_size, 159 uint64_t data_size,
174 const void *data, 160 const void *data,
175 uint64_t sols_size, 161 size_t solutions_size,
176 char *sols 162 char *sols
177) 163)
178{ 164{
179 int8_t d; 165 int8_t d;
180 uint8_t t, shortest_sol; 166 uint8_t t, shortest_sol;
181 int64_t nsols, ndepth; 167 int64_t nsols, ndepth;
182 uint64_t sols_used; 168 size_t solutions_used;
183 cube_t c; 169 cube_t c;
184 const void *coord_data; 170 const void *coord_data;
185 const uint8_t *ptable; 171 const uint8_t *ptable;
@@ -200,8 +186,8 @@ solve_coord(
200 } 186 }
201 187
202 nsols = 0; 188 nsols = 0;
203 sols_used = 0; 189 solutions_used = 0;
204 shortest_sol = MAXLEN_COORDSOL + 1; 190 shortest_sol = MAXLEN + 1;
205 t = coord->axistrans[axis]; 191 t = coord->axistrans[axis];
206 c = transform(cube, t); 192 c = transform(cube, t);
207 193
@@ -215,16 +201,15 @@ solve_coord(
215 .maxsolutions = (int64_t)maxsolutions, 201 .maxsolutions = (int64_t)maxsolutions,
216 .optimal = optimal, 202 .optimal = optimal,
217 .shortest_sol = &shortest_sol, 203 .shortest_sol = &shortest_sol,
218 .solutions_size = sols_size, 204 .solutions_size = solutions_size,
219 .solutions_used = &sols_used, 205 .solutions_used = &solutions_used,
220 .solutions = &sols, 206 .solutions = &sols,
221 }; 207 };
222 208
223 if (coord->coord(c, coord_data) == 0) { 209 if (coord->coord(c, coord_data) == 0) {
224 if (minmoves == 0) { 210 if (minmoves == 0) {
225 nsols = 1; 211 nsols = 1;
226 if (!solve_coord_appendchar( 212 if (!appendchar(solutions_size, sols, &solutions_used, '\n'))
227 sols, sols_size, &sols_used, '\n'))
228 goto solve_coord_error_buffer; 213 goto solve_coord_error_buffer;
229 } 214 }
230 goto solve_coord_done; 215 goto solve_coord_done;
@@ -254,7 +239,7 @@ solve_coord(
254 } 239 }
255 240
256solve_coord_done: 241solve_coord_done:
257 if (!solve_coord_appendchar(sols, sols_size, &sols_used, '\0')) 242 if (!appendchar(solutions_size, sols, &solutions_used, '\0'))
258 goto solve_coord_error_buffer; 243 goto solve_coord_error_buffer;
259 244
260 return nsols; 245 return nsols;
diff --git a/src/solvers/coord/types_macros.h b/src/solvers/coord/types_macros.h
index 2309efc..f2d436c 100644
--- a/src/solvers/coord/types_macros.h
+++ b/src/solvers/coord/types_macros.h
@@ -11,5 +11,5 @@ typedef struct {
11 uint32_t moves_mask; 11 uint32_t moves_mask;
12 uint64_t trans_mask; 12 uint64_t trans_mask;
13 uint8_t axistrans[3]; 13 uint8_t axistrans[3];
14 bool (*is_admissible)(int n, uint8_t [n]); 14 bool (*is_admissible)(size_t n, uint8_t [n]);
15} coord_t; 15} coord_t;
diff --git a/src/solvers/h48/gendata_h48.h b/src/solvers/h48/gendata_h48.h
index 55db1be..2d9a9f7 100644
--- a/src/solvers/h48/gendata_h48.h
+++ b/src/solvers/h48/gendata_h48.h
@@ -533,7 +533,7 @@ gendata_h48k2_dfs(h48k2_dfs_arg_t *arg)
533 /* Depth d+3 */ 533 /* Depth d+3 */
534 for (m[2] = 0; m[2] < 18; m[2]++) { 534 for (m[2] = 0; m[2] < 18; m[2]++) {
535 markarg.depth = d+3; 535 markarg.depth = d+3;
536 if (!allowednextmove(m, 3)) { 536 if (!allowednextmove(3, m)) {
537 m[2] += 2; 537 m[2] += 2;
538 continue; 538 continue;
539 } 539 }
@@ -548,7 +548,7 @@ gendata_h48k2_dfs(h48k2_dfs_arg_t *arg)
548 /* Depth d+4 */ 548 /* Depth d+4 */
549 for (m[3] = 0; m[3] < 18; m[3]++) { 549 for (m[3] = 0; m[3] < 18; m[3]++) {
550 markarg.depth = d+4; 550 markarg.depth = d+4;
551 if (!allowednextmove(m, 4)) { 551 if (!allowednextmove(4, m)) {
552 m[3] += 2; 552 m[3] += 2;
553 continue; 553 continue;
554 } 554 }
diff --git a/src/solvers/h48/gendata_types_macros.h b/src/solvers/h48/gendata_types_macros.h
index a2026b2..ec6576a 100644
--- a/src/solvers/h48/gendata_types_macros.h
+++ b/src/solvers/h48/gendata_types_macros.h
@@ -28,7 +28,6 @@
28#define H48_SHIFT(i, k) ((uint8_t)(k) * (uint8_t)((i) % H48_COEFF(k))) 28#define H48_SHIFT(i, k) ((uint8_t)(k) * (uint8_t)((i) % H48_COEFF(k)))
29#define H48_MASK(i, k) ((UINT8_BIT(k) - UINT8_C(1)) << H48_SHIFT(i, k)) 29#define H48_MASK(i, k) ((UINT8_BIT(k) - UINT8_C(1)) << H48_SHIFT(i, k))
30 30
31#define MAXLEN 20
32#define CHUNKS COCSEP_CLASSES 31#define CHUNKS COCSEP_CLASSES
33 32
34/* 33/*
diff --git a/src/solvers/h48/solve.h b/src/solvers/h48/solve.h
index 182beee..f7565d5 100644
--- a/src/solvers/h48/solve.h
+++ b/src/solvers/h48/solve.h
@@ -32,8 +32,8 @@ typedef struct {
32 const uint8_t *h48data; 32 const uint8_t *h48data;
33 const uint8_t *h48data_fallback_h0k4; 33 const uint8_t *h48data_fallback_h0k4;
34 const void *h48data_fallback_eoesep; 34 const void *h48data_fallback_eoesep;
35 uint64_t solutions_size; 35 size_t solutions_size;
36 uint64_t *solutions_used; 36 size_t *solutions_used;
37 char **solutions; 37 char **solutions;
38 uint32_t movemask_normal; 38 uint32_t movemask_normal;
39 uint32_t movemask_inverse; 39 uint32_t movemask_inverse;
@@ -58,7 +58,6 @@ typedef struct {
58 58
59STATIC int64_t solve_h48_appendsolution(dfsarg_solve_h48_t *); 59STATIC int64_t solve_h48_appendsolution(dfsarg_solve_h48_t *);
60STATIC int64_t solve_h48_appendallsym(dfsarg_solve_h48_t *); 60STATIC int64_t solve_h48_appendallsym(dfsarg_solve_h48_t *);
61STATIC bool solve_h48_appendchar(dfsarg_solve_h48_t *, char);
62STATIC_INLINE bool solve_h48_stop(dfsarg_solve_h48_t *); 61STATIC_INLINE bool solve_h48_stop(dfsarg_solve_h48_t *);
63STATIC int64_t solve_h48_maketasks( 62STATIC int64_t solve_h48_maketasks(
64 dfsarg_solve_h48_t *, dfsarg_solve_h48_maketasks_t *, 63 dfsarg_solve_h48_t *, dfsarg_solve_h48_maketasks_t *,
@@ -66,7 +65,7 @@ STATIC int64_t solve_h48_maketasks(
66STATIC void *solve_h48_runthread(void *); 65STATIC void *solve_h48_runthread(void *);
67STATIC int64_t solve_h48_dfs(dfsarg_solve_h48_t *); 66STATIC int64_t solve_h48_dfs(dfsarg_solve_h48_t *);
68STATIC int64_t solve_h48(cube_t, int8_t, int8_t, uint64_t, int8_t, int8_t, 67STATIC int64_t solve_h48(cube_t, int8_t, int8_t, uint64_t, int8_t, int8_t,
69 uint64_t, const void *, uint64_t, char *, 68 uint64_t, const void *, size_t, char *,
70 long long [static NISSY_SIZE_SOLVE_STATS]); 69 long long [static NISSY_SIZE_SOLVE_STATS]);
71 70
72STATIC int64_t 71STATIC int64_t
@@ -76,15 +75,15 @@ solve_h48_appendsolution(dfsarg_solve_h48_t *arg)
76 arg->nmoves + arg->npremoves > *arg->shortest_sol + arg->optimal) 75 arg->nmoves + arg->npremoves > *arg->shortest_sol + arg->optimal)
77 return 0; 76 return 0;
78 77
79 invertmoves(arg->premoves, arg->npremoves, arg->moves + arg->nmoves); 78 invertmoves(arg->npremoves, arg->premoves, arg->moves + arg->nmoves);
80 79
81 /* Sort parallel moves for consistency */ 80 /* Sort parallel moves for consistency */
82 sortparallel(arg->moves, arg->nmoves + arg->npremoves); 81 sortparallel(arg->nmoves + arg->npremoves, arg->moves);
83 82
84 /* Do not append the solution in case premoves cancel with normal */ 83 /* Do not append the solution in case premoves cancel with normal */
85 if (arg->npremoves > 0 && !allowednextmove(arg->moves, arg->nmoves+1)) 84 if (arg->npremoves > 0 && !allowednextmove(arg->nmoves+1, arg->moves))
86 return 0; 85 return 0;
87 if (arg->npremoves > 1 && !allowednextmove(arg->moves, arg->nmoves+2)) 86 if (arg->npremoves > 1 && !allowednextmove(arg->nmoves+2, arg->moves))
88 return 0; 87 return 0;
89 88
90 return solve_h48_appendallsym(arg); 89 return solve_h48_appendallsym(arg);
@@ -109,7 +108,7 @@ solve_h48_appendallsym(dfsarg_solve_h48_t *arg)
109 all[j][i] = transform_move(arg->moves[i], t); 108 all[j][i] = transform_move(arg->moves[i], t);
110 109
111 /* Sort parallel moves for consistency */ 110 /* Sort parallel moves for consistency */
112 sortparallel(all[j], n); 111 sortparallel(n, all[j]);
113 112
114 /* Check for duplicate solutions */ 113 /* Check for duplicate solutions */
115 for (k = 0; k < j; k++) { 114 for (k = 0; k < j; k++) {
@@ -133,7 +132,7 @@ solve_h48_appendallsym(dfsarg_solve_h48_t *arg)
133 for (k = 0; k < j && *arg->nsols < arg->maxsolutions; k++) { 132 for (k = 0; k < j && *arg->nsols < arg->maxsolutions; k++) {
134 l = arg->solutions_size - *arg->solutions_used; 133 l = arg->solutions_size - *arg->solutions_used;
135 m = *arg->solutions + *arg->solutions_used; 134 m = *arg->solutions + *arg->solutions_used;
136 strl = writemoves(all[k], n, l, m); 135 strl = writemoves(n, all[k], l, m);
137 if (strl < 0) 136 if (strl < 0)
138 goto solve_h48_appendallsym_error; 137 goto solve_h48_appendallsym_error;
139 138
@@ -141,7 +140,8 @@ solve_h48_appendallsym(dfsarg_solve_h48_t *arg)
141 140
142 *arg->solutions_used += MAX(0, strl-1); 141 *arg->solutions_used += MAX(0, strl-1);
143 142
144 if (!solve_h48_appendchar(arg, '\n')) 143 if (!appendchar(arg->solutions_size,
144 *arg->solutions, arg->solutions_used, '\n'))
145 goto solve_h48_appendallsym_error; 145 goto solve_h48_appendallsym_error;
146 146
147 (*arg->nsols)++; 147 (*arg->nsols)++;
@@ -156,18 +156,6 @@ solve_h48_appendallsym_error:
156 return NISSY_ERROR_BUFFER_SIZE; 156 return NISSY_ERROR_BUFFER_SIZE;
157} 157}
158 158
159STATIC bool
160solve_h48_appendchar(dfsarg_solve_h48_t *arg, char c)
161{
162 if (arg->solutions_size <= *arg->solutions_used)
163 return false;
164
165 *(*arg->solutions + *arg->solutions_used) = c;
166 (*arg->solutions_used)++;
167
168 return true;
169}
170
171STATIC_INLINE bool 159STATIC_INLINE bool
172solve_h48_stop(dfsarg_solve_h48_t *arg) 160solve_h48_stop(dfsarg_solve_h48_t *arg)
173{ 161{
@@ -283,9 +271,9 @@ solve_h48_dfs(dfsarg_solve_h48_t *arg)
283 ulbi = arg->use_lb_inverse; 271 ulbi = arg->use_lb_inverse;
284 272
285 ret = 0; 273 ret = 0;
286 mm_normal = allowednextmove_mask(arg->moves, arg->nmoves) & 274 mm_normal = allowednextmove_mask(arg->nmoves, arg->moves) &
287 arg->movemask_normal; 275 arg->movemask_normal;
288 mm_inverse = allowednextmove_mask(arg->premoves, arg->npremoves) & 276 mm_inverse = allowednextmove_mask(arg->npremoves, arg->premoves) &
289 arg->movemask_inverse; 277 arg->movemask_inverse;
290 if (popcount_u32(mm_normal) <= popcount_u32(mm_inverse)) { 278 if (popcount_u32(mm_normal) <= popcount_u32(mm_inverse)) {
291 arg->nmoves++; 279 arg->nmoves++;
@@ -397,7 +385,7 @@ solve_h48_maketasks(
397 return NISSY_OK; 385 return NISSY_OK;
398 } 386 }
399 387
400 mm = allowednextmove_mask(maketasks_arg->moves, maketasks_arg->nmoves); 388 mm = allowednextmove_mask(maketasks_arg->nmoves, maketasks_arg->moves);
401 389
402 maketasks_arg->nmoves++; 390 maketasks_arg->nmoves++;
403 backup_cube = maketasks_arg->cube; 391 backup_cube = maketasks_arg->cube;
@@ -435,7 +423,7 @@ solve_h48(
435 int8_t threads, 423 int8_t threads,
436 uint64_t data_size, 424 uint64_t data_size,
437 const void *data, 425 const void *data,
438 uint64_t solutions_size, 426 size_t solutions_size,
439 char *solutions, 427 char *solutions,
440 long long stats[static NISSY_SIZE_SOLVE_STATS] 428 long long stats[static NISSY_SIZE_SOLVE_STATS]
441) 429)
@@ -447,7 +435,8 @@ solve_h48(
447 solve_h48_task_t tasks[STARTING_CUBES]; 435 solve_h48_task_t tasks[STARTING_CUBES];
448 dfsarg_solve_h48_maketasks_t maketasks_arg; 436 dfsarg_solve_h48_maketasks_t maketasks_arg;
449 long double fallback_rate, lookups_per_node; 437 long double fallback_rate, lookups_per_node;
450 uint64_t solutions_used, symmask, offset; 438 uint64_t symmask, offset;
439 size_t solutions_used;
451 int64_t nodes_visited, table_lookups, table_fallbacks; 440 int64_t nodes_visited, table_lookups, table_fallbacks;
452 tableinfo_t info, fbinfo, fbinfo2; 441 tableinfo_t info, fbinfo, fbinfo2;
453 const uint32_t *cocsepdata; 442 const uint32_t *cocsepdata;
@@ -561,7 +550,8 @@ solve_h48(
561 } 550 }
562 551
563solve_h48_done: 552solve_h48_done:
564 if (!solve_h48_appendchar(&arg[0], '\0')) 553 if (!appendchar(arg[0].solutions_size, *arg[0].solutions,
554 arg[0].solutions_used, '\0'))
565 goto solve_h48_error_solutions_buffer; 555 goto solve_h48_error_solutions_buffer;
566 556
567 nodes_visited = table_lookups = table_fallbacks = 0; 557 nodes_visited = table_lookups = table_fallbacks = 0;
diff --git a/src/solvers/solutions.h b/src/solvers/solutions.h
new file mode 100644
index 0000000..1396210
--- /dev/null
+++ b/src/solvers/solutions.h
@@ -0,0 +1,14 @@
1#define MAXLEN 20
2
3STATIC bool appendchar(size_t n, char [n], size_t *, char);
4
5STATIC bool
6appendchar(size_t n, char s[n], size_t *used, char c)
7{
8 if (n <= *used)
9 return false;
10
11 s[(*used)++] = c;
12
13 return true;
14}
diff --git a/src/solvers/solvers.h b/src/solvers/solvers.h
index 7ef1746..42184b9 100644
--- a/src/solvers/solvers.h
+++ b/src/solvers/solvers.h
@@ -1,3 +1,4 @@
1#include "solutions.h"
1#include "tables.h" 2#include "tables.h"
2#include "h48/h48.h" 3#include "h48/h48.h"
3#include "coord/coord.h" 4#include "coord/coord.h"
diff --git a/src/utils/constants.h b/src/utils/constants.h
index 1db1940..97bf436 100644
--- a/src/utils/constants.h
+++ b/src/utils/constants.h
@@ -102,8 +102,10 @@ STATIC int64_t binomial[12][12] = {
102 102
103#define MM_ALLMOVES UINT32_C(0x3FFFF) 103#define MM_ALLMOVES UINT32_C(0x3FFFF)
104#define MM_NOHALFTURNS UINT32_C(0x2DB6D) 104#define MM_NOHALFTURNS UINT32_C(0x2DB6D)
105#define MM_SIDE(m) (UINT32_C(7) << (uint32_t)(m))
105 106
106#define TM_ALLTRANS UINT64_C(0xFFFFFFFFFFFF) 107#define TM_ALLTRANS UINT64_C(0xFFFFFFFFFFFF)
108#define TM_SINGLE_UFr UINT64_C(1)
107 109
108#define CORNER_UFR UINT8_C(0) 110#define CORNER_UFR UINT8_C(0)
109#define CORNER_UBL UINT8_C(1) 111#define CORNER_UBL UINT8_C(1)
diff --git a/src/utils/math.h b/src/utils/math.h
index 1ab51e6..21fc60b 100644
--- a/src/utils/math.h
+++ b/src/utils/math.h
@@ -4,12 +4,12 @@
4#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) 4#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
5 5
6STATIC int64_t factorial(int64_t); 6STATIC int64_t factorial(int64_t);
7STATIC bool isperm(uint8_t *, int64_t); 7STATIC bool isperm(size_t n, const uint8_t [n]);
8STATIC int64_t permtoindex(uint8_t *, int64_t); 8STATIC int64_t permtoindex(size_t n, const uint8_t [n]);
9STATIC void indextoperm(int64_t, int64_t, uint8_t *); 9STATIC void indextoperm(int64_t, size_t n, uint8_t [n]);
10STATIC int permsign(uint8_t *, int64_t); 10STATIC int permsign(size_t n, const uint8_t [n]);
11STATIC int64_t digitstosumzero(uint8_t *, uint8_t, uint8_t); 11STATIC int64_t digitstosumzero(size_t n, const uint8_t [n], uint8_t);
12STATIC void sumzerotodigits(int64_t, uint8_t, uint8_t, uint8_t *); 12STATIC void sumzerotodigits(int64_t, size_t n, uint8_t, uint8_t [n]);
13 13
14STATIC int64_t 14STATIC int64_t
15factorial(int64_t n) 15factorial(int64_t n)
@@ -32,13 +32,13 @@ factorial(int64_t n)
32} 32}
33 33
34STATIC bool 34STATIC bool
35isperm(uint8_t *a, int64_t n) 35isperm(size_t n, const uint8_t a[n])
36{ 36{
37 int64_t i; 37 size_t i;
38 bool aux[FACTORIAL_MAX+1]; 38 bool aux[FACTORIAL_MAX+1];
39 39
40 if (n > FACTORIAL_MAX) { 40 if (n > (size_t)FACTORIAL_MAX) {
41 LOG("Error: won't compute 'isperm()' for n=%" PRId64 " because" 41 LOG("Error: won't compute 'isperm()' for n=%zu because"
42 " it is larger than %" PRId64 "\n", n, FACTORIAL_MAX); 42 " it is larger than %" PRId64 "\n", n, FACTORIAL_MAX);
43 return false; 43 return false;
44 } 44 }
@@ -60,18 +60,18 @@ isperm(uint8_t *a, int64_t n)
60} 60}
61 61
62STATIC int64_t 62STATIC int64_t
63permtoindex(uint8_t *a, int64_t n) 63permtoindex(size_t n, const uint8_t a[n])
64{ 64{
65 int64_t i, j, c, ret; 65 size_t i, j;
66 int64_t c, ret;
66 67
67 if (n > FACTORIAL_MAX) { 68 if (n > (size_t)FACTORIAL_MAX) {
68 LOG("Error: won't compute 'permtoindex()' for n=%" PRId64 69 LOG("Error: won't compute 'permtoindex()' for n=%zu because "
69 " because it is larger than %" PRId64 "\n", 70 "it is larger than %" PRId64 "\n", n, FACTORIAL_MAX);
70 n, FACTORIAL_MAX);
71 return -1; 71 return -1;
72 } 72 }
73 73
74 if (!isperm(a, n)) 74 if (!isperm(n, a))
75 return -1; 75 return -1;
76 76
77 for (i = 0, ret = 0; i < n; i++) { 77 for (i = 0, ret = 0; i < n; i++) {
@@ -84,15 +84,15 @@ permtoindex(uint8_t *a, int64_t n)
84} 84}
85 85
86STATIC void 86STATIC void
87indextoperm(int64_t p, int64_t n, uint8_t *r) 87indextoperm(int64_t p, size_t n, uint8_t r[n])
88{ 88{
89 int64_t i, j, c; 89 int64_t c;
90 size_t i, j;
90 uint8_t a[FACTORIAL_MAX+1]; 91 uint8_t a[FACTORIAL_MAX+1];
91 92
92 if (n > FACTORIAL_MAX) { 93 if (n > FACTORIAL_MAX) {
93 LOG("Error: won't compute 'permtoindex()' for n=%" PRId64 94 LOG("Error: won't compute 'indextoperm()' for n=%zu because "
94 " because it is larger than %" PRId64 "\n", 95 "it is larger than %" PRId64 "\n", n, FACTORIAL_MAX);
95 n, FACTORIAL_MAX);
96 goto indextoperm_error; 96 goto indextoperm_error;
97 } 97 }
98 98
@@ -109,7 +109,7 @@ indextoperm(int64_t p, int64_t n, uint8_t *r)
109 p %= factorial(n-i-1); 109 p %= factorial(n-i-1);
110 } 110 }
111 111
112 if (!isperm(r, n)) 112 if (!isperm(n, r))
113 goto indextoperm_error; 113 goto indextoperm_error;
114 114
115 return; 115 return;
@@ -119,10 +119,9 @@ indextoperm_error:
119} 119}
120 120
121STATIC int 121STATIC int
122permsign(uint8_t *a, int64_t n) 122permsign(size_t n, const uint8_t a[n])
123{ 123{
124 int i, j; 124 size_t i, j, ret;
125 uint8_t ret;
126 125
127 for (i = 0, ret = 0; i < n; i++) 126 for (i = 0, ret = 0; i < n; i++)
128 for (j = i+1; j < n; j++) 127 for (j = i+1; j < n; j++)
@@ -132,13 +131,13 @@ permsign(uint8_t *a, int64_t n)
132} 131}
133 132
134STATIC int64_t 133STATIC int64_t
135digitstosumzero(uint8_t *a, uint8_t n, uint8_t b) 134digitstosumzero(size_t n, const uint8_t a[n], uint8_t b)
136{ 135{
137 int64_t ret, p; 136 int64_t ret, p;
138 uint8_t i, sum; 137 size_t i, sum;
139 138
140 if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) { 139 if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) {
141 LOG("Won't compute 'sumzero' for n=%" PRIu8 "and b=%" PRIu8 140 LOG("Won't compute 'sumzero' for n=%zu and b=%" PRIu8
142 " (use n=8 b=3 or n=12 b=2)\n", n, b); 141 " (use n=8 b=3 or n=12 b=2)\n", n, b);
143 return -1; 142 return -1;
144 } 143 }
@@ -162,15 +161,15 @@ digitstosumzero(uint8_t *a, uint8_t n, uint8_t b)
162} 161}
163 162
164STATIC void 163STATIC void
165sumzerotodigits(int64_t d, uint8_t n, uint8_t b, uint8_t *a) 164sumzerotodigits(int64_t d, size_t n, uint8_t b, uint8_t a[n])
166{ 165{
167 uint8_t sum; 166 uint8_t sum;
168 int64_t i; 167 size_t i;
169 168
170 if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) { 169 if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) {
171 LOG("Won't compute 'digits' for n=%" PRIu8 "and b=%" PRIu8 170 LOG("Won't compute 'digits' for n=%" PRIu8 "and b=%" PRIu8
172 " (use n=8 b=3 or n=12 b=2)\n"); 171 " (use n=8 b=3 or n=12 b=2)\n");
173 goto digitstosumzero_error; 172 goto sumzerotodigits_error;
174 } 173 }
175 174
176 for (i = 1, sum = 0; i < n; i++, d /= (int64_t)b) { 175 for (i = 1, sum = 0; i < n; i++, d /= (int64_t)b) {
@@ -181,6 +180,6 @@ sumzerotodigits(int64_t d, uint8_t n, uint8_t b, uint8_t *a)
181 180
182 return; 181 return;
183 182
184digitstosumzero_error: 183sumzerotodigits_error:
185 memset(a, UINT8_ERROR, n); 184 memset(a, UINT8_ERROR, n);
186} 185}
diff --git a/test/010_math_permtoindex/permtoindex_tests.c b/test/010_math_permtoindex/permtoindex_tests.c
index 98c8207..ff61945 100644
--- a/test/010_math_permtoindex/permtoindex_tests.c
+++ b/test/010_math_permtoindex/permtoindex_tests.c
@@ -1,6 +1,6 @@
1#include "../test.h" 1#include "../test.h"
2 2
3int64_t permtoindex(uint8_t *, int64_t); 3int64_t permtoindex(size_t n, const uint8_t [n]);
4 4
5void run(void) { 5void run(void) {
6 char str[STRLENMAX]; 6 char str[STRLENMAX];
@@ -14,6 +14,6 @@ void run(void) {
14 a[i] = atoi(str); 14 a[i] = atoi(str);
15 } 15 }
16 16
17 p = permtoindex(a, n); 17 p = permtoindex(n, a);
18 printf("%" PRId64 "\n", p); 18 printf("%" PRId64 "\n", p);
19} 19}
diff --git a/test/011_math_indextoperm/indextoperm_tests.c b/test/011_math_indextoperm/indextoperm_tests.c
index cb1306d..87399ef 100644
--- a/test/011_math_indextoperm/indextoperm_tests.c
+++ b/test/011_math_indextoperm/indextoperm_tests.c
@@ -1,6 +1,6 @@
1#include "../test.h" 1#include "../test.h"
2 2
3void indextoperm(int64_t, int64_t, uint8_t *); 3void indextoperm(int64_t, size_t n, uint8_t [n]);
4 4
5void run(void) { 5void run(void) {
6 char str[STRLENMAX]; 6 char str[STRLENMAX];
diff --git a/test/012_math_permsign/permsign_tests.c b/test/012_math_permsign/permsign_tests.c
index fa8ce9b..0ec22af 100644
--- a/test/012_math_permsign/permsign_tests.c
+++ b/test/012_math_permsign/permsign_tests.c
@@ -1,6 +1,6 @@
1#include "../test.h" 1#include "../test.h"
2 2
3int permsign(uint8_t *, int64_t); 3int permsign(size_t n, const uint8_t [n]);
4 4
5void run(void) { 5void run(void) {
6 char str[STRLENMAX]; 6 char str[STRLENMAX];
@@ -16,6 +16,6 @@ void run(void) {
16 a[i] = atoi(str); 16 a[i] = atoi(str);
17 } 17 }
18 18
19 p = permsign(a, n); 19 p = permsign(n, a);
20 printf("%d\n", p); 20 printf("%d\n", p);
21} 21}
diff --git a/test/013_math_digitstosumzero/digitstosumzero_tests.c b/test/013_math_digitstosumzero/digitstosumzero_tests.c
index f28c02a..795bd9b 100644
--- a/test/013_math_digitstosumzero/digitstosumzero_tests.c
+++ b/test/013_math_digitstosumzero/digitstosumzero_tests.c
@@ -1,10 +1,11 @@
1#include "../test.h" 1#include "../test.h"
2 2
3int64_t digitstosumzero(uint8_t *, uint8_t, uint8_t); 3int64_t digitstosumzero(size_t n, uint8_t [n], uint8_t);
4 4
5void run(void) { 5void run(void) {
6 char str[STRLENMAX]; 6 char str[STRLENMAX];
7 uint8_t i, n, b, a[100]; 7 uint8_t i, b, a[100];
8 size_t n;
8 int64_t p; 9 int64_t p;
9 10
10 fgets(str, STRLENMAX, stdin); 11 fgets(str, STRLENMAX, stdin);
@@ -16,6 +17,6 @@ void run(void) {
16 a[i] = atoi(str); 17 a[i] = atoi(str);
17 } 18 }
18 19
19 p = digitstosumzero(a, n, b); 20 p = digitstosumzero(n, a, b);
20 printf("%" PRId64 "\n", p); 21 printf("%" PRId64 "\n", p);
21} 22}
diff --git a/test/014_math_sumzerotodigits/sumzerotodigits.c b/test/014_math_sumzerotodigits/sumzerotodigits.c
index 5bea7ca..19272ea 100644
--- a/test/014_math_sumzerotodigits/sumzerotodigits.c
+++ b/test/014_math_sumzerotodigits/sumzerotodigits.c
@@ -1,6 +1,6 @@
1#include "../test.h" 1#include "../test.h"
2 2
3void sumzerotodigits(int64_t, uint8_t, uint8_t, uint8_t *); 3void sumzerotodigits(int64_t, size_t n, uint8_t, uint8_t [n]);
4 4
5void run(void) { 5void run(void) {
6 char str[STRLENMAX]; 6 char str[STRLENMAX];
diff --git a/test/032_invertmoves/invertmoves_tests.c b/test/032_invertmoves/invertmoves_tests.c
index d5da323..d791524 100644
--- a/test/032_invertmoves/invertmoves_tests.c
+++ b/test/032_invertmoves/invertmoves_tests.c
@@ -3,8 +3,8 @@
3#define MAXMOVES 20 3#define MAXMOVES 20
4 4
5int64_t readmoves(const char *, int, uint8_t *); 5int64_t readmoves(const char *, int, uint8_t *);
6void writemoves(uint8_t *, int, uint64_t, char *); 6void writemoves(size_t n, uint8_t [n], size_t m, char [m]);
7void invertmoves(uint8_t *, uint8_t, uint8_t *); 7void invertmoves(size_t n, const uint8_t [n], uint8_t [n]);
8 8
9void run(void) { 9void run(void) {
10 char movestr[STRLENMAX], outstr[STRLENMAX]; 10 char movestr[STRLENMAX], outstr[STRLENMAX];
@@ -14,8 +14,8 @@ void run(void) {
14 fgets(movestr, STRLENMAX, stdin); 14 fgets(movestr, STRLENMAX, stdin);
15 c = readmoves(movestr, MAXMOVES, moves); 15 c = readmoves(movestr, MAXMOVES, moves);
16 16
17 invertmoves(moves, c, ret); 17 invertmoves(c, moves, ret);
18 writemoves(ret, c, STRLENMAX, outstr); 18 writemoves(c, ret, STRLENMAX, outstr);
19 19
20 printf("%s\n", outstr); 20 printf("%s\n", outstr);
21} 21}
diff --git a/test/080_allowednext/allowednext_tests.c b/test/080_allowednext/allowednext_tests.c
index 644d4c5..08fe491 100644
--- a/test/080_allowednext/allowednext_tests.c
+++ b/test/080_allowednext/allowednext_tests.c
@@ -1,6 +1,6 @@
1#include "../test.h" 1#include "../test.h"
2 2
3bool allowednextmove(uint8_t *, uint8_t); 3bool allowednextmove(size_t n, const uint8_t [n]);
4 4
5static char *moves[] = { 5static char *moves[] = {
6 "U", "U2", "U'", 6 "U", "U2", "U'",
@@ -31,5 +31,5 @@ void run(void) {
31 n > 2 ? moves[m[n-3]] : "-", 31 n > 2 ? moves[m[n-3]] : "-",
32 n > 1 ? moves[m[n-2]] : "-", 32 n > 1 ? moves[m[n-2]] : "-",
33 n > 0 ? moves[m[n-1]] : "-"); 33 n > 0 ? moves[m[n-1]] : "-");
34 printf("%s\n", allowednextmove(m, n) ? "true" : "false"); 34 printf("%s\n", allowednextmove(n, m) ? "true" : "false");
35} 35}
diff --git a/test/test.h b/test/test.h
index 4b281e0..3854520 100644
--- a/test/test.h
+++ b/test/test.h
@@ -25,7 +25,7 @@ bool isconsistent(cube_t);
25bool issolvable(cube_t); 25bool issolvable(cube_t);
26bool issolved(cube_t); 26bool issolved(cube_t);
27cube_t readcube(char *, char *); 27cube_t readcube(char *, char *);
28int64_t writecube(char *, cube_t, uint64_t, char *); 28int64_t writecube(char *, cube_t, size_t n, char [n]);
29 29
30/* Test function to be implemented by all tests */ 30/* Test function to be implemented by all tests */
31void run(void); 31void run(void);

Generated with cgit - Back to sebastiano.tronto.net