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/solvers/h48 | |
| 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/solvers/h48')
| -rw-r--r-- | src/solvers/h48/gendata_h48.h | 4 | ||||
| -rw-r--r-- | src/solvers/h48/gendata_types_macros.h | 1 | ||||
| -rw-r--r-- | src/solvers/h48/solve.h | 48 |
3 files changed, 21 insertions, 32 deletions
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 | ||
| 59 | STATIC int64_t solve_h48_appendsolution(dfsarg_solve_h48_t *); | 59 | STATIC int64_t solve_h48_appendsolution(dfsarg_solve_h48_t *); |
| 60 | STATIC int64_t solve_h48_appendallsym(dfsarg_solve_h48_t *); | 60 | STATIC int64_t solve_h48_appendallsym(dfsarg_solve_h48_t *); |
| 61 | STATIC bool solve_h48_appendchar(dfsarg_solve_h48_t *, char); | ||
| 62 | STATIC_INLINE bool solve_h48_stop(dfsarg_solve_h48_t *); | 61 | STATIC_INLINE bool solve_h48_stop(dfsarg_solve_h48_t *); |
| 63 | STATIC int64_t solve_h48_maketasks( | 62 | STATIC 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( | |||
| 66 | STATIC void *solve_h48_runthread(void *); | 65 | STATIC void *solve_h48_runthread(void *); |
| 67 | STATIC int64_t solve_h48_dfs(dfsarg_solve_h48_t *); | 66 | STATIC int64_t solve_h48_dfs(dfsarg_solve_h48_t *); |
| 68 | STATIC int64_t solve_h48(cube_t, int8_t, int8_t, uint64_t, int8_t, int8_t, | 67 | STATIC 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 | ||
| 72 | STATIC int64_t | 71 | STATIC 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 | ||
| 159 | STATIC bool | ||
| 160 | solve_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 | |||
| 171 | STATIC_INLINE bool | 159 | STATIC_INLINE bool |
| 172 | solve_h48_stop(dfsarg_solve_h48_t *arg) | 160 | solve_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 | ||
| 563 | solve_h48_done: | 552 | solve_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; |
