diff options
| author | enricotenuti <tenutz_27@outlook.it> | 2024-09-29 12:42:31 +0200 |
|---|---|---|
| committer | enricotenuti <tenutz_27@outlook.it> | 2024-09-29 12:42:31 +0200 |
| commit | 7a52b4cd50a40e4bce919b6bd51203d0e9867141 (patch) | |
| tree | 95ab133f9771c4791d5c784f1b3d4822b1dd7d3c /tools/tool.h | |
| parent | 8fcfb3a33fe053ed2032d58ecc0b5d640c155931 (diff) | |
| parent | 774a824a6c80b5af495f4fb99d98758e3b9f6b81 (diff) | |
| download | nissy-core-7a52b4cd50a40e4bce919b6bd51203d0e9867141.tar.gz nissy-core-7a52b4cd50a40e4bce919b6bd51203d0e9867141.zip | |
Merge remote-tracking branch 'upstream/master'
Merge upstream
Diffstat (limited to 'tools/tool.h')
| -rw-r--r-- | tools/tool.h | 92 |
1 files changed, 88 insertions, 4 deletions
diff --git a/tools/tool.h b/tools/tool.h index 757657b..c44828d 100644 --- a/tools/tool.h +++ b/tools/tool.h | |||
| @@ -6,14 +6,19 @@ | |||
| 6 | #include <stdlib.h> | 6 | #include <stdlib.h> |
| 7 | 7 | ||
| 8 | #include "../src/nissy.h" | 8 | #include "../src/nissy.h" |
| 9 | #include "nissy_extra.h" | ||
| 9 | 10 | ||
| 10 | static void log_stderr(const char *, ...); | 11 | static void log_stderr(const char *, ...); |
| 11 | static void log_stdout(const char *, ...); | 12 | static void log_stdout(const char *, ...); |
| 12 | static double timerun(void (*)(void), const char *); | 13 | static double timerun(void (*)(void), const char *); |
| 14 | static void getfilename(const char *, const char *, char *); | ||
| 13 | static void writetable(const char *, int64_t, const char *); | 15 | static void writetable(const char *, int64_t, const char *); |
| 14 | static int64_t generatetable(const char *, const char *, char **); | 16 | static int64_t generatetable(const char *, const char *, char **); |
| 17 | static int64_t derivetable(const char *, const char *, const char *, char **); | ||
| 15 | static int getdata(const char *, const char *, char **, const char *); | 18 | static int getdata(const char *, const char *, char **, const char *); |
| 16 | static void gendata_run(const char *, const char *, const char *, uint64_t[static 21]); | 19 | static void gendata_run(const char *, const char *, uint64_t[static 21]); |
| 20 | static void derivedata_run( | ||
| 21 | const char *, const char *, const char *, const char *); | ||
| 17 | 22 | ||
| 18 | static void | 23 | static void |
| 19 | log_stderr(const char *str, ...) | 24 | log_stderr(const char *str, ...) |
| @@ -70,6 +75,17 @@ timerun(void (*run)(void), const char *name) | |||
| 70 | } | 75 | } |
| 71 | 76 | ||
| 72 | static void | 77 | static void |
| 78 | getfilename(const char *solver, const char *options, char *filename) | ||
| 79 | { | ||
| 80 | uint8_t h, k; | ||
| 81 | |||
| 82 | /* Only h48 supported for now */ | ||
| 83 | parse_h48_options(options, &h, &k, NULL); | ||
| 84 | |||
| 85 | sprintf(filename, "tables/%sh%dk%d", solver, h, k); | ||
| 86 | } | ||
| 87 | |||
| 88 | static void | ||
| 73 | writetable(const char *buf, int64_t size, const char *filename) | 89 | writetable(const char *buf, int64_t size, const char *filename) |
| 74 | { | 90 | { |
| 75 | FILE *f; | 91 | FILE *f; |
| @@ -109,6 +125,44 @@ generatetable(const char *solver, const char *options, char **buf) | |||
| 109 | return gensize; | 125 | return gensize; |
| 110 | } | 126 | } |
| 111 | 127 | ||
| 128 | static int64_t | ||
| 129 | derivetable( | ||
| 130 | const char *opts_large, | ||
| 131 | const char *opts_small, | ||
| 132 | const char *filename_large, | ||
| 133 | char **buf | ||
| 134 | ) | ||
| 135 | { | ||
| 136 | uint8_t h; | ||
| 137 | int64_t size, gensize; | ||
| 138 | char *fulltable; | ||
| 139 | |||
| 140 | if (getdata("h48", opts_large, &fulltable, filename_large) != 0) { | ||
| 141 | printf("Error reading full table.\n"); | ||
| 142 | return -1; | ||
| 143 | } | ||
| 144 | |||
| 145 | size = nissy_datasize("h48", opts_small); | ||
| 146 | if (size == -1) { | ||
| 147 | printf("Error getting table size.\n"); | ||
| 148 | free(fulltable); | ||
| 149 | return -1; | ||
| 150 | } | ||
| 151 | |||
| 152 | h = atoi(opts_small); /* TODO: use option parser */ | ||
| 153 | *buf = malloc(size); | ||
| 154 | gensize = gendata_h48_derive(h, fulltable, *buf); | ||
| 155 | |||
| 156 | if (gensize != size) { | ||
| 157 | fprintf(stderr, "Error deriving table\n"); | ||
| 158 | free(fulltable); | ||
| 159 | return -2; | ||
| 160 | } | ||
| 161 | |||
| 162 | free(fulltable); | ||
| 163 | return gensize; | ||
| 164 | } | ||
| 165 | |||
| 112 | static int | 166 | static int |
| 113 | getdata( | 167 | getdata( |
| 114 | const char *solver, | 168 | const char *solver, |
| @@ -155,13 +209,12 @@ static void | |||
| 155 | gendata_run( | 209 | gendata_run( |
| 156 | const char *solver, | 210 | const char *solver, |
| 157 | const char *options, | 211 | const char *options, |
| 158 | const char *filename, /* TODO: remove filename, use solver name */ | ||
| 159 | uint64_t expected[static 21] | 212 | uint64_t expected[static 21] |
| 160 | ) { | 213 | ) { |
| 161 | int64_t size; | 214 | int64_t size; |
| 162 | char *buf; | 215 | char *buf, filename[1024]; |
| 163 | 216 | ||
| 164 | 217 | getfilename(solver, options, filename); | |
| 165 | size = generatetable(solver, options, &buf); | 218 | size = generatetable(solver, options, &buf); |
| 166 | switch (size) { | 219 | switch (size) { |
| 167 | case -1: | 220 | case -1: |
| @@ -182,3 +235,34 @@ gendata_run( | |||
| 182 | gendata_run_finish: | 235 | gendata_run_finish: |
| 183 | free(buf); | 236 | free(buf); |
| 184 | } | 237 | } |
| 238 | |||
| 239 | static void | ||
| 240 | derivedata_run( | ||
| 241 | const char *opts_large, | ||
| 242 | const char *opts_small, | ||
| 243 | const char *filename_large, | ||
| 244 | const char *filename_small | ||
| 245 | ) | ||
| 246 | { | ||
| 247 | int64_t size; | ||
| 248 | char *buf; | ||
| 249 | |||
| 250 | size = derivetable(opts_large, opts_small, filename_large, &buf); | ||
| 251 | switch (size) { | ||
| 252 | case -1: | ||
| 253 | return; | ||
| 254 | case -2: | ||
| 255 | goto derivedata_run_finish; | ||
| 256 | default: | ||
| 257 | nissy_datainfo(buf, write_stdout); | ||
| 258 | printf("\n"); | ||
| 259 | printf("Succesfully generated %" PRId64 " bytes. " | ||
| 260 | "See above for details on the tables.\n", size); | ||
| 261 | |||
| 262 | writetable(buf, size, filename_small); | ||
| 263 | break; | ||
| 264 | } | ||
| 265 | |||
| 266 | derivedata_run_finish: | ||
| 267 | free(buf); | ||
| 268 | } | ||
