diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-03-22 06:43:11 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-03-22 18:45:47 +0100 |
| commit | ce3f1cc0ef9f46d70ab5387b1458e9098b40711d (patch) | |
| tree | 4949745670b829f2211381bf14b8440dcc6ca7b1 /src/core | |
| parent | 0550a16c1cce868bbc3f3b5ad59f80e35cf2a6cd (diff) | |
| download | nissy-core-ce3f1cc0ef9f46d70ab5387b1458e9098b40711d.tar.gz nissy-core-ce3f1cc0ef9f46d70ab5387b1458e9098b40711d.zip | |
Some safety with move arrays, small refactor appendchar
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/cube.h | 8 | ||||
| -rw-r--r-- | src/core/io_cube.h | 24 | ||||
| -rw-r--r-- | src/core/io_moves.h | 17 | ||||
| -rw-r--r-- | src/core/moves.h | 29 |
4 files changed, 40 insertions, 38 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 @@ | |||
| 1 | STATIC cube_t readcube(const char *, const char *); | 1 | STATIC cube_t readcube(const char *, const char *); |
| 2 | STATIC int64_t writecube(const char *, cube_t, uint64_t, char *); | 2 | STATIC int64_t writecube(const char *, cube_t, size_t n, char [n]); |
| 3 | STATIC void log_available_formats(void); | 3 | STATIC void log_available_formats(void); |
| 4 | STATIC uint8_t readco(const char *); | 4 | STATIC uint8_t readco(const char *); |
| 5 | STATIC uint8_t readcp(const char *); | 5 | STATIC uint8_t readcp(const char *); |
| @@ -10,10 +10,10 @@ STATIC cube_t readcube_H48(const char *); | |||
| 10 | STATIC uint8_t readpiece_LST(const char **); | 10 | STATIC uint8_t readpiece_LST(const char **); |
| 11 | STATIC cube_t readcube_LST(const char *); | 11 | STATIC cube_t readcube_LST(const char *); |
| 12 | 12 | ||
| 13 | STATIC int64_t writepiece_LST(uint8_t, uint64_t, char *); | 13 | STATIC int64_t writepiece_LST(uint8_t, size_t n, char [n]); |
| 14 | STATIC int64_t writecube_B32(cube_t, uint64_t, char *); | 14 | STATIC int64_t writecube_B32(cube_t, size_t n, char [n]); |
| 15 | STATIC int64_t writecube_H48(cube_t, uint64_t, char *); | 15 | STATIC int64_t writecube_H48(cube_t, size_t n, char [n]); |
| 16 | STATIC int64_t writecube_LST(cube_t, uint64_t, char *); | 16 | STATIC int64_t writecube_LST(cube_t, size_t n, char [n]); |
| 17 | 17 | ||
| 18 | STATIC uint8_t b32toedge(char); | 18 | STATIC uint8_t b32toedge(char); |
| 19 | STATIC uint8_t b32tocorner(char); | 19 | STATIC uint8_t b32tocorner(char); |
| @@ -23,7 +23,7 @@ STATIC char cornertob32(uint8_t); | |||
| 23 | STATIC struct { | 23 | STATIC 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 | ||
| 49 | STATIC int64_t | 49 | STATIC int64_t |
| 50 | writecube(const char *format, cube_t cube, uint64_t buf_size, char *buf) | 50 | writecube(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 | ||
| 235 | STATIC int64_t | 235 | STATIC int64_t |
| 236 | writepiece_LST(uint8_t piece, uint64_t buf_size, char *buf) | 236 | writepiece_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 | ||
| 265 | STATIC int64_t | 265 | STATIC int64_t |
| 266 | writecube_B32(cube_t cube, uint64_t buf_size, char *buf) | 266 | writecube_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 | ||
| 293 | STATIC int64_t | 293 | STATIC int64_t |
| 294 | writecube_H48(cube_t cube, uint64_t buf_size, char *buf) | 294 | writecube_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 | ||
| 333 | STATIC int64_t | 333 | STATIC int64_t |
| 334 | writecube_LST(cube_t cube, uint64_t buf_size, char *buf) | 334 | writecube_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 @@ | |||
| 1 | STATIC uint8_t readmove(char); | 1 | STATIC uint8_t readmove(char); |
| 2 | STATIC uint8_t readmodifier(char); | 2 | STATIC uint8_t readmodifier(char); |
| 3 | STATIC int64_t writemoves(uint8_t *, int, uint64_t, char *); | 3 | STATIC int64_t writemoves(size_t n, uint8_t [n], size_t m, char [m]); |
| 4 | 4 | ||
| 5 | STATIC uint8_t | 5 | STATIC uint8_t |
| 6 | readmove(char c) | 6 | readmove(char c) |
| @@ -39,11 +39,14 @@ readmodifier(char c) | |||
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | STATIC int64_t | 41 | STATIC int64_t |
| 42 | writemoves(uint8_t *m, int n, uint64_t buf_size, char *buf) | 42 | writemoves( |
| 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 | ||
| 75 | writemoves_error: | 78 | writemoves_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 | ||
| 4 | STATIC_INLINE bool allowednextmove(uint8_t *, uint8_t); | 4 | STATIC_INLINE bool allowednextmove(size_t n, const uint8_t [n]); |
| 5 | STATIC_INLINE uint32_t allowednextmove_mask(uint8_t *, uint8_t); | 5 | STATIC_INLINE uint32_t allowednextmove_mask(size_t n, const uint8_t [n]); |
| 6 | 6 | ||
| 7 | STATIC_INLINE uint8_t movebase(uint8_t); | 7 | STATIC_INLINE uint8_t movebase(uint8_t); |
| 8 | STATIC_INLINE uint8_t moveaxis(uint8_t); | 8 | STATIC_INLINE uint8_t moveaxis(uint8_t); |
| @@ -13,9 +13,9 @@ STATIC_INLINE uint32_t disable_moves(uint32_t, uint8_t); | |||
| 13 | STATIC cube_t move(cube_t, uint8_t); | 13 | STATIC cube_t move(cube_t, uint8_t); |
| 14 | STATIC cube_t premove(cube_t, uint8_t); | 14 | STATIC cube_t premove(cube_t, uint8_t); |
| 15 | STATIC uint8_t inverse_move(uint8_t); | 15 | STATIC uint8_t inverse_move(uint8_t); |
| 16 | STATIC void invertmoves(uint8_t *, uint8_t, uint8_t *); | 16 | STATIC void invertmoves(size_t n, const uint8_t [n], uint8_t [n]); |
| 17 | STATIC void sortparallel(uint8_t *, uint8_t); | 17 | STATIC void sortparallel(size_t n, uint8_t [n]); |
| 18 | STATIC bool are_lastmoves_singlecw(int n, uint8_t [n]); | 18 | STATIC bool are_lastmoves_singlecw(size_t n, uint8_t [n]); |
| 19 | 19 | ||
| 20 | STATIC int readmoves(const char *, int, uint8_t *); | 20 | STATIC int readmoves(const char *, int, uint8_t *); |
| 21 | STATIC cube_t applymoves(cube_t, const char *); | 21 | STATIC cube_t applymoves(cube_t, const char *); |
| @@ -40,14 +40,13 @@ STATIC cube_t applymoves(cube_t, const char *); | |||
| 40 | } | 40 | } |
| 41 | 41 | ||
| 42 | STATIC bool | 42 | STATIC bool |
| 43 | allowednextmove(uint8_t *moves, uint8_t n) | 43 | allowednextmove(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 | ||
| 49 | STATIC uint32_t | 48 | STATIC uint32_t |
| 50 | allowednextmove_mask(uint8_t *moves, uint8_t n) | 49 | allowednextmove_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) | |||
| 79 | STATIC_INLINE uint32_t | 78 | STATIC_INLINE uint32_t |
| 80 | disable_moves(uint32_t current_result, uint8_t base_index) | 79 | disable_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 | ||
| 85 | STATIC_INLINE uint8_t | 84 | STATIC_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") |
| 238 | STATIC void | 237 | STATIC void |
| 239 | invertmoves(uint8_t *moves, uint8_t nmoves, uint8_t *ret) | 238 | invertmoves(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 | ||
| 248 | STATIC void | 247 | STATIC void |
| 249 | sortparallel(uint8_t *moves, uint8_t n) | 248 | sortparallel(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 | ||
| 259 | STATIC bool | 258 | STATIC bool |
| 260 | are_lastmoves_singlecw(int n, uint8_t moves[n]) | 259 | are_lastmoves_singlecw(size_t n, uint8_t moves[n]) |
| 261 | { | 260 | { |
| 262 | bool two; | 261 | bool two; |
| 263 | 262 | ||
