aboutsummaryrefslogtreecommitdiff
path: root/tools/tool.h
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-10-10 14:30:39 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2024-10-10 14:30:39 +0200
commitf8d247c53c3c3bd94fed645b5b47a9096cadc123 (patch)
tree7a62cc7322ea8175a7df9b655d4a9d5e026a004a /tools/tool.h
parent2e93de4ad102973961cb24d5b91f0c299763299f (diff)
downloadnissy-core-f8d247c53c3c3bd94fed645b5b47a9096cadc123.tar.gz
nissy-core-f8d247c53c3c3bd94fed645b5b47a9096cadc123.zip
Removed "options" from solver selection
Now the solver name fully determines the solver (and options) to be used. For example, now one must specify "h48h0k4" as the name of the solver. This PR also fixes a couple of things in tools.
Diffstat (limited to 'tools/tool.h')
-rw-r--r--tools/tool.h88
1 files changed, 41 insertions, 47 deletions
diff --git a/tools/tool.h b/tools/tool.h
index 2aaabae..02ff0f2 100644
--- a/tools/tool.h
+++ b/tools/tool.h
@@ -12,12 +12,11 @@
12static void log_stderr(const char *, ...); 12static void log_stderr(const char *, ...);
13static void log_stdout(const char *, ...); 13static void log_stdout(const char *, ...);
14static double timerun(void (*)(void)); 14static double timerun(void (*)(void));
15static void getfilename(const char *, const char *, char *);
16static void writetable(const char *, int64_t, const char *); 15static void writetable(const char *, int64_t, const char *);
17static int64_t generatetable(const char *, const char *, char **); 16static int64_t generatetable(const char *, char **);
18static int64_t derivetable(const char *, const char *, const char *, char **); 17static int64_t derivetable(const char *, const char *, const char *, char **);
19static int getdata(const char *, const char *, char **, const char *); 18static int getdata(const char *, char **, const char *);
20static void gendata_run(const char *, const char *, uint64_t[static 21]); 19static void gendata_run(const char *, uint64_t[static 21]);
21static void derivedata_run( 20static void derivedata_run(
22 const char *, const char *, const char *, const char *); 21 const char *, const char *, const char *, const char *);
23 22
@@ -71,50 +70,39 @@ timerun(void (*run)(void))
71} 70}
72 71
73static void 72static void
74getfilename(const char *solver, const char *options, char *filename)
75{
76 uint8_t h, k;
77
78 /* Only h48 supported for now */
79 parse_h48_options(options, &h, &k, NULL);
80
81 sprintf(filename, "tables/%sh%dk%d", solver, h, k);
82}
83
84static void
85writetable(const char *buf, int64_t size, const char *filename) 73writetable(const char *buf, int64_t size, const char *filename)
86{ 74{
87 FILE *f; 75 FILE *f;
88 76
89 if ((f = fopen(filename, "wb")) == NULL) { 77 if ((f = fopen(filename, "wb")) == NULL) {
90 fprintf(stderr, "Could not write tables to file %s" 78 printf("Could not write tables to file %s"
91 ", will be regenerated next time.\n", filename); 79 ", will be regenerated next time.\n", filename);
92 } else { 80 } else {
93 fwrite(buf, size, 1, f); 81 fwrite(buf, size, 1, f);
94 fclose(f); 82 fclose(f);
95 fprintf(stderr, "Table written to %s.\n", filename); 83 printf("Table written to %s.\n", filename);
96 } 84 }
97} 85}
98 86
99static int64_t 87static int64_t
100generatetable(const char *solver, const char *options, char **buf) 88generatetable(const char *solver, char **buf)
101{ 89{
102 int64_t size, gensize; 90 int64_t size, gensize;
103 91
104 size = nissy_datasize(solver, options); 92 size = nissy_datasize(solver);
105 if (size == -1) { 93 if (size == -1) {
106 printf("Error getting table size.\n"); 94 printf("Error getting table size.\n");
107 return -1; 95 return -1;
108 } 96 }
109 97
110 *buf = malloc(size); 98 *buf = malloc(size);
111 gensize = nissy_gendata(solver, options, *buf); 99 gensize = nissy_gendata(solver, *buf);
112 100
113 if (gensize != size) { 101 if (gensize != size) {
114 fprintf(stderr, "Error generating table"); 102 printf("Error generating table");
115 if (gensize != -1) 103 if (gensize != -1)
116 fprintf(stderr, " (got %" PRId64 " bytes)", gensize); 104 printf(" (got %" PRId64 " bytes)", gensize);
117 fprintf(stderr, "\n"); 105 printf("\n");
118 return -2; 106 return -2;
119 } 107 }
120 108
@@ -123,46 +111,53 @@ generatetable(const char *solver, const char *options, char **buf)
123 111
124static int64_t 112static int64_t
125derivetable( 113derivetable(
126 const char *opts_large, 114 const char *solver_large,
127 const char *opts_small, 115 const char *solver_small,
128 const char *filename_large, 116 const char *filename_large,
129 char **buf 117 char **buf
130) 118)
131{ 119{
132 uint8_t h; 120 uint8_t h, k;
133 int64_t size, gensize; 121 int64_t size, gensize;
134 char *fulltable; 122 char *fulltable;
135 123
136 if (getdata("h48", opts_large, &fulltable, filename_large) != 0) { 124 if (getdata(solver_large, &fulltable, filename_large) != 0) {
137 printf("Error reading full table.\n"); 125 printf("Error reading full table.\n");
138 return -1; 126 gensize = -1;
127 goto derivetable_error_nofree;
139 } 128 }
140 129
141 size = nissy_datasize("h48", opts_small); 130 size = nissy_datasize(solver_small);
142 if (size == -1) { 131 if (size == -1) {
143 printf("Error getting table size.\n"); 132 printf("Error getting table size.\n");
144 free(fulltable); 133 gensize = -2;
145 return -1; 134 goto derivetable_error;
135 }
136
137 if (parse_h48_solver(solver_small, &h, &k) != 0) {
138 gensize = -3;
139 goto derivetable_error;
146 } 140 }
147 141
148 h = atoi(opts_small); /* TODO: use option parser */
149 *buf = malloc(size); 142 *buf = malloc(size);
150 gensize = gendata_h48_derive(h, fulltable, *buf); 143 gensize = gendata_h48_derive(h, fulltable, *buf);
151 144
152 if (gensize != size) { 145 if (gensize != size) {
153 fprintf(stderr, "Error deriving table\n"); 146 printf("Error deriving table\n");
154 free(fulltable); 147 gensize = -4;
155 return -2; 148 goto derivetable_error;
156 } 149 }
157 150
151derivetable_error:
158 free(fulltable); 152 free(fulltable);
153
154derivetable_error_nofree:
159 return gensize; 155 return gensize;
160} 156}
161 157
162static int 158static int
163getdata( 159getdata(
164 const char *solver, 160 const char *solver,
165 const char *options,
166 char **buf, 161 char **buf,
167 const char *filename 162 const char *filename
168) { 163) {
@@ -170,8 +165,8 @@ getdata(
170 FILE *f; 165 FILE *f;
171 166
172 if ((f = fopen(filename, "rb")) == NULL) { 167 if ((f = fopen(filename, "rb")) == NULL) {
173 fprintf(stderr, "Table file not found, generating it.\n"); 168 printf("Table file not found, generating it.\n");
174 size = generatetable(solver, options, buf); 169 size = generatetable(solver, buf);
175 switch (size) { 170 switch (size) {
176 case -1: 171 case -1:
177 goto getdata_error_nofree; 172 goto getdata_error_nofree;
@@ -182,13 +177,13 @@ getdata(
182 break; 177 break;
183 } 178 }
184 } else { 179 } else {
185 fprintf(stderr, "Reading tables from file %s\n", filename); 180 printf("Reading tables from file %s\n", filename);
186 size = nissy_datasize(solver, options); 181 size = nissy_datasize(solver);
187 *buf = malloc(size); 182 *buf = malloc(size);
188 sizeread = fread(*buf, size, 1, f); 183 sizeread = fread(*buf, size, 1, f);
189 fclose(f); 184 fclose(f);
190 if (sizeread != 1) { 185 if (sizeread != 1) {
191 fprintf(stderr, "Error reading table, stopping\n"); 186 printf("Error reading table, stopping\n");
192 goto getdata_error; 187 goto getdata_error;
193 } 188 }
194 } 189 }
@@ -204,14 +199,13 @@ getdata_error_nofree:
204static void 199static void
205gendata_run( 200gendata_run(
206 const char *solver, 201 const char *solver,
207 const char *options,
208 uint64_t expected[static 21] 202 uint64_t expected[static 21]
209) { 203) {
210 int64_t size; 204 int64_t size;
211 char *buf, filename[1024]; 205 char *buf, filename[1024];
212 206
213 getfilename(solver, options, filename); 207 sprintf(filename, "tables/%s", solver);
214 size = generatetable(solver, options, &buf); 208 size = generatetable(solver, &buf);
215 switch (size) { 209 switch (size) {
216 case -1: 210 case -1:
217 return; 211 return;
@@ -234,8 +228,8 @@ gendata_run_finish:
234 228
235static void 229static void
236derivedata_run( 230derivedata_run(
237 const char *opts_large, 231 const char *solver_large,
238 const char *opts_small, 232 const char *solver_small,
239 const char *filename_large, 233 const char *filename_large,
240 const char *filename_small 234 const char *filename_small
241) 235)
@@ -243,7 +237,7 @@ derivedata_run(
243 int64_t size; 237 int64_t size;
244 char *buf; 238 char *buf;
245 239
246 size = derivetable(opts_large, opts_small, filename_large, &buf); 240 size = derivetable(solver_large, solver_small, filename_large, &buf);
247 switch (size) { 241 switch (size) {
248 case -1: 242 case -1:
249 return; 243 return;

Generated with cgit - Back to sebastiano.tronto.net