aboutsummaryrefslogtreecommitdiff
path: root/src/nissy.c
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-05-19 17:45:14 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2025-05-19 17:45:14 +0200
commit62d87e063318cc4c842b1b2d8c184f48aeaf6659 (patch)
tree832bf338232c477975bea69f7370ca13f350fb07 /src/nissy.c
parent8b94d135429a9f3253cc7f25a1453b412065c4a0 (diff)
downloadnissy-core-62d87e063318cc4c842b1b2d8c184f48aeaf6659.tar.gz
nissy-core-62d87e063318cc4c842b1b2d8c184f48aeaf6659.zip
Refactored checkdata
Relevant changes include: - Changed the signature of nissy_checkdata(). - Removed expected_distribution.h from tools; this data is now included in each solver's src/ code. - Removed distribution check for cocsep; may add back later.
Diffstat (limited to 'src/nissy.c')
-rw-r--r--src/nissy.c120
1 files changed, 7 insertions, 113 deletions
diff --git a/src/nissy.c b/src/nissy.c
index 1f929c0..aaa5c2a 100644
--- a/src/nissy.c
+++ b/src/nissy.c
@@ -12,9 +12,6 @@
12#include "core/core.h" 12#include "core/core.h"
13#include "solvers/solvers.h" 13#include "solvers/solvers.h"
14 14
15STATIC bool checkdata(const unsigned char *, const tableinfo_t [static 1]);
16STATIC bool distribution_equal(const uint64_t [static INFO_DISTRIBUTION_LEN],
17 const uint64_t [static INFO_DISTRIBUTION_LEN], uint8_t);
18STATIC long long write_result(oriented_cube_t, char [static NISSY_SIZE_CUBE]); 15STATIC long long write_result(oriented_cube_t, char [static NISSY_SIZE_CUBE]);
19STATIC long long nissy_dataid(const char *, char [static NISSY_SIZE_DATAID]); 16STATIC long long nissy_dataid(const char *, char [static NISSY_SIZE_DATAID]);
20STATIC long long nissy_gendata_unsafe( 17STATIC long long nissy_gendata_unsafe(
@@ -30,47 +27,6 @@ struct {
30 GETCUBE_OPTIONS(NULL, NULL) 27 GETCUBE_OPTIONS(NULL, NULL)
31}; 28};
32 29
33STATIC bool
34checkdata(const unsigned char *buf, const tableinfo_t info[static 1])
35{
36 uint64_t distr[INFO_DISTRIBUTION_LEN];
37
38 if (info->type == TABLETYPE_PRUNING) {
39 getdistribution(buf + INFOSIZE, distr, info);
40 LOG("\n[checkdata] Checking distribution for %s\n", info->solver);
41 return distribution_equal(info->distribution, distr, info->maxvalue);
42 } else {
43 LOG("\n[checkdata] Skipping distribution check for "
44 "special table %s\n", info->solver);
45 return true;
46 }
47}
48
49STATIC bool
50distribution_equal(
51 const uint64_t expected[static INFO_DISTRIBUTION_LEN],
52 const uint64_t actual[static INFO_DISTRIBUTION_LEN],
53 uint8_t maxvalue
54)
55{
56 int wrong;
57 uint8_t i;
58
59 for (i = 0, wrong = 0; i <= MIN(maxvalue, 20); i++) {
60 if (expected[i] != actual[i]) {
61 wrong++;
62 LOG("[checkdata] Value for depth %" PRIu8
63 ": expected %" PRIu64 ", found %" PRIu64 "\n",
64 i, expected[i], actual[i]);
65 } else {
66 LOG("[checkdata] Value for depth %" PRIu8
67 " is correct (%" PRIu64 ")\n", i, actual[i]);
68 }
69 }
70
71 return wrong == 0;
72}
73
74STATIC long long 30STATIC long long
75write_result(oriented_cube_t cube, char result[static NISSY_SIZE_CUBE]) 31write_result(oriented_cube_t cube, char result[static NISSY_SIZE_CUBE])
76{ 32{
@@ -227,55 +183,6 @@ nissy_getcube(
227 return write_result(oc, result); 183 return write_result(oc, result);
228} 184}
229 185
230long long
231nissy_datainfo(
232 uint64_t data_size,
233 const unsigned char data[data_size]
234)
235{
236 uint8_t i;
237 tableinfo_t info;
238 long long ret;
239
240 if ((size_t)data % 8 != 0) {
241 LOG("[datainfo] Error: buffer is not 8-byte aligned\n");
242 return NISSY_ERROR_DATA;
243 }
244
245 ret = readtableinfo(data_size, data, &info);
246 if (ret != 0)
247 return ret;
248
249 LOG("\n---------\n\n"
250 "Table information for '%s'\n\n"
251 "Size: %" PRIu64 " bytes\n"
252 "Entries: %" PRIu64 " (%" PRIu8 " bits per entry)\n",
253 info.solver, info.fullsize, info.entries, info.bits);
254
255 switch (info.type) {
256 case TABLETYPE_PRUNING:
257 LOG("\nTable distribution:\nValue\tPositions\n");
258 for (i = 0; i <= info.maxvalue; i++) {
259 LOG("%" PRIu8 "\t%" PRIu64 "\n",
260 i + info.base, info.distribution[i]);
261 }
262 break;
263 case TABLETYPE_SPECIAL:
264 LOG("This is an ad-hoc table\n");
265 break;
266 default:
267 LOG("datainfo: unknown table type\n");
268 return NISSY_ERROR_DATA;
269 }
270
271 if (info.next != 0)
272 return nissy_datainfo(data_size - info.next, data + info.next);
273
274 LOG("\n---------\n");
275
276 return NISSY_OK;
277}
278
279STATIC long long 186STATIC long long
280nissy_dataid(const char *solver, char dataid[static NISSY_SIZE_DATAID]) 187nissy_dataid(const char *solver, char dataid[static NISSY_SIZE_DATAID])
281{ 188{
@@ -344,33 +251,20 @@ nissy_gendata_unsafe(
344 251
345long long 252long long
346nissy_checkdata( 253nissy_checkdata(
254 const char *solver,
347 unsigned long long data_size, 255 unsigned long long data_size,
348 const unsigned char data[data_size] 256 const unsigned char data[data_size]
349) 257)
350{ 258{
351 tableinfo_t info; 259 solver_dispatch_t *dispatch;
352 int64_t err;
353
354 if ((size_t)data % 8 != 0) {
355 LOG("[checkdata] Error: buffer is not 8-byte aligned\n");
356 return NISSY_ERROR_DATA;
357 }
358
359 for (const unsigned char *buf = data;
360 (err = readtableinfo(data_size, buf, &info)) == NISSY_OK;
361 buf += info.next, data_size -= info.next)
362 {
363 if (!checkdata(buf, &info)) {
364 LOG("[checkdata] Error: data for solver '%s' is "
365 "corrupted!\n", info.solver);
366 return NISSY_ERROR_DATA;
367 }
368 260
369 if (info.next == 0) 261 dispatch = match_solver(solver);
370 break; 262 if (dispatch == NULL) {
263 LOG("[checkdata] Unknown solver %s\n", solver);
264 return NISSY_ERROR_INVALID_SOLVER;
371 } 265 }
372 266
373 return err; 267 return dispatch->checkdata(solver, data_size, data);
374} 268}
375 269
376long long 270long long

Generated with cgit - Back to sebastiano.tronto.net