1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
#include <time.h>
#include <stdarg.h>
#include <stdbool.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../src/nissy.h"
#include "nissy_extra.h"
static void log_stderr(const char *, void *);
static double timerun(void (*)(void));
static void writetable(const unsigned char *, int64_t, const char *);
static long long int generatetable(const char *, unsigned char **,
char [static NISSY_SIZE_DATAID]);
static long long int derivetable(
const char *, const char *, const char *, unsigned char **);
static int getdata(const char *, unsigned char **, const char *);
static void gendata_run(const char *, uint64_t[static 21]);
static void derivedata_run(
const char *, const char *, const char *, const char *);
static void
log_stderr(const char *str, void *unused)
{
fprintf(stderr, "%s", str);
}
static double
timerun(void (*run)(void))
{
struct timespec start, end;
double tdiff, tdsec, tdnano;
fflush(stdout);
if (run == NULL) {
printf("nothing to run!\n");
fflush(stdout);
return -1.0;
}
clock_gettime(CLOCK_MONOTONIC, &start);
run();
clock_gettime(CLOCK_MONOTONIC, &end);
tdsec = end.tv_sec - start.tv_sec;
tdnano = end.tv_nsec - start.tv_nsec;
tdiff = tdsec + 1e-9 * tdnano;
printf("---------\n");
printf("\nTotal time: %.4fs\n", tdiff);
fflush(stdout);
return tdiff;
}
static void
writetable(const unsigned char *buf, int64_t size, const char *filename)
{
FILE *f;
if ((f = fopen(filename, "wb")) == NULL) {
printf("Could not write tables to file %s"
", will be regenerated next time.\n", filename);
} else {
fwrite(buf, size, 1, f);
fclose(f);
printf("Table written to %s.\n", filename);
}
}
static long long int
generatetable(
const char *solver,
unsigned char **buf,
char dataid[static NISSY_SIZE_DATAID]
)
{
long long int size, gensize;
size = nissy_solverinfo(solver, dataid);
if (size < 0) {
printf("Error getting table size.\n");
return -1;
}
*buf = malloc(size);
gensize = nissy_gendata(solver, size, *buf);
if (gensize != size) {
printf("Error generating table");
if (gensize == NISSY_OK)
printf(" (got %lld bytes)", gensize);
printf("\n");
return -2;
}
return gensize;
}
static long long int
derivetable(
const char *solver_large,
const char *solver_small,
const char *filename_large,
unsigned char **buf
)
{
uint8_t h, k;
long long int size, gensize;
char dataid[NISSY_SIZE_DATAID];
unsigned char *fulltable;
if (getdata(solver_large, &fulltable, filename_large) != 0) {
printf("Error reading full table.\n");
gensize = -1;
goto derivetable_error_nofree;
}
size = nissy_solverinfo(solver_small, dataid);
if (size == -1) {
printf("Error getting table size.\n");
gensize = -2;
goto derivetable_error;
}
if (parse_h48_hk(solver_small, &h, &k) != NISSY_OK) {
gensize = -3;
goto derivetable_error;
}
*buf = malloc(size);
gensize = gendata_h48_derive(h, fulltable, *buf);
if (gensize != size) {
printf("Error deriving table\n");
gensize = -4;
goto derivetable_error;
}
derivetable_error:
free(fulltable);
derivetable_error_nofree:
return gensize;
}
static int
getdata(
const char *solver,
unsigned char **buf,
const char *filename
) {
long long int size, sizeread;
FILE *f;
char dataid[NISSY_SIZE_DATAID];
if ((f = fopen(filename, "rb")) == NULL) {
printf("Table file not found, generating it.\n");
size = generatetable(solver, buf, dataid);
switch (size) {
case -1:
goto getdata_error_nofree;
case -2:
goto getdata_error;
default:
writetable(*buf, size, filename);
break;
}
} else {
printf("Reading tables from file %s\n", filename);
size = nissy_solverinfo(solver, dataid);
*buf = malloc(size);
sizeread = fread(*buf, size, 1, f);
fclose(f);
if (sizeread != 1) {
printf("Error reading table, stopping\n");
goto getdata_error;
}
}
return 0;
getdata_error:
free(*buf);
getdata_error_nofree:
return 1;
}
static void
gendata_run(
const char *solver,
uint64_t expected[static 21]
) {
long long int size;
char filename[1024], dataid[NISSY_SIZE_DATAID];
unsigned char *buf;
size = generatetable(solver, &buf, dataid);
sprintf(filename, "tables/%s", dataid);
switch (size) {
case -1:
return;
case -2:
goto gendata_run_finish;
default:
nissy_datainfo(size, buf);
printf("\n");
printf("Succesfully generated %lld bytes. "
"See above for details on the tables.\n", size);
/* TODO: check that the table is correct */
writetable(buf, size, filename);
break;
}
gendata_run_finish:
free(buf);
}
static void
derivedata_run(
const char *solver_large,
const char *solver_small,
const char *filename_large,
const char *filename_small
)
{
long long int size;
unsigned char *buf;
buf = NULL;
size = derivetable(solver_large, solver_small, filename_large, &buf);
switch (size) {
case -1:
return;
case -2:
goto derivedata_run_finish;
default:
nissy_datainfo(size, buf);
printf("\n");
printf("Succesfully generated %lld bytes. "
"See above for details on the tables.\n", size);
writetable(buf, size, filename_small);
break;
}
derivedata_run_finish:
free(buf);
}
|