diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-08 15:16:21 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-08 15:16:21 +0200 |
| commit | e4f356b9592599ad91aac34bf2265c9bcdfbb81f (patch) | |
| tree | 5a95908f9cb9cf5ad7d7802661e1d0c3bc2a0e3a /src | |
| parent | c5a6ff0443fc9e5e890fe72cb13ea78d4fe2bbcb (diff) | |
| download | nissy-core-e4f356b9592599ad91aac34bf2265c9bcdfbb81f.tar.gz nissy-core-e4f356b9592599ad91aac34bf2265c9bcdfbb81f.zip | |
Some minor changes to the interface.
- Simplified logger to accept only a string, not a variadic list
of args like printf(). This can still use some improvement,
but now it is easier to use from other languages.
- Fixed some misuses of the logger (wrong types etc)
- Renamed some constants
- Fixed some typos in comments.
Diffstat (limited to '')
| -rw-r--r-- | src/core/io_cube.h | 10 | ||||
| -rw-r--r-- | src/nissy.c | 38 | ||||
| -rw-r--r-- | src/nissy.h | 21 | ||||
| -rw-r--r-- | src/solvers/coord/gendata.h | 2 | ||||
| -rw-r--r-- | src/solvers/coord/utils.h | 4 | ||||
| -rw-r--r-- | src/solvers/h48/gendata_h48.h | 2 | ||||
| -rw-r--r-- | src/utils/dbg_log.h | 21 | ||||
| -rw-r--r-- | src/utils/math.h | 4 |
8 files changed, 59 insertions, 43 deletions
diff --git a/src/core/io_cube.h b/src/core/io_cube.h index 39f3545..f5668d4 100644 --- a/src/core/io_cube.h +++ b/src/core/io_cube.h | |||
| @@ -269,9 +269,9 @@ writecube_B32(cube_t cube, size_t buf_size, char buf[buf_size]) | |||
| 269 | uint8_t corner[8], edge[12]; | 269 | uint8_t corner[8], edge[12]; |
| 270 | 270 | ||
| 271 | if (buf_size < NISSY_SIZE_B32) { | 271 | if (buf_size < NISSY_SIZE_B32) { |
| 272 | LOG("Cannot write cube in B32 format: buffer size must be " | 272 | LOG("Cannot write cube in B32 format: buffer size must be at " |
| 273 | "at least %" PRIu64 " bytes, but the provided one is %" | 273 | "least %u bytes, but the provided one is %zu bytes.\n", |
| 274 | PRIu64 " bytes.\n", NISSY_SIZE_B32, buf_size); | 274 | NISSY_SIZE_B32, buf_size); |
| 275 | return NISSY_ERROR_BUFFER_SIZE; | 275 | return NISSY_ERROR_BUFFER_SIZE; |
| 276 | } | 276 | } |
| 277 | 277 | ||
| @@ -298,8 +298,8 @@ writecube_H48(cube_t cube, size_t buf_size, char buf[buf_size]) | |||
| 298 | 298 | ||
| 299 | if (buf_size < NISSY_SIZE_H48) { | 299 | if (buf_size < NISSY_SIZE_H48) { |
| 300 | LOG("Cannot write cube in H48 format: buffer size must be " | 300 | LOG("Cannot write cube in H48 format: buffer size must be " |
| 301 | "at least %" PRIu64 " bytes, but the provided one is %" | 301 | "at least %u bytes, but the provided one is %zu bytes.\n", |
| 302 | PRIu64 " bytes.\n", NISSY_SIZE_H48, buf_size); | 302 | NISSY_SIZE_H48, buf_size); |
| 303 | return NISSY_ERROR_BUFFER_SIZE; | 303 | return NISSY_ERROR_BUFFER_SIZE; |
| 304 | } | 304 | } |
| 305 | 305 | ||
diff --git a/src/nissy.c b/src/nissy.c index 40e4be7..92fc01b 100644 --- a/src/nissy.c +++ b/src/nissy.c | |||
| @@ -19,7 +19,7 @@ STATIC bool distribution_equal(const uint64_t [static INFO_DISTRIBUTION_LEN], | |||
| 19 | const uint64_t [static INFO_DISTRIBUTION_LEN], uint8_t); | 19 | const uint64_t [static INFO_DISTRIBUTION_LEN], uint8_t); |
| 20 | STATIC long long write_result(cube_t, char [static NISSY_SIZE_B32]); | 20 | STATIC long long write_result(cube_t, char [static NISSY_SIZE_B32]); |
| 21 | STATIC size_t my_strnlen(const char *, size_t); | 21 | STATIC size_t my_strnlen(const char *, size_t); |
| 22 | STATIC long long nissy_dataid(const char *, char [static NISSY_DATAID_SIZE]); | 22 | STATIC long long nissy_dataid(const char *, char [static NISSY_SIZE_DATAID]); |
| 23 | STATIC long long nissy_gendata_unsafe( | 23 | STATIC long long nissy_gendata_unsafe( |
| 24 | const char *, unsigned long long, char *); | 24 | const char *, unsigned long long, char *); |
| 25 | 25 | ||
| @@ -355,9 +355,8 @@ nissy_getcube( | |||
| 355 | c = getcube(ep, eo, cp, co); | 355 | c = getcube(ep, eo, cp, co); |
| 356 | 356 | ||
| 357 | if (!isconsistent(c)) { | 357 | if (!isconsistent(c)) { |
| 358 | LOG("Error: could not get cube with ep=%" PRId64 ", eo=%" | 358 | LOG("Error: could not get cube with ep=%lld, eo=%lld, " |
| 359 | PRId64 ", cp=%" PRId64 ", co=%" PRId64 ".\n", | 359 | "cp=%lld, co=%lld.\n", ep, eo, cp, co); |
| 360 | ep, eo, cp, co); | ||
| 361 | return NISSY_ERROR_OPTIONS; | 360 | return NISSY_ERROR_OPTIONS; |
| 362 | } | 361 | } |
| 363 | 362 | ||
| @@ -368,7 +367,7 @@ long long | |||
| 368 | nissy_datainfo( | 367 | nissy_datainfo( |
| 369 | uint64_t data_size, | 368 | uint64_t data_size, |
| 370 | const char data[data_size], | 369 | const char data[data_size], |
| 371 | void (*write)(const char *, ...) | 370 | void (*write)(const char *) |
| 372 | ) | 371 | ) |
| 373 | { | 372 | { |
| 374 | uint8_t i; | 373 | uint8_t i; |
| @@ -384,25 +383,24 @@ nissy_datainfo( | |||
| 384 | if (ret != 0) | 383 | if (ret != 0) |
| 385 | return ret; | 384 | return ret; |
| 386 | 385 | ||
| 387 | write("\n---------\n\n"); | 386 | write_wrapper(write, |
| 388 | write("Table information for '%s'\n", info.solver); | 387 | "\n---------\n\n" |
| 389 | write("\n"); | 388 | "Table information for '%s'\n\n" |
| 390 | write("Size: %" PRIu64 " bytes\n", info.fullsize); | 389 | "Size: %" PRIu64 " bytes\n" |
| 391 | write("Entries: %" PRIu64 " (%" PRIu8 " bits per entry)", | 390 | "Entries: %" PRIu64 " (%" PRIu8 " bits per entry)\n", |
| 392 | info.entries, info.bits); | 391 | info.solver, info.fullsize, info.entries, info.bits); |
| 393 | write("\n"); | ||
| 394 | 392 | ||
| 395 | switch (info.type) { | 393 | switch (info.type) { |
| 396 | case TABLETYPE_PRUNING: | 394 | case TABLETYPE_PRUNING: |
| 397 | write("\n"); | 395 | write_wrapper(write, "\nTable distribution:\n" |
| 398 | write("Table distribution:\nValue\tPositions\n"); | 396 | "Value\tPositions\n"); |
| 399 | for (i = 0; i <= info.maxvalue; i++) { | 397 | for (i = 0; i <= info.maxvalue; i++) { |
| 400 | write("%" PRIu8 "\t%" PRIu64 "\n", | 398 | write_wrapper(write, "%" PRIu8 "\t%" PRIu64 "\n", |
| 401 | i + info.base, info.distribution[i]); | 399 | i + info.base, info.distribution[i]); |
| 402 | } | 400 | } |
| 403 | break; | 401 | break; |
| 404 | case TABLETYPE_SPECIAL: | 402 | case TABLETYPE_SPECIAL: |
| 405 | write("This is an ad-hoc table\n"); | 403 | write_wrapper(write, "This is an ad-hoc table\n"); |
| 406 | break; | 404 | break; |
| 407 | default: | 405 | default: |
| 408 | LOG("datainfo: unknown table type\n"); | 406 | LOG("datainfo: unknown table type\n"); |
| @@ -413,13 +411,13 @@ nissy_datainfo( | |||
| 413 | return nissy_datainfo( | 411 | return nissy_datainfo( |
| 414 | data_size - info.next, (char *)data + info.next, write); | 412 | data_size - info.next, (char *)data + info.next, write); |
| 415 | 413 | ||
| 416 | write("\n---------\n"); | 414 | write_wrapper(write, "\n---------\n"); |
| 417 | 415 | ||
| 418 | return NISSY_OK; | 416 | return NISSY_OK; |
| 419 | } | 417 | } |
| 420 | 418 | ||
| 421 | STATIC long long | 419 | STATIC long long |
| 422 | nissy_dataid(const char *solver, char dataid[static NISSY_DATAID_SIZE]) | 420 | nissy_dataid(const char *solver, char dataid[static NISSY_SIZE_DATAID]) |
| 423 | { | 421 | { |
| 424 | if (!strncmp(solver, "h48", 3)) { | 422 | if (!strncmp(solver, "h48", 3)) { |
| 425 | uint8_t h, k; | 423 | uint8_t h, k; |
| @@ -442,7 +440,7 @@ nissy_dataid(const char *solver, char dataid[static NISSY_DATAID_SIZE]) | |||
| 442 | long long | 440 | long long |
| 443 | nissy_solverinfo( | 441 | nissy_solverinfo( |
| 444 | const char *solver, | 442 | const char *solver, |
| 445 | char dataid[static NISSY_DATAID_SIZE] | 443 | char dataid[static NISSY_SIZE_DATAID] |
| 446 | ) | 444 | ) |
| 447 | { | 445 | { |
| 448 | long long err; | 446 | long long err; |
| @@ -626,7 +624,7 @@ nissy_countmoves( | |||
| 626 | 624 | ||
| 627 | long long | 625 | long long |
| 628 | nissy_setlogger( | 626 | nissy_setlogger( |
| 629 | void (*log)(const char *, ...) | 627 | void (*log)(const char *) |
| 630 | ) | 628 | ) |
| 631 | { | 629 | { |
| 632 | nissy_log = log; | 630 | nissy_log = log; |
diff --git a/src/nissy.h b/src/nissy.h index add88e4..42429d3 100644 --- a/src/nissy.h +++ b/src/nissy.h | |||
| @@ -25,7 +25,7 @@ for example 'rotation UF' or 'mirrored BL'. | |||
| 25 | #define NISSY_SIZE_H48 88U | 25 | #define NISSY_SIZE_H48 88U |
| 26 | #define NISSY_SIZE_TRANSFORMATION 12U | 26 | #define NISSY_SIZE_TRANSFORMATION 12U |
| 27 | #define NISSY_SIZE_SOLVE_STATS 10U | 27 | #define NISSY_SIZE_SOLVE_STATS 10U |
| 28 | #define NISSY_DATAID_SIZE 255U | 28 | #define NISSY_SIZE_DATAID 255U |
| 29 | 29 | ||
| 30 | /* Flags for NISS options */ | 30 | /* Flags for NISS options */ |
| 31 | #define NISSY_NISSFLAG_NORMAL 1U | 31 | #define NISSY_NISSFLAG_NORMAL 1U |
| @@ -141,7 +141,7 @@ Parameters: | |||
| 141 | format_in - The input format. | 141 | format_in - The input format. |
| 142 | format_out - The output format. | 142 | format_out - The output format. |
| 143 | cube_string - The cube, in format_in format. | 143 | cube_string - The cube, in format_in format. |
| 144 | retult_size - The allocated size of the result array. | 144 | result_size - The allocated size of the result array. |
| 145 | result - Return parameter for the cube in format_out format. | 145 | result - Return parameter for the cube in format_out format. |
| 146 | 146 | ||
| 147 | Return values: | 147 | Return values: |
| @@ -198,7 +198,7 @@ the given solver, and other useful information. | |||
| 198 | Parameters: | 198 | Parameters: |
| 199 | solver - The name of the solver. | 199 | solver - The name of the solver. |
| 200 | dataid - An identifier for the data computed for the solver. Different | 200 | dataid - An identifier for the data computed for the solver. Different |
| 201 | solver may use equivalent data. This identifier can be used | 201 | solvers may use equivalent data. This identifier can be used |
| 202 | e.g. as a filename or database key to save and retrieve the | 202 | e.g. as a filename or database key to save and retrieve the |
| 203 | correct data for each solver, without duplication. | 203 | correct data for each solver, without duplication. |
| 204 | 204 | ||
| @@ -211,7 +211,7 @@ Return values: | |||
| 211 | long long | 211 | long long |
| 212 | nissy_solverinfo( | 212 | nissy_solverinfo( |
| 213 | const char *solver, | 213 | const char *solver, |
| 214 | char dataid[static NISSY_DATAID_SIZE] | 214 | char dataid[static NISSY_SIZE_DATAID] |
| 215 | ); | 215 | ); |
| 216 | 216 | ||
| 217 | /* | 217 | /* |
| @@ -219,8 +219,8 @@ Compute the data for the given solver and store it in generated_data. | |||
| 219 | 219 | ||
| 220 | Parameters: | 220 | Parameters: |
| 221 | solver - The name of the solver. | 221 | solver - The name of the solver. |
| 222 | data_size - The size of the data buffer. It is advised to use nissy_datasize | 222 | data_size - The size of the data buffer. It is advised to use |
| 223 | to check how much memory is needed. | 223 | nissy_solverinfo to check how much memory is needed. |
| 224 | data - The return parameter for the generated data. | 224 | data - The return parameter for the generated data. |
| 225 | This buffer must have 8-byte alignment. | 225 | This buffer must have 8-byte alignment. |
| 226 | 226 | ||
| @@ -324,18 +324,19 @@ nissy_countmoves( | |||
| 324 | ); | 324 | ); |
| 325 | 325 | ||
| 326 | /* | 326 | /* |
| 327 | Set a global logger function used by this library. | 327 | Set a global logger function used by this library. Setting the logger to NULL |
| 328 | disables logging. | ||
| 328 | 329 | ||
| 329 | Parameters: | 330 | Parameters: |
| 330 | write - A callback writer with the same signature as printf(3). | 331 | write - A callback writer with the same signature as printf(3). |
| 331 | 332 | ||
| 332 | Return values: | 333 | Return values: |
| 333 | NISSY_OK - Logger set succesfully. No warning or error is goind to be given | 334 | NISSY_OK - Logger set succesfully. No warning or error is going to be given |
| 334 | if the logger is NULL or invalid. | 335 | if the logger is invalid. |
| 335 | */ | 336 | */ |
| 336 | long long | 337 | long long |
| 337 | nissy_setlogger( | 338 | nissy_setlogger( |
| 338 | void (*logger_function)(const char *, ...) | 339 | void (*logger_function)(const char *) |
| 339 | ); | 340 | ); |
| 340 | 341 | ||
| 341 | 342 | ||
diff --git a/src/solvers/coord/gendata.h b/src/solvers/coord/gendata.h index 63e1631..6abb5eb 100644 --- a/src/solvers/coord/gendata.h +++ b/src/solvers/coord/gendata.h | |||
| @@ -22,7 +22,7 @@ gendata_coord_dispatch(const char *coordstr, void *buf) | |||
| 22 | parse_coord_and_axis(strlen(coordstr), coordstr, &coord, NULL); | 22 | parse_coord_and_axis(strlen(coordstr), coordstr, &coord, NULL); |
| 23 | 23 | ||
| 24 | if (coord == NULL) { | 24 | if (coord == NULL) { |
| 25 | LOG("Could not parse coordinate '%s'\n", coord); | 25 | LOG("Could not parse coordinate '%s'\n", coordstr); |
| 26 | return NISSY_ERROR_INVALID_SOLVER; | 26 | return NISSY_ERROR_INVALID_SOLVER; |
| 27 | } | 27 | } |
| 28 | 28 | ||
diff --git a/src/solvers/coord/utils.h b/src/solvers/coord/utils.h index e9d0595..5407de3 100644 --- a/src/solvers/coord/utils.h +++ b/src/solvers/coord/utils.h | |||
| @@ -2,7 +2,7 @@ STATIC coord_t *parse_coord(size_t n, const char [n]); | |||
| 2 | STATIC uint8_t parse_axis(size_t n, const char [n]); | 2 | STATIC uint8_t parse_axis(size_t n, const char [n]); |
| 3 | STATIC void parse_coord_and_axis( | 3 | STATIC void parse_coord_and_axis( |
| 4 | size_t n, const char [n], coord_t **, uint8_t *); | 4 | size_t n, const char [n], coord_t **, uint8_t *); |
| 5 | STATIC int64_t dataid_coord(const char *, char [static NISSY_DATAID_SIZE]); | 5 | STATIC int64_t dataid_coord(const char *, char [static NISSY_SIZE_DATAID]); |
| 6 | 6 | ||
| 7 | STATIC coord_t * | 7 | STATIC coord_t * |
| 8 | parse_coord(size_t n, const char coord[n]) | 8 | parse_coord(size_t n, const char coord[n]) |
| @@ -52,7 +52,7 @@ parse_coord_and_axis( | |||
| 52 | } | 52 | } |
| 53 | 53 | ||
| 54 | STATIC int64_t | 54 | STATIC int64_t |
| 55 | dataid_coord(const char *ca, char dataid[static NISSY_DATAID_SIZE]) | 55 | dataid_coord(const char *ca, char dataid[static NISSY_SIZE_DATAID]) |
| 56 | { | 56 | { |
| 57 | coord_t *c; | 57 | coord_t *c; |
| 58 | 58 | ||
diff --git a/src/solvers/h48/gendata_h48.h b/src/solvers/h48/gendata_h48.h index b6647ed..ba0cc50 100644 --- a/src/solvers/h48/gendata_h48.h +++ b/src/solvers/h48/gendata_h48.h | |||
| @@ -232,7 +232,7 @@ gendata_h48h0k4(gendata_h48_arg_t arg[static 1]) | |||
| 232 | bfsarg[t].table_mutex[tt] = &table_mutex[tt]; | 232 | bfsarg[t].table_mutex[tt] = &table_mutex[tt]; |
| 233 | } | 233 | } |
| 234 | for (done = 1, d = 1; done < h48max && d <= arg->maxdepth; d++) { | 234 | for (done = 1, d = 1; done < h48max && d <= arg->maxdepth; d++) { |
| 235 | LOG("h48: generating depth %" PRIu8 "\n", d); | 235 | LOG("h48: generating depth %" PRId64 "\n", d); |
| 236 | 236 | ||
| 237 | for (t = 0; t < THREADS; t++) { | 237 | for (t = 0; t < THREADS; t++) { |
| 238 | bfsarg[t].depth = d; | 238 | bfsarg[t].depth = d; |
diff --git a/src/utils/dbg_log.h b/src/utils/dbg_log.h index f9b1d8a..c30993a 100644 --- a/src/utils/dbg_log.h +++ b/src/utils/dbg_log.h | |||
| @@ -1,6 +1,23 @@ | |||
| 1 | void (*nissy_log)(const char *, ...); | 1 | #include <stdio.h> |
| 2 | 2 | ||
| 3 | #define LOG(...) if (nissy_log != NULL) nissy_log(__VA_ARGS__); | 3 | void (*nissy_log)(const char *); |
| 4 | void write_wrapper(void (*)(const char *), const char *, ...); | ||
| 5 | |||
| 6 | void | ||
| 7 | write_wrapper(void (*write)(const char *), const char *str, ...) | ||
| 8 | { | ||
| 9 | static const size_t len = 1000; | ||
| 10 | char message[len]; | ||
| 11 | va_list args; | ||
| 12 | |||
| 13 | va_start(args, str); | ||
| 14 | sprintf(message, str, args); | ||
| 15 | va_end(args); | ||
| 16 | |||
| 17 | write(message); | ||
| 18 | } | ||
| 19 | |||
| 20 | #define LOG(...) if (nissy_log != NULL) write_wrapper(nissy_log, __VA_ARGS__); | ||
| 4 | 21 | ||
| 5 | #ifdef DEBUG | 22 | #ifdef DEBUG |
| 6 | #define STATIC | 23 | #define STATIC |
diff --git a/src/utils/math.h b/src/utils/math.h index a9d9b85..1ae4013 100644 --- a/src/utils/math.h +++ b/src/utils/math.h | |||
| @@ -168,8 +168,8 @@ sumzerotodigits(int64_t d, size_t n, uint8_t b, uint8_t a[n]) | |||
| 168 | size_t i; | 168 | size_t i; |
| 169 | 169 | ||
| 170 | if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) { | 170 | if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) { |
| 171 | LOG("Won't compute 'digits' for n=%" PRIu8 "and b=%" PRIu8 | 171 | LOG("Won't compute 'digits' for n=%zu and b=%" PRIu8 |
| 172 | " (use n=8 b=3 or n=12 b=2)\n"); | 172 | " (use n=8 b=3 or n=12 b=2)\n", n, b); |
| 173 | goto sumzerotodigits_error; | 173 | goto sumzerotodigits_error; |
| 174 | } | 174 | } |
| 175 | 175 | ||
